net: rtl8192cu: fix wrong configuration in Makefile
[platform/kernel/linux-rpi.git] / drivers / scsi / scsi_transport_iscsi.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * iSCSI transport class definitions
4  *
5  * Copyright (C) IBM Corporation, 2004
6  * Copyright (C) Mike Christie, 2004 - 2005
7  * Copyright (C) Dmitry Yusupov, 2004 - 2005
8  * Copyright (C) Alex Aizman, 2004 - 2005
9  */
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/bsg-lib.h>
14 #include <linux/idr.h>
15 #include <net/tcp.h>
16 #include <scsi/scsi.h>
17 #include <scsi/scsi_host.h>
18 #include <scsi/scsi_device.h>
19 #include <scsi/scsi_transport.h>
20 #include <scsi/scsi_transport_iscsi.h>
21 #include <scsi/iscsi_if.h>
22 #include <scsi/scsi_cmnd.h>
23 #include <scsi/scsi_bsg_iscsi.h>
24
25 #define ISCSI_TRANSPORT_VERSION "2.0-870"
26
27 #define ISCSI_SEND_MAX_ALLOWED  10
28
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/iscsi.h>
31
32 /*
33  * Export tracepoint symbols to be used by other modules.
34  */
35 EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_conn);
36 EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_eh);
37 EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_session);
38 EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_tcp);
39 EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_sw_tcp);
40
41 static int dbg_session;
42 module_param_named(debug_session, dbg_session, int,
43                    S_IRUGO | S_IWUSR);
44 MODULE_PARM_DESC(debug_session,
45                  "Turn on debugging for sessions in scsi_transport_iscsi "
46                  "module. Set to 1 to turn on, and zero to turn off. Default "
47                  "is off.");
48
49 static int dbg_conn;
50 module_param_named(debug_conn, dbg_conn, int,
51                    S_IRUGO | S_IWUSR);
52 MODULE_PARM_DESC(debug_conn,
53                  "Turn on debugging for connections in scsi_transport_iscsi "
54                  "module. Set to 1 to turn on, and zero to turn off. Default "
55                  "is off.");
56
57 #define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...)              \
58         do {                                                            \
59                 if (dbg_session)                                        \
60                         iscsi_cls_session_printk(KERN_INFO, _session,   \
61                                                  "%s: " dbg_fmt,        \
62                                                  __func__, ##arg);      \
63                 iscsi_dbg_trace(trace_iscsi_dbg_trans_session,          \
64                                 &(_session)->dev,                       \
65                                 "%s " dbg_fmt, __func__, ##arg);        \
66         } while (0);
67
68 #define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...)                    \
69         do {                                                            \
70                 if (dbg_conn)                                           \
71                         iscsi_cls_conn_printk(KERN_INFO, _conn,         \
72                                               "%s: " dbg_fmt,           \
73                                               __func__, ##arg);         \
74                 iscsi_dbg_trace(trace_iscsi_dbg_trans_conn,             \
75                                 &(_conn)->dev,                          \
76                                 "%s " dbg_fmt, __func__, ##arg);        \
77         } while (0);
78
79 struct iscsi_internal {
80         struct scsi_transport_template t;
81         struct iscsi_transport *iscsi_transport;
82         struct list_head list;
83         struct device dev;
84
85         struct transport_container conn_cont;
86         struct transport_container session_cont;
87 };
88
89 static DEFINE_IDR(iscsi_ep_idr);
90 static DEFINE_MUTEX(iscsi_ep_idr_mutex);
91
92 static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
93 static struct workqueue_struct *iscsi_eh_timer_workq;
94
95 static struct workqueue_struct *iscsi_conn_cleanup_workq;
96
97 static DEFINE_IDA(iscsi_sess_ida);
98 /*
99  * list of registered transports and lock that must
100  * be held while accessing list. The iscsi_transport_lock must
101  * be acquired after the rx_queue_mutex.
102  */
103 static LIST_HEAD(iscsi_transports);
104 static DEFINE_SPINLOCK(iscsi_transport_lock);
105
106 #define to_iscsi_internal(tmpl) \
107         container_of(tmpl, struct iscsi_internal, t)
108
109 #define dev_to_iscsi_internal(_dev) \
110         container_of(_dev, struct iscsi_internal, dev)
111
112 static void iscsi_transport_release(struct device *dev)
113 {
114         struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
115         kfree(priv);
116 }
117
118 /*
119  * iscsi_transport_class represents the iscsi_transports that are
120  * registered.
121  */
122 static struct class iscsi_transport_class = {
123         .name = "iscsi_transport",
124         .dev_release = iscsi_transport_release,
125 };
126
127 static ssize_t
128 show_transport_handle(struct device *dev, struct device_attribute *attr,
129                       char *buf)
130 {
131         struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
132
133         if (!capable(CAP_SYS_ADMIN))
134                 return -EACCES;
135         return sysfs_emit(buf, "%llu\n",
136                   (unsigned long long)iscsi_handle(priv->iscsi_transport));
137 }
138 static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
139
140 #define show_transport_attr(name, format)                               \
141 static ssize_t                                                          \
142 show_transport_##name(struct device *dev,                               \
143                       struct device_attribute *attr,char *buf)          \
144 {                                                                       \
145         struct iscsi_internal *priv = dev_to_iscsi_internal(dev);       \
146         return sysfs_emit(buf, format"\n", priv->iscsi_transport->name);\
147 }                                                                       \
148 static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
149
150 show_transport_attr(caps, "0x%x");
151
152 static struct attribute *iscsi_transport_attrs[] = {
153         &dev_attr_handle.attr,
154         &dev_attr_caps.attr,
155         NULL,
156 };
157
158 static struct attribute_group iscsi_transport_group = {
159         .attrs = iscsi_transport_attrs,
160 };
161
162 /*
163  * iSCSI endpoint attrs
164  */
165 #define iscsi_dev_to_endpoint(_dev) \
166         container_of(_dev, struct iscsi_endpoint, dev)
167
168 #define ISCSI_ATTR(_prefix,_name,_mode,_show,_store)    \
169 struct device_attribute dev_attr_##_prefix##_##_name =  \
170         __ATTR(_name,_mode,_show,_store)
171
172 static void iscsi_endpoint_release(struct device *dev)
173 {
174         struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
175
176         mutex_lock(&iscsi_ep_idr_mutex);
177         idr_remove(&iscsi_ep_idr, ep->id);
178         mutex_unlock(&iscsi_ep_idr_mutex);
179
180         kfree(ep);
181 }
182
183 static struct class iscsi_endpoint_class = {
184         .name = "iscsi_endpoint",
185         .dev_release = iscsi_endpoint_release,
186 };
187
188 static ssize_t
189 show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
190 {
191         struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
192         return sysfs_emit(buf, "%d\n", ep->id);
193 }
194 static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
195
196 static struct attribute *iscsi_endpoint_attrs[] = {
197         &dev_attr_ep_handle.attr,
198         NULL,
199 };
200
201 static struct attribute_group iscsi_endpoint_group = {
202         .attrs = iscsi_endpoint_attrs,
203 };
204
205 struct iscsi_endpoint *
206 iscsi_create_endpoint(int dd_size)
207 {
208         struct iscsi_endpoint *ep;
209         int err, id;
210
211         ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
212         if (!ep)
213                 return NULL;
214
215         mutex_lock(&iscsi_ep_idr_mutex);
216
217         /*
218          * First endpoint id should be 1 to comply with user space
219          * applications (iscsid).
220          */
221         id = idr_alloc(&iscsi_ep_idr, ep, 1, -1, GFP_NOIO);
222         if (id < 0) {
223                 mutex_unlock(&iscsi_ep_idr_mutex);
224                 printk(KERN_ERR "Could not allocate endpoint ID. Error %d.\n",
225                        id);
226                 goto free_ep;
227         }
228         mutex_unlock(&iscsi_ep_idr_mutex);
229
230         ep->id = id;
231         ep->dev.class = &iscsi_endpoint_class;
232         dev_set_name(&ep->dev, "ep-%d", id);
233         err = device_register(&ep->dev);
234         if (err)
235                 goto put_dev;
236
237         err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
238         if (err)
239                 goto unregister_dev;
240
241         if (dd_size)
242                 ep->dd_data = &ep[1];
243         return ep;
244
245 unregister_dev:
246         device_unregister(&ep->dev);
247         return NULL;
248
249 put_dev:
250         mutex_lock(&iscsi_ep_idr_mutex);
251         idr_remove(&iscsi_ep_idr, id);
252         mutex_unlock(&iscsi_ep_idr_mutex);
253         put_device(&ep->dev);
254         return NULL;
255 free_ep:
256         kfree(ep);
257         return NULL;
258 }
259 EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
260
261 void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
262 {
263         sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
264         device_unregister(&ep->dev);
265 }
266 EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
267
268 void iscsi_put_endpoint(struct iscsi_endpoint *ep)
269 {
270         put_device(&ep->dev);
271 }
272 EXPORT_SYMBOL_GPL(iscsi_put_endpoint);
273
274 /**
275  * iscsi_lookup_endpoint - get ep from handle
276  * @handle: endpoint handle
277  *
278  * Caller must do a iscsi_put_endpoint.
279  */
280 struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
281 {
282         struct iscsi_endpoint *ep;
283
284         mutex_lock(&iscsi_ep_idr_mutex);
285         ep = idr_find(&iscsi_ep_idr, handle);
286         if (!ep)
287                 goto unlock;
288
289         get_device(&ep->dev);
290 unlock:
291         mutex_unlock(&iscsi_ep_idr_mutex);
292         return ep;
293 }
294 EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
295
296 /*
297  * Interface to display network param to sysfs
298  */
299
300 static void iscsi_iface_release(struct device *dev)
301 {
302         struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
303         struct device *parent = iface->dev.parent;
304
305         kfree(iface);
306         put_device(parent);
307 }
308
309
310 static struct class iscsi_iface_class = {
311         .name = "iscsi_iface",
312         .dev_release = iscsi_iface_release,
313 };
314
315 #define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store)  \
316 struct device_attribute dev_attr_##_prefix##_##_name =          \
317         __ATTR(_name, _mode, _show, _store)
318
319 /* iface attrs show */
320 #define iscsi_iface_attr_show(type, name, param_type, param)            \
321 static ssize_t                                                          \
322 show_##type##_##name(struct device *dev, struct device_attribute *attr, \
323                      char *buf)                                         \
324 {                                                                       \
325         struct iscsi_iface *iface = iscsi_dev_to_iface(dev);            \
326         struct iscsi_transport *t = iface->transport;                   \
327         return t->get_iface_param(iface, param_type, param, buf);       \
328 }                                                                       \
329
330 #define iscsi_iface_net_attr(type, name, param)                         \
331         iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param)       \
332 static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
333
334 #define iscsi_iface_attr(type, name, param)                             \
335         iscsi_iface_attr_show(type, name, ISCSI_IFACE_PARAM, param)     \
336 static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
337
338 /* generic read only ipv4 attribute */
339 iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
340 iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
341 iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
342 iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
343 iscsi_iface_net_attr(ipv4_iface, dhcp_dns_address_en,
344                      ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN);
345 iscsi_iface_net_attr(ipv4_iface, dhcp_slp_da_info_en,
346                      ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN);
347 iscsi_iface_net_attr(ipv4_iface, tos_en, ISCSI_NET_PARAM_IPV4_TOS_EN);
348 iscsi_iface_net_attr(ipv4_iface, tos, ISCSI_NET_PARAM_IPV4_TOS);
349 iscsi_iface_net_attr(ipv4_iface, grat_arp_en,
350                      ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN);
351 iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id_en,
352                      ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN);
353 iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id,
354                      ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID);
355 iscsi_iface_net_attr(ipv4_iface, dhcp_req_vendor_id_en,
356                      ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN);
357 iscsi_iface_net_attr(ipv4_iface, dhcp_use_vendor_id_en,
358                      ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN);
359 iscsi_iface_net_attr(ipv4_iface, dhcp_vendor_id,
360                      ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID);
361 iscsi_iface_net_attr(ipv4_iface, dhcp_learn_iqn_en,
362                      ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN);
363 iscsi_iface_net_attr(ipv4_iface, fragment_disable,
364                      ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE);
365 iscsi_iface_net_attr(ipv4_iface, incoming_forwarding_en,
366                      ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN);
367 iscsi_iface_net_attr(ipv4_iface, ttl, ISCSI_NET_PARAM_IPV4_TTL);
368
369 /* generic read only ipv6 attribute */
370 iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
371 iscsi_iface_net_attr(ipv6_iface, link_local_addr,
372                      ISCSI_NET_PARAM_IPV6_LINKLOCAL);
373 iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
374 iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
375                      ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
376 iscsi_iface_net_attr(ipv6_iface, link_local_autocfg,
377                      ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
378 iscsi_iface_net_attr(ipv6_iface, link_local_state,
379                      ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE);
380 iscsi_iface_net_attr(ipv6_iface, router_state,
381                      ISCSI_NET_PARAM_IPV6_ROUTER_STATE);
382 iscsi_iface_net_attr(ipv6_iface, grat_neighbor_adv_en,
383                      ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN);
384 iscsi_iface_net_attr(ipv6_iface, mld_en, ISCSI_NET_PARAM_IPV6_MLD_EN);
385 iscsi_iface_net_attr(ipv6_iface, flow_label, ISCSI_NET_PARAM_IPV6_FLOW_LABEL);
386 iscsi_iface_net_attr(ipv6_iface, traffic_class,
387                      ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS);
388 iscsi_iface_net_attr(ipv6_iface, hop_limit, ISCSI_NET_PARAM_IPV6_HOP_LIMIT);
389 iscsi_iface_net_attr(ipv6_iface, nd_reachable_tmo,
390                      ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO);
391 iscsi_iface_net_attr(ipv6_iface, nd_rexmit_time,
392                      ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME);
393 iscsi_iface_net_attr(ipv6_iface, nd_stale_tmo,
394                      ISCSI_NET_PARAM_IPV6_ND_STALE_TMO);
395 iscsi_iface_net_attr(ipv6_iface, dup_addr_detect_cnt,
396                      ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT);
397 iscsi_iface_net_attr(ipv6_iface, router_adv_link_mtu,
398                      ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU);
399
400 /* common read only iface attribute */
401 iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
402 iscsi_iface_net_attr(iface, vlan_id, ISCSI_NET_PARAM_VLAN_ID);
403 iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
404 iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
405 iscsi_iface_net_attr(iface, mtu, ISCSI_NET_PARAM_MTU);
406 iscsi_iface_net_attr(iface, port, ISCSI_NET_PARAM_PORT);
407 iscsi_iface_net_attr(iface, ipaddress_state, ISCSI_NET_PARAM_IPADDR_STATE);
408 iscsi_iface_net_attr(iface, delayed_ack_en, ISCSI_NET_PARAM_DELAYED_ACK_EN);
409 iscsi_iface_net_attr(iface, tcp_nagle_disable,
410                      ISCSI_NET_PARAM_TCP_NAGLE_DISABLE);
411 iscsi_iface_net_attr(iface, tcp_wsf_disable, ISCSI_NET_PARAM_TCP_WSF_DISABLE);
412 iscsi_iface_net_attr(iface, tcp_wsf, ISCSI_NET_PARAM_TCP_WSF);
413 iscsi_iface_net_attr(iface, tcp_timer_scale, ISCSI_NET_PARAM_TCP_TIMER_SCALE);
414 iscsi_iface_net_attr(iface, tcp_timestamp_en, ISCSI_NET_PARAM_TCP_TIMESTAMP_EN);
415 iscsi_iface_net_attr(iface, cache_id, ISCSI_NET_PARAM_CACHE_ID);
416 iscsi_iface_net_attr(iface, redirect_en, ISCSI_NET_PARAM_REDIRECT_EN);
417
418 /* common iscsi specific settings attributes */
419 iscsi_iface_attr(iface, def_taskmgmt_tmo, ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO);
420 iscsi_iface_attr(iface, header_digest, ISCSI_IFACE_PARAM_HDRDGST_EN);
421 iscsi_iface_attr(iface, data_digest, ISCSI_IFACE_PARAM_DATADGST_EN);
422 iscsi_iface_attr(iface, immediate_data, ISCSI_IFACE_PARAM_IMM_DATA_EN);
423 iscsi_iface_attr(iface, initial_r2t, ISCSI_IFACE_PARAM_INITIAL_R2T_EN);
424 iscsi_iface_attr(iface, data_seq_in_order,
425                  ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN);
426 iscsi_iface_attr(iface, data_pdu_in_order, ISCSI_IFACE_PARAM_PDU_INORDER_EN);
427 iscsi_iface_attr(iface, erl, ISCSI_IFACE_PARAM_ERL);
428 iscsi_iface_attr(iface, max_recv_dlength, ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH);
429 iscsi_iface_attr(iface, first_burst_len, ISCSI_IFACE_PARAM_FIRST_BURST);
430 iscsi_iface_attr(iface, max_outstanding_r2t, ISCSI_IFACE_PARAM_MAX_R2T);
431 iscsi_iface_attr(iface, max_burst_len, ISCSI_IFACE_PARAM_MAX_BURST);
432 iscsi_iface_attr(iface, chap_auth, ISCSI_IFACE_PARAM_CHAP_AUTH_EN);
433 iscsi_iface_attr(iface, bidi_chap, ISCSI_IFACE_PARAM_BIDI_CHAP_EN);
434 iscsi_iface_attr(iface, discovery_auth_optional,
435                  ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL);
436 iscsi_iface_attr(iface, discovery_logout,
437                  ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN);
438 iscsi_iface_attr(iface, strict_login_comp_en,
439                  ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN);
440 iscsi_iface_attr(iface, initiator_name, ISCSI_IFACE_PARAM_INITIATOR_NAME);
441
442 static umode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
443                                           struct attribute *attr, int i)
444 {
445         struct device *dev = container_of(kobj, struct device, kobj);
446         struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
447         struct iscsi_transport *t = iface->transport;
448         int param = -1;
449
450         if (attr == &dev_attr_iface_def_taskmgmt_tmo.attr)
451                 param = ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO;
452         else if (attr == &dev_attr_iface_header_digest.attr)
453                 param = ISCSI_IFACE_PARAM_HDRDGST_EN;
454         else if (attr == &dev_attr_iface_data_digest.attr)
455                 param = ISCSI_IFACE_PARAM_DATADGST_EN;
456         else if (attr == &dev_attr_iface_immediate_data.attr)
457                 param = ISCSI_IFACE_PARAM_IMM_DATA_EN;
458         else if (attr == &dev_attr_iface_initial_r2t.attr)
459                 param = ISCSI_IFACE_PARAM_INITIAL_R2T_EN;
460         else if (attr == &dev_attr_iface_data_seq_in_order.attr)
461                 param = ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN;
462         else if (attr == &dev_attr_iface_data_pdu_in_order.attr)
463                 param = ISCSI_IFACE_PARAM_PDU_INORDER_EN;
464         else if (attr == &dev_attr_iface_erl.attr)
465                 param = ISCSI_IFACE_PARAM_ERL;
466         else if (attr == &dev_attr_iface_max_recv_dlength.attr)
467                 param = ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH;
468         else if (attr == &dev_attr_iface_first_burst_len.attr)
469                 param = ISCSI_IFACE_PARAM_FIRST_BURST;
470         else if (attr == &dev_attr_iface_max_outstanding_r2t.attr)
471                 param = ISCSI_IFACE_PARAM_MAX_R2T;
472         else if (attr == &dev_attr_iface_max_burst_len.attr)
473                 param = ISCSI_IFACE_PARAM_MAX_BURST;
474         else if (attr == &dev_attr_iface_chap_auth.attr)
475                 param = ISCSI_IFACE_PARAM_CHAP_AUTH_EN;
476         else if (attr == &dev_attr_iface_bidi_chap.attr)
477                 param = ISCSI_IFACE_PARAM_BIDI_CHAP_EN;
478         else if (attr == &dev_attr_iface_discovery_auth_optional.attr)
479                 param = ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL;
480         else if (attr == &dev_attr_iface_discovery_logout.attr)
481                 param = ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN;
482         else if (attr == &dev_attr_iface_strict_login_comp_en.attr)
483                 param = ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN;
484         else if (attr == &dev_attr_iface_initiator_name.attr)
485                 param = ISCSI_IFACE_PARAM_INITIATOR_NAME;
486
487         if (param != -1)
488                 return t->attr_is_visible(ISCSI_IFACE_PARAM, param);
489
490         if (attr == &dev_attr_iface_enabled.attr)
491                 param = ISCSI_NET_PARAM_IFACE_ENABLE;
492         else if (attr == &dev_attr_iface_vlan_id.attr)
493                 param = ISCSI_NET_PARAM_VLAN_ID;
494         else if (attr == &dev_attr_iface_vlan_priority.attr)
495                 param = ISCSI_NET_PARAM_VLAN_PRIORITY;
496         else if (attr == &dev_attr_iface_vlan_enabled.attr)
497                 param = ISCSI_NET_PARAM_VLAN_ENABLED;
498         else if (attr == &dev_attr_iface_mtu.attr)
499                 param = ISCSI_NET_PARAM_MTU;
500         else if (attr == &dev_attr_iface_port.attr)
501                 param = ISCSI_NET_PARAM_PORT;
502         else if (attr == &dev_attr_iface_ipaddress_state.attr)
503                 param = ISCSI_NET_PARAM_IPADDR_STATE;
504         else if (attr == &dev_attr_iface_delayed_ack_en.attr)
505                 param = ISCSI_NET_PARAM_DELAYED_ACK_EN;
506         else if (attr == &dev_attr_iface_tcp_nagle_disable.attr)
507                 param = ISCSI_NET_PARAM_TCP_NAGLE_DISABLE;
508         else if (attr == &dev_attr_iface_tcp_wsf_disable.attr)
509                 param = ISCSI_NET_PARAM_TCP_WSF_DISABLE;
510         else if (attr == &dev_attr_iface_tcp_wsf.attr)
511                 param = ISCSI_NET_PARAM_TCP_WSF;
512         else if (attr == &dev_attr_iface_tcp_timer_scale.attr)
513                 param = ISCSI_NET_PARAM_TCP_TIMER_SCALE;
514         else if (attr == &dev_attr_iface_tcp_timestamp_en.attr)
515                 param = ISCSI_NET_PARAM_TCP_TIMESTAMP_EN;
516         else if (attr == &dev_attr_iface_cache_id.attr)
517                 param = ISCSI_NET_PARAM_CACHE_ID;
518         else if (attr == &dev_attr_iface_redirect_en.attr)
519                 param = ISCSI_NET_PARAM_REDIRECT_EN;
520         else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
521                 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
522                         param = ISCSI_NET_PARAM_IPV4_ADDR;
523                 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
524                         param = ISCSI_NET_PARAM_IPV4_GW;
525                 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
526                         param = ISCSI_NET_PARAM_IPV4_SUBNET;
527                 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
528                         param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
529                 else if (attr ==
530                          &dev_attr_ipv4_iface_dhcp_dns_address_en.attr)
531                         param = ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN;
532                 else if (attr ==
533                          &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr)
534                         param = ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN;
535                 else if (attr == &dev_attr_ipv4_iface_tos_en.attr)
536                         param = ISCSI_NET_PARAM_IPV4_TOS_EN;
537                 else if (attr == &dev_attr_ipv4_iface_tos.attr)
538                         param = ISCSI_NET_PARAM_IPV4_TOS;
539                 else if (attr == &dev_attr_ipv4_iface_grat_arp_en.attr)
540                         param = ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN;
541                 else if (attr ==
542                          &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr)
543                         param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN;
544                 else if (attr == &dev_attr_ipv4_iface_dhcp_alt_client_id.attr)
545                         param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID;
546                 else if (attr ==
547                          &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr)
548                         param = ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN;
549                 else if (attr ==
550                          &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr)
551                         param = ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN;
552                 else if (attr == &dev_attr_ipv4_iface_dhcp_vendor_id.attr)
553                         param = ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID;
554                 else if (attr ==
555                          &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr)
556                         param = ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN;
557                 else if (attr ==
558                          &dev_attr_ipv4_iface_fragment_disable.attr)
559                         param = ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE;
560                 else if (attr ==
561                          &dev_attr_ipv4_iface_incoming_forwarding_en.attr)
562                         param = ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN;
563                 else if (attr == &dev_attr_ipv4_iface_ttl.attr)
564                         param = ISCSI_NET_PARAM_IPV4_TTL;
565                 else
566                         return 0;
567         } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
568                 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
569                         param = ISCSI_NET_PARAM_IPV6_ADDR;
570                 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
571                         param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
572                 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
573                         param = ISCSI_NET_PARAM_IPV6_ROUTER;
574                 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
575                         param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
576                 else if (attr == &dev_attr_ipv6_iface_link_local_autocfg.attr)
577                         param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
578                 else if (attr == &dev_attr_ipv6_iface_link_local_state.attr)
579                         param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE;
580                 else if (attr == &dev_attr_ipv6_iface_router_state.attr)
581                         param = ISCSI_NET_PARAM_IPV6_ROUTER_STATE;
582                 else if (attr ==
583                          &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr)
584                         param = ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN;
585                 else if (attr == &dev_attr_ipv6_iface_mld_en.attr)
586                         param = ISCSI_NET_PARAM_IPV6_MLD_EN;
587                 else if (attr == &dev_attr_ipv6_iface_flow_label.attr)
588                         param = ISCSI_NET_PARAM_IPV6_FLOW_LABEL;
589                 else if (attr == &dev_attr_ipv6_iface_traffic_class.attr)
590                         param = ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS;
591                 else if (attr == &dev_attr_ipv6_iface_hop_limit.attr)
592                         param = ISCSI_NET_PARAM_IPV6_HOP_LIMIT;
593                 else if (attr == &dev_attr_ipv6_iface_nd_reachable_tmo.attr)
594                         param = ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO;
595                 else if (attr == &dev_attr_ipv6_iface_nd_rexmit_time.attr)
596                         param = ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME;
597                 else if (attr == &dev_attr_ipv6_iface_nd_stale_tmo.attr)
598                         param = ISCSI_NET_PARAM_IPV6_ND_STALE_TMO;
599                 else if (attr == &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr)
600                         param = ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT;
601                 else if (attr == &dev_attr_ipv6_iface_router_adv_link_mtu.attr)
602                         param = ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU;
603                 else
604                         return 0;
605         } else {
606                 WARN_ONCE(1, "Invalid iface attr");
607                 return 0;
608         }
609
610         return t->attr_is_visible(ISCSI_NET_PARAM, param);
611 }
612
613 static struct attribute *iscsi_iface_attrs[] = {
614         &dev_attr_iface_enabled.attr,
615         &dev_attr_iface_vlan_id.attr,
616         &dev_attr_iface_vlan_priority.attr,
617         &dev_attr_iface_vlan_enabled.attr,
618         &dev_attr_ipv4_iface_ipaddress.attr,
619         &dev_attr_ipv4_iface_gateway.attr,
620         &dev_attr_ipv4_iface_subnet.attr,
621         &dev_attr_ipv4_iface_bootproto.attr,
622         &dev_attr_ipv6_iface_ipaddress.attr,
623         &dev_attr_ipv6_iface_link_local_addr.attr,
624         &dev_attr_ipv6_iface_router_addr.attr,
625         &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
626         &dev_attr_ipv6_iface_link_local_autocfg.attr,
627         &dev_attr_iface_mtu.attr,
628         &dev_attr_iface_port.attr,
629         &dev_attr_iface_ipaddress_state.attr,
630         &dev_attr_iface_delayed_ack_en.attr,
631         &dev_attr_iface_tcp_nagle_disable.attr,
632         &dev_attr_iface_tcp_wsf_disable.attr,
633         &dev_attr_iface_tcp_wsf.attr,
634         &dev_attr_iface_tcp_timer_scale.attr,
635         &dev_attr_iface_tcp_timestamp_en.attr,
636         &dev_attr_iface_cache_id.attr,
637         &dev_attr_iface_redirect_en.attr,
638         &dev_attr_iface_def_taskmgmt_tmo.attr,
639         &dev_attr_iface_header_digest.attr,
640         &dev_attr_iface_data_digest.attr,
641         &dev_attr_iface_immediate_data.attr,
642         &dev_attr_iface_initial_r2t.attr,
643         &dev_attr_iface_data_seq_in_order.attr,
644         &dev_attr_iface_data_pdu_in_order.attr,
645         &dev_attr_iface_erl.attr,
646         &dev_attr_iface_max_recv_dlength.attr,
647         &dev_attr_iface_first_burst_len.attr,
648         &dev_attr_iface_max_outstanding_r2t.attr,
649         &dev_attr_iface_max_burst_len.attr,
650         &dev_attr_iface_chap_auth.attr,
651         &dev_attr_iface_bidi_chap.attr,
652         &dev_attr_iface_discovery_auth_optional.attr,
653         &dev_attr_iface_discovery_logout.attr,
654         &dev_attr_iface_strict_login_comp_en.attr,
655         &dev_attr_iface_initiator_name.attr,
656         &dev_attr_ipv4_iface_dhcp_dns_address_en.attr,
657         &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr,
658         &dev_attr_ipv4_iface_tos_en.attr,
659         &dev_attr_ipv4_iface_tos.attr,
660         &dev_attr_ipv4_iface_grat_arp_en.attr,
661         &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr,
662         &dev_attr_ipv4_iface_dhcp_alt_client_id.attr,
663         &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr,
664         &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr,
665         &dev_attr_ipv4_iface_dhcp_vendor_id.attr,
666         &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr,
667         &dev_attr_ipv4_iface_fragment_disable.attr,
668         &dev_attr_ipv4_iface_incoming_forwarding_en.attr,
669         &dev_attr_ipv4_iface_ttl.attr,
670         &dev_attr_ipv6_iface_link_local_state.attr,
671         &dev_attr_ipv6_iface_router_state.attr,
672         &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr,
673         &dev_attr_ipv6_iface_mld_en.attr,
674         &dev_attr_ipv6_iface_flow_label.attr,
675         &dev_attr_ipv6_iface_traffic_class.attr,
676         &dev_attr_ipv6_iface_hop_limit.attr,
677         &dev_attr_ipv6_iface_nd_reachable_tmo.attr,
678         &dev_attr_ipv6_iface_nd_rexmit_time.attr,
679         &dev_attr_ipv6_iface_nd_stale_tmo.attr,
680         &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr,
681         &dev_attr_ipv6_iface_router_adv_link_mtu.attr,
682         NULL,
683 };
684
685 static struct attribute_group iscsi_iface_group = {
686         .attrs = iscsi_iface_attrs,
687         .is_visible = iscsi_iface_attr_is_visible,
688 };
689
690 /* convert iscsi_ipaddress_state values to ascii string name */
691 static const struct {
692         enum iscsi_ipaddress_state      value;
693         char                            *name;
694 } iscsi_ipaddress_state_names[] = {
695         {ISCSI_IPDDRESS_STATE_UNCONFIGURED,     "Unconfigured" },
696         {ISCSI_IPDDRESS_STATE_ACQUIRING,        "Acquiring" },
697         {ISCSI_IPDDRESS_STATE_TENTATIVE,        "Tentative" },
698         {ISCSI_IPDDRESS_STATE_VALID,            "Valid" },
699         {ISCSI_IPDDRESS_STATE_DISABLING,        "Disabling" },
700         {ISCSI_IPDDRESS_STATE_INVALID,          "Invalid" },
701         {ISCSI_IPDDRESS_STATE_DEPRECATED,       "Deprecated" },
702 };
703
704 char *iscsi_get_ipaddress_state_name(enum iscsi_ipaddress_state port_state)
705 {
706         int i;
707         char *state = NULL;
708
709         for (i = 0; i < ARRAY_SIZE(iscsi_ipaddress_state_names); i++) {
710                 if (iscsi_ipaddress_state_names[i].value == port_state) {
711                         state = iscsi_ipaddress_state_names[i].name;
712                         break;
713                 }
714         }
715         return state;
716 }
717 EXPORT_SYMBOL_GPL(iscsi_get_ipaddress_state_name);
718
719 /* convert iscsi_router_state values to ascii string name */
720 static const struct {
721         enum iscsi_router_state value;
722         char                    *name;
723 } iscsi_router_state_names[] = {
724         {ISCSI_ROUTER_STATE_UNKNOWN,            "Unknown" },
725         {ISCSI_ROUTER_STATE_ADVERTISED,         "Advertised" },
726         {ISCSI_ROUTER_STATE_MANUAL,             "Manual" },
727         {ISCSI_ROUTER_STATE_STALE,              "Stale" },
728 };
729
730 char *iscsi_get_router_state_name(enum iscsi_router_state router_state)
731 {
732         int i;
733         char *state = NULL;
734
735         for (i = 0; i < ARRAY_SIZE(iscsi_router_state_names); i++) {
736                 if (iscsi_router_state_names[i].value == router_state) {
737                         state = iscsi_router_state_names[i].name;
738                         break;
739                 }
740         }
741         return state;
742 }
743 EXPORT_SYMBOL_GPL(iscsi_get_router_state_name);
744
745 struct iscsi_iface *
746 iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
747                    uint32_t iface_type, uint32_t iface_num, int dd_size)
748 {
749         struct iscsi_iface *iface;
750         int err;
751
752         iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
753         if (!iface)
754                 return NULL;
755
756         iface->transport = transport;
757         iface->iface_type = iface_type;
758         iface->iface_num = iface_num;
759         iface->dev.release = iscsi_iface_release;
760         iface->dev.class = &iscsi_iface_class;
761         /* parent reference released in iscsi_iface_release */
762         iface->dev.parent = get_device(&shost->shost_gendev);
763         if (iface_type == ISCSI_IFACE_TYPE_IPV4)
764                 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
765                              iface_num);
766         else
767                 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
768                              iface_num);
769
770         err = device_register(&iface->dev);
771         if (err)
772                 goto put_dev;
773
774         err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
775         if (err)
776                 goto unreg_iface;
777
778         if (dd_size)
779                 iface->dd_data = &iface[1];
780         return iface;
781
782 unreg_iface:
783         device_unregister(&iface->dev);
784         return NULL;
785
786 put_dev:
787         put_device(&iface->dev);
788         return NULL;
789 }
790 EXPORT_SYMBOL_GPL(iscsi_create_iface);
791
792 void iscsi_destroy_iface(struct iscsi_iface *iface)
793 {
794         sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
795         device_unregister(&iface->dev);
796 }
797 EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
798
799 /*
800  * Interface to display flash node params to sysfs
801  */
802
803 #define ISCSI_FLASHNODE_ATTR(_prefix, _name, _mode, _show, _store)      \
804 struct device_attribute dev_attr_##_prefix##_##_name =                  \
805         __ATTR(_name, _mode, _show, _store)
806
807 /* flash node session attrs show */
808 #define iscsi_flashnode_sess_attr_show(type, name, param)               \
809 static ssize_t                                                          \
810 show_##type##_##name(struct device *dev, struct device_attribute *attr, \
811                      char *buf)                                         \
812 {                                                                       \
813         struct iscsi_bus_flash_session *fnode_sess =                    \
814                                         iscsi_dev_to_flash_session(dev);\
815         struct iscsi_transport *t = fnode_sess->transport;              \
816         return t->get_flashnode_param(fnode_sess, param, buf);          \
817 }                                                                       \
818
819
820 #define iscsi_flashnode_sess_attr(type, name, param)                    \
821         iscsi_flashnode_sess_attr_show(type, name, param)               \
822 static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO,                        \
823                             show_##type##_##name, NULL);
824
825 /* Flash node session attributes */
826
827 iscsi_flashnode_sess_attr(fnode, auto_snd_tgt_disable,
828                           ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE);
829 iscsi_flashnode_sess_attr(fnode, discovery_session,
830                           ISCSI_FLASHNODE_DISCOVERY_SESS);
831 iscsi_flashnode_sess_attr(fnode, portal_type, ISCSI_FLASHNODE_PORTAL_TYPE);
832 iscsi_flashnode_sess_attr(fnode, entry_enable, ISCSI_FLASHNODE_ENTRY_EN);
833 iscsi_flashnode_sess_attr(fnode, immediate_data, ISCSI_FLASHNODE_IMM_DATA_EN);
834 iscsi_flashnode_sess_attr(fnode, initial_r2t, ISCSI_FLASHNODE_INITIAL_R2T_EN);
835 iscsi_flashnode_sess_attr(fnode, data_seq_in_order,
836                           ISCSI_FLASHNODE_DATASEQ_INORDER);
837 iscsi_flashnode_sess_attr(fnode, data_pdu_in_order,
838                           ISCSI_FLASHNODE_PDU_INORDER);
839 iscsi_flashnode_sess_attr(fnode, chap_auth, ISCSI_FLASHNODE_CHAP_AUTH_EN);
840 iscsi_flashnode_sess_attr(fnode, discovery_logout,
841                           ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN);
842 iscsi_flashnode_sess_attr(fnode, bidi_chap, ISCSI_FLASHNODE_BIDI_CHAP_EN);
843 iscsi_flashnode_sess_attr(fnode, discovery_auth_optional,
844                           ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL);
845 iscsi_flashnode_sess_attr(fnode, erl, ISCSI_FLASHNODE_ERL);
846 iscsi_flashnode_sess_attr(fnode, first_burst_len, ISCSI_FLASHNODE_FIRST_BURST);
847 iscsi_flashnode_sess_attr(fnode, def_time2wait, ISCSI_FLASHNODE_DEF_TIME2WAIT);
848 iscsi_flashnode_sess_attr(fnode, def_time2retain,
849                           ISCSI_FLASHNODE_DEF_TIME2RETAIN);
850 iscsi_flashnode_sess_attr(fnode, max_outstanding_r2t, ISCSI_FLASHNODE_MAX_R2T);
851 iscsi_flashnode_sess_attr(fnode, isid, ISCSI_FLASHNODE_ISID);
852 iscsi_flashnode_sess_attr(fnode, tsid, ISCSI_FLASHNODE_TSID);
853 iscsi_flashnode_sess_attr(fnode, max_burst_len, ISCSI_FLASHNODE_MAX_BURST);
854 iscsi_flashnode_sess_attr(fnode, def_taskmgmt_tmo,
855                           ISCSI_FLASHNODE_DEF_TASKMGMT_TMO);
856 iscsi_flashnode_sess_attr(fnode, targetalias, ISCSI_FLASHNODE_ALIAS);
857 iscsi_flashnode_sess_attr(fnode, targetname, ISCSI_FLASHNODE_NAME);
858 iscsi_flashnode_sess_attr(fnode, tpgt, ISCSI_FLASHNODE_TPGT);
859 iscsi_flashnode_sess_attr(fnode, discovery_parent_idx,
860                           ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX);
861 iscsi_flashnode_sess_attr(fnode, discovery_parent_type,
862                           ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE);
863 iscsi_flashnode_sess_attr(fnode, chap_in_idx, ISCSI_FLASHNODE_CHAP_IN_IDX);
864 iscsi_flashnode_sess_attr(fnode, chap_out_idx, ISCSI_FLASHNODE_CHAP_OUT_IDX);
865 iscsi_flashnode_sess_attr(fnode, username, ISCSI_FLASHNODE_USERNAME);
866 iscsi_flashnode_sess_attr(fnode, username_in, ISCSI_FLASHNODE_USERNAME_IN);
867 iscsi_flashnode_sess_attr(fnode, password, ISCSI_FLASHNODE_PASSWORD);
868 iscsi_flashnode_sess_attr(fnode, password_in, ISCSI_FLASHNODE_PASSWORD_IN);
869 iscsi_flashnode_sess_attr(fnode, is_boot_target, ISCSI_FLASHNODE_IS_BOOT_TGT);
870
871 static struct attribute *iscsi_flashnode_sess_attrs[] = {
872         &dev_attr_fnode_auto_snd_tgt_disable.attr,
873         &dev_attr_fnode_discovery_session.attr,
874         &dev_attr_fnode_portal_type.attr,
875         &dev_attr_fnode_entry_enable.attr,
876         &dev_attr_fnode_immediate_data.attr,
877         &dev_attr_fnode_initial_r2t.attr,
878         &dev_attr_fnode_data_seq_in_order.attr,
879         &dev_attr_fnode_data_pdu_in_order.attr,
880         &dev_attr_fnode_chap_auth.attr,
881         &dev_attr_fnode_discovery_logout.attr,
882         &dev_attr_fnode_bidi_chap.attr,
883         &dev_attr_fnode_discovery_auth_optional.attr,
884         &dev_attr_fnode_erl.attr,
885         &dev_attr_fnode_first_burst_len.attr,
886         &dev_attr_fnode_def_time2wait.attr,
887         &dev_attr_fnode_def_time2retain.attr,
888         &dev_attr_fnode_max_outstanding_r2t.attr,
889         &dev_attr_fnode_isid.attr,
890         &dev_attr_fnode_tsid.attr,
891         &dev_attr_fnode_max_burst_len.attr,
892         &dev_attr_fnode_def_taskmgmt_tmo.attr,
893         &dev_attr_fnode_targetalias.attr,
894         &dev_attr_fnode_targetname.attr,
895         &dev_attr_fnode_tpgt.attr,
896         &dev_attr_fnode_discovery_parent_idx.attr,
897         &dev_attr_fnode_discovery_parent_type.attr,
898         &dev_attr_fnode_chap_in_idx.attr,
899         &dev_attr_fnode_chap_out_idx.attr,
900         &dev_attr_fnode_username.attr,
901         &dev_attr_fnode_username_in.attr,
902         &dev_attr_fnode_password.attr,
903         &dev_attr_fnode_password_in.attr,
904         &dev_attr_fnode_is_boot_target.attr,
905         NULL,
906 };
907
908 static umode_t iscsi_flashnode_sess_attr_is_visible(struct kobject *kobj,
909                                                     struct attribute *attr,
910                                                     int i)
911 {
912         struct device *dev = container_of(kobj, struct device, kobj);
913         struct iscsi_bus_flash_session *fnode_sess =
914                                                 iscsi_dev_to_flash_session(dev);
915         struct iscsi_transport *t = fnode_sess->transport;
916         int param;
917
918         if (attr == &dev_attr_fnode_auto_snd_tgt_disable.attr) {
919                 param = ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE;
920         } else if (attr == &dev_attr_fnode_discovery_session.attr) {
921                 param = ISCSI_FLASHNODE_DISCOVERY_SESS;
922         } else if (attr == &dev_attr_fnode_portal_type.attr) {
923                 param = ISCSI_FLASHNODE_PORTAL_TYPE;
924         } else if (attr == &dev_attr_fnode_entry_enable.attr) {
925                 param = ISCSI_FLASHNODE_ENTRY_EN;
926         } else if (attr == &dev_attr_fnode_immediate_data.attr) {
927                 param = ISCSI_FLASHNODE_IMM_DATA_EN;
928         } else if (attr == &dev_attr_fnode_initial_r2t.attr) {
929                 param = ISCSI_FLASHNODE_INITIAL_R2T_EN;
930         } else if (attr == &dev_attr_fnode_data_seq_in_order.attr) {
931                 param = ISCSI_FLASHNODE_DATASEQ_INORDER;
932         } else if (attr == &dev_attr_fnode_data_pdu_in_order.attr) {
933                 param = ISCSI_FLASHNODE_PDU_INORDER;
934         } else if (attr == &dev_attr_fnode_chap_auth.attr) {
935                 param = ISCSI_FLASHNODE_CHAP_AUTH_EN;
936         } else if (attr == &dev_attr_fnode_discovery_logout.attr) {
937                 param = ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN;
938         } else if (attr == &dev_attr_fnode_bidi_chap.attr) {
939                 param = ISCSI_FLASHNODE_BIDI_CHAP_EN;
940         } else if (attr == &dev_attr_fnode_discovery_auth_optional.attr) {
941                 param = ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL;
942         } else if (attr == &dev_attr_fnode_erl.attr) {
943                 param = ISCSI_FLASHNODE_ERL;
944         } else if (attr == &dev_attr_fnode_first_burst_len.attr) {
945                 param = ISCSI_FLASHNODE_FIRST_BURST;
946         } else if (attr == &dev_attr_fnode_def_time2wait.attr) {
947                 param = ISCSI_FLASHNODE_DEF_TIME2WAIT;
948         } else if (attr == &dev_attr_fnode_def_time2retain.attr) {
949                 param = ISCSI_FLASHNODE_DEF_TIME2RETAIN;
950         } else if (attr == &dev_attr_fnode_max_outstanding_r2t.attr) {
951                 param = ISCSI_FLASHNODE_MAX_R2T;
952         } else if (attr == &dev_attr_fnode_isid.attr) {
953                 param = ISCSI_FLASHNODE_ISID;
954         } else if (attr == &dev_attr_fnode_tsid.attr) {
955                 param = ISCSI_FLASHNODE_TSID;
956         } else if (attr == &dev_attr_fnode_max_burst_len.attr) {
957                 param = ISCSI_FLASHNODE_MAX_BURST;
958         } else if (attr == &dev_attr_fnode_def_taskmgmt_tmo.attr) {
959                 param = ISCSI_FLASHNODE_DEF_TASKMGMT_TMO;
960         } else if (attr == &dev_attr_fnode_targetalias.attr) {
961                 param = ISCSI_FLASHNODE_ALIAS;
962         } else if (attr == &dev_attr_fnode_targetname.attr) {
963                 param = ISCSI_FLASHNODE_NAME;
964         } else if (attr == &dev_attr_fnode_tpgt.attr) {
965                 param = ISCSI_FLASHNODE_TPGT;
966         } else if (attr == &dev_attr_fnode_discovery_parent_idx.attr) {
967                 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX;
968         } else if (attr == &dev_attr_fnode_discovery_parent_type.attr) {
969                 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE;
970         } else if (attr == &dev_attr_fnode_chap_in_idx.attr) {
971                 param = ISCSI_FLASHNODE_CHAP_IN_IDX;
972         } else if (attr == &dev_attr_fnode_chap_out_idx.attr) {
973                 param = ISCSI_FLASHNODE_CHAP_OUT_IDX;
974         } else if (attr == &dev_attr_fnode_username.attr) {
975                 param = ISCSI_FLASHNODE_USERNAME;
976         } else if (attr == &dev_attr_fnode_username_in.attr) {
977                 param = ISCSI_FLASHNODE_USERNAME_IN;
978         } else if (attr == &dev_attr_fnode_password.attr) {
979                 param = ISCSI_FLASHNODE_PASSWORD;
980         } else if (attr == &dev_attr_fnode_password_in.attr) {
981                 param = ISCSI_FLASHNODE_PASSWORD_IN;
982         } else if (attr == &dev_attr_fnode_is_boot_target.attr) {
983                 param = ISCSI_FLASHNODE_IS_BOOT_TGT;
984         } else {
985                 WARN_ONCE(1, "Invalid flashnode session attr");
986                 return 0;
987         }
988
989         return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
990 }
991
992 static struct attribute_group iscsi_flashnode_sess_attr_group = {
993         .attrs = iscsi_flashnode_sess_attrs,
994         .is_visible = iscsi_flashnode_sess_attr_is_visible,
995 };
996
997 static const struct attribute_group *iscsi_flashnode_sess_attr_groups[] = {
998         &iscsi_flashnode_sess_attr_group,
999         NULL,
1000 };
1001
1002 static void iscsi_flashnode_sess_release(struct device *dev)
1003 {
1004         struct iscsi_bus_flash_session *fnode_sess =
1005                                                 iscsi_dev_to_flash_session(dev);
1006
1007         kfree(fnode_sess->targetname);
1008         kfree(fnode_sess->targetalias);
1009         kfree(fnode_sess->portal_type);
1010         kfree(fnode_sess);
1011 }
1012
1013 static const struct device_type iscsi_flashnode_sess_dev_type = {
1014         .name = "iscsi_flashnode_sess_dev_type",
1015         .groups = iscsi_flashnode_sess_attr_groups,
1016         .release = iscsi_flashnode_sess_release,
1017 };
1018
1019 /* flash node connection attrs show */
1020 #define iscsi_flashnode_conn_attr_show(type, name, param)               \
1021 static ssize_t                                                          \
1022 show_##type##_##name(struct device *dev, struct device_attribute *attr, \
1023                      char *buf)                                         \
1024 {                                                                       \
1025         struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);\
1026         struct iscsi_bus_flash_session *fnode_sess =                    \
1027                                 iscsi_flash_conn_to_flash_session(fnode_conn);\
1028         struct iscsi_transport *t = fnode_conn->transport;              \
1029         return t->get_flashnode_param(fnode_sess, param, buf);          \
1030 }                                                                       \
1031
1032
1033 #define iscsi_flashnode_conn_attr(type, name, param)                    \
1034         iscsi_flashnode_conn_attr_show(type, name, param)               \
1035 static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO,                        \
1036                             show_##type##_##name, NULL);
1037
1038 /* Flash node connection attributes */
1039
1040 iscsi_flashnode_conn_attr(fnode, is_fw_assigned_ipv6,
1041                           ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6);
1042 iscsi_flashnode_conn_attr(fnode, header_digest, ISCSI_FLASHNODE_HDR_DGST_EN);
1043 iscsi_flashnode_conn_attr(fnode, data_digest, ISCSI_FLASHNODE_DATA_DGST_EN);
1044 iscsi_flashnode_conn_attr(fnode, snack_req, ISCSI_FLASHNODE_SNACK_REQ_EN);
1045 iscsi_flashnode_conn_attr(fnode, tcp_timestamp_stat,
1046                           ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT);
1047 iscsi_flashnode_conn_attr(fnode, tcp_nagle_disable,
1048                           ISCSI_FLASHNODE_TCP_NAGLE_DISABLE);
1049 iscsi_flashnode_conn_attr(fnode, tcp_wsf_disable,
1050                           ISCSI_FLASHNODE_TCP_WSF_DISABLE);
1051 iscsi_flashnode_conn_attr(fnode, tcp_timer_scale,
1052                           ISCSI_FLASHNODE_TCP_TIMER_SCALE);
1053 iscsi_flashnode_conn_attr(fnode, tcp_timestamp_enable,
1054                           ISCSI_FLASHNODE_TCP_TIMESTAMP_EN);
1055 iscsi_flashnode_conn_attr(fnode, fragment_disable,
1056                           ISCSI_FLASHNODE_IP_FRAG_DISABLE);
1057 iscsi_flashnode_conn_attr(fnode, keepalive_tmo, ISCSI_FLASHNODE_KEEPALIVE_TMO);
1058 iscsi_flashnode_conn_attr(fnode, port, ISCSI_FLASHNODE_PORT);
1059 iscsi_flashnode_conn_attr(fnode, ipaddress, ISCSI_FLASHNODE_IPADDR);
1060 iscsi_flashnode_conn_attr(fnode, max_recv_dlength,
1061                           ISCSI_FLASHNODE_MAX_RECV_DLENGTH);
1062 iscsi_flashnode_conn_attr(fnode, max_xmit_dlength,
1063                           ISCSI_FLASHNODE_MAX_XMIT_DLENGTH);
1064 iscsi_flashnode_conn_attr(fnode, local_port, ISCSI_FLASHNODE_LOCAL_PORT);
1065 iscsi_flashnode_conn_attr(fnode, ipv4_tos, ISCSI_FLASHNODE_IPV4_TOS);
1066 iscsi_flashnode_conn_attr(fnode, ipv6_traffic_class, ISCSI_FLASHNODE_IPV6_TC);
1067 iscsi_flashnode_conn_attr(fnode, ipv6_flow_label,
1068                           ISCSI_FLASHNODE_IPV6_FLOW_LABEL);
1069 iscsi_flashnode_conn_attr(fnode, redirect_ipaddr,
1070                           ISCSI_FLASHNODE_REDIRECT_IPADDR);
1071 iscsi_flashnode_conn_attr(fnode, max_segment_size,
1072                           ISCSI_FLASHNODE_MAX_SEGMENT_SIZE);
1073 iscsi_flashnode_conn_attr(fnode, link_local_ipv6,
1074                           ISCSI_FLASHNODE_LINK_LOCAL_IPV6);
1075 iscsi_flashnode_conn_attr(fnode, tcp_xmit_wsf, ISCSI_FLASHNODE_TCP_XMIT_WSF);
1076 iscsi_flashnode_conn_attr(fnode, tcp_recv_wsf, ISCSI_FLASHNODE_TCP_RECV_WSF);
1077 iscsi_flashnode_conn_attr(fnode, statsn, ISCSI_FLASHNODE_STATSN);
1078 iscsi_flashnode_conn_attr(fnode, exp_statsn, ISCSI_FLASHNODE_EXP_STATSN);
1079
1080 static struct attribute *iscsi_flashnode_conn_attrs[] = {
1081         &dev_attr_fnode_is_fw_assigned_ipv6.attr,
1082         &dev_attr_fnode_header_digest.attr,
1083         &dev_attr_fnode_data_digest.attr,
1084         &dev_attr_fnode_snack_req.attr,
1085         &dev_attr_fnode_tcp_timestamp_stat.attr,
1086         &dev_attr_fnode_tcp_nagle_disable.attr,
1087         &dev_attr_fnode_tcp_wsf_disable.attr,
1088         &dev_attr_fnode_tcp_timer_scale.attr,
1089         &dev_attr_fnode_tcp_timestamp_enable.attr,
1090         &dev_attr_fnode_fragment_disable.attr,
1091         &dev_attr_fnode_max_recv_dlength.attr,
1092         &dev_attr_fnode_max_xmit_dlength.attr,
1093         &dev_attr_fnode_keepalive_tmo.attr,
1094         &dev_attr_fnode_port.attr,
1095         &dev_attr_fnode_ipaddress.attr,
1096         &dev_attr_fnode_redirect_ipaddr.attr,
1097         &dev_attr_fnode_max_segment_size.attr,
1098         &dev_attr_fnode_local_port.attr,
1099         &dev_attr_fnode_ipv4_tos.attr,
1100         &dev_attr_fnode_ipv6_traffic_class.attr,
1101         &dev_attr_fnode_ipv6_flow_label.attr,
1102         &dev_attr_fnode_link_local_ipv6.attr,
1103         &dev_attr_fnode_tcp_xmit_wsf.attr,
1104         &dev_attr_fnode_tcp_recv_wsf.attr,
1105         &dev_attr_fnode_statsn.attr,
1106         &dev_attr_fnode_exp_statsn.attr,
1107         NULL,
1108 };
1109
1110 static umode_t iscsi_flashnode_conn_attr_is_visible(struct kobject *kobj,
1111                                                     struct attribute *attr,
1112                                                     int i)
1113 {
1114         struct device *dev = container_of(kobj, struct device, kobj);
1115         struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
1116         struct iscsi_transport *t = fnode_conn->transport;
1117         int param;
1118
1119         if (attr == &dev_attr_fnode_is_fw_assigned_ipv6.attr) {
1120                 param = ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6;
1121         } else if (attr == &dev_attr_fnode_header_digest.attr) {
1122                 param = ISCSI_FLASHNODE_HDR_DGST_EN;
1123         } else if (attr == &dev_attr_fnode_data_digest.attr) {
1124                 param = ISCSI_FLASHNODE_DATA_DGST_EN;
1125         } else if (attr == &dev_attr_fnode_snack_req.attr) {
1126                 param = ISCSI_FLASHNODE_SNACK_REQ_EN;
1127         } else if (attr == &dev_attr_fnode_tcp_timestamp_stat.attr) {
1128                 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT;
1129         } else if (attr == &dev_attr_fnode_tcp_nagle_disable.attr) {
1130                 param = ISCSI_FLASHNODE_TCP_NAGLE_DISABLE;
1131         } else if (attr == &dev_attr_fnode_tcp_wsf_disable.attr) {
1132                 param = ISCSI_FLASHNODE_TCP_WSF_DISABLE;
1133         } else if (attr == &dev_attr_fnode_tcp_timer_scale.attr) {
1134                 param = ISCSI_FLASHNODE_TCP_TIMER_SCALE;
1135         } else if (attr == &dev_attr_fnode_tcp_timestamp_enable.attr) {
1136                 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_EN;
1137         } else if (attr == &dev_attr_fnode_fragment_disable.attr) {
1138                 param = ISCSI_FLASHNODE_IP_FRAG_DISABLE;
1139         } else if (attr == &dev_attr_fnode_max_recv_dlength.attr) {
1140                 param = ISCSI_FLASHNODE_MAX_RECV_DLENGTH;
1141         } else if (attr == &dev_attr_fnode_max_xmit_dlength.attr) {
1142                 param = ISCSI_FLASHNODE_MAX_XMIT_DLENGTH;
1143         } else if (attr == &dev_attr_fnode_keepalive_tmo.attr) {
1144                 param = ISCSI_FLASHNODE_KEEPALIVE_TMO;
1145         } else if (attr == &dev_attr_fnode_port.attr) {
1146                 param = ISCSI_FLASHNODE_PORT;
1147         } else if (attr == &dev_attr_fnode_ipaddress.attr) {
1148                 param = ISCSI_FLASHNODE_IPADDR;
1149         } else if (attr == &dev_attr_fnode_redirect_ipaddr.attr) {
1150                 param = ISCSI_FLASHNODE_REDIRECT_IPADDR;
1151         } else if (attr == &dev_attr_fnode_max_segment_size.attr) {
1152                 param = ISCSI_FLASHNODE_MAX_SEGMENT_SIZE;
1153         } else if (attr == &dev_attr_fnode_local_port.attr) {
1154                 param = ISCSI_FLASHNODE_LOCAL_PORT;
1155         } else if (attr == &dev_attr_fnode_ipv4_tos.attr) {
1156                 param = ISCSI_FLASHNODE_IPV4_TOS;
1157         } else if (attr == &dev_attr_fnode_ipv6_traffic_class.attr) {
1158                 param = ISCSI_FLASHNODE_IPV6_TC;
1159         } else if (attr == &dev_attr_fnode_ipv6_flow_label.attr) {
1160                 param = ISCSI_FLASHNODE_IPV6_FLOW_LABEL;
1161         } else if (attr == &dev_attr_fnode_link_local_ipv6.attr) {
1162                 param = ISCSI_FLASHNODE_LINK_LOCAL_IPV6;
1163         } else if (attr == &dev_attr_fnode_tcp_xmit_wsf.attr) {
1164                 param = ISCSI_FLASHNODE_TCP_XMIT_WSF;
1165         } else if (attr == &dev_attr_fnode_tcp_recv_wsf.attr) {
1166                 param = ISCSI_FLASHNODE_TCP_RECV_WSF;
1167         } else if (attr == &dev_attr_fnode_statsn.attr) {
1168                 param = ISCSI_FLASHNODE_STATSN;
1169         } else if (attr == &dev_attr_fnode_exp_statsn.attr) {
1170                 param = ISCSI_FLASHNODE_EXP_STATSN;
1171         } else {
1172                 WARN_ONCE(1, "Invalid flashnode connection attr");
1173                 return 0;
1174         }
1175
1176         return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
1177 }
1178
1179 static struct attribute_group iscsi_flashnode_conn_attr_group = {
1180         .attrs = iscsi_flashnode_conn_attrs,
1181         .is_visible = iscsi_flashnode_conn_attr_is_visible,
1182 };
1183
1184 static const struct attribute_group *iscsi_flashnode_conn_attr_groups[] = {
1185         &iscsi_flashnode_conn_attr_group,
1186         NULL,
1187 };
1188
1189 static void iscsi_flashnode_conn_release(struct device *dev)
1190 {
1191         struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
1192
1193         kfree(fnode_conn->ipaddress);
1194         kfree(fnode_conn->redirect_ipaddr);
1195         kfree(fnode_conn->link_local_ipv6_addr);
1196         kfree(fnode_conn);
1197 }
1198
1199 static const struct device_type iscsi_flashnode_conn_dev_type = {
1200         .name = "iscsi_flashnode_conn_dev_type",
1201         .groups = iscsi_flashnode_conn_attr_groups,
1202         .release = iscsi_flashnode_conn_release,
1203 };
1204
1205 static struct bus_type iscsi_flashnode_bus;
1206
1207 int iscsi_flashnode_bus_match(struct device *dev,
1208                                      struct device_driver *drv)
1209 {
1210         if (dev->bus == &iscsi_flashnode_bus)
1211                 return 1;
1212         return 0;
1213 }
1214 EXPORT_SYMBOL_GPL(iscsi_flashnode_bus_match);
1215
1216 static struct bus_type iscsi_flashnode_bus = {
1217         .name = "iscsi_flashnode",
1218         .match = &iscsi_flashnode_bus_match,
1219 };
1220
1221 /**
1222  * iscsi_create_flashnode_sess - Add flashnode session entry in sysfs
1223  * @shost: pointer to host data
1224  * @index: index of flashnode to add in sysfs
1225  * @transport: pointer to transport data
1226  * @dd_size: total size to allocate
1227  *
1228  * Adds a sysfs entry for the flashnode session attributes
1229  *
1230  * Returns:
1231  *  pointer to allocated flashnode sess on success
1232  *  %NULL on failure
1233  */
1234 struct iscsi_bus_flash_session *
1235 iscsi_create_flashnode_sess(struct Scsi_Host *shost, int index,
1236                             struct iscsi_transport *transport,
1237                             int dd_size)
1238 {
1239         struct iscsi_bus_flash_session *fnode_sess;
1240         int err;
1241
1242         fnode_sess = kzalloc(sizeof(*fnode_sess) + dd_size, GFP_KERNEL);
1243         if (!fnode_sess)
1244                 return NULL;
1245
1246         fnode_sess->transport = transport;
1247         fnode_sess->target_id = index;
1248         fnode_sess->dev.type = &iscsi_flashnode_sess_dev_type;
1249         fnode_sess->dev.bus = &iscsi_flashnode_bus;
1250         fnode_sess->dev.parent = &shost->shost_gendev;
1251         dev_set_name(&fnode_sess->dev, "flashnode_sess-%u:%u",
1252                      shost->host_no, index);
1253
1254         err = device_register(&fnode_sess->dev);
1255         if (err)
1256                 goto put_dev;
1257
1258         if (dd_size)
1259                 fnode_sess->dd_data = &fnode_sess[1];
1260
1261         return fnode_sess;
1262
1263 put_dev:
1264         put_device(&fnode_sess->dev);
1265         return NULL;
1266 }
1267 EXPORT_SYMBOL_GPL(iscsi_create_flashnode_sess);
1268
1269 /**
1270  * iscsi_create_flashnode_conn - Add flashnode conn entry in sysfs
1271  * @shost: pointer to host data
1272  * @fnode_sess: pointer to the parent flashnode session entry
1273  * @transport: pointer to transport data
1274  * @dd_size: total size to allocate
1275  *
1276  * Adds a sysfs entry for the flashnode connection attributes
1277  *
1278  * Returns:
1279  *  pointer to allocated flashnode conn on success
1280  *  %NULL on failure
1281  */
1282 struct iscsi_bus_flash_conn *
1283 iscsi_create_flashnode_conn(struct Scsi_Host *shost,
1284                             struct iscsi_bus_flash_session *fnode_sess,
1285                             struct iscsi_transport *transport,
1286                             int dd_size)
1287 {
1288         struct iscsi_bus_flash_conn *fnode_conn;
1289         int err;
1290
1291         fnode_conn = kzalloc(sizeof(*fnode_conn) + dd_size, GFP_KERNEL);
1292         if (!fnode_conn)
1293                 return NULL;
1294
1295         fnode_conn->transport = transport;
1296         fnode_conn->dev.type = &iscsi_flashnode_conn_dev_type;
1297         fnode_conn->dev.bus = &iscsi_flashnode_bus;
1298         fnode_conn->dev.parent = &fnode_sess->dev;
1299         dev_set_name(&fnode_conn->dev, "flashnode_conn-%u:%u:0",
1300                      shost->host_no, fnode_sess->target_id);
1301
1302         err = device_register(&fnode_conn->dev);
1303         if (err)
1304                 goto put_dev;
1305
1306         if (dd_size)
1307                 fnode_conn->dd_data = &fnode_conn[1];
1308
1309         return fnode_conn;
1310
1311 put_dev:
1312         put_device(&fnode_conn->dev);
1313         return NULL;
1314 }
1315 EXPORT_SYMBOL_GPL(iscsi_create_flashnode_conn);
1316
1317 /**
1318  * iscsi_is_flashnode_conn_dev - verify passed device is to be flashnode conn
1319  * @dev: device to verify
1320  * @data: pointer to data containing value to use for verification
1321  *
1322  * Verifies if the passed device is flashnode conn device
1323  *
1324  * Returns:
1325  *  1 on success
1326  *  0 on failure
1327  */
1328 static int iscsi_is_flashnode_conn_dev(struct device *dev, void *data)
1329 {
1330         return dev->bus == &iscsi_flashnode_bus;
1331 }
1332
1333 static int iscsi_destroy_flashnode_conn(struct iscsi_bus_flash_conn *fnode_conn)
1334 {
1335         device_unregister(&fnode_conn->dev);
1336         return 0;
1337 }
1338
1339 static int flashnode_match_index(struct device *dev, void *data)
1340 {
1341         struct iscsi_bus_flash_session *fnode_sess = NULL;
1342         int ret = 0;
1343
1344         if (!iscsi_flashnode_bus_match(dev, NULL))
1345                 goto exit_match_index;
1346
1347         fnode_sess = iscsi_dev_to_flash_session(dev);
1348         ret = (fnode_sess->target_id == *((int *)data)) ? 1 : 0;
1349
1350 exit_match_index:
1351         return ret;
1352 }
1353
1354 /**
1355  * iscsi_get_flashnode_by_index -finds flashnode session entry by index
1356  * @shost: pointer to host data
1357  * @idx: index to match
1358  *
1359  * Finds the flashnode session object for the passed index
1360  *
1361  * Returns:
1362  *  pointer to found flashnode session object on success
1363  *  %NULL on failure
1364  */
1365 static struct iscsi_bus_flash_session *
1366 iscsi_get_flashnode_by_index(struct Scsi_Host *shost, uint32_t idx)
1367 {
1368         struct iscsi_bus_flash_session *fnode_sess = NULL;
1369         struct device *dev;
1370
1371         dev = device_find_child(&shost->shost_gendev, &idx,
1372                                 flashnode_match_index);
1373         if (dev)
1374                 fnode_sess = iscsi_dev_to_flash_session(dev);
1375
1376         return fnode_sess;
1377 }
1378
1379 /**
1380  * iscsi_find_flashnode_sess - finds flashnode session entry
1381  * @shost: pointer to host data
1382  * @data: pointer to data containing value to use for comparison
1383  * @fn: function pointer that does actual comparison
1384  *
1385  * Finds the flashnode session object comparing the data passed using logic
1386  * defined in passed function pointer
1387  *
1388  * Returns:
1389  *  pointer to found flashnode session device object on success
1390  *  %NULL on failure
1391  */
1392 struct device *
1393 iscsi_find_flashnode_sess(struct Scsi_Host *shost, void *data,
1394                           int (*fn)(struct device *dev, void *data))
1395 {
1396         return device_find_child(&shost->shost_gendev, data, fn);
1397 }
1398 EXPORT_SYMBOL_GPL(iscsi_find_flashnode_sess);
1399
1400 /**
1401  * iscsi_find_flashnode_conn - finds flashnode connection entry
1402  * @fnode_sess: pointer to parent flashnode session entry
1403  *
1404  * Finds the flashnode connection object comparing the data passed using logic
1405  * defined in passed function pointer
1406  *
1407  * Returns:
1408  *  pointer to found flashnode connection device object on success
1409  *  %NULL on failure
1410  */
1411 struct device *
1412 iscsi_find_flashnode_conn(struct iscsi_bus_flash_session *fnode_sess)
1413 {
1414         return device_find_child(&fnode_sess->dev, NULL,
1415                                  iscsi_is_flashnode_conn_dev);
1416 }
1417 EXPORT_SYMBOL_GPL(iscsi_find_flashnode_conn);
1418
1419 static int iscsi_iter_destroy_flashnode_conn_fn(struct device *dev, void *data)
1420 {
1421         if (!iscsi_is_flashnode_conn_dev(dev, NULL))
1422                 return 0;
1423
1424         return iscsi_destroy_flashnode_conn(iscsi_dev_to_flash_conn(dev));
1425 }
1426
1427 /**
1428  * iscsi_destroy_flashnode_sess - destroy flashnode session entry
1429  * @fnode_sess: pointer to flashnode session entry to be destroyed
1430  *
1431  * Deletes the flashnode session entry and all children flashnode connection
1432  * entries from sysfs
1433  */
1434 void iscsi_destroy_flashnode_sess(struct iscsi_bus_flash_session *fnode_sess)
1435 {
1436         int err;
1437
1438         err = device_for_each_child(&fnode_sess->dev, NULL,
1439                                     iscsi_iter_destroy_flashnode_conn_fn);
1440         if (err)
1441                 pr_err("Could not delete all connections for %s. Error %d.\n",
1442                        fnode_sess->dev.kobj.name, err);
1443
1444         device_unregister(&fnode_sess->dev);
1445 }
1446 EXPORT_SYMBOL_GPL(iscsi_destroy_flashnode_sess);
1447
1448 static int iscsi_iter_destroy_flashnode_fn(struct device *dev, void *data)
1449 {
1450         if (!iscsi_flashnode_bus_match(dev, NULL))
1451                 return 0;
1452
1453         iscsi_destroy_flashnode_sess(iscsi_dev_to_flash_session(dev));
1454         return 0;
1455 }
1456
1457 /**
1458  * iscsi_destroy_all_flashnode - destroy all flashnode session entries
1459  * @shost: pointer to host data
1460  *
1461  * Destroys all the flashnode session entries and all corresponding children
1462  * flashnode connection entries from sysfs
1463  */
1464 void iscsi_destroy_all_flashnode(struct Scsi_Host *shost)
1465 {
1466         device_for_each_child(&shost->shost_gendev, NULL,
1467                               iscsi_iter_destroy_flashnode_fn);
1468 }
1469 EXPORT_SYMBOL_GPL(iscsi_destroy_all_flashnode);
1470
1471 /*
1472  * BSG support
1473  */
1474 /**
1475  * iscsi_bsg_host_dispatch - Dispatch command to LLD.
1476  * @job: bsg job to be processed
1477  */
1478 static int iscsi_bsg_host_dispatch(struct bsg_job *job)
1479 {
1480         struct Scsi_Host *shost = iscsi_job_to_shost(job);
1481         struct iscsi_bsg_request *req = job->request;
1482         struct iscsi_bsg_reply *reply = job->reply;
1483         struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1484         int cmdlen = sizeof(uint32_t);  /* start with length of msgcode */
1485         int ret;
1486
1487         /* check if we have the msgcode value at least */
1488         if (job->request_len < sizeof(uint32_t)) {
1489                 ret = -ENOMSG;
1490                 goto fail_host_msg;
1491         }
1492
1493         /* Validate the host command */
1494         switch (req->msgcode) {
1495         case ISCSI_BSG_HST_VENDOR:
1496                 cmdlen += sizeof(struct iscsi_bsg_host_vendor);
1497                 if ((shost->hostt->vendor_id == 0L) ||
1498                     (req->rqst_data.h_vendor.vendor_id !=
1499                         shost->hostt->vendor_id)) {
1500                         ret = -ESRCH;
1501                         goto fail_host_msg;
1502                 }
1503                 break;
1504         default:
1505                 ret = -EBADR;
1506                 goto fail_host_msg;
1507         }
1508
1509         /* check if we really have all the request data needed */
1510         if (job->request_len < cmdlen) {
1511                 ret = -ENOMSG;
1512                 goto fail_host_msg;
1513         }
1514
1515         ret = i->iscsi_transport->bsg_request(job);
1516         if (!ret)
1517                 return 0;
1518
1519 fail_host_msg:
1520         /* return the errno failure code as the only status */
1521         BUG_ON(job->reply_len < sizeof(uint32_t));
1522         reply->reply_payload_rcv_len = 0;
1523         reply->result = ret;
1524         job->reply_len = sizeof(uint32_t);
1525         bsg_job_done(job, ret, 0);
1526         return 0;
1527 }
1528
1529 /**
1530  * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
1531  * @shost: shost for iscsi_host
1532  * @ihost: iscsi_cls_host adding the structures to
1533  */
1534 static int
1535 iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
1536 {
1537         struct device *dev = &shost->shost_gendev;
1538         struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1539         struct request_queue *q;
1540         char bsg_name[20];
1541
1542         if (!i->iscsi_transport->bsg_request)
1543                 return -ENOTSUPP;
1544
1545         snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
1546         q = bsg_setup_queue(dev, bsg_name, iscsi_bsg_host_dispatch, NULL, 0);
1547         if (IS_ERR(q)) {
1548                 shost_printk(KERN_ERR, shost, "bsg interface failed to "
1549                              "initialize - no request queue\n");
1550                 return PTR_ERR(q);
1551         }
1552         __scsi_init_queue(shost, q);
1553
1554         ihost->bsg_q = q;
1555         return 0;
1556 }
1557
1558 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
1559                             struct device *cdev)
1560 {
1561         struct Scsi_Host *shost = dev_to_shost(dev);
1562         struct iscsi_cls_host *ihost = shost->shost_data;
1563
1564         memset(ihost, 0, sizeof(*ihost));
1565         atomic_set(&ihost->nr_scans, 0);
1566         mutex_init(&ihost->mutex);
1567
1568         iscsi_bsg_host_add(shost, ihost);
1569         /* ignore any bsg add error - we just can't do sgio */
1570
1571         return 0;
1572 }
1573
1574 static int iscsi_remove_host(struct transport_container *tc,
1575                              struct device *dev, struct device *cdev)
1576 {
1577         struct Scsi_Host *shost = dev_to_shost(dev);
1578         struct iscsi_cls_host *ihost = shost->shost_data;
1579
1580         bsg_remove_queue(ihost->bsg_q);
1581         return 0;
1582 }
1583
1584 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
1585                                "iscsi_host",
1586                                iscsi_setup_host,
1587                                iscsi_remove_host,
1588                                NULL);
1589
1590 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
1591                                "iscsi_session",
1592                                NULL,
1593                                NULL,
1594                                NULL);
1595
1596 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
1597                                "iscsi_connection",
1598                                NULL,
1599                                NULL,
1600                                NULL);
1601
1602 static struct sock *nls;
1603 static DEFINE_MUTEX(rx_queue_mutex);
1604
1605 static LIST_HEAD(sesslist);
1606 static DEFINE_SPINLOCK(sesslock);
1607 static LIST_HEAD(connlist);
1608 static LIST_HEAD(connlist_err);
1609 static DEFINE_SPINLOCK(connlock);
1610
1611 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
1612 {
1613         struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
1614         return sess->sid;
1615 }
1616
1617 /*
1618  * Returns the matching session to a given sid
1619  */
1620 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
1621 {
1622         unsigned long flags;
1623         struct iscsi_cls_session *sess;
1624
1625         spin_lock_irqsave(&sesslock, flags);
1626         list_for_each_entry(sess, &sesslist, sess_list) {
1627                 if (sess->sid == sid) {
1628                         spin_unlock_irqrestore(&sesslock, flags);
1629                         return sess;
1630                 }
1631         }
1632         spin_unlock_irqrestore(&sesslock, flags);
1633         return NULL;
1634 }
1635
1636 /*
1637  * Returns the matching connection to a given sid / cid tuple
1638  */
1639 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
1640 {
1641         unsigned long flags;
1642         struct iscsi_cls_conn *conn;
1643
1644         spin_lock_irqsave(&connlock, flags);
1645         list_for_each_entry(conn, &connlist, conn_list) {
1646                 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
1647                         spin_unlock_irqrestore(&connlock, flags);
1648                         return conn;
1649                 }
1650         }
1651         spin_unlock_irqrestore(&connlock, flags);
1652         return NULL;
1653 }
1654
1655 /*
1656  * The following functions can be used by LLDs that allocate
1657  * their own scsi_hosts or by software iscsi LLDs
1658  */
1659 static struct {
1660         int value;
1661         char *name;
1662 } iscsi_session_state_names[] = {
1663         { ISCSI_SESSION_LOGGED_IN,      "LOGGED_IN" },
1664         { ISCSI_SESSION_FAILED,         "FAILED" },
1665         { ISCSI_SESSION_FREE,           "FREE" },
1666 };
1667
1668 static const char *iscsi_session_state_name(int state)
1669 {
1670         int i;
1671         char *name = NULL;
1672
1673         for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
1674                 if (iscsi_session_state_names[i].value == state) {
1675                         name = iscsi_session_state_names[i].name;
1676                         break;
1677                 }
1678         }
1679         return name;
1680 }
1681
1682 static char *iscsi_session_target_state_name[] = {
1683         [ISCSI_SESSION_TARGET_UNBOUND]   = "UNBOUND",
1684         [ISCSI_SESSION_TARGET_ALLOCATED] = "ALLOCATED",
1685         [ISCSI_SESSION_TARGET_SCANNED]   = "SCANNED",
1686         [ISCSI_SESSION_TARGET_UNBINDING] = "UNBINDING",
1687 };
1688
1689 int iscsi_session_chkready(struct iscsi_cls_session *session)
1690 {
1691         int err;
1692
1693         switch (session->state) {
1694         case ISCSI_SESSION_LOGGED_IN:
1695                 err = 0;
1696                 break;
1697         case ISCSI_SESSION_FAILED:
1698                 err = DID_IMM_RETRY << 16;
1699                 break;
1700         case ISCSI_SESSION_FREE:
1701                 err = DID_TRANSPORT_FAILFAST << 16;
1702                 break;
1703         default:
1704                 err = DID_NO_CONNECT << 16;
1705                 break;
1706         }
1707         return err;
1708 }
1709 EXPORT_SYMBOL_GPL(iscsi_session_chkready);
1710
1711 int iscsi_is_session_online(struct iscsi_cls_session *session)
1712 {
1713         unsigned long flags;
1714         int ret = 0;
1715
1716         spin_lock_irqsave(&session->lock, flags);
1717         if (session->state == ISCSI_SESSION_LOGGED_IN)
1718                 ret = 1;
1719         spin_unlock_irqrestore(&session->lock, flags);
1720         return ret;
1721 }
1722 EXPORT_SYMBOL_GPL(iscsi_is_session_online);
1723
1724 static void iscsi_session_release(struct device *dev)
1725 {
1726         struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
1727         struct Scsi_Host *shost;
1728
1729         shost = iscsi_session_to_shost(session);
1730         scsi_host_put(shost);
1731         ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
1732         kfree(session);
1733 }
1734
1735 int iscsi_is_session_dev(const struct device *dev)
1736 {
1737         return dev->release == iscsi_session_release;
1738 }
1739 EXPORT_SYMBOL_GPL(iscsi_is_session_dev);
1740
1741 static int iscsi_iter_session_fn(struct device *dev, void *data)
1742 {
1743         void (* fn) (struct iscsi_cls_session *) = data;
1744
1745         if (!iscsi_is_session_dev(dev))
1746                 return 0;
1747         fn(iscsi_dev_to_session(dev));
1748         return 0;
1749 }
1750
1751 void iscsi_host_for_each_session(struct Scsi_Host *shost,
1752                                  void (*fn)(struct iscsi_cls_session *))
1753 {
1754         device_for_each_child(&shost->shost_gendev, fn,
1755                               iscsi_iter_session_fn);
1756 }
1757 EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
1758
1759 /**
1760  * iscsi_scan_finished - helper to report when running scans are done
1761  * @shost: scsi host
1762  * @time: scan run time
1763  *
1764  * This function can be used by drives like qla4xxx to report to the scsi
1765  * layer when the scans it kicked off at module load time are done.
1766  */
1767 int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
1768 {
1769         struct iscsi_cls_host *ihost = shost->shost_data;
1770         /*
1771          * qla4xxx will have kicked off some session unblocks before calling
1772          * scsi_scan_host, so just wait for them to complete.
1773          */
1774         return !atomic_read(&ihost->nr_scans);
1775 }
1776 EXPORT_SYMBOL_GPL(iscsi_scan_finished);
1777
1778 struct iscsi_scan_data {
1779         unsigned int channel;
1780         unsigned int id;
1781         u64 lun;
1782         enum scsi_scan_mode rescan;
1783 };
1784
1785 static int iscsi_user_scan_session(struct device *dev, void *data)
1786 {
1787         struct iscsi_scan_data *scan_data = data;
1788         struct iscsi_cls_session *session;
1789         struct Scsi_Host *shost;
1790         struct iscsi_cls_host *ihost;
1791         unsigned long flags;
1792         unsigned int id;
1793
1794         if (!iscsi_is_session_dev(dev))
1795                 return 0;
1796
1797         session = iscsi_dev_to_session(dev);
1798
1799         ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
1800
1801         shost = iscsi_session_to_shost(session);
1802         ihost = shost->shost_data;
1803
1804         mutex_lock(&ihost->mutex);
1805         spin_lock_irqsave(&session->lock, flags);
1806         if (session->state != ISCSI_SESSION_LOGGED_IN) {
1807                 spin_unlock_irqrestore(&session->lock, flags);
1808                 goto user_scan_exit;
1809         }
1810         id = session->target_id;
1811         spin_unlock_irqrestore(&session->lock, flags);
1812
1813         if (id != ISCSI_MAX_TARGET) {
1814                 if ((scan_data->channel == SCAN_WILD_CARD ||
1815                      scan_data->channel == 0) &&
1816                     (scan_data->id == SCAN_WILD_CARD ||
1817                      scan_data->id == id)) {
1818                         scsi_scan_target(&session->dev, 0, id,
1819                                          scan_data->lun, scan_data->rescan);
1820                         spin_lock_irqsave(&session->lock, flags);
1821                         session->target_state = ISCSI_SESSION_TARGET_SCANNED;
1822                         spin_unlock_irqrestore(&session->lock, flags);
1823                 }
1824         }
1825
1826 user_scan_exit:
1827         mutex_unlock(&ihost->mutex);
1828         ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
1829         return 0;
1830 }
1831
1832 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
1833                            uint id, u64 lun)
1834 {
1835         struct iscsi_scan_data scan_data;
1836
1837         scan_data.channel = channel;
1838         scan_data.id = id;
1839         scan_data.lun = lun;
1840         scan_data.rescan = SCSI_SCAN_MANUAL;
1841
1842         return device_for_each_child(&shost->shost_gendev, &scan_data,
1843                                      iscsi_user_scan_session);
1844 }
1845
1846 static void iscsi_scan_session(struct work_struct *work)
1847 {
1848         struct iscsi_cls_session *session =
1849                         container_of(work, struct iscsi_cls_session, scan_work);
1850         struct Scsi_Host *shost = iscsi_session_to_shost(session);
1851         struct iscsi_cls_host *ihost = shost->shost_data;
1852         struct iscsi_scan_data scan_data;
1853
1854         scan_data.channel = 0;
1855         scan_data.id = SCAN_WILD_CARD;
1856         scan_data.lun = SCAN_WILD_CARD;
1857         scan_data.rescan = SCSI_SCAN_RESCAN;
1858
1859         iscsi_user_scan_session(&session->dev, &scan_data);
1860         atomic_dec(&ihost->nr_scans);
1861 }
1862
1863 /**
1864  * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
1865  * @cmd: scsi cmd passed to scsi eh handler
1866  *
1867  * If the session is down this function will wait for the recovery
1868  * timer to fire or for the session to be logged back in. If the
1869  * recovery timer fires then FAST_IO_FAIL is returned. The caller
1870  * should pass this error value to the scsi eh.
1871  */
1872 int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
1873 {
1874         struct iscsi_cls_session *session =
1875                         starget_to_session(scsi_target(cmd->device));
1876         unsigned long flags;
1877         int ret = 0;
1878
1879         spin_lock_irqsave(&session->lock, flags);
1880         while (session->state != ISCSI_SESSION_LOGGED_IN) {
1881                 if (session->state == ISCSI_SESSION_FREE) {
1882                         ret = FAST_IO_FAIL;
1883                         break;
1884                 }
1885                 spin_unlock_irqrestore(&session->lock, flags);
1886                 msleep(1000);
1887                 spin_lock_irqsave(&session->lock, flags);
1888         }
1889         spin_unlock_irqrestore(&session->lock, flags);
1890         return ret;
1891 }
1892 EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
1893
1894 static void session_recovery_timedout(struct work_struct *work)
1895 {
1896         struct iscsi_cls_session *session =
1897                 container_of(work, struct iscsi_cls_session,
1898                              recovery_work.work);
1899         unsigned long flags;
1900
1901         iscsi_cls_session_printk(KERN_INFO, session,
1902                                  "session recovery timed out after %d secs\n",
1903                                  session->recovery_tmo);
1904
1905         spin_lock_irqsave(&session->lock, flags);
1906         switch (session->state) {
1907         case ISCSI_SESSION_FAILED:
1908                 session->state = ISCSI_SESSION_FREE;
1909                 break;
1910         case ISCSI_SESSION_LOGGED_IN:
1911         case ISCSI_SESSION_FREE:
1912                 /* we raced with the unblock's flush */
1913                 spin_unlock_irqrestore(&session->lock, flags);
1914                 return;
1915         }
1916         spin_unlock_irqrestore(&session->lock, flags);
1917
1918         ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
1919         scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
1920         ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
1921
1922         if (session->transport->session_recovery_timedout)
1923                 session->transport->session_recovery_timedout(session);
1924 }
1925
1926 static void __iscsi_unblock_session(struct work_struct *work)
1927 {
1928         struct iscsi_cls_session *session =
1929                         container_of(work, struct iscsi_cls_session,
1930                                      unblock_work);
1931         struct Scsi_Host *shost = iscsi_session_to_shost(session);
1932         struct iscsi_cls_host *ihost = shost->shost_data;
1933         unsigned long flags;
1934
1935         ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
1936         /*
1937          * The recovery and unblock work get run from the same workqueue,
1938          * so try to cancel it if it was going to run after this unblock.
1939          */
1940         cancel_delayed_work(&session->recovery_work);
1941         spin_lock_irqsave(&session->lock, flags);
1942         session->state = ISCSI_SESSION_LOGGED_IN;
1943         spin_unlock_irqrestore(&session->lock, flags);
1944         /* start IO */
1945         scsi_target_unblock(&session->dev, SDEV_RUNNING);
1946         /*
1947          * Only do kernel scanning if the driver is properly hooked into
1948          * the async scanning code (drivers like iscsi_tcp do login and
1949          * scanning from userspace).
1950          */
1951         if (shost->hostt->scan_finished) {
1952                 if (scsi_queue_work(shost, &session->scan_work))
1953                         atomic_inc(&ihost->nr_scans);
1954         }
1955         ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
1956 }
1957
1958 /**
1959  * iscsi_unblock_session - set a session as logged in and start IO.
1960  * @session: iscsi session
1961  *
1962  * Mark a session as ready to accept IO.
1963  */
1964 void iscsi_unblock_session(struct iscsi_cls_session *session)
1965 {
1966         flush_work(&session->block_work);
1967
1968         queue_work(iscsi_eh_timer_workq, &session->unblock_work);
1969         /*
1970          * Blocking the session can be done from any context so we only
1971          * queue the block work. Make sure the unblock work has completed
1972          * because it flushes/cancels the other works and updates the state.
1973          */
1974         flush_work(&session->unblock_work);
1975 }
1976 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
1977
1978 static void __iscsi_block_session(struct work_struct *work)
1979 {
1980         struct iscsi_cls_session *session =
1981                         container_of(work, struct iscsi_cls_session,
1982                                      block_work);
1983         unsigned long flags;
1984
1985         ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
1986         spin_lock_irqsave(&session->lock, flags);
1987         session->state = ISCSI_SESSION_FAILED;
1988         spin_unlock_irqrestore(&session->lock, flags);
1989         scsi_target_block(&session->dev);
1990         ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
1991         if (session->recovery_tmo >= 0)
1992                 queue_delayed_work(iscsi_eh_timer_workq,
1993                                    &session->recovery_work,
1994                                    session->recovery_tmo * HZ);
1995 }
1996
1997 void iscsi_block_session(struct iscsi_cls_session *session)
1998 {
1999         queue_work(iscsi_eh_timer_workq, &session->block_work);
2000 }
2001 EXPORT_SYMBOL_GPL(iscsi_block_session);
2002
2003 static void __iscsi_unbind_session(struct work_struct *work)
2004 {
2005         struct iscsi_cls_session *session =
2006                         container_of(work, struct iscsi_cls_session,
2007                                      unbind_work);
2008         struct Scsi_Host *shost = iscsi_session_to_shost(session);
2009         struct iscsi_cls_host *ihost = shost->shost_data;
2010         unsigned long flags;
2011         unsigned int target_id;
2012         bool remove_target = true;
2013
2014         ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
2015
2016         /* Prevent new scans and make sure scanning is not in progress */
2017         mutex_lock(&ihost->mutex);
2018         spin_lock_irqsave(&session->lock, flags);
2019         if (session->target_state == ISCSI_SESSION_TARGET_ALLOCATED) {
2020                 remove_target = false;
2021         } else if (session->target_state != ISCSI_SESSION_TARGET_SCANNED) {
2022                 spin_unlock_irqrestore(&session->lock, flags);
2023                 mutex_unlock(&ihost->mutex);
2024                 ISCSI_DBG_TRANS_SESSION(session,
2025                         "Skipping target unbinding: Session is unbound/unbinding.\n");
2026                 return;
2027         }
2028
2029         session->target_state = ISCSI_SESSION_TARGET_UNBINDING;
2030         target_id = session->target_id;
2031         session->target_id = ISCSI_MAX_TARGET;
2032         spin_unlock_irqrestore(&session->lock, flags);
2033         mutex_unlock(&ihost->mutex);
2034
2035         if (remove_target)
2036                 scsi_remove_target(&session->dev);
2037
2038         if (session->ida_used)
2039                 ida_simple_remove(&iscsi_sess_ida, target_id);
2040
2041         iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
2042         ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
2043
2044         spin_lock_irqsave(&session->lock, flags);
2045         session->target_state = ISCSI_SESSION_TARGET_UNBOUND;
2046         spin_unlock_irqrestore(&session->lock, flags);
2047 }
2048
2049 static void __iscsi_destroy_session(struct work_struct *work)
2050 {
2051         struct iscsi_cls_session *session =
2052                 container_of(work, struct iscsi_cls_session, destroy_work);
2053
2054         session->transport->destroy_session(session);
2055 }
2056
2057 struct iscsi_cls_session *
2058 iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
2059                     int dd_size)
2060 {
2061         struct iscsi_cls_session *session;
2062
2063         session = kzalloc(sizeof(*session) + dd_size,
2064                           GFP_KERNEL);
2065         if (!session)
2066                 return NULL;
2067
2068         session->transport = transport;
2069         session->creator = -1;
2070         session->recovery_tmo = 120;
2071         session->recovery_tmo_sysfs_override = false;
2072         session->state = ISCSI_SESSION_FREE;
2073         INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
2074         INIT_LIST_HEAD(&session->sess_list);
2075         INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
2076         INIT_WORK(&session->block_work, __iscsi_block_session);
2077         INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
2078         INIT_WORK(&session->scan_work, iscsi_scan_session);
2079         INIT_WORK(&session->destroy_work, __iscsi_destroy_session);
2080         spin_lock_init(&session->lock);
2081
2082         /* this is released in the dev's release function */
2083         scsi_host_get(shost);
2084         session->dev.parent = &shost->shost_gendev;
2085         session->dev.release = iscsi_session_release;
2086         device_initialize(&session->dev);
2087         if (dd_size)
2088                 session->dd_data = &session[1];
2089
2090         ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
2091         return session;
2092 }
2093 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
2094
2095 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
2096 {
2097         unsigned long flags;
2098         int id = 0;
2099         int err;
2100
2101         session->sid = atomic_add_return(1, &iscsi_session_nr);
2102
2103         if (target_id == ISCSI_MAX_TARGET) {
2104                 id = ida_simple_get(&iscsi_sess_ida, 0, 0, GFP_KERNEL);
2105
2106                 if (id < 0) {
2107                         iscsi_cls_session_printk(KERN_ERR, session,
2108                                         "Failure in Target ID Allocation\n");
2109                         return id;
2110                 }
2111                 session->target_id = (unsigned int)id;
2112                 session->ida_used = true;
2113         } else
2114                 session->target_id = target_id;
2115         spin_lock_irqsave(&session->lock, flags);
2116         session->target_state = ISCSI_SESSION_TARGET_ALLOCATED;
2117         spin_unlock_irqrestore(&session->lock, flags);
2118
2119         dev_set_name(&session->dev, "session%u", session->sid);
2120         err = device_add(&session->dev);
2121         if (err) {
2122                 iscsi_cls_session_printk(KERN_ERR, session,
2123                                          "could not register session's dev\n");
2124                 goto release_ida;
2125         }
2126         err = transport_register_device(&session->dev);
2127         if (err) {
2128                 iscsi_cls_session_printk(KERN_ERR, session,
2129                                          "could not register transport's dev\n");
2130                 goto release_dev;
2131         }
2132
2133         spin_lock_irqsave(&sesslock, flags);
2134         list_add(&session->sess_list, &sesslist);
2135         spin_unlock_irqrestore(&sesslock, flags);
2136
2137         iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
2138         ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
2139         return 0;
2140
2141 release_dev:
2142         device_del(&session->dev);
2143 release_ida:
2144         if (session->ida_used)
2145                 ida_simple_remove(&iscsi_sess_ida, session->target_id);
2146
2147         return err;
2148 }
2149 EXPORT_SYMBOL_GPL(iscsi_add_session);
2150
2151 /**
2152  * iscsi_create_session - create iscsi class session
2153  * @shost: scsi host
2154  * @transport: iscsi transport
2155  * @dd_size: private driver data size
2156  * @target_id: which target
2157  *
2158  * This can be called from a LLD or iscsi_transport.
2159  */
2160 struct iscsi_cls_session *
2161 iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
2162                      int dd_size, unsigned int target_id)
2163 {
2164         struct iscsi_cls_session *session;
2165
2166         session = iscsi_alloc_session(shost, transport, dd_size);
2167         if (!session)
2168                 return NULL;
2169
2170         if (iscsi_add_session(session, target_id)) {
2171                 iscsi_free_session(session);
2172                 return NULL;
2173         }
2174         return session;
2175 }
2176 EXPORT_SYMBOL_GPL(iscsi_create_session);
2177
2178 static void iscsi_conn_release(struct device *dev)
2179 {
2180         struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
2181         struct device *parent = conn->dev.parent;
2182
2183         ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
2184         kfree(conn);
2185         put_device(parent);
2186 }
2187
2188 static int iscsi_is_conn_dev(const struct device *dev)
2189 {
2190         return dev->release == iscsi_conn_release;
2191 }
2192
2193 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
2194 {
2195         if (!iscsi_is_conn_dev(dev))
2196                 return 0;
2197         return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
2198 }
2199
2200 void iscsi_remove_session(struct iscsi_cls_session *session)
2201 {
2202         unsigned long flags;
2203         int err;
2204
2205         ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
2206
2207         spin_lock_irqsave(&sesslock, flags);
2208         if (!list_empty(&session->sess_list))
2209                 list_del(&session->sess_list);
2210         spin_unlock_irqrestore(&sesslock, flags);
2211
2212         flush_work(&session->block_work);
2213         flush_work(&session->unblock_work);
2214         cancel_delayed_work_sync(&session->recovery_work);
2215         /*
2216          * If we are blocked let commands flow again. The lld or iscsi
2217          * layer should set up the queuecommand to fail commands.
2218          * We assume that LLD will not be calling block/unblock while
2219          * removing the session.
2220          */
2221         spin_lock_irqsave(&session->lock, flags);
2222         session->state = ISCSI_SESSION_FREE;
2223         spin_unlock_irqrestore(&session->lock, flags);
2224
2225         scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
2226         /* flush running scans then delete devices */
2227         flush_work(&session->scan_work);
2228         /* flush running unbind operations */
2229         flush_work(&session->unbind_work);
2230         __iscsi_unbind_session(&session->unbind_work);
2231
2232         /* hw iscsi may not have removed all connections from session */
2233         err = device_for_each_child(&session->dev, NULL,
2234                                     iscsi_iter_destroy_conn_fn);
2235         if (err)
2236                 iscsi_cls_session_printk(KERN_ERR, session,
2237                                          "Could not delete all connections "
2238                                          "for session. Error %d.\n", err);
2239
2240         transport_unregister_device(&session->dev);
2241
2242         ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
2243         device_del(&session->dev);
2244 }
2245 EXPORT_SYMBOL_GPL(iscsi_remove_session);
2246
2247 static void iscsi_stop_conn(struct iscsi_cls_conn *conn, int flag)
2248 {
2249         ISCSI_DBG_TRANS_CONN(conn, "Stopping conn.\n");
2250
2251         switch (flag) {
2252         case STOP_CONN_RECOVER:
2253                 WRITE_ONCE(conn->state, ISCSI_CONN_FAILED);
2254                 break;
2255         case STOP_CONN_TERM:
2256                 WRITE_ONCE(conn->state, ISCSI_CONN_DOWN);
2257                 break;
2258         default:
2259                 iscsi_cls_conn_printk(KERN_ERR, conn, "invalid stop flag %d\n",
2260                                       flag);
2261                 return;
2262         }
2263
2264         conn->transport->stop_conn(conn, flag);
2265         ISCSI_DBG_TRANS_CONN(conn, "Stopping conn done.\n");
2266 }
2267
2268 static void iscsi_ep_disconnect(struct iscsi_cls_conn *conn, bool is_active)
2269 {
2270         struct iscsi_cls_session *session = iscsi_conn_to_session(conn);
2271         struct iscsi_endpoint *ep;
2272
2273         ISCSI_DBG_TRANS_CONN(conn, "disconnect ep.\n");
2274         WRITE_ONCE(conn->state, ISCSI_CONN_FAILED);
2275
2276         if (!conn->ep || !session->transport->ep_disconnect)
2277                 return;
2278
2279         ep = conn->ep;
2280         conn->ep = NULL;
2281
2282         session->transport->unbind_conn(conn, is_active);
2283         session->transport->ep_disconnect(ep);
2284         ISCSI_DBG_TRANS_CONN(conn, "disconnect ep done.\n");
2285 }
2286
2287 static void iscsi_if_disconnect_bound_ep(struct iscsi_cls_conn *conn,
2288                                          struct iscsi_endpoint *ep,
2289                                          bool is_active)
2290 {
2291         /* Check if this was a conn error and the kernel took ownership */
2292         spin_lock_irq(&conn->lock);
2293         if (!test_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags)) {
2294                 spin_unlock_irq(&conn->lock);
2295                 iscsi_ep_disconnect(conn, is_active);
2296         } else {
2297                 spin_unlock_irq(&conn->lock);
2298                 ISCSI_DBG_TRANS_CONN(conn, "flush kernel conn cleanup.\n");
2299                 mutex_unlock(&conn->ep_mutex);
2300
2301                 flush_work(&conn->cleanup_work);
2302                 /*
2303                  * Userspace is now done with the EP so we can release the ref
2304                  * iscsi_cleanup_conn_work_fn took.
2305                  */
2306                 iscsi_put_endpoint(ep);
2307                 mutex_lock(&conn->ep_mutex);
2308         }
2309 }
2310
2311 static int iscsi_if_stop_conn(struct iscsi_cls_conn *conn, int flag)
2312 {
2313         ISCSI_DBG_TRANS_CONN(conn, "iscsi if conn stop.\n");
2314         /*
2315          * If this is a termination we have to call stop_conn with that flag
2316          * so the correct states get set. If we haven't run the work yet try to
2317          * avoid the extra run.
2318          */
2319         if (flag == STOP_CONN_TERM) {
2320                 cancel_work_sync(&conn->cleanup_work);
2321                 iscsi_stop_conn(conn, flag);
2322         } else {
2323                 /*
2324                  * For offload, when iscsid is restarted it won't know about
2325                  * existing endpoints so it can't do a ep_disconnect. We clean
2326                  * it up here for userspace.
2327                  */
2328                 mutex_lock(&conn->ep_mutex);
2329                 if (conn->ep)
2330                         iscsi_if_disconnect_bound_ep(conn, conn->ep, true);
2331                 mutex_unlock(&conn->ep_mutex);
2332
2333                 /*
2334                  * Figure out if it was the kernel or userspace initiating this.
2335                  */
2336                 spin_lock_irq(&conn->lock);
2337                 if (!test_and_set_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags)) {
2338                         spin_unlock_irq(&conn->lock);
2339                         iscsi_stop_conn(conn, flag);
2340                 } else {
2341                         spin_unlock_irq(&conn->lock);
2342                         ISCSI_DBG_TRANS_CONN(conn,
2343                                              "flush kernel conn cleanup.\n");
2344                         flush_work(&conn->cleanup_work);
2345                 }
2346                 /*
2347                  * Only clear for recovery to avoid extra cleanup runs during
2348                  * termination.
2349                  */
2350                 spin_lock_irq(&conn->lock);
2351                 clear_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags);
2352                 spin_unlock_irq(&conn->lock);
2353         }
2354         ISCSI_DBG_TRANS_CONN(conn, "iscsi if conn stop done.\n");
2355         return 0;
2356 }
2357
2358 static void iscsi_cleanup_conn_work_fn(struct work_struct *work)
2359 {
2360         struct iscsi_cls_conn *conn = container_of(work, struct iscsi_cls_conn,
2361                                                    cleanup_work);
2362         struct iscsi_cls_session *session = iscsi_conn_to_session(conn);
2363
2364         mutex_lock(&conn->ep_mutex);
2365         /*
2366          * Get a ref to the ep, so we don't release its ID until after
2367          * userspace is done referencing it in iscsi_if_disconnect_bound_ep.
2368          */
2369         if (conn->ep)
2370                 get_device(&conn->ep->dev);
2371         iscsi_ep_disconnect(conn, false);
2372
2373         if (system_state != SYSTEM_RUNNING) {
2374                 /*
2375                  * If the user has set up for the session to never timeout
2376                  * then hang like they wanted. For all other cases fail right
2377                  * away since userspace is not going to relogin.
2378                  */
2379                 if (session->recovery_tmo > 0)
2380                         session->recovery_tmo = 0;
2381         }
2382
2383         iscsi_stop_conn(conn, STOP_CONN_RECOVER);
2384         mutex_unlock(&conn->ep_mutex);
2385         ISCSI_DBG_TRANS_CONN(conn, "cleanup done.\n");
2386 }
2387
2388 static int iscsi_iter_force_destroy_conn_fn(struct device *dev, void *data)
2389 {
2390         struct iscsi_transport *transport;
2391         struct iscsi_cls_conn *conn;
2392
2393         if (!iscsi_is_conn_dev(dev))
2394                 return 0;
2395
2396         conn = iscsi_dev_to_conn(dev);
2397         transport = conn->transport;
2398
2399         if (READ_ONCE(conn->state) != ISCSI_CONN_DOWN)
2400                 iscsi_if_stop_conn(conn, STOP_CONN_TERM);
2401
2402         transport->destroy_conn(conn);
2403         return 0;
2404 }
2405
2406 /**
2407  * iscsi_force_destroy_session - destroy a session from the kernel
2408  * @session: session to destroy
2409  *
2410  * Force the destruction of a session from the kernel. This should only be
2411  * used when userspace is no longer running during system shutdown.
2412  */
2413 void iscsi_force_destroy_session(struct iscsi_cls_session *session)
2414 {
2415         struct iscsi_transport *transport = session->transport;
2416         unsigned long flags;
2417
2418         WARN_ON_ONCE(system_state == SYSTEM_RUNNING);
2419
2420         spin_lock_irqsave(&sesslock, flags);
2421         if (list_empty(&session->sess_list)) {
2422                 spin_unlock_irqrestore(&sesslock, flags);
2423                 /*
2424                  * Conn/ep is already freed. Session is being torn down via
2425                  * async path. For shutdown we don't care about it so return.
2426                  */
2427                 return;
2428         }
2429         spin_unlock_irqrestore(&sesslock, flags);
2430
2431         device_for_each_child(&session->dev, NULL,
2432                               iscsi_iter_force_destroy_conn_fn);
2433         transport->destroy_session(session);
2434 }
2435 EXPORT_SYMBOL_GPL(iscsi_force_destroy_session);
2436
2437 void iscsi_free_session(struct iscsi_cls_session *session)
2438 {
2439         ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
2440         iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
2441         put_device(&session->dev);
2442 }
2443 EXPORT_SYMBOL_GPL(iscsi_free_session);
2444
2445 /**
2446  * iscsi_create_conn - create iscsi class connection
2447  * @session: iscsi cls session
2448  * @dd_size: private driver data size
2449  * @cid: connection id
2450  *
2451  * This can be called from a LLD or iscsi_transport. The connection
2452  * is child of the session so cid must be unique for all connections
2453  * on the session.
2454  *
2455  * Since we do not support MCS, cid will normally be zero. In some cases
2456  * for software iscsi we could be trying to preallocate a connection struct
2457  * in which case there could be two connection structs and cid would be
2458  * non-zero.
2459  */
2460 struct iscsi_cls_conn *
2461 iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
2462 {
2463         struct iscsi_transport *transport = session->transport;
2464         struct iscsi_cls_conn *conn;
2465         unsigned long flags;
2466         int err;
2467
2468         conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
2469         if (!conn)
2470                 return NULL;
2471         if (dd_size)
2472                 conn->dd_data = &conn[1];
2473
2474         mutex_init(&conn->ep_mutex);
2475         spin_lock_init(&conn->lock);
2476         INIT_LIST_HEAD(&conn->conn_list);
2477         INIT_WORK(&conn->cleanup_work, iscsi_cleanup_conn_work_fn);
2478         conn->transport = transport;
2479         conn->cid = cid;
2480         WRITE_ONCE(conn->state, ISCSI_CONN_DOWN);
2481
2482         /* this is released in the dev's release function */
2483         if (!get_device(&session->dev))
2484                 goto free_conn;
2485
2486         dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
2487         conn->dev.parent = &session->dev;
2488         conn->dev.release = iscsi_conn_release;
2489         err = device_register(&conn->dev);
2490         if (err) {
2491                 iscsi_cls_session_printk(KERN_ERR, session, "could not "
2492                                          "register connection's dev\n");
2493                 goto release_parent_ref;
2494         }
2495         err = transport_register_device(&conn->dev);
2496         if (err) {
2497                 iscsi_cls_session_printk(KERN_ERR, session, "could not "
2498                                          "register transport's dev\n");
2499                 goto release_conn_ref;
2500         }
2501
2502         spin_lock_irqsave(&connlock, flags);
2503         list_add(&conn->conn_list, &connlist);
2504         spin_unlock_irqrestore(&connlock, flags);
2505
2506         ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
2507         return conn;
2508
2509 release_conn_ref:
2510         device_unregister(&conn->dev);
2511         put_device(&session->dev);
2512         return NULL;
2513 release_parent_ref:
2514         put_device(&session->dev);
2515 free_conn:
2516         kfree(conn);
2517         return NULL;
2518 }
2519
2520 EXPORT_SYMBOL_GPL(iscsi_create_conn);
2521
2522 /**
2523  * iscsi_destroy_conn - destroy iscsi class connection
2524  * @conn: iscsi cls session
2525  *
2526  * This can be called from a LLD or iscsi_transport.
2527  */
2528 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
2529 {
2530         unsigned long flags;
2531
2532         spin_lock_irqsave(&connlock, flags);
2533         list_del(&conn->conn_list);
2534         spin_unlock_irqrestore(&connlock, flags);
2535
2536         transport_unregister_device(&conn->dev);
2537         ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
2538         device_unregister(&conn->dev);
2539         return 0;
2540 }
2541 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
2542
2543 void iscsi_put_conn(struct iscsi_cls_conn *conn)
2544 {
2545         put_device(&conn->dev);
2546 }
2547 EXPORT_SYMBOL_GPL(iscsi_put_conn);
2548
2549 void iscsi_get_conn(struct iscsi_cls_conn *conn)
2550 {
2551         get_device(&conn->dev);
2552 }
2553 EXPORT_SYMBOL_GPL(iscsi_get_conn);
2554
2555 /*
2556  * iscsi interface functions
2557  */
2558 static struct iscsi_internal *
2559 iscsi_if_transport_lookup(struct iscsi_transport *tt)
2560 {
2561         struct iscsi_internal *priv;
2562         unsigned long flags;
2563
2564         spin_lock_irqsave(&iscsi_transport_lock, flags);
2565         list_for_each_entry(priv, &iscsi_transports, list) {
2566                 if (tt == priv->iscsi_transport) {
2567                         spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2568                         return priv;
2569                 }
2570         }
2571         spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2572         return NULL;
2573 }
2574
2575 static int
2576 iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
2577 {
2578         return nlmsg_multicast(nls, skb, 0, group, gfp);
2579 }
2580
2581 static int
2582 iscsi_unicast_skb(struct sk_buff *skb, u32 portid)
2583 {
2584         return nlmsg_unicast(nls, skb, portid);
2585 }
2586
2587 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
2588                    char *data, uint32_t data_size)
2589 {
2590         struct nlmsghdr *nlh;
2591         struct sk_buff *skb;
2592         struct iscsi_uevent *ev;
2593         char *pdu;
2594         struct iscsi_internal *priv;
2595         int len = nlmsg_total_size(sizeof(*ev) + sizeof(struct iscsi_hdr) +
2596                                    data_size);
2597
2598         priv = iscsi_if_transport_lookup(conn->transport);
2599         if (!priv)
2600                 return -EINVAL;
2601
2602         skb = alloc_skb(len, GFP_ATOMIC);
2603         if (!skb) {
2604                 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
2605                 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
2606                                       "control PDU: OOM\n");
2607                 return -ENOMEM;
2608         }
2609
2610         nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2611         ev = nlmsg_data(nlh);
2612         memset(ev, 0, sizeof(*ev));
2613         ev->transport_handle = iscsi_handle(conn->transport);
2614         ev->type = ISCSI_KEVENT_RECV_PDU;
2615         ev->r.recv_req.cid = conn->cid;
2616         ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
2617         pdu = (char*)ev + sizeof(*ev);
2618         memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
2619         memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
2620
2621         return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2622 }
2623 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
2624
2625 int iscsi_offload_mesg(struct Scsi_Host *shost,
2626                        struct iscsi_transport *transport, uint32_t type,
2627                        char *data, uint16_t data_size)
2628 {
2629         struct nlmsghdr *nlh;
2630         struct sk_buff *skb;
2631         struct iscsi_uevent *ev;
2632         int len = nlmsg_total_size(sizeof(*ev) + data_size);
2633
2634         skb = alloc_skb(len, GFP_ATOMIC);
2635         if (!skb) {
2636                 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
2637                 return -ENOMEM;
2638         }
2639
2640         nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2641         ev = nlmsg_data(nlh);
2642         memset(ev, 0, sizeof(*ev));
2643         ev->type = type;
2644         ev->transport_handle = iscsi_handle(transport);
2645         switch (type) {
2646         case ISCSI_KEVENT_PATH_REQ:
2647                 ev->r.req_path.host_no = shost->host_no;
2648                 break;
2649         case ISCSI_KEVENT_IF_DOWN:
2650                 ev->r.notify_if_down.host_no = shost->host_no;
2651                 break;
2652         }
2653
2654         memcpy((char *)ev + sizeof(*ev), data, data_size);
2655
2656         return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
2657 }
2658 EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
2659
2660 void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
2661 {
2662         struct nlmsghdr *nlh;
2663         struct sk_buff  *skb;
2664         struct iscsi_uevent *ev;
2665         struct iscsi_internal *priv;
2666         int len = nlmsg_total_size(sizeof(*ev));
2667         unsigned long flags;
2668         int state;
2669
2670         spin_lock_irqsave(&conn->lock, flags);
2671         /*
2672          * Userspace will only do a stop call if we are at least bound. And, we
2673          * only need to do the in kernel cleanup if in the UP state so cmds can
2674          * be released to upper layers. If in other states just wait for
2675          * userspace to avoid races that can leave the cleanup_work queued.
2676          */
2677         state = READ_ONCE(conn->state);
2678         switch (state) {
2679         case ISCSI_CONN_BOUND:
2680         case ISCSI_CONN_UP:
2681                 if (!test_and_set_bit(ISCSI_CLS_CONN_BIT_CLEANUP,
2682                                       &conn->flags)) {
2683                         queue_work(iscsi_conn_cleanup_workq,
2684                                    &conn->cleanup_work);
2685                 }
2686                 break;
2687         default:
2688                 ISCSI_DBG_TRANS_CONN(conn, "Got conn error in state %d\n",
2689                                      state);
2690                 break;
2691         }
2692         spin_unlock_irqrestore(&conn->lock, flags);
2693
2694         priv = iscsi_if_transport_lookup(conn->transport);
2695         if (!priv)
2696                 return;
2697
2698         skb = alloc_skb(len, GFP_ATOMIC);
2699         if (!skb) {
2700                 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2701                                       "conn error (%d)\n", error);
2702                 return;
2703         }
2704
2705         nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2706         ev = nlmsg_data(nlh);
2707         ev->transport_handle = iscsi_handle(conn->transport);
2708         ev->type = ISCSI_KEVENT_CONN_ERROR;
2709         ev->r.connerror.error = error;
2710         ev->r.connerror.cid = conn->cid;
2711         ev->r.connerror.sid = iscsi_conn_get_sid(conn);
2712
2713         iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2714
2715         iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
2716                               error);
2717 }
2718 EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
2719
2720 void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
2721                             enum iscsi_conn_state state)
2722 {
2723         struct nlmsghdr *nlh;
2724         struct sk_buff  *skb;
2725         struct iscsi_uevent *ev;
2726         struct iscsi_internal *priv;
2727         int len = nlmsg_total_size(sizeof(*ev));
2728
2729         priv = iscsi_if_transport_lookup(conn->transport);
2730         if (!priv)
2731                 return;
2732
2733         skb = alloc_skb(len, GFP_ATOMIC);
2734         if (!skb) {
2735                 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2736                                       "conn login (%d)\n", state);
2737                 return;
2738         }
2739
2740         nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2741         ev = nlmsg_data(nlh);
2742         ev->transport_handle = iscsi_handle(conn->transport);
2743         ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
2744         ev->r.conn_login.state = state;
2745         ev->r.conn_login.cid = conn->cid;
2746         ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
2747         iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2748
2749         iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
2750                               state);
2751 }
2752 EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
2753
2754 void iscsi_post_host_event(uint32_t host_no, struct iscsi_transport *transport,
2755                            enum iscsi_host_event_code code, uint32_t data_size,
2756                            uint8_t *data)
2757 {
2758         struct nlmsghdr *nlh;
2759         struct sk_buff *skb;
2760         struct iscsi_uevent *ev;
2761         int len = nlmsg_total_size(sizeof(*ev) + data_size);
2762
2763         skb = alloc_skb(len, GFP_NOIO);
2764         if (!skb) {
2765                 printk(KERN_ERR "gracefully ignored host event (%d):%d OOM\n",
2766                        host_no, code);
2767                 return;
2768         }
2769
2770         nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2771         ev = nlmsg_data(nlh);
2772         ev->transport_handle = iscsi_handle(transport);
2773         ev->type = ISCSI_KEVENT_HOST_EVENT;
2774         ev->r.host_event.host_no = host_no;
2775         ev->r.host_event.code = code;
2776         ev->r.host_event.data_size = data_size;
2777
2778         if (data_size)
2779                 memcpy((char *)ev + sizeof(*ev), data, data_size);
2780
2781         iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
2782 }
2783 EXPORT_SYMBOL_GPL(iscsi_post_host_event);
2784
2785 void iscsi_ping_comp_event(uint32_t host_no, struct iscsi_transport *transport,
2786                            uint32_t status, uint32_t pid, uint32_t data_size,
2787                            uint8_t *data)
2788 {
2789         struct nlmsghdr *nlh;
2790         struct sk_buff *skb;
2791         struct iscsi_uevent *ev;
2792         int len = nlmsg_total_size(sizeof(*ev) + data_size);
2793
2794         skb = alloc_skb(len, GFP_NOIO);
2795         if (!skb) {
2796                 printk(KERN_ERR "gracefully ignored ping comp: OOM\n");
2797                 return;
2798         }
2799
2800         nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2801         ev = nlmsg_data(nlh);
2802         ev->transport_handle = iscsi_handle(transport);
2803         ev->type = ISCSI_KEVENT_PING_COMP;
2804         ev->r.ping_comp.host_no = host_no;
2805         ev->r.ping_comp.status = status;
2806         ev->r.ping_comp.pid = pid;
2807         ev->r.ping_comp.data_size = data_size;
2808         memcpy((char *)ev + sizeof(*ev), data, data_size);
2809
2810         iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
2811 }
2812 EXPORT_SYMBOL_GPL(iscsi_ping_comp_event);
2813
2814 static int
2815 iscsi_if_send_reply(u32 portid, int type, void *payload, int size)
2816 {
2817         struct sk_buff  *skb;
2818         struct nlmsghdr *nlh;
2819         int len = nlmsg_total_size(size);
2820
2821         skb = alloc_skb(len, GFP_ATOMIC);
2822         if (!skb) {
2823                 printk(KERN_ERR "Could not allocate skb to send reply.\n");
2824                 return -ENOMEM;
2825         }
2826
2827         nlh = __nlmsg_put(skb, 0, 0, type, (len - sizeof(*nlh)), 0);
2828         memcpy(nlmsg_data(nlh), payload, size);
2829         return iscsi_unicast_skb(skb, portid);
2830 }
2831
2832 static int
2833 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
2834 {
2835         struct iscsi_uevent *ev = nlmsg_data(nlh);
2836         struct iscsi_stats *stats;
2837         struct sk_buff *skbstat;
2838         struct iscsi_cls_conn *conn;
2839         struct nlmsghdr *nlhstat;
2840         struct iscsi_uevent *evstat;
2841         struct iscsi_internal *priv;
2842         int len = nlmsg_total_size(sizeof(*ev) +
2843                                    sizeof(struct iscsi_stats) +
2844                                    sizeof(struct iscsi_stats_custom) *
2845                                    ISCSI_STATS_CUSTOM_MAX);
2846         int err = 0;
2847
2848         priv = iscsi_if_transport_lookup(transport);
2849         if (!priv)
2850                 return -EINVAL;
2851
2852         conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
2853         if (!conn)
2854                 return -EEXIST;
2855
2856         do {
2857                 int actual_size;
2858
2859                 skbstat = alloc_skb(len, GFP_ATOMIC);
2860                 if (!skbstat) {
2861                         iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
2862                                               "deliver stats: OOM\n");
2863                         return -ENOMEM;
2864                 }
2865
2866                 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
2867                                       (len - sizeof(*nlhstat)), 0);
2868                 evstat = nlmsg_data(nlhstat);
2869                 memset(evstat, 0, sizeof(*evstat));
2870                 evstat->transport_handle = iscsi_handle(conn->transport);
2871                 evstat->type = nlh->nlmsg_type;
2872                 evstat->u.get_stats.cid =
2873                         ev->u.get_stats.cid;
2874                 evstat->u.get_stats.sid =
2875                         ev->u.get_stats.sid;
2876                 stats = (struct iscsi_stats *)
2877                         ((char*)evstat + sizeof(*evstat));
2878                 memset(stats, 0, sizeof(*stats));
2879
2880                 transport->get_stats(conn, stats);
2881                 actual_size = nlmsg_total_size(sizeof(struct iscsi_uevent) +
2882                                                sizeof(struct iscsi_stats) +
2883                                                sizeof(struct iscsi_stats_custom) *
2884                                                stats->custom_length);
2885                 actual_size -= sizeof(*nlhstat);
2886                 actual_size = nlmsg_msg_size(actual_size);
2887                 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
2888                 nlhstat->nlmsg_len = actual_size;
2889
2890                 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
2891                                           GFP_ATOMIC);
2892         } while (err < 0 && err != -ECONNREFUSED);
2893
2894         return err;
2895 }
2896
2897 /**
2898  * iscsi_session_event - send session destr. completion event
2899  * @session: iscsi class session
2900  * @event: type of event
2901  */
2902 int iscsi_session_event(struct iscsi_cls_session *session,
2903                         enum iscsi_uevent_e event)
2904 {
2905         struct iscsi_internal *priv;
2906         struct Scsi_Host *shost;
2907         struct iscsi_uevent *ev;
2908         struct sk_buff  *skb;
2909         struct nlmsghdr *nlh;
2910         int rc, len = nlmsg_total_size(sizeof(*ev));
2911
2912         priv = iscsi_if_transport_lookup(session->transport);
2913         if (!priv)
2914                 return -EINVAL;
2915         shost = iscsi_session_to_shost(session);
2916
2917         skb = alloc_skb(len, GFP_KERNEL);
2918         if (!skb) {
2919                 iscsi_cls_session_printk(KERN_ERR, session,
2920                                          "Cannot notify userspace of session "
2921                                          "event %u\n", event);
2922                 return -ENOMEM;
2923         }
2924
2925         nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2926         ev = nlmsg_data(nlh);
2927         ev->transport_handle = iscsi_handle(session->transport);
2928
2929         ev->type = event;
2930         switch (event) {
2931         case ISCSI_KEVENT_DESTROY_SESSION:
2932                 ev->r.d_session.host_no = shost->host_no;
2933                 ev->r.d_session.sid = session->sid;
2934                 break;
2935         case ISCSI_KEVENT_CREATE_SESSION:
2936                 ev->r.c_session_ret.host_no = shost->host_no;
2937                 ev->r.c_session_ret.sid = session->sid;
2938                 break;
2939         case ISCSI_KEVENT_UNBIND_SESSION:
2940                 ev->r.unbind_session.host_no = shost->host_no;
2941                 ev->r.unbind_session.sid = session->sid;
2942                 break;
2943         default:
2944                 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
2945                                          "%u.\n", event);
2946                 kfree_skb(skb);
2947                 return -EINVAL;
2948         }
2949
2950         /*
2951          * this will occur if the daemon is not up, so we just warn
2952          * the user and when the daemon is restarted it will handle it
2953          */
2954         rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
2955         if (rc == -ESRCH)
2956                 iscsi_cls_session_printk(KERN_ERR, session,
2957                                          "Cannot notify userspace of session "
2958                                          "event %u. Check iscsi daemon\n",
2959                                          event);
2960
2961         ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
2962                                 event, rc);
2963         return rc;
2964 }
2965 EXPORT_SYMBOL_GPL(iscsi_session_event);
2966
2967 static int
2968 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
2969                         struct iscsi_uevent *ev, pid_t pid,
2970                         uint32_t initial_cmdsn, uint16_t cmds_max,
2971                         uint16_t queue_depth)
2972 {
2973         struct iscsi_transport *transport = priv->iscsi_transport;
2974         struct iscsi_cls_session *session;
2975         struct Scsi_Host *shost;
2976
2977         session = transport->create_session(ep, cmds_max, queue_depth,
2978                                             initial_cmdsn);
2979         if (!session)
2980                 return -ENOMEM;
2981
2982         session->creator = pid;
2983         shost = iscsi_session_to_shost(session);
2984         ev->r.c_session_ret.host_no = shost->host_no;
2985         ev->r.c_session_ret.sid = session->sid;
2986         ISCSI_DBG_TRANS_SESSION(session,
2987                                 "Completed creating transport session\n");
2988         return 0;
2989 }
2990
2991 static int
2992 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2993 {
2994         struct iscsi_cls_conn *conn;
2995         struct iscsi_cls_session *session;
2996
2997         session = iscsi_session_lookup(ev->u.c_conn.sid);
2998         if (!session) {
2999                 printk(KERN_ERR "iscsi: invalid session %d.\n",
3000                        ev->u.c_conn.sid);
3001                 return -EINVAL;
3002         }
3003
3004         conn = transport->create_conn(session, ev->u.c_conn.cid);
3005         if (!conn) {
3006                 iscsi_cls_session_printk(KERN_ERR, session,
3007                                          "couldn't create a new connection.");
3008                 return -ENOMEM;
3009         }
3010
3011         ev->r.c_conn_ret.sid = session->sid;
3012         ev->r.c_conn_ret.cid = conn->cid;
3013
3014         ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
3015         return 0;
3016 }
3017
3018 static int
3019 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
3020 {
3021         struct iscsi_cls_conn *conn;
3022
3023         conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
3024         if (!conn)
3025                 return -EINVAL;
3026
3027         ISCSI_DBG_TRANS_CONN(conn, "Flushing cleanup during destruction\n");
3028         flush_work(&conn->cleanup_work);
3029         ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
3030
3031         if (transport->destroy_conn)
3032                 transport->destroy_conn(conn);
3033         return 0;
3034 }
3035
3036 static int
3037 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
3038 {
3039         char *data = (char*)ev + sizeof(*ev);
3040         struct iscsi_cls_conn *conn;
3041         struct iscsi_cls_session *session;
3042         int err = 0, value = 0, state;
3043
3044         if (ev->u.set_param.len > PAGE_SIZE)
3045                 return -EINVAL;
3046
3047         session = iscsi_session_lookup(ev->u.set_param.sid);
3048         conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
3049         if (!conn || !session)
3050                 return -EINVAL;
3051
3052         switch (ev->u.set_param.param) {
3053         case ISCSI_PARAM_SESS_RECOVERY_TMO:
3054                 sscanf(data, "%d", &value);
3055                 if (!session->recovery_tmo_sysfs_override)
3056                         session->recovery_tmo = value;
3057                 break;
3058         default:
3059                 state = READ_ONCE(conn->state);
3060                 if (state == ISCSI_CONN_BOUND || state == ISCSI_CONN_UP) {
3061                         err = transport->set_param(conn, ev->u.set_param.param,
3062                                         data, ev->u.set_param.len);
3063                 } else {
3064                         return -ENOTCONN;
3065                 }
3066         }
3067
3068         return err;
3069 }
3070
3071 static int iscsi_if_ep_connect(struct iscsi_transport *transport,
3072                                struct iscsi_uevent *ev, int msg_type)
3073 {
3074         struct iscsi_endpoint *ep;
3075         struct sockaddr *dst_addr;
3076         struct Scsi_Host *shost = NULL;
3077         int non_blocking, err = 0;
3078
3079         if (!transport->ep_connect)
3080                 return -EINVAL;
3081
3082         if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
3083                 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
3084                 if (!shost) {
3085                         printk(KERN_ERR "ep connect failed. Could not find "
3086                                "host no %u\n",
3087                                ev->u.ep_connect_through_host.host_no);
3088                         return -ENODEV;
3089                 }
3090                 non_blocking = ev->u.ep_connect_through_host.non_blocking;
3091         } else
3092                 non_blocking = ev->u.ep_connect.non_blocking;
3093
3094         dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
3095         ep = transport->ep_connect(shost, dst_addr, non_blocking);
3096         if (IS_ERR(ep)) {
3097                 err = PTR_ERR(ep);
3098                 goto release_host;
3099         }
3100
3101         ev->r.ep_connect_ret.handle = ep->id;
3102 release_host:
3103         if (shost)
3104                 scsi_host_put(shost);
3105         return err;
3106 }
3107
3108 static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
3109                                   u64 ep_handle)
3110 {
3111         struct iscsi_cls_conn *conn;
3112         struct iscsi_endpoint *ep;
3113
3114         if (!transport->ep_disconnect)
3115                 return -EINVAL;
3116
3117         ep = iscsi_lookup_endpoint(ep_handle);
3118         if (!ep)
3119                 return -EINVAL;
3120
3121         conn = ep->conn;
3122         if (!conn) {
3123                 /*
3124                  * conn was not even bound yet, so we can't get iscsi conn
3125                  * failures yet.
3126                  */
3127                 transport->ep_disconnect(ep);
3128                 goto put_ep;
3129         }
3130
3131         mutex_lock(&conn->ep_mutex);
3132         iscsi_if_disconnect_bound_ep(conn, ep, false);
3133         mutex_unlock(&conn->ep_mutex);
3134 put_ep:
3135         iscsi_put_endpoint(ep);
3136         return 0;
3137 }
3138
3139 static int
3140 iscsi_if_transport_ep(struct iscsi_transport *transport,
3141                       struct iscsi_uevent *ev, int msg_type)
3142 {
3143         struct iscsi_endpoint *ep;
3144         int rc = 0;
3145
3146         switch (msg_type) {
3147         case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
3148         case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
3149                 rc = iscsi_if_ep_connect(transport, ev, msg_type);
3150                 break;
3151         case ISCSI_UEVENT_TRANSPORT_EP_POLL:
3152                 if (!transport->ep_poll)
3153                         return -EINVAL;
3154
3155                 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
3156                 if (!ep)
3157                         return -EINVAL;
3158
3159                 ev->r.retcode = transport->ep_poll(ep,
3160                                                    ev->u.ep_poll.timeout_ms);
3161                 iscsi_put_endpoint(ep);
3162                 break;
3163         case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
3164                 rc = iscsi_if_ep_disconnect(transport,
3165                                             ev->u.ep_disconnect.ep_handle);
3166                 break;
3167         }
3168         return rc;
3169 }
3170
3171 static int
3172 iscsi_tgt_dscvr(struct iscsi_transport *transport,
3173                 struct iscsi_uevent *ev)
3174 {
3175         struct Scsi_Host *shost;
3176         struct sockaddr *dst_addr;
3177         int err;
3178
3179         if (!transport->tgt_dscvr)
3180                 return -EINVAL;
3181
3182         shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
3183         if (!shost) {
3184                 printk(KERN_ERR "target discovery could not find host no %u\n",
3185                        ev->u.tgt_dscvr.host_no);
3186                 return -ENODEV;
3187         }
3188
3189
3190         dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
3191         err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
3192                                    ev->u.tgt_dscvr.enable, dst_addr);
3193         scsi_host_put(shost);
3194         return err;
3195 }
3196
3197 static int
3198 iscsi_set_host_param(struct iscsi_transport *transport,
3199                      struct iscsi_uevent *ev)
3200 {
3201         char *data = (char*)ev + sizeof(*ev);
3202         struct Scsi_Host *shost;
3203         int err;
3204
3205         if (!transport->set_host_param)
3206                 return -ENOSYS;
3207
3208         if (ev->u.set_host_param.len > PAGE_SIZE)
3209                 return -EINVAL;
3210
3211         shost = scsi_host_lookup(ev->u.set_host_param.host_no);
3212         if (!shost) {
3213                 printk(KERN_ERR "set_host_param could not find host no %u\n",
3214                        ev->u.set_host_param.host_no);
3215                 return -ENODEV;
3216         }
3217
3218         err = transport->set_host_param(shost, ev->u.set_host_param.param,
3219                                         data, ev->u.set_host_param.len);
3220         scsi_host_put(shost);
3221         return err;
3222 }
3223
3224 static int
3225 iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
3226 {
3227         struct Scsi_Host *shost;
3228         struct iscsi_path *params;
3229         int err;
3230
3231         if (!transport->set_path)
3232                 return -ENOSYS;
3233
3234         shost = scsi_host_lookup(ev->u.set_path.host_no);
3235         if (!shost) {
3236                 printk(KERN_ERR "set path could not find host no %u\n",
3237                        ev->u.set_path.host_no);
3238                 return -ENODEV;
3239         }
3240
3241         params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
3242         err = transport->set_path(shost, params);
3243
3244         scsi_host_put(shost);
3245         return err;
3246 }
3247
3248 static int iscsi_session_has_conns(int sid)
3249 {
3250         struct iscsi_cls_conn *conn;
3251         unsigned long flags;
3252         int found = 0;
3253
3254         spin_lock_irqsave(&connlock, flags);
3255         list_for_each_entry(conn, &connlist, conn_list) {
3256                 if (iscsi_conn_get_sid(conn) == sid) {
3257                         found = 1;
3258                         break;
3259                 }
3260         }
3261         spin_unlock_irqrestore(&connlock, flags);
3262
3263         return found;
3264 }
3265
3266 static int
3267 iscsi_set_iface_params(struct iscsi_transport *transport,
3268                        struct iscsi_uevent *ev, uint32_t len)
3269 {
3270         char *data = (char *)ev + sizeof(*ev);
3271         struct Scsi_Host *shost;
3272         int err;
3273
3274         if (!transport->set_iface_param)
3275                 return -ENOSYS;
3276
3277         shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
3278         if (!shost) {
3279                 printk(KERN_ERR "set_iface_params could not find host no %u\n",
3280                        ev->u.set_iface_params.host_no);
3281                 return -ENODEV;
3282         }
3283
3284         err = transport->set_iface_param(shost, data, len);
3285         scsi_host_put(shost);
3286         return err;
3287 }
3288
3289 static int
3290 iscsi_send_ping(struct iscsi_transport *transport, struct iscsi_uevent *ev)
3291 {
3292         struct Scsi_Host *shost;
3293         struct sockaddr *dst_addr;
3294         int err;
3295
3296         if (!transport->send_ping)
3297                 return -ENOSYS;
3298
3299         shost = scsi_host_lookup(ev->u.iscsi_ping.host_no);
3300         if (!shost) {
3301                 printk(KERN_ERR "iscsi_ping could not find host no %u\n",
3302                        ev->u.iscsi_ping.host_no);
3303                 return -ENODEV;
3304         }
3305
3306         dst_addr = (struct sockaddr *)((char *)ev + sizeof(*ev));
3307         err = transport->send_ping(shost, ev->u.iscsi_ping.iface_num,
3308                                    ev->u.iscsi_ping.iface_type,
3309                                    ev->u.iscsi_ping.payload_size,
3310                                    ev->u.iscsi_ping.pid,
3311                                    dst_addr);
3312         scsi_host_put(shost);
3313         return err;
3314 }
3315
3316 static int
3317 iscsi_get_chap(struct iscsi_transport *transport, struct nlmsghdr *nlh)
3318 {
3319         struct iscsi_uevent *ev = nlmsg_data(nlh);
3320         struct Scsi_Host *shost = NULL;
3321         struct iscsi_chap_rec *chap_rec;
3322         struct iscsi_internal *priv;
3323         struct sk_buff *skbchap;
3324         struct nlmsghdr *nlhchap;
3325         struct iscsi_uevent *evchap;
3326         uint32_t chap_buf_size;
3327         int len, err = 0;
3328         char *buf;
3329
3330         if (!transport->get_chap)
3331                 return -EINVAL;
3332
3333         priv = iscsi_if_transport_lookup(transport);
3334         if (!priv)
3335                 return -EINVAL;
3336
3337         chap_buf_size = (ev->u.get_chap.num_entries * sizeof(*chap_rec));
3338         len = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
3339
3340         shost = scsi_host_lookup(ev->u.get_chap.host_no);
3341         if (!shost) {
3342                 printk(KERN_ERR "%s: failed. Could not find host no %u\n",
3343                        __func__, ev->u.get_chap.host_no);
3344                 return -ENODEV;
3345         }
3346
3347         do {
3348                 int actual_size;
3349
3350                 skbchap = alloc_skb(len, GFP_KERNEL);
3351                 if (!skbchap) {
3352                         printk(KERN_ERR "can not deliver chap: OOM\n");
3353                         err = -ENOMEM;
3354                         goto exit_get_chap;
3355                 }
3356
3357                 nlhchap = __nlmsg_put(skbchap, 0, 0, 0,
3358                                       (len - sizeof(*nlhchap)), 0);
3359                 evchap = nlmsg_data(nlhchap);
3360                 memset(evchap, 0, sizeof(*evchap));
3361                 evchap->transport_handle = iscsi_handle(transport);
3362                 evchap->type = nlh->nlmsg_type;
3363                 evchap->u.get_chap.host_no = ev->u.get_chap.host_no;
3364                 evchap->u.get_chap.chap_tbl_idx = ev->u.get_chap.chap_tbl_idx;
3365                 evchap->u.get_chap.num_entries = ev->u.get_chap.num_entries;
3366                 buf = (char *)evchap + sizeof(*evchap);
3367                 memset(buf, 0, chap_buf_size);
3368
3369                 err = transport->get_chap(shost, ev->u.get_chap.chap_tbl_idx,
3370                                     &evchap->u.get_chap.num_entries, buf);
3371
3372                 actual_size = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
3373                 skb_trim(skbchap, NLMSG_ALIGN(actual_size));
3374                 nlhchap->nlmsg_len = actual_size;
3375
3376                 err = iscsi_multicast_skb(skbchap, ISCSI_NL_GRP_ISCSID,
3377                                           GFP_KERNEL);
3378         } while (err < 0 && err != -ECONNREFUSED);
3379
3380 exit_get_chap:
3381         scsi_host_put(shost);
3382         return err;
3383 }
3384
3385 static int iscsi_set_chap(struct iscsi_transport *transport,
3386                           struct iscsi_uevent *ev, uint32_t len)
3387 {
3388         char *data = (char *)ev + sizeof(*ev);
3389         struct Scsi_Host *shost;
3390         int err = 0;
3391
3392         if (!transport->set_chap)
3393                 return -ENOSYS;
3394
3395         shost = scsi_host_lookup(ev->u.set_path.host_no);
3396         if (!shost) {
3397                 pr_err("%s could not find host no %u\n",
3398                        __func__, ev->u.set_path.host_no);
3399                 return -ENODEV;
3400         }
3401
3402         err = transport->set_chap(shost, data, len);
3403         scsi_host_put(shost);
3404         return err;
3405 }
3406
3407 static int iscsi_delete_chap(struct iscsi_transport *transport,
3408                              struct iscsi_uevent *ev)
3409 {
3410         struct Scsi_Host *shost;
3411         int err = 0;
3412
3413         if (!transport->delete_chap)
3414                 return -ENOSYS;
3415
3416         shost = scsi_host_lookup(ev->u.delete_chap.host_no);
3417         if (!shost) {
3418                 printk(KERN_ERR "%s could not find host no %u\n",
3419                        __func__, ev->u.delete_chap.host_no);
3420                 return -ENODEV;
3421         }
3422
3423         err = transport->delete_chap(shost, ev->u.delete_chap.chap_tbl_idx);
3424         scsi_host_put(shost);
3425         return err;
3426 }
3427
3428 static const struct {
3429         enum iscsi_discovery_parent_type value;
3430         char                            *name;
3431 } iscsi_discovery_parent_names[] = {
3432         {ISCSI_DISC_PARENT_UNKNOWN,     "Unknown" },
3433         {ISCSI_DISC_PARENT_SENDTGT,     "Sendtarget" },
3434         {ISCSI_DISC_PARENT_ISNS,        "isns" },
3435 };
3436
3437 char *iscsi_get_discovery_parent_name(int parent_type)
3438 {
3439         int i;
3440         char *state = "Unknown!";
3441
3442         for (i = 0; i < ARRAY_SIZE(iscsi_discovery_parent_names); i++) {
3443                 if (iscsi_discovery_parent_names[i].value & parent_type) {
3444                         state = iscsi_discovery_parent_names[i].name;
3445                         break;
3446                 }
3447         }
3448         return state;
3449 }
3450 EXPORT_SYMBOL_GPL(iscsi_get_discovery_parent_name);
3451
3452 static int iscsi_set_flashnode_param(struct iscsi_transport *transport,
3453                                      struct iscsi_uevent *ev, uint32_t len)
3454 {
3455         char *data = (char *)ev + sizeof(*ev);
3456         struct Scsi_Host *shost;
3457         struct iscsi_bus_flash_session *fnode_sess;
3458         struct iscsi_bus_flash_conn *fnode_conn;
3459         struct device *dev;
3460         uint32_t idx;
3461         int err = 0;
3462
3463         if (!transport->set_flashnode_param) {
3464                 err = -ENOSYS;
3465                 goto exit_set_fnode;
3466         }
3467
3468         shost = scsi_host_lookup(ev->u.set_flashnode.host_no);
3469         if (!shost) {
3470                 pr_err("%s could not find host no %u\n",
3471                        __func__, ev->u.set_flashnode.host_no);
3472                 err = -ENODEV;
3473                 goto exit_set_fnode;
3474         }
3475
3476         idx = ev->u.set_flashnode.flashnode_idx;
3477         fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3478         if (!fnode_sess) {
3479                 pr_err("%s could not find flashnode %u for host no %u\n",
3480                        __func__, idx, ev->u.set_flashnode.host_no);
3481                 err = -ENODEV;
3482                 goto put_host;
3483         }
3484
3485         dev = iscsi_find_flashnode_conn(fnode_sess);
3486         if (!dev) {
3487                 err = -ENODEV;
3488                 goto put_sess;
3489         }
3490
3491         fnode_conn = iscsi_dev_to_flash_conn(dev);
3492         err = transport->set_flashnode_param(fnode_sess, fnode_conn, data, len);
3493         put_device(dev);
3494
3495 put_sess:
3496         put_device(&fnode_sess->dev);
3497
3498 put_host:
3499         scsi_host_put(shost);
3500
3501 exit_set_fnode:
3502         return err;
3503 }
3504
3505 static int iscsi_new_flashnode(struct iscsi_transport *transport,
3506                                struct iscsi_uevent *ev, uint32_t len)
3507 {
3508         char *data = (char *)ev + sizeof(*ev);
3509         struct Scsi_Host *shost;
3510         int index;
3511         int err = 0;
3512
3513         if (!transport->new_flashnode) {
3514                 err = -ENOSYS;
3515                 goto exit_new_fnode;
3516         }
3517
3518         shost = scsi_host_lookup(ev->u.new_flashnode.host_no);
3519         if (!shost) {
3520                 pr_err("%s could not find host no %u\n",
3521                        __func__, ev->u.new_flashnode.host_no);
3522                 err = -ENODEV;
3523                 goto put_host;
3524         }
3525
3526         index = transport->new_flashnode(shost, data, len);
3527
3528         if (index >= 0)
3529                 ev->r.new_flashnode_ret.flashnode_idx = index;
3530         else
3531                 err = -EIO;
3532
3533 put_host:
3534         scsi_host_put(shost);
3535
3536 exit_new_fnode:
3537         return err;
3538 }
3539
3540 static int iscsi_del_flashnode(struct iscsi_transport *transport,
3541                                struct iscsi_uevent *ev)
3542 {
3543         struct Scsi_Host *shost;
3544         struct iscsi_bus_flash_session *fnode_sess;
3545         uint32_t idx;
3546         int err = 0;
3547
3548         if (!transport->del_flashnode) {
3549                 err = -ENOSYS;
3550                 goto exit_del_fnode;
3551         }
3552
3553         shost = scsi_host_lookup(ev->u.del_flashnode.host_no);
3554         if (!shost) {
3555                 pr_err("%s could not find host no %u\n",
3556                        __func__, ev->u.del_flashnode.host_no);
3557                 err = -ENODEV;
3558                 goto put_host;
3559         }
3560
3561         idx = ev->u.del_flashnode.flashnode_idx;
3562         fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3563         if (!fnode_sess) {
3564                 pr_err("%s could not find flashnode %u for host no %u\n",
3565                        __func__, idx, ev->u.del_flashnode.host_no);
3566                 err = -ENODEV;
3567                 goto put_host;
3568         }
3569
3570         err = transport->del_flashnode(fnode_sess);
3571         put_device(&fnode_sess->dev);
3572
3573 put_host:
3574         scsi_host_put(shost);
3575
3576 exit_del_fnode:
3577         return err;
3578 }
3579
3580 static int iscsi_login_flashnode(struct iscsi_transport *transport,
3581                                  struct iscsi_uevent *ev)
3582 {
3583         struct Scsi_Host *shost;
3584         struct iscsi_bus_flash_session *fnode_sess;
3585         struct iscsi_bus_flash_conn *fnode_conn;
3586         struct device *dev;
3587         uint32_t idx;
3588         int err = 0;
3589
3590         if (!transport->login_flashnode) {
3591                 err = -ENOSYS;
3592                 goto exit_login_fnode;
3593         }
3594
3595         shost = scsi_host_lookup(ev->u.login_flashnode.host_no);
3596         if (!shost) {
3597                 pr_err("%s could not find host no %u\n",
3598                        __func__, ev->u.login_flashnode.host_no);
3599                 err = -ENODEV;
3600                 goto put_host;
3601         }
3602
3603         idx = ev->u.login_flashnode.flashnode_idx;
3604         fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3605         if (!fnode_sess) {
3606                 pr_err("%s could not find flashnode %u for host no %u\n",
3607                        __func__, idx, ev->u.login_flashnode.host_no);
3608                 err = -ENODEV;
3609                 goto put_host;
3610         }
3611
3612         dev = iscsi_find_flashnode_conn(fnode_sess);
3613         if (!dev) {
3614                 err = -ENODEV;
3615                 goto put_sess;
3616         }
3617
3618         fnode_conn = iscsi_dev_to_flash_conn(dev);
3619         err = transport->login_flashnode(fnode_sess, fnode_conn);
3620         put_device(dev);
3621
3622 put_sess:
3623         put_device(&fnode_sess->dev);
3624
3625 put_host:
3626         scsi_host_put(shost);
3627
3628 exit_login_fnode:
3629         return err;
3630 }
3631
3632 static int iscsi_logout_flashnode(struct iscsi_transport *transport,
3633                                   struct iscsi_uevent *ev)
3634 {
3635         struct Scsi_Host *shost;
3636         struct iscsi_bus_flash_session *fnode_sess;
3637         struct iscsi_bus_flash_conn *fnode_conn;
3638         struct device *dev;
3639         uint32_t idx;
3640         int err = 0;
3641
3642         if (!transport->logout_flashnode) {
3643                 err = -ENOSYS;
3644                 goto exit_logout_fnode;
3645         }
3646
3647         shost = scsi_host_lookup(ev->u.logout_flashnode.host_no);
3648         if (!shost) {
3649                 pr_err("%s could not find host no %u\n",
3650                        __func__, ev->u.logout_flashnode.host_no);
3651                 err = -ENODEV;
3652                 goto put_host;
3653         }
3654
3655         idx = ev->u.logout_flashnode.flashnode_idx;
3656         fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3657         if (!fnode_sess) {
3658                 pr_err("%s could not find flashnode %u for host no %u\n",
3659                        __func__, idx, ev->u.logout_flashnode.host_no);
3660                 err = -ENODEV;
3661                 goto put_host;
3662         }
3663
3664         dev = iscsi_find_flashnode_conn(fnode_sess);
3665         if (!dev) {
3666                 err = -ENODEV;
3667                 goto put_sess;
3668         }
3669
3670         fnode_conn = iscsi_dev_to_flash_conn(dev);
3671
3672         err = transport->logout_flashnode(fnode_sess, fnode_conn);
3673         put_device(dev);
3674
3675 put_sess:
3676         put_device(&fnode_sess->dev);
3677
3678 put_host:
3679         scsi_host_put(shost);
3680
3681 exit_logout_fnode:
3682         return err;
3683 }
3684
3685 static int iscsi_logout_flashnode_sid(struct iscsi_transport *transport,
3686                                       struct iscsi_uevent *ev)
3687 {
3688         struct Scsi_Host *shost;
3689         struct iscsi_cls_session *session;
3690         int err = 0;
3691
3692         if (!transport->logout_flashnode_sid) {
3693                 err = -ENOSYS;
3694                 goto exit_logout_sid;
3695         }
3696
3697         shost = scsi_host_lookup(ev->u.logout_flashnode_sid.host_no);
3698         if (!shost) {
3699                 pr_err("%s could not find host no %u\n",
3700                        __func__, ev->u.logout_flashnode.host_no);
3701                 err = -ENODEV;
3702                 goto put_host;
3703         }
3704
3705         session = iscsi_session_lookup(ev->u.logout_flashnode_sid.sid);
3706         if (!session) {
3707                 pr_err("%s could not find session id %u\n",
3708                        __func__, ev->u.logout_flashnode_sid.sid);
3709                 err = -EINVAL;
3710                 goto put_host;
3711         }
3712
3713         err = transport->logout_flashnode_sid(session);
3714
3715 put_host:
3716         scsi_host_put(shost);
3717
3718 exit_logout_sid:
3719         return err;
3720 }
3721
3722 static int
3723 iscsi_get_host_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
3724 {
3725         struct iscsi_uevent *ev = nlmsg_data(nlh);
3726         struct Scsi_Host *shost = NULL;
3727         struct iscsi_internal *priv;
3728         struct sk_buff *skbhost_stats;
3729         struct nlmsghdr *nlhhost_stats;
3730         struct iscsi_uevent *evhost_stats;
3731         int host_stats_size = 0;
3732         int len, err = 0;
3733         char *buf;
3734
3735         if (!transport->get_host_stats)
3736                 return -ENOSYS;
3737
3738         priv = iscsi_if_transport_lookup(transport);
3739         if (!priv)
3740                 return -EINVAL;
3741
3742         host_stats_size = sizeof(struct iscsi_offload_host_stats);
3743         len = nlmsg_total_size(sizeof(*ev) + host_stats_size);
3744
3745         shost = scsi_host_lookup(ev->u.get_host_stats.host_no);
3746         if (!shost) {
3747                 pr_err("%s: failed. Could not find host no %u\n",
3748                        __func__, ev->u.get_host_stats.host_no);
3749                 return -ENODEV;
3750         }
3751
3752         do {
3753                 int actual_size;
3754
3755                 skbhost_stats = alloc_skb(len, GFP_KERNEL);
3756                 if (!skbhost_stats) {
3757                         pr_err("cannot deliver host stats: OOM\n");
3758                         err = -ENOMEM;
3759                         goto exit_host_stats;
3760                 }
3761
3762                 nlhhost_stats = __nlmsg_put(skbhost_stats, 0, 0, 0,
3763                                       (len - sizeof(*nlhhost_stats)), 0);
3764                 evhost_stats = nlmsg_data(nlhhost_stats);
3765                 memset(evhost_stats, 0, sizeof(*evhost_stats));
3766                 evhost_stats->transport_handle = iscsi_handle(transport);
3767                 evhost_stats->type = nlh->nlmsg_type;
3768                 evhost_stats->u.get_host_stats.host_no =
3769                                         ev->u.get_host_stats.host_no;
3770                 buf = (char *)evhost_stats + sizeof(*evhost_stats);
3771                 memset(buf, 0, host_stats_size);
3772
3773                 err = transport->get_host_stats(shost, buf, host_stats_size);
3774                 if (err) {
3775                         kfree_skb(skbhost_stats);
3776                         goto exit_host_stats;
3777                 }
3778
3779                 actual_size = nlmsg_total_size(sizeof(*ev) + host_stats_size);
3780                 skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));
3781                 nlhhost_stats->nlmsg_len = actual_size;
3782
3783                 err = iscsi_multicast_skb(skbhost_stats, ISCSI_NL_GRP_ISCSID,
3784                                           GFP_KERNEL);
3785         } while (err < 0 && err != -ECONNREFUSED);
3786
3787 exit_host_stats:
3788         scsi_host_put(shost);
3789         return err;
3790 }
3791
3792 static int iscsi_if_transport_conn(struct iscsi_transport *transport,
3793                                    struct nlmsghdr *nlh)
3794 {
3795         struct iscsi_uevent *ev = nlmsg_data(nlh);
3796         struct iscsi_cls_session *session;
3797         struct iscsi_cls_conn *conn = NULL;
3798         struct iscsi_endpoint *ep;
3799         uint32_t pdu_len;
3800         int err = 0;
3801
3802         switch (nlh->nlmsg_type) {
3803         case ISCSI_UEVENT_CREATE_CONN:
3804                 return iscsi_if_create_conn(transport, ev);
3805         case ISCSI_UEVENT_DESTROY_CONN:
3806                 return iscsi_if_destroy_conn(transport, ev);
3807         case ISCSI_UEVENT_STOP_CONN:
3808                 conn = iscsi_conn_lookup(ev->u.stop_conn.sid,
3809                                          ev->u.stop_conn.cid);
3810                 if (!conn)
3811                         return -EINVAL;
3812
3813                 return iscsi_if_stop_conn(conn, ev->u.stop_conn.flag);
3814         }
3815
3816         /*
3817          * The following cmds need to be run under the ep_mutex so in kernel
3818          * conn cleanup (ep_disconnect + unbind and conn) is not done while
3819          * these are running. They also must not run if we have just run a conn
3820          * cleanup because they would set the state in a way that might allow
3821          * IO or send IO themselves.
3822          */
3823         switch (nlh->nlmsg_type) {
3824         case ISCSI_UEVENT_START_CONN:
3825                 conn = iscsi_conn_lookup(ev->u.start_conn.sid,
3826                                          ev->u.start_conn.cid);
3827                 break;
3828         case ISCSI_UEVENT_BIND_CONN:
3829                 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
3830                 break;
3831         case ISCSI_UEVENT_SEND_PDU:
3832                 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
3833                 break;
3834         }
3835
3836         if (!conn)
3837                 return -EINVAL;
3838
3839         mutex_lock(&conn->ep_mutex);
3840         spin_lock_irq(&conn->lock);
3841         if (test_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags)) {
3842                 spin_unlock_irq(&conn->lock);
3843                 mutex_unlock(&conn->ep_mutex);
3844                 ev->r.retcode = -ENOTCONN;
3845                 return 0;
3846         }
3847         spin_unlock_irq(&conn->lock);
3848
3849         switch (nlh->nlmsg_type) {
3850         case ISCSI_UEVENT_BIND_CONN:
3851                 session = iscsi_session_lookup(ev->u.b_conn.sid);
3852                 if (!session) {
3853                         err = -EINVAL;
3854                         break;
3855                 }
3856
3857                 ev->r.retcode = transport->bind_conn(session, conn,
3858                                                 ev->u.b_conn.transport_eph,
3859                                                 ev->u.b_conn.is_leading);
3860                 if (!ev->r.retcode)
3861                         WRITE_ONCE(conn->state, ISCSI_CONN_BOUND);
3862
3863                 if (ev->r.retcode || !transport->ep_connect)
3864                         break;
3865
3866                 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
3867                 if (ep) {
3868                         ep->conn = conn;
3869                         conn->ep = ep;
3870                         iscsi_put_endpoint(ep);
3871                 } else {
3872                         err = -ENOTCONN;
3873                         iscsi_cls_conn_printk(KERN_ERR, conn,
3874                                               "Could not set ep conn binding\n");
3875                 }
3876                 break;
3877         case ISCSI_UEVENT_START_CONN:
3878                 ev->r.retcode = transport->start_conn(conn);
3879                 if (!ev->r.retcode)
3880                         WRITE_ONCE(conn->state, ISCSI_CONN_UP);
3881
3882                 break;
3883         case ISCSI_UEVENT_SEND_PDU:
3884                 pdu_len = nlh->nlmsg_len - sizeof(*nlh) - sizeof(*ev);
3885
3886                 if ((ev->u.send_pdu.hdr_size > pdu_len) ||
3887                     (ev->u.send_pdu.data_size > (pdu_len - ev->u.send_pdu.hdr_size))) {
3888                         err = -EINVAL;
3889                         break;
3890                 }
3891
3892                 ev->r.retcode = transport->send_pdu(conn,
3893                                 (struct iscsi_hdr *)((char *)ev + sizeof(*ev)),
3894                                 (char *)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
3895                                 ev->u.send_pdu.data_size);
3896                 break;
3897         default:
3898                 err = -ENOSYS;
3899         }
3900
3901         mutex_unlock(&conn->ep_mutex);
3902         return err;
3903 }
3904
3905 static int
3906 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
3907 {
3908         int err = 0;
3909         u32 portid;
3910         struct iscsi_uevent *ev = nlmsg_data(nlh);
3911         struct iscsi_transport *transport = NULL;
3912         struct iscsi_internal *priv;
3913         struct iscsi_cls_session *session;
3914         struct iscsi_endpoint *ep = NULL;
3915
3916         if (!netlink_capable(skb, CAP_SYS_ADMIN))
3917                 return -EPERM;
3918
3919         if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
3920                 *group = ISCSI_NL_GRP_UIP;
3921         else
3922                 *group = ISCSI_NL_GRP_ISCSID;
3923
3924         priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
3925         if (!priv)
3926                 return -EINVAL;
3927         transport = priv->iscsi_transport;
3928
3929         if (!try_module_get(transport->owner))
3930                 return -EINVAL;
3931
3932         portid = NETLINK_CB(skb).portid;
3933
3934         switch (nlh->nlmsg_type) {
3935         case ISCSI_UEVENT_CREATE_SESSION:
3936                 err = iscsi_if_create_session(priv, ep, ev,
3937                                               portid,
3938                                               ev->u.c_session.initial_cmdsn,
3939                                               ev->u.c_session.cmds_max,
3940                                               ev->u.c_session.queue_depth);
3941                 break;
3942         case ISCSI_UEVENT_CREATE_BOUND_SESSION:
3943                 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
3944                 if (!ep) {
3945                         err = -EINVAL;
3946                         break;
3947                 }
3948
3949                 err = iscsi_if_create_session(priv, ep, ev,
3950                                         portid,
3951                                         ev->u.c_bound_session.initial_cmdsn,
3952                                         ev->u.c_bound_session.cmds_max,
3953                                         ev->u.c_bound_session.queue_depth);
3954                 iscsi_put_endpoint(ep);
3955                 break;
3956         case ISCSI_UEVENT_DESTROY_SESSION:
3957                 session = iscsi_session_lookup(ev->u.d_session.sid);
3958                 if (!session)
3959                         err = -EINVAL;
3960                 else if (iscsi_session_has_conns(ev->u.d_session.sid))
3961                         err = -EBUSY;
3962                 else
3963                         transport->destroy_session(session);
3964                 break;
3965         case ISCSI_UEVENT_DESTROY_SESSION_ASYNC:
3966                 session = iscsi_session_lookup(ev->u.d_session.sid);
3967                 if (!session)
3968                         err = -EINVAL;
3969                 else if (iscsi_session_has_conns(ev->u.d_session.sid))
3970                         err = -EBUSY;
3971                 else {
3972                         unsigned long flags;
3973
3974                         /* Prevent this session from being found again */
3975                         spin_lock_irqsave(&sesslock, flags);
3976                         list_del_init(&session->sess_list);
3977                         spin_unlock_irqrestore(&sesslock, flags);
3978
3979                         queue_work(system_unbound_wq, &session->destroy_work);
3980                 }
3981                 break;
3982         case ISCSI_UEVENT_UNBIND_SESSION:
3983                 session = iscsi_session_lookup(ev->u.d_session.sid);
3984                 if (session)
3985                         scsi_queue_work(iscsi_session_to_shost(session),
3986                                         &session->unbind_work);
3987                 else
3988                         err = -EINVAL;
3989                 break;
3990         case ISCSI_UEVENT_SET_PARAM:
3991                 err = iscsi_set_param(transport, ev);
3992                 break;
3993         case ISCSI_UEVENT_CREATE_CONN:
3994         case ISCSI_UEVENT_DESTROY_CONN:
3995         case ISCSI_UEVENT_STOP_CONN:
3996         case ISCSI_UEVENT_START_CONN:
3997         case ISCSI_UEVENT_BIND_CONN:
3998         case ISCSI_UEVENT_SEND_PDU:
3999                 err = iscsi_if_transport_conn(transport, nlh);
4000                 break;
4001         case ISCSI_UEVENT_GET_STATS:
4002                 err = iscsi_if_get_stats(transport, nlh);
4003                 break;
4004         case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
4005         case ISCSI_UEVENT_TRANSPORT_EP_POLL:
4006         case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
4007         case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
4008                 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
4009                 break;
4010         case ISCSI_UEVENT_TGT_DSCVR:
4011                 err = iscsi_tgt_dscvr(transport, ev);
4012                 break;
4013         case ISCSI_UEVENT_SET_HOST_PARAM:
4014                 err = iscsi_set_host_param(transport, ev);
4015                 break;
4016         case ISCSI_UEVENT_PATH_UPDATE:
4017                 err = iscsi_set_path(transport, ev);
4018                 break;
4019         case ISCSI_UEVENT_SET_IFACE_PARAMS:
4020                 err = iscsi_set_iface_params(transport, ev,
4021                                              nlmsg_attrlen(nlh, sizeof(*ev)));
4022                 break;
4023         case ISCSI_UEVENT_PING:
4024                 err = iscsi_send_ping(transport, ev);
4025                 break;
4026         case ISCSI_UEVENT_GET_CHAP:
4027                 err = iscsi_get_chap(transport, nlh);
4028                 break;
4029         case ISCSI_UEVENT_DELETE_CHAP:
4030                 err = iscsi_delete_chap(transport, ev);
4031                 break;
4032         case ISCSI_UEVENT_SET_FLASHNODE_PARAMS:
4033                 err = iscsi_set_flashnode_param(transport, ev,
4034                                                 nlmsg_attrlen(nlh,
4035                                                               sizeof(*ev)));
4036                 break;
4037         case ISCSI_UEVENT_NEW_FLASHNODE:
4038                 err = iscsi_new_flashnode(transport, ev,
4039                                           nlmsg_attrlen(nlh, sizeof(*ev)));
4040                 break;
4041         case ISCSI_UEVENT_DEL_FLASHNODE:
4042                 err = iscsi_del_flashnode(transport, ev);
4043                 break;
4044         case ISCSI_UEVENT_LOGIN_FLASHNODE:
4045                 err = iscsi_login_flashnode(transport, ev);
4046                 break;
4047         case ISCSI_UEVENT_LOGOUT_FLASHNODE:
4048                 err = iscsi_logout_flashnode(transport, ev);
4049                 break;
4050         case ISCSI_UEVENT_LOGOUT_FLASHNODE_SID:
4051                 err = iscsi_logout_flashnode_sid(transport, ev);
4052                 break;
4053         case ISCSI_UEVENT_SET_CHAP:
4054                 err = iscsi_set_chap(transport, ev,
4055                                      nlmsg_attrlen(nlh, sizeof(*ev)));
4056                 break;
4057         case ISCSI_UEVENT_GET_HOST_STATS:
4058                 err = iscsi_get_host_stats(transport, nlh);
4059                 break;
4060         default:
4061                 err = -ENOSYS;
4062                 break;
4063         }
4064
4065         module_put(transport->owner);
4066         return err;
4067 }
4068
4069 /*
4070  * Get message from skb.  Each message is processed by iscsi_if_recv_msg.
4071  * Malformed skbs with wrong lengths or invalid creds are not processed.
4072  */
4073 static void
4074 iscsi_if_rx(struct sk_buff *skb)
4075 {
4076         u32 portid = NETLINK_CB(skb).portid;
4077
4078         mutex_lock(&rx_queue_mutex);
4079         while (skb->len >= NLMSG_HDRLEN) {
4080                 int err;
4081                 uint32_t rlen;
4082                 struct nlmsghdr *nlh;
4083                 struct iscsi_uevent *ev;
4084                 uint32_t group;
4085                 int retries = ISCSI_SEND_MAX_ALLOWED;
4086
4087                 nlh = nlmsg_hdr(skb);
4088                 if (nlh->nlmsg_len < sizeof(*nlh) + sizeof(*ev) ||
4089                     skb->len < nlh->nlmsg_len) {
4090                         break;
4091                 }
4092
4093                 ev = nlmsg_data(nlh);
4094                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
4095                 if (rlen > skb->len)
4096                         rlen = skb->len;
4097
4098                 err = iscsi_if_recv_msg(skb, nlh, &group);
4099                 if (err) {
4100                         ev->type = ISCSI_KEVENT_IF_ERROR;
4101                         ev->iferror = err;
4102                 }
4103                 do {
4104                         /*
4105                          * special case for GET_STATS:
4106                          * on success - sending reply and stats from
4107                          * inside of if_recv_msg(),
4108                          * on error - fall through.
4109                          */
4110                         if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
4111                                 break;
4112                         if (ev->type == ISCSI_UEVENT_GET_CHAP && !err)
4113                                 break;
4114                         err = iscsi_if_send_reply(portid, nlh->nlmsg_type,
4115                                                   ev, sizeof(*ev));
4116                         if (err == -EAGAIN && --retries < 0) {
4117                                 printk(KERN_WARNING "Send reply failed, error %d\n", err);
4118                                 break;
4119                         }
4120                 } while (err < 0 && err != -ECONNREFUSED && err != -ESRCH);
4121                 skb_pull(skb, rlen);
4122         }
4123         mutex_unlock(&rx_queue_mutex);
4124 }
4125
4126 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store)              \
4127 struct device_attribute dev_attr_##_prefix##_##_name =  \
4128         __ATTR(_name,_mode,_show,_store)
4129
4130 /*
4131  * iSCSI connection attrs
4132  */
4133 #define iscsi_conn_attr_show(param)                                     \
4134 static ssize_t                                                          \
4135 show_conn_param_##param(struct device *dev,                             \
4136                         struct device_attribute *attr, char *buf)       \
4137 {                                                                       \
4138         struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);   \
4139         struct iscsi_transport *t = conn->transport;                    \
4140         return t->get_conn_param(conn, param, buf);                     \
4141 }
4142
4143 #define iscsi_conn_attr(field, param)                                   \
4144         iscsi_conn_attr_show(param)                                     \
4145 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param,  \
4146                         NULL);
4147
4148 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
4149 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
4150 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
4151 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
4152 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
4153 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
4154 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
4155 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
4156 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
4157 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
4158 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
4159 iscsi_conn_attr(local_port, ISCSI_PARAM_LOCAL_PORT);
4160 iscsi_conn_attr(statsn, ISCSI_PARAM_STATSN);
4161 iscsi_conn_attr(keepalive_tmo, ISCSI_PARAM_KEEPALIVE_TMO);
4162 iscsi_conn_attr(max_segment_size, ISCSI_PARAM_MAX_SEGMENT_SIZE);
4163 iscsi_conn_attr(tcp_timestamp_stat, ISCSI_PARAM_TCP_TIMESTAMP_STAT);
4164 iscsi_conn_attr(tcp_wsf_disable, ISCSI_PARAM_TCP_WSF_DISABLE);
4165 iscsi_conn_attr(tcp_nagle_disable, ISCSI_PARAM_TCP_NAGLE_DISABLE);
4166 iscsi_conn_attr(tcp_timer_scale, ISCSI_PARAM_TCP_TIMER_SCALE);
4167 iscsi_conn_attr(tcp_timestamp_enable, ISCSI_PARAM_TCP_TIMESTAMP_EN);
4168 iscsi_conn_attr(fragment_disable, ISCSI_PARAM_IP_FRAGMENT_DISABLE);
4169 iscsi_conn_attr(ipv4_tos, ISCSI_PARAM_IPV4_TOS);
4170 iscsi_conn_attr(ipv6_traffic_class, ISCSI_PARAM_IPV6_TC);
4171 iscsi_conn_attr(ipv6_flow_label, ISCSI_PARAM_IPV6_FLOW_LABEL);
4172 iscsi_conn_attr(is_fw_assigned_ipv6, ISCSI_PARAM_IS_FW_ASSIGNED_IPV6);
4173 iscsi_conn_attr(tcp_xmit_wsf, ISCSI_PARAM_TCP_XMIT_WSF);
4174 iscsi_conn_attr(tcp_recv_wsf, ISCSI_PARAM_TCP_RECV_WSF);
4175 iscsi_conn_attr(local_ipaddr, ISCSI_PARAM_LOCAL_IPADDR);
4176
4177 static const char *const connection_state_names[] = {
4178         [ISCSI_CONN_UP] = "up",
4179         [ISCSI_CONN_DOWN] = "down",
4180         [ISCSI_CONN_FAILED] = "failed",
4181         [ISCSI_CONN_BOUND] = "bound"
4182 };
4183
4184 static ssize_t show_conn_state(struct device *dev,
4185                                struct device_attribute *attr, char *buf)
4186 {
4187         struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);
4188         const char *state = "unknown";
4189         int conn_state = READ_ONCE(conn->state);
4190
4191         if (conn_state >= 0 &&
4192             conn_state < ARRAY_SIZE(connection_state_names))
4193                 state = connection_state_names[conn_state];
4194
4195         return sysfs_emit(buf, "%s\n", state);
4196 }
4197 static ISCSI_CLASS_ATTR(conn, state, S_IRUGO, show_conn_state,
4198                         NULL);
4199
4200 #define iscsi_conn_ep_attr_show(param)                                  \
4201 static ssize_t show_conn_ep_param_##param(struct device *dev,           \
4202                                           struct device_attribute *attr,\
4203                                           char *buf)                    \
4204 {                                                                       \
4205         struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);   \
4206         struct iscsi_transport *t = conn->transport;                    \
4207         struct iscsi_endpoint *ep;                                      \
4208         ssize_t rc;                                                     \
4209                                                                         \
4210         /*                                                              \
4211          * Need to make sure ep_disconnect does not free the LLD's      \
4212          * interconnect resources while we are trying to read them.     \
4213          */                                                             \
4214         mutex_lock(&conn->ep_mutex);                                    \
4215         ep = conn->ep;                                                  \
4216         if (!ep && t->ep_connect) {                                     \
4217                 mutex_unlock(&conn->ep_mutex);                          \
4218                 return -ENOTCONN;                                       \
4219         }                                                               \
4220                                                                         \
4221         if (ep)                                                         \
4222                 rc = t->get_ep_param(ep, param, buf);                   \
4223         else                                                            \
4224                 rc = t->get_conn_param(conn, param, buf);               \
4225         mutex_unlock(&conn->ep_mutex);                                  \
4226         return rc;                                                      \
4227 }
4228
4229 #define iscsi_conn_ep_attr(field, param)                                \
4230         iscsi_conn_ep_attr_show(param)                                  \
4231 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO,                           \
4232                         show_conn_ep_param_##param, NULL);
4233
4234 iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
4235 iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
4236
4237 static struct attribute *iscsi_conn_attrs[] = {
4238         &dev_attr_conn_max_recv_dlength.attr,
4239         &dev_attr_conn_max_xmit_dlength.attr,
4240         &dev_attr_conn_header_digest.attr,
4241         &dev_attr_conn_data_digest.attr,
4242         &dev_attr_conn_ifmarker.attr,
4243         &dev_attr_conn_ofmarker.attr,
4244         &dev_attr_conn_address.attr,
4245         &dev_attr_conn_port.attr,
4246         &dev_attr_conn_exp_statsn.attr,
4247         &dev_attr_conn_persistent_address.attr,
4248         &dev_attr_conn_persistent_port.attr,
4249         &dev_attr_conn_ping_tmo.attr,
4250         &dev_attr_conn_recv_tmo.attr,
4251         &dev_attr_conn_local_port.attr,
4252         &dev_attr_conn_statsn.attr,
4253         &dev_attr_conn_keepalive_tmo.attr,
4254         &dev_attr_conn_max_segment_size.attr,
4255         &dev_attr_conn_tcp_timestamp_stat.attr,
4256         &dev_attr_conn_tcp_wsf_disable.attr,
4257         &dev_attr_conn_tcp_nagle_disable.attr,
4258         &dev_attr_conn_tcp_timer_scale.attr,
4259         &dev_attr_conn_tcp_timestamp_enable.attr,
4260         &dev_attr_conn_fragment_disable.attr,
4261         &dev_attr_conn_ipv4_tos.attr,
4262         &dev_attr_conn_ipv6_traffic_class.attr,
4263         &dev_attr_conn_ipv6_flow_label.attr,
4264         &dev_attr_conn_is_fw_assigned_ipv6.attr,
4265         &dev_attr_conn_tcp_xmit_wsf.attr,
4266         &dev_attr_conn_tcp_recv_wsf.attr,
4267         &dev_attr_conn_local_ipaddr.attr,
4268         &dev_attr_conn_state.attr,
4269         NULL,
4270 };
4271
4272 static umode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
4273                                          struct attribute *attr, int i)
4274 {
4275         struct device *cdev = container_of(kobj, struct device, kobj);
4276         struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
4277         struct iscsi_transport *t = conn->transport;
4278         int param;
4279
4280         if (attr == &dev_attr_conn_max_recv_dlength.attr)
4281                 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
4282         else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
4283                 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
4284         else if (attr == &dev_attr_conn_header_digest.attr)
4285                 param = ISCSI_PARAM_HDRDGST_EN;
4286         else if (attr == &dev_attr_conn_data_digest.attr)
4287                 param = ISCSI_PARAM_DATADGST_EN;
4288         else if (attr == &dev_attr_conn_ifmarker.attr)
4289                 param = ISCSI_PARAM_IFMARKER_EN;
4290         else if (attr == &dev_attr_conn_ofmarker.attr)
4291                 param = ISCSI_PARAM_OFMARKER_EN;
4292         else if (attr == &dev_attr_conn_address.attr)
4293                 param = ISCSI_PARAM_CONN_ADDRESS;
4294         else if (attr == &dev_attr_conn_port.attr)
4295                 param = ISCSI_PARAM_CONN_PORT;
4296         else if (attr == &dev_attr_conn_exp_statsn.attr)
4297                 param = ISCSI_PARAM_EXP_STATSN;
4298         else if (attr == &dev_attr_conn_persistent_address.attr)
4299                 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
4300         else if (attr == &dev_attr_conn_persistent_port.attr)
4301                 param = ISCSI_PARAM_PERSISTENT_PORT;
4302         else if (attr == &dev_attr_conn_ping_tmo.attr)
4303                 param = ISCSI_PARAM_PING_TMO;
4304         else if (attr == &dev_attr_conn_recv_tmo.attr)
4305                 param = ISCSI_PARAM_RECV_TMO;
4306         else if (attr == &dev_attr_conn_local_port.attr)
4307                 param = ISCSI_PARAM_LOCAL_PORT;
4308         else if (attr == &dev_attr_conn_statsn.attr)
4309                 param = ISCSI_PARAM_STATSN;
4310         else if (attr == &dev_attr_conn_keepalive_tmo.attr)
4311                 param = ISCSI_PARAM_KEEPALIVE_TMO;
4312         else if (attr == &dev_attr_conn_max_segment_size.attr)
4313                 param = ISCSI_PARAM_MAX_SEGMENT_SIZE;
4314         else if (attr == &dev_attr_conn_tcp_timestamp_stat.attr)
4315                 param = ISCSI_PARAM_TCP_TIMESTAMP_STAT;
4316         else if (attr == &dev_attr_conn_tcp_wsf_disable.attr)
4317                 param = ISCSI_PARAM_TCP_WSF_DISABLE;
4318         else if (attr == &dev_attr_conn_tcp_nagle_disable.attr)
4319                 param = ISCSI_PARAM_TCP_NAGLE_DISABLE;
4320         else if (attr == &dev_attr_conn_tcp_timer_scale.attr)
4321                 param = ISCSI_PARAM_TCP_TIMER_SCALE;
4322         else if (attr == &dev_attr_conn_tcp_timestamp_enable.attr)
4323                 param = ISCSI_PARAM_TCP_TIMESTAMP_EN;
4324         else if (attr == &dev_attr_conn_fragment_disable.attr)
4325                 param = ISCSI_PARAM_IP_FRAGMENT_DISABLE;
4326         else if (attr == &dev_attr_conn_ipv4_tos.attr)
4327                 param = ISCSI_PARAM_IPV4_TOS;
4328         else if (attr == &dev_attr_conn_ipv6_traffic_class.attr)
4329                 param = ISCSI_PARAM_IPV6_TC;
4330         else if (attr == &dev_attr_conn_ipv6_flow_label.attr)
4331                 param = ISCSI_PARAM_IPV6_FLOW_LABEL;
4332         else if (attr == &dev_attr_conn_is_fw_assigned_ipv6.attr)
4333                 param = ISCSI_PARAM_IS_FW_ASSIGNED_IPV6;
4334         else if (attr == &dev_attr_conn_tcp_xmit_wsf.attr)
4335                 param = ISCSI_PARAM_TCP_XMIT_WSF;
4336         else if (attr == &dev_attr_conn_tcp_recv_wsf.attr)
4337                 param = ISCSI_PARAM_TCP_RECV_WSF;
4338         else if (attr == &dev_attr_conn_local_ipaddr.attr)
4339                 param = ISCSI_PARAM_LOCAL_IPADDR;
4340         else if (attr == &dev_attr_conn_state.attr)
4341                 return S_IRUGO;
4342         else {
4343                 WARN_ONCE(1, "Invalid conn attr");
4344                 return 0;
4345         }
4346
4347         return t->attr_is_visible(ISCSI_PARAM, param);
4348 }
4349
4350 static struct attribute_group iscsi_conn_group = {
4351         .attrs = iscsi_conn_attrs,
4352         .is_visible = iscsi_conn_attr_is_visible,
4353 };
4354
4355 /*
4356  * iSCSI session attrs
4357  */
4358 #define iscsi_session_attr_show(param, perm)                            \
4359 static ssize_t                                                          \
4360 show_session_param_##param(struct device *dev,                          \
4361                            struct device_attribute *attr, char *buf)    \
4362 {                                                                       \
4363         struct iscsi_cls_session *session =                             \
4364                 iscsi_dev_to_session(dev->parent);                      \
4365         struct iscsi_transport *t = session->transport;                 \
4366                                                                         \
4367         if (perm && !capable(CAP_SYS_ADMIN))                            \
4368                 return -EACCES;                                         \
4369         return t->get_session_param(session, param, buf);               \
4370 }
4371
4372 #define iscsi_session_attr(field, param, perm)                          \
4373         iscsi_session_attr_show(param, perm)                            \
4374 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
4375                         NULL);
4376 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
4377 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
4378 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
4379 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
4380 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
4381 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
4382 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
4383 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
4384 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
4385 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
4386 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
4387 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
4388 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
4389 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
4390 iscsi_session_attr(chap_out_idx, ISCSI_PARAM_CHAP_OUT_IDX, 1);
4391 iscsi_session_attr(chap_in_idx, ISCSI_PARAM_CHAP_IN_IDX, 1);
4392 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
4393 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
4394 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
4395 iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
4396 iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
4397 iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
4398 iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
4399 iscsi_session_attr(boot_root, ISCSI_PARAM_BOOT_ROOT, 0);
4400 iscsi_session_attr(boot_nic, ISCSI_PARAM_BOOT_NIC, 0);
4401 iscsi_session_attr(boot_target, ISCSI_PARAM_BOOT_TARGET, 0);
4402 iscsi_session_attr(auto_snd_tgt_disable, ISCSI_PARAM_AUTO_SND_TGT_DISABLE, 0);
4403 iscsi_session_attr(discovery_session, ISCSI_PARAM_DISCOVERY_SESS, 0);
4404 iscsi_session_attr(portal_type, ISCSI_PARAM_PORTAL_TYPE, 0);
4405 iscsi_session_attr(chap_auth, ISCSI_PARAM_CHAP_AUTH_EN, 0);
4406 iscsi_session_attr(discovery_logout, ISCSI_PARAM_DISCOVERY_LOGOUT_EN, 0);
4407 iscsi_session_attr(bidi_chap, ISCSI_PARAM_BIDI_CHAP_EN, 0);
4408 iscsi_session_attr(discovery_auth_optional,
4409                    ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL, 0);
4410 iscsi_session_attr(def_time2wait, ISCSI_PARAM_DEF_TIME2WAIT, 0);
4411 iscsi_session_attr(def_time2retain, ISCSI_PARAM_DEF_TIME2RETAIN, 0);
4412 iscsi_session_attr(isid, ISCSI_PARAM_ISID, 0);
4413 iscsi_session_attr(tsid, ISCSI_PARAM_TSID, 0);
4414 iscsi_session_attr(def_taskmgmt_tmo, ISCSI_PARAM_DEF_TASKMGMT_TMO, 0);
4415 iscsi_session_attr(discovery_parent_idx, ISCSI_PARAM_DISCOVERY_PARENT_IDX, 0);
4416 iscsi_session_attr(discovery_parent_type, ISCSI_PARAM_DISCOVERY_PARENT_TYPE, 0);
4417
4418 static ssize_t
4419 show_priv_session_target_state(struct device *dev, struct device_attribute *attr,
4420                         char *buf)
4421 {
4422         struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
4423
4424         return sysfs_emit(buf, "%s\n",
4425                         iscsi_session_target_state_name[session->target_state]);
4426 }
4427
4428 static ISCSI_CLASS_ATTR(priv_sess, target_state, S_IRUGO,
4429                         show_priv_session_target_state, NULL);
4430
4431 static ssize_t
4432 show_priv_session_state(struct device *dev, struct device_attribute *attr,
4433                         char *buf)
4434 {
4435         struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
4436         return sysfs_emit(buf, "%s\n", iscsi_session_state_name(session->state));
4437 }
4438 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
4439                         NULL);
4440 static ssize_t
4441 show_priv_session_creator(struct device *dev, struct device_attribute *attr,
4442                         char *buf)
4443 {
4444         struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
4445         return sysfs_emit(buf, "%d\n", session->creator);
4446 }
4447 static ISCSI_CLASS_ATTR(priv_sess, creator, S_IRUGO, show_priv_session_creator,
4448                         NULL);
4449 static ssize_t
4450 show_priv_session_target_id(struct device *dev, struct device_attribute *attr,
4451                             char *buf)
4452 {
4453         struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
4454         return sysfs_emit(buf, "%d\n", session->target_id);
4455 }
4456 static ISCSI_CLASS_ATTR(priv_sess, target_id, S_IRUGO,
4457                         show_priv_session_target_id, NULL);
4458
4459 #define iscsi_priv_session_attr_show(field, format)                     \
4460 static ssize_t                                                          \
4461 show_priv_session_##field(struct device *dev,                           \
4462                           struct device_attribute *attr, char *buf)     \
4463 {                                                                       \
4464         struct iscsi_cls_session *session =                             \
4465                         iscsi_dev_to_session(dev->parent);              \
4466         if (session->field == -1)                                       \
4467                 return sysfs_emit(buf, "off\n");                        \
4468         return sysfs_emit(buf, format"\n", session->field);             \
4469 }
4470
4471 #define iscsi_priv_session_attr_store(field)                            \
4472 static ssize_t                                                          \
4473 store_priv_session_##field(struct device *dev,                          \
4474                            struct device_attribute *attr,               \
4475                            const char *buf, size_t count)               \
4476 {                                                                       \
4477         int val;                                                        \
4478         char *cp;                                                       \
4479         struct iscsi_cls_session *session =                             \
4480                 iscsi_dev_to_session(dev->parent);                      \
4481         if ((session->state == ISCSI_SESSION_FREE) ||                   \
4482             (session->state == ISCSI_SESSION_FAILED))                   \
4483                 return -EBUSY;                                          \
4484         if (strncmp(buf, "off", 3) == 0) {                              \
4485                 session->field = -1;                                    \
4486                 session->field##_sysfs_override = true;                 \
4487         } else {                                                        \
4488                 val = simple_strtoul(buf, &cp, 0);                      \
4489                 if (*cp != '\0' && *cp != '\n')                         \
4490                         return -EINVAL;                                 \
4491                 session->field = val;                                   \
4492                 session->field##_sysfs_override = true;                 \
4493         }                                                               \
4494         return count;                                                   \
4495 }
4496
4497 #define iscsi_priv_session_rw_attr(field, format)                       \
4498         iscsi_priv_session_attr_show(field, format)                     \
4499         iscsi_priv_session_attr_store(field)                            \
4500 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR,            \
4501                         show_priv_session_##field,                      \
4502                         store_priv_session_##field)
4503
4504 iscsi_priv_session_rw_attr(recovery_tmo, "%d");
4505
4506 static struct attribute *iscsi_session_attrs[] = {
4507         &dev_attr_sess_initial_r2t.attr,
4508         &dev_attr_sess_max_outstanding_r2t.attr,
4509         &dev_attr_sess_immediate_data.attr,
4510         &dev_attr_sess_first_burst_len.attr,
4511         &dev_attr_sess_max_burst_len.attr,
4512         &dev_attr_sess_data_pdu_in_order.attr,
4513         &dev_attr_sess_data_seq_in_order.attr,
4514         &dev_attr_sess_erl.attr,
4515         &dev_attr_sess_targetname.attr,
4516         &dev_attr_sess_tpgt.attr,
4517         &dev_attr_sess_password.attr,
4518         &dev_attr_sess_password_in.attr,
4519         &dev_attr_sess_username.attr,
4520         &dev_attr_sess_username_in.attr,
4521         &dev_attr_sess_fast_abort.attr,
4522         &dev_attr_sess_abort_tmo.attr,
4523         &dev_attr_sess_lu_reset_tmo.attr,
4524         &dev_attr_sess_tgt_reset_tmo.attr,
4525         &dev_attr_sess_ifacename.attr,
4526         &dev_attr_sess_initiatorname.attr,
4527         &dev_attr_sess_targetalias.attr,
4528         &dev_attr_sess_boot_root.attr,
4529         &dev_attr_sess_boot_nic.attr,
4530         &dev_attr_sess_boot_target.attr,
4531         &dev_attr_priv_sess_recovery_tmo.attr,
4532         &dev_attr_priv_sess_state.attr,
4533         &dev_attr_priv_sess_target_state.attr,
4534         &dev_attr_priv_sess_creator.attr,
4535         &dev_attr_sess_chap_out_idx.attr,
4536         &dev_attr_sess_chap_in_idx.attr,
4537         &dev_attr_priv_sess_target_id.attr,
4538         &dev_attr_sess_auto_snd_tgt_disable.attr,
4539         &dev_attr_sess_discovery_session.attr,
4540         &dev_attr_sess_portal_type.attr,
4541         &dev_attr_sess_chap_auth.attr,
4542         &dev_attr_sess_discovery_logout.attr,
4543         &dev_attr_sess_bidi_chap.attr,
4544         &dev_attr_sess_discovery_auth_optional.attr,
4545         &dev_attr_sess_def_time2wait.attr,
4546         &dev_attr_sess_def_time2retain.attr,
4547         &dev_attr_sess_isid.attr,
4548         &dev_attr_sess_tsid.attr,
4549         &dev_attr_sess_def_taskmgmt_tmo.attr,
4550         &dev_attr_sess_discovery_parent_idx.attr,
4551         &dev_attr_sess_discovery_parent_type.attr,
4552         NULL,
4553 };
4554
4555 static umode_t iscsi_session_attr_is_visible(struct kobject *kobj,
4556                                             struct attribute *attr, int i)
4557 {
4558         struct device *cdev = container_of(kobj, struct device, kobj);
4559         struct iscsi_cls_session *session = transport_class_to_session(cdev);
4560         struct iscsi_transport *t = session->transport;
4561         int param;
4562
4563         if (attr == &dev_attr_sess_initial_r2t.attr)
4564                 param = ISCSI_PARAM_INITIAL_R2T_EN;
4565         else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
4566                 param = ISCSI_PARAM_MAX_R2T;
4567         else if (attr == &dev_attr_sess_immediate_data.attr)
4568                 param = ISCSI_PARAM_IMM_DATA_EN;
4569         else if (attr == &dev_attr_sess_first_burst_len.attr)
4570                 param = ISCSI_PARAM_FIRST_BURST;
4571         else if (attr == &dev_attr_sess_max_burst_len.attr)
4572                 param = ISCSI_PARAM_MAX_BURST;
4573         else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
4574                 param = ISCSI_PARAM_PDU_INORDER_EN;
4575         else if (attr == &dev_attr_sess_data_seq_in_order.attr)
4576                 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
4577         else if (attr == &dev_attr_sess_erl.attr)
4578                 param = ISCSI_PARAM_ERL;
4579         else if (attr == &dev_attr_sess_targetname.attr)
4580                 param = ISCSI_PARAM_TARGET_NAME;
4581         else if (attr == &dev_attr_sess_tpgt.attr)
4582                 param = ISCSI_PARAM_TPGT;
4583         else if (attr == &dev_attr_sess_chap_in_idx.attr)
4584                 param = ISCSI_PARAM_CHAP_IN_IDX;
4585         else if (attr == &dev_attr_sess_chap_out_idx.attr)
4586                 param = ISCSI_PARAM_CHAP_OUT_IDX;
4587         else if (attr == &dev_attr_sess_password.attr)
4588                 param = ISCSI_PARAM_USERNAME;
4589         else if (attr == &dev_attr_sess_password_in.attr)
4590                 param = ISCSI_PARAM_USERNAME_IN;
4591         else if (attr == &dev_attr_sess_username.attr)
4592                 param = ISCSI_PARAM_PASSWORD;
4593         else if (attr == &dev_attr_sess_username_in.attr)
4594                 param = ISCSI_PARAM_PASSWORD_IN;
4595         else if (attr == &dev_attr_sess_fast_abort.attr)
4596                 param = ISCSI_PARAM_FAST_ABORT;
4597         else if (attr == &dev_attr_sess_abort_tmo.attr)
4598                 param = ISCSI_PARAM_ABORT_TMO;
4599         else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
4600                 param = ISCSI_PARAM_LU_RESET_TMO;
4601         else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
4602                 param = ISCSI_PARAM_TGT_RESET_TMO;
4603         else if (attr == &dev_attr_sess_ifacename.attr)
4604                 param = ISCSI_PARAM_IFACE_NAME;
4605         else if (attr == &dev_attr_sess_initiatorname.attr)
4606                 param = ISCSI_PARAM_INITIATOR_NAME;
4607         else if (attr == &dev_attr_sess_targetalias.attr)
4608                 param = ISCSI_PARAM_TARGET_ALIAS;
4609         else if (attr == &dev_attr_sess_boot_root.attr)
4610                 param = ISCSI_PARAM_BOOT_ROOT;
4611         else if (attr == &dev_attr_sess_boot_nic.attr)
4612                 param = ISCSI_PARAM_BOOT_NIC;
4613         else if (attr == &dev_attr_sess_boot_target.attr)
4614                 param = ISCSI_PARAM_BOOT_TARGET;
4615         else if (attr == &dev_attr_sess_auto_snd_tgt_disable.attr)
4616                 param = ISCSI_PARAM_AUTO_SND_TGT_DISABLE;
4617         else if (attr == &dev_attr_sess_discovery_session.attr)
4618                 param = ISCSI_PARAM_DISCOVERY_SESS;
4619         else if (attr == &dev_attr_sess_portal_type.attr)
4620                 param = ISCSI_PARAM_PORTAL_TYPE;
4621         else if (attr == &dev_attr_sess_chap_auth.attr)
4622                 param = ISCSI_PARAM_CHAP_AUTH_EN;
4623         else if (attr == &dev_attr_sess_discovery_logout.attr)
4624                 param = ISCSI_PARAM_DISCOVERY_LOGOUT_EN;
4625         else if (attr == &dev_attr_sess_bidi_chap.attr)
4626                 param = ISCSI_PARAM_BIDI_CHAP_EN;
4627         else if (attr == &dev_attr_sess_discovery_auth_optional.attr)
4628                 param = ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL;
4629         else if (attr == &dev_attr_sess_def_time2wait.attr)
4630                 param = ISCSI_PARAM_DEF_TIME2WAIT;
4631         else if (attr == &dev_attr_sess_def_time2retain.attr)
4632                 param = ISCSI_PARAM_DEF_TIME2RETAIN;
4633         else if (attr == &dev_attr_sess_isid.attr)
4634                 param = ISCSI_PARAM_ISID;
4635         else if (attr == &dev_attr_sess_tsid.attr)
4636                 param = ISCSI_PARAM_TSID;
4637         else if (attr == &dev_attr_sess_def_taskmgmt_tmo.attr)
4638                 param = ISCSI_PARAM_DEF_TASKMGMT_TMO;
4639         else if (attr == &dev_attr_sess_discovery_parent_idx.attr)
4640                 param = ISCSI_PARAM_DISCOVERY_PARENT_IDX;
4641         else if (attr == &dev_attr_sess_discovery_parent_type.attr)
4642                 param = ISCSI_PARAM_DISCOVERY_PARENT_TYPE;
4643         else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
4644                 return S_IRUGO | S_IWUSR;
4645         else if (attr == &dev_attr_priv_sess_state.attr)
4646                 return S_IRUGO;
4647         else if (attr == &dev_attr_priv_sess_target_state.attr)
4648                 return S_IRUGO;
4649         else if (attr == &dev_attr_priv_sess_creator.attr)
4650                 return S_IRUGO;
4651         else if (attr == &dev_attr_priv_sess_target_id.attr)
4652                 return S_IRUGO;
4653         else {
4654                 WARN_ONCE(1, "Invalid session attr");
4655                 return 0;
4656         }
4657
4658         return t->attr_is_visible(ISCSI_PARAM, param);
4659 }
4660
4661 static struct attribute_group iscsi_session_group = {
4662         .attrs = iscsi_session_attrs,
4663         .is_visible = iscsi_session_attr_is_visible,
4664 };
4665
4666 /*
4667  * iSCSI host attrs
4668  */
4669 #define iscsi_host_attr_show(param)                                     \
4670 static ssize_t                                                          \
4671 show_host_param_##param(struct device *dev,                             \
4672                         struct device_attribute *attr, char *buf)       \
4673 {                                                                       \
4674         struct Scsi_Host *shost = transport_class_to_shost(dev);        \
4675         struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
4676         return priv->iscsi_transport->get_host_param(shost, param, buf); \
4677 }
4678
4679 #define iscsi_host_attr(field, param)                                   \
4680         iscsi_host_attr_show(param)                                     \
4681 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param,  \
4682                         NULL);
4683
4684 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
4685 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
4686 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
4687 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
4688 iscsi_host_attr(port_state, ISCSI_HOST_PARAM_PORT_STATE);
4689 iscsi_host_attr(port_speed, ISCSI_HOST_PARAM_PORT_SPEED);
4690
4691 static struct attribute *iscsi_host_attrs[] = {
4692         &dev_attr_host_netdev.attr,
4693         &dev_attr_host_hwaddress.attr,
4694         &dev_attr_host_ipaddress.attr,
4695         &dev_attr_host_initiatorname.attr,
4696         &dev_attr_host_port_state.attr,
4697         &dev_attr_host_port_speed.attr,
4698         NULL,
4699 };
4700
4701 static umode_t iscsi_host_attr_is_visible(struct kobject *kobj,
4702                                          struct attribute *attr, int i)
4703 {
4704         struct device *cdev = container_of(kobj, struct device, kobj);
4705         struct Scsi_Host *shost = transport_class_to_shost(cdev);
4706         struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
4707         int param;
4708
4709         if (attr == &dev_attr_host_netdev.attr)
4710                 param = ISCSI_HOST_PARAM_NETDEV_NAME;
4711         else if (attr == &dev_attr_host_hwaddress.attr)
4712                 param = ISCSI_HOST_PARAM_HWADDRESS;
4713         else if (attr == &dev_attr_host_ipaddress.attr)
4714                 param = ISCSI_HOST_PARAM_IPADDRESS;
4715         else if (attr == &dev_attr_host_initiatorname.attr)
4716                 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
4717         else if (attr == &dev_attr_host_port_state.attr)
4718                 param = ISCSI_HOST_PARAM_PORT_STATE;
4719         else if (attr == &dev_attr_host_port_speed.attr)
4720                 param = ISCSI_HOST_PARAM_PORT_SPEED;
4721         else {
4722                 WARN_ONCE(1, "Invalid host attr");
4723                 return 0;
4724         }
4725
4726         return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
4727 }
4728
4729 static struct attribute_group iscsi_host_group = {
4730         .attrs = iscsi_host_attrs,
4731         .is_visible = iscsi_host_attr_is_visible,
4732 };
4733
4734 /* convert iscsi_port_speed values to ascii string name */
4735 static const struct {
4736         enum iscsi_port_speed   value;
4737         char                    *name;
4738 } iscsi_port_speed_names[] = {
4739         {ISCSI_PORT_SPEED_UNKNOWN,      "Unknown" },
4740         {ISCSI_PORT_SPEED_10MBPS,       "10 Mbps" },
4741         {ISCSI_PORT_SPEED_100MBPS,      "100 Mbps" },
4742         {ISCSI_PORT_SPEED_1GBPS,        "1 Gbps" },
4743         {ISCSI_PORT_SPEED_10GBPS,       "10 Gbps" },
4744         {ISCSI_PORT_SPEED_25GBPS,       "25 Gbps" },
4745         {ISCSI_PORT_SPEED_40GBPS,       "40 Gbps" },
4746 };
4747
4748 char *iscsi_get_port_speed_name(struct Scsi_Host *shost)
4749 {
4750         int i;
4751         char *speed = "Unknown!";
4752         struct iscsi_cls_host *ihost = shost->shost_data;
4753         uint32_t port_speed = ihost->port_speed;
4754
4755         for (i = 0; i < ARRAY_SIZE(iscsi_port_speed_names); i++) {
4756                 if (iscsi_port_speed_names[i].value & port_speed) {
4757                         speed = iscsi_port_speed_names[i].name;
4758                         break;
4759                 }
4760         }
4761         return speed;
4762 }
4763 EXPORT_SYMBOL_GPL(iscsi_get_port_speed_name);
4764
4765 /* convert iscsi_port_state values to ascii string name */
4766 static const struct {
4767         enum iscsi_port_state   value;
4768         char                    *name;
4769 } iscsi_port_state_names[] = {
4770         {ISCSI_PORT_STATE_DOWN,         "LINK DOWN" },
4771         {ISCSI_PORT_STATE_UP,           "LINK UP" },
4772 };
4773
4774 char *iscsi_get_port_state_name(struct Scsi_Host *shost)
4775 {
4776         int i;
4777         char *state = "Unknown!";
4778         struct iscsi_cls_host *ihost = shost->shost_data;
4779         uint32_t port_state = ihost->port_state;
4780
4781         for (i = 0; i < ARRAY_SIZE(iscsi_port_state_names); i++) {
4782                 if (iscsi_port_state_names[i].value & port_state) {
4783                         state = iscsi_port_state_names[i].name;
4784                         break;
4785                 }
4786         }
4787         return state;
4788 }
4789 EXPORT_SYMBOL_GPL(iscsi_get_port_state_name);
4790
4791 static int iscsi_session_match(struct attribute_container *cont,
4792                            struct device *dev)
4793 {
4794         struct iscsi_cls_session *session;
4795         struct Scsi_Host *shost;
4796         struct iscsi_internal *priv;
4797
4798         if (!iscsi_is_session_dev(dev))
4799                 return 0;
4800
4801         session = iscsi_dev_to_session(dev);
4802         shost = iscsi_session_to_shost(session);
4803         if (!shost->transportt)
4804                 return 0;
4805
4806         priv = to_iscsi_internal(shost->transportt);
4807         if (priv->session_cont.ac.class != &iscsi_session_class.class)
4808                 return 0;
4809
4810         return &priv->session_cont.ac == cont;
4811 }
4812
4813 static int iscsi_conn_match(struct attribute_container *cont,
4814                            struct device *dev)
4815 {
4816         struct iscsi_cls_session *session;
4817         struct iscsi_cls_conn *conn;
4818         struct Scsi_Host *shost;
4819         struct iscsi_internal *priv;
4820
4821         if (!iscsi_is_conn_dev(dev))
4822                 return 0;
4823
4824         conn = iscsi_dev_to_conn(dev);
4825         session = iscsi_dev_to_session(conn->dev.parent);
4826         shost = iscsi_session_to_shost(session);
4827
4828         if (!shost->transportt)
4829                 return 0;
4830
4831         priv = to_iscsi_internal(shost->transportt);
4832         if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
4833                 return 0;
4834
4835         return &priv->conn_cont.ac == cont;
4836 }
4837
4838 static int iscsi_host_match(struct attribute_container *cont,
4839                             struct device *dev)
4840 {
4841         struct Scsi_Host *shost;
4842         struct iscsi_internal *priv;
4843
4844         if (!scsi_is_host_device(dev))
4845                 return 0;
4846
4847         shost = dev_to_shost(dev);
4848         if (!shost->transportt  ||
4849             shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
4850                 return 0;
4851
4852         priv = to_iscsi_internal(shost->transportt);
4853         return &priv->t.host_attrs.ac == cont;
4854 }
4855
4856 struct scsi_transport_template *
4857 iscsi_register_transport(struct iscsi_transport *tt)
4858 {
4859         struct iscsi_internal *priv;
4860         unsigned long flags;
4861         int err;
4862
4863         BUG_ON(!tt);
4864         WARN_ON(tt->ep_disconnect && !tt->unbind_conn);
4865
4866         priv = iscsi_if_transport_lookup(tt);
4867         if (priv)
4868                 return NULL;
4869
4870         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
4871         if (!priv)
4872                 return NULL;
4873         INIT_LIST_HEAD(&priv->list);
4874         priv->iscsi_transport = tt;
4875         priv->t.user_scan = iscsi_user_scan;
4876         priv->t.create_work_queue = 1;
4877
4878         priv->dev.class = &iscsi_transport_class;
4879         dev_set_name(&priv->dev, "%s", tt->name);
4880         err = device_register(&priv->dev);
4881         if (err)
4882                 goto put_dev;
4883
4884         err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
4885         if (err)
4886                 goto unregister_dev;
4887
4888         /* host parameters */
4889         priv->t.host_attrs.ac.class = &iscsi_host_class.class;
4890         priv->t.host_attrs.ac.match = iscsi_host_match;
4891         priv->t.host_attrs.ac.grp = &iscsi_host_group;
4892         priv->t.host_size = sizeof(struct iscsi_cls_host);
4893         transport_container_register(&priv->t.host_attrs);
4894
4895         /* connection parameters */
4896         priv->conn_cont.ac.class = &iscsi_connection_class.class;
4897         priv->conn_cont.ac.match = iscsi_conn_match;
4898         priv->conn_cont.ac.grp = &iscsi_conn_group;
4899         transport_container_register(&priv->conn_cont);
4900
4901         /* session parameters */
4902         priv->session_cont.ac.class = &iscsi_session_class.class;
4903         priv->session_cont.ac.match = iscsi_session_match;
4904         priv->session_cont.ac.grp = &iscsi_session_group;
4905         transport_container_register(&priv->session_cont);
4906
4907         spin_lock_irqsave(&iscsi_transport_lock, flags);
4908         list_add(&priv->list, &iscsi_transports);
4909         spin_unlock_irqrestore(&iscsi_transport_lock, flags);
4910
4911         printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
4912         return &priv->t;
4913
4914 unregister_dev:
4915         device_unregister(&priv->dev);
4916         return NULL;
4917 put_dev:
4918         put_device(&priv->dev);
4919         return NULL;
4920 }
4921 EXPORT_SYMBOL_GPL(iscsi_register_transport);
4922
4923 int iscsi_unregister_transport(struct iscsi_transport *tt)
4924 {
4925         struct iscsi_internal *priv;
4926         unsigned long flags;
4927
4928         BUG_ON(!tt);
4929
4930         mutex_lock(&rx_queue_mutex);
4931
4932         priv = iscsi_if_transport_lookup(tt);
4933         BUG_ON (!priv);
4934
4935         spin_lock_irqsave(&iscsi_transport_lock, flags);
4936         list_del(&priv->list);
4937         spin_unlock_irqrestore(&iscsi_transport_lock, flags);
4938
4939         transport_container_unregister(&priv->conn_cont);
4940         transport_container_unregister(&priv->session_cont);
4941         transport_container_unregister(&priv->t.host_attrs);
4942
4943         sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
4944         device_unregister(&priv->dev);
4945         mutex_unlock(&rx_queue_mutex);
4946
4947         return 0;
4948 }
4949 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
4950
4951 void iscsi_dbg_trace(void (*trace)(struct device *dev, struct va_format *),
4952                      struct device *dev, const char *fmt, ...)
4953 {
4954         struct va_format vaf;
4955         va_list args;
4956
4957         va_start(args, fmt);
4958         vaf.fmt = fmt;
4959         vaf.va = &args;
4960         trace(dev, &vaf);
4961         va_end(args);
4962 }
4963 EXPORT_SYMBOL_GPL(iscsi_dbg_trace);
4964
4965 static __init int iscsi_transport_init(void)
4966 {
4967         int err;
4968         struct netlink_kernel_cfg cfg = {
4969                 .groups = 1,
4970                 .input  = iscsi_if_rx,
4971         };
4972         printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
4973                 ISCSI_TRANSPORT_VERSION);
4974
4975         atomic_set(&iscsi_session_nr, 0);
4976
4977         err = class_register(&iscsi_transport_class);
4978         if (err)
4979                 return err;
4980
4981         err = class_register(&iscsi_endpoint_class);
4982         if (err)
4983                 goto unregister_transport_class;
4984
4985         err = class_register(&iscsi_iface_class);
4986         if (err)
4987                 goto unregister_endpoint_class;
4988
4989         err = transport_class_register(&iscsi_host_class);
4990         if (err)
4991                 goto unregister_iface_class;
4992
4993         err = transport_class_register(&iscsi_connection_class);
4994         if (err)
4995                 goto unregister_host_class;
4996
4997         err = transport_class_register(&iscsi_session_class);
4998         if (err)
4999                 goto unregister_conn_class;
5000
5001         err = bus_register(&iscsi_flashnode_bus);
5002         if (err)
5003                 goto unregister_session_class;
5004
5005         nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, &cfg);
5006         if (!nls) {
5007                 err = -ENOBUFS;
5008                 goto unregister_flashnode_bus;
5009         }
5010
5011         iscsi_eh_timer_workq = alloc_workqueue("%s",
5012                         WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND,
5013                         1, "iscsi_eh");
5014         if (!iscsi_eh_timer_workq) {
5015                 err = -ENOMEM;
5016                 goto release_nls;
5017         }
5018
5019         iscsi_conn_cleanup_workq = alloc_workqueue("%s",
5020                         WQ_SYSFS | WQ_MEM_RECLAIM | WQ_UNBOUND, 0,
5021                         "iscsi_conn_cleanup");
5022         if (!iscsi_conn_cleanup_workq) {
5023                 err = -ENOMEM;
5024                 goto destroy_wq;
5025         }
5026
5027         return 0;
5028
5029 destroy_wq:
5030         destroy_workqueue(iscsi_eh_timer_workq);
5031 release_nls:
5032         netlink_kernel_release(nls);
5033 unregister_flashnode_bus:
5034         bus_unregister(&iscsi_flashnode_bus);
5035 unregister_session_class:
5036         transport_class_unregister(&iscsi_session_class);
5037 unregister_conn_class:
5038         transport_class_unregister(&iscsi_connection_class);
5039 unregister_host_class:
5040         transport_class_unregister(&iscsi_host_class);
5041 unregister_iface_class:
5042         class_unregister(&iscsi_iface_class);
5043 unregister_endpoint_class:
5044         class_unregister(&iscsi_endpoint_class);
5045 unregister_transport_class:
5046         class_unregister(&iscsi_transport_class);
5047         return err;
5048 }
5049
5050 static void __exit iscsi_transport_exit(void)
5051 {
5052         destroy_workqueue(iscsi_conn_cleanup_workq);
5053         destroy_workqueue(iscsi_eh_timer_workq);
5054         netlink_kernel_release(nls);
5055         bus_unregister(&iscsi_flashnode_bus);
5056         transport_class_unregister(&iscsi_connection_class);
5057         transport_class_unregister(&iscsi_session_class);
5058         transport_class_unregister(&iscsi_host_class);
5059         class_unregister(&iscsi_endpoint_class);
5060         class_unregister(&iscsi_iface_class);
5061         class_unregister(&iscsi_transport_class);
5062 }
5063
5064 module_init(iscsi_transport_init);
5065 module_exit(iscsi_transport_exit);
5066
5067 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
5068               "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
5069               "Alex Aizman <itn780@yahoo.com>");
5070 MODULE_DESCRIPTION("iSCSI Transport Interface");
5071 MODULE_LICENSE("GPL");
5072 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
5073 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);