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