HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / drivers / scsi / scsi_transport_iscsi.c
blob4903640316480ecfb38658b0032692266b107592
1 /*
2 * iSCSI transport class definitions
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
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.
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.
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.
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>
38 #define ISCSI_TRANSPORT_VERSION "2.0-870"
40 #define ISCSI_SEND_MAX_ALLOWED 10
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.");
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.");
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);
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);
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;
80 struct transport_container conn_cont;
81 struct transport_container session_cont;
84 static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
85 static struct workqueue_struct *iscsi_eh_timer_workq;
87 static DEFINE_IDA(iscsi_sess_ida);
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.
93 static LIST_HEAD(iscsi_transports);
94 static DEFINE_SPINLOCK(iscsi_transport_lock);
96 #define to_iscsi_internal(tmpl) \
97 container_of(tmpl, struct iscsi_internal, t)
99 #define dev_to_iscsi_internal(_dev) \
100 container_of(_dev, struct iscsi_internal, dev)
102 static void iscsi_transport_release(struct device *dev)
104 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
105 kfree(priv);
109 * iscsi_transport_class represents the iscsi_transports that are
110 * registered.
112 static struct class iscsi_transport_class = {
113 .name = "iscsi_transport",
114 .dev_release = iscsi_transport_release,
117 static ssize_t
118 show_transport_handle(struct device *dev, struct device_attribute *attr,
119 char *buf)
121 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
122 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
124 static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
126 #define show_transport_attr(name, format) \
127 static ssize_t \
128 show_transport_##name(struct device *dev, \
129 struct device_attribute *attr,char *buf) \
131 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
132 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
134 static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
136 show_transport_attr(caps, "0x%x");
138 static struct attribute *iscsi_transport_attrs[] = {
139 &dev_attr_handle.attr,
140 &dev_attr_caps.attr,
141 NULL,
144 static struct attribute_group iscsi_transport_group = {
145 .attrs = iscsi_transport_attrs,
149 * iSCSI endpoint attrs
151 #define iscsi_dev_to_endpoint(_dev) \
152 container_of(_dev, struct iscsi_endpoint, dev)
154 #define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
155 struct device_attribute dev_attr_##_prefix##_##_name = \
156 __ATTR(_name,_mode,_show,_store)
158 static void iscsi_endpoint_release(struct device *dev)
160 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
161 kfree(ep);
164 static struct class iscsi_endpoint_class = {
165 .name = "iscsi_endpoint",
166 .dev_release = iscsi_endpoint_release,
169 static ssize_t
170 show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
172 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
173 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
175 static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
177 static struct attribute *iscsi_endpoint_attrs[] = {
178 &dev_attr_ep_handle.attr,
179 NULL,
182 static struct attribute_group iscsi_endpoint_group = {
183 .attrs = iscsi_endpoint_attrs,
186 #define ISCSI_MAX_EPID -1
188 static int iscsi_match_epid(struct device *dev, const void *data)
190 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
191 const uint64_t *epid = data;
193 return *epid == ep->id;
196 struct iscsi_endpoint *
197 iscsi_create_endpoint(int dd_size)
199 struct device *dev;
200 struct iscsi_endpoint *ep;
201 uint64_t id;
202 int err;
204 for (id = 1; id < ISCSI_MAX_EPID; id++) {
205 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
206 iscsi_match_epid);
207 if (!dev)
208 break;
209 else
210 put_device(dev);
212 if (id == ISCSI_MAX_EPID) {
213 printk(KERN_ERR "Too many connections. Max supported %u\n",
214 ISCSI_MAX_EPID - 1);
215 return NULL;
218 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
219 if (!ep)
220 return NULL;
222 ep->id = id;
223 ep->dev.class = &iscsi_endpoint_class;
224 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
225 err = device_register(&ep->dev);
226 if (err)
227 goto free_ep;
229 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
230 if (err)
231 goto unregister_dev;
233 if (dd_size)
234 ep->dd_data = &ep[1];
235 return ep;
237 unregister_dev:
238 device_unregister(&ep->dev);
239 return NULL;
241 free_ep:
242 kfree(ep);
243 return NULL;
245 EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
247 void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
249 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
250 device_unregister(&ep->dev);
252 EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
254 struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
256 struct iscsi_endpoint *ep;
257 struct device *dev;
259 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
260 iscsi_match_epid);
261 if (!dev)
262 return NULL;
264 ep = iscsi_dev_to_endpoint(dev);
266 * we can drop this now because the interface will prevent
267 * removals and lookups from racing.
269 put_device(dev);
270 return ep;
272 EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
275 * Interface to display network param to sysfs
278 static void iscsi_iface_release(struct device *dev)
280 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
281 struct device *parent = iface->dev.parent;
283 kfree(iface);
284 put_device(parent);
288 static struct class iscsi_iface_class = {
289 .name = "iscsi_iface",
290 .dev_release = iscsi_iface_release,
293 #define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
294 struct device_attribute dev_attr_##_prefix##_##_name = \
295 __ATTR(_name, _mode, _show, _store)
297 /* iface attrs show */
298 #define iscsi_iface_attr_show(type, name, param_type, param) \
299 static ssize_t \
300 show_##type##_##name(struct device *dev, struct device_attribute *attr, \
301 char *buf) \
303 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
304 struct iscsi_transport *t = iface->transport; \
305 return t->get_iface_param(iface, param_type, param, buf); \
308 #define iscsi_iface_net_attr(type, name, param) \
309 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
310 static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
312 #define iscsi_iface_attr(type, name, param) \
313 iscsi_iface_attr_show(type, name, ISCSI_IFACE_PARAM, param) \
314 static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
316 /* generic read only ipv4 attribute */
317 iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
318 iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
319 iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
320 iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
321 iscsi_iface_net_attr(ipv4_iface, dhcp_dns_address_en,
322 ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN);
323 iscsi_iface_net_attr(ipv4_iface, dhcp_slp_da_info_en,
324 ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN);
325 iscsi_iface_net_attr(ipv4_iface, tos_en, ISCSI_NET_PARAM_IPV4_TOS_EN);
326 iscsi_iface_net_attr(ipv4_iface, tos, ISCSI_NET_PARAM_IPV4_TOS);
327 iscsi_iface_net_attr(ipv4_iface, grat_arp_en,
328 ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN);
329 iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id_en,
330 ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN);
331 iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id,
332 ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID);
333 iscsi_iface_net_attr(ipv4_iface, dhcp_req_vendor_id_en,
334 ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN);
335 iscsi_iface_net_attr(ipv4_iface, dhcp_use_vendor_id_en,
336 ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN);
337 iscsi_iface_net_attr(ipv4_iface, dhcp_vendor_id,
338 ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID);
339 iscsi_iface_net_attr(ipv4_iface, dhcp_learn_iqn_en,
340 ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN);
341 iscsi_iface_net_attr(ipv4_iface, fragment_disable,
342 ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE);
343 iscsi_iface_net_attr(ipv4_iface, incoming_forwarding_en,
344 ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN);
345 iscsi_iface_net_attr(ipv4_iface, ttl, ISCSI_NET_PARAM_IPV4_TTL);
347 /* generic read only ipv6 attribute */
348 iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
349 iscsi_iface_net_attr(ipv6_iface, link_local_addr,
350 ISCSI_NET_PARAM_IPV6_LINKLOCAL);
351 iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
352 iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
353 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
354 iscsi_iface_net_attr(ipv6_iface, link_local_autocfg,
355 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
356 iscsi_iface_net_attr(ipv6_iface, link_local_state,
357 ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE);
358 iscsi_iface_net_attr(ipv6_iface, router_state,
359 ISCSI_NET_PARAM_IPV6_ROUTER_STATE);
360 iscsi_iface_net_attr(ipv6_iface, grat_neighbor_adv_en,
361 ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN);
362 iscsi_iface_net_attr(ipv6_iface, mld_en, ISCSI_NET_PARAM_IPV6_MLD_EN);
363 iscsi_iface_net_attr(ipv6_iface, flow_label, ISCSI_NET_PARAM_IPV6_FLOW_LABEL);
364 iscsi_iface_net_attr(ipv6_iface, traffic_class,
365 ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS);
366 iscsi_iface_net_attr(ipv6_iface, hop_limit, ISCSI_NET_PARAM_IPV6_HOP_LIMIT);
367 iscsi_iface_net_attr(ipv6_iface, nd_reachable_tmo,
368 ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO);
369 iscsi_iface_net_attr(ipv6_iface, nd_rexmit_time,
370 ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME);
371 iscsi_iface_net_attr(ipv6_iface, nd_stale_tmo,
372 ISCSI_NET_PARAM_IPV6_ND_STALE_TMO);
373 iscsi_iface_net_attr(ipv6_iface, dup_addr_detect_cnt,
374 ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT);
375 iscsi_iface_net_attr(ipv6_iface, router_adv_link_mtu,
376 ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU);
378 /* common read only iface attribute */
379 iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
380 iscsi_iface_net_attr(iface, vlan_id, ISCSI_NET_PARAM_VLAN_ID);
381 iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
382 iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
383 iscsi_iface_net_attr(iface, mtu, ISCSI_NET_PARAM_MTU);
384 iscsi_iface_net_attr(iface, port, ISCSI_NET_PARAM_PORT);
385 iscsi_iface_net_attr(iface, ipaddress_state, ISCSI_NET_PARAM_IPADDR_STATE);
386 iscsi_iface_net_attr(iface, delayed_ack_en, ISCSI_NET_PARAM_DELAYED_ACK_EN);
387 iscsi_iface_net_attr(iface, tcp_nagle_disable,
388 ISCSI_NET_PARAM_TCP_NAGLE_DISABLE);
389 iscsi_iface_net_attr(iface, tcp_wsf_disable, ISCSI_NET_PARAM_TCP_WSF_DISABLE);
390 iscsi_iface_net_attr(iface, tcp_wsf, ISCSI_NET_PARAM_TCP_WSF);
391 iscsi_iface_net_attr(iface, tcp_timer_scale, ISCSI_NET_PARAM_TCP_TIMER_SCALE);
392 iscsi_iface_net_attr(iface, tcp_timestamp_en, ISCSI_NET_PARAM_TCP_TIMESTAMP_EN);
393 iscsi_iface_net_attr(iface, cache_id, ISCSI_NET_PARAM_CACHE_ID);
394 iscsi_iface_net_attr(iface, redirect_en, ISCSI_NET_PARAM_REDIRECT_EN);
396 /* common iscsi specific settings attributes */
397 iscsi_iface_attr(iface, def_taskmgmt_tmo, ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO);
398 iscsi_iface_attr(iface, header_digest, ISCSI_IFACE_PARAM_HDRDGST_EN);
399 iscsi_iface_attr(iface, data_digest, ISCSI_IFACE_PARAM_DATADGST_EN);
400 iscsi_iface_attr(iface, immediate_data, ISCSI_IFACE_PARAM_IMM_DATA_EN);
401 iscsi_iface_attr(iface, initial_r2t, ISCSI_IFACE_PARAM_INITIAL_R2T_EN);
402 iscsi_iface_attr(iface, data_seq_in_order,
403 ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN);
404 iscsi_iface_attr(iface, data_pdu_in_order, ISCSI_IFACE_PARAM_PDU_INORDER_EN);
405 iscsi_iface_attr(iface, erl, ISCSI_IFACE_PARAM_ERL);
406 iscsi_iface_attr(iface, max_recv_dlength, ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH);
407 iscsi_iface_attr(iface, first_burst_len, ISCSI_IFACE_PARAM_FIRST_BURST);
408 iscsi_iface_attr(iface, max_outstanding_r2t, ISCSI_IFACE_PARAM_MAX_R2T);
409 iscsi_iface_attr(iface, max_burst_len, ISCSI_IFACE_PARAM_MAX_BURST);
410 iscsi_iface_attr(iface, chap_auth, ISCSI_IFACE_PARAM_CHAP_AUTH_EN);
411 iscsi_iface_attr(iface, bidi_chap, ISCSI_IFACE_PARAM_BIDI_CHAP_EN);
412 iscsi_iface_attr(iface, discovery_auth_optional,
413 ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL);
414 iscsi_iface_attr(iface, discovery_logout,
415 ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN);
416 iscsi_iface_attr(iface, strict_login_comp_en,
417 ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN);
418 iscsi_iface_attr(iface, initiator_name, ISCSI_IFACE_PARAM_INITIATOR_NAME);
420 static umode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
421 struct attribute *attr, int i)
423 struct device *dev = container_of(kobj, struct device, kobj);
424 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
425 struct iscsi_transport *t = iface->transport;
426 int param;
427 int param_type;
429 if (attr == &dev_attr_iface_enabled.attr)
430 param = ISCSI_NET_PARAM_IFACE_ENABLE;
431 else if (attr == &dev_attr_iface_vlan_id.attr)
432 param = ISCSI_NET_PARAM_VLAN_ID;
433 else if (attr == &dev_attr_iface_vlan_priority.attr)
434 param = ISCSI_NET_PARAM_VLAN_PRIORITY;
435 else if (attr == &dev_attr_iface_vlan_enabled.attr)
436 param = ISCSI_NET_PARAM_VLAN_ENABLED;
437 else if (attr == &dev_attr_iface_mtu.attr)
438 param = ISCSI_NET_PARAM_MTU;
439 else if (attr == &dev_attr_iface_port.attr)
440 param = ISCSI_NET_PARAM_PORT;
441 else if (attr == &dev_attr_iface_ipaddress_state.attr)
442 param = ISCSI_NET_PARAM_IPADDR_STATE;
443 else if (attr == &dev_attr_iface_delayed_ack_en.attr)
444 param = ISCSI_NET_PARAM_DELAYED_ACK_EN;
445 else if (attr == &dev_attr_iface_tcp_nagle_disable.attr)
446 param = ISCSI_NET_PARAM_TCP_NAGLE_DISABLE;
447 else if (attr == &dev_attr_iface_tcp_wsf_disable.attr)
448 param = ISCSI_NET_PARAM_TCP_WSF_DISABLE;
449 else if (attr == &dev_attr_iface_tcp_wsf.attr)
450 param = ISCSI_NET_PARAM_TCP_WSF;
451 else if (attr == &dev_attr_iface_tcp_timer_scale.attr)
452 param = ISCSI_NET_PARAM_TCP_TIMER_SCALE;
453 else if (attr == &dev_attr_iface_tcp_timestamp_en.attr)
454 param = ISCSI_NET_PARAM_TCP_TIMESTAMP_EN;
455 else if (attr == &dev_attr_iface_cache_id.attr)
456 param = ISCSI_NET_PARAM_CACHE_ID;
457 else if (attr == &dev_attr_iface_redirect_en.attr)
458 param = ISCSI_NET_PARAM_REDIRECT_EN;
459 else if (attr == &dev_attr_iface_def_taskmgmt_tmo.attr)
460 param = ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO;
461 else if (attr == &dev_attr_iface_header_digest.attr)
462 param = ISCSI_IFACE_PARAM_HDRDGST_EN;
463 else if (attr == &dev_attr_iface_data_digest.attr)
464 param = ISCSI_IFACE_PARAM_DATADGST_EN;
465 else if (attr == &dev_attr_iface_immediate_data.attr)
466 param = ISCSI_IFACE_PARAM_IMM_DATA_EN;
467 else if (attr == &dev_attr_iface_initial_r2t.attr)
468 param = ISCSI_IFACE_PARAM_INITIAL_R2T_EN;
469 else if (attr == &dev_attr_iface_data_seq_in_order.attr)
470 param = ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN;
471 else if (attr == &dev_attr_iface_data_pdu_in_order.attr)
472 param = ISCSI_IFACE_PARAM_PDU_INORDER_EN;
473 else if (attr == &dev_attr_iface_erl.attr)
474 param = ISCSI_IFACE_PARAM_ERL;
475 else if (attr == &dev_attr_iface_max_recv_dlength.attr)
476 param = ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH;
477 else if (attr == &dev_attr_iface_first_burst_len.attr)
478 param = ISCSI_IFACE_PARAM_FIRST_BURST;
479 else if (attr == &dev_attr_iface_max_outstanding_r2t.attr)
480 param = ISCSI_IFACE_PARAM_MAX_R2T;
481 else if (attr == &dev_attr_iface_max_burst_len.attr)
482 param = ISCSI_IFACE_PARAM_MAX_BURST;
483 else if (attr == &dev_attr_iface_chap_auth.attr)
484 param = ISCSI_IFACE_PARAM_CHAP_AUTH_EN;
485 else if (attr == &dev_attr_iface_bidi_chap.attr)
486 param = ISCSI_IFACE_PARAM_BIDI_CHAP_EN;
487 else if (attr == &dev_attr_iface_discovery_auth_optional.attr)
488 param = ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL;
489 else if (attr == &dev_attr_iface_discovery_logout.attr)
490 param = ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN;
491 else if (attr == &dev_attr_iface_strict_login_comp_en.attr)
492 param = ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN;
493 else if (attr == &dev_attr_iface_initiator_name.attr)
494 param = ISCSI_IFACE_PARAM_INITIATOR_NAME;
495 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
496 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
497 param = ISCSI_NET_PARAM_IPV4_ADDR;
498 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
499 param = ISCSI_NET_PARAM_IPV4_GW;
500 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
501 param = ISCSI_NET_PARAM_IPV4_SUBNET;
502 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
503 param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
504 else if (attr ==
505 &dev_attr_ipv4_iface_dhcp_dns_address_en.attr)
506 param = ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN;
507 else if (attr ==
508 &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr)
509 param = ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN;
510 else if (attr == &dev_attr_ipv4_iface_tos_en.attr)
511 param = ISCSI_NET_PARAM_IPV4_TOS_EN;
512 else if (attr == &dev_attr_ipv4_iface_tos.attr)
513 param = ISCSI_NET_PARAM_IPV4_TOS;
514 else if (attr == &dev_attr_ipv4_iface_grat_arp_en.attr)
515 param = ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN;
516 else if (attr ==
517 &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr)
518 param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN;
519 else if (attr == &dev_attr_ipv4_iface_dhcp_alt_client_id.attr)
520 param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID;
521 else if (attr ==
522 &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr)
523 param = ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN;
524 else if (attr ==
525 &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr)
526 param = ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN;
527 else if (attr == &dev_attr_ipv4_iface_dhcp_vendor_id.attr)
528 param = ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID;
529 else if (attr ==
530 &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr)
531 param = ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN;
532 else if (attr ==
533 &dev_attr_ipv4_iface_fragment_disable.attr)
534 param = ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE;
535 else if (attr ==
536 &dev_attr_ipv4_iface_incoming_forwarding_en.attr)
537 param = ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN;
538 else if (attr == &dev_attr_ipv4_iface_ttl.attr)
539 param = ISCSI_NET_PARAM_IPV4_TTL;
540 else
541 return 0;
542 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
543 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
544 param = ISCSI_NET_PARAM_IPV6_ADDR;
545 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
546 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
547 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
548 param = ISCSI_NET_PARAM_IPV6_ROUTER;
549 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
550 param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
551 else if (attr == &dev_attr_ipv6_iface_link_local_autocfg.attr)
552 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
553 else if (attr == &dev_attr_ipv6_iface_link_local_state.attr)
554 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE;
555 else if (attr == &dev_attr_ipv6_iface_router_state.attr)
556 param = ISCSI_NET_PARAM_IPV6_ROUTER_STATE;
557 else if (attr ==
558 &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr)
559 param = ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN;
560 else if (attr == &dev_attr_ipv6_iface_mld_en.attr)
561 param = ISCSI_NET_PARAM_IPV6_MLD_EN;
562 else if (attr == &dev_attr_ipv6_iface_flow_label.attr)
563 param = ISCSI_NET_PARAM_IPV6_FLOW_LABEL;
564 else if (attr == &dev_attr_ipv6_iface_traffic_class.attr)
565 param = ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS;
566 else if (attr == &dev_attr_ipv6_iface_hop_limit.attr)
567 param = ISCSI_NET_PARAM_IPV6_HOP_LIMIT;
568 else if (attr == &dev_attr_ipv6_iface_nd_reachable_tmo.attr)
569 param = ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO;
570 else if (attr == &dev_attr_ipv6_iface_nd_rexmit_time.attr)
571 param = ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME;
572 else if (attr == &dev_attr_ipv6_iface_nd_stale_tmo.attr)
573 param = ISCSI_NET_PARAM_IPV6_ND_STALE_TMO;
574 else if (attr == &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr)
575 param = ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT;
576 else if (attr == &dev_attr_ipv6_iface_router_adv_link_mtu.attr)
577 param = ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU;
578 else
579 return 0;
580 } else {
581 WARN_ONCE(1, "Invalid iface attr");
582 return 0;
585 switch (param) {
586 case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
587 case ISCSI_IFACE_PARAM_HDRDGST_EN:
588 case ISCSI_IFACE_PARAM_DATADGST_EN:
589 case ISCSI_IFACE_PARAM_IMM_DATA_EN:
590 case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
591 case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
592 case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
593 case ISCSI_IFACE_PARAM_ERL:
594 case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
595 case ISCSI_IFACE_PARAM_FIRST_BURST:
596 case ISCSI_IFACE_PARAM_MAX_R2T:
597 case ISCSI_IFACE_PARAM_MAX_BURST:
598 case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
599 case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
600 case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
601 case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
602 case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
603 case ISCSI_IFACE_PARAM_INITIATOR_NAME:
604 param_type = ISCSI_IFACE_PARAM;
605 break;
606 default:
607 param_type = ISCSI_NET_PARAM;
610 return t->attr_is_visible(param_type, param);
613 static struct attribute *iscsi_iface_attrs[] = {
614 &dev_attr_iface_enabled.attr,
615 &dev_attr_iface_vlan_id.attr,
616 &dev_attr_iface_vlan_priority.attr,
617 &dev_attr_iface_vlan_enabled.attr,
618 &dev_attr_ipv4_iface_ipaddress.attr,
619 &dev_attr_ipv4_iface_gateway.attr,
620 &dev_attr_ipv4_iface_subnet.attr,
621 &dev_attr_ipv4_iface_bootproto.attr,
622 &dev_attr_ipv6_iface_ipaddress.attr,
623 &dev_attr_ipv6_iface_link_local_addr.attr,
624 &dev_attr_ipv6_iface_router_addr.attr,
625 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
626 &dev_attr_ipv6_iface_link_local_autocfg.attr,
627 &dev_attr_iface_mtu.attr,
628 &dev_attr_iface_port.attr,
629 &dev_attr_iface_ipaddress_state.attr,
630 &dev_attr_iface_delayed_ack_en.attr,
631 &dev_attr_iface_tcp_nagle_disable.attr,
632 &dev_attr_iface_tcp_wsf_disable.attr,
633 &dev_attr_iface_tcp_wsf.attr,
634 &dev_attr_iface_tcp_timer_scale.attr,
635 &dev_attr_iface_tcp_timestamp_en.attr,
636 &dev_attr_iface_cache_id.attr,
637 &dev_attr_iface_redirect_en.attr,
638 &dev_attr_iface_def_taskmgmt_tmo.attr,
639 &dev_attr_iface_header_digest.attr,
640 &dev_attr_iface_data_digest.attr,
641 &dev_attr_iface_immediate_data.attr,
642 &dev_attr_iface_initial_r2t.attr,
643 &dev_attr_iface_data_seq_in_order.attr,
644 &dev_attr_iface_data_pdu_in_order.attr,
645 &dev_attr_iface_erl.attr,
646 &dev_attr_iface_max_recv_dlength.attr,
647 &dev_attr_iface_first_burst_len.attr,
648 &dev_attr_iface_max_outstanding_r2t.attr,
649 &dev_attr_iface_max_burst_len.attr,
650 &dev_attr_iface_chap_auth.attr,
651 &dev_attr_iface_bidi_chap.attr,
652 &dev_attr_iface_discovery_auth_optional.attr,
653 &dev_attr_iface_discovery_logout.attr,
654 &dev_attr_iface_strict_login_comp_en.attr,
655 &dev_attr_iface_initiator_name.attr,
656 &dev_attr_ipv4_iface_dhcp_dns_address_en.attr,
657 &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr,
658 &dev_attr_ipv4_iface_tos_en.attr,
659 &dev_attr_ipv4_iface_tos.attr,
660 &dev_attr_ipv4_iface_grat_arp_en.attr,
661 &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr,
662 &dev_attr_ipv4_iface_dhcp_alt_client_id.attr,
663 &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr,
664 &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr,
665 &dev_attr_ipv4_iface_dhcp_vendor_id.attr,
666 &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr,
667 &dev_attr_ipv4_iface_fragment_disable.attr,
668 &dev_attr_ipv4_iface_incoming_forwarding_en.attr,
669 &dev_attr_ipv4_iface_ttl.attr,
670 &dev_attr_ipv6_iface_link_local_state.attr,
671 &dev_attr_ipv6_iface_router_state.attr,
672 &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr,
673 &dev_attr_ipv6_iface_mld_en.attr,
674 &dev_attr_ipv6_iface_flow_label.attr,
675 &dev_attr_ipv6_iface_traffic_class.attr,
676 &dev_attr_ipv6_iface_hop_limit.attr,
677 &dev_attr_ipv6_iface_nd_reachable_tmo.attr,
678 &dev_attr_ipv6_iface_nd_rexmit_time.attr,
679 &dev_attr_ipv6_iface_nd_stale_tmo.attr,
680 &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr,
681 &dev_attr_ipv6_iface_router_adv_link_mtu.attr,
682 NULL,
685 static struct attribute_group iscsi_iface_group = {
686 .attrs = iscsi_iface_attrs,
687 .is_visible = iscsi_iface_attr_is_visible,
690 /* convert iscsi_ipaddress_state values to ascii string name */
691 static const struct {
692 enum iscsi_ipaddress_state value;
693 char *name;
694 } iscsi_ipaddress_state_names[] = {
695 {ISCSI_IPDDRESS_STATE_UNCONFIGURED, "Unconfigured" },
696 {ISCSI_IPDDRESS_STATE_ACQUIRING, "Acquiring" },
697 {ISCSI_IPDDRESS_STATE_TENTATIVE, "Tentative" },
698 {ISCSI_IPDDRESS_STATE_VALID, "Valid" },
699 {ISCSI_IPDDRESS_STATE_DISABLING, "Disabling" },
700 {ISCSI_IPDDRESS_STATE_INVALID, "Invalid" },
701 {ISCSI_IPDDRESS_STATE_DEPRECATED, "Deprecated" },
704 char *iscsi_get_ipaddress_state_name(enum iscsi_ipaddress_state port_state)
706 int i;
707 char *state = NULL;
709 for (i = 0; i < ARRAY_SIZE(iscsi_ipaddress_state_names); i++) {
710 if (iscsi_ipaddress_state_names[i].value == port_state) {
711 state = iscsi_ipaddress_state_names[i].name;
712 break;
715 return state;
717 EXPORT_SYMBOL_GPL(iscsi_get_ipaddress_state_name);
719 /* convert iscsi_router_state values to ascii string name */
720 static const struct {
721 enum iscsi_router_state value;
722 char *name;
723 } iscsi_router_state_names[] = {
724 {ISCSI_ROUTER_STATE_UNKNOWN, "Unknown" },
725 {ISCSI_ROUTER_STATE_ADVERTISED, "Advertised" },
726 {ISCSI_ROUTER_STATE_MANUAL, "Manual" },
727 {ISCSI_ROUTER_STATE_STALE, "Stale" },
730 char *iscsi_get_router_state_name(enum iscsi_router_state router_state)
732 int i;
733 char *state = NULL;
735 for (i = 0; i < ARRAY_SIZE(iscsi_router_state_names); i++) {
736 if (iscsi_router_state_names[i].value == router_state) {
737 state = iscsi_router_state_names[i].name;
738 break;
741 return state;
743 EXPORT_SYMBOL_GPL(iscsi_get_router_state_name);
745 struct iscsi_iface *
746 iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
747 uint32_t iface_type, uint32_t iface_num, int dd_size)
749 struct iscsi_iface *iface;
750 int err;
752 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
753 if (!iface)
754 return NULL;
756 iface->transport = transport;
757 iface->iface_type = iface_type;
758 iface->iface_num = iface_num;
759 iface->dev.release = iscsi_iface_release;
760 iface->dev.class = &iscsi_iface_class;
761 /* parent reference released in iscsi_iface_release */
762 iface->dev.parent = get_device(&shost->shost_gendev);
763 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
764 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
765 iface_num);
766 else
767 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
768 iface_num);
770 err = device_register(&iface->dev);
771 if (err)
772 goto free_iface;
774 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
775 if (err)
776 goto unreg_iface;
778 if (dd_size)
779 iface->dd_data = &iface[1];
780 return iface;
782 unreg_iface:
783 device_unregister(&iface->dev);
784 return NULL;
786 free_iface:
787 put_device(iface->dev.parent);
788 kfree(iface);
789 return NULL;
791 EXPORT_SYMBOL_GPL(iscsi_create_iface);
793 void iscsi_destroy_iface(struct iscsi_iface *iface)
795 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
796 device_unregister(&iface->dev);
798 EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
801 * Interface to display flash node params to sysfs
804 #define ISCSI_FLASHNODE_ATTR(_prefix, _name, _mode, _show, _store) \
805 struct device_attribute dev_attr_##_prefix##_##_name = \
806 __ATTR(_name, _mode, _show, _store)
808 /* flash node session attrs show */
809 #define iscsi_flashnode_sess_attr_show(type, name, param) \
810 static ssize_t \
811 show_##type##_##name(struct device *dev, struct device_attribute *attr, \
812 char *buf) \
814 struct iscsi_bus_flash_session *fnode_sess = \
815 iscsi_dev_to_flash_session(dev);\
816 struct iscsi_transport *t = fnode_sess->transport; \
817 return t->get_flashnode_param(fnode_sess, param, buf); \
821 #define iscsi_flashnode_sess_attr(type, name, param) \
822 iscsi_flashnode_sess_attr_show(type, name, param) \
823 static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
824 show_##type##_##name, NULL);
826 /* Flash node session attributes */
828 iscsi_flashnode_sess_attr(fnode, auto_snd_tgt_disable,
829 ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE);
830 iscsi_flashnode_sess_attr(fnode, discovery_session,
831 ISCSI_FLASHNODE_DISCOVERY_SESS);
832 iscsi_flashnode_sess_attr(fnode, portal_type, ISCSI_FLASHNODE_PORTAL_TYPE);
833 iscsi_flashnode_sess_attr(fnode, entry_enable, ISCSI_FLASHNODE_ENTRY_EN);
834 iscsi_flashnode_sess_attr(fnode, immediate_data, ISCSI_FLASHNODE_IMM_DATA_EN);
835 iscsi_flashnode_sess_attr(fnode, initial_r2t, ISCSI_FLASHNODE_INITIAL_R2T_EN);
836 iscsi_flashnode_sess_attr(fnode, data_seq_in_order,
837 ISCSI_FLASHNODE_DATASEQ_INORDER);
838 iscsi_flashnode_sess_attr(fnode, data_pdu_in_order,
839 ISCSI_FLASHNODE_PDU_INORDER);
840 iscsi_flashnode_sess_attr(fnode, chap_auth, ISCSI_FLASHNODE_CHAP_AUTH_EN);
841 iscsi_flashnode_sess_attr(fnode, discovery_logout,
842 ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN);
843 iscsi_flashnode_sess_attr(fnode, bidi_chap, ISCSI_FLASHNODE_BIDI_CHAP_EN);
844 iscsi_flashnode_sess_attr(fnode, discovery_auth_optional,
845 ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL);
846 iscsi_flashnode_sess_attr(fnode, erl, ISCSI_FLASHNODE_ERL);
847 iscsi_flashnode_sess_attr(fnode, first_burst_len, ISCSI_FLASHNODE_FIRST_BURST);
848 iscsi_flashnode_sess_attr(fnode, def_time2wait, ISCSI_FLASHNODE_DEF_TIME2WAIT);
849 iscsi_flashnode_sess_attr(fnode, def_time2retain,
850 ISCSI_FLASHNODE_DEF_TIME2RETAIN);
851 iscsi_flashnode_sess_attr(fnode, max_outstanding_r2t, ISCSI_FLASHNODE_MAX_R2T);
852 iscsi_flashnode_sess_attr(fnode, isid, ISCSI_FLASHNODE_ISID);
853 iscsi_flashnode_sess_attr(fnode, tsid, ISCSI_FLASHNODE_TSID);
854 iscsi_flashnode_sess_attr(fnode, max_burst_len, ISCSI_FLASHNODE_MAX_BURST);
855 iscsi_flashnode_sess_attr(fnode, def_taskmgmt_tmo,
856 ISCSI_FLASHNODE_DEF_TASKMGMT_TMO);
857 iscsi_flashnode_sess_attr(fnode, targetalias, ISCSI_FLASHNODE_ALIAS);
858 iscsi_flashnode_sess_attr(fnode, targetname, ISCSI_FLASHNODE_NAME);
859 iscsi_flashnode_sess_attr(fnode, tpgt, ISCSI_FLASHNODE_TPGT);
860 iscsi_flashnode_sess_attr(fnode, discovery_parent_idx,
861 ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX);
862 iscsi_flashnode_sess_attr(fnode, discovery_parent_type,
863 ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE);
864 iscsi_flashnode_sess_attr(fnode, chap_in_idx, ISCSI_FLASHNODE_CHAP_IN_IDX);
865 iscsi_flashnode_sess_attr(fnode, chap_out_idx, ISCSI_FLASHNODE_CHAP_OUT_IDX);
866 iscsi_flashnode_sess_attr(fnode, username, ISCSI_FLASHNODE_USERNAME);
867 iscsi_flashnode_sess_attr(fnode, username_in, ISCSI_FLASHNODE_USERNAME_IN);
868 iscsi_flashnode_sess_attr(fnode, password, ISCSI_FLASHNODE_PASSWORD);
869 iscsi_flashnode_sess_attr(fnode, password_in, ISCSI_FLASHNODE_PASSWORD_IN);
870 iscsi_flashnode_sess_attr(fnode, is_boot_target, ISCSI_FLASHNODE_IS_BOOT_TGT);
872 static struct attribute *iscsi_flashnode_sess_attrs[] = {
873 &dev_attr_fnode_auto_snd_tgt_disable.attr,
874 &dev_attr_fnode_discovery_session.attr,
875 &dev_attr_fnode_portal_type.attr,
876 &dev_attr_fnode_entry_enable.attr,
877 &dev_attr_fnode_immediate_data.attr,
878 &dev_attr_fnode_initial_r2t.attr,
879 &dev_attr_fnode_data_seq_in_order.attr,
880 &dev_attr_fnode_data_pdu_in_order.attr,
881 &dev_attr_fnode_chap_auth.attr,
882 &dev_attr_fnode_discovery_logout.attr,
883 &dev_attr_fnode_bidi_chap.attr,
884 &dev_attr_fnode_discovery_auth_optional.attr,
885 &dev_attr_fnode_erl.attr,
886 &dev_attr_fnode_first_burst_len.attr,
887 &dev_attr_fnode_def_time2wait.attr,
888 &dev_attr_fnode_def_time2retain.attr,
889 &dev_attr_fnode_max_outstanding_r2t.attr,
890 &dev_attr_fnode_isid.attr,
891 &dev_attr_fnode_tsid.attr,
892 &dev_attr_fnode_max_burst_len.attr,
893 &dev_attr_fnode_def_taskmgmt_tmo.attr,
894 &dev_attr_fnode_targetalias.attr,
895 &dev_attr_fnode_targetname.attr,
896 &dev_attr_fnode_tpgt.attr,
897 &dev_attr_fnode_discovery_parent_idx.attr,
898 &dev_attr_fnode_discovery_parent_type.attr,
899 &dev_attr_fnode_chap_in_idx.attr,
900 &dev_attr_fnode_chap_out_idx.attr,
901 &dev_attr_fnode_username.attr,
902 &dev_attr_fnode_username_in.attr,
903 &dev_attr_fnode_password.attr,
904 &dev_attr_fnode_password_in.attr,
905 &dev_attr_fnode_is_boot_target.attr,
906 NULL,
909 static umode_t iscsi_flashnode_sess_attr_is_visible(struct kobject *kobj,
910 struct attribute *attr,
911 int i)
913 struct device *dev = container_of(kobj, struct device, kobj);
914 struct iscsi_bus_flash_session *fnode_sess =
915 iscsi_dev_to_flash_session(dev);
916 struct iscsi_transport *t = fnode_sess->transport;
917 int param;
919 if (attr == &dev_attr_fnode_auto_snd_tgt_disable.attr) {
920 param = ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE;
921 } else if (attr == &dev_attr_fnode_discovery_session.attr) {
922 param = ISCSI_FLASHNODE_DISCOVERY_SESS;
923 } else if (attr == &dev_attr_fnode_portal_type.attr) {
924 param = ISCSI_FLASHNODE_PORTAL_TYPE;
925 } else if (attr == &dev_attr_fnode_entry_enable.attr) {
926 param = ISCSI_FLASHNODE_ENTRY_EN;
927 } else if (attr == &dev_attr_fnode_immediate_data.attr) {
928 param = ISCSI_FLASHNODE_IMM_DATA_EN;
929 } else if (attr == &dev_attr_fnode_initial_r2t.attr) {
930 param = ISCSI_FLASHNODE_INITIAL_R2T_EN;
931 } else if (attr == &dev_attr_fnode_data_seq_in_order.attr) {
932 param = ISCSI_FLASHNODE_DATASEQ_INORDER;
933 } else if (attr == &dev_attr_fnode_data_pdu_in_order.attr) {
934 param = ISCSI_FLASHNODE_PDU_INORDER;
935 } else if (attr == &dev_attr_fnode_chap_auth.attr) {
936 param = ISCSI_FLASHNODE_CHAP_AUTH_EN;
937 } else if (attr == &dev_attr_fnode_discovery_logout.attr) {
938 param = ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN;
939 } else if (attr == &dev_attr_fnode_bidi_chap.attr) {
940 param = ISCSI_FLASHNODE_BIDI_CHAP_EN;
941 } else if (attr == &dev_attr_fnode_discovery_auth_optional.attr) {
942 param = ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL;
943 } else if (attr == &dev_attr_fnode_erl.attr) {
944 param = ISCSI_FLASHNODE_ERL;
945 } else if (attr == &dev_attr_fnode_first_burst_len.attr) {
946 param = ISCSI_FLASHNODE_FIRST_BURST;
947 } else if (attr == &dev_attr_fnode_def_time2wait.attr) {
948 param = ISCSI_FLASHNODE_DEF_TIME2WAIT;
949 } else if (attr == &dev_attr_fnode_def_time2retain.attr) {
950 param = ISCSI_FLASHNODE_DEF_TIME2RETAIN;
951 } else if (attr == &dev_attr_fnode_max_outstanding_r2t.attr) {
952 param = ISCSI_FLASHNODE_MAX_R2T;
953 } else if (attr == &dev_attr_fnode_isid.attr) {
954 param = ISCSI_FLASHNODE_ISID;
955 } else if (attr == &dev_attr_fnode_tsid.attr) {
956 param = ISCSI_FLASHNODE_TSID;
957 } else if (attr == &dev_attr_fnode_max_burst_len.attr) {
958 param = ISCSI_FLASHNODE_MAX_BURST;
959 } else if (attr == &dev_attr_fnode_def_taskmgmt_tmo.attr) {
960 param = ISCSI_FLASHNODE_DEF_TASKMGMT_TMO;
961 } else if (attr == &dev_attr_fnode_targetalias.attr) {
962 param = ISCSI_FLASHNODE_ALIAS;
963 } else if (attr == &dev_attr_fnode_targetname.attr) {
964 param = ISCSI_FLASHNODE_NAME;
965 } else if (attr == &dev_attr_fnode_tpgt.attr) {
966 param = ISCSI_FLASHNODE_TPGT;
967 } else if (attr == &dev_attr_fnode_discovery_parent_idx.attr) {
968 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX;
969 } else if (attr == &dev_attr_fnode_discovery_parent_type.attr) {
970 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE;
971 } else if (attr == &dev_attr_fnode_chap_in_idx.attr) {
972 param = ISCSI_FLASHNODE_CHAP_IN_IDX;
973 } else if (attr == &dev_attr_fnode_chap_out_idx.attr) {
974 param = ISCSI_FLASHNODE_CHAP_OUT_IDX;
975 } else if (attr == &dev_attr_fnode_username.attr) {
976 param = ISCSI_FLASHNODE_USERNAME;
977 } else if (attr == &dev_attr_fnode_username_in.attr) {
978 param = ISCSI_FLASHNODE_USERNAME_IN;
979 } else if (attr == &dev_attr_fnode_password.attr) {
980 param = ISCSI_FLASHNODE_PASSWORD;
981 } else if (attr == &dev_attr_fnode_password_in.attr) {
982 param = ISCSI_FLASHNODE_PASSWORD_IN;
983 } else if (attr == &dev_attr_fnode_is_boot_target.attr) {
984 param = ISCSI_FLASHNODE_IS_BOOT_TGT;
985 } else {
986 WARN_ONCE(1, "Invalid flashnode session attr");
987 return 0;
990 return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
993 static struct attribute_group iscsi_flashnode_sess_attr_group = {
994 .attrs = iscsi_flashnode_sess_attrs,
995 .is_visible = iscsi_flashnode_sess_attr_is_visible,
998 static const struct attribute_group *iscsi_flashnode_sess_attr_groups[] = {
999 &iscsi_flashnode_sess_attr_group,
1000 NULL,
1003 static void iscsi_flashnode_sess_release(struct device *dev)
1005 struct iscsi_bus_flash_session *fnode_sess =
1006 iscsi_dev_to_flash_session(dev);
1008 kfree(fnode_sess->targetname);
1009 kfree(fnode_sess->targetalias);
1010 kfree(fnode_sess->portal_type);
1011 kfree(fnode_sess);
1014 struct device_type iscsi_flashnode_sess_dev_type = {
1015 .name = "iscsi_flashnode_sess_dev_type",
1016 .groups = iscsi_flashnode_sess_attr_groups,
1017 .release = iscsi_flashnode_sess_release,
1020 /* flash node connection attrs show */
1021 #define iscsi_flashnode_conn_attr_show(type, name, param) \
1022 static ssize_t \
1023 show_##type##_##name(struct device *dev, struct device_attribute *attr, \
1024 char *buf) \
1026 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);\
1027 struct iscsi_bus_flash_session *fnode_sess = \
1028 iscsi_flash_conn_to_flash_session(fnode_conn);\
1029 struct iscsi_transport *t = fnode_conn->transport; \
1030 return t->get_flashnode_param(fnode_sess, param, buf); \
1034 #define iscsi_flashnode_conn_attr(type, name, param) \
1035 iscsi_flashnode_conn_attr_show(type, name, param) \
1036 static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
1037 show_##type##_##name, NULL);
1039 /* Flash node connection attributes */
1041 iscsi_flashnode_conn_attr(fnode, is_fw_assigned_ipv6,
1042 ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6);
1043 iscsi_flashnode_conn_attr(fnode, header_digest, ISCSI_FLASHNODE_HDR_DGST_EN);
1044 iscsi_flashnode_conn_attr(fnode, data_digest, ISCSI_FLASHNODE_DATA_DGST_EN);
1045 iscsi_flashnode_conn_attr(fnode, snack_req, ISCSI_FLASHNODE_SNACK_REQ_EN);
1046 iscsi_flashnode_conn_attr(fnode, tcp_timestamp_stat,
1047 ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT);
1048 iscsi_flashnode_conn_attr(fnode, tcp_nagle_disable,
1049 ISCSI_FLASHNODE_TCP_NAGLE_DISABLE);
1050 iscsi_flashnode_conn_attr(fnode, tcp_wsf_disable,
1051 ISCSI_FLASHNODE_TCP_WSF_DISABLE);
1052 iscsi_flashnode_conn_attr(fnode, tcp_timer_scale,
1053 ISCSI_FLASHNODE_TCP_TIMER_SCALE);
1054 iscsi_flashnode_conn_attr(fnode, tcp_timestamp_enable,
1055 ISCSI_FLASHNODE_TCP_TIMESTAMP_EN);
1056 iscsi_flashnode_conn_attr(fnode, fragment_disable,
1057 ISCSI_FLASHNODE_IP_FRAG_DISABLE);
1058 iscsi_flashnode_conn_attr(fnode, keepalive_tmo, ISCSI_FLASHNODE_KEEPALIVE_TMO);
1059 iscsi_flashnode_conn_attr(fnode, port, ISCSI_FLASHNODE_PORT);
1060 iscsi_flashnode_conn_attr(fnode, ipaddress, ISCSI_FLASHNODE_IPADDR);
1061 iscsi_flashnode_conn_attr(fnode, max_recv_dlength,
1062 ISCSI_FLASHNODE_MAX_RECV_DLENGTH);
1063 iscsi_flashnode_conn_attr(fnode, max_xmit_dlength,
1064 ISCSI_FLASHNODE_MAX_XMIT_DLENGTH);
1065 iscsi_flashnode_conn_attr(fnode, local_port, ISCSI_FLASHNODE_LOCAL_PORT);
1066 iscsi_flashnode_conn_attr(fnode, ipv4_tos, ISCSI_FLASHNODE_IPV4_TOS);
1067 iscsi_flashnode_conn_attr(fnode, ipv6_traffic_class, ISCSI_FLASHNODE_IPV6_TC);
1068 iscsi_flashnode_conn_attr(fnode, ipv6_flow_label,
1069 ISCSI_FLASHNODE_IPV6_FLOW_LABEL);
1070 iscsi_flashnode_conn_attr(fnode, redirect_ipaddr,
1071 ISCSI_FLASHNODE_REDIRECT_IPADDR);
1072 iscsi_flashnode_conn_attr(fnode, max_segment_size,
1073 ISCSI_FLASHNODE_MAX_SEGMENT_SIZE);
1074 iscsi_flashnode_conn_attr(fnode, link_local_ipv6,
1075 ISCSI_FLASHNODE_LINK_LOCAL_IPV6);
1076 iscsi_flashnode_conn_attr(fnode, tcp_xmit_wsf, ISCSI_FLASHNODE_TCP_XMIT_WSF);
1077 iscsi_flashnode_conn_attr(fnode, tcp_recv_wsf, ISCSI_FLASHNODE_TCP_RECV_WSF);
1078 iscsi_flashnode_conn_attr(fnode, statsn, ISCSI_FLASHNODE_STATSN);
1079 iscsi_flashnode_conn_attr(fnode, exp_statsn, ISCSI_FLASHNODE_EXP_STATSN);
1081 static struct attribute *iscsi_flashnode_conn_attrs[] = {
1082 &dev_attr_fnode_is_fw_assigned_ipv6.attr,
1083 &dev_attr_fnode_header_digest.attr,
1084 &dev_attr_fnode_data_digest.attr,
1085 &dev_attr_fnode_snack_req.attr,
1086 &dev_attr_fnode_tcp_timestamp_stat.attr,
1087 &dev_attr_fnode_tcp_nagle_disable.attr,
1088 &dev_attr_fnode_tcp_wsf_disable.attr,
1089 &dev_attr_fnode_tcp_timer_scale.attr,
1090 &dev_attr_fnode_tcp_timestamp_enable.attr,
1091 &dev_attr_fnode_fragment_disable.attr,
1092 &dev_attr_fnode_max_recv_dlength.attr,
1093 &dev_attr_fnode_max_xmit_dlength.attr,
1094 &dev_attr_fnode_keepalive_tmo.attr,
1095 &dev_attr_fnode_port.attr,
1096 &dev_attr_fnode_ipaddress.attr,
1097 &dev_attr_fnode_redirect_ipaddr.attr,
1098 &dev_attr_fnode_max_segment_size.attr,
1099 &dev_attr_fnode_local_port.attr,
1100 &dev_attr_fnode_ipv4_tos.attr,
1101 &dev_attr_fnode_ipv6_traffic_class.attr,
1102 &dev_attr_fnode_ipv6_flow_label.attr,
1103 &dev_attr_fnode_link_local_ipv6.attr,
1104 &dev_attr_fnode_tcp_xmit_wsf.attr,
1105 &dev_attr_fnode_tcp_recv_wsf.attr,
1106 &dev_attr_fnode_statsn.attr,
1107 &dev_attr_fnode_exp_statsn.attr,
1108 NULL,
1111 static umode_t iscsi_flashnode_conn_attr_is_visible(struct kobject *kobj,
1112 struct attribute *attr,
1113 int i)
1115 struct device *dev = container_of(kobj, struct device, kobj);
1116 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
1117 struct iscsi_transport *t = fnode_conn->transport;
1118 int param;
1120 if (attr == &dev_attr_fnode_is_fw_assigned_ipv6.attr) {
1121 param = ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6;
1122 } else if (attr == &dev_attr_fnode_header_digest.attr) {
1123 param = ISCSI_FLASHNODE_HDR_DGST_EN;
1124 } else if (attr == &dev_attr_fnode_data_digest.attr) {
1125 param = ISCSI_FLASHNODE_DATA_DGST_EN;
1126 } else if (attr == &dev_attr_fnode_snack_req.attr) {
1127 param = ISCSI_FLASHNODE_SNACK_REQ_EN;
1128 } else if (attr == &dev_attr_fnode_tcp_timestamp_stat.attr) {
1129 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT;
1130 } else if (attr == &dev_attr_fnode_tcp_nagle_disable.attr) {
1131 param = ISCSI_FLASHNODE_TCP_NAGLE_DISABLE;
1132 } else if (attr == &dev_attr_fnode_tcp_wsf_disable.attr) {
1133 param = ISCSI_FLASHNODE_TCP_WSF_DISABLE;
1134 } else if (attr == &dev_attr_fnode_tcp_timer_scale.attr) {
1135 param = ISCSI_FLASHNODE_TCP_TIMER_SCALE;
1136 } else if (attr == &dev_attr_fnode_tcp_timestamp_enable.attr) {
1137 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_EN;
1138 } else if (attr == &dev_attr_fnode_fragment_disable.attr) {
1139 param = ISCSI_FLASHNODE_IP_FRAG_DISABLE;
1140 } else if (attr == &dev_attr_fnode_max_recv_dlength.attr) {
1141 param = ISCSI_FLASHNODE_MAX_RECV_DLENGTH;
1142 } else if (attr == &dev_attr_fnode_max_xmit_dlength.attr) {
1143 param = ISCSI_FLASHNODE_MAX_XMIT_DLENGTH;
1144 } else if (attr == &dev_attr_fnode_keepalive_tmo.attr) {
1145 param = ISCSI_FLASHNODE_KEEPALIVE_TMO;
1146 } else if (attr == &dev_attr_fnode_port.attr) {
1147 param = ISCSI_FLASHNODE_PORT;
1148 } else if (attr == &dev_attr_fnode_ipaddress.attr) {
1149 param = ISCSI_FLASHNODE_IPADDR;
1150 } else if (attr == &dev_attr_fnode_redirect_ipaddr.attr) {
1151 param = ISCSI_FLASHNODE_REDIRECT_IPADDR;
1152 } else if (attr == &dev_attr_fnode_max_segment_size.attr) {
1153 param = ISCSI_FLASHNODE_MAX_SEGMENT_SIZE;
1154 } else if (attr == &dev_attr_fnode_local_port.attr) {
1155 param = ISCSI_FLASHNODE_LOCAL_PORT;
1156 } else if (attr == &dev_attr_fnode_ipv4_tos.attr) {
1157 param = ISCSI_FLASHNODE_IPV4_TOS;
1158 } else if (attr == &dev_attr_fnode_ipv6_traffic_class.attr) {
1159 param = ISCSI_FLASHNODE_IPV6_TC;
1160 } else if (attr == &dev_attr_fnode_ipv6_flow_label.attr) {
1161 param = ISCSI_FLASHNODE_IPV6_FLOW_LABEL;
1162 } else if (attr == &dev_attr_fnode_link_local_ipv6.attr) {
1163 param = ISCSI_FLASHNODE_LINK_LOCAL_IPV6;
1164 } else if (attr == &dev_attr_fnode_tcp_xmit_wsf.attr) {
1165 param = ISCSI_FLASHNODE_TCP_XMIT_WSF;
1166 } else if (attr == &dev_attr_fnode_tcp_recv_wsf.attr) {
1167 param = ISCSI_FLASHNODE_TCP_RECV_WSF;
1168 } else if (attr == &dev_attr_fnode_statsn.attr) {
1169 param = ISCSI_FLASHNODE_STATSN;
1170 } else if (attr == &dev_attr_fnode_exp_statsn.attr) {
1171 param = ISCSI_FLASHNODE_EXP_STATSN;
1172 } else {
1173 WARN_ONCE(1, "Invalid flashnode connection attr");
1174 return 0;
1177 return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
1180 static struct attribute_group iscsi_flashnode_conn_attr_group = {
1181 .attrs = iscsi_flashnode_conn_attrs,
1182 .is_visible = iscsi_flashnode_conn_attr_is_visible,
1185 static const struct attribute_group *iscsi_flashnode_conn_attr_groups[] = {
1186 &iscsi_flashnode_conn_attr_group,
1187 NULL,
1190 static void iscsi_flashnode_conn_release(struct device *dev)
1192 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
1194 kfree(fnode_conn->ipaddress);
1195 kfree(fnode_conn->redirect_ipaddr);
1196 kfree(fnode_conn->link_local_ipv6_addr);
1197 kfree(fnode_conn);
1200 struct device_type iscsi_flashnode_conn_dev_type = {
1201 .name = "iscsi_flashnode_conn_dev_type",
1202 .groups = iscsi_flashnode_conn_attr_groups,
1203 .release = iscsi_flashnode_conn_release,
1206 struct bus_type iscsi_flashnode_bus;
1208 int iscsi_flashnode_bus_match(struct device *dev,
1209 struct device_driver *drv)
1211 if (dev->bus == &iscsi_flashnode_bus)
1212 return 1;
1213 return 0;
1215 EXPORT_SYMBOL_GPL(iscsi_flashnode_bus_match);
1217 struct bus_type iscsi_flashnode_bus = {
1218 .name = "iscsi_flashnode",
1219 .match = &iscsi_flashnode_bus_match,
1223 * iscsi_create_flashnode_sess - Add flashnode session entry in sysfs
1224 * @shost: pointer to host data
1225 * @index: index of flashnode to add in sysfs
1226 * @transport: pointer to transport data
1227 * @dd_size: total size to allocate
1229 * Adds a sysfs entry for the flashnode session attributes
1231 * Returns:
1232 * pointer to allocated flashnode sess on success
1233 * %NULL on failure
1235 struct iscsi_bus_flash_session *
1236 iscsi_create_flashnode_sess(struct Scsi_Host *shost, int index,
1237 struct iscsi_transport *transport,
1238 int dd_size)
1240 struct iscsi_bus_flash_session *fnode_sess;
1241 int err;
1243 fnode_sess = kzalloc(sizeof(*fnode_sess) + dd_size, GFP_KERNEL);
1244 if (!fnode_sess)
1245 return NULL;
1247 fnode_sess->transport = transport;
1248 fnode_sess->target_id = index;
1249 fnode_sess->dev.type = &iscsi_flashnode_sess_dev_type;
1250 fnode_sess->dev.bus = &iscsi_flashnode_bus;
1251 fnode_sess->dev.parent = &shost->shost_gendev;
1252 dev_set_name(&fnode_sess->dev, "flashnode_sess-%u:%u",
1253 shost->host_no, index);
1255 err = device_register(&fnode_sess->dev);
1256 if (err)
1257 goto free_fnode_sess;
1259 if (dd_size)
1260 fnode_sess->dd_data = &fnode_sess[1];
1262 return fnode_sess;
1264 free_fnode_sess:
1265 kfree(fnode_sess);
1266 return NULL;
1268 EXPORT_SYMBOL_GPL(iscsi_create_flashnode_sess);
1271 * iscsi_create_flashnode_conn - Add flashnode conn entry in sysfs
1272 * @shost: pointer to host data
1273 * @fnode_sess: pointer to the parent flashnode session entry
1274 * @transport: pointer to transport data
1275 * @dd_size: total size to allocate
1277 * Adds a sysfs entry for the flashnode connection attributes
1279 * Returns:
1280 * pointer to allocated flashnode conn on success
1281 * %NULL on failure
1283 struct iscsi_bus_flash_conn *
1284 iscsi_create_flashnode_conn(struct Scsi_Host *shost,
1285 struct iscsi_bus_flash_session *fnode_sess,
1286 struct iscsi_transport *transport,
1287 int dd_size)
1289 struct iscsi_bus_flash_conn *fnode_conn;
1290 int err;
1292 fnode_conn = kzalloc(sizeof(*fnode_conn) + dd_size, GFP_KERNEL);
1293 if (!fnode_conn)
1294 return NULL;
1296 fnode_conn->transport = transport;
1297 fnode_conn->dev.type = &iscsi_flashnode_conn_dev_type;
1298 fnode_conn->dev.bus = &iscsi_flashnode_bus;
1299 fnode_conn->dev.parent = &fnode_sess->dev;
1300 dev_set_name(&fnode_conn->dev, "flashnode_conn-%u:%u:0",
1301 shost->host_no, fnode_sess->target_id);
1303 err = device_register(&fnode_conn->dev);
1304 if (err)
1305 goto free_fnode_conn;
1307 if (dd_size)
1308 fnode_conn->dd_data = &fnode_conn[1];
1310 return fnode_conn;
1312 free_fnode_conn:
1313 kfree(fnode_conn);
1314 return NULL;
1316 EXPORT_SYMBOL_GPL(iscsi_create_flashnode_conn);
1319 * iscsi_is_flashnode_conn_dev - verify passed device is to be flashnode conn
1320 * @dev: device to verify
1321 * @data: pointer to data containing value to use for verification
1323 * Verifies if the passed device is flashnode conn device
1325 * Returns:
1326 * 1 on success
1327 * 0 on failure
1329 int iscsi_is_flashnode_conn_dev(struct device *dev, void *data)
1331 return dev->bus == &iscsi_flashnode_bus;
1333 EXPORT_SYMBOL_GPL(iscsi_is_flashnode_conn_dev);
1335 static int iscsi_destroy_flashnode_conn(struct iscsi_bus_flash_conn *fnode_conn)
1337 device_unregister(&fnode_conn->dev);
1338 return 0;
1341 static int flashnode_match_index(struct device *dev, void *data)
1343 struct iscsi_bus_flash_session *fnode_sess = NULL;
1344 int ret = 0;
1346 if (!iscsi_flashnode_bus_match(dev, NULL))
1347 goto exit_match_index;
1349 fnode_sess = iscsi_dev_to_flash_session(dev);
1350 ret = (fnode_sess->target_id == *((int *)data)) ? 1 : 0;
1352 exit_match_index:
1353 return ret;
1357 * iscsi_get_flashnode_by_index -finds flashnode session entry by index
1358 * @shost: pointer to host data
1359 * @idx: index to match
1361 * Finds the flashnode session object for the passed index
1363 * Returns:
1364 * pointer to found flashnode session object on success
1365 * %NULL on failure
1367 static struct iscsi_bus_flash_session *
1368 iscsi_get_flashnode_by_index(struct Scsi_Host *shost, uint32_t idx)
1370 struct iscsi_bus_flash_session *fnode_sess = NULL;
1371 struct device *dev;
1373 dev = device_find_child(&shost->shost_gendev, &idx,
1374 flashnode_match_index);
1375 if (dev)
1376 fnode_sess = iscsi_dev_to_flash_session(dev);
1378 return fnode_sess;
1382 * iscsi_find_flashnode_sess - finds flashnode session entry
1383 * @shost: pointer to host data
1384 * @data: pointer to data containing value to use for comparison
1385 * @fn: function pointer that does actual comparison
1387 * Finds the flashnode session object comparing the data passed using logic
1388 * defined in passed function pointer
1390 * Returns:
1391 * pointer to found flashnode session device object on success
1392 * %NULL on failure
1394 struct device *
1395 iscsi_find_flashnode_sess(struct Scsi_Host *shost, void *data,
1396 int (*fn)(struct device *dev, void *data))
1398 return device_find_child(&shost->shost_gendev, data, fn);
1400 EXPORT_SYMBOL_GPL(iscsi_find_flashnode_sess);
1403 * iscsi_find_flashnode_conn - finds flashnode connection entry
1404 * @fnode_sess: pointer to parent flashnode session entry
1406 * Finds the flashnode connection object comparing the data passed using logic
1407 * defined in passed function pointer
1409 * Returns:
1410 * pointer to found flashnode connection device object on success
1411 * %NULL on failure
1413 struct device *
1414 iscsi_find_flashnode_conn(struct iscsi_bus_flash_session *fnode_sess)
1416 return device_find_child(&fnode_sess->dev, NULL,
1417 iscsi_is_flashnode_conn_dev);
1419 EXPORT_SYMBOL_GPL(iscsi_find_flashnode_conn);
1421 static int iscsi_iter_destroy_flashnode_conn_fn(struct device *dev, void *data)
1423 if (!iscsi_is_flashnode_conn_dev(dev, NULL))
1424 return 0;
1426 return iscsi_destroy_flashnode_conn(iscsi_dev_to_flash_conn(dev));
1430 * iscsi_destroy_flashnode_sess - destroy flashnode session entry
1431 * @fnode_sess: pointer to flashnode session entry to be destroyed
1433 * Deletes the flashnode session entry and all children flashnode connection
1434 * entries from sysfs
1436 void iscsi_destroy_flashnode_sess(struct iscsi_bus_flash_session *fnode_sess)
1438 int err;
1440 err = device_for_each_child(&fnode_sess->dev, NULL,
1441 iscsi_iter_destroy_flashnode_conn_fn);
1442 if (err)
1443 pr_err("Could not delete all connections for %s. Error %d.\n",
1444 fnode_sess->dev.kobj.name, err);
1446 device_unregister(&fnode_sess->dev);
1448 EXPORT_SYMBOL_GPL(iscsi_destroy_flashnode_sess);
1450 static int iscsi_iter_destroy_flashnode_fn(struct device *dev, void *data)
1452 if (!iscsi_flashnode_bus_match(dev, NULL))
1453 return 0;
1455 iscsi_destroy_flashnode_sess(iscsi_dev_to_flash_session(dev));
1456 return 0;
1460 * iscsi_destroy_all_flashnode - destroy all flashnode session entries
1461 * @shost: pointer to host data
1463 * Destroys all the flashnode session entries and all corresponding children
1464 * flashnode connection entries from sysfs
1466 void iscsi_destroy_all_flashnode(struct Scsi_Host *shost)
1468 device_for_each_child(&shost->shost_gendev, NULL,
1469 iscsi_iter_destroy_flashnode_fn);
1471 EXPORT_SYMBOL_GPL(iscsi_destroy_all_flashnode);
1474 * BSG support
1477 * iscsi_bsg_host_dispatch - Dispatch command to LLD.
1478 * @job: bsg job to be processed
1480 static int iscsi_bsg_host_dispatch(struct bsg_job *job)
1482 struct Scsi_Host *shost = iscsi_job_to_shost(job);
1483 struct iscsi_bsg_request *req = job->request;
1484 struct iscsi_bsg_reply *reply = job->reply;
1485 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1486 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
1487 int ret;
1489 /* check if we have the msgcode value at least */
1490 if (job->request_len < sizeof(uint32_t)) {
1491 ret = -ENOMSG;
1492 goto fail_host_msg;
1495 /* Validate the host command */
1496 switch (req->msgcode) {
1497 case ISCSI_BSG_HST_VENDOR:
1498 cmdlen += sizeof(struct iscsi_bsg_host_vendor);
1499 if ((shost->hostt->vendor_id == 0L) ||
1500 (req->rqst_data.h_vendor.vendor_id !=
1501 shost->hostt->vendor_id)) {
1502 ret = -ESRCH;
1503 goto fail_host_msg;
1505 break;
1506 default:
1507 ret = -EBADR;
1508 goto fail_host_msg;
1511 /* check if we really have all the request data needed */
1512 if (job->request_len < cmdlen) {
1513 ret = -ENOMSG;
1514 goto fail_host_msg;
1517 ret = i->iscsi_transport->bsg_request(job);
1518 if (!ret)
1519 return 0;
1521 fail_host_msg:
1522 /* return the errno failure code as the only status */
1523 BUG_ON(job->reply_len < sizeof(uint32_t));
1524 reply->reply_payload_rcv_len = 0;
1525 reply->result = ret;
1526 job->reply_len = sizeof(uint32_t);
1527 bsg_job_done(job, ret, 0);
1528 return 0;
1532 * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
1533 * @shost: shost for iscsi_host
1534 * @ihost: iscsi_cls_host adding the structures to
1536 static int
1537 iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
1539 struct device *dev = &shost->shost_gendev;
1540 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1541 struct request_queue *q;
1542 char bsg_name[20];
1543 int ret;
1545 if (!i->iscsi_transport->bsg_request)
1546 return -ENOTSUPP;
1548 snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
1550 q = __scsi_alloc_queue(shost, bsg_request_fn);
1551 if (!q)
1552 return -ENOMEM;
1554 ret = bsg_setup_queue(dev, q, bsg_name, iscsi_bsg_host_dispatch, 0);
1555 if (ret) {
1556 shost_printk(KERN_ERR, shost, "bsg interface failed to "
1557 "initialize - no request queue\n");
1558 blk_cleanup_queue(q);
1559 return ret;
1562 ihost->bsg_q = q;
1563 return 0;
1566 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
1567 struct device *cdev)
1569 struct Scsi_Host *shost = dev_to_shost(dev);
1570 struct iscsi_cls_host *ihost = shost->shost_data;
1572 memset(ihost, 0, sizeof(*ihost));
1573 atomic_set(&ihost->nr_scans, 0);
1574 mutex_init(&ihost->mutex);
1576 iscsi_bsg_host_add(shost, ihost);
1577 /* ignore any bsg add error - we just can't do sgio */
1579 return 0;
1582 static int iscsi_remove_host(struct transport_container *tc,
1583 struct device *dev, struct device *cdev)
1585 struct Scsi_Host *shost = dev_to_shost(dev);
1586 struct iscsi_cls_host *ihost = shost->shost_data;
1588 if (ihost->bsg_q) {
1589 bsg_unregister_queue(ihost->bsg_q);
1590 blk_cleanup_queue(ihost->bsg_q);
1592 return 0;
1595 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
1596 "iscsi_host",
1597 iscsi_setup_host,
1598 iscsi_remove_host,
1599 NULL);
1601 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
1602 "iscsi_session",
1603 NULL,
1604 NULL,
1605 NULL);
1607 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
1608 "iscsi_connection",
1609 NULL,
1610 NULL,
1611 NULL);
1613 static struct sock *nls;
1614 static DEFINE_MUTEX(rx_queue_mutex);
1616 static LIST_HEAD(sesslist);
1617 static DEFINE_SPINLOCK(sesslock);
1618 static LIST_HEAD(connlist);
1619 static DEFINE_SPINLOCK(connlock);
1621 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
1623 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
1624 return sess->sid;
1628 * Returns the matching session to a given sid
1630 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
1632 unsigned long flags;
1633 struct iscsi_cls_session *sess;
1635 spin_lock_irqsave(&sesslock, flags);
1636 list_for_each_entry(sess, &sesslist, sess_list) {
1637 if (sess->sid == sid) {
1638 spin_unlock_irqrestore(&sesslock, flags);
1639 return sess;
1642 spin_unlock_irqrestore(&sesslock, flags);
1643 return NULL;
1647 * Returns the matching connection to a given sid / cid tuple
1649 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
1651 unsigned long flags;
1652 struct iscsi_cls_conn *conn;
1654 spin_lock_irqsave(&connlock, flags);
1655 list_for_each_entry(conn, &connlist, conn_list) {
1656 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
1657 spin_unlock_irqrestore(&connlock, flags);
1658 return conn;
1661 spin_unlock_irqrestore(&connlock, flags);
1662 return NULL;
1666 * The following functions can be used by LLDs that allocate
1667 * their own scsi_hosts or by software iscsi LLDs
1669 static struct {
1670 int value;
1671 char *name;
1672 } iscsi_session_state_names[] = {
1673 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
1674 { ISCSI_SESSION_FAILED, "FAILED" },
1675 { ISCSI_SESSION_FREE, "FREE" },
1678 static const char *iscsi_session_state_name(int state)
1680 int i;
1681 char *name = NULL;
1683 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
1684 if (iscsi_session_state_names[i].value == state) {
1685 name = iscsi_session_state_names[i].name;
1686 break;
1689 return name;
1692 int iscsi_session_chkready(struct iscsi_cls_session *session)
1694 unsigned long flags;
1695 int err;
1697 spin_lock_irqsave(&session->lock, flags);
1698 switch (session->state) {
1699 case ISCSI_SESSION_LOGGED_IN:
1700 err = 0;
1701 break;
1702 case ISCSI_SESSION_FAILED:
1703 err = DID_IMM_RETRY << 16;
1704 break;
1705 case ISCSI_SESSION_FREE:
1706 err = DID_TRANSPORT_FAILFAST << 16;
1707 break;
1708 default:
1709 err = DID_NO_CONNECT << 16;
1710 break;
1712 spin_unlock_irqrestore(&session->lock, flags);
1713 return err;
1715 EXPORT_SYMBOL_GPL(iscsi_session_chkready);
1717 int iscsi_is_session_online(struct iscsi_cls_session *session)
1719 unsigned long flags;
1720 int ret = 0;
1722 spin_lock_irqsave(&session->lock, flags);
1723 if (session->state == ISCSI_SESSION_LOGGED_IN)
1724 ret = 1;
1725 spin_unlock_irqrestore(&session->lock, flags);
1726 return ret;
1728 EXPORT_SYMBOL_GPL(iscsi_is_session_online);
1730 static void iscsi_session_release(struct device *dev)
1732 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
1733 struct Scsi_Host *shost;
1735 shost = iscsi_session_to_shost(session);
1736 scsi_host_put(shost);
1737 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
1738 kfree(session);
1741 int iscsi_is_session_dev(const struct device *dev)
1743 return dev->release == iscsi_session_release;
1745 EXPORT_SYMBOL_GPL(iscsi_is_session_dev);
1747 static int iscsi_iter_session_fn(struct device *dev, void *data)
1749 void (* fn) (struct iscsi_cls_session *) = data;
1751 if (!iscsi_is_session_dev(dev))
1752 return 0;
1753 fn(iscsi_dev_to_session(dev));
1754 return 0;
1757 void iscsi_host_for_each_session(struct Scsi_Host *shost,
1758 void (*fn)(struct iscsi_cls_session *))
1760 device_for_each_child(&shost->shost_gendev, fn,
1761 iscsi_iter_session_fn);
1763 EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
1766 * iscsi_scan_finished - helper to report when running scans are done
1767 * @shost: scsi host
1768 * @time: scan run time
1770 * This function can be used by drives like qla4xxx to report to the scsi
1771 * layer when the scans it kicked off at module load time are done.
1773 int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
1775 struct iscsi_cls_host *ihost = shost->shost_data;
1777 * qla4xxx will have kicked off some session unblocks before calling
1778 * scsi_scan_host, so just wait for them to complete.
1780 return !atomic_read(&ihost->nr_scans);
1782 EXPORT_SYMBOL_GPL(iscsi_scan_finished);
1784 struct iscsi_scan_data {
1785 unsigned int channel;
1786 unsigned int id;
1787 u64 lun;
1790 static int iscsi_user_scan_session(struct device *dev, void *data)
1792 struct iscsi_scan_data *scan_data = data;
1793 struct iscsi_cls_session *session;
1794 struct Scsi_Host *shost;
1795 struct iscsi_cls_host *ihost;
1796 unsigned long flags;
1797 unsigned int id;
1799 if (!iscsi_is_session_dev(dev))
1800 return 0;
1802 session = iscsi_dev_to_session(dev);
1804 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
1806 shost = iscsi_session_to_shost(session);
1807 ihost = shost->shost_data;
1809 mutex_lock(&ihost->mutex);
1810 spin_lock_irqsave(&session->lock, flags);
1811 if (session->state != ISCSI_SESSION_LOGGED_IN) {
1812 spin_unlock_irqrestore(&session->lock, flags);
1813 goto user_scan_exit;
1815 id = session->target_id;
1816 spin_unlock_irqrestore(&session->lock, flags);
1818 if (id != ISCSI_MAX_TARGET) {
1819 if ((scan_data->channel == SCAN_WILD_CARD ||
1820 scan_data->channel == 0) &&
1821 (scan_data->id == SCAN_WILD_CARD ||
1822 scan_data->id == id))
1823 scsi_scan_target(&session->dev, 0, id,
1824 scan_data->lun, 1);
1827 user_scan_exit:
1828 mutex_unlock(&ihost->mutex);
1829 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
1830 return 0;
1833 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
1834 uint id, u64 lun)
1836 struct iscsi_scan_data scan_data;
1838 scan_data.channel = channel;
1839 scan_data.id = id;
1840 scan_data.lun = lun;
1842 return device_for_each_child(&shost->shost_gendev, &scan_data,
1843 iscsi_user_scan_session);
1846 static void iscsi_scan_session(struct work_struct *work)
1848 struct iscsi_cls_session *session =
1849 container_of(work, struct iscsi_cls_session, scan_work);
1850 struct Scsi_Host *shost = iscsi_session_to_shost(session);
1851 struct iscsi_cls_host *ihost = shost->shost_data;
1852 struct iscsi_scan_data scan_data;
1854 scan_data.channel = 0;
1855 scan_data.id = SCAN_WILD_CARD;
1856 scan_data.lun = SCAN_WILD_CARD;
1858 iscsi_user_scan_session(&session->dev, &scan_data);
1859 atomic_dec(&ihost->nr_scans);
1863 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
1864 * @cmd: scsi cmd passed to scsi eh handler
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.
1871 int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
1873 struct iscsi_cls_session *session =
1874 starget_to_session(scsi_target(cmd->device));
1875 unsigned long flags;
1876 int ret = 0;
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;
1884 spin_unlock_irqrestore(&session->lock, flags);
1885 msleep(1000);
1886 spin_lock_irqsave(&session->lock, flags);
1888 spin_unlock_irqrestore(&session->lock, flags);
1889 return ret;
1891 EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
1893 static void session_recovery_timedout(struct work_struct *work)
1895 struct iscsi_cls_session *session =
1896 container_of(work, struct iscsi_cls_session,
1897 recovery_work.work);
1898 unsigned long flags;
1900 iscsi_cls_session_printk(KERN_INFO, session,
1901 "session recovery timed out after %d secs\n",
1902 session->recovery_tmo);
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;
1915 spin_unlock_irqrestore(&session->lock, flags);
1917 if (session->transport->session_recovery_timedout)
1918 session->transport->session_recovery_timedout(session);
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");
1925 static void __iscsi_unblock_session(struct work_struct *work)
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;
1934 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
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.
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);
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).
1950 if (shost->hostt->scan_finished) {
1951 if (scsi_queue_work(shost, &session->scan_work))
1952 atomic_inc(&ihost->nr_scans);
1954 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
1958 * iscsi_unblock_session - set a session as logged in and start IO.
1959 * @session: iscsi session
1961 * Mark a session as ready to accept IO.
1963 void iscsi_unblock_session(struct iscsi_cls_session *session)
1965 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
1967 * make sure all the events have completed before tell the driver
1968 * it is safe
1970 flush_workqueue(iscsi_eh_timer_workq);
1972 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
1974 static void __iscsi_block_session(struct work_struct *work)
1976 struct iscsi_cls_session *session =
1977 container_of(work, struct iscsi_cls_session,
1978 block_work);
1979 unsigned long flags;
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);
1993 void iscsi_block_session(struct iscsi_cls_session *session)
1995 queue_work(iscsi_eh_timer_workq, &session->block_work);
1997 EXPORT_SYMBOL_GPL(iscsi_block_session);
1999 static void __iscsi_unbind_session(struct work_struct *work)
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;
2009 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
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;
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);
2025 if (session->ida_used)
2026 ida_simple_remove(&iscsi_sess_ida, target_id);
2028 scsi_remove_target(&session->dev);
2030 unbind_session_exit:
2031 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
2032 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
2035 struct iscsi_cls_session *
2036 iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
2037 int dd_size)
2039 struct iscsi_cls_session *session;
2041 session = kzalloc(sizeof(*session) + dd_size,
2042 GFP_KERNEL);
2043 if (!session)
2044 return NULL;
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);
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];
2067 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
2068 return session;
2070 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
2072 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
2074 struct Scsi_Host *shost = iscsi_session_to_shost(session);
2075 struct iscsi_cls_host *ihost;
2076 unsigned long flags;
2077 int id = 0;
2078 int err;
2080 ihost = shost->shost_data;
2081 session->sid = atomic_add_return(1, &iscsi_session_nr);
2083 if (target_id == ISCSI_MAX_TARGET) {
2084 id = ida_simple_get(&iscsi_sess_ida, 0, 0, GFP_KERNEL);
2086 if (id < 0) {
2087 iscsi_cls_session_printk(KERN_ERR, session,
2088 "Failure in Target ID Allocation\n");
2089 return id;
2091 session->target_id = (unsigned int)id;
2092 session->ida_used = true;
2093 } else
2094 session->target_id = target_id;
2096 dev_set_name(&session->dev, "session%u", session->sid);
2097 err = device_add(&session->dev);
2098 if (err) {
2099 iscsi_cls_session_printk(KERN_ERR, session,
2100 "could not register session's dev\n");
2101 goto release_ida;
2103 transport_register_device(&session->dev);
2105 spin_lock_irqsave(&sesslock, flags);
2106 list_add(&session->sess_list, &sesslist);
2107 spin_unlock_irqrestore(&sesslock, flags);
2109 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
2110 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
2111 return 0;
2113 release_ida:
2114 if (session->ida_used)
2115 ida_simple_remove(&iscsi_sess_ida, session->target_id);
2117 return err;
2119 EXPORT_SYMBOL_GPL(iscsi_add_session);
2122 * iscsi_create_session - create iscsi class session
2123 * @shost: scsi host
2124 * @transport: iscsi transport
2125 * @dd_size: private driver data size
2126 * @target_id: which target
2128 * This can be called from a LLD or iscsi_transport.
2130 struct iscsi_cls_session *
2131 iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
2132 int dd_size, unsigned int target_id)
2134 struct iscsi_cls_session *session;
2136 session = iscsi_alloc_session(shost, transport, dd_size);
2137 if (!session)
2138 return NULL;
2140 if (iscsi_add_session(session, target_id)) {
2141 iscsi_free_session(session);
2142 return NULL;
2144 return session;
2146 EXPORT_SYMBOL_GPL(iscsi_create_session);
2148 static void iscsi_conn_release(struct device *dev)
2150 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
2151 struct device *parent = conn->dev.parent;
2153 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
2154 kfree(conn);
2155 put_device(parent);
2158 static int iscsi_is_conn_dev(const struct device *dev)
2160 return dev->release == iscsi_conn_release;
2163 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
2165 if (!iscsi_is_conn_dev(dev))
2166 return 0;
2167 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
2170 void iscsi_remove_session(struct iscsi_cls_session *session)
2172 struct Scsi_Host *shost = iscsi_session_to_shost(session);
2173 unsigned long flags;
2174 int err;
2176 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
2178 spin_lock_irqsave(&sesslock, flags);
2179 list_del(&session->sess_list);
2180 spin_unlock_irqrestore(&sesslock, flags);
2182 /* make sure there are no blocks/unblocks queued */
2183 flush_workqueue(iscsi_eh_timer_workq);
2184 /* make sure the timedout callout is not running */
2185 if (!cancel_delayed_work(&session->recovery_work))
2186 flush_workqueue(iscsi_eh_timer_workq);
2188 * If we are blocked let commands flow again. The lld or iscsi
2189 * layer should set up the queuecommand to fail commands.
2190 * We assume that LLD will not be calling block/unblock while
2191 * removing the session.
2193 spin_lock_irqsave(&session->lock, flags);
2194 session->state = ISCSI_SESSION_FREE;
2195 spin_unlock_irqrestore(&session->lock, flags);
2197 scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
2198 /* flush running scans then delete devices */
2199 scsi_flush_work(shost);
2200 __iscsi_unbind_session(&session->unbind_work);
2202 /* hw iscsi may not have removed all connections from session */
2203 err = device_for_each_child(&session->dev, NULL,
2204 iscsi_iter_destroy_conn_fn);
2205 if (err)
2206 iscsi_cls_session_printk(KERN_ERR, session,
2207 "Could not delete all connections "
2208 "for session. Error %d.\n", err);
2210 transport_unregister_device(&session->dev);
2212 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
2213 device_del(&session->dev);
2215 EXPORT_SYMBOL_GPL(iscsi_remove_session);
2217 void iscsi_free_session(struct iscsi_cls_session *session)
2219 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
2220 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
2221 put_device(&session->dev);
2223 EXPORT_SYMBOL_GPL(iscsi_free_session);
2226 * iscsi_destroy_session - destroy iscsi session
2227 * @session: iscsi_session
2229 * Can be called by a LLD or iscsi_transport. There must not be
2230 * any running connections.
2232 int iscsi_destroy_session(struct iscsi_cls_session *session)
2234 iscsi_remove_session(session);
2235 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
2236 iscsi_free_session(session);
2237 return 0;
2239 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
2242 * iscsi_create_conn - create iscsi class connection
2243 * @session: iscsi cls session
2244 * @dd_size: private driver data size
2245 * @cid: connection id
2247 * This can be called from a LLD or iscsi_transport. The connection
2248 * is child of the session so cid must be unique for all connections
2249 * on the session.
2251 * Since we do not support MCS, cid will normally be zero. In some cases
2252 * for software iscsi we could be trying to preallocate a connection struct
2253 * in which case there could be two connection structs and cid would be
2254 * non-zero.
2256 struct iscsi_cls_conn *
2257 iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
2259 struct iscsi_transport *transport = session->transport;
2260 struct iscsi_cls_conn *conn;
2261 unsigned long flags;
2262 int err;
2264 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
2265 if (!conn)
2266 return NULL;
2267 if (dd_size)
2268 conn->dd_data = &conn[1];
2270 mutex_init(&conn->ep_mutex);
2271 INIT_LIST_HEAD(&conn->conn_list);
2272 conn->transport = transport;
2273 conn->cid = cid;
2275 /* this is released in the dev's release function */
2276 if (!get_device(&session->dev))
2277 goto free_conn;
2279 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
2280 conn->dev.parent = &session->dev;
2281 conn->dev.release = iscsi_conn_release;
2282 err = device_register(&conn->dev);
2283 if (err) {
2284 iscsi_cls_session_printk(KERN_ERR, session, "could not "
2285 "register connection's dev\n");
2286 goto release_parent_ref;
2288 transport_register_device(&conn->dev);
2290 spin_lock_irqsave(&connlock, flags);
2291 list_add(&conn->conn_list, &connlist);
2292 spin_unlock_irqrestore(&connlock, flags);
2294 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
2295 return conn;
2297 release_parent_ref:
2298 put_device(&session->dev);
2299 free_conn:
2300 kfree(conn);
2301 return NULL;
2304 EXPORT_SYMBOL_GPL(iscsi_create_conn);
2307 * iscsi_destroy_conn - destroy iscsi class connection
2308 * @conn: iscsi cls session
2310 * This can be called from a LLD or iscsi_transport.
2312 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
2314 unsigned long flags;
2316 spin_lock_irqsave(&connlock, flags);
2317 list_del(&conn->conn_list);
2318 spin_unlock_irqrestore(&connlock, flags);
2320 transport_unregister_device(&conn->dev);
2321 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
2322 device_unregister(&conn->dev);
2323 return 0;
2325 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
2328 * iscsi interface functions
2330 static struct iscsi_internal *
2331 iscsi_if_transport_lookup(struct iscsi_transport *tt)
2333 struct iscsi_internal *priv;
2334 unsigned long flags;
2336 spin_lock_irqsave(&iscsi_transport_lock, flags);
2337 list_for_each_entry(priv, &iscsi_transports, list) {
2338 if (tt == priv->iscsi_transport) {
2339 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2340 return priv;
2343 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2344 return NULL;
2347 static int
2348 iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
2350 return nlmsg_multicast(nls, skb, 0, group, gfp);
2353 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
2354 char *data, uint32_t data_size)
2356 struct nlmsghdr *nlh;
2357 struct sk_buff *skb;
2358 struct iscsi_uevent *ev;
2359 char *pdu;
2360 struct iscsi_internal *priv;
2361 int len = nlmsg_total_size(sizeof(*ev) + sizeof(struct iscsi_hdr) +
2362 data_size);
2364 priv = iscsi_if_transport_lookup(conn->transport);
2365 if (!priv)
2366 return -EINVAL;
2368 skb = alloc_skb(len, GFP_ATOMIC);
2369 if (!skb) {
2370 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
2371 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
2372 "control PDU: OOM\n");
2373 return -ENOMEM;
2376 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2377 ev = nlmsg_data(nlh);
2378 memset(ev, 0, sizeof(*ev));
2379 ev->transport_handle = iscsi_handle(conn->transport);
2380 ev->type = ISCSI_KEVENT_RECV_PDU;
2381 ev->r.recv_req.cid = conn->cid;
2382 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
2383 pdu = (char*)ev + sizeof(*ev);
2384 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
2385 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
2387 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2389 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
2391 int iscsi_offload_mesg(struct Scsi_Host *shost,
2392 struct iscsi_transport *transport, uint32_t type,
2393 char *data, uint16_t data_size)
2395 struct nlmsghdr *nlh;
2396 struct sk_buff *skb;
2397 struct iscsi_uevent *ev;
2398 int len = nlmsg_total_size(sizeof(*ev) + data_size);
2400 skb = alloc_skb(len, GFP_ATOMIC);
2401 if (!skb) {
2402 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
2403 return -ENOMEM;
2406 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2407 ev = nlmsg_data(nlh);
2408 memset(ev, 0, sizeof(*ev));
2409 ev->type = type;
2410 ev->transport_handle = iscsi_handle(transport);
2411 switch (type) {
2412 case ISCSI_KEVENT_PATH_REQ:
2413 ev->r.req_path.host_no = shost->host_no;
2414 break;
2415 case ISCSI_KEVENT_IF_DOWN:
2416 ev->r.notify_if_down.host_no = shost->host_no;
2417 break;
2420 memcpy((char *)ev + sizeof(*ev), data, data_size);
2422 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
2424 EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
2426 void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
2428 struct nlmsghdr *nlh;
2429 struct sk_buff *skb;
2430 struct iscsi_uevent *ev;
2431 struct iscsi_internal *priv;
2432 int len = nlmsg_total_size(sizeof(*ev));
2434 priv = iscsi_if_transport_lookup(conn->transport);
2435 if (!priv)
2436 return;
2438 skb = alloc_skb(len, GFP_ATOMIC);
2439 if (!skb) {
2440 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2441 "conn error (%d)\n", error);
2442 return;
2445 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2446 ev = nlmsg_data(nlh);
2447 ev->transport_handle = iscsi_handle(conn->transport);
2448 ev->type = ISCSI_KEVENT_CONN_ERROR;
2449 ev->r.connerror.error = error;
2450 ev->r.connerror.cid = conn->cid;
2451 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
2453 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2455 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
2456 error);
2458 EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
2460 void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
2461 enum iscsi_conn_state state)
2463 struct nlmsghdr *nlh;
2464 struct sk_buff *skb;
2465 struct iscsi_uevent *ev;
2466 struct iscsi_internal *priv;
2467 int len = nlmsg_total_size(sizeof(*ev));
2469 priv = iscsi_if_transport_lookup(conn->transport);
2470 if (!priv)
2471 return;
2473 skb = alloc_skb(len, GFP_ATOMIC);
2474 if (!skb) {
2475 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2476 "conn login (%d)\n", state);
2477 return;
2480 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2481 ev = nlmsg_data(nlh);
2482 ev->transport_handle = iscsi_handle(conn->transport);
2483 ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
2484 ev->r.conn_login.state = state;
2485 ev->r.conn_login.cid = conn->cid;
2486 ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
2487 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2489 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
2490 state);
2492 EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
2494 void iscsi_post_host_event(uint32_t host_no, struct iscsi_transport *transport,
2495 enum iscsi_host_event_code code, uint32_t data_size,
2496 uint8_t *data)
2498 struct nlmsghdr *nlh;
2499 struct sk_buff *skb;
2500 struct iscsi_uevent *ev;
2501 int len = nlmsg_total_size(sizeof(*ev) + data_size);
2503 skb = alloc_skb(len, GFP_NOIO);
2504 if (!skb) {
2505 printk(KERN_ERR "gracefully ignored host event (%d):%d OOM\n",
2506 host_no, code);
2507 return;
2510 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2511 ev = nlmsg_data(nlh);
2512 ev->transport_handle = iscsi_handle(transport);
2513 ev->type = ISCSI_KEVENT_HOST_EVENT;
2514 ev->r.host_event.host_no = host_no;
2515 ev->r.host_event.code = code;
2516 ev->r.host_event.data_size = data_size;
2518 if (data_size)
2519 memcpy((char *)ev + sizeof(*ev), data, data_size);
2521 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
2523 EXPORT_SYMBOL_GPL(iscsi_post_host_event);
2525 void iscsi_ping_comp_event(uint32_t host_no, struct iscsi_transport *transport,
2526 uint32_t status, uint32_t pid, uint32_t data_size,
2527 uint8_t *data)
2529 struct nlmsghdr *nlh;
2530 struct sk_buff *skb;
2531 struct iscsi_uevent *ev;
2532 int len = nlmsg_total_size(sizeof(*ev) + data_size);
2534 skb = alloc_skb(len, GFP_NOIO);
2535 if (!skb) {
2536 printk(KERN_ERR "gracefully ignored ping comp: OOM\n");
2537 return;
2540 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2541 ev = nlmsg_data(nlh);
2542 ev->transport_handle = iscsi_handle(transport);
2543 ev->type = ISCSI_KEVENT_PING_COMP;
2544 ev->r.ping_comp.host_no = host_no;
2545 ev->r.ping_comp.status = status;
2546 ev->r.ping_comp.pid = pid;
2547 ev->r.ping_comp.data_size = data_size;
2548 memcpy((char *)ev + sizeof(*ev), data, data_size);
2550 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
2552 EXPORT_SYMBOL_GPL(iscsi_ping_comp_event);
2554 static int
2555 iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
2556 void *payload, int size)
2558 struct sk_buff *skb;
2559 struct nlmsghdr *nlh;
2560 int len = nlmsg_total_size(size);
2561 int flags = multi ? NLM_F_MULTI : 0;
2562 int t = done ? NLMSG_DONE : type;
2564 skb = alloc_skb(len, GFP_ATOMIC);
2565 if (!skb) {
2566 printk(KERN_ERR "Could not allocate skb to send reply.\n");
2567 return -ENOMEM;
2570 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
2571 nlh->nlmsg_flags = flags;
2572 memcpy(nlmsg_data(nlh), payload, size);
2573 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
2576 static int
2577 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
2579 struct iscsi_uevent *ev = nlmsg_data(nlh);
2580 struct iscsi_stats *stats;
2581 struct sk_buff *skbstat;
2582 struct iscsi_cls_conn *conn;
2583 struct nlmsghdr *nlhstat;
2584 struct iscsi_uevent *evstat;
2585 struct iscsi_internal *priv;
2586 int len = nlmsg_total_size(sizeof(*ev) +
2587 sizeof(struct iscsi_stats) +
2588 sizeof(struct iscsi_stats_custom) *
2589 ISCSI_STATS_CUSTOM_MAX);
2590 int err = 0;
2592 priv = iscsi_if_transport_lookup(transport);
2593 if (!priv)
2594 return -EINVAL;
2596 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
2597 if (!conn)
2598 return -EEXIST;
2600 do {
2601 int actual_size;
2603 skbstat = alloc_skb(len, GFP_ATOMIC);
2604 if (!skbstat) {
2605 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
2606 "deliver stats: OOM\n");
2607 return -ENOMEM;
2610 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
2611 (len - sizeof(*nlhstat)), 0);
2612 evstat = nlmsg_data(nlhstat);
2613 memset(evstat, 0, sizeof(*evstat));
2614 evstat->transport_handle = iscsi_handle(conn->transport);
2615 evstat->type = nlh->nlmsg_type;
2616 evstat->u.get_stats.cid =
2617 ev->u.get_stats.cid;
2618 evstat->u.get_stats.sid =
2619 ev->u.get_stats.sid;
2620 stats = (struct iscsi_stats *)
2621 ((char*)evstat + sizeof(*evstat));
2622 memset(stats, 0, sizeof(*stats));
2624 transport->get_stats(conn, stats);
2625 actual_size = nlmsg_total_size(sizeof(struct iscsi_uevent) +
2626 sizeof(struct iscsi_stats) +
2627 sizeof(struct iscsi_stats_custom) *
2628 stats->custom_length);
2629 actual_size -= sizeof(*nlhstat);
2630 actual_size = nlmsg_msg_size(actual_size);
2631 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
2632 nlhstat->nlmsg_len = actual_size;
2634 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
2635 GFP_ATOMIC);
2636 } while (err < 0 && err != -ECONNREFUSED);
2638 return err;
2642 * iscsi_session_event - send session destr. completion event
2643 * @session: iscsi class session
2644 * @event: type of event
2646 int iscsi_session_event(struct iscsi_cls_session *session,
2647 enum iscsi_uevent_e event)
2649 struct iscsi_internal *priv;
2650 struct Scsi_Host *shost;
2651 struct iscsi_uevent *ev;
2652 struct sk_buff *skb;
2653 struct nlmsghdr *nlh;
2654 int rc, len = nlmsg_total_size(sizeof(*ev));
2656 priv = iscsi_if_transport_lookup(session->transport);
2657 if (!priv)
2658 return -EINVAL;
2659 shost = iscsi_session_to_shost(session);
2661 skb = alloc_skb(len, GFP_KERNEL);
2662 if (!skb) {
2663 iscsi_cls_session_printk(KERN_ERR, session,
2664 "Cannot notify userspace of session "
2665 "event %u\n", event);
2666 return -ENOMEM;
2669 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2670 ev = nlmsg_data(nlh);
2671 ev->transport_handle = iscsi_handle(session->transport);
2673 ev->type = event;
2674 switch (event) {
2675 case ISCSI_KEVENT_DESTROY_SESSION:
2676 ev->r.d_session.host_no = shost->host_no;
2677 ev->r.d_session.sid = session->sid;
2678 break;
2679 case ISCSI_KEVENT_CREATE_SESSION:
2680 ev->r.c_session_ret.host_no = shost->host_no;
2681 ev->r.c_session_ret.sid = session->sid;
2682 break;
2683 case ISCSI_KEVENT_UNBIND_SESSION:
2684 ev->r.unbind_session.host_no = shost->host_no;
2685 ev->r.unbind_session.sid = session->sid;
2686 break;
2687 default:
2688 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
2689 "%u.\n", event);
2690 kfree_skb(skb);
2691 return -EINVAL;
2695 * this will occur if the daemon is not up, so we just warn
2696 * the user and when the daemon is restarted it will handle it
2698 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
2699 if (rc == -ESRCH)
2700 iscsi_cls_session_printk(KERN_ERR, session,
2701 "Cannot notify userspace of session "
2702 "event %u. Check iscsi daemon\n",
2703 event);
2705 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
2706 event, rc);
2707 return rc;
2709 EXPORT_SYMBOL_GPL(iscsi_session_event);
2711 static int
2712 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
2713 struct iscsi_uevent *ev, pid_t pid,
2714 uint32_t initial_cmdsn, uint16_t cmds_max,
2715 uint16_t queue_depth)
2717 struct iscsi_transport *transport = priv->iscsi_transport;
2718 struct iscsi_cls_session *session;
2719 struct Scsi_Host *shost;
2721 session = transport->create_session(ep, cmds_max, queue_depth,
2722 initial_cmdsn);
2723 if (!session)
2724 return -ENOMEM;
2726 session->creator = pid;
2727 shost = iscsi_session_to_shost(session);
2728 ev->r.c_session_ret.host_no = shost->host_no;
2729 ev->r.c_session_ret.sid = session->sid;
2730 ISCSI_DBG_TRANS_SESSION(session,
2731 "Completed creating transport session\n");
2732 return 0;
2735 static int
2736 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2738 struct iscsi_cls_conn *conn;
2739 struct iscsi_cls_session *session;
2741 session = iscsi_session_lookup(ev->u.c_conn.sid);
2742 if (!session) {
2743 printk(KERN_ERR "iscsi: invalid session %d.\n",
2744 ev->u.c_conn.sid);
2745 return -EINVAL;
2748 conn = transport->create_conn(session, ev->u.c_conn.cid);
2749 if (!conn) {
2750 iscsi_cls_session_printk(KERN_ERR, session,
2751 "couldn't create a new connection.");
2752 return -ENOMEM;
2755 ev->r.c_conn_ret.sid = session->sid;
2756 ev->r.c_conn_ret.cid = conn->cid;
2758 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
2759 return 0;
2762 static int
2763 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2765 struct iscsi_cls_conn *conn;
2767 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
2768 if (!conn)
2769 return -EINVAL;
2771 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
2772 if (transport->destroy_conn)
2773 transport->destroy_conn(conn);
2775 return 0;
2778 static int
2779 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2781 char *data = (char*)ev + sizeof(*ev);
2782 struct iscsi_cls_conn *conn;
2783 struct iscsi_cls_session *session;
2784 int err = 0, value = 0;
2786 session = iscsi_session_lookup(ev->u.set_param.sid);
2787 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
2788 if (!conn || !session)
2789 return -EINVAL;
2791 switch (ev->u.set_param.param) {
2792 case ISCSI_PARAM_SESS_RECOVERY_TMO:
2793 sscanf(data, "%d", &value);
2794 if (!session->recovery_tmo_sysfs_override)
2795 session->recovery_tmo = value;
2796 break;
2797 default:
2798 err = transport->set_param(conn, ev->u.set_param.param,
2799 data, ev->u.set_param.len);
2802 return err;
2805 static int iscsi_if_ep_connect(struct iscsi_transport *transport,
2806 struct iscsi_uevent *ev, int msg_type)
2808 struct iscsi_endpoint *ep;
2809 struct sockaddr *dst_addr;
2810 struct Scsi_Host *shost = NULL;
2811 int non_blocking, err = 0;
2813 if (!transport->ep_connect)
2814 return -EINVAL;
2816 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
2817 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
2818 if (!shost) {
2819 printk(KERN_ERR "ep connect failed. Could not find "
2820 "host no %u\n",
2821 ev->u.ep_connect_through_host.host_no);
2822 return -ENODEV;
2824 non_blocking = ev->u.ep_connect_through_host.non_blocking;
2825 } else
2826 non_blocking = ev->u.ep_connect.non_blocking;
2828 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
2829 ep = transport->ep_connect(shost, dst_addr, non_blocking);
2830 if (IS_ERR(ep)) {
2831 err = PTR_ERR(ep);
2832 goto release_host;
2835 ev->r.ep_connect_ret.handle = ep->id;
2836 release_host:
2837 if (shost)
2838 scsi_host_put(shost);
2839 return err;
2842 static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
2843 u64 ep_handle)
2845 struct iscsi_cls_conn *conn;
2846 struct iscsi_endpoint *ep;
2848 if (!transport->ep_disconnect)
2849 return -EINVAL;
2851 ep = iscsi_lookup_endpoint(ep_handle);
2852 if (!ep)
2853 return -EINVAL;
2854 conn = ep->conn;
2855 if (conn) {
2856 mutex_lock(&conn->ep_mutex);
2857 conn->ep = NULL;
2858 mutex_unlock(&conn->ep_mutex);
2861 transport->ep_disconnect(ep);
2862 return 0;
2865 static int
2866 iscsi_if_transport_ep(struct iscsi_transport *transport,
2867 struct iscsi_uevent *ev, int msg_type)
2869 struct iscsi_endpoint *ep;
2870 int rc = 0;
2872 switch (msg_type) {
2873 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
2874 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
2875 rc = iscsi_if_ep_connect(transport, ev, msg_type);
2876 break;
2877 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
2878 if (!transport->ep_poll)
2879 return -EINVAL;
2881 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
2882 if (!ep)
2883 return -EINVAL;
2885 ev->r.retcode = transport->ep_poll(ep,
2886 ev->u.ep_poll.timeout_ms);
2887 break;
2888 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
2889 rc = iscsi_if_ep_disconnect(transport,
2890 ev->u.ep_disconnect.ep_handle);
2891 break;
2893 return rc;
2896 static int
2897 iscsi_tgt_dscvr(struct iscsi_transport *transport,
2898 struct iscsi_uevent *ev)
2900 struct Scsi_Host *shost;
2901 struct sockaddr *dst_addr;
2902 int err;
2904 if (!transport->tgt_dscvr)
2905 return -EINVAL;
2907 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
2908 if (!shost) {
2909 printk(KERN_ERR "target discovery could not find host no %u\n",
2910 ev->u.tgt_dscvr.host_no);
2911 return -ENODEV;
2915 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
2916 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
2917 ev->u.tgt_dscvr.enable, dst_addr);
2918 scsi_host_put(shost);
2919 return err;
2922 static int
2923 iscsi_set_host_param(struct iscsi_transport *transport,
2924 struct iscsi_uevent *ev)
2926 char *data = (char*)ev + sizeof(*ev);
2927 struct Scsi_Host *shost;
2928 int err;
2930 if (!transport->set_host_param)
2931 return -ENOSYS;
2933 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
2934 if (!shost) {
2935 printk(KERN_ERR "set_host_param could not find host no %u\n",
2936 ev->u.set_host_param.host_no);
2937 return -ENODEV;
2940 err = transport->set_host_param(shost, ev->u.set_host_param.param,
2941 data, ev->u.set_host_param.len);
2942 scsi_host_put(shost);
2943 return err;
2946 static int
2947 iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2949 struct Scsi_Host *shost;
2950 struct iscsi_path *params;
2951 int err;
2953 if (!transport->set_path)
2954 return -ENOSYS;
2956 shost = scsi_host_lookup(ev->u.set_path.host_no);
2957 if (!shost) {
2958 printk(KERN_ERR "set path could not find host no %u\n",
2959 ev->u.set_path.host_no);
2960 return -ENODEV;
2963 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
2964 err = transport->set_path(shost, params);
2966 scsi_host_put(shost);
2967 return err;
2970 static int iscsi_session_has_conns(int sid)
2972 struct iscsi_cls_conn *conn;
2973 unsigned long flags;
2974 int found = 0;
2976 spin_lock_irqsave(&connlock, flags);
2977 list_for_each_entry(conn, &connlist, conn_list) {
2978 if (iscsi_conn_get_sid(conn) == sid) {
2979 found = 1;
2980 break;
2983 spin_unlock_irqrestore(&connlock, flags);
2985 return found;
2988 static int
2989 iscsi_set_iface_params(struct iscsi_transport *transport,
2990 struct iscsi_uevent *ev, uint32_t len)
2992 char *data = (char *)ev + sizeof(*ev);
2993 struct Scsi_Host *shost;
2994 int err;
2996 if (!transport->set_iface_param)
2997 return -ENOSYS;
2999 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
3000 if (!shost) {
3001 printk(KERN_ERR "set_iface_params could not find host no %u\n",
3002 ev->u.set_iface_params.host_no);
3003 return -ENODEV;
3006 err = transport->set_iface_param(shost, data, len);
3007 scsi_host_put(shost);
3008 return err;
3011 static int
3012 iscsi_send_ping(struct iscsi_transport *transport, struct iscsi_uevent *ev)
3014 struct Scsi_Host *shost;
3015 struct sockaddr *dst_addr;
3016 int err;
3018 if (!transport->send_ping)
3019 return -ENOSYS;
3021 shost = scsi_host_lookup(ev->u.iscsi_ping.host_no);
3022 if (!shost) {
3023 printk(KERN_ERR "iscsi_ping could not find host no %u\n",
3024 ev->u.iscsi_ping.host_no);
3025 return -ENODEV;
3028 dst_addr = (struct sockaddr *)((char *)ev + sizeof(*ev));
3029 err = transport->send_ping(shost, ev->u.iscsi_ping.iface_num,
3030 ev->u.iscsi_ping.iface_type,
3031 ev->u.iscsi_ping.payload_size,
3032 ev->u.iscsi_ping.pid,
3033 dst_addr);
3034 scsi_host_put(shost);
3035 return err;
3038 static int
3039 iscsi_get_chap(struct iscsi_transport *transport, struct nlmsghdr *nlh)
3041 struct iscsi_uevent *ev = nlmsg_data(nlh);
3042 struct Scsi_Host *shost = NULL;
3043 struct iscsi_chap_rec *chap_rec;
3044 struct iscsi_internal *priv;
3045 struct sk_buff *skbchap;
3046 struct nlmsghdr *nlhchap;
3047 struct iscsi_uevent *evchap;
3048 uint32_t chap_buf_size;
3049 int len, err = 0;
3050 char *buf;
3052 if (!transport->get_chap)
3053 return -EINVAL;
3055 priv = iscsi_if_transport_lookup(transport);
3056 if (!priv)
3057 return -EINVAL;
3059 chap_buf_size = (ev->u.get_chap.num_entries * sizeof(*chap_rec));
3060 len = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
3062 shost = scsi_host_lookup(ev->u.get_chap.host_no);
3063 if (!shost) {
3064 printk(KERN_ERR "%s: failed. Could not find host no %u\n",
3065 __func__, ev->u.get_chap.host_no);
3066 return -ENODEV;
3069 do {
3070 int actual_size;
3072 skbchap = alloc_skb(len, GFP_KERNEL);
3073 if (!skbchap) {
3074 printk(KERN_ERR "can not deliver chap: OOM\n");
3075 err = -ENOMEM;
3076 goto exit_get_chap;
3079 nlhchap = __nlmsg_put(skbchap, 0, 0, 0,
3080 (len - sizeof(*nlhchap)), 0);
3081 evchap = nlmsg_data(nlhchap);
3082 memset(evchap, 0, sizeof(*evchap));
3083 evchap->transport_handle = iscsi_handle(transport);
3084 evchap->type = nlh->nlmsg_type;
3085 evchap->u.get_chap.host_no = ev->u.get_chap.host_no;
3086 evchap->u.get_chap.chap_tbl_idx = ev->u.get_chap.chap_tbl_idx;
3087 evchap->u.get_chap.num_entries = ev->u.get_chap.num_entries;
3088 buf = (char *)evchap + sizeof(*evchap);
3089 memset(buf, 0, chap_buf_size);
3091 err = transport->get_chap(shost, ev->u.get_chap.chap_tbl_idx,
3092 &evchap->u.get_chap.num_entries, buf);
3094 actual_size = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
3095 skb_trim(skbchap, NLMSG_ALIGN(actual_size));
3096 nlhchap->nlmsg_len = actual_size;
3098 err = iscsi_multicast_skb(skbchap, ISCSI_NL_GRP_ISCSID,
3099 GFP_KERNEL);
3100 } while (err < 0 && err != -ECONNREFUSED);
3102 exit_get_chap:
3103 scsi_host_put(shost);
3104 return err;
3107 static int iscsi_set_chap(struct iscsi_transport *transport,
3108 struct iscsi_uevent *ev, uint32_t len)
3110 char *data = (char *)ev + sizeof(*ev);
3111 struct Scsi_Host *shost;
3112 int err = 0;
3114 if (!transport->set_chap)
3115 return -ENOSYS;
3117 shost = scsi_host_lookup(ev->u.set_path.host_no);
3118 if (!shost) {
3119 pr_err("%s could not find host no %u\n",
3120 __func__, ev->u.set_path.host_no);
3121 return -ENODEV;
3124 err = transport->set_chap(shost, data, len);
3125 scsi_host_put(shost);
3126 return err;
3129 static int iscsi_delete_chap(struct iscsi_transport *transport,
3130 struct iscsi_uevent *ev)
3132 struct Scsi_Host *shost;
3133 int err = 0;
3135 if (!transport->delete_chap)
3136 return -ENOSYS;
3138 shost = scsi_host_lookup(ev->u.delete_chap.host_no);
3139 if (!shost) {
3140 printk(KERN_ERR "%s could not find host no %u\n",
3141 __func__, ev->u.delete_chap.host_no);
3142 return -ENODEV;
3145 err = transport->delete_chap(shost, ev->u.delete_chap.chap_tbl_idx);
3146 scsi_host_put(shost);
3147 return err;
3150 static const struct {
3151 enum iscsi_discovery_parent_type value;
3152 char *name;
3153 } iscsi_discovery_parent_names[] = {
3154 {ISCSI_DISC_PARENT_UNKNOWN, "Unknown" },
3155 {ISCSI_DISC_PARENT_SENDTGT, "Sendtarget" },
3156 {ISCSI_DISC_PARENT_ISNS, "isns" },
3159 char *iscsi_get_discovery_parent_name(int parent_type)
3161 int i;
3162 char *state = "Unknown!";
3164 for (i = 0; i < ARRAY_SIZE(iscsi_discovery_parent_names); i++) {
3165 if (iscsi_discovery_parent_names[i].value & parent_type) {
3166 state = iscsi_discovery_parent_names[i].name;
3167 break;
3170 return state;
3172 EXPORT_SYMBOL_GPL(iscsi_get_discovery_parent_name);
3174 static int iscsi_set_flashnode_param(struct iscsi_transport *transport,
3175 struct iscsi_uevent *ev, uint32_t len)
3177 char *data = (char *)ev + sizeof(*ev);
3178 struct Scsi_Host *shost;
3179 struct iscsi_bus_flash_session *fnode_sess;
3180 struct iscsi_bus_flash_conn *fnode_conn;
3181 struct device *dev;
3182 uint32_t idx;
3183 int err = 0;
3185 if (!transport->set_flashnode_param) {
3186 err = -ENOSYS;
3187 goto exit_set_fnode;
3190 shost = scsi_host_lookup(ev->u.set_flashnode.host_no);
3191 if (!shost) {
3192 pr_err("%s could not find host no %u\n",
3193 __func__, ev->u.set_flashnode.host_no);
3194 err = -ENODEV;
3195 goto exit_set_fnode;
3198 idx = ev->u.set_flashnode.flashnode_idx;
3199 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3200 if (!fnode_sess) {
3201 pr_err("%s could not find flashnode %u for host no %u\n",
3202 __func__, idx, ev->u.set_flashnode.host_no);
3203 err = -ENODEV;
3204 goto put_host;
3207 dev = iscsi_find_flashnode_conn(fnode_sess);
3208 if (!dev) {
3209 err = -ENODEV;
3210 goto put_sess;
3213 fnode_conn = iscsi_dev_to_flash_conn(dev);
3214 err = transport->set_flashnode_param(fnode_sess, fnode_conn, data, len);
3215 put_device(dev);
3217 put_sess:
3218 put_device(&fnode_sess->dev);
3220 put_host:
3221 scsi_host_put(shost);
3223 exit_set_fnode:
3224 return err;
3227 static int iscsi_new_flashnode(struct iscsi_transport *transport,
3228 struct iscsi_uevent *ev, uint32_t len)
3230 char *data = (char *)ev + sizeof(*ev);
3231 struct Scsi_Host *shost;
3232 int index;
3233 int err = 0;
3235 if (!transport->new_flashnode) {
3236 err = -ENOSYS;
3237 goto exit_new_fnode;
3240 shost = scsi_host_lookup(ev->u.new_flashnode.host_no);
3241 if (!shost) {
3242 pr_err("%s could not find host no %u\n",
3243 __func__, ev->u.new_flashnode.host_no);
3244 err = -ENODEV;
3245 goto put_host;
3248 index = transport->new_flashnode(shost, data, len);
3250 if (index >= 0)
3251 ev->r.new_flashnode_ret.flashnode_idx = index;
3252 else
3253 err = -EIO;
3255 put_host:
3256 scsi_host_put(shost);
3258 exit_new_fnode:
3259 return err;
3262 static int iscsi_del_flashnode(struct iscsi_transport *transport,
3263 struct iscsi_uevent *ev)
3265 struct Scsi_Host *shost;
3266 struct iscsi_bus_flash_session *fnode_sess;
3267 uint32_t idx;
3268 int err = 0;
3270 if (!transport->del_flashnode) {
3271 err = -ENOSYS;
3272 goto exit_del_fnode;
3275 shost = scsi_host_lookup(ev->u.del_flashnode.host_no);
3276 if (!shost) {
3277 pr_err("%s could not find host no %u\n",
3278 __func__, ev->u.del_flashnode.host_no);
3279 err = -ENODEV;
3280 goto put_host;
3283 idx = ev->u.del_flashnode.flashnode_idx;
3284 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3285 if (!fnode_sess) {
3286 pr_err("%s could not find flashnode %u for host no %u\n",
3287 __func__, idx, ev->u.del_flashnode.host_no);
3288 err = -ENODEV;
3289 goto put_host;
3292 err = transport->del_flashnode(fnode_sess);
3293 put_device(&fnode_sess->dev);
3295 put_host:
3296 scsi_host_put(shost);
3298 exit_del_fnode:
3299 return err;
3302 static int iscsi_login_flashnode(struct iscsi_transport *transport,
3303 struct iscsi_uevent *ev)
3305 struct Scsi_Host *shost;
3306 struct iscsi_bus_flash_session *fnode_sess;
3307 struct iscsi_bus_flash_conn *fnode_conn;
3308 struct device *dev;
3309 uint32_t idx;
3310 int err = 0;
3312 if (!transport->login_flashnode) {
3313 err = -ENOSYS;
3314 goto exit_login_fnode;
3317 shost = scsi_host_lookup(ev->u.login_flashnode.host_no);
3318 if (!shost) {
3319 pr_err("%s could not find host no %u\n",
3320 __func__, ev->u.login_flashnode.host_no);
3321 err = -ENODEV;
3322 goto put_host;
3325 idx = ev->u.login_flashnode.flashnode_idx;
3326 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3327 if (!fnode_sess) {
3328 pr_err("%s could not find flashnode %u for host no %u\n",
3329 __func__, idx, ev->u.login_flashnode.host_no);
3330 err = -ENODEV;
3331 goto put_host;
3334 dev = iscsi_find_flashnode_conn(fnode_sess);
3335 if (!dev) {
3336 err = -ENODEV;
3337 goto put_sess;
3340 fnode_conn = iscsi_dev_to_flash_conn(dev);
3341 err = transport->login_flashnode(fnode_sess, fnode_conn);
3342 put_device(dev);
3344 put_sess:
3345 put_device(&fnode_sess->dev);
3347 put_host:
3348 scsi_host_put(shost);
3350 exit_login_fnode:
3351 return err;
3354 static int iscsi_logout_flashnode(struct iscsi_transport *transport,
3355 struct iscsi_uevent *ev)
3357 struct Scsi_Host *shost;
3358 struct iscsi_bus_flash_session *fnode_sess;
3359 struct iscsi_bus_flash_conn *fnode_conn;
3360 struct device *dev;
3361 uint32_t idx;
3362 int err = 0;
3364 if (!transport->logout_flashnode) {
3365 err = -ENOSYS;
3366 goto exit_logout_fnode;
3369 shost = scsi_host_lookup(ev->u.logout_flashnode.host_no);
3370 if (!shost) {
3371 pr_err("%s could not find host no %u\n",
3372 __func__, ev->u.logout_flashnode.host_no);
3373 err = -ENODEV;
3374 goto put_host;
3377 idx = ev->u.logout_flashnode.flashnode_idx;
3378 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3379 if (!fnode_sess) {
3380 pr_err("%s could not find flashnode %u for host no %u\n",
3381 __func__, idx, ev->u.logout_flashnode.host_no);
3382 err = -ENODEV;
3383 goto put_host;
3386 dev = iscsi_find_flashnode_conn(fnode_sess);
3387 if (!dev) {
3388 err = -ENODEV;
3389 goto put_sess;
3392 fnode_conn = iscsi_dev_to_flash_conn(dev);
3394 err = transport->logout_flashnode(fnode_sess, fnode_conn);
3395 put_device(dev);
3397 put_sess:
3398 put_device(&fnode_sess->dev);
3400 put_host:
3401 scsi_host_put(shost);
3403 exit_logout_fnode:
3404 return err;
3407 static int iscsi_logout_flashnode_sid(struct iscsi_transport *transport,
3408 struct iscsi_uevent *ev)
3410 struct Scsi_Host *shost;
3411 struct iscsi_cls_session *session;
3412 int err = 0;
3414 if (!transport->logout_flashnode_sid) {
3415 err = -ENOSYS;
3416 goto exit_logout_sid;
3419 shost = scsi_host_lookup(ev->u.logout_flashnode_sid.host_no);
3420 if (!shost) {
3421 pr_err("%s could not find host no %u\n",
3422 __func__, ev->u.logout_flashnode.host_no);
3423 err = -ENODEV;
3424 goto put_host;
3427 session = iscsi_session_lookup(ev->u.logout_flashnode_sid.sid);
3428 if (!session) {
3429 pr_err("%s could not find session id %u\n",
3430 __func__, ev->u.logout_flashnode_sid.sid);
3431 err = -EINVAL;
3432 goto put_host;
3435 err = transport->logout_flashnode_sid(session);
3437 put_host:
3438 scsi_host_put(shost);
3440 exit_logout_sid:
3441 return err;
3444 static int
3445 iscsi_get_host_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
3447 struct iscsi_uevent *ev = nlmsg_data(nlh);
3448 struct Scsi_Host *shost = NULL;
3449 struct iscsi_internal *priv;
3450 struct sk_buff *skbhost_stats;
3451 struct nlmsghdr *nlhhost_stats;
3452 struct iscsi_uevent *evhost_stats;
3453 int host_stats_size = 0;
3454 int len, err = 0;
3455 char *buf;
3457 if (!transport->get_host_stats)
3458 return -ENOSYS;
3460 priv = iscsi_if_transport_lookup(transport);
3461 if (!priv)
3462 return -EINVAL;
3464 host_stats_size = sizeof(struct iscsi_offload_host_stats);
3465 len = nlmsg_total_size(sizeof(*ev) + host_stats_size);
3467 shost = scsi_host_lookup(ev->u.get_host_stats.host_no);
3468 if (!shost) {
3469 pr_err("%s: failed. Cound not find host no %u\n",
3470 __func__, ev->u.get_host_stats.host_no);
3471 return -ENODEV;
3474 do {
3475 int actual_size;
3477 skbhost_stats = alloc_skb(len, GFP_KERNEL);
3478 if (!skbhost_stats) {
3479 pr_err("cannot deliver host stats: OOM\n");
3480 err = -ENOMEM;
3481 goto exit_host_stats;
3484 nlhhost_stats = __nlmsg_put(skbhost_stats, 0, 0, 0,
3485 (len - sizeof(*nlhhost_stats)), 0);
3486 evhost_stats = nlmsg_data(nlhhost_stats);
3487 memset(evhost_stats, 0, sizeof(*evhost_stats));
3488 evhost_stats->transport_handle = iscsi_handle(transport);
3489 evhost_stats->type = nlh->nlmsg_type;
3490 evhost_stats->u.get_host_stats.host_no =
3491 ev->u.get_host_stats.host_no;
3492 buf = (char *)evhost_stats + sizeof(*evhost_stats);
3493 memset(buf, 0, host_stats_size);
3495 err = transport->get_host_stats(shost, buf, host_stats_size);
3496 if (err) {
3497 kfree_skb(skbhost_stats);
3498 goto exit_host_stats;
3501 actual_size = nlmsg_total_size(sizeof(*ev) + host_stats_size);
3502 skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));
3503 nlhhost_stats->nlmsg_len = actual_size;
3505 err = iscsi_multicast_skb(skbhost_stats, ISCSI_NL_GRP_ISCSID,
3506 GFP_KERNEL);
3507 } while (err < 0 && err != -ECONNREFUSED);
3509 exit_host_stats:
3510 scsi_host_put(shost);
3511 return err;
3515 static int
3516 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
3518 int err = 0;
3519 struct iscsi_uevent *ev = nlmsg_data(nlh);
3520 struct iscsi_transport *transport = NULL;
3521 struct iscsi_internal *priv;
3522 struct iscsi_cls_session *session;
3523 struct iscsi_cls_conn *conn;
3524 struct iscsi_endpoint *ep = NULL;
3526 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
3527 *group = ISCSI_NL_GRP_UIP;
3528 else
3529 *group = ISCSI_NL_GRP_ISCSID;
3531 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
3532 if (!priv)
3533 return -EINVAL;
3534 transport = priv->iscsi_transport;
3536 if (!try_module_get(transport->owner))
3537 return -EINVAL;
3539 switch (nlh->nlmsg_type) {
3540 case ISCSI_UEVENT_CREATE_SESSION:
3541 err = iscsi_if_create_session(priv, ep, ev,
3542 NETLINK_CB(skb).portid,
3543 ev->u.c_session.initial_cmdsn,
3544 ev->u.c_session.cmds_max,
3545 ev->u.c_session.queue_depth);
3546 break;
3547 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
3548 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
3549 if (!ep) {
3550 err = -EINVAL;
3551 break;
3554 err = iscsi_if_create_session(priv, ep, ev,
3555 NETLINK_CB(skb).portid,
3556 ev->u.c_bound_session.initial_cmdsn,
3557 ev->u.c_bound_session.cmds_max,
3558 ev->u.c_bound_session.queue_depth);
3559 break;
3560 case ISCSI_UEVENT_DESTROY_SESSION:
3561 session = iscsi_session_lookup(ev->u.d_session.sid);
3562 if (!session)
3563 err = -EINVAL;
3564 else if (iscsi_session_has_conns(ev->u.d_session.sid))
3565 err = -EBUSY;
3566 else
3567 transport->destroy_session(session);
3568 break;
3569 case ISCSI_UEVENT_UNBIND_SESSION:
3570 session = iscsi_session_lookup(ev->u.d_session.sid);
3571 if (session)
3572 scsi_queue_work(iscsi_session_to_shost(session),
3573 &session->unbind_work);
3574 else
3575 err = -EINVAL;
3576 break;
3577 case ISCSI_UEVENT_CREATE_CONN:
3578 err = iscsi_if_create_conn(transport, ev);
3579 break;
3580 case ISCSI_UEVENT_DESTROY_CONN:
3581 err = iscsi_if_destroy_conn(transport, ev);
3582 break;
3583 case ISCSI_UEVENT_BIND_CONN:
3584 session = iscsi_session_lookup(ev->u.b_conn.sid);
3585 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
3587 if (conn && conn->ep)
3588 iscsi_if_ep_disconnect(transport, conn->ep->id);
3590 if (!session || !conn) {
3591 err = -EINVAL;
3592 break;
3595 ev->r.retcode = transport->bind_conn(session, conn,
3596 ev->u.b_conn.transport_eph,
3597 ev->u.b_conn.is_leading);
3598 if (ev->r.retcode || !transport->ep_connect)
3599 break;
3601 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
3602 if (ep) {
3603 ep->conn = conn;
3605 mutex_lock(&conn->ep_mutex);
3606 conn->ep = ep;
3607 mutex_unlock(&conn->ep_mutex);
3608 } else
3609 iscsi_cls_conn_printk(KERN_ERR, conn,
3610 "Could not set ep conn "
3611 "binding\n");
3612 break;
3613 case ISCSI_UEVENT_SET_PARAM:
3614 err = iscsi_set_param(transport, ev);
3615 break;
3616 case ISCSI_UEVENT_START_CONN:
3617 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
3618 if (conn)
3619 ev->r.retcode = transport->start_conn(conn);
3620 else
3621 err = -EINVAL;
3622 break;
3623 case ISCSI_UEVENT_STOP_CONN:
3624 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
3625 if (conn)
3626 transport->stop_conn(conn, ev->u.stop_conn.flag);
3627 else
3628 err = -EINVAL;
3629 break;
3630 case ISCSI_UEVENT_SEND_PDU:
3631 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
3632 if (conn)
3633 ev->r.retcode = transport->send_pdu(conn,
3634 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
3635 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
3636 ev->u.send_pdu.data_size);
3637 else
3638 err = -EINVAL;
3639 break;
3640 case ISCSI_UEVENT_GET_STATS:
3641 err = iscsi_if_get_stats(transport, nlh);
3642 break;
3643 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
3644 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
3645 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
3646 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
3647 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
3648 break;
3649 case ISCSI_UEVENT_TGT_DSCVR:
3650 err = iscsi_tgt_dscvr(transport, ev);
3651 break;
3652 case ISCSI_UEVENT_SET_HOST_PARAM:
3653 err = iscsi_set_host_param(transport, ev);
3654 break;
3655 case ISCSI_UEVENT_PATH_UPDATE:
3656 err = iscsi_set_path(transport, ev);
3657 break;
3658 case ISCSI_UEVENT_SET_IFACE_PARAMS:
3659 err = iscsi_set_iface_params(transport, ev,
3660 nlmsg_attrlen(nlh, sizeof(*ev)));
3661 break;
3662 case ISCSI_UEVENT_PING:
3663 err = iscsi_send_ping(transport, ev);
3664 break;
3665 case ISCSI_UEVENT_GET_CHAP:
3666 err = iscsi_get_chap(transport, nlh);
3667 break;
3668 case ISCSI_UEVENT_DELETE_CHAP:
3669 err = iscsi_delete_chap(transport, ev);
3670 break;
3671 case ISCSI_UEVENT_SET_FLASHNODE_PARAMS:
3672 err = iscsi_set_flashnode_param(transport, ev,
3673 nlmsg_attrlen(nlh,
3674 sizeof(*ev)));
3675 break;
3676 case ISCSI_UEVENT_NEW_FLASHNODE:
3677 err = iscsi_new_flashnode(transport, ev,
3678 nlmsg_attrlen(nlh, sizeof(*ev)));
3679 break;
3680 case ISCSI_UEVENT_DEL_FLASHNODE:
3681 err = iscsi_del_flashnode(transport, ev);
3682 break;
3683 case ISCSI_UEVENT_LOGIN_FLASHNODE:
3684 err = iscsi_login_flashnode(transport, ev);
3685 break;
3686 case ISCSI_UEVENT_LOGOUT_FLASHNODE:
3687 err = iscsi_logout_flashnode(transport, ev);
3688 break;
3689 case ISCSI_UEVENT_LOGOUT_FLASHNODE_SID:
3690 err = iscsi_logout_flashnode_sid(transport, ev);
3691 break;
3692 case ISCSI_UEVENT_SET_CHAP:
3693 err = iscsi_set_chap(transport, ev,
3694 nlmsg_attrlen(nlh, sizeof(*ev)));
3695 break;
3696 case ISCSI_UEVENT_GET_HOST_STATS:
3697 err = iscsi_get_host_stats(transport, nlh);
3698 break;
3699 default:
3700 err = -ENOSYS;
3701 break;
3704 module_put(transport->owner);
3705 return err;
3709 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
3710 * Malformed skbs with wrong lengths or invalid creds are not processed.
3712 static void
3713 iscsi_if_rx(struct sk_buff *skb)
3715 mutex_lock(&rx_queue_mutex);
3716 while (skb->len >= NLMSG_HDRLEN) {
3717 int err;
3718 uint32_t rlen;
3719 struct nlmsghdr *nlh;
3720 struct iscsi_uevent *ev;
3721 uint32_t group;
3722 int retries = ISCSI_SEND_MAX_ALLOWED;
3724 nlh = nlmsg_hdr(skb);
3725 if (nlh->nlmsg_len < sizeof(*nlh) + sizeof(*ev) ||
3726 skb->len < nlh->nlmsg_len) {
3727 break;
3730 ev = nlmsg_data(nlh);
3731 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
3732 if (rlen > skb->len)
3733 rlen = skb->len;
3735 err = iscsi_if_recv_msg(skb, nlh, &group);
3736 if (err) {
3737 ev->type = ISCSI_KEVENT_IF_ERROR;
3738 ev->iferror = err;
3740 do {
3742 * special case for GET_STATS:
3743 * on success - sending reply and stats from
3744 * inside of if_recv_msg(),
3745 * on error - fall through.
3747 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
3748 break;
3749 if (ev->type == ISCSI_UEVENT_GET_CHAP && !err)
3750 break;
3751 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
3752 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
3753 if (err == -EAGAIN && --retries < 0) {
3754 printk(KERN_WARNING "Send reply failed, error %d\n", err);
3755 break;
3757 } while (err < 0 && err != -ECONNREFUSED && err != -ESRCH);
3758 skb_pull(skb, rlen);
3760 mutex_unlock(&rx_queue_mutex);
3763 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
3764 struct device_attribute dev_attr_##_prefix##_##_name = \
3765 __ATTR(_name,_mode,_show,_store)
3768 * iSCSI connection attrs
3770 #define iscsi_conn_attr_show(param) \
3771 static ssize_t \
3772 show_conn_param_##param(struct device *dev, \
3773 struct device_attribute *attr, char *buf) \
3775 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
3776 struct iscsi_transport *t = conn->transport; \
3777 return t->get_conn_param(conn, param, buf); \
3780 #define iscsi_conn_attr(field, param) \
3781 iscsi_conn_attr_show(param) \
3782 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
3783 NULL);
3785 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
3786 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
3787 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
3788 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
3789 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
3790 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
3791 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
3792 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
3793 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
3794 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
3795 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
3796 iscsi_conn_attr(local_port, ISCSI_PARAM_LOCAL_PORT);
3797 iscsi_conn_attr(statsn, ISCSI_PARAM_STATSN);
3798 iscsi_conn_attr(keepalive_tmo, ISCSI_PARAM_KEEPALIVE_TMO);
3799 iscsi_conn_attr(max_segment_size, ISCSI_PARAM_MAX_SEGMENT_SIZE);
3800 iscsi_conn_attr(tcp_timestamp_stat, ISCSI_PARAM_TCP_TIMESTAMP_STAT);
3801 iscsi_conn_attr(tcp_wsf_disable, ISCSI_PARAM_TCP_WSF_DISABLE);
3802 iscsi_conn_attr(tcp_nagle_disable, ISCSI_PARAM_TCP_NAGLE_DISABLE);
3803 iscsi_conn_attr(tcp_timer_scale, ISCSI_PARAM_TCP_TIMER_SCALE);
3804 iscsi_conn_attr(tcp_timestamp_enable, ISCSI_PARAM_TCP_TIMESTAMP_EN);
3805 iscsi_conn_attr(fragment_disable, ISCSI_PARAM_IP_FRAGMENT_DISABLE);
3806 iscsi_conn_attr(ipv4_tos, ISCSI_PARAM_IPV4_TOS);
3807 iscsi_conn_attr(ipv6_traffic_class, ISCSI_PARAM_IPV6_TC);
3808 iscsi_conn_attr(ipv6_flow_label, ISCSI_PARAM_IPV6_FLOW_LABEL);
3809 iscsi_conn_attr(is_fw_assigned_ipv6, ISCSI_PARAM_IS_FW_ASSIGNED_IPV6);
3810 iscsi_conn_attr(tcp_xmit_wsf, ISCSI_PARAM_TCP_XMIT_WSF);
3811 iscsi_conn_attr(tcp_recv_wsf, ISCSI_PARAM_TCP_RECV_WSF);
3812 iscsi_conn_attr(local_ipaddr, ISCSI_PARAM_LOCAL_IPADDR);
3815 #define iscsi_conn_ep_attr_show(param) \
3816 static ssize_t show_conn_ep_param_##param(struct device *dev, \
3817 struct device_attribute *attr,\
3818 char *buf) \
3820 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
3821 struct iscsi_transport *t = conn->transport; \
3822 struct iscsi_endpoint *ep; \
3823 ssize_t rc; \
3825 /* \
3826 * Need to make sure ep_disconnect does not free the LLD's \
3827 * interconnect resources while we are trying to read them. \
3828 */ \
3829 mutex_lock(&conn->ep_mutex); \
3830 ep = conn->ep; \
3831 if (!ep && t->ep_connect) { \
3832 mutex_unlock(&conn->ep_mutex); \
3833 return -ENOTCONN; \
3836 if (ep) \
3837 rc = t->get_ep_param(ep, param, buf); \
3838 else \
3839 rc = t->get_conn_param(conn, param, buf); \
3840 mutex_unlock(&conn->ep_mutex); \
3841 return rc; \
3844 #define iscsi_conn_ep_attr(field, param) \
3845 iscsi_conn_ep_attr_show(param) \
3846 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
3847 show_conn_ep_param_##param, NULL);
3849 iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
3850 iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
3852 static struct attribute *iscsi_conn_attrs[] = {
3853 &dev_attr_conn_max_recv_dlength.attr,
3854 &dev_attr_conn_max_xmit_dlength.attr,
3855 &dev_attr_conn_header_digest.attr,
3856 &dev_attr_conn_data_digest.attr,
3857 &dev_attr_conn_ifmarker.attr,
3858 &dev_attr_conn_ofmarker.attr,
3859 &dev_attr_conn_address.attr,
3860 &dev_attr_conn_port.attr,
3861 &dev_attr_conn_exp_statsn.attr,
3862 &dev_attr_conn_persistent_address.attr,
3863 &dev_attr_conn_persistent_port.attr,
3864 &dev_attr_conn_ping_tmo.attr,
3865 &dev_attr_conn_recv_tmo.attr,
3866 &dev_attr_conn_local_port.attr,
3867 &dev_attr_conn_statsn.attr,
3868 &dev_attr_conn_keepalive_tmo.attr,
3869 &dev_attr_conn_max_segment_size.attr,
3870 &dev_attr_conn_tcp_timestamp_stat.attr,
3871 &dev_attr_conn_tcp_wsf_disable.attr,
3872 &dev_attr_conn_tcp_nagle_disable.attr,
3873 &dev_attr_conn_tcp_timer_scale.attr,
3874 &dev_attr_conn_tcp_timestamp_enable.attr,
3875 &dev_attr_conn_fragment_disable.attr,
3876 &dev_attr_conn_ipv4_tos.attr,
3877 &dev_attr_conn_ipv6_traffic_class.attr,
3878 &dev_attr_conn_ipv6_flow_label.attr,
3879 &dev_attr_conn_is_fw_assigned_ipv6.attr,
3880 &dev_attr_conn_tcp_xmit_wsf.attr,
3881 &dev_attr_conn_tcp_recv_wsf.attr,
3882 &dev_attr_conn_local_ipaddr.attr,
3883 NULL,
3886 static umode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
3887 struct attribute *attr, int i)
3889 struct device *cdev = container_of(kobj, struct device, kobj);
3890 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
3891 struct iscsi_transport *t = conn->transport;
3892 int param;
3894 if (attr == &dev_attr_conn_max_recv_dlength.attr)
3895 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
3896 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
3897 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
3898 else if (attr == &dev_attr_conn_header_digest.attr)
3899 param = ISCSI_PARAM_HDRDGST_EN;
3900 else if (attr == &dev_attr_conn_data_digest.attr)
3901 param = ISCSI_PARAM_DATADGST_EN;
3902 else if (attr == &dev_attr_conn_ifmarker.attr)
3903 param = ISCSI_PARAM_IFMARKER_EN;
3904 else if (attr == &dev_attr_conn_ofmarker.attr)
3905 param = ISCSI_PARAM_OFMARKER_EN;
3906 else if (attr == &dev_attr_conn_address.attr)
3907 param = ISCSI_PARAM_CONN_ADDRESS;
3908 else if (attr == &dev_attr_conn_port.attr)
3909 param = ISCSI_PARAM_CONN_PORT;
3910 else if (attr == &dev_attr_conn_exp_statsn.attr)
3911 param = ISCSI_PARAM_EXP_STATSN;
3912 else if (attr == &dev_attr_conn_persistent_address.attr)
3913 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
3914 else if (attr == &dev_attr_conn_persistent_port.attr)
3915 param = ISCSI_PARAM_PERSISTENT_PORT;
3916 else if (attr == &dev_attr_conn_ping_tmo.attr)
3917 param = ISCSI_PARAM_PING_TMO;
3918 else if (attr == &dev_attr_conn_recv_tmo.attr)
3919 param = ISCSI_PARAM_RECV_TMO;
3920 else if (attr == &dev_attr_conn_local_port.attr)
3921 param = ISCSI_PARAM_LOCAL_PORT;
3922 else if (attr == &dev_attr_conn_statsn.attr)
3923 param = ISCSI_PARAM_STATSN;
3924 else if (attr == &dev_attr_conn_keepalive_tmo.attr)
3925 param = ISCSI_PARAM_KEEPALIVE_TMO;
3926 else if (attr == &dev_attr_conn_max_segment_size.attr)
3927 param = ISCSI_PARAM_MAX_SEGMENT_SIZE;
3928 else if (attr == &dev_attr_conn_tcp_timestamp_stat.attr)
3929 param = ISCSI_PARAM_TCP_TIMESTAMP_STAT;
3930 else if (attr == &dev_attr_conn_tcp_wsf_disable.attr)
3931 param = ISCSI_PARAM_TCP_WSF_DISABLE;
3932 else if (attr == &dev_attr_conn_tcp_nagle_disable.attr)
3933 param = ISCSI_PARAM_TCP_NAGLE_DISABLE;
3934 else if (attr == &dev_attr_conn_tcp_timer_scale.attr)
3935 param = ISCSI_PARAM_TCP_TIMER_SCALE;
3936 else if (attr == &dev_attr_conn_tcp_timestamp_enable.attr)
3937 param = ISCSI_PARAM_TCP_TIMESTAMP_EN;
3938 else if (attr == &dev_attr_conn_fragment_disable.attr)
3939 param = ISCSI_PARAM_IP_FRAGMENT_DISABLE;
3940 else if (attr == &dev_attr_conn_ipv4_tos.attr)
3941 param = ISCSI_PARAM_IPV4_TOS;
3942 else if (attr == &dev_attr_conn_ipv6_traffic_class.attr)
3943 param = ISCSI_PARAM_IPV6_TC;
3944 else if (attr == &dev_attr_conn_ipv6_flow_label.attr)
3945 param = ISCSI_PARAM_IPV6_FLOW_LABEL;
3946 else if (attr == &dev_attr_conn_is_fw_assigned_ipv6.attr)
3947 param = ISCSI_PARAM_IS_FW_ASSIGNED_IPV6;
3948 else if (attr == &dev_attr_conn_tcp_xmit_wsf.attr)
3949 param = ISCSI_PARAM_TCP_XMIT_WSF;
3950 else if (attr == &dev_attr_conn_tcp_recv_wsf.attr)
3951 param = ISCSI_PARAM_TCP_RECV_WSF;
3952 else if (attr == &dev_attr_conn_local_ipaddr.attr)
3953 param = ISCSI_PARAM_LOCAL_IPADDR;
3954 else {
3955 WARN_ONCE(1, "Invalid conn attr");
3956 return 0;
3959 return t->attr_is_visible(ISCSI_PARAM, param);
3962 static struct attribute_group iscsi_conn_group = {
3963 .attrs = iscsi_conn_attrs,
3964 .is_visible = iscsi_conn_attr_is_visible,
3968 * iSCSI session attrs
3970 #define iscsi_session_attr_show(param, perm) \
3971 static ssize_t \
3972 show_session_param_##param(struct device *dev, \
3973 struct device_attribute *attr, char *buf) \
3975 struct iscsi_cls_session *session = \
3976 iscsi_dev_to_session(dev->parent); \
3977 struct iscsi_transport *t = session->transport; \
3979 if (perm && !capable(CAP_SYS_ADMIN)) \
3980 return -EACCES; \
3981 return t->get_session_param(session, param, buf); \
3984 #define iscsi_session_attr(field, param, perm) \
3985 iscsi_session_attr_show(param, perm) \
3986 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
3987 NULL);
3988 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
3989 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
3990 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
3991 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
3992 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
3993 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
3994 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
3995 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
3996 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
3997 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
3998 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
3999 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
4000 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
4001 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
4002 iscsi_session_attr(chap_out_idx, ISCSI_PARAM_CHAP_OUT_IDX, 1);
4003 iscsi_session_attr(chap_in_idx, ISCSI_PARAM_CHAP_IN_IDX, 1);
4004 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
4005 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
4006 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
4007 iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
4008 iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
4009 iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
4010 iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
4011 iscsi_session_attr(boot_root, ISCSI_PARAM_BOOT_ROOT, 0);
4012 iscsi_session_attr(boot_nic, ISCSI_PARAM_BOOT_NIC, 0);
4013 iscsi_session_attr(boot_target, ISCSI_PARAM_BOOT_TARGET, 0);
4014 iscsi_session_attr(auto_snd_tgt_disable, ISCSI_PARAM_AUTO_SND_TGT_DISABLE, 0);
4015 iscsi_session_attr(discovery_session, ISCSI_PARAM_DISCOVERY_SESS, 0);
4016 iscsi_session_attr(portal_type, ISCSI_PARAM_PORTAL_TYPE, 0);
4017 iscsi_session_attr(chap_auth, ISCSI_PARAM_CHAP_AUTH_EN, 0);
4018 iscsi_session_attr(discovery_logout, ISCSI_PARAM_DISCOVERY_LOGOUT_EN, 0);
4019 iscsi_session_attr(bidi_chap, ISCSI_PARAM_BIDI_CHAP_EN, 0);
4020 iscsi_session_attr(discovery_auth_optional,
4021 ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL, 0);
4022 iscsi_session_attr(def_time2wait, ISCSI_PARAM_DEF_TIME2WAIT, 0);
4023 iscsi_session_attr(def_time2retain, ISCSI_PARAM_DEF_TIME2RETAIN, 0);
4024 iscsi_session_attr(isid, ISCSI_PARAM_ISID, 0);
4025 iscsi_session_attr(tsid, ISCSI_PARAM_TSID, 0);
4026 iscsi_session_attr(def_taskmgmt_tmo, ISCSI_PARAM_DEF_TASKMGMT_TMO, 0);
4027 iscsi_session_attr(discovery_parent_idx, ISCSI_PARAM_DISCOVERY_PARENT_IDX, 0);
4028 iscsi_session_attr(discovery_parent_type, ISCSI_PARAM_DISCOVERY_PARENT_TYPE, 0);
4030 static ssize_t
4031 show_priv_session_state(struct device *dev, struct device_attribute *attr,
4032 char *buf)
4034 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
4035 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
4037 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
4038 NULL);
4039 static ssize_t
4040 show_priv_session_creator(struct device *dev, struct device_attribute *attr,
4041 char *buf)
4043 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
4044 return sprintf(buf, "%d\n", session->creator);
4046 static ISCSI_CLASS_ATTR(priv_sess, creator, S_IRUGO, show_priv_session_creator,
4047 NULL);
4048 static ssize_t
4049 show_priv_session_target_id(struct device *dev, struct device_attribute *attr,
4050 char *buf)
4052 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
4053 return sprintf(buf, "%d\n", session->target_id);
4055 static ISCSI_CLASS_ATTR(priv_sess, target_id, S_IRUGO,
4056 show_priv_session_target_id, NULL);
4058 #define iscsi_priv_session_attr_show(field, format) \
4059 static ssize_t \
4060 show_priv_session_##field(struct device *dev, \
4061 struct device_attribute *attr, char *buf) \
4063 struct iscsi_cls_session *session = \
4064 iscsi_dev_to_session(dev->parent); \
4065 if (session->field == -1) \
4066 return sprintf(buf, "off\n"); \
4067 return sprintf(buf, format"\n", session->field); \
4070 #define iscsi_priv_session_attr_store(field) \
4071 static ssize_t \
4072 store_priv_session_##field(struct device *dev, \
4073 struct device_attribute *attr, \
4074 const char *buf, size_t count) \
4076 int val; \
4077 char *cp; \
4078 struct iscsi_cls_session *session = \
4079 iscsi_dev_to_session(dev->parent); \
4080 if ((session->state == ISCSI_SESSION_FREE) || \
4081 (session->state == ISCSI_SESSION_FAILED)) \
4082 return -EBUSY; \
4083 if (strncmp(buf, "off", 3) == 0) { \
4084 session->field = -1; \
4085 session->field##_sysfs_override = true; \
4086 } else { \
4087 val = simple_strtoul(buf, &cp, 0); \
4088 if (*cp != '\0' && *cp != '\n') \
4089 return -EINVAL; \
4090 session->field = val; \
4091 session->field##_sysfs_override = true; \
4093 return count; \
4096 #define iscsi_priv_session_rw_attr(field, format) \
4097 iscsi_priv_session_attr_show(field, format) \
4098 iscsi_priv_session_attr_store(field) \
4099 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
4100 show_priv_session_##field, \
4101 store_priv_session_##field)
4103 iscsi_priv_session_rw_attr(recovery_tmo, "%d");
4105 static struct attribute *iscsi_session_attrs[] = {
4106 &dev_attr_sess_initial_r2t.attr,
4107 &dev_attr_sess_max_outstanding_r2t.attr,
4108 &dev_attr_sess_immediate_data.attr,
4109 &dev_attr_sess_first_burst_len.attr,
4110 &dev_attr_sess_max_burst_len.attr,
4111 &dev_attr_sess_data_pdu_in_order.attr,
4112 &dev_attr_sess_data_seq_in_order.attr,
4113 &dev_attr_sess_erl.attr,
4114 &dev_attr_sess_targetname.attr,
4115 &dev_attr_sess_tpgt.attr,
4116 &dev_attr_sess_password.attr,
4117 &dev_attr_sess_password_in.attr,
4118 &dev_attr_sess_username.attr,
4119 &dev_attr_sess_username_in.attr,
4120 &dev_attr_sess_fast_abort.attr,
4121 &dev_attr_sess_abort_tmo.attr,
4122 &dev_attr_sess_lu_reset_tmo.attr,
4123 &dev_attr_sess_tgt_reset_tmo.attr,
4124 &dev_attr_sess_ifacename.attr,
4125 &dev_attr_sess_initiatorname.attr,
4126 &dev_attr_sess_targetalias.attr,
4127 &dev_attr_sess_boot_root.attr,
4128 &dev_attr_sess_boot_nic.attr,
4129 &dev_attr_sess_boot_target.attr,
4130 &dev_attr_priv_sess_recovery_tmo.attr,
4131 &dev_attr_priv_sess_state.attr,
4132 &dev_attr_priv_sess_creator.attr,
4133 &dev_attr_sess_chap_out_idx.attr,
4134 &dev_attr_sess_chap_in_idx.attr,
4135 &dev_attr_priv_sess_target_id.attr,
4136 &dev_attr_sess_auto_snd_tgt_disable.attr,
4137 &dev_attr_sess_discovery_session.attr,
4138 &dev_attr_sess_portal_type.attr,
4139 &dev_attr_sess_chap_auth.attr,
4140 &dev_attr_sess_discovery_logout.attr,
4141 &dev_attr_sess_bidi_chap.attr,
4142 &dev_attr_sess_discovery_auth_optional.attr,
4143 &dev_attr_sess_def_time2wait.attr,
4144 &dev_attr_sess_def_time2retain.attr,
4145 &dev_attr_sess_isid.attr,
4146 &dev_attr_sess_tsid.attr,
4147 &dev_attr_sess_def_taskmgmt_tmo.attr,
4148 &dev_attr_sess_discovery_parent_idx.attr,
4149 &dev_attr_sess_discovery_parent_type.attr,
4150 NULL,
4153 static umode_t iscsi_session_attr_is_visible(struct kobject *kobj,
4154 struct attribute *attr, int i)
4156 struct device *cdev = container_of(kobj, struct device, kobj);
4157 struct iscsi_cls_session *session = transport_class_to_session(cdev);
4158 struct iscsi_transport *t = session->transport;
4159 int param;
4161 if (attr == &dev_attr_sess_initial_r2t.attr)
4162 param = ISCSI_PARAM_INITIAL_R2T_EN;
4163 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
4164 param = ISCSI_PARAM_MAX_R2T;
4165 else if (attr == &dev_attr_sess_immediate_data.attr)
4166 param = ISCSI_PARAM_IMM_DATA_EN;
4167 else if (attr == &dev_attr_sess_first_burst_len.attr)
4168 param = ISCSI_PARAM_FIRST_BURST;
4169 else if (attr == &dev_attr_sess_max_burst_len.attr)
4170 param = ISCSI_PARAM_MAX_BURST;
4171 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
4172 param = ISCSI_PARAM_PDU_INORDER_EN;
4173 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
4174 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
4175 else if (attr == &dev_attr_sess_erl.attr)
4176 param = ISCSI_PARAM_ERL;
4177 else if (attr == &dev_attr_sess_targetname.attr)
4178 param = ISCSI_PARAM_TARGET_NAME;
4179 else if (attr == &dev_attr_sess_tpgt.attr)
4180 param = ISCSI_PARAM_TPGT;
4181 else if (attr == &dev_attr_sess_chap_in_idx.attr)
4182 param = ISCSI_PARAM_CHAP_IN_IDX;
4183 else if (attr == &dev_attr_sess_chap_out_idx.attr)
4184 param = ISCSI_PARAM_CHAP_OUT_IDX;
4185 else if (attr == &dev_attr_sess_password.attr)
4186 param = ISCSI_PARAM_USERNAME;
4187 else if (attr == &dev_attr_sess_password_in.attr)
4188 param = ISCSI_PARAM_USERNAME_IN;
4189 else if (attr == &dev_attr_sess_username.attr)
4190 param = ISCSI_PARAM_PASSWORD;
4191 else if (attr == &dev_attr_sess_username_in.attr)
4192 param = ISCSI_PARAM_PASSWORD_IN;
4193 else if (attr == &dev_attr_sess_fast_abort.attr)
4194 param = ISCSI_PARAM_FAST_ABORT;
4195 else if (attr == &dev_attr_sess_abort_tmo.attr)
4196 param = ISCSI_PARAM_ABORT_TMO;
4197 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
4198 param = ISCSI_PARAM_LU_RESET_TMO;
4199 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
4200 param = ISCSI_PARAM_TGT_RESET_TMO;
4201 else if (attr == &dev_attr_sess_ifacename.attr)
4202 param = ISCSI_PARAM_IFACE_NAME;
4203 else if (attr == &dev_attr_sess_initiatorname.attr)
4204 param = ISCSI_PARAM_INITIATOR_NAME;
4205 else if (attr == &dev_attr_sess_targetalias.attr)
4206 param = ISCSI_PARAM_TARGET_ALIAS;
4207 else if (attr == &dev_attr_sess_boot_root.attr)
4208 param = ISCSI_PARAM_BOOT_ROOT;
4209 else if (attr == &dev_attr_sess_boot_nic.attr)
4210 param = ISCSI_PARAM_BOOT_NIC;
4211 else if (attr == &dev_attr_sess_boot_target.attr)
4212 param = ISCSI_PARAM_BOOT_TARGET;
4213 else if (attr == &dev_attr_sess_auto_snd_tgt_disable.attr)
4214 param = ISCSI_PARAM_AUTO_SND_TGT_DISABLE;
4215 else if (attr == &dev_attr_sess_discovery_session.attr)
4216 param = ISCSI_PARAM_DISCOVERY_SESS;
4217 else if (attr == &dev_attr_sess_portal_type.attr)
4218 param = ISCSI_PARAM_PORTAL_TYPE;
4219 else if (attr == &dev_attr_sess_chap_auth.attr)
4220 param = ISCSI_PARAM_CHAP_AUTH_EN;
4221 else if (attr == &dev_attr_sess_discovery_logout.attr)
4222 param = ISCSI_PARAM_DISCOVERY_LOGOUT_EN;
4223 else if (attr == &dev_attr_sess_bidi_chap.attr)
4224 param = ISCSI_PARAM_BIDI_CHAP_EN;
4225 else if (attr == &dev_attr_sess_discovery_auth_optional.attr)
4226 param = ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL;
4227 else if (attr == &dev_attr_sess_def_time2wait.attr)
4228 param = ISCSI_PARAM_DEF_TIME2WAIT;
4229 else if (attr == &dev_attr_sess_def_time2retain.attr)
4230 param = ISCSI_PARAM_DEF_TIME2RETAIN;
4231 else if (attr == &dev_attr_sess_isid.attr)
4232 param = ISCSI_PARAM_ISID;
4233 else if (attr == &dev_attr_sess_tsid.attr)
4234 param = ISCSI_PARAM_TSID;
4235 else if (attr == &dev_attr_sess_def_taskmgmt_tmo.attr)
4236 param = ISCSI_PARAM_DEF_TASKMGMT_TMO;
4237 else if (attr == &dev_attr_sess_discovery_parent_idx.attr)
4238 param = ISCSI_PARAM_DISCOVERY_PARENT_IDX;
4239 else if (attr == &dev_attr_sess_discovery_parent_type.attr)
4240 param = ISCSI_PARAM_DISCOVERY_PARENT_TYPE;
4241 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
4242 return S_IRUGO | S_IWUSR;
4243 else if (attr == &dev_attr_priv_sess_state.attr)
4244 return S_IRUGO;
4245 else if (attr == &dev_attr_priv_sess_creator.attr)
4246 return S_IRUGO;
4247 else if (attr == &dev_attr_priv_sess_target_id.attr)
4248 return S_IRUGO;
4249 else {
4250 WARN_ONCE(1, "Invalid session attr");
4251 return 0;
4254 return t->attr_is_visible(ISCSI_PARAM, param);
4257 static struct attribute_group iscsi_session_group = {
4258 .attrs = iscsi_session_attrs,
4259 .is_visible = iscsi_session_attr_is_visible,
4263 * iSCSI host attrs
4265 #define iscsi_host_attr_show(param) \
4266 static ssize_t \
4267 show_host_param_##param(struct device *dev, \
4268 struct device_attribute *attr, char *buf) \
4270 struct Scsi_Host *shost = transport_class_to_shost(dev); \
4271 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
4272 return priv->iscsi_transport->get_host_param(shost, param, buf); \
4275 #define iscsi_host_attr(field, param) \
4276 iscsi_host_attr_show(param) \
4277 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
4278 NULL);
4280 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
4281 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
4282 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
4283 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
4284 iscsi_host_attr(port_state, ISCSI_HOST_PARAM_PORT_STATE);
4285 iscsi_host_attr(port_speed, ISCSI_HOST_PARAM_PORT_SPEED);
4287 static struct attribute *iscsi_host_attrs[] = {
4288 &dev_attr_host_netdev.attr,
4289 &dev_attr_host_hwaddress.attr,
4290 &dev_attr_host_ipaddress.attr,
4291 &dev_attr_host_initiatorname.attr,
4292 &dev_attr_host_port_state.attr,
4293 &dev_attr_host_port_speed.attr,
4294 NULL,
4297 static umode_t iscsi_host_attr_is_visible(struct kobject *kobj,
4298 struct attribute *attr, int i)
4300 struct device *cdev = container_of(kobj, struct device, kobj);
4301 struct Scsi_Host *shost = transport_class_to_shost(cdev);
4302 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
4303 int param;
4305 if (attr == &dev_attr_host_netdev.attr)
4306 param = ISCSI_HOST_PARAM_NETDEV_NAME;
4307 else if (attr == &dev_attr_host_hwaddress.attr)
4308 param = ISCSI_HOST_PARAM_HWADDRESS;
4309 else if (attr == &dev_attr_host_ipaddress.attr)
4310 param = ISCSI_HOST_PARAM_IPADDRESS;
4311 else if (attr == &dev_attr_host_initiatorname.attr)
4312 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
4313 else if (attr == &dev_attr_host_port_state.attr)
4314 param = ISCSI_HOST_PARAM_PORT_STATE;
4315 else if (attr == &dev_attr_host_port_speed.attr)
4316 param = ISCSI_HOST_PARAM_PORT_SPEED;
4317 else {
4318 WARN_ONCE(1, "Invalid host attr");
4319 return 0;
4322 return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
4325 static struct attribute_group iscsi_host_group = {
4326 .attrs = iscsi_host_attrs,
4327 .is_visible = iscsi_host_attr_is_visible,
4330 /* convert iscsi_port_speed values to ascii string name */
4331 static const struct {
4332 enum iscsi_port_speed value;
4333 char *name;
4334 } iscsi_port_speed_names[] = {
4335 {ISCSI_PORT_SPEED_UNKNOWN, "Unknown" },
4336 {ISCSI_PORT_SPEED_10MBPS, "10 Mbps" },
4337 {ISCSI_PORT_SPEED_100MBPS, "100 Mbps" },
4338 {ISCSI_PORT_SPEED_1GBPS, "1 Gbps" },
4339 {ISCSI_PORT_SPEED_10GBPS, "10 Gbps" },
4342 char *iscsi_get_port_speed_name(struct Scsi_Host *shost)
4344 int i;
4345 char *speed = "Unknown!";
4346 struct iscsi_cls_host *ihost = shost->shost_data;
4347 uint32_t port_speed = ihost->port_speed;
4349 for (i = 0; i < ARRAY_SIZE(iscsi_port_speed_names); i++) {
4350 if (iscsi_port_speed_names[i].value & port_speed) {
4351 speed = iscsi_port_speed_names[i].name;
4352 break;
4355 return speed;
4357 EXPORT_SYMBOL_GPL(iscsi_get_port_speed_name);
4359 /* convert iscsi_port_state values to ascii string name */
4360 static const struct {
4361 enum iscsi_port_state value;
4362 char *name;
4363 } iscsi_port_state_names[] = {
4364 {ISCSI_PORT_STATE_DOWN, "LINK DOWN" },
4365 {ISCSI_PORT_STATE_UP, "LINK UP" },
4368 char *iscsi_get_port_state_name(struct Scsi_Host *shost)
4370 int i;
4371 char *state = "Unknown!";
4372 struct iscsi_cls_host *ihost = shost->shost_data;
4373 uint32_t port_state = ihost->port_state;
4375 for (i = 0; i < ARRAY_SIZE(iscsi_port_state_names); i++) {
4376 if (iscsi_port_state_names[i].value & port_state) {
4377 state = iscsi_port_state_names[i].name;
4378 break;
4381 return state;
4383 EXPORT_SYMBOL_GPL(iscsi_get_port_state_name);
4385 static int iscsi_session_match(struct attribute_container *cont,
4386 struct device *dev)
4388 struct iscsi_cls_session *session;
4389 struct Scsi_Host *shost;
4390 struct iscsi_internal *priv;
4392 if (!iscsi_is_session_dev(dev))
4393 return 0;
4395 session = iscsi_dev_to_session(dev);
4396 shost = iscsi_session_to_shost(session);
4397 if (!shost->transportt)
4398 return 0;
4400 priv = to_iscsi_internal(shost->transportt);
4401 if (priv->session_cont.ac.class != &iscsi_session_class.class)
4402 return 0;
4404 return &priv->session_cont.ac == cont;
4407 static int iscsi_conn_match(struct attribute_container *cont,
4408 struct device *dev)
4410 struct iscsi_cls_session *session;
4411 struct iscsi_cls_conn *conn;
4412 struct Scsi_Host *shost;
4413 struct iscsi_internal *priv;
4415 if (!iscsi_is_conn_dev(dev))
4416 return 0;
4418 conn = iscsi_dev_to_conn(dev);
4419 session = iscsi_dev_to_session(conn->dev.parent);
4420 shost = iscsi_session_to_shost(session);
4422 if (!shost->transportt)
4423 return 0;
4425 priv = to_iscsi_internal(shost->transportt);
4426 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
4427 return 0;
4429 return &priv->conn_cont.ac == cont;
4432 static int iscsi_host_match(struct attribute_container *cont,
4433 struct device *dev)
4435 struct Scsi_Host *shost;
4436 struct iscsi_internal *priv;
4438 if (!scsi_is_host_device(dev))
4439 return 0;
4441 shost = dev_to_shost(dev);
4442 if (!shost->transportt ||
4443 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
4444 return 0;
4446 priv = to_iscsi_internal(shost->transportt);
4447 return &priv->t.host_attrs.ac == cont;
4450 struct scsi_transport_template *
4451 iscsi_register_transport(struct iscsi_transport *tt)
4453 struct iscsi_internal *priv;
4454 unsigned long flags;
4455 int err;
4457 BUG_ON(!tt);
4459 priv = iscsi_if_transport_lookup(tt);
4460 if (priv)
4461 return NULL;
4463 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
4464 if (!priv)
4465 return NULL;
4466 INIT_LIST_HEAD(&priv->list);
4467 priv->iscsi_transport = tt;
4468 priv->t.user_scan = iscsi_user_scan;
4469 priv->t.create_work_queue = 1;
4471 priv->dev.class = &iscsi_transport_class;
4472 dev_set_name(&priv->dev, "%s", tt->name);
4473 err = device_register(&priv->dev);
4474 if (err)
4475 goto free_priv;
4477 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
4478 if (err)
4479 goto unregister_dev;
4481 /* host parameters */
4482 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
4483 priv->t.host_attrs.ac.match = iscsi_host_match;
4484 priv->t.host_attrs.ac.grp = &iscsi_host_group;
4485 priv->t.host_size = sizeof(struct iscsi_cls_host);
4486 transport_container_register(&priv->t.host_attrs);
4488 /* connection parameters */
4489 priv->conn_cont.ac.class = &iscsi_connection_class.class;
4490 priv->conn_cont.ac.match = iscsi_conn_match;
4491 priv->conn_cont.ac.grp = &iscsi_conn_group;
4492 transport_container_register(&priv->conn_cont);
4494 /* session parameters */
4495 priv->session_cont.ac.class = &iscsi_session_class.class;
4496 priv->session_cont.ac.match = iscsi_session_match;
4497 priv->session_cont.ac.grp = &iscsi_session_group;
4498 transport_container_register(&priv->session_cont);
4500 spin_lock_irqsave(&iscsi_transport_lock, flags);
4501 list_add(&priv->list, &iscsi_transports);
4502 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
4504 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
4505 return &priv->t;
4507 unregister_dev:
4508 device_unregister(&priv->dev);
4509 return NULL;
4510 free_priv:
4511 kfree(priv);
4512 return NULL;
4514 EXPORT_SYMBOL_GPL(iscsi_register_transport);
4516 int iscsi_unregister_transport(struct iscsi_transport *tt)
4518 struct iscsi_internal *priv;
4519 unsigned long flags;
4521 BUG_ON(!tt);
4523 mutex_lock(&rx_queue_mutex);
4525 priv = iscsi_if_transport_lookup(tt);
4526 BUG_ON (!priv);
4528 spin_lock_irqsave(&iscsi_transport_lock, flags);
4529 list_del(&priv->list);
4530 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
4532 transport_container_unregister(&priv->conn_cont);
4533 transport_container_unregister(&priv->session_cont);
4534 transport_container_unregister(&priv->t.host_attrs);
4536 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
4537 device_unregister(&priv->dev);
4538 mutex_unlock(&rx_queue_mutex);
4540 return 0;
4542 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
4544 static __init int iscsi_transport_init(void)
4546 int err;
4547 struct netlink_kernel_cfg cfg = {
4548 .groups = 1,
4549 .input = iscsi_if_rx,
4551 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
4552 ISCSI_TRANSPORT_VERSION);
4554 atomic_set(&iscsi_session_nr, 0);
4556 err = class_register(&iscsi_transport_class);
4557 if (err)
4558 return err;
4560 err = class_register(&iscsi_endpoint_class);
4561 if (err)
4562 goto unregister_transport_class;
4564 err = class_register(&iscsi_iface_class);
4565 if (err)
4566 goto unregister_endpoint_class;
4568 err = transport_class_register(&iscsi_host_class);
4569 if (err)
4570 goto unregister_iface_class;
4572 err = transport_class_register(&iscsi_connection_class);
4573 if (err)
4574 goto unregister_host_class;
4576 err = transport_class_register(&iscsi_session_class);
4577 if (err)
4578 goto unregister_conn_class;
4580 err = bus_register(&iscsi_flashnode_bus);
4581 if (err)
4582 goto unregister_session_class;
4584 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, &cfg);
4585 if (!nls) {
4586 err = -ENOBUFS;
4587 goto unregister_flashnode_bus;
4590 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
4591 if (!iscsi_eh_timer_workq) {
4592 err = -ENOMEM;
4593 goto release_nls;
4596 return 0;
4598 release_nls:
4599 netlink_kernel_release(nls);
4600 unregister_flashnode_bus:
4601 bus_unregister(&iscsi_flashnode_bus);
4602 unregister_session_class:
4603 transport_class_unregister(&iscsi_session_class);
4604 unregister_conn_class:
4605 transport_class_unregister(&iscsi_connection_class);
4606 unregister_host_class:
4607 transport_class_unregister(&iscsi_host_class);
4608 unregister_iface_class:
4609 class_unregister(&iscsi_iface_class);
4610 unregister_endpoint_class:
4611 class_unregister(&iscsi_endpoint_class);
4612 unregister_transport_class:
4613 class_unregister(&iscsi_transport_class);
4614 return err;
4617 static void __exit iscsi_transport_exit(void)
4619 destroy_workqueue(iscsi_eh_timer_workq);
4620 netlink_kernel_release(nls);
4621 bus_unregister(&iscsi_flashnode_bus);
4622 transport_class_unregister(&iscsi_connection_class);
4623 transport_class_unregister(&iscsi_session_class);
4624 transport_class_unregister(&iscsi_host_class);
4625 class_unregister(&iscsi_endpoint_class);
4626 class_unregister(&iscsi_iface_class);
4627 class_unregister(&iscsi_transport_class);
4630 module_init(iscsi_transport_init);
4631 module_exit(iscsi_transport_exit);
4633 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
4634 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
4635 "Alex Aizman <itn780@yahoo.com>");
4636 MODULE_DESCRIPTION("iSCSI Transport Interface");
4637 MODULE_LICENSE("GPL");
4638 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
4639 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);