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_free(&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_alloc(&iscsi_sess_ida, 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_free(&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 	return 0;
2142 }
2143 
2144 void iscsi_remove_session(struct iscsi_cls_session *session)
2145 {
2146 	unsigned long flags;
2147 	int err;
2148 
2149 	ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
2150 
2151 	spin_lock_irqsave(&sesslock, flags);
2152 	if (!list_empty(&session->sess_list))
2153 		list_del(&session->sess_list);
2154 	spin_unlock_irqrestore(&sesslock, flags);
2155 
2156 	if (!cancel_work_sync(&session->block_work))
2157 		cancel_delayed_work_sync(&session->recovery_work);
2158 	cancel_work_sync(&session->unblock_work);
2159 	/*
2160 	 * If we are blocked let commands flow again. The lld or iscsi
2161 	 * layer should set up the queuecommand to fail commands.
2162 	 * We assume that LLD will not be calling block/unblock while
2163 	 * removing the session.
2164 	 */
2165 	spin_lock_irqsave(&session->lock, flags);
2166 	session->state = ISCSI_SESSION_FREE;
2167 	spin_unlock_irqrestore(&session->lock, flags);
2168 
2169 	scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
2170 	/*
2171 	 * qla4xxx can perform it's own scans when it runs in kernel only
2172 	 * mode. Make sure to flush those scans.
2173 	 */
2174 	flush_work(&session->scan_work);
2175 	/* flush running unbind operations */
2176 	flush_work(&session->unbind_work);
2177 	__iscsi_unbind_session(&session->unbind_work);
2178 
2179 	/* hw iscsi may not have removed all connections from session */
2180 	err = device_for_each_child(&session->dev, NULL,
2181 				    iscsi_iter_destroy_conn_fn);
2182 	if (err)
2183 		iscsi_cls_session_printk(KERN_ERR, session,
2184 					 "Could not delete all connections "
2185 					 "for session. Error %d.\n", err);
2186 
2187 	transport_unregister_device(&session->dev);
2188 
2189 	destroy_workqueue(session->workq);
2190 
2191 	ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
2192 	device_del(&session->dev);
2193 }
2194 EXPORT_SYMBOL_GPL(iscsi_remove_session);
2195 
2196 static void iscsi_stop_conn(struct iscsi_cls_conn *conn, int flag)
2197 {
2198 	ISCSI_DBG_TRANS_CONN(conn, "Stopping conn.\n");
2199 
2200 	switch (flag) {
2201 	case STOP_CONN_RECOVER:
2202 		WRITE_ONCE(conn->state, ISCSI_CONN_FAILED);
2203 		break;
2204 	case STOP_CONN_TERM:
2205 		WRITE_ONCE(conn->state, ISCSI_CONN_DOWN);
2206 		break;
2207 	default:
2208 		iscsi_cls_conn_printk(KERN_ERR, conn, "invalid stop flag %d\n",
2209 				      flag);
2210 		return;
2211 	}
2212 
2213 	conn->transport->stop_conn(conn, flag);
2214 	ISCSI_DBG_TRANS_CONN(conn, "Stopping conn done.\n");
2215 }
2216 
2217 static void iscsi_ep_disconnect(struct iscsi_cls_conn *conn, bool is_active)
2218 {
2219 	struct iscsi_cls_session *session = iscsi_conn_to_session(conn);
2220 	struct iscsi_endpoint *ep;
2221 
2222 	ISCSI_DBG_TRANS_CONN(conn, "disconnect ep.\n");
2223 	WRITE_ONCE(conn->state, ISCSI_CONN_FAILED);
2224 
2225 	if (!conn->ep || !session->transport->ep_disconnect)
2226 		return;
2227 
2228 	ep = conn->ep;
2229 	conn->ep = NULL;
2230 
2231 	session->transport->unbind_conn(conn, is_active);
2232 	session->transport->ep_disconnect(ep);
2233 	ISCSI_DBG_TRANS_CONN(conn, "disconnect ep done.\n");
2234 }
2235 
2236 static void iscsi_if_disconnect_bound_ep(struct iscsi_cls_conn *conn,
2237 					 struct iscsi_endpoint *ep,
2238 					 bool is_active)
2239 {
2240 	/* Check if this was a conn error and the kernel took ownership */
2241 	spin_lock_irq(&conn->lock);
2242 	if (!test_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags)) {
2243 		spin_unlock_irq(&conn->lock);
2244 		iscsi_ep_disconnect(conn, is_active);
2245 	} else {
2246 		spin_unlock_irq(&conn->lock);
2247 		ISCSI_DBG_TRANS_CONN(conn, "flush kernel conn cleanup.\n");
2248 		mutex_unlock(&conn->ep_mutex);
2249 
2250 		flush_work(&conn->cleanup_work);
2251 		/*
2252 		 * Userspace is now done with the EP so we can release the ref
2253 		 * iscsi_cleanup_conn_work_fn took.
2254 		 */
2255 		iscsi_put_endpoint(ep);
2256 		mutex_lock(&conn->ep_mutex);
2257 	}
2258 }
2259 
2260 static int iscsi_if_stop_conn(struct iscsi_cls_conn *conn, int flag)
2261 {
2262 	ISCSI_DBG_TRANS_CONN(conn, "iscsi if conn stop.\n");
2263 	/*
2264 	 * For offload, iscsid may not know about the ep like when iscsid is
2265 	 * restarted or for kernel based session shutdown iscsid is not even
2266 	 * up. For these cases, we do the disconnect now.
2267 	 */
2268 	mutex_lock(&conn->ep_mutex);
2269 	if (conn->ep)
2270 		iscsi_if_disconnect_bound_ep(conn, conn->ep, true);
2271 	mutex_unlock(&conn->ep_mutex);
2272 
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 		 * Figure out if it was the kernel or userspace initiating this.
2284 		 */
2285 		spin_lock_irq(&conn->lock);
2286 		if (!test_and_set_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags)) {
2287 			spin_unlock_irq(&conn->lock);
2288 			iscsi_stop_conn(conn, flag);
2289 		} else {
2290 			spin_unlock_irq(&conn->lock);
2291 			ISCSI_DBG_TRANS_CONN(conn,
2292 					     "flush kernel conn cleanup.\n");
2293 			flush_work(&conn->cleanup_work);
2294 		}
2295 		/*
2296 		 * Only clear for recovery to avoid extra cleanup runs during
2297 		 * termination.
2298 		 */
2299 		spin_lock_irq(&conn->lock);
2300 		clear_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags);
2301 		spin_unlock_irq(&conn->lock);
2302 	}
2303 	ISCSI_DBG_TRANS_CONN(conn, "iscsi if conn stop done.\n");
2304 	return 0;
2305 }
2306 
2307 static void iscsi_cleanup_conn_work_fn(struct work_struct *work)
2308 {
2309 	struct iscsi_cls_conn *conn = container_of(work, struct iscsi_cls_conn,
2310 						   cleanup_work);
2311 	struct iscsi_cls_session *session = iscsi_conn_to_session(conn);
2312 
2313 	mutex_lock(&conn->ep_mutex);
2314 	/*
2315 	 * Get a ref to the ep, so we don't release its ID until after
2316 	 * userspace is done referencing it in iscsi_if_disconnect_bound_ep.
2317 	 */
2318 	if (conn->ep)
2319 		get_device(&conn->ep->dev);
2320 	iscsi_ep_disconnect(conn, false);
2321 
2322 	if (system_state != SYSTEM_RUNNING) {
2323 		/*
2324 		 * If the user has set up for the session to never timeout
2325 		 * then hang like they wanted. For all other cases fail right
2326 		 * away since userspace is not going to relogin.
2327 		 */
2328 		if (session->recovery_tmo > 0)
2329 			session->recovery_tmo = 0;
2330 	}
2331 
2332 	iscsi_stop_conn(conn, STOP_CONN_RECOVER);
2333 	mutex_unlock(&conn->ep_mutex);
2334 	ISCSI_DBG_TRANS_CONN(conn, "cleanup done.\n");
2335 }
2336 
2337 static int iscsi_iter_force_destroy_conn_fn(struct device *dev, void *data)
2338 {
2339 	struct iscsi_transport *transport;
2340 	struct iscsi_cls_conn *conn;
2341 
2342 	if (!iscsi_is_conn_dev(dev))
2343 		return 0;
2344 
2345 	conn = iscsi_dev_to_conn(dev);
2346 	transport = conn->transport;
2347 
2348 	if (READ_ONCE(conn->state) != ISCSI_CONN_DOWN)
2349 		iscsi_if_stop_conn(conn, STOP_CONN_TERM);
2350 
2351 	transport->destroy_conn(conn);
2352 	return 0;
2353 }
2354 
2355 /**
2356  * iscsi_force_destroy_session - destroy a session from the kernel
2357  * @session: session to destroy
2358  *
2359  * Force the destruction of a session from the kernel. This should only be
2360  * used when userspace is no longer running during system shutdown.
2361  */
2362 void iscsi_force_destroy_session(struct iscsi_cls_session *session)
2363 {
2364 	struct iscsi_transport *transport = session->transport;
2365 	unsigned long flags;
2366 
2367 	WARN_ON_ONCE(system_state == SYSTEM_RUNNING);
2368 
2369 	spin_lock_irqsave(&sesslock, flags);
2370 	if (list_empty(&session->sess_list)) {
2371 		spin_unlock_irqrestore(&sesslock, flags);
2372 		/*
2373 		 * Conn/ep is already freed. Session is being torn down via
2374 		 * async path. For shutdown we don't care about it so return.
2375 		 */
2376 		return;
2377 	}
2378 	spin_unlock_irqrestore(&sesslock, flags);
2379 
2380 	device_for_each_child(&session->dev, NULL,
2381 			      iscsi_iter_force_destroy_conn_fn);
2382 	transport->destroy_session(session);
2383 }
2384 EXPORT_SYMBOL_GPL(iscsi_force_destroy_session);
2385 
2386 void iscsi_free_session(struct iscsi_cls_session *session)
2387 {
2388 	ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
2389 	iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
2390 	put_device(&session->dev);
2391 }
2392 EXPORT_SYMBOL_GPL(iscsi_free_session);
2393 
2394 /**
2395  * iscsi_alloc_conn - alloc iscsi class connection
2396  * @session: iscsi cls session
2397  * @dd_size: private driver data size
2398  * @cid: connection id
2399  */
2400 struct iscsi_cls_conn *
2401 iscsi_alloc_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
2402 {
2403 	struct iscsi_transport *transport = session->transport;
2404 	struct iscsi_cls_conn *conn;
2405 
2406 	conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
2407 	if (!conn)
2408 		return NULL;
2409 	if (dd_size)
2410 		conn->dd_data = &conn[1];
2411 
2412 	mutex_init(&conn->ep_mutex);
2413 	spin_lock_init(&conn->lock);
2414 	INIT_LIST_HEAD(&conn->conn_list);
2415 	INIT_WORK(&conn->cleanup_work, iscsi_cleanup_conn_work_fn);
2416 	conn->transport = transport;
2417 	conn->cid = cid;
2418 	WRITE_ONCE(conn->state, ISCSI_CONN_DOWN);
2419 
2420 	/* this is released in the dev's release function */
2421 	if (!get_device(&session->dev))
2422 		goto free_conn;
2423 
2424 	dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
2425 	device_initialize(&conn->dev);
2426 	conn->dev.parent = &session->dev;
2427 	conn->dev.release = iscsi_conn_release;
2428 
2429 	return conn;
2430 
2431 free_conn:
2432 	kfree(conn);
2433 	return NULL;
2434 }
2435 EXPORT_SYMBOL_GPL(iscsi_alloc_conn);
2436 
2437 /**
2438  * iscsi_add_conn - add iscsi class connection
2439  * @conn: iscsi cls connection
2440  *
2441  * This will expose iscsi_cls_conn to sysfs so make sure the related
2442  * resources for sysfs attributes are initialized before calling this.
2443  */
2444 int iscsi_add_conn(struct iscsi_cls_conn *conn)
2445 {
2446 	int err;
2447 	unsigned long flags;
2448 	struct iscsi_cls_session *session = iscsi_dev_to_session(conn->dev.parent);
2449 
2450 	err = device_add(&conn->dev);
2451 	if (err) {
2452 		iscsi_cls_session_printk(KERN_ERR, session,
2453 					 "could not register connection's dev\n");
2454 		return err;
2455 	}
2456 	err = transport_register_device(&conn->dev);
2457 	if (err) {
2458 		iscsi_cls_session_printk(KERN_ERR, session,
2459 					 "could not register transport's dev\n");
2460 		device_del(&conn->dev);
2461 		return err;
2462 	}
2463 
2464 	spin_lock_irqsave(&connlock, flags);
2465 	list_add(&conn->conn_list, &connlist);
2466 	spin_unlock_irqrestore(&connlock, flags);
2467 
2468 	return 0;
2469 }
2470 EXPORT_SYMBOL_GPL(iscsi_add_conn);
2471 
2472 /**
2473  * iscsi_remove_conn - remove iscsi class connection from sysfs
2474  * @conn: iscsi cls connection
2475  *
2476  * Remove iscsi_cls_conn from sysfs, and wait for previous
2477  * read/write of iscsi_cls_conn's attributes in sysfs to finish.
2478  */
2479 void iscsi_remove_conn(struct iscsi_cls_conn *conn)
2480 {
2481 	unsigned long flags;
2482 
2483 	spin_lock_irqsave(&connlock, flags);
2484 	list_del(&conn->conn_list);
2485 	spin_unlock_irqrestore(&connlock, flags);
2486 
2487 	transport_unregister_device(&conn->dev);
2488 	device_del(&conn->dev);
2489 }
2490 EXPORT_SYMBOL_GPL(iscsi_remove_conn);
2491 
2492 void iscsi_put_conn(struct iscsi_cls_conn *conn)
2493 {
2494 	put_device(&conn->dev);
2495 }
2496 EXPORT_SYMBOL_GPL(iscsi_put_conn);
2497 
2498 void iscsi_get_conn(struct iscsi_cls_conn *conn)
2499 {
2500 	get_device(&conn->dev);
2501 }
2502 EXPORT_SYMBOL_GPL(iscsi_get_conn);
2503 
2504 /*
2505  * iscsi interface functions
2506  */
2507 static struct iscsi_internal *
2508 iscsi_if_transport_lookup(struct iscsi_transport *tt)
2509 {
2510 	struct iscsi_internal *priv;
2511 	unsigned long flags;
2512 
2513 	spin_lock_irqsave(&iscsi_transport_lock, flags);
2514 	list_for_each_entry(priv, &iscsi_transports, list) {
2515 		if (tt == priv->iscsi_transport) {
2516 			spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2517 			return priv;
2518 		}
2519 	}
2520 	spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2521 	return NULL;
2522 }
2523 
2524 static int
2525 iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
2526 {
2527 	return nlmsg_multicast(nls, skb, 0, group, gfp);
2528 }
2529 
2530 static int
2531 iscsi_unicast_skb(struct sk_buff *skb, u32 portid)
2532 {
2533 	return nlmsg_unicast(nls, skb, portid);
2534 }
2535 
2536 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
2537 		   char *data, uint32_t data_size)
2538 {
2539 	struct nlmsghdr	*nlh;
2540 	struct sk_buff *skb;
2541 	struct iscsi_uevent *ev;
2542 	char *pdu;
2543 	struct iscsi_internal *priv;
2544 	int len = nlmsg_total_size(sizeof(*ev) + sizeof(struct iscsi_hdr) +
2545 				   data_size);
2546 
2547 	priv = iscsi_if_transport_lookup(conn->transport);
2548 	if (!priv)
2549 		return -EINVAL;
2550 
2551 	skb = alloc_skb(len, GFP_ATOMIC);
2552 	if (!skb) {
2553 		iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
2554 		iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
2555 				      "control PDU: OOM\n");
2556 		return -ENOMEM;
2557 	}
2558 
2559 	nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2560 	ev = nlmsg_data(nlh);
2561 	memset(ev, 0, sizeof(*ev));
2562 	ev->transport_handle = iscsi_handle(conn->transport);
2563 	ev->type = ISCSI_KEVENT_RECV_PDU;
2564 	ev->r.recv_req.cid = conn->cid;
2565 	ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
2566 	pdu = (char*)ev + sizeof(*ev);
2567 	memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
2568 	memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
2569 
2570 	return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2571 }
2572 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
2573 
2574 int iscsi_offload_mesg(struct Scsi_Host *shost,
2575 		       struct iscsi_transport *transport, uint32_t type,
2576 		       char *data, uint16_t data_size)
2577 {
2578 	struct nlmsghdr	*nlh;
2579 	struct sk_buff *skb;
2580 	struct iscsi_uevent *ev;
2581 	int len = nlmsg_total_size(sizeof(*ev) + data_size);
2582 
2583 	skb = alloc_skb(len, GFP_ATOMIC);
2584 	if (!skb) {
2585 		printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
2586 		return -ENOMEM;
2587 	}
2588 
2589 	nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2590 	ev = nlmsg_data(nlh);
2591 	memset(ev, 0, sizeof(*ev));
2592 	ev->type = type;
2593 	ev->transport_handle = iscsi_handle(transport);
2594 	switch (type) {
2595 	case ISCSI_KEVENT_PATH_REQ:
2596 		ev->r.req_path.host_no = shost->host_no;
2597 		break;
2598 	case ISCSI_KEVENT_IF_DOWN:
2599 		ev->r.notify_if_down.host_no = shost->host_no;
2600 		break;
2601 	}
2602 
2603 	memcpy((char *)ev + sizeof(*ev), data, data_size);
2604 
2605 	return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
2606 }
2607 EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
2608 
2609 void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
2610 {
2611 	struct nlmsghdr	*nlh;
2612 	struct sk_buff	*skb;
2613 	struct iscsi_uevent *ev;
2614 	struct iscsi_internal *priv;
2615 	int len = nlmsg_total_size(sizeof(*ev));
2616 	unsigned long flags;
2617 	int state;
2618 
2619 	spin_lock_irqsave(&conn->lock, flags);
2620 	/*
2621 	 * Userspace will only do a stop call if we are at least bound. And, we
2622 	 * only need to do the in kernel cleanup if in the UP state so cmds can
2623 	 * be released to upper layers. If in other states just wait for
2624 	 * userspace to avoid races that can leave the cleanup_work queued.
2625 	 */
2626 	state = READ_ONCE(conn->state);
2627 	switch (state) {
2628 	case ISCSI_CONN_BOUND:
2629 	case ISCSI_CONN_UP:
2630 		if (!test_and_set_bit(ISCSI_CLS_CONN_BIT_CLEANUP,
2631 				      &conn->flags)) {
2632 			queue_work(iscsi_conn_cleanup_workq,
2633 				   &conn->cleanup_work);
2634 		}
2635 		break;
2636 	default:
2637 		ISCSI_DBG_TRANS_CONN(conn, "Got conn error in state %d\n",
2638 				     state);
2639 		break;
2640 	}
2641 	spin_unlock_irqrestore(&conn->lock, flags);
2642 
2643 	priv = iscsi_if_transport_lookup(conn->transport);
2644 	if (!priv)
2645 		return;
2646 
2647 	skb = alloc_skb(len, GFP_ATOMIC);
2648 	if (!skb) {
2649 		iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2650 				      "conn error (%d)\n", error);
2651 		return;
2652 	}
2653 
2654 	nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2655 	ev = nlmsg_data(nlh);
2656 	ev->transport_handle = iscsi_handle(conn->transport);
2657 	ev->type = ISCSI_KEVENT_CONN_ERROR;
2658 	ev->r.connerror.error = error;
2659 	ev->r.connerror.cid = conn->cid;
2660 	ev->r.connerror.sid = iscsi_conn_get_sid(conn);
2661 
2662 	iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2663 
2664 	iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
2665 			      error);
2666 }
2667 EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
2668 
2669 void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
2670 			    enum iscsi_conn_state state)
2671 {
2672 	struct nlmsghdr *nlh;
2673 	struct sk_buff  *skb;
2674 	struct iscsi_uevent *ev;
2675 	struct iscsi_internal *priv;
2676 	int len = nlmsg_total_size(sizeof(*ev));
2677 
2678 	priv = iscsi_if_transport_lookup(conn->transport);
2679 	if (!priv)
2680 		return;
2681 
2682 	skb = alloc_skb(len, GFP_ATOMIC);
2683 	if (!skb) {
2684 		iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2685 				      "conn login (%d)\n", state);
2686 		return;
2687 	}
2688 
2689 	nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2690 	ev = nlmsg_data(nlh);
2691 	ev->transport_handle = iscsi_handle(conn->transport);
2692 	ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
2693 	ev->r.conn_login.state = state;
2694 	ev->r.conn_login.cid = conn->cid;
2695 	ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
2696 	iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2697 
2698 	iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
2699 			      state);
2700 }
2701 EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
2702 
2703 void iscsi_post_host_event(uint32_t host_no, struct iscsi_transport *transport,
2704 			   enum iscsi_host_event_code code, uint32_t data_size,
2705 			   uint8_t *data)
2706 {
2707 	struct nlmsghdr *nlh;
2708 	struct sk_buff *skb;
2709 	struct iscsi_uevent *ev;
2710 	int len = nlmsg_total_size(sizeof(*ev) + data_size);
2711 
2712 	skb = alloc_skb(len, GFP_NOIO);
2713 	if (!skb) {
2714 		printk(KERN_ERR "gracefully ignored host event (%d):%d OOM\n",
2715 		       host_no, code);
2716 		return;
2717 	}
2718 
2719 	nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2720 	ev = nlmsg_data(nlh);
2721 	ev->transport_handle = iscsi_handle(transport);
2722 	ev->type = ISCSI_KEVENT_HOST_EVENT;
2723 	ev->r.host_event.host_no = host_no;
2724 	ev->r.host_event.code = code;
2725 	ev->r.host_event.data_size = data_size;
2726 
2727 	if (data_size)
2728 		memcpy((char *)ev + sizeof(*ev), data, data_size);
2729 
2730 	iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
2731 }
2732 EXPORT_SYMBOL_GPL(iscsi_post_host_event);
2733 
2734 void iscsi_ping_comp_event(uint32_t host_no, struct iscsi_transport *transport,
2735 			   uint32_t status, uint32_t pid, uint32_t data_size,
2736 			   uint8_t *data)
2737 {
2738 	struct nlmsghdr *nlh;
2739 	struct sk_buff *skb;
2740 	struct iscsi_uevent *ev;
2741 	int len = nlmsg_total_size(sizeof(*ev) + data_size);
2742 
2743 	skb = alloc_skb(len, GFP_NOIO);
2744 	if (!skb) {
2745 		printk(KERN_ERR "gracefully ignored ping comp: OOM\n");
2746 		return;
2747 	}
2748 
2749 	nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2750 	ev = nlmsg_data(nlh);
2751 	ev->transport_handle = iscsi_handle(transport);
2752 	ev->type = ISCSI_KEVENT_PING_COMP;
2753 	ev->r.ping_comp.host_no = host_no;
2754 	ev->r.ping_comp.status = status;
2755 	ev->r.ping_comp.pid = pid;
2756 	ev->r.ping_comp.data_size = data_size;
2757 	memcpy((char *)ev + sizeof(*ev), data, data_size);
2758 
2759 	iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
2760 }
2761 EXPORT_SYMBOL_GPL(iscsi_ping_comp_event);
2762 
2763 static int
2764 iscsi_if_send_reply(u32 portid, int type, void *payload, int size)
2765 {
2766 	struct sk_buff	*skb;
2767 	struct nlmsghdr	*nlh;
2768 	int len = nlmsg_total_size(size);
2769 
2770 	skb = alloc_skb(len, GFP_ATOMIC);
2771 	if (!skb) {
2772 		printk(KERN_ERR "Could not allocate skb to send reply.\n");
2773 		return -ENOMEM;
2774 	}
2775 
2776 	nlh = __nlmsg_put(skb, 0, 0, type, (len - sizeof(*nlh)), 0);
2777 	memcpy(nlmsg_data(nlh), payload, size);
2778 	return iscsi_unicast_skb(skb, portid);
2779 }
2780 
2781 static int
2782 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
2783 {
2784 	struct iscsi_uevent *ev = nlmsg_data(nlh);
2785 	struct iscsi_stats *stats;
2786 	struct sk_buff *skbstat;
2787 	struct iscsi_cls_conn *conn;
2788 	struct nlmsghdr	*nlhstat;
2789 	struct iscsi_uevent *evstat;
2790 	struct iscsi_internal *priv;
2791 	int len = nlmsg_total_size(sizeof(*ev) +
2792 				   sizeof(struct iscsi_stats) +
2793 				   sizeof(struct iscsi_stats_custom) *
2794 				   ISCSI_STATS_CUSTOM_MAX);
2795 	int err = 0;
2796 
2797 	priv = iscsi_if_transport_lookup(transport);
2798 	if (!priv)
2799 		return -EINVAL;
2800 
2801 	conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
2802 	if (!conn)
2803 		return -EEXIST;
2804 
2805 	do {
2806 		int actual_size;
2807 
2808 		skbstat = alloc_skb(len, GFP_ATOMIC);
2809 		if (!skbstat) {
2810 			iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
2811 					      "deliver stats: OOM\n");
2812 			return -ENOMEM;
2813 		}
2814 
2815 		nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
2816 				      (len - sizeof(*nlhstat)), 0);
2817 		evstat = nlmsg_data(nlhstat);
2818 		memset(evstat, 0, sizeof(*evstat));
2819 		evstat->transport_handle = iscsi_handle(conn->transport);
2820 		evstat->type = nlh->nlmsg_type;
2821 		evstat->u.get_stats.cid =
2822 			ev->u.get_stats.cid;
2823 		evstat->u.get_stats.sid =
2824 			ev->u.get_stats.sid;
2825 		stats = (struct iscsi_stats *)
2826 			((char*)evstat + sizeof(*evstat));
2827 		memset(stats, 0, sizeof(*stats));
2828 
2829 		transport->get_stats(conn, stats);
2830 		actual_size = nlmsg_total_size(sizeof(struct iscsi_uevent) +
2831 					       sizeof(struct iscsi_stats) +
2832 					       sizeof(struct iscsi_stats_custom) *
2833 					       stats->custom_length);
2834 		actual_size -= sizeof(*nlhstat);
2835 		actual_size = nlmsg_msg_size(actual_size);
2836 		skb_trim(skbstat, NLMSG_ALIGN(actual_size));
2837 		nlhstat->nlmsg_len = actual_size;
2838 
2839 		err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
2840 					  GFP_ATOMIC);
2841 	} while (err < 0 && err != -ECONNREFUSED);
2842 
2843 	return err;
2844 }
2845 
2846 /**
2847  * iscsi_session_event - send session destr. completion event
2848  * @session: iscsi class session
2849  * @event: type of event
2850  */
2851 int iscsi_session_event(struct iscsi_cls_session *session,
2852 			enum iscsi_uevent_e event)
2853 {
2854 	struct iscsi_internal *priv;
2855 	struct Scsi_Host *shost;
2856 	struct iscsi_uevent *ev;
2857 	struct sk_buff  *skb;
2858 	struct nlmsghdr *nlh;
2859 	int rc, len = nlmsg_total_size(sizeof(*ev));
2860 
2861 	priv = iscsi_if_transport_lookup(session->transport);
2862 	if (!priv)
2863 		return -EINVAL;
2864 	shost = iscsi_session_to_shost(session);
2865 
2866 	skb = alloc_skb(len, GFP_KERNEL);
2867 	if (!skb) {
2868 		iscsi_cls_session_printk(KERN_ERR, session,
2869 					 "Cannot notify userspace of session "
2870 					 "event %u\n", event);
2871 		return -ENOMEM;
2872 	}
2873 
2874 	nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2875 	ev = nlmsg_data(nlh);
2876 	ev->transport_handle = iscsi_handle(session->transport);
2877 
2878 	ev->type = event;
2879 	switch (event) {
2880 	case ISCSI_KEVENT_DESTROY_SESSION:
2881 		ev->r.d_session.host_no = shost->host_no;
2882 		ev->r.d_session.sid = session->sid;
2883 		break;
2884 	case ISCSI_KEVENT_CREATE_SESSION:
2885 		ev->r.c_session_ret.host_no = shost->host_no;
2886 		ev->r.c_session_ret.sid = session->sid;
2887 		break;
2888 	case ISCSI_KEVENT_UNBIND_SESSION:
2889 		ev->r.unbind_session.host_no = shost->host_no;
2890 		ev->r.unbind_session.sid = session->sid;
2891 		break;
2892 	default:
2893 		iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
2894 					 "%u.\n", event);
2895 		kfree_skb(skb);
2896 		return -EINVAL;
2897 	}
2898 
2899 	/*
2900 	 * this will occur if the daemon is not up, so we just warn
2901 	 * the user and when the daemon is restarted it will handle it
2902 	 */
2903 	rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
2904 	if (rc == -ESRCH)
2905 		iscsi_cls_session_printk(KERN_ERR, session,
2906 					 "Cannot notify userspace of session "
2907 					 "event %u. Check iscsi daemon\n",
2908 					 event);
2909 
2910 	ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
2911 				event, rc);
2912 	return rc;
2913 }
2914 EXPORT_SYMBOL_GPL(iscsi_session_event);
2915 
2916 static int
2917 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
2918 			struct iscsi_uevent *ev, pid_t pid,
2919 			uint32_t initial_cmdsn,	uint16_t cmds_max,
2920 			uint16_t queue_depth)
2921 {
2922 	struct iscsi_transport *transport = priv->iscsi_transport;
2923 	struct iscsi_cls_session *session;
2924 	struct Scsi_Host *shost;
2925 
2926 	session = transport->create_session(ep, cmds_max, queue_depth,
2927 					    initial_cmdsn);
2928 	if (!session)
2929 		return -ENOMEM;
2930 
2931 	session->creator = pid;
2932 	shost = iscsi_session_to_shost(session);
2933 	ev->r.c_session_ret.host_no = shost->host_no;
2934 	ev->r.c_session_ret.sid = session->sid;
2935 	ISCSI_DBG_TRANS_SESSION(session,
2936 				"Completed creating transport session\n");
2937 	return 0;
2938 }
2939 
2940 static int
2941 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2942 {
2943 	struct iscsi_cls_conn *conn;
2944 	struct iscsi_cls_session *session;
2945 
2946 	session = iscsi_session_lookup(ev->u.c_conn.sid);
2947 	if (!session) {
2948 		printk(KERN_ERR "iscsi: invalid session %d.\n",
2949 		       ev->u.c_conn.sid);
2950 		return -EINVAL;
2951 	}
2952 
2953 	conn = transport->create_conn(session, ev->u.c_conn.cid);
2954 	if (!conn) {
2955 		iscsi_cls_session_printk(KERN_ERR, session,
2956 					 "couldn't create a new connection.");
2957 		return -ENOMEM;
2958 	}
2959 
2960 	ev->r.c_conn_ret.sid = session->sid;
2961 	ev->r.c_conn_ret.cid = conn->cid;
2962 
2963 	ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
2964 	return 0;
2965 }
2966 
2967 static int
2968 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2969 {
2970 	struct iscsi_cls_conn *conn;
2971 
2972 	conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
2973 	if (!conn)
2974 		return -EINVAL;
2975 
2976 	ISCSI_DBG_TRANS_CONN(conn, "Flushing cleanup during destruction\n");
2977 	flush_work(&conn->cleanup_work);
2978 	ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
2979 
2980 	if (transport->destroy_conn)
2981 		transport->destroy_conn(conn);
2982 	return 0;
2983 }
2984 
2985 static int
2986 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2987 {
2988 	char *data = (char*)ev + sizeof(*ev);
2989 	struct iscsi_cls_conn *conn;
2990 	struct iscsi_cls_session *session;
2991 	int err = 0, value = 0, state;
2992 
2993 	if (ev->u.set_param.len > PAGE_SIZE)
2994 		return -EINVAL;
2995 
2996 	session = iscsi_session_lookup(ev->u.set_param.sid);
2997 	conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
2998 	if (!conn || !session)
2999 		return -EINVAL;
3000 
3001 	switch (ev->u.set_param.param) {
3002 	case ISCSI_PARAM_SESS_RECOVERY_TMO:
3003 		sscanf(data, "%d", &value);
3004 		if (!session->recovery_tmo_sysfs_override)
3005 			session->recovery_tmo = value;
3006 		break;
3007 	default:
3008 		state = READ_ONCE(conn->state);
3009 		if (state == ISCSI_CONN_BOUND || state == ISCSI_CONN_UP) {
3010 			err = transport->set_param(conn, ev->u.set_param.param,
3011 					data, ev->u.set_param.len);
3012 		} else {
3013 			return -ENOTCONN;
3014 		}
3015 	}
3016 
3017 	return err;
3018 }
3019 
3020 static int iscsi_if_ep_connect(struct iscsi_transport *transport,
3021 			       struct iscsi_uevent *ev, int msg_type)
3022 {
3023 	struct iscsi_endpoint *ep;
3024 	struct sockaddr *dst_addr;
3025 	struct Scsi_Host *shost = NULL;
3026 	int non_blocking, err = 0;
3027 
3028 	if (!transport->ep_connect)
3029 		return -EINVAL;
3030 
3031 	if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
3032 		shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
3033 		if (!shost) {
3034 			printk(KERN_ERR "ep connect failed. Could not find "
3035 			       "host no %u\n",
3036 			       ev->u.ep_connect_through_host.host_no);
3037 			return -ENODEV;
3038 		}
3039 		non_blocking = ev->u.ep_connect_through_host.non_blocking;
3040 	} else
3041 		non_blocking = ev->u.ep_connect.non_blocking;
3042 
3043 	dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
3044 	ep = transport->ep_connect(shost, dst_addr, non_blocking);
3045 	if (IS_ERR(ep)) {
3046 		err = PTR_ERR(ep);
3047 		goto release_host;
3048 	}
3049 
3050 	ev->r.ep_connect_ret.handle = ep->id;
3051 release_host:
3052 	if (shost)
3053 		scsi_host_put(shost);
3054 	return err;
3055 }
3056 
3057 static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
3058 				  u64 ep_handle)
3059 {
3060 	struct iscsi_cls_conn *conn;
3061 	struct iscsi_endpoint *ep;
3062 
3063 	if (!transport->ep_disconnect)
3064 		return -EINVAL;
3065 
3066 	ep = iscsi_lookup_endpoint(ep_handle);
3067 	if (!ep)
3068 		return -EINVAL;
3069 
3070 	conn = ep->conn;
3071 	if (!conn) {
3072 		/*
3073 		 * conn was not even bound yet, so we can't get iscsi conn
3074 		 * failures yet.
3075 		 */
3076 		transport->ep_disconnect(ep);
3077 		goto put_ep;
3078 	}
3079 
3080 	mutex_lock(&conn->ep_mutex);
3081 	iscsi_if_disconnect_bound_ep(conn, ep, false);
3082 	mutex_unlock(&conn->ep_mutex);
3083 put_ep:
3084 	iscsi_put_endpoint(ep);
3085 	return 0;
3086 }
3087 
3088 static int
3089 iscsi_if_transport_ep(struct iscsi_transport *transport,
3090 		      struct iscsi_uevent *ev, int msg_type)
3091 {
3092 	struct iscsi_endpoint *ep;
3093 	int rc = 0;
3094 
3095 	switch (msg_type) {
3096 	case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
3097 	case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
3098 		rc = iscsi_if_ep_connect(transport, ev, msg_type);
3099 		break;
3100 	case ISCSI_UEVENT_TRANSPORT_EP_POLL:
3101 		if (!transport->ep_poll)
3102 			return -EINVAL;
3103 
3104 		ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
3105 		if (!ep)
3106 			return -EINVAL;
3107 
3108 		ev->r.retcode = transport->ep_poll(ep,
3109 						   ev->u.ep_poll.timeout_ms);
3110 		iscsi_put_endpoint(ep);
3111 		break;
3112 	case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
3113 		rc = iscsi_if_ep_disconnect(transport,
3114 					    ev->u.ep_disconnect.ep_handle);
3115 		break;
3116 	}
3117 	return rc;
3118 }
3119 
3120 static int
3121 iscsi_tgt_dscvr(struct iscsi_transport *transport,
3122 		struct iscsi_uevent *ev)
3123 {
3124 	struct Scsi_Host *shost;
3125 	struct sockaddr *dst_addr;
3126 	int err;
3127 
3128 	if (!transport->tgt_dscvr)
3129 		return -EINVAL;
3130 
3131 	shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
3132 	if (!shost) {
3133 		printk(KERN_ERR "target discovery could not find host no %u\n",
3134 		       ev->u.tgt_dscvr.host_no);
3135 		return -ENODEV;
3136 	}
3137 
3138 
3139 	dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
3140 	err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
3141 				   ev->u.tgt_dscvr.enable, dst_addr);
3142 	scsi_host_put(shost);
3143 	return err;
3144 }
3145 
3146 static int
3147 iscsi_set_host_param(struct iscsi_transport *transport,
3148 		     struct iscsi_uevent *ev)
3149 {
3150 	char *data = (char*)ev + sizeof(*ev);
3151 	struct Scsi_Host *shost;
3152 	int err;
3153 
3154 	if (!transport->set_host_param)
3155 		return -ENOSYS;
3156 
3157 	if (ev->u.set_host_param.len > PAGE_SIZE)
3158 		return -EINVAL;
3159 
3160 	shost = scsi_host_lookup(ev->u.set_host_param.host_no);
3161 	if (!shost) {
3162 		printk(KERN_ERR "set_host_param could not find host no %u\n",
3163 		       ev->u.set_host_param.host_no);
3164 		return -ENODEV;
3165 	}
3166 
3167 	err = transport->set_host_param(shost, ev->u.set_host_param.param,
3168 					data, ev->u.set_host_param.len);
3169 	scsi_host_put(shost);
3170 	return err;
3171 }
3172 
3173 static int
3174 iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
3175 {
3176 	struct Scsi_Host *shost;
3177 	struct iscsi_path *params;
3178 	int err;
3179 
3180 	if (!transport->set_path)
3181 		return -ENOSYS;
3182 
3183 	shost = scsi_host_lookup(ev->u.set_path.host_no);
3184 	if (!shost) {
3185 		printk(KERN_ERR "set path could not find host no %u\n",
3186 		       ev->u.set_path.host_no);
3187 		return -ENODEV;
3188 	}
3189 
3190 	params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
3191 	err = transport->set_path(shost, params);
3192 
3193 	scsi_host_put(shost);
3194 	return err;
3195 }
3196 
3197 static int iscsi_session_has_conns(int sid)
3198 {
3199 	struct iscsi_cls_conn *conn;
3200 	unsigned long flags;
3201 	int found = 0;
3202 
3203 	spin_lock_irqsave(&connlock, flags);
3204 	list_for_each_entry(conn, &connlist, conn_list) {
3205 		if (iscsi_conn_get_sid(conn) == sid) {
3206 			found = 1;
3207 			break;
3208 		}
3209 	}
3210 	spin_unlock_irqrestore(&connlock, flags);
3211 
3212 	return found;
3213 }
3214 
3215 static int
3216 iscsi_set_iface_params(struct iscsi_transport *transport,
3217 		       struct iscsi_uevent *ev, uint32_t len)
3218 {
3219 	char *data = (char *)ev + sizeof(*ev);
3220 	struct Scsi_Host *shost;
3221 	int err;
3222 
3223 	if (!transport->set_iface_param)
3224 		return -ENOSYS;
3225 
3226 	shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
3227 	if (!shost) {
3228 		printk(KERN_ERR "set_iface_params could not find host no %u\n",
3229 		       ev->u.set_iface_params.host_no);
3230 		return -ENODEV;
3231 	}
3232 
3233 	err = transport->set_iface_param(shost, data, len);
3234 	scsi_host_put(shost);
3235 	return err;
3236 }
3237 
3238 static int
3239 iscsi_send_ping(struct iscsi_transport *transport, struct iscsi_uevent *ev)
3240 {
3241 	struct Scsi_Host *shost;
3242 	struct sockaddr *dst_addr;
3243 	int err;
3244 
3245 	if (!transport->send_ping)
3246 		return -ENOSYS;
3247 
3248 	shost = scsi_host_lookup(ev->u.iscsi_ping.host_no);
3249 	if (!shost) {
3250 		printk(KERN_ERR "iscsi_ping could not find host no %u\n",
3251 		       ev->u.iscsi_ping.host_no);
3252 		return -ENODEV;
3253 	}
3254 
3255 	dst_addr = (struct sockaddr *)((char *)ev + sizeof(*ev));
3256 	err = transport->send_ping(shost, ev->u.iscsi_ping.iface_num,
3257 				   ev->u.iscsi_ping.iface_type,
3258 				   ev->u.iscsi_ping.payload_size,
3259 				   ev->u.iscsi_ping.pid,
3260 				   dst_addr);
3261 	scsi_host_put(shost);
3262 	return err;
3263 }
3264 
3265 static int
3266 iscsi_get_chap(struct iscsi_transport *transport, struct nlmsghdr *nlh)
3267 {
3268 	struct iscsi_uevent *ev = nlmsg_data(nlh);
3269 	struct Scsi_Host *shost = NULL;
3270 	struct iscsi_chap_rec *chap_rec;
3271 	struct iscsi_internal *priv;
3272 	struct sk_buff *skbchap;
3273 	struct nlmsghdr *nlhchap;
3274 	struct iscsi_uevent *evchap;
3275 	uint32_t chap_buf_size;
3276 	int len, err = 0;
3277 	char *buf;
3278 
3279 	if (!transport->get_chap)
3280 		return -EINVAL;
3281 
3282 	priv = iscsi_if_transport_lookup(transport);
3283 	if (!priv)
3284 		return -EINVAL;
3285 
3286 	chap_buf_size = (ev->u.get_chap.num_entries * sizeof(*chap_rec));
3287 	len = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
3288 
3289 	shost = scsi_host_lookup(ev->u.get_chap.host_no);
3290 	if (!shost) {
3291 		printk(KERN_ERR "%s: failed. Could not find host no %u\n",
3292 		       __func__, ev->u.get_chap.host_no);
3293 		return -ENODEV;
3294 	}
3295 
3296 	do {
3297 		int actual_size;
3298 
3299 		skbchap = alloc_skb(len, GFP_KERNEL);
3300 		if (!skbchap) {
3301 			printk(KERN_ERR "can not deliver chap: OOM\n");
3302 			err = -ENOMEM;
3303 			goto exit_get_chap;
3304 		}
3305 
3306 		nlhchap = __nlmsg_put(skbchap, 0, 0, 0,
3307 				      (len - sizeof(*nlhchap)), 0);
3308 		evchap = nlmsg_data(nlhchap);
3309 		memset(evchap, 0, sizeof(*evchap));
3310 		evchap->transport_handle = iscsi_handle(transport);
3311 		evchap->type = nlh->nlmsg_type;
3312 		evchap->u.get_chap.host_no = ev->u.get_chap.host_no;
3313 		evchap->u.get_chap.chap_tbl_idx = ev->u.get_chap.chap_tbl_idx;
3314 		evchap->u.get_chap.num_entries = ev->u.get_chap.num_entries;
3315 		buf = (char *)evchap + sizeof(*evchap);
3316 		memset(buf, 0, chap_buf_size);
3317 
3318 		err = transport->get_chap(shost, ev->u.get_chap.chap_tbl_idx,
3319 				    &evchap->u.get_chap.num_entries, buf);
3320 
3321 		actual_size = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
3322 		skb_trim(skbchap, NLMSG_ALIGN(actual_size));
3323 		nlhchap->nlmsg_len = actual_size;
3324 
3325 		err = iscsi_multicast_skb(skbchap, ISCSI_NL_GRP_ISCSID,
3326 					  GFP_KERNEL);
3327 	} while (err < 0 && err != -ECONNREFUSED);
3328 
3329 exit_get_chap:
3330 	scsi_host_put(shost);
3331 	return err;
3332 }
3333 
3334 static int iscsi_set_chap(struct iscsi_transport *transport,
3335 			  struct iscsi_uevent *ev, uint32_t len)
3336 {
3337 	char *data = (char *)ev + sizeof(*ev);
3338 	struct Scsi_Host *shost;
3339 	int err = 0;
3340 
3341 	if (!transport->set_chap)
3342 		return -ENOSYS;
3343 
3344 	shost = scsi_host_lookup(ev->u.set_path.host_no);
3345 	if (!shost) {
3346 		pr_err("%s could not find host no %u\n",
3347 		       __func__, ev->u.set_path.host_no);
3348 		return -ENODEV;
3349 	}
3350 
3351 	err = transport->set_chap(shost, data, len);
3352 	scsi_host_put(shost);
3353 	return err;
3354 }
3355 
3356 static int iscsi_delete_chap(struct iscsi_transport *transport,
3357 			     struct iscsi_uevent *ev)
3358 {
3359 	struct Scsi_Host *shost;
3360 	int err = 0;
3361 
3362 	if (!transport->delete_chap)
3363 		return -ENOSYS;
3364 
3365 	shost = scsi_host_lookup(ev->u.delete_chap.host_no);
3366 	if (!shost) {
3367 		printk(KERN_ERR "%s could not find host no %u\n",
3368 		       __func__, ev->u.delete_chap.host_no);
3369 		return -ENODEV;
3370 	}
3371 
3372 	err = transport->delete_chap(shost, ev->u.delete_chap.chap_tbl_idx);
3373 	scsi_host_put(shost);
3374 	return err;
3375 }
3376 
3377 static const struct {
3378 	enum iscsi_discovery_parent_type value;
3379 	char				*name;
3380 } iscsi_discovery_parent_names[] = {
3381 	{ISCSI_DISC_PARENT_UNKNOWN,	"Unknown" },
3382 	{ISCSI_DISC_PARENT_SENDTGT,	"Sendtarget" },
3383 	{ISCSI_DISC_PARENT_ISNS,	"isns" },
3384 };
3385 
3386 char *iscsi_get_discovery_parent_name(int parent_type)
3387 {
3388 	int i;
3389 	char *state = "Unknown!";
3390 
3391 	for (i = 0; i < ARRAY_SIZE(iscsi_discovery_parent_names); i++) {
3392 		if (iscsi_discovery_parent_names[i].value & parent_type) {
3393 			state = iscsi_discovery_parent_names[i].name;
3394 			break;
3395 		}
3396 	}
3397 	return state;
3398 }
3399 EXPORT_SYMBOL_GPL(iscsi_get_discovery_parent_name);
3400 
3401 static int iscsi_set_flashnode_param(struct iscsi_transport *transport,
3402 				     struct iscsi_uevent *ev, uint32_t len)
3403 {
3404 	char *data = (char *)ev + sizeof(*ev);
3405 	struct Scsi_Host *shost;
3406 	struct iscsi_bus_flash_session *fnode_sess;
3407 	struct iscsi_bus_flash_conn *fnode_conn;
3408 	struct device *dev;
3409 	uint32_t idx;
3410 	int err = 0;
3411 
3412 	if (!transport->set_flashnode_param) {
3413 		err = -ENOSYS;
3414 		goto exit_set_fnode;
3415 	}
3416 
3417 	shost = scsi_host_lookup(ev->u.set_flashnode.host_no);
3418 	if (!shost) {
3419 		pr_err("%s could not find host no %u\n",
3420 		       __func__, ev->u.set_flashnode.host_no);
3421 		err = -ENODEV;
3422 		goto exit_set_fnode;
3423 	}
3424 
3425 	idx = ev->u.set_flashnode.flashnode_idx;
3426 	fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3427 	if (!fnode_sess) {
3428 		pr_err("%s could not find flashnode %u for host no %u\n",
3429 		       __func__, idx, ev->u.set_flashnode.host_no);
3430 		err = -ENODEV;
3431 		goto put_host;
3432 	}
3433 
3434 	dev = iscsi_find_flashnode_conn(fnode_sess);
3435 	if (!dev) {
3436 		err = -ENODEV;
3437 		goto put_sess;
3438 	}
3439 
3440 	fnode_conn = iscsi_dev_to_flash_conn(dev);
3441 	err = transport->set_flashnode_param(fnode_sess, fnode_conn, data, len);
3442 	put_device(dev);
3443 
3444 put_sess:
3445 	put_device(&fnode_sess->dev);
3446 
3447 put_host:
3448 	scsi_host_put(shost);
3449 
3450 exit_set_fnode:
3451 	return err;
3452 }
3453 
3454 static int iscsi_new_flashnode(struct iscsi_transport *transport,
3455 			       struct iscsi_uevent *ev, uint32_t len)
3456 {
3457 	char *data = (char *)ev + sizeof(*ev);
3458 	struct Scsi_Host *shost;
3459 	int index;
3460 	int err = 0;
3461 
3462 	if (!transport->new_flashnode) {
3463 		err = -ENOSYS;
3464 		goto exit_new_fnode;
3465 	}
3466 
3467 	shost = scsi_host_lookup(ev->u.new_flashnode.host_no);
3468 	if (!shost) {
3469 		pr_err("%s could not find host no %u\n",
3470 		       __func__, ev->u.new_flashnode.host_no);
3471 		err = -ENODEV;
3472 		goto put_host;
3473 	}
3474 
3475 	index = transport->new_flashnode(shost, data, len);
3476 
3477 	if (index >= 0)
3478 		ev->r.new_flashnode_ret.flashnode_idx = index;
3479 	else
3480 		err = -EIO;
3481 
3482 put_host:
3483 	scsi_host_put(shost);
3484 
3485 exit_new_fnode:
3486 	return err;
3487 }
3488 
3489 static int iscsi_del_flashnode(struct iscsi_transport *transport,
3490 			       struct iscsi_uevent *ev)
3491 {
3492 	struct Scsi_Host *shost;
3493 	struct iscsi_bus_flash_session *fnode_sess;
3494 	uint32_t idx;
3495 	int err = 0;
3496 
3497 	if (!transport->del_flashnode) {
3498 		err = -ENOSYS;
3499 		goto exit_del_fnode;
3500 	}
3501 
3502 	shost = scsi_host_lookup(ev->u.del_flashnode.host_no);
3503 	if (!shost) {
3504 		pr_err("%s could not find host no %u\n",
3505 		       __func__, ev->u.del_flashnode.host_no);
3506 		err = -ENODEV;
3507 		goto put_host;
3508 	}
3509 
3510 	idx = ev->u.del_flashnode.flashnode_idx;
3511 	fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3512 	if (!fnode_sess) {
3513 		pr_err("%s could not find flashnode %u for host no %u\n",
3514 		       __func__, idx, ev->u.del_flashnode.host_no);
3515 		err = -ENODEV;
3516 		goto put_host;
3517 	}
3518 
3519 	err = transport->del_flashnode(fnode_sess);
3520 	put_device(&fnode_sess->dev);
3521 
3522 put_host:
3523 	scsi_host_put(shost);
3524 
3525 exit_del_fnode:
3526 	return err;
3527 }
3528 
3529 static int iscsi_login_flashnode(struct iscsi_transport *transport,
3530 				 struct iscsi_uevent *ev)
3531 {
3532 	struct Scsi_Host *shost;
3533 	struct iscsi_bus_flash_session *fnode_sess;
3534 	struct iscsi_bus_flash_conn *fnode_conn;
3535 	struct device *dev;
3536 	uint32_t idx;
3537 	int err = 0;
3538 
3539 	if (!transport->login_flashnode) {
3540 		err = -ENOSYS;
3541 		goto exit_login_fnode;
3542 	}
3543 
3544 	shost = scsi_host_lookup(ev->u.login_flashnode.host_no);
3545 	if (!shost) {
3546 		pr_err("%s could not find host no %u\n",
3547 		       __func__, ev->u.login_flashnode.host_no);
3548 		err = -ENODEV;
3549 		goto put_host;
3550 	}
3551 
3552 	idx = ev->u.login_flashnode.flashnode_idx;
3553 	fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3554 	if (!fnode_sess) {
3555 		pr_err("%s could not find flashnode %u for host no %u\n",
3556 		       __func__, idx, ev->u.login_flashnode.host_no);
3557 		err = -ENODEV;
3558 		goto put_host;
3559 	}
3560 
3561 	dev = iscsi_find_flashnode_conn(fnode_sess);
3562 	if (!dev) {
3563 		err = -ENODEV;
3564 		goto put_sess;
3565 	}
3566 
3567 	fnode_conn = iscsi_dev_to_flash_conn(dev);
3568 	err = transport->login_flashnode(fnode_sess, fnode_conn);
3569 	put_device(dev);
3570 
3571 put_sess:
3572 	put_device(&fnode_sess->dev);
3573 
3574 put_host:
3575 	scsi_host_put(shost);
3576 
3577 exit_login_fnode:
3578 	return err;
3579 }
3580 
3581 static int iscsi_logout_flashnode(struct iscsi_transport *transport,
3582 				  struct iscsi_uevent *ev)
3583 {
3584 	struct Scsi_Host *shost;
3585 	struct iscsi_bus_flash_session *fnode_sess;
3586 	struct iscsi_bus_flash_conn *fnode_conn;
3587 	struct device *dev;
3588 	uint32_t idx;
3589 	int err = 0;
3590 
3591 	if (!transport->logout_flashnode) {
3592 		err = -ENOSYS;
3593 		goto exit_logout_fnode;
3594 	}
3595 
3596 	shost = scsi_host_lookup(ev->u.logout_flashnode.host_no);
3597 	if (!shost) {
3598 		pr_err("%s could not find host no %u\n",
3599 		       __func__, ev->u.logout_flashnode.host_no);
3600 		err = -ENODEV;
3601 		goto put_host;
3602 	}
3603 
3604 	idx = ev->u.logout_flashnode.flashnode_idx;
3605 	fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3606 	if (!fnode_sess) {
3607 		pr_err("%s could not find flashnode %u for host no %u\n",
3608 		       __func__, idx, ev->u.logout_flashnode.host_no);
3609 		err = -ENODEV;
3610 		goto put_host;
3611 	}
3612 
3613 	dev = iscsi_find_flashnode_conn(fnode_sess);
3614 	if (!dev) {
3615 		err = -ENODEV;
3616 		goto put_sess;
3617 	}
3618 
3619 	fnode_conn = iscsi_dev_to_flash_conn(dev);
3620 
3621 	err = transport->logout_flashnode(fnode_sess, fnode_conn);
3622 	put_device(dev);
3623 
3624 put_sess:
3625 	put_device(&fnode_sess->dev);
3626 
3627 put_host:
3628 	scsi_host_put(shost);
3629 
3630 exit_logout_fnode:
3631 	return err;
3632 }
3633 
3634 static int iscsi_logout_flashnode_sid(struct iscsi_transport *transport,
3635 				      struct iscsi_uevent *ev)
3636 {
3637 	struct Scsi_Host *shost;
3638 	struct iscsi_cls_session *session;
3639 	int err = 0;
3640 
3641 	if (!transport->logout_flashnode_sid) {
3642 		err = -ENOSYS;
3643 		goto exit_logout_sid;
3644 	}
3645 
3646 	shost = scsi_host_lookup(ev->u.logout_flashnode_sid.host_no);
3647 	if (!shost) {
3648 		pr_err("%s could not find host no %u\n",
3649 		       __func__, ev->u.logout_flashnode.host_no);
3650 		err = -ENODEV;
3651 		goto put_host;
3652 	}
3653 
3654 	session = iscsi_session_lookup(ev->u.logout_flashnode_sid.sid);
3655 	if (!session) {
3656 		pr_err("%s could not find session id %u\n",
3657 		       __func__, ev->u.logout_flashnode_sid.sid);
3658 		err = -EINVAL;
3659 		goto put_host;
3660 	}
3661 
3662 	err = transport->logout_flashnode_sid(session);
3663 
3664 put_host:
3665 	scsi_host_put(shost);
3666 
3667 exit_logout_sid:
3668 	return err;
3669 }
3670 
3671 static int
3672 iscsi_get_host_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
3673 {
3674 	struct iscsi_uevent *ev = nlmsg_data(nlh);
3675 	struct Scsi_Host *shost = NULL;
3676 	struct iscsi_internal *priv;
3677 	struct sk_buff *skbhost_stats;
3678 	struct nlmsghdr *nlhhost_stats;
3679 	struct iscsi_uevent *evhost_stats;
3680 	int host_stats_size = 0;
3681 	int len, err = 0;
3682 	char *buf;
3683 
3684 	if (!transport->get_host_stats)
3685 		return -ENOSYS;
3686 
3687 	priv = iscsi_if_transport_lookup(transport);
3688 	if (!priv)
3689 		return -EINVAL;
3690 
3691 	host_stats_size = sizeof(struct iscsi_offload_host_stats);
3692 	len = nlmsg_total_size(sizeof(*ev) + host_stats_size);
3693 
3694 	shost = scsi_host_lookup(ev->u.get_host_stats.host_no);
3695 	if (!shost) {
3696 		pr_err("%s: failed. Could not find host no %u\n",
3697 		       __func__, ev->u.get_host_stats.host_no);
3698 		return -ENODEV;
3699 	}
3700 
3701 	do {
3702 		int actual_size;
3703 
3704 		skbhost_stats = alloc_skb(len, GFP_KERNEL);
3705 		if (!skbhost_stats) {
3706 			pr_err("cannot deliver host stats: OOM\n");
3707 			err = -ENOMEM;
3708 			goto exit_host_stats;
3709 		}
3710 
3711 		nlhhost_stats = __nlmsg_put(skbhost_stats, 0, 0, 0,
3712 				      (len - sizeof(*nlhhost_stats)), 0);
3713 		evhost_stats = nlmsg_data(nlhhost_stats);
3714 		memset(evhost_stats, 0, sizeof(*evhost_stats));
3715 		evhost_stats->transport_handle = iscsi_handle(transport);
3716 		evhost_stats->type = nlh->nlmsg_type;
3717 		evhost_stats->u.get_host_stats.host_no =
3718 					ev->u.get_host_stats.host_no;
3719 		buf = (char *)evhost_stats + sizeof(*evhost_stats);
3720 		memset(buf, 0, host_stats_size);
3721 
3722 		err = transport->get_host_stats(shost, buf, host_stats_size);
3723 		if (err) {
3724 			kfree_skb(skbhost_stats);
3725 			goto exit_host_stats;
3726 		}
3727 
3728 		actual_size = nlmsg_total_size(sizeof(*ev) + host_stats_size);
3729 		skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));
3730 		nlhhost_stats->nlmsg_len = actual_size;
3731 
3732 		err = iscsi_multicast_skb(skbhost_stats, ISCSI_NL_GRP_ISCSID,
3733 					  GFP_KERNEL);
3734 	} while (err < 0 && err != -ECONNREFUSED);
3735 
3736 exit_host_stats:
3737 	scsi_host_put(shost);
3738 	return err;
3739 }
3740 
3741 static int iscsi_if_transport_conn(struct iscsi_transport *transport,
3742 				   struct nlmsghdr *nlh)
3743 {
3744 	struct iscsi_uevent *ev = nlmsg_data(nlh);
3745 	struct iscsi_cls_session *session;
3746 	struct iscsi_cls_conn *conn = NULL;
3747 	struct iscsi_endpoint *ep;
3748 	uint32_t pdu_len;
3749 	int err = 0;
3750 
3751 	switch (nlh->nlmsg_type) {
3752 	case ISCSI_UEVENT_CREATE_CONN:
3753 		return iscsi_if_create_conn(transport, ev);
3754 	case ISCSI_UEVENT_DESTROY_CONN:
3755 		return iscsi_if_destroy_conn(transport, ev);
3756 	case ISCSI_UEVENT_STOP_CONN:
3757 		conn = iscsi_conn_lookup(ev->u.stop_conn.sid,
3758 					 ev->u.stop_conn.cid);
3759 		if (!conn)
3760 			return -EINVAL;
3761 
3762 		return iscsi_if_stop_conn(conn, ev->u.stop_conn.flag);
3763 	}
3764 
3765 	/*
3766 	 * The following cmds need to be run under the ep_mutex so in kernel
3767 	 * conn cleanup (ep_disconnect + unbind and conn) is not done while
3768 	 * these are running. They also must not run if we have just run a conn
3769 	 * cleanup because they would set the state in a way that might allow
3770 	 * IO or send IO themselves.
3771 	 */
3772 	switch (nlh->nlmsg_type) {
3773 	case ISCSI_UEVENT_START_CONN:
3774 		conn = iscsi_conn_lookup(ev->u.start_conn.sid,
3775 					 ev->u.start_conn.cid);
3776 		break;
3777 	case ISCSI_UEVENT_BIND_CONN:
3778 		conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
3779 		break;
3780 	case ISCSI_UEVENT_SEND_PDU:
3781 		conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
3782 		break;
3783 	}
3784 
3785 	if (!conn)
3786 		return -EINVAL;
3787 
3788 	mutex_lock(&conn->ep_mutex);
3789 	spin_lock_irq(&conn->lock);
3790 	if (test_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags)) {
3791 		spin_unlock_irq(&conn->lock);
3792 		mutex_unlock(&conn->ep_mutex);
3793 		ev->r.retcode = -ENOTCONN;
3794 		return 0;
3795 	}
3796 	spin_unlock_irq(&conn->lock);
3797 
3798 	switch (nlh->nlmsg_type) {
3799 	case ISCSI_UEVENT_BIND_CONN:
3800 		session = iscsi_session_lookup(ev->u.b_conn.sid);
3801 		if (!session) {
3802 			err = -EINVAL;
3803 			break;
3804 		}
3805 
3806 		ev->r.retcode =	transport->bind_conn(session, conn,
3807 						ev->u.b_conn.transport_eph,
3808 						ev->u.b_conn.is_leading);
3809 		if (!ev->r.retcode)
3810 			WRITE_ONCE(conn->state, ISCSI_CONN_BOUND);
3811 
3812 		if (ev->r.retcode || !transport->ep_connect)
3813 			break;
3814 
3815 		ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
3816 		if (ep) {
3817 			ep->conn = conn;
3818 			conn->ep = ep;
3819 			iscsi_put_endpoint(ep);
3820 		} else {
3821 			err = -ENOTCONN;
3822 			iscsi_cls_conn_printk(KERN_ERR, conn,
3823 					      "Could not set ep conn binding\n");
3824 		}
3825 		break;
3826 	case ISCSI_UEVENT_START_CONN:
3827 		ev->r.retcode = transport->start_conn(conn);
3828 		if (!ev->r.retcode)
3829 			WRITE_ONCE(conn->state, ISCSI_CONN_UP);
3830 
3831 		break;
3832 	case ISCSI_UEVENT_SEND_PDU:
3833 		pdu_len = nlh->nlmsg_len - sizeof(*nlh) - sizeof(*ev);
3834 
3835 		if ((ev->u.send_pdu.hdr_size > pdu_len) ||
3836 		    (ev->u.send_pdu.data_size > (pdu_len - ev->u.send_pdu.hdr_size))) {
3837 			err = -EINVAL;
3838 			break;
3839 		}
3840 
3841 		ev->r.retcode =	transport->send_pdu(conn,
3842 				(struct iscsi_hdr *)((char *)ev + sizeof(*ev)),
3843 				(char *)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
3844 				ev->u.send_pdu.data_size);
3845 		break;
3846 	default:
3847 		err = -ENOSYS;
3848 	}
3849 
3850 	mutex_unlock(&conn->ep_mutex);
3851 	return err;
3852 }
3853 
3854 static int
3855 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
3856 {
3857 	int err = 0;
3858 	u32 portid;
3859 	struct iscsi_uevent *ev = nlmsg_data(nlh);
3860 	struct iscsi_transport *transport = NULL;
3861 	struct iscsi_internal *priv;
3862 	struct iscsi_cls_session *session;
3863 	struct iscsi_endpoint *ep = NULL;
3864 
3865 	if (!netlink_capable(skb, CAP_SYS_ADMIN))
3866 		return -EPERM;
3867 
3868 	if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
3869 		*group = ISCSI_NL_GRP_UIP;
3870 	else
3871 		*group = ISCSI_NL_GRP_ISCSID;
3872 
3873 	priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
3874 	if (!priv)
3875 		return -EINVAL;
3876 	transport = priv->iscsi_transport;
3877 
3878 	if (!try_module_get(transport->owner))
3879 		return -EINVAL;
3880 
3881 	portid = NETLINK_CB(skb).portid;
3882 
3883 	switch (nlh->nlmsg_type) {
3884 	case ISCSI_UEVENT_CREATE_SESSION:
3885 		err = iscsi_if_create_session(priv, ep, ev,
3886 					      portid,
3887 					      ev->u.c_session.initial_cmdsn,
3888 					      ev->u.c_session.cmds_max,
3889 					      ev->u.c_session.queue_depth);
3890 		break;
3891 	case ISCSI_UEVENT_CREATE_BOUND_SESSION:
3892 		ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
3893 		if (!ep) {
3894 			err = -EINVAL;
3895 			break;
3896 		}
3897 
3898 		err = iscsi_if_create_session(priv, ep, ev,
3899 					portid,
3900 					ev->u.c_bound_session.initial_cmdsn,
3901 					ev->u.c_bound_session.cmds_max,
3902 					ev->u.c_bound_session.queue_depth);
3903 		iscsi_put_endpoint(ep);
3904 		break;
3905 	case ISCSI_UEVENT_DESTROY_SESSION:
3906 		session = iscsi_session_lookup(ev->u.d_session.sid);
3907 		if (!session)
3908 			err = -EINVAL;
3909 		else if (iscsi_session_has_conns(ev->u.d_session.sid))
3910 			err = -EBUSY;
3911 		else
3912 			transport->destroy_session(session);
3913 		break;
3914 	case ISCSI_UEVENT_DESTROY_SESSION_ASYNC:
3915 		session = iscsi_session_lookup(ev->u.d_session.sid);
3916 		if (!session)
3917 			err = -EINVAL;
3918 		else if (iscsi_session_has_conns(ev->u.d_session.sid))
3919 			err = -EBUSY;
3920 		else {
3921 			unsigned long flags;
3922 
3923 			/* Prevent this session from being found again */
3924 			spin_lock_irqsave(&sesslock, flags);
3925 			list_del_init(&session->sess_list);
3926 			spin_unlock_irqrestore(&sesslock, flags);
3927 
3928 			queue_work(system_unbound_wq, &session->destroy_work);
3929 		}
3930 		break;
3931 	case ISCSI_UEVENT_UNBIND_SESSION:
3932 		session = iscsi_session_lookup(ev->u.d_session.sid);
3933 		if (session)
3934 			queue_work(session->workq, &session->unbind_work);
3935 		else
3936 			err = -EINVAL;
3937 		break;
3938 	case ISCSI_UEVENT_SET_PARAM:
3939 		err = iscsi_set_param(transport, ev);
3940 		break;
3941 	case ISCSI_UEVENT_CREATE_CONN:
3942 	case ISCSI_UEVENT_DESTROY_CONN:
3943 	case ISCSI_UEVENT_STOP_CONN:
3944 	case ISCSI_UEVENT_START_CONN:
3945 	case ISCSI_UEVENT_BIND_CONN:
3946 	case ISCSI_UEVENT_SEND_PDU:
3947 		err = iscsi_if_transport_conn(transport, nlh);
3948 		break;
3949 	case ISCSI_UEVENT_GET_STATS:
3950 		err = iscsi_if_get_stats(transport, nlh);
3951 		break;
3952 	case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
3953 	case ISCSI_UEVENT_TRANSPORT_EP_POLL:
3954 	case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
3955 	case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
3956 		err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
3957 		break;
3958 	case ISCSI_UEVENT_TGT_DSCVR:
3959 		err = iscsi_tgt_dscvr(transport, ev);
3960 		break;
3961 	case ISCSI_UEVENT_SET_HOST_PARAM:
3962 		err = iscsi_set_host_param(transport, ev);
3963 		break;
3964 	case ISCSI_UEVENT_PATH_UPDATE:
3965 		err = iscsi_set_path(transport, ev);
3966 		break;
3967 	case ISCSI_UEVENT_SET_IFACE_PARAMS:
3968 		err = iscsi_set_iface_params(transport, ev,
3969 					     nlmsg_attrlen(nlh, sizeof(*ev)));
3970 		break;
3971 	case ISCSI_UEVENT_PING:
3972 		err = iscsi_send_ping(transport, ev);
3973 		break;
3974 	case ISCSI_UEVENT_GET_CHAP:
3975 		err = iscsi_get_chap(transport, nlh);
3976 		break;
3977 	case ISCSI_UEVENT_DELETE_CHAP:
3978 		err = iscsi_delete_chap(transport, ev);
3979 		break;
3980 	case ISCSI_UEVENT_SET_FLASHNODE_PARAMS:
3981 		err = iscsi_set_flashnode_param(transport, ev,
3982 						nlmsg_attrlen(nlh,
3983 							      sizeof(*ev)));
3984 		break;
3985 	case ISCSI_UEVENT_NEW_FLASHNODE:
3986 		err = iscsi_new_flashnode(transport, ev,
3987 					  nlmsg_attrlen(nlh, sizeof(*ev)));
3988 		break;
3989 	case ISCSI_UEVENT_DEL_FLASHNODE:
3990 		err = iscsi_del_flashnode(transport, ev);
3991 		break;
3992 	case ISCSI_UEVENT_LOGIN_FLASHNODE:
3993 		err = iscsi_login_flashnode(transport, ev);
3994 		break;
3995 	case ISCSI_UEVENT_LOGOUT_FLASHNODE:
3996 		err = iscsi_logout_flashnode(transport, ev);
3997 		break;
3998 	case ISCSI_UEVENT_LOGOUT_FLASHNODE_SID:
3999 		err = iscsi_logout_flashnode_sid(transport, ev);
4000 		break;
4001 	case ISCSI_UEVENT_SET_CHAP:
4002 		err = iscsi_set_chap(transport, ev,
4003 				     nlmsg_attrlen(nlh, sizeof(*ev)));
4004 		break;
4005 	case ISCSI_UEVENT_GET_HOST_STATS:
4006 		err = iscsi_get_host_stats(transport, nlh);
4007 		break;
4008 	default:
4009 		err = -ENOSYS;
4010 		break;
4011 	}
4012 
4013 	module_put(transport->owner);
4014 	return err;
4015 }
4016 
4017 /*
4018  * Get message from skb.  Each message is processed by iscsi_if_recv_msg.
4019  * Malformed skbs with wrong lengths or invalid creds are not processed.
4020  */
4021 static void
4022 iscsi_if_rx(struct sk_buff *skb)
4023 {
4024 	u32 portid = NETLINK_CB(skb).portid;
4025 
4026 	mutex_lock(&rx_queue_mutex);
4027 	while (skb->len >= NLMSG_HDRLEN) {
4028 		int err;
4029 		uint32_t rlen;
4030 		struct nlmsghdr	*nlh;
4031 		struct iscsi_uevent *ev;
4032 		uint32_t group;
4033 		int retries = ISCSI_SEND_MAX_ALLOWED;
4034 
4035 		nlh = nlmsg_hdr(skb);
4036 		if (nlh->nlmsg_len < sizeof(*nlh) + sizeof(*ev) ||
4037 		    skb->len < nlh->nlmsg_len) {
4038 			break;
4039 		}
4040 
4041 		ev = nlmsg_data(nlh);
4042 		rlen = NLMSG_ALIGN(nlh->nlmsg_len);
4043 		if (rlen > skb->len)
4044 			rlen = skb->len;
4045 
4046 		err = iscsi_if_recv_msg(skb, nlh, &group);
4047 		if (err) {
4048 			ev->type = ISCSI_KEVENT_IF_ERROR;
4049 			ev->iferror = err;
4050 		}
4051 		do {
4052 			/*
4053 			 * special case for GET_STATS:
4054 			 * on success - sending reply and stats from
4055 			 * inside of if_recv_msg(),
4056 			 * on error - fall through.
4057 			 */
4058 			if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
4059 				break;
4060 			if (ev->type == ISCSI_UEVENT_GET_CHAP && !err)
4061 				break;
4062 			err = iscsi_if_send_reply(portid, nlh->nlmsg_type,
4063 						  ev, sizeof(*ev));
4064 			if (err == -EAGAIN && --retries < 0) {
4065 				printk(KERN_WARNING "Send reply failed, error %d\n", err);
4066 				break;
4067 			}
4068 		} while (err < 0 && err != -ECONNREFUSED && err != -ESRCH);
4069 		skb_pull(skb, rlen);
4070 	}
4071 	mutex_unlock(&rx_queue_mutex);
4072 }
4073 
4074 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store)		\
4075 struct device_attribute dev_attr_##_prefix##_##_name =	\
4076 	__ATTR(_name,_mode,_show,_store)
4077 
4078 /*
4079  * iSCSI connection attrs
4080  */
4081 #define iscsi_conn_attr_show(param)					\
4082 static ssize_t								\
4083 show_conn_param_##param(struct device *dev, 				\
4084 			struct device_attribute *attr, char *buf)	\
4085 {									\
4086 	struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);	\
4087 	struct iscsi_transport *t = conn->transport;			\
4088 	return t->get_conn_param(conn, param, buf);			\
4089 }
4090 
4091 #define iscsi_conn_attr(field, param)					\
4092 	iscsi_conn_attr_show(param)					\
4093 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param,	\
4094 			NULL);
4095 
4096 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
4097 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
4098 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
4099 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
4100 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
4101 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
4102 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
4103 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
4104 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
4105 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
4106 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
4107 iscsi_conn_attr(local_port, ISCSI_PARAM_LOCAL_PORT);
4108 iscsi_conn_attr(statsn, ISCSI_PARAM_STATSN);
4109 iscsi_conn_attr(keepalive_tmo, ISCSI_PARAM_KEEPALIVE_TMO);
4110 iscsi_conn_attr(max_segment_size, ISCSI_PARAM_MAX_SEGMENT_SIZE);
4111 iscsi_conn_attr(tcp_timestamp_stat, ISCSI_PARAM_TCP_TIMESTAMP_STAT);
4112 iscsi_conn_attr(tcp_wsf_disable, ISCSI_PARAM_TCP_WSF_DISABLE);
4113 iscsi_conn_attr(tcp_nagle_disable, ISCSI_PARAM_TCP_NAGLE_DISABLE);
4114 iscsi_conn_attr(tcp_timer_scale, ISCSI_PARAM_TCP_TIMER_SCALE);
4115 iscsi_conn_attr(tcp_timestamp_enable, ISCSI_PARAM_TCP_TIMESTAMP_EN);
4116 iscsi_conn_attr(fragment_disable, ISCSI_PARAM_IP_FRAGMENT_DISABLE);
4117 iscsi_conn_attr(ipv4_tos, ISCSI_PARAM_IPV4_TOS);
4118 iscsi_conn_attr(ipv6_traffic_class, ISCSI_PARAM_IPV6_TC);
4119 iscsi_conn_attr(ipv6_flow_label, ISCSI_PARAM_IPV6_FLOW_LABEL);
4120 iscsi_conn_attr(is_fw_assigned_ipv6, ISCSI_PARAM_IS_FW_ASSIGNED_IPV6);
4121 iscsi_conn_attr(tcp_xmit_wsf, ISCSI_PARAM_TCP_XMIT_WSF);
4122 iscsi_conn_attr(tcp_recv_wsf, ISCSI_PARAM_TCP_RECV_WSF);
4123 iscsi_conn_attr(local_ipaddr, ISCSI_PARAM_LOCAL_IPADDR);
4124 
4125 static const char *const connection_state_names[] = {
4126 	[ISCSI_CONN_UP] = "up",
4127 	[ISCSI_CONN_DOWN] = "down",
4128 	[ISCSI_CONN_FAILED] = "failed",
4129 	[ISCSI_CONN_BOUND] = "bound"
4130 };
4131 
4132 static ssize_t show_conn_state(struct device *dev,
4133 			       struct device_attribute *attr, char *buf)
4134 {
4135 	struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);
4136 	const char *state = "unknown";
4137 	int conn_state = READ_ONCE(conn->state);
4138 
4139 	if (conn_state >= 0 &&
4140 	    conn_state < ARRAY_SIZE(connection_state_names))
4141 		state = connection_state_names[conn_state];
4142 
4143 	return sysfs_emit(buf, "%s\n", state);
4144 }
4145 static ISCSI_CLASS_ATTR(conn, state, S_IRUGO, show_conn_state,
4146 			NULL);
4147 
4148 #define iscsi_conn_ep_attr_show(param)					\
4149 static ssize_t show_conn_ep_param_##param(struct device *dev,		\
4150 					  struct device_attribute *attr,\
4151 					  char *buf)			\
4152 {									\
4153 	struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);	\
4154 	struct iscsi_transport *t = conn->transport;			\
4155 	struct iscsi_endpoint *ep;					\
4156 	ssize_t rc;							\
4157 									\
4158 	/*								\
4159 	 * Need to make sure ep_disconnect does not free the LLD's	\
4160 	 * interconnect resources while we are trying to read them.	\
4161 	 */								\
4162 	mutex_lock(&conn->ep_mutex);					\
4163 	ep = conn->ep;							\
4164 	if (!ep && t->ep_connect) {					\
4165 		mutex_unlock(&conn->ep_mutex);				\
4166 		return -ENOTCONN;					\
4167 	}								\
4168 									\
4169 	if (ep)								\
4170 		rc = t->get_ep_param(ep, param, buf);			\
4171 	else								\
4172 		rc = t->get_conn_param(conn, param, buf);		\
4173 	mutex_unlock(&conn->ep_mutex);					\
4174 	return rc;							\
4175 }
4176 
4177 #define iscsi_conn_ep_attr(field, param)				\
4178 	iscsi_conn_ep_attr_show(param)					\
4179 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO,				\
4180 			show_conn_ep_param_##param, NULL);
4181 
4182 iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
4183 iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
4184 
4185 static struct attribute *iscsi_conn_attrs[] = {
4186 	&dev_attr_conn_max_recv_dlength.attr,
4187 	&dev_attr_conn_max_xmit_dlength.attr,
4188 	&dev_attr_conn_header_digest.attr,
4189 	&dev_attr_conn_data_digest.attr,
4190 	&dev_attr_conn_ifmarker.attr,
4191 	&dev_attr_conn_ofmarker.attr,
4192 	&dev_attr_conn_address.attr,
4193 	&dev_attr_conn_port.attr,
4194 	&dev_attr_conn_exp_statsn.attr,
4195 	&dev_attr_conn_persistent_address.attr,
4196 	&dev_attr_conn_persistent_port.attr,
4197 	&dev_attr_conn_ping_tmo.attr,
4198 	&dev_attr_conn_recv_tmo.attr,
4199 	&dev_attr_conn_local_port.attr,
4200 	&dev_attr_conn_statsn.attr,
4201 	&dev_attr_conn_keepalive_tmo.attr,
4202 	&dev_attr_conn_max_segment_size.attr,
4203 	&dev_attr_conn_tcp_timestamp_stat.attr,
4204 	&dev_attr_conn_tcp_wsf_disable.attr,
4205 	&dev_attr_conn_tcp_nagle_disable.attr,
4206 	&dev_attr_conn_tcp_timer_scale.attr,
4207 	&dev_attr_conn_tcp_timestamp_enable.attr,
4208 	&dev_attr_conn_fragment_disable.attr,
4209 	&dev_attr_conn_ipv4_tos.attr,
4210 	&dev_attr_conn_ipv6_traffic_class.attr,
4211 	&dev_attr_conn_ipv6_flow_label.attr,
4212 	&dev_attr_conn_is_fw_assigned_ipv6.attr,
4213 	&dev_attr_conn_tcp_xmit_wsf.attr,
4214 	&dev_attr_conn_tcp_recv_wsf.attr,
4215 	&dev_attr_conn_local_ipaddr.attr,
4216 	&dev_attr_conn_state.attr,
4217 	NULL,
4218 };
4219 
4220 static umode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
4221 					 struct attribute *attr, int i)
4222 {
4223 	struct device *cdev = container_of(kobj, struct device, kobj);
4224 	struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
4225 	struct iscsi_transport *t = conn->transport;
4226 	int param;
4227 
4228 	if (attr == &dev_attr_conn_max_recv_dlength.attr)
4229 		param = ISCSI_PARAM_MAX_RECV_DLENGTH;
4230 	else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
4231 		param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
4232 	else if (attr == &dev_attr_conn_header_digest.attr)
4233 		param = ISCSI_PARAM_HDRDGST_EN;
4234 	else if (attr == &dev_attr_conn_data_digest.attr)
4235 		param = ISCSI_PARAM_DATADGST_EN;
4236 	else if (attr == &dev_attr_conn_ifmarker.attr)
4237 		param = ISCSI_PARAM_IFMARKER_EN;
4238 	else if (attr == &dev_attr_conn_ofmarker.attr)
4239 		param = ISCSI_PARAM_OFMARKER_EN;
4240 	else if (attr == &dev_attr_conn_address.attr)
4241 		param = ISCSI_PARAM_CONN_ADDRESS;
4242 	else if (attr == &dev_attr_conn_port.attr)
4243 		param = ISCSI_PARAM_CONN_PORT;
4244 	else if (attr == &dev_attr_conn_exp_statsn.attr)
4245 		param = ISCSI_PARAM_EXP_STATSN;
4246 	else if (attr == &dev_attr_conn_persistent_address.attr)
4247 		param = ISCSI_PARAM_PERSISTENT_ADDRESS;
4248 	else if (attr == &dev_attr_conn_persistent_port.attr)
4249 		param = ISCSI_PARAM_PERSISTENT_PORT;
4250 	else if (attr == &dev_attr_conn_ping_tmo.attr)
4251 		param = ISCSI_PARAM_PING_TMO;
4252 	else if (attr == &dev_attr_conn_recv_tmo.attr)
4253 		param = ISCSI_PARAM_RECV_TMO;
4254 	else if (attr == &dev_attr_conn_local_port.attr)
4255 		param = ISCSI_PARAM_LOCAL_PORT;
4256 	else if (attr == &dev_attr_conn_statsn.attr)
4257 		param = ISCSI_PARAM_STATSN;
4258 	else if (attr == &dev_attr_conn_keepalive_tmo.attr)
4259 		param = ISCSI_PARAM_KEEPALIVE_TMO;
4260 	else if (attr == &dev_attr_conn_max_segment_size.attr)
4261 		param = ISCSI_PARAM_MAX_SEGMENT_SIZE;
4262 	else if (attr == &dev_attr_conn_tcp_timestamp_stat.attr)
4263 		param = ISCSI_PARAM_TCP_TIMESTAMP_STAT;
4264 	else if (attr == &dev_attr_conn_tcp_wsf_disable.attr)
4265 		param = ISCSI_PARAM_TCP_WSF_DISABLE;
4266 	else if (attr == &dev_attr_conn_tcp_nagle_disable.attr)
4267 		param = ISCSI_PARAM_TCP_NAGLE_DISABLE;
4268 	else if (attr == &dev_attr_conn_tcp_timer_scale.attr)
4269 		param = ISCSI_PARAM_TCP_TIMER_SCALE;
4270 	else if (attr == &dev_attr_conn_tcp_timestamp_enable.attr)
4271 		param = ISCSI_PARAM_TCP_TIMESTAMP_EN;
4272 	else if (attr == &dev_attr_conn_fragment_disable.attr)
4273 		param = ISCSI_PARAM_IP_FRAGMENT_DISABLE;
4274 	else if (attr == &dev_attr_conn_ipv4_tos.attr)
4275 		param = ISCSI_PARAM_IPV4_TOS;
4276 	else if (attr == &dev_attr_conn_ipv6_traffic_class.attr)
4277 		param = ISCSI_PARAM_IPV6_TC;
4278 	else if (attr == &dev_attr_conn_ipv6_flow_label.attr)
4279 		param = ISCSI_PARAM_IPV6_FLOW_LABEL;
4280 	else if (attr == &dev_attr_conn_is_fw_assigned_ipv6.attr)
4281 		param = ISCSI_PARAM_IS_FW_ASSIGNED_IPV6;
4282 	else if (attr == &dev_attr_conn_tcp_xmit_wsf.attr)
4283 		param = ISCSI_PARAM_TCP_XMIT_WSF;
4284 	else if (attr == &dev_attr_conn_tcp_recv_wsf.attr)
4285 		param = ISCSI_PARAM_TCP_RECV_WSF;
4286 	else if (attr == &dev_attr_conn_local_ipaddr.attr)
4287 		param = ISCSI_PARAM_LOCAL_IPADDR;
4288 	else if (attr == &dev_attr_conn_state.attr)
4289 		return S_IRUGO;
4290 	else {
4291 		WARN_ONCE(1, "Invalid conn attr");
4292 		return 0;
4293 	}
4294 
4295 	return t->attr_is_visible(ISCSI_PARAM, param);
4296 }
4297 
4298 static struct attribute_group iscsi_conn_group = {
4299 	.attrs = iscsi_conn_attrs,
4300 	.is_visible = iscsi_conn_attr_is_visible,
4301 };
4302 
4303 /*
4304  * iSCSI session attrs
4305  */
4306 #define iscsi_session_attr_show(param, perm)				\
4307 static ssize_t								\
4308 show_session_param_##param(struct device *dev,				\
4309 			   struct device_attribute *attr, char *buf)	\
4310 {									\
4311 	struct iscsi_cls_session *session = 				\
4312 		iscsi_dev_to_session(dev->parent);			\
4313 	struct iscsi_transport *t = session->transport;			\
4314 									\
4315 	if (perm && !capable(CAP_SYS_ADMIN))				\
4316 		return -EACCES;						\
4317 	return t->get_session_param(session, param, buf);		\
4318 }
4319 
4320 #define iscsi_session_attr(field, param, perm)				\
4321 	iscsi_session_attr_show(param, perm)				\
4322 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
4323 			NULL);
4324 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
4325 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
4326 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
4327 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
4328 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
4329 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
4330 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
4331 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
4332 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
4333 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
4334 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
4335 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
4336 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
4337 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
4338 iscsi_session_attr(chap_out_idx, ISCSI_PARAM_CHAP_OUT_IDX, 1);
4339 iscsi_session_attr(chap_in_idx, ISCSI_PARAM_CHAP_IN_IDX, 1);
4340 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
4341 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
4342 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
4343 iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
4344 iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
4345 iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
4346 iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
4347 iscsi_session_attr(boot_root, ISCSI_PARAM_BOOT_ROOT, 0);
4348 iscsi_session_attr(boot_nic, ISCSI_PARAM_BOOT_NIC, 0);
4349 iscsi_session_attr(boot_target, ISCSI_PARAM_BOOT_TARGET, 0);
4350 iscsi_session_attr(auto_snd_tgt_disable, ISCSI_PARAM_AUTO_SND_TGT_DISABLE, 0);
4351 iscsi_session_attr(discovery_session, ISCSI_PARAM_DISCOVERY_SESS, 0);
4352 iscsi_session_attr(portal_type, ISCSI_PARAM_PORTAL_TYPE, 0);
4353 iscsi_session_attr(chap_auth, ISCSI_PARAM_CHAP_AUTH_EN, 0);
4354 iscsi_session_attr(discovery_logout, ISCSI_PARAM_DISCOVERY_LOGOUT_EN, 0);
4355 iscsi_session_attr(bidi_chap, ISCSI_PARAM_BIDI_CHAP_EN, 0);
4356 iscsi_session_attr(discovery_auth_optional,
4357 		   ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL, 0);
4358 iscsi_session_attr(def_time2wait, ISCSI_PARAM_DEF_TIME2WAIT, 0);
4359 iscsi_session_attr(def_time2retain, ISCSI_PARAM_DEF_TIME2RETAIN, 0);
4360 iscsi_session_attr(isid, ISCSI_PARAM_ISID, 0);
4361 iscsi_session_attr(tsid, ISCSI_PARAM_TSID, 0);
4362 iscsi_session_attr(def_taskmgmt_tmo, ISCSI_PARAM_DEF_TASKMGMT_TMO, 0);
4363 iscsi_session_attr(discovery_parent_idx, ISCSI_PARAM_DISCOVERY_PARENT_IDX, 0);
4364 iscsi_session_attr(discovery_parent_type, ISCSI_PARAM_DISCOVERY_PARENT_TYPE, 0);
4365 
4366 static ssize_t
4367 show_priv_session_state(struct device *dev, struct device_attribute *attr,
4368 			char *buf)
4369 {
4370 	struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
4371 	return sysfs_emit(buf, "%s\n", iscsi_session_state_name(session->state));
4372 }
4373 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
4374 			NULL);
4375 static ssize_t
4376 show_priv_session_creator(struct device *dev, struct device_attribute *attr,
4377 			char *buf)
4378 {
4379 	struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
4380 	return sysfs_emit(buf, "%d\n", session->creator);
4381 }
4382 static ISCSI_CLASS_ATTR(priv_sess, creator, S_IRUGO, show_priv_session_creator,
4383 			NULL);
4384 static ssize_t
4385 show_priv_session_target_id(struct device *dev, struct device_attribute *attr,
4386 			    char *buf)
4387 {
4388 	struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
4389 	return sysfs_emit(buf, "%d\n", session->target_id);
4390 }
4391 static ISCSI_CLASS_ATTR(priv_sess, target_id, S_IRUGO,
4392 			show_priv_session_target_id, NULL);
4393 
4394 #define iscsi_priv_session_attr_show(field, format)			\
4395 static ssize_t								\
4396 show_priv_session_##field(struct device *dev, 				\
4397 			  struct device_attribute *attr, char *buf)	\
4398 {									\
4399 	struct iscsi_cls_session *session = 				\
4400 			iscsi_dev_to_session(dev->parent);		\
4401 	if (session->field == -1)					\
4402 		return sysfs_emit(buf, "off\n");			\
4403 	return sysfs_emit(buf, format"\n", session->field);		\
4404 }
4405 
4406 #define iscsi_priv_session_attr_store(field)				\
4407 static ssize_t								\
4408 store_priv_session_##field(struct device *dev,				\
4409 			   struct device_attribute *attr,		\
4410 			   const char *buf, size_t count)		\
4411 {									\
4412 	int val;							\
4413 	char *cp;							\
4414 	struct iscsi_cls_session *session =				\
4415 		iscsi_dev_to_session(dev->parent);			\
4416 	if ((session->state == ISCSI_SESSION_FREE) ||			\
4417 	    (session->state == ISCSI_SESSION_FAILED))			\
4418 		return -EBUSY;						\
4419 	if (strncmp(buf, "off", 3) == 0) {				\
4420 		session->field = -1;					\
4421 		session->field##_sysfs_override = true;			\
4422 	} else {							\
4423 		val = simple_strtoul(buf, &cp, 0);			\
4424 		if (*cp != '\0' && *cp != '\n')				\
4425 			return -EINVAL;					\
4426 		session->field = val;					\
4427 		session->field##_sysfs_override = true;			\
4428 	}								\
4429 	return count;							\
4430 }
4431 
4432 #define iscsi_priv_session_rw_attr(field, format)			\
4433 	iscsi_priv_session_attr_show(field, format)			\
4434 	iscsi_priv_session_attr_store(field)				\
4435 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR,		\
4436 			show_priv_session_##field,			\
4437 			store_priv_session_##field)
4438 
4439 iscsi_priv_session_rw_attr(recovery_tmo, "%d");
4440 
4441 static struct attribute *iscsi_session_attrs[] = {
4442 	&dev_attr_sess_initial_r2t.attr,
4443 	&dev_attr_sess_max_outstanding_r2t.attr,
4444 	&dev_attr_sess_immediate_data.attr,
4445 	&dev_attr_sess_first_burst_len.attr,
4446 	&dev_attr_sess_max_burst_len.attr,
4447 	&dev_attr_sess_data_pdu_in_order.attr,
4448 	&dev_attr_sess_data_seq_in_order.attr,
4449 	&dev_attr_sess_erl.attr,
4450 	&dev_attr_sess_targetname.attr,
4451 	&dev_attr_sess_tpgt.attr,
4452 	&dev_attr_sess_password.attr,
4453 	&dev_attr_sess_password_in.attr,
4454 	&dev_attr_sess_username.attr,
4455 	&dev_attr_sess_username_in.attr,
4456 	&dev_attr_sess_fast_abort.attr,
4457 	&dev_attr_sess_abort_tmo.attr,
4458 	&dev_attr_sess_lu_reset_tmo.attr,
4459 	&dev_attr_sess_tgt_reset_tmo.attr,
4460 	&dev_attr_sess_ifacename.attr,
4461 	&dev_attr_sess_initiatorname.attr,
4462 	&dev_attr_sess_targetalias.attr,
4463 	&dev_attr_sess_boot_root.attr,
4464 	&dev_attr_sess_boot_nic.attr,
4465 	&dev_attr_sess_boot_target.attr,
4466 	&dev_attr_priv_sess_recovery_tmo.attr,
4467 	&dev_attr_priv_sess_state.attr,
4468 	&dev_attr_priv_sess_creator.attr,
4469 	&dev_attr_sess_chap_out_idx.attr,
4470 	&dev_attr_sess_chap_in_idx.attr,
4471 	&dev_attr_priv_sess_target_id.attr,
4472 	&dev_attr_sess_auto_snd_tgt_disable.attr,
4473 	&dev_attr_sess_discovery_session.attr,
4474 	&dev_attr_sess_portal_type.attr,
4475 	&dev_attr_sess_chap_auth.attr,
4476 	&dev_attr_sess_discovery_logout.attr,
4477 	&dev_attr_sess_bidi_chap.attr,
4478 	&dev_attr_sess_discovery_auth_optional.attr,
4479 	&dev_attr_sess_def_time2wait.attr,
4480 	&dev_attr_sess_def_time2retain.attr,
4481 	&dev_attr_sess_isid.attr,
4482 	&dev_attr_sess_tsid.attr,
4483 	&dev_attr_sess_def_taskmgmt_tmo.attr,
4484 	&dev_attr_sess_discovery_parent_idx.attr,
4485 	&dev_attr_sess_discovery_parent_type.attr,
4486 	NULL,
4487 };
4488 
4489 static umode_t iscsi_session_attr_is_visible(struct kobject *kobj,
4490 					    struct attribute *attr, int i)
4491 {
4492 	struct device *cdev = container_of(kobj, struct device, kobj);
4493 	struct iscsi_cls_session *session = transport_class_to_session(cdev);
4494 	struct iscsi_transport *t = session->transport;
4495 	int param;
4496 
4497 	if (attr == &dev_attr_sess_initial_r2t.attr)
4498 		param = ISCSI_PARAM_INITIAL_R2T_EN;
4499 	else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
4500 		param = ISCSI_PARAM_MAX_R2T;
4501 	else if (attr == &dev_attr_sess_immediate_data.attr)
4502 		param = ISCSI_PARAM_IMM_DATA_EN;
4503 	else if (attr == &dev_attr_sess_first_burst_len.attr)
4504 		param = ISCSI_PARAM_FIRST_BURST;
4505 	else if (attr == &dev_attr_sess_max_burst_len.attr)
4506 		param = ISCSI_PARAM_MAX_BURST;
4507 	else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
4508 		param = ISCSI_PARAM_PDU_INORDER_EN;
4509 	else if (attr == &dev_attr_sess_data_seq_in_order.attr)
4510 		param = ISCSI_PARAM_DATASEQ_INORDER_EN;
4511 	else if (attr == &dev_attr_sess_erl.attr)
4512 		param = ISCSI_PARAM_ERL;
4513 	else if (attr == &dev_attr_sess_targetname.attr)
4514 		param = ISCSI_PARAM_TARGET_NAME;
4515 	else if (attr == &dev_attr_sess_tpgt.attr)
4516 		param = ISCSI_PARAM_TPGT;
4517 	else if (attr == &dev_attr_sess_chap_in_idx.attr)
4518 		param = ISCSI_PARAM_CHAP_IN_IDX;
4519 	else if (attr == &dev_attr_sess_chap_out_idx.attr)
4520 		param = ISCSI_PARAM_CHAP_OUT_IDX;
4521 	else if (attr == &dev_attr_sess_password.attr)
4522 		param = ISCSI_PARAM_USERNAME;
4523 	else if (attr == &dev_attr_sess_password_in.attr)
4524 		param = ISCSI_PARAM_USERNAME_IN;
4525 	else if (attr == &dev_attr_sess_username.attr)
4526 		param = ISCSI_PARAM_PASSWORD;
4527 	else if (attr == &dev_attr_sess_username_in.attr)
4528 		param = ISCSI_PARAM_PASSWORD_IN;
4529 	else if (attr == &dev_attr_sess_fast_abort.attr)
4530 		param = ISCSI_PARAM_FAST_ABORT;
4531 	else if (attr == &dev_attr_sess_abort_tmo.attr)
4532 		param = ISCSI_PARAM_ABORT_TMO;
4533 	else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
4534 		param = ISCSI_PARAM_LU_RESET_TMO;
4535 	else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
4536 		param = ISCSI_PARAM_TGT_RESET_TMO;
4537 	else if (attr == &dev_attr_sess_ifacename.attr)
4538 		param = ISCSI_PARAM_IFACE_NAME;
4539 	else if (attr == &dev_attr_sess_initiatorname.attr)
4540 		param = ISCSI_PARAM_INITIATOR_NAME;
4541 	else if (attr == &dev_attr_sess_targetalias.attr)
4542 		param = ISCSI_PARAM_TARGET_ALIAS;
4543 	else if (attr == &dev_attr_sess_boot_root.attr)
4544 		param = ISCSI_PARAM_BOOT_ROOT;
4545 	else if (attr == &dev_attr_sess_boot_nic.attr)
4546 		param = ISCSI_PARAM_BOOT_NIC;
4547 	else if (attr == &dev_attr_sess_boot_target.attr)
4548 		param = ISCSI_PARAM_BOOT_TARGET;
4549 	else if (attr == &dev_attr_sess_auto_snd_tgt_disable.attr)
4550 		param = ISCSI_PARAM_AUTO_SND_TGT_DISABLE;
4551 	else if (attr == &dev_attr_sess_discovery_session.attr)
4552 		param = ISCSI_PARAM_DISCOVERY_SESS;
4553 	else if (attr == &dev_attr_sess_portal_type.attr)
4554 		param = ISCSI_PARAM_PORTAL_TYPE;
4555 	else if (attr == &dev_attr_sess_chap_auth.attr)
4556 		param = ISCSI_PARAM_CHAP_AUTH_EN;
4557 	else if (attr == &dev_attr_sess_discovery_logout.attr)
4558 		param = ISCSI_PARAM_DISCOVERY_LOGOUT_EN;
4559 	else if (attr == &dev_attr_sess_bidi_chap.attr)
4560 		param = ISCSI_PARAM_BIDI_CHAP_EN;
4561 	else if (attr == &dev_attr_sess_discovery_auth_optional.attr)
4562 		param = ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL;
4563 	else if (attr == &dev_attr_sess_def_time2wait.attr)
4564 		param = ISCSI_PARAM_DEF_TIME2WAIT;
4565 	else if (attr == &dev_attr_sess_def_time2retain.attr)
4566 		param = ISCSI_PARAM_DEF_TIME2RETAIN;
4567 	else if (attr == &dev_attr_sess_isid.attr)
4568 		param = ISCSI_PARAM_ISID;
4569 	else if (attr == &dev_attr_sess_tsid.attr)
4570 		param = ISCSI_PARAM_TSID;
4571 	else if (attr == &dev_attr_sess_def_taskmgmt_tmo.attr)
4572 		param = ISCSI_PARAM_DEF_TASKMGMT_TMO;
4573 	else if (attr == &dev_attr_sess_discovery_parent_idx.attr)
4574 		param = ISCSI_PARAM_DISCOVERY_PARENT_IDX;
4575 	else if (attr == &dev_attr_sess_discovery_parent_type.attr)
4576 		param = ISCSI_PARAM_DISCOVERY_PARENT_TYPE;
4577 	else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
4578 		return S_IRUGO | S_IWUSR;
4579 	else if (attr == &dev_attr_priv_sess_state.attr)
4580 		return S_IRUGO;
4581 	else if (attr == &dev_attr_priv_sess_creator.attr)
4582 		return S_IRUGO;
4583 	else if (attr == &dev_attr_priv_sess_target_id.attr)
4584 		return S_IRUGO;
4585 	else {
4586 		WARN_ONCE(1, "Invalid session attr");
4587 		return 0;
4588 	}
4589 
4590 	return t->attr_is_visible(ISCSI_PARAM, param);
4591 }
4592 
4593 static struct attribute_group iscsi_session_group = {
4594 	.attrs = iscsi_session_attrs,
4595 	.is_visible = iscsi_session_attr_is_visible,
4596 };
4597 
4598 /*
4599  * iSCSI host attrs
4600  */
4601 #define iscsi_host_attr_show(param)					\
4602 static ssize_t								\
4603 show_host_param_##param(struct device *dev, 				\
4604 			struct device_attribute *attr, char *buf)	\
4605 {									\
4606 	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
4607 	struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
4608 	return priv->iscsi_transport->get_host_param(shost, param, buf); \
4609 }
4610 
4611 #define iscsi_host_attr(field, param)					\
4612 	iscsi_host_attr_show(param)					\
4613 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param,	\
4614 			NULL);
4615 
4616 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
4617 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
4618 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
4619 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
4620 iscsi_host_attr(port_state, ISCSI_HOST_PARAM_PORT_STATE);
4621 iscsi_host_attr(port_speed, ISCSI_HOST_PARAM_PORT_SPEED);
4622 
4623 static struct attribute *iscsi_host_attrs[] = {
4624 	&dev_attr_host_netdev.attr,
4625 	&dev_attr_host_hwaddress.attr,
4626 	&dev_attr_host_ipaddress.attr,
4627 	&dev_attr_host_initiatorname.attr,
4628 	&dev_attr_host_port_state.attr,
4629 	&dev_attr_host_port_speed.attr,
4630 	NULL,
4631 };
4632 
4633 static umode_t iscsi_host_attr_is_visible(struct kobject *kobj,
4634 					 struct attribute *attr, int i)
4635 {
4636 	struct device *cdev = container_of(kobj, struct device, kobj);
4637 	struct Scsi_Host *shost = transport_class_to_shost(cdev);
4638 	struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
4639 	int param;
4640 
4641 	if (attr == &dev_attr_host_netdev.attr)
4642 		param = ISCSI_HOST_PARAM_NETDEV_NAME;
4643 	else if (attr == &dev_attr_host_hwaddress.attr)
4644 		param = ISCSI_HOST_PARAM_HWADDRESS;
4645 	else if (attr == &dev_attr_host_ipaddress.attr)
4646 		param = ISCSI_HOST_PARAM_IPADDRESS;
4647 	else if (attr == &dev_attr_host_initiatorname.attr)
4648 		param = ISCSI_HOST_PARAM_INITIATOR_NAME;
4649 	else if (attr == &dev_attr_host_port_state.attr)
4650 		param = ISCSI_HOST_PARAM_PORT_STATE;
4651 	else if (attr == &dev_attr_host_port_speed.attr)
4652 		param = ISCSI_HOST_PARAM_PORT_SPEED;
4653 	else {
4654 		WARN_ONCE(1, "Invalid host attr");
4655 		return 0;
4656 	}
4657 
4658 	return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
4659 }
4660 
4661 static struct attribute_group iscsi_host_group = {
4662 	.attrs = iscsi_host_attrs,
4663 	.is_visible = iscsi_host_attr_is_visible,
4664 };
4665 
4666 /* convert iscsi_port_speed values to ascii string name */
4667 static const struct {
4668 	enum iscsi_port_speed	value;
4669 	char			*name;
4670 } iscsi_port_speed_names[] = {
4671 	{ISCSI_PORT_SPEED_UNKNOWN,	"Unknown" },
4672 	{ISCSI_PORT_SPEED_10MBPS,	"10 Mbps" },
4673 	{ISCSI_PORT_SPEED_100MBPS,	"100 Mbps" },
4674 	{ISCSI_PORT_SPEED_1GBPS,	"1 Gbps" },
4675 	{ISCSI_PORT_SPEED_10GBPS,	"10 Gbps" },
4676 	{ISCSI_PORT_SPEED_25GBPS,       "25 Gbps" },
4677 	{ISCSI_PORT_SPEED_40GBPS,       "40 Gbps" },
4678 };
4679 
4680 char *iscsi_get_port_speed_name(struct Scsi_Host *shost)
4681 {
4682 	int i;
4683 	char *speed = "Unknown!";
4684 	struct iscsi_cls_host *ihost = shost->shost_data;
4685 	uint32_t port_speed = ihost->port_speed;
4686 
4687 	for (i = 0; i < ARRAY_SIZE(iscsi_port_speed_names); i++) {
4688 		if (iscsi_port_speed_names[i].value & port_speed) {
4689 			speed = iscsi_port_speed_names[i].name;
4690 			break;
4691 		}
4692 	}
4693 	return speed;
4694 }
4695 EXPORT_SYMBOL_GPL(iscsi_get_port_speed_name);
4696 
4697 /* convert iscsi_port_state values to ascii string name */
4698 static const struct {
4699 	enum iscsi_port_state	value;
4700 	char			*name;
4701 } iscsi_port_state_names[] = {
4702 	{ISCSI_PORT_STATE_DOWN,		"LINK DOWN" },
4703 	{ISCSI_PORT_STATE_UP,		"LINK UP" },
4704 };
4705 
4706 char *iscsi_get_port_state_name(struct Scsi_Host *shost)
4707 {
4708 	int i;
4709 	char *state = "Unknown!";
4710 	struct iscsi_cls_host *ihost = shost->shost_data;
4711 	uint32_t port_state = ihost->port_state;
4712 
4713 	for (i = 0; i < ARRAY_SIZE(iscsi_port_state_names); i++) {
4714 		if (iscsi_port_state_names[i].value & port_state) {
4715 			state = iscsi_port_state_names[i].name;
4716 			break;
4717 		}
4718 	}
4719 	return state;
4720 }
4721 EXPORT_SYMBOL_GPL(iscsi_get_port_state_name);
4722 
4723 static int iscsi_session_match(struct attribute_container *cont,
4724 			   struct device *dev)
4725 {
4726 	struct iscsi_cls_session *session;
4727 	struct Scsi_Host *shost;
4728 	struct iscsi_internal *priv;
4729 
4730 	if (!iscsi_is_session_dev(dev))
4731 		return 0;
4732 
4733 	session = iscsi_dev_to_session(dev);
4734 	shost = iscsi_session_to_shost(session);
4735 	if (!shost->transportt)
4736 		return 0;
4737 
4738 	priv = to_iscsi_internal(shost->transportt);
4739 	if (priv->session_cont.ac.class != &iscsi_session_class.class)
4740 		return 0;
4741 
4742 	return &priv->session_cont.ac == cont;
4743 }
4744 
4745 static int iscsi_conn_match(struct attribute_container *cont,
4746 			   struct device *dev)
4747 {
4748 	struct iscsi_cls_session *session;
4749 	struct iscsi_cls_conn *conn;
4750 	struct Scsi_Host *shost;
4751 	struct iscsi_internal *priv;
4752 
4753 	if (!iscsi_is_conn_dev(dev))
4754 		return 0;
4755 
4756 	conn = iscsi_dev_to_conn(dev);
4757 	session = iscsi_dev_to_session(conn->dev.parent);
4758 	shost = iscsi_session_to_shost(session);
4759 
4760 	if (!shost->transportt)
4761 		return 0;
4762 
4763 	priv = to_iscsi_internal(shost->transportt);
4764 	if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
4765 		return 0;
4766 
4767 	return &priv->conn_cont.ac == cont;
4768 }
4769 
4770 static int iscsi_host_match(struct attribute_container *cont,
4771 			    struct device *dev)
4772 {
4773 	struct Scsi_Host *shost;
4774 	struct iscsi_internal *priv;
4775 
4776 	if (!scsi_is_host_device(dev))
4777 		return 0;
4778 
4779 	shost = dev_to_shost(dev);
4780 	if (!shost->transportt  ||
4781 	    shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
4782 		return 0;
4783 
4784         priv = to_iscsi_internal(shost->transportt);
4785         return &priv->t.host_attrs.ac == cont;
4786 }
4787 
4788 struct scsi_transport_template *
4789 iscsi_register_transport(struct iscsi_transport *tt)
4790 {
4791 	struct iscsi_internal *priv;
4792 	unsigned long flags;
4793 	int err;
4794 
4795 	BUG_ON(!tt);
4796 	WARN_ON(tt->ep_disconnect && !tt->unbind_conn);
4797 
4798 	priv = iscsi_if_transport_lookup(tt);
4799 	if (priv)
4800 		return NULL;
4801 
4802 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
4803 	if (!priv)
4804 		return NULL;
4805 	INIT_LIST_HEAD(&priv->list);
4806 	priv->iscsi_transport = tt;
4807 	priv->t.user_scan = iscsi_user_scan;
4808 
4809 	priv->dev.class = &iscsi_transport_class;
4810 	dev_set_name(&priv->dev, "%s", tt->name);
4811 	err = device_register(&priv->dev);
4812 	if (err)
4813 		goto free_priv;
4814 
4815 	err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
4816 	if (err)
4817 		goto unregister_dev;
4818 
4819 	/* host parameters */
4820 	priv->t.host_attrs.ac.class = &iscsi_host_class.class;
4821 	priv->t.host_attrs.ac.match = iscsi_host_match;
4822 	priv->t.host_attrs.ac.grp = &iscsi_host_group;
4823 	priv->t.host_size = sizeof(struct iscsi_cls_host);
4824 	transport_container_register(&priv->t.host_attrs);
4825 
4826 	/* connection parameters */
4827 	priv->conn_cont.ac.class = &iscsi_connection_class.class;
4828 	priv->conn_cont.ac.match = iscsi_conn_match;
4829 	priv->conn_cont.ac.grp = &iscsi_conn_group;
4830 	transport_container_register(&priv->conn_cont);
4831 
4832 	/* session parameters */
4833 	priv->session_cont.ac.class = &iscsi_session_class.class;
4834 	priv->session_cont.ac.match = iscsi_session_match;
4835 	priv->session_cont.ac.grp = &iscsi_session_group;
4836 	transport_container_register(&priv->session_cont);
4837 
4838 	spin_lock_irqsave(&iscsi_transport_lock, flags);
4839 	list_add(&priv->list, &iscsi_transports);
4840 	spin_unlock_irqrestore(&iscsi_transport_lock, flags);
4841 
4842 	printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
4843 	return &priv->t;
4844 
4845 unregister_dev:
4846 	device_unregister(&priv->dev);
4847 	return NULL;
4848 free_priv:
4849 	kfree(priv);
4850 	return NULL;
4851 }
4852 EXPORT_SYMBOL_GPL(iscsi_register_transport);
4853 
4854 void iscsi_unregister_transport(struct iscsi_transport *tt)
4855 {
4856 	struct iscsi_internal *priv;
4857 	unsigned long flags;
4858 
4859 	BUG_ON(!tt);
4860 
4861 	mutex_lock(&rx_queue_mutex);
4862 
4863 	priv = iscsi_if_transport_lookup(tt);
4864 	BUG_ON (!priv);
4865 
4866 	spin_lock_irqsave(&iscsi_transport_lock, flags);
4867 	list_del(&priv->list);
4868 	spin_unlock_irqrestore(&iscsi_transport_lock, flags);
4869 
4870 	transport_container_unregister(&priv->conn_cont);
4871 	transport_container_unregister(&priv->session_cont);
4872 	transport_container_unregister(&priv->t.host_attrs);
4873 
4874 	sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
4875 	device_unregister(&priv->dev);
4876 	mutex_unlock(&rx_queue_mutex);
4877 }
4878 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
4879 
4880 void iscsi_dbg_trace(void (*trace)(struct device *dev, struct va_format *),
4881 		     struct device *dev, const char *fmt, ...)
4882 {
4883 	struct va_format vaf;
4884 	va_list args;
4885 
4886 	va_start(args, fmt);
4887 	vaf.fmt = fmt;
4888 	vaf.va = &args;
4889 	trace(dev, &vaf);
4890 	va_end(args);
4891 }
4892 EXPORT_SYMBOL_GPL(iscsi_dbg_trace);
4893 
4894 static __init int iscsi_transport_init(void)
4895 {
4896 	int err;
4897 	struct netlink_kernel_cfg cfg = {
4898 		.groups	= 1,
4899 		.input	= iscsi_if_rx,
4900 	};
4901 	printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
4902 		ISCSI_TRANSPORT_VERSION);
4903 
4904 	atomic_set(&iscsi_session_nr, 0);
4905 
4906 	err = class_register(&iscsi_transport_class);
4907 	if (err)
4908 		return err;
4909 
4910 	err = class_register(&iscsi_endpoint_class);
4911 	if (err)
4912 		goto unregister_transport_class;
4913 
4914 	err = class_register(&iscsi_iface_class);
4915 	if (err)
4916 		goto unregister_endpoint_class;
4917 
4918 	err = transport_class_register(&iscsi_host_class);
4919 	if (err)
4920 		goto unregister_iface_class;
4921 
4922 	err = transport_class_register(&iscsi_connection_class);
4923 	if (err)
4924 		goto unregister_host_class;
4925 
4926 	err = transport_class_register(&iscsi_session_class);
4927 	if (err)
4928 		goto unregister_conn_class;
4929 
4930 	err = bus_register(&iscsi_flashnode_bus);
4931 	if (err)
4932 		goto unregister_session_class;
4933 
4934 	nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, &cfg);
4935 	if (!nls) {
4936 		err = -ENOBUFS;
4937 		goto unregister_flashnode_bus;
4938 	}
4939 
4940 	iscsi_conn_cleanup_workq = alloc_workqueue("%s",
4941 			WQ_SYSFS | WQ_MEM_RECLAIM | WQ_UNBOUND, 0,
4942 			"iscsi_conn_cleanup");
4943 	if (!iscsi_conn_cleanup_workq) {
4944 		err = -ENOMEM;
4945 		goto release_nls;
4946 	}
4947 
4948 	return 0;
4949 
4950 release_nls:
4951 	netlink_kernel_release(nls);
4952 unregister_flashnode_bus:
4953 	bus_unregister(&iscsi_flashnode_bus);
4954 unregister_session_class:
4955 	transport_class_unregister(&iscsi_session_class);
4956 unregister_conn_class:
4957 	transport_class_unregister(&iscsi_connection_class);
4958 unregister_host_class:
4959 	transport_class_unregister(&iscsi_host_class);
4960 unregister_iface_class:
4961 	class_unregister(&iscsi_iface_class);
4962 unregister_endpoint_class:
4963 	class_unregister(&iscsi_endpoint_class);
4964 unregister_transport_class:
4965 	class_unregister(&iscsi_transport_class);
4966 	return err;
4967 }
4968 
4969 static void __exit iscsi_transport_exit(void)
4970 {
4971 	destroy_workqueue(iscsi_conn_cleanup_workq);
4972 	netlink_kernel_release(nls);
4973 	bus_unregister(&iscsi_flashnode_bus);
4974 	transport_class_unregister(&iscsi_connection_class);
4975 	transport_class_unregister(&iscsi_session_class);
4976 	transport_class_unregister(&iscsi_host_class);
4977 	class_unregister(&iscsi_endpoint_class);
4978 	class_unregister(&iscsi_iface_class);
4979 	class_unregister(&iscsi_transport_class);
4980 }
4981 
4982 module_init(iscsi_transport_init);
4983 module_exit(iscsi_transport_exit);
4984 
4985 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
4986 	      "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
4987 	      "Alex Aizman <itn780@yahoo.com>");
4988 MODULE_DESCRIPTION("iSCSI Transport Interface");
4989 MODULE_LICENSE("GPL");
4990 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
4991 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);
4992