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