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