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