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