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