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