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