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