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