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>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_transport.h>
31 #include <scsi/scsi_transport_iscsi.h>
32 #include <scsi/iscsi_if.h>
33 #include <scsi/scsi_cmnd.h>
35 #define ISCSI_TRANSPORT_VERSION "2.0-870"
37 static int dbg_session
;
38 module_param_named(debug_session
, dbg_session
, int,
40 MODULE_PARM_DESC(debug_session
,
41 "Turn on debugging for sessions in scsi_transport_iscsi "
42 "module. Set to 1 to turn on, and zero to turn off. Default "
46 module_param_named(debug_conn
, dbg_conn
, int,
48 MODULE_PARM_DESC(debug_conn
,
49 "Turn on debugging for connections in scsi_transport_iscsi "
50 "module. Set to 1 to turn on, and zero to turn off. Default "
53 #define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
56 iscsi_cls_session_printk(KERN_INFO, _session, \
61 #define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
64 iscsi_cls_conn_printk(KERN_INFO, _conn, \
69 struct iscsi_internal
{
70 struct scsi_transport_template t
;
71 struct iscsi_transport
*iscsi_transport
;
72 struct list_head list
;
75 struct transport_container conn_cont
;
76 struct transport_container session_cont
;
79 static atomic_t iscsi_session_nr
; /* sysfs session id for next new session */
80 static struct workqueue_struct
*iscsi_eh_timer_workq
;
83 * list of registered transports and lock that must
84 * be held while accessing list. The iscsi_transport_lock must
85 * be acquired after the rx_queue_mutex.
87 static LIST_HEAD(iscsi_transports
);
88 static DEFINE_SPINLOCK(iscsi_transport_lock
);
90 #define to_iscsi_internal(tmpl) \
91 container_of(tmpl, struct iscsi_internal, t)
93 #define dev_to_iscsi_internal(_dev) \
94 container_of(_dev, struct iscsi_internal, dev)
96 static void iscsi_transport_release(struct device
*dev
)
98 struct iscsi_internal
*priv
= dev_to_iscsi_internal(dev
);
103 * iscsi_transport_class represents the iscsi_transports that are
106 static struct class iscsi_transport_class
= {
107 .name
= "iscsi_transport",
108 .dev_release
= iscsi_transport_release
,
112 show_transport_handle(struct device
*dev
, struct device_attribute
*attr
,
115 struct iscsi_internal
*priv
= dev_to_iscsi_internal(dev
);
116 return sprintf(buf
, "%llu\n", (unsigned long long)iscsi_handle(priv
->iscsi_transport
));
118 static DEVICE_ATTR(handle
, S_IRUGO
, show_transport_handle
, NULL
);
120 #define show_transport_attr(name, format) \
122 show_transport_##name(struct device *dev, \
123 struct device_attribute *attr,char *buf) \
125 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
126 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
128 static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
130 show_transport_attr(caps
, "0x%x");
132 static struct attribute
*iscsi_transport_attrs
[] = {
133 &dev_attr_handle
.attr
,
138 static struct attribute_group iscsi_transport_group
= {
139 .attrs
= iscsi_transport_attrs
,
143 * iSCSI endpoint attrs
145 #define iscsi_dev_to_endpoint(_dev) \
146 container_of(_dev, struct iscsi_endpoint, dev)
148 #define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
149 struct device_attribute dev_attr_##_prefix##_##_name = \
150 __ATTR(_name,_mode,_show,_store)
152 static void iscsi_endpoint_release(struct device
*dev
)
154 struct iscsi_endpoint
*ep
= iscsi_dev_to_endpoint(dev
);
158 static struct class iscsi_endpoint_class
= {
159 .name
= "iscsi_endpoint",
160 .dev_release
= iscsi_endpoint_release
,
164 show_ep_handle(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
166 struct iscsi_endpoint
*ep
= iscsi_dev_to_endpoint(dev
);
167 return sprintf(buf
, "%llu\n", (unsigned long long) ep
->id
);
169 static ISCSI_ATTR(ep
, handle
, S_IRUGO
, show_ep_handle
, NULL
);
171 static struct attribute
*iscsi_endpoint_attrs
[] = {
172 &dev_attr_ep_handle
.attr
,
176 static struct attribute_group iscsi_endpoint_group
= {
177 .attrs
= iscsi_endpoint_attrs
,
180 #define ISCSI_MAX_EPID -1
182 static int iscsi_match_epid(struct device
*dev
, void *data
)
184 struct iscsi_endpoint
*ep
= iscsi_dev_to_endpoint(dev
);
185 uint64_t *epid
= (uint64_t *) data
;
187 return *epid
== ep
->id
;
190 struct iscsi_endpoint
*
191 iscsi_create_endpoint(int dd_size
)
194 struct iscsi_endpoint
*ep
;
198 for (id
= 1; id
< ISCSI_MAX_EPID
; id
++) {
199 dev
= class_find_device(&iscsi_endpoint_class
, NULL
, &id
,
204 if (id
== ISCSI_MAX_EPID
) {
205 printk(KERN_ERR
"Too many connections. Max supported %u\n",
210 ep
= kzalloc(sizeof(*ep
) + dd_size
, GFP_KERNEL
);
215 ep
->dev
.class = &iscsi_endpoint_class
;
216 dev_set_name(&ep
->dev
, "ep-%llu", (unsigned long long) id
);
217 err
= device_register(&ep
->dev
);
221 err
= sysfs_create_group(&ep
->dev
.kobj
, &iscsi_endpoint_group
);
226 ep
->dd_data
= &ep
[1];
230 device_unregister(&ep
->dev
);
237 EXPORT_SYMBOL_GPL(iscsi_create_endpoint
);
239 void iscsi_destroy_endpoint(struct iscsi_endpoint
*ep
)
241 sysfs_remove_group(&ep
->dev
.kobj
, &iscsi_endpoint_group
);
242 device_unregister(&ep
->dev
);
244 EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint
);
246 struct iscsi_endpoint
*iscsi_lookup_endpoint(u64 handle
)
248 struct iscsi_endpoint
*ep
;
251 dev
= class_find_device(&iscsi_endpoint_class
, NULL
, &handle
,
256 ep
= iscsi_dev_to_endpoint(dev
);
258 * we can drop this now because the interface will prevent
259 * removals and lookups from racing.
264 EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint
);
267 * Interface to display network param to sysfs
270 static void iscsi_iface_release(struct device
*dev
)
272 struct iscsi_iface
*iface
= iscsi_dev_to_iface(dev
);
273 struct device
*parent
= iface
->dev
.parent
;
280 static struct class iscsi_iface_class
= {
281 .name
= "iscsi_iface",
282 .dev_release
= iscsi_iface_release
,
285 #define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
286 struct device_attribute dev_attr_##_prefix##_##_name = \
287 __ATTR(_name, _mode, _show, _store)
289 /* iface attrs show */
290 #define iscsi_iface_attr_show(type, name, param_type, param) \
292 show_##type##_##name(struct device *dev, struct device_attribute *attr, \
295 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
296 struct iscsi_transport *t = iface->transport; \
297 return t->get_iface_param(iface, param_type, param, buf); \
300 #define iscsi_iface_net_attr(type, name, param) \
301 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
302 static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
304 /* generic read only ipvi4 attribute */
305 iscsi_iface_net_attr(ipv4_iface
, ipaddress
, ISCSI_NET_PARAM_IPV4_ADDR
);
306 iscsi_iface_net_attr(ipv4_iface
, gateway
, ISCSI_NET_PARAM_IPV4_GW
);
307 iscsi_iface_net_attr(ipv4_iface
, subnet
, ISCSI_NET_PARAM_IPV4_SUBNET
);
308 iscsi_iface_net_attr(ipv4_iface
, bootproto
, ISCSI_NET_PARAM_IPV4_BOOTPROTO
);
310 /* generic read only ipv6 attribute */
311 iscsi_iface_net_attr(ipv6_iface
, ipaddress
, ISCSI_NET_PARAM_IPV6_ADDR
);
312 iscsi_iface_net_attr(ipv6_iface
, link_local_addr
, ISCSI_NET_PARAM_IPV6_LINKLOCAL
);
313 iscsi_iface_net_attr(ipv6_iface
, router_addr
, ISCSI_NET_PARAM_IPV6_ROUTER
);
314 iscsi_iface_net_attr(ipv6_iface
, ipaddr_autocfg
,
315 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG
);
316 iscsi_iface_net_attr(ipv6_iface
, linklocal_autocfg
,
317 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG
);
319 /* common read only iface attribute */
320 iscsi_iface_net_attr(iface
, enabled
, ISCSI_NET_PARAM_IFACE_ENABLE
);
321 iscsi_iface_net_attr(iface
, vlan
, ISCSI_NET_PARAM_VLAN_ID
);
322 iscsi_iface_net_attr(iface
, vlan_priority
, ISCSI_NET_PARAM_VLAN_PRIORITY
);
323 iscsi_iface_net_attr(iface
, vlan_enabled
, ISCSI_NET_PARAM_VLAN_ENABLED
);
325 static mode_t
iscsi_iface_attr_is_visible(struct kobject
*kobj
,
326 struct attribute
*attr
, int i
)
328 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
329 struct iscsi_iface
*iface
= iscsi_dev_to_iface(dev
);
330 struct iscsi_transport
*t
= iface
->transport
;
333 if (attr
== &dev_attr_iface_enabled
.attr
)
334 param
= ISCSI_NET_PARAM_IFACE_ENABLE
;
335 else if (attr
== &dev_attr_iface_vlan
.attr
)
336 param
= ISCSI_NET_PARAM_VLAN_ID
;
337 else if (attr
== &dev_attr_iface_vlan_priority
.attr
)
338 param
= ISCSI_NET_PARAM_VLAN_PRIORITY
;
339 else if (attr
== &dev_attr_iface_vlan_enabled
.attr
)
340 param
= ISCSI_NET_PARAM_VLAN_ENABLED
;
341 else if (iface
->iface_type
== ISCSI_IFACE_TYPE_IPV4
) {
342 if (attr
== &dev_attr_ipv4_iface_ipaddress
.attr
)
343 param
= ISCSI_NET_PARAM_IPV4_ADDR
;
344 else if (attr
== &dev_attr_ipv4_iface_gateway
.attr
)
345 param
= ISCSI_NET_PARAM_IPV4_GW
;
346 else if (attr
== &dev_attr_ipv4_iface_subnet
.attr
)
347 param
= ISCSI_NET_PARAM_IPV4_SUBNET
;
348 else if (attr
== &dev_attr_ipv4_iface_bootproto
.attr
)
349 param
= ISCSI_NET_PARAM_IPV4_BOOTPROTO
;
352 } else if (iface
->iface_type
== ISCSI_IFACE_TYPE_IPV6
) {
353 if (attr
== &dev_attr_ipv6_iface_ipaddress
.attr
)
354 param
= ISCSI_NET_PARAM_IPV6_ADDR
;
355 else if (attr
== &dev_attr_ipv6_iface_link_local_addr
.attr
)
356 param
= ISCSI_NET_PARAM_IPV6_LINKLOCAL
;
357 else if (attr
== &dev_attr_ipv6_iface_router_addr
.attr
)
358 param
= ISCSI_NET_PARAM_IPV6_ROUTER
;
359 else if (attr
== &dev_attr_ipv6_iface_ipaddr_autocfg
.attr
)
360 param
= ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG
;
361 else if (attr
== &dev_attr_ipv6_iface_linklocal_autocfg
.attr
)
362 param
= ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG
;
366 WARN_ONCE(1, "Invalid iface attr");
370 return t
->attr_is_visible(ISCSI_NET_PARAM
, param
);
373 static struct attribute
*iscsi_iface_attrs
[] = {
374 &dev_attr_iface_enabled
.attr
,
375 &dev_attr_iface_vlan
.attr
,
376 &dev_attr_iface_vlan_priority
.attr
,
377 &dev_attr_iface_vlan_enabled
.attr
,
378 &dev_attr_ipv4_iface_ipaddress
.attr
,
379 &dev_attr_ipv4_iface_gateway
.attr
,
380 &dev_attr_ipv4_iface_subnet
.attr
,
381 &dev_attr_ipv4_iface_bootproto
.attr
,
382 &dev_attr_ipv6_iface_ipaddress
.attr
,
383 &dev_attr_ipv6_iface_link_local_addr
.attr
,
384 &dev_attr_ipv6_iface_router_addr
.attr
,
385 &dev_attr_ipv6_iface_ipaddr_autocfg
.attr
,
386 &dev_attr_ipv6_iface_linklocal_autocfg
.attr
,
390 static struct attribute_group iscsi_iface_group
= {
391 .attrs
= iscsi_iface_attrs
,
392 .is_visible
= iscsi_iface_attr_is_visible
,
396 iscsi_create_iface(struct Scsi_Host
*shost
, struct iscsi_transport
*transport
,
397 uint32_t iface_type
, uint32_t iface_num
, int dd_size
)
399 struct iscsi_iface
*iface
;
402 iface
= kzalloc(sizeof(*iface
) + dd_size
, GFP_KERNEL
);
406 iface
->transport
= transport
;
407 iface
->iface_type
= iface_type
;
408 iface
->iface_num
= iface_num
;
409 iface
->dev
.release
= iscsi_iface_release
;
410 iface
->dev
.class = &iscsi_iface_class
;
411 /* parent reference released in iscsi_iface_release */
412 iface
->dev
.parent
= get_device(&shost
->shost_gendev
);
413 if (iface_type
== ISCSI_IFACE_TYPE_IPV4
)
414 dev_set_name(&iface
->dev
, "ipv4-iface-%u-%u", shost
->host_no
,
417 dev_set_name(&iface
->dev
, "ipv6-iface-%u-%u", shost
->host_no
,
420 err
= device_register(&iface
->dev
);
424 err
= sysfs_create_group(&iface
->dev
.kobj
, &iscsi_iface_group
);
429 iface
->dd_data
= &iface
[1];
433 device_unregister(&iface
->dev
);
437 put_device(iface
->dev
.parent
);
441 EXPORT_SYMBOL_GPL(iscsi_create_iface
);
443 void iscsi_destroy_iface(struct iscsi_iface
*iface
)
445 sysfs_remove_group(&iface
->dev
.kobj
, &iscsi_iface_group
);
446 device_unregister(&iface
->dev
);
448 EXPORT_SYMBOL_GPL(iscsi_destroy_iface
);
450 static int iscsi_setup_host(struct transport_container
*tc
, struct device
*dev
,
453 struct Scsi_Host
*shost
= dev_to_shost(dev
);
454 struct iscsi_cls_host
*ihost
= shost
->shost_data
;
456 memset(ihost
, 0, sizeof(*ihost
));
457 atomic_set(&ihost
->nr_scans
, 0);
458 mutex_init(&ihost
->mutex
);
462 static DECLARE_TRANSPORT_CLASS(iscsi_host_class
,
468 static DECLARE_TRANSPORT_CLASS(iscsi_session_class
,
474 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class
,
480 static struct sock
*nls
;
481 static DEFINE_MUTEX(rx_queue_mutex
);
483 static LIST_HEAD(sesslist
);
484 static DEFINE_SPINLOCK(sesslock
);
485 static LIST_HEAD(connlist
);
486 static DEFINE_SPINLOCK(connlock
);
488 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn
*conn
)
490 struct iscsi_cls_session
*sess
= iscsi_dev_to_session(conn
->dev
.parent
);
495 * Returns the matching session to a given sid
497 static struct iscsi_cls_session
*iscsi_session_lookup(uint32_t sid
)
500 struct iscsi_cls_session
*sess
;
502 spin_lock_irqsave(&sesslock
, flags
);
503 list_for_each_entry(sess
, &sesslist
, sess_list
) {
504 if (sess
->sid
== sid
) {
505 spin_unlock_irqrestore(&sesslock
, flags
);
509 spin_unlock_irqrestore(&sesslock
, flags
);
514 * Returns the matching connection to a given sid / cid tuple
516 static struct iscsi_cls_conn
*iscsi_conn_lookup(uint32_t sid
, uint32_t cid
)
519 struct iscsi_cls_conn
*conn
;
521 spin_lock_irqsave(&connlock
, flags
);
522 list_for_each_entry(conn
, &connlist
, conn_list
) {
523 if ((conn
->cid
== cid
) && (iscsi_conn_get_sid(conn
) == sid
)) {
524 spin_unlock_irqrestore(&connlock
, flags
);
528 spin_unlock_irqrestore(&connlock
, flags
);
533 * The following functions can be used by LLDs that allocate
534 * their own scsi_hosts or by software iscsi LLDs
539 } iscsi_session_state_names
[] = {
540 { ISCSI_SESSION_LOGGED_IN
, "LOGGED_IN" },
541 { ISCSI_SESSION_FAILED
, "FAILED" },
542 { ISCSI_SESSION_FREE
, "FREE" },
545 static const char *iscsi_session_state_name(int state
)
550 for (i
= 0; i
< ARRAY_SIZE(iscsi_session_state_names
); i
++) {
551 if (iscsi_session_state_names
[i
].value
== state
) {
552 name
= iscsi_session_state_names
[i
].name
;
559 int iscsi_session_chkready(struct iscsi_cls_session
*session
)
564 spin_lock_irqsave(&session
->lock
, flags
);
565 switch (session
->state
) {
566 case ISCSI_SESSION_LOGGED_IN
:
569 case ISCSI_SESSION_FAILED
:
570 err
= DID_IMM_RETRY
<< 16;
572 case ISCSI_SESSION_FREE
:
573 err
= DID_TRANSPORT_FAILFAST
<< 16;
576 err
= DID_NO_CONNECT
<< 16;
579 spin_unlock_irqrestore(&session
->lock
, flags
);
582 EXPORT_SYMBOL_GPL(iscsi_session_chkready
);
584 static void iscsi_session_release(struct device
*dev
)
586 struct iscsi_cls_session
*session
= iscsi_dev_to_session(dev
);
587 struct Scsi_Host
*shost
;
589 shost
= iscsi_session_to_shost(session
);
590 scsi_host_put(shost
);
591 ISCSI_DBG_TRANS_SESSION(session
, "Completing session release\n");
595 static int iscsi_is_session_dev(const struct device
*dev
)
597 return dev
->release
== iscsi_session_release
;
600 static int iscsi_iter_session_fn(struct device
*dev
, void *data
)
602 void (* fn
) (struct iscsi_cls_session
*) = data
;
604 if (!iscsi_is_session_dev(dev
))
606 fn(iscsi_dev_to_session(dev
));
610 void iscsi_host_for_each_session(struct Scsi_Host
*shost
,
611 void (*fn
)(struct iscsi_cls_session
*))
613 device_for_each_child(&shost
->shost_gendev
, fn
,
614 iscsi_iter_session_fn
);
616 EXPORT_SYMBOL_GPL(iscsi_host_for_each_session
);
619 * iscsi_scan_finished - helper to report when running scans are done
621 * @time: scan run time
623 * This function can be used by drives like qla4xxx to report to the scsi
624 * layer when the scans it kicked off at module load time are done.
626 int iscsi_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
628 struct iscsi_cls_host
*ihost
= shost
->shost_data
;
630 * qla4xxx will have kicked off some session unblocks before calling
631 * scsi_scan_host, so just wait for them to complete.
633 return !atomic_read(&ihost
->nr_scans
);
635 EXPORT_SYMBOL_GPL(iscsi_scan_finished
);
637 struct iscsi_scan_data
{
638 unsigned int channel
;
643 static int iscsi_user_scan_session(struct device
*dev
, void *data
)
645 struct iscsi_scan_data
*scan_data
= data
;
646 struct iscsi_cls_session
*session
;
647 struct Scsi_Host
*shost
;
648 struct iscsi_cls_host
*ihost
;
652 if (!iscsi_is_session_dev(dev
))
655 session
= iscsi_dev_to_session(dev
);
657 ISCSI_DBG_TRANS_SESSION(session
, "Scanning session\n");
659 shost
= iscsi_session_to_shost(session
);
660 ihost
= shost
->shost_data
;
662 mutex_lock(&ihost
->mutex
);
663 spin_lock_irqsave(&session
->lock
, flags
);
664 if (session
->state
!= ISCSI_SESSION_LOGGED_IN
) {
665 spin_unlock_irqrestore(&session
->lock
, flags
);
668 id
= session
->target_id
;
669 spin_unlock_irqrestore(&session
->lock
, flags
);
671 if (id
!= ISCSI_MAX_TARGET
) {
672 if ((scan_data
->channel
== SCAN_WILD_CARD
||
673 scan_data
->channel
== 0) &&
674 (scan_data
->id
== SCAN_WILD_CARD
||
675 scan_data
->id
== id
))
676 scsi_scan_target(&session
->dev
, 0, id
,
681 mutex_unlock(&ihost
->mutex
);
682 ISCSI_DBG_TRANS_SESSION(session
, "Completed session scan\n");
686 static int iscsi_user_scan(struct Scsi_Host
*shost
, uint channel
,
689 struct iscsi_scan_data scan_data
;
691 scan_data
.channel
= channel
;
695 return device_for_each_child(&shost
->shost_gendev
, &scan_data
,
696 iscsi_user_scan_session
);
699 static void iscsi_scan_session(struct work_struct
*work
)
701 struct iscsi_cls_session
*session
=
702 container_of(work
, struct iscsi_cls_session
, scan_work
);
703 struct Scsi_Host
*shost
= iscsi_session_to_shost(session
);
704 struct iscsi_cls_host
*ihost
= shost
->shost_data
;
705 struct iscsi_scan_data scan_data
;
707 scan_data
.channel
= 0;
708 scan_data
.id
= SCAN_WILD_CARD
;
709 scan_data
.lun
= SCAN_WILD_CARD
;
711 iscsi_user_scan_session(&session
->dev
, &scan_data
);
712 atomic_dec(&ihost
->nr_scans
);
716 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
717 * @cmd: scsi cmd passed to scsi eh handler
719 * If the session is down this function will wait for the recovery
720 * timer to fire or for the session to be logged back in. If the
721 * recovery timer fires then FAST_IO_FAIL is returned. The caller
722 * should pass this error value to the scsi eh.
724 int iscsi_block_scsi_eh(struct scsi_cmnd
*cmd
)
726 struct iscsi_cls_session
*session
=
727 starget_to_session(scsi_target(cmd
->device
));
731 spin_lock_irqsave(&session
->lock
, flags
);
732 while (session
->state
!= ISCSI_SESSION_LOGGED_IN
) {
733 if (session
->state
== ISCSI_SESSION_FREE
) {
737 spin_unlock_irqrestore(&session
->lock
, flags
);
739 spin_lock_irqsave(&session
->lock
, flags
);
741 spin_unlock_irqrestore(&session
->lock
, flags
);
744 EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh
);
746 static void session_recovery_timedout(struct work_struct
*work
)
748 struct iscsi_cls_session
*session
=
749 container_of(work
, struct iscsi_cls_session
,
753 iscsi_cls_session_printk(KERN_INFO
, session
,
754 "session recovery timed out after %d secs\n",
755 session
->recovery_tmo
);
757 spin_lock_irqsave(&session
->lock
, flags
);
758 switch (session
->state
) {
759 case ISCSI_SESSION_FAILED
:
760 session
->state
= ISCSI_SESSION_FREE
;
762 case ISCSI_SESSION_LOGGED_IN
:
763 case ISCSI_SESSION_FREE
:
764 /* we raced with the unblock's flush */
765 spin_unlock_irqrestore(&session
->lock
, flags
);
768 spin_unlock_irqrestore(&session
->lock
, flags
);
770 if (session
->transport
->session_recovery_timedout
)
771 session
->transport
->session_recovery_timedout(session
);
773 ISCSI_DBG_TRANS_SESSION(session
, "Unblocking SCSI target\n");
774 scsi_target_unblock(&session
->dev
);
775 ISCSI_DBG_TRANS_SESSION(session
, "Completed unblocking SCSI target\n");
778 static void __iscsi_unblock_session(struct work_struct
*work
)
780 struct iscsi_cls_session
*session
=
781 container_of(work
, struct iscsi_cls_session
,
783 struct Scsi_Host
*shost
= iscsi_session_to_shost(session
);
784 struct iscsi_cls_host
*ihost
= shost
->shost_data
;
787 ISCSI_DBG_TRANS_SESSION(session
, "Unblocking session\n");
789 * The recovery and unblock work get run from the same workqueue,
790 * so try to cancel it if it was going to run after this unblock.
792 cancel_delayed_work(&session
->recovery_work
);
793 spin_lock_irqsave(&session
->lock
, flags
);
794 session
->state
= ISCSI_SESSION_LOGGED_IN
;
795 spin_unlock_irqrestore(&session
->lock
, flags
);
797 scsi_target_unblock(&session
->dev
);
799 * Only do kernel scanning if the driver is properly hooked into
800 * the async scanning code (drivers like iscsi_tcp do login and
801 * scanning from userspace).
803 if (shost
->hostt
->scan_finished
) {
804 if (scsi_queue_work(shost
, &session
->scan_work
))
805 atomic_inc(&ihost
->nr_scans
);
807 ISCSI_DBG_TRANS_SESSION(session
, "Completed unblocking session\n");
811 * iscsi_unblock_session - set a session as logged in and start IO.
812 * @session: iscsi session
814 * Mark a session as ready to accept IO.
816 void iscsi_unblock_session(struct iscsi_cls_session
*session
)
818 queue_work(iscsi_eh_timer_workq
, &session
->unblock_work
);
820 * make sure all the events have completed before tell the driver
823 flush_workqueue(iscsi_eh_timer_workq
);
825 EXPORT_SYMBOL_GPL(iscsi_unblock_session
);
827 static void __iscsi_block_session(struct work_struct
*work
)
829 struct iscsi_cls_session
*session
=
830 container_of(work
, struct iscsi_cls_session
,
834 ISCSI_DBG_TRANS_SESSION(session
, "Blocking session\n");
835 spin_lock_irqsave(&session
->lock
, flags
);
836 session
->state
= ISCSI_SESSION_FAILED
;
837 spin_unlock_irqrestore(&session
->lock
, flags
);
838 scsi_target_block(&session
->dev
);
839 ISCSI_DBG_TRANS_SESSION(session
, "Completed SCSI target blocking\n");
840 if (session
->recovery_tmo
>= 0)
841 queue_delayed_work(iscsi_eh_timer_workq
,
842 &session
->recovery_work
,
843 session
->recovery_tmo
* HZ
);
846 void iscsi_block_session(struct iscsi_cls_session
*session
)
848 queue_work(iscsi_eh_timer_workq
, &session
->block_work
);
850 EXPORT_SYMBOL_GPL(iscsi_block_session
);
852 static void __iscsi_unbind_session(struct work_struct
*work
)
854 struct iscsi_cls_session
*session
=
855 container_of(work
, struct iscsi_cls_session
,
857 struct Scsi_Host
*shost
= iscsi_session_to_shost(session
);
858 struct iscsi_cls_host
*ihost
= shost
->shost_data
;
861 ISCSI_DBG_TRANS_SESSION(session
, "Unbinding session\n");
863 /* Prevent new scans and make sure scanning is not in progress */
864 mutex_lock(&ihost
->mutex
);
865 spin_lock_irqsave(&session
->lock
, flags
);
866 if (session
->target_id
== ISCSI_MAX_TARGET
) {
867 spin_unlock_irqrestore(&session
->lock
, flags
);
868 mutex_unlock(&ihost
->mutex
);
871 session
->target_id
= ISCSI_MAX_TARGET
;
872 spin_unlock_irqrestore(&session
->lock
, flags
);
873 mutex_unlock(&ihost
->mutex
);
875 scsi_remove_target(&session
->dev
);
876 iscsi_session_event(session
, ISCSI_KEVENT_UNBIND_SESSION
);
877 ISCSI_DBG_TRANS_SESSION(session
, "Completed target removal\n");
880 struct iscsi_cls_session
*
881 iscsi_alloc_session(struct Scsi_Host
*shost
, struct iscsi_transport
*transport
,
884 struct iscsi_cls_session
*session
;
886 session
= kzalloc(sizeof(*session
) + dd_size
,
891 session
->transport
= transport
;
892 session
->recovery_tmo
= 120;
893 session
->state
= ISCSI_SESSION_FREE
;
894 INIT_DELAYED_WORK(&session
->recovery_work
, session_recovery_timedout
);
895 INIT_LIST_HEAD(&session
->sess_list
);
896 INIT_WORK(&session
->unblock_work
, __iscsi_unblock_session
);
897 INIT_WORK(&session
->block_work
, __iscsi_block_session
);
898 INIT_WORK(&session
->unbind_work
, __iscsi_unbind_session
);
899 INIT_WORK(&session
->scan_work
, iscsi_scan_session
);
900 spin_lock_init(&session
->lock
);
902 /* this is released in the dev's release function */
903 scsi_host_get(shost
);
904 session
->dev
.parent
= &shost
->shost_gendev
;
905 session
->dev
.release
= iscsi_session_release
;
906 device_initialize(&session
->dev
);
908 session
->dd_data
= &session
[1];
910 ISCSI_DBG_TRANS_SESSION(session
, "Completed session allocation\n");
913 EXPORT_SYMBOL_GPL(iscsi_alloc_session
);
915 static int iscsi_get_next_target_id(struct device
*dev
, void *data
)
917 struct iscsi_cls_session
*session
;
921 if (!iscsi_is_session_dev(dev
))
924 session
= iscsi_dev_to_session(dev
);
925 spin_lock_irqsave(&session
->lock
, flags
);
926 if (*((unsigned int *) data
) == session
->target_id
)
928 spin_unlock_irqrestore(&session
->lock
, flags
);
932 int iscsi_add_session(struct iscsi_cls_session
*session
, unsigned int target_id
)
934 struct Scsi_Host
*shost
= iscsi_session_to_shost(session
);
935 struct iscsi_cls_host
*ihost
;
937 unsigned int id
= target_id
;
940 ihost
= shost
->shost_data
;
941 session
->sid
= atomic_add_return(1, &iscsi_session_nr
);
943 if (id
== ISCSI_MAX_TARGET
) {
944 for (id
= 0; id
< ISCSI_MAX_TARGET
; id
++) {
945 err
= device_for_each_child(&shost
->shost_gendev
, &id
,
946 iscsi_get_next_target_id
);
951 if (id
== ISCSI_MAX_TARGET
) {
952 iscsi_cls_session_printk(KERN_ERR
, session
,
953 "Too many iscsi targets. Max "
954 "number of targets is %d.\n",
955 ISCSI_MAX_TARGET
- 1);
960 session
->target_id
= id
;
962 dev_set_name(&session
->dev
, "session%u", session
->sid
);
963 err
= device_add(&session
->dev
);
965 iscsi_cls_session_printk(KERN_ERR
, session
,
966 "could not register session's dev\n");
969 transport_register_device(&session
->dev
);
971 spin_lock_irqsave(&sesslock
, flags
);
972 list_add(&session
->sess_list
, &sesslist
);
973 spin_unlock_irqrestore(&sesslock
, flags
);
975 iscsi_session_event(session
, ISCSI_KEVENT_CREATE_SESSION
);
976 ISCSI_DBG_TRANS_SESSION(session
, "Completed session adding\n");
980 scsi_host_put(shost
);
983 EXPORT_SYMBOL_GPL(iscsi_add_session
);
986 * iscsi_create_session - create iscsi class session
988 * @transport: iscsi transport
989 * @dd_size: private driver data size
990 * @target_id: which target
992 * This can be called from a LLD or iscsi_transport.
994 struct iscsi_cls_session
*
995 iscsi_create_session(struct Scsi_Host
*shost
, struct iscsi_transport
*transport
,
996 int dd_size
, unsigned int target_id
)
998 struct iscsi_cls_session
*session
;
1000 session
= iscsi_alloc_session(shost
, transport
, dd_size
);
1004 if (iscsi_add_session(session
, target_id
)) {
1005 iscsi_free_session(session
);
1010 EXPORT_SYMBOL_GPL(iscsi_create_session
);
1012 static void iscsi_conn_release(struct device
*dev
)
1014 struct iscsi_cls_conn
*conn
= iscsi_dev_to_conn(dev
);
1015 struct device
*parent
= conn
->dev
.parent
;
1017 ISCSI_DBG_TRANS_CONN(conn
, "Releasing conn\n");
1022 static int iscsi_is_conn_dev(const struct device
*dev
)
1024 return dev
->release
== iscsi_conn_release
;
1027 static int iscsi_iter_destroy_conn_fn(struct device
*dev
, void *data
)
1029 if (!iscsi_is_conn_dev(dev
))
1031 return iscsi_destroy_conn(iscsi_dev_to_conn(dev
));
1034 void iscsi_remove_session(struct iscsi_cls_session
*session
)
1036 struct Scsi_Host
*shost
= iscsi_session_to_shost(session
);
1037 unsigned long flags
;
1040 ISCSI_DBG_TRANS_SESSION(session
, "Removing session\n");
1042 spin_lock_irqsave(&sesslock
, flags
);
1043 list_del(&session
->sess_list
);
1044 spin_unlock_irqrestore(&sesslock
, flags
);
1046 /* make sure there are no blocks/unblocks queued */
1047 flush_workqueue(iscsi_eh_timer_workq
);
1048 /* make sure the timedout callout is not running */
1049 if (!cancel_delayed_work(&session
->recovery_work
))
1050 flush_workqueue(iscsi_eh_timer_workq
);
1052 * If we are blocked let commands flow again. The lld or iscsi
1053 * layer should set up the queuecommand to fail commands.
1054 * We assume that LLD will not be calling block/unblock while
1055 * removing the session.
1057 spin_lock_irqsave(&session
->lock
, flags
);
1058 session
->state
= ISCSI_SESSION_FREE
;
1059 spin_unlock_irqrestore(&session
->lock
, flags
);
1061 scsi_target_unblock(&session
->dev
);
1062 /* flush running scans then delete devices */
1063 scsi_flush_work(shost
);
1064 __iscsi_unbind_session(&session
->unbind_work
);
1066 /* hw iscsi may not have removed all connections from session */
1067 err
= device_for_each_child(&session
->dev
, NULL
,
1068 iscsi_iter_destroy_conn_fn
);
1070 iscsi_cls_session_printk(KERN_ERR
, session
,
1071 "Could not delete all connections "
1072 "for session. Error %d.\n", err
);
1074 transport_unregister_device(&session
->dev
);
1076 ISCSI_DBG_TRANS_SESSION(session
, "Completing session removal\n");
1077 device_del(&session
->dev
);
1079 EXPORT_SYMBOL_GPL(iscsi_remove_session
);
1081 void iscsi_free_session(struct iscsi_cls_session
*session
)
1083 ISCSI_DBG_TRANS_SESSION(session
, "Freeing session\n");
1084 iscsi_session_event(session
, ISCSI_KEVENT_DESTROY_SESSION
);
1085 put_device(&session
->dev
);
1087 EXPORT_SYMBOL_GPL(iscsi_free_session
);
1090 * iscsi_destroy_session - destroy iscsi session
1091 * @session: iscsi_session
1093 * Can be called by a LLD or iscsi_transport. There must not be
1094 * any running connections.
1096 int iscsi_destroy_session(struct iscsi_cls_session
*session
)
1098 iscsi_remove_session(session
);
1099 ISCSI_DBG_TRANS_SESSION(session
, "Completing session destruction\n");
1100 iscsi_free_session(session
);
1103 EXPORT_SYMBOL_GPL(iscsi_destroy_session
);
1106 * iscsi_create_conn - create iscsi class connection
1107 * @session: iscsi cls session
1108 * @dd_size: private driver data size
1109 * @cid: connection id
1111 * This can be called from a LLD or iscsi_transport. The connection
1112 * is child of the session so cid must be unique for all connections
1115 * Since we do not support MCS, cid will normally be zero. In some cases
1116 * for software iscsi we could be trying to preallocate a connection struct
1117 * in which case there could be two connection structs and cid would be
1120 struct iscsi_cls_conn
*
1121 iscsi_create_conn(struct iscsi_cls_session
*session
, int dd_size
, uint32_t cid
)
1123 struct iscsi_transport
*transport
= session
->transport
;
1124 struct iscsi_cls_conn
*conn
;
1125 unsigned long flags
;
1128 conn
= kzalloc(sizeof(*conn
) + dd_size
, GFP_KERNEL
);
1132 conn
->dd_data
= &conn
[1];
1134 mutex_init(&conn
->ep_mutex
);
1135 INIT_LIST_HEAD(&conn
->conn_list
);
1136 conn
->transport
= transport
;
1139 /* this is released in the dev's release function */
1140 if (!get_device(&session
->dev
))
1143 dev_set_name(&conn
->dev
, "connection%d:%u", session
->sid
, cid
);
1144 conn
->dev
.parent
= &session
->dev
;
1145 conn
->dev
.release
= iscsi_conn_release
;
1146 err
= device_register(&conn
->dev
);
1148 iscsi_cls_session_printk(KERN_ERR
, session
, "could not "
1149 "register connection's dev\n");
1150 goto release_parent_ref
;
1152 transport_register_device(&conn
->dev
);
1154 spin_lock_irqsave(&connlock
, flags
);
1155 list_add(&conn
->conn_list
, &connlist
);
1156 spin_unlock_irqrestore(&connlock
, flags
);
1158 ISCSI_DBG_TRANS_CONN(conn
, "Completed conn creation\n");
1162 put_device(&session
->dev
);
1168 EXPORT_SYMBOL_GPL(iscsi_create_conn
);
1171 * iscsi_destroy_conn - destroy iscsi class connection
1172 * @conn: iscsi cls session
1174 * This can be called from a LLD or iscsi_transport.
1176 int iscsi_destroy_conn(struct iscsi_cls_conn
*conn
)
1178 unsigned long flags
;
1180 spin_lock_irqsave(&connlock
, flags
);
1181 list_del(&conn
->conn_list
);
1182 spin_unlock_irqrestore(&connlock
, flags
);
1184 transport_unregister_device(&conn
->dev
);
1185 ISCSI_DBG_TRANS_CONN(conn
, "Completing conn destruction\n");
1186 device_unregister(&conn
->dev
);
1189 EXPORT_SYMBOL_GPL(iscsi_destroy_conn
);
1192 * iscsi interface functions
1194 static struct iscsi_internal
*
1195 iscsi_if_transport_lookup(struct iscsi_transport
*tt
)
1197 struct iscsi_internal
*priv
;
1198 unsigned long flags
;
1200 spin_lock_irqsave(&iscsi_transport_lock
, flags
);
1201 list_for_each_entry(priv
, &iscsi_transports
, list
) {
1202 if (tt
== priv
->iscsi_transport
) {
1203 spin_unlock_irqrestore(&iscsi_transport_lock
, flags
);
1207 spin_unlock_irqrestore(&iscsi_transport_lock
, flags
);
1212 iscsi_multicast_skb(struct sk_buff
*skb
, uint32_t group
, gfp_t gfp
)
1214 return nlmsg_multicast(nls
, skb
, 0, group
, gfp
);
1217 int iscsi_recv_pdu(struct iscsi_cls_conn
*conn
, struct iscsi_hdr
*hdr
,
1218 char *data
, uint32_t data_size
)
1220 struct nlmsghdr
*nlh
;
1221 struct sk_buff
*skb
;
1222 struct iscsi_uevent
*ev
;
1224 struct iscsi_internal
*priv
;
1225 int len
= NLMSG_SPACE(sizeof(*ev
) + sizeof(struct iscsi_hdr
) +
1228 priv
= iscsi_if_transport_lookup(conn
->transport
);
1232 skb
= alloc_skb(len
, GFP_ATOMIC
);
1234 iscsi_conn_error_event(conn
, ISCSI_ERR_CONN_FAILED
);
1235 iscsi_cls_conn_printk(KERN_ERR
, conn
, "can not deliver "
1236 "control PDU: OOM\n");
1240 nlh
= __nlmsg_put(skb
, 0, 0, 0, (len
- sizeof(*nlh
)), 0);
1241 ev
= NLMSG_DATA(nlh
);
1242 memset(ev
, 0, sizeof(*ev
));
1243 ev
->transport_handle
= iscsi_handle(conn
->transport
);
1244 ev
->type
= ISCSI_KEVENT_RECV_PDU
;
1245 ev
->r
.recv_req
.cid
= conn
->cid
;
1246 ev
->r
.recv_req
.sid
= iscsi_conn_get_sid(conn
);
1247 pdu
= (char*)ev
+ sizeof(*ev
);
1248 memcpy(pdu
, hdr
, sizeof(struct iscsi_hdr
));
1249 memcpy(pdu
+ sizeof(struct iscsi_hdr
), data
, data_size
);
1251 return iscsi_multicast_skb(skb
, ISCSI_NL_GRP_ISCSID
, GFP_ATOMIC
);
1253 EXPORT_SYMBOL_GPL(iscsi_recv_pdu
);
1255 int iscsi_offload_mesg(struct Scsi_Host
*shost
,
1256 struct iscsi_transport
*transport
, uint32_t type
,
1257 char *data
, uint16_t data_size
)
1259 struct nlmsghdr
*nlh
;
1260 struct sk_buff
*skb
;
1261 struct iscsi_uevent
*ev
;
1262 int len
= NLMSG_SPACE(sizeof(*ev
) + data_size
);
1264 skb
= alloc_skb(len
, GFP_ATOMIC
);
1266 printk(KERN_ERR
"can not deliver iscsi offload message:OOM\n");
1270 nlh
= __nlmsg_put(skb
, 0, 0, 0, (len
- sizeof(*nlh
)), 0);
1271 ev
= NLMSG_DATA(nlh
);
1272 memset(ev
, 0, sizeof(*ev
));
1274 ev
->transport_handle
= iscsi_handle(transport
);
1276 case ISCSI_KEVENT_PATH_REQ
:
1277 ev
->r
.req_path
.host_no
= shost
->host_no
;
1279 case ISCSI_KEVENT_IF_DOWN
:
1280 ev
->r
.notify_if_down
.host_no
= shost
->host_no
;
1284 memcpy((char *)ev
+ sizeof(*ev
), data
, data_size
);
1286 return iscsi_multicast_skb(skb
, ISCSI_NL_GRP_UIP
, GFP_ATOMIC
);
1288 EXPORT_SYMBOL_GPL(iscsi_offload_mesg
);
1290 void iscsi_conn_error_event(struct iscsi_cls_conn
*conn
, enum iscsi_err error
)
1292 struct nlmsghdr
*nlh
;
1293 struct sk_buff
*skb
;
1294 struct iscsi_uevent
*ev
;
1295 struct iscsi_internal
*priv
;
1296 int len
= NLMSG_SPACE(sizeof(*ev
));
1298 priv
= iscsi_if_transport_lookup(conn
->transport
);
1302 skb
= alloc_skb(len
, GFP_ATOMIC
);
1304 iscsi_cls_conn_printk(KERN_ERR
, conn
, "gracefully ignored "
1305 "conn error (%d)\n", error
);
1309 nlh
= __nlmsg_put(skb
, 0, 0, 0, (len
- sizeof(*nlh
)), 0);
1310 ev
= NLMSG_DATA(nlh
);
1311 ev
->transport_handle
= iscsi_handle(conn
->transport
);
1312 ev
->type
= ISCSI_KEVENT_CONN_ERROR
;
1313 ev
->r
.connerror
.error
= error
;
1314 ev
->r
.connerror
.cid
= conn
->cid
;
1315 ev
->r
.connerror
.sid
= iscsi_conn_get_sid(conn
);
1317 iscsi_multicast_skb(skb
, ISCSI_NL_GRP_ISCSID
, GFP_ATOMIC
);
1319 iscsi_cls_conn_printk(KERN_INFO
, conn
, "detected conn error (%d)\n",
1322 EXPORT_SYMBOL_GPL(iscsi_conn_error_event
);
1325 iscsi_if_send_reply(uint32_t group
, int seq
, int type
, int done
, int multi
,
1326 void *payload
, int size
)
1328 struct sk_buff
*skb
;
1329 struct nlmsghdr
*nlh
;
1330 int len
= NLMSG_SPACE(size
);
1331 int flags
= multi
? NLM_F_MULTI
: 0;
1332 int t
= done
? NLMSG_DONE
: type
;
1334 skb
= alloc_skb(len
, GFP_ATOMIC
);
1336 printk(KERN_ERR
"Could not allocate skb to send reply.\n");
1340 nlh
= __nlmsg_put(skb
, 0, 0, t
, (len
- sizeof(*nlh
)), 0);
1341 nlh
->nlmsg_flags
= flags
;
1342 memcpy(NLMSG_DATA(nlh
), payload
, size
);
1343 return iscsi_multicast_skb(skb
, group
, GFP_ATOMIC
);
1347 iscsi_if_get_stats(struct iscsi_transport
*transport
, struct nlmsghdr
*nlh
)
1349 struct iscsi_uevent
*ev
= NLMSG_DATA(nlh
);
1350 struct iscsi_stats
*stats
;
1351 struct sk_buff
*skbstat
;
1352 struct iscsi_cls_conn
*conn
;
1353 struct nlmsghdr
*nlhstat
;
1354 struct iscsi_uevent
*evstat
;
1355 struct iscsi_internal
*priv
;
1356 int len
= NLMSG_SPACE(sizeof(*ev
) +
1357 sizeof(struct iscsi_stats
) +
1358 sizeof(struct iscsi_stats_custom
) *
1359 ISCSI_STATS_CUSTOM_MAX
);
1362 priv
= iscsi_if_transport_lookup(transport
);
1366 conn
= iscsi_conn_lookup(ev
->u
.get_stats
.sid
, ev
->u
.get_stats
.cid
);
1373 skbstat
= alloc_skb(len
, GFP_ATOMIC
);
1375 iscsi_cls_conn_printk(KERN_ERR
, conn
, "can not "
1376 "deliver stats: OOM\n");
1380 nlhstat
= __nlmsg_put(skbstat
, 0, 0, 0,
1381 (len
- sizeof(*nlhstat
)), 0);
1382 evstat
= NLMSG_DATA(nlhstat
);
1383 memset(evstat
, 0, sizeof(*evstat
));
1384 evstat
->transport_handle
= iscsi_handle(conn
->transport
);
1385 evstat
->type
= nlh
->nlmsg_type
;
1386 evstat
->u
.get_stats
.cid
=
1387 ev
->u
.get_stats
.cid
;
1388 evstat
->u
.get_stats
.sid
=
1389 ev
->u
.get_stats
.sid
;
1390 stats
= (struct iscsi_stats
*)
1391 ((char*)evstat
+ sizeof(*evstat
));
1392 memset(stats
, 0, sizeof(*stats
));
1394 transport
->get_stats(conn
, stats
);
1395 actual_size
= NLMSG_SPACE(sizeof(struct iscsi_uevent
) +
1396 sizeof(struct iscsi_stats
) +
1397 sizeof(struct iscsi_stats_custom
) *
1398 stats
->custom_length
);
1399 actual_size
-= sizeof(*nlhstat
);
1400 actual_size
= NLMSG_LENGTH(actual_size
);
1401 skb_trim(skbstat
, NLMSG_ALIGN(actual_size
));
1402 nlhstat
->nlmsg_len
= actual_size
;
1404 err
= iscsi_multicast_skb(skbstat
, ISCSI_NL_GRP_ISCSID
,
1406 } while (err
< 0 && err
!= -ECONNREFUSED
);
1412 * iscsi_session_event - send session destr. completion event
1413 * @session: iscsi class session
1414 * @event: type of event
1416 int iscsi_session_event(struct iscsi_cls_session
*session
,
1417 enum iscsi_uevent_e event
)
1419 struct iscsi_internal
*priv
;
1420 struct Scsi_Host
*shost
;
1421 struct iscsi_uevent
*ev
;
1422 struct sk_buff
*skb
;
1423 struct nlmsghdr
*nlh
;
1424 int rc
, len
= NLMSG_SPACE(sizeof(*ev
));
1426 priv
= iscsi_if_transport_lookup(session
->transport
);
1429 shost
= iscsi_session_to_shost(session
);
1431 skb
= alloc_skb(len
, GFP_KERNEL
);
1433 iscsi_cls_session_printk(KERN_ERR
, session
,
1434 "Cannot notify userspace of session "
1435 "event %u\n", event
);
1439 nlh
= __nlmsg_put(skb
, 0, 0, 0, (len
- sizeof(*nlh
)), 0);
1440 ev
= NLMSG_DATA(nlh
);
1441 ev
->transport_handle
= iscsi_handle(session
->transport
);
1445 case ISCSI_KEVENT_DESTROY_SESSION
:
1446 ev
->r
.d_session
.host_no
= shost
->host_no
;
1447 ev
->r
.d_session
.sid
= session
->sid
;
1449 case ISCSI_KEVENT_CREATE_SESSION
:
1450 ev
->r
.c_session_ret
.host_no
= shost
->host_no
;
1451 ev
->r
.c_session_ret
.sid
= session
->sid
;
1453 case ISCSI_KEVENT_UNBIND_SESSION
:
1454 ev
->r
.unbind_session
.host_no
= shost
->host_no
;
1455 ev
->r
.unbind_session
.sid
= session
->sid
;
1458 iscsi_cls_session_printk(KERN_ERR
, session
, "Invalid event "
1465 * this will occur if the daemon is not up, so we just warn
1466 * the user and when the daemon is restarted it will handle it
1468 rc
= iscsi_multicast_skb(skb
, ISCSI_NL_GRP_ISCSID
, GFP_KERNEL
);
1470 iscsi_cls_session_printk(KERN_ERR
, session
,
1471 "Cannot notify userspace of session "
1472 "event %u. Check iscsi daemon\n",
1475 ISCSI_DBG_TRANS_SESSION(session
, "Completed handling event %d rc %d\n",
1479 EXPORT_SYMBOL_GPL(iscsi_session_event
);
1482 iscsi_if_create_session(struct iscsi_internal
*priv
, struct iscsi_endpoint
*ep
,
1483 struct iscsi_uevent
*ev
, uint32_t initial_cmdsn
,
1484 uint16_t cmds_max
, uint16_t queue_depth
)
1486 struct iscsi_transport
*transport
= priv
->iscsi_transport
;
1487 struct iscsi_cls_session
*session
;
1488 struct Scsi_Host
*shost
;
1490 session
= transport
->create_session(ep
, cmds_max
, queue_depth
,
1495 shost
= iscsi_session_to_shost(session
);
1496 ev
->r
.c_session_ret
.host_no
= shost
->host_no
;
1497 ev
->r
.c_session_ret
.sid
= session
->sid
;
1498 ISCSI_DBG_TRANS_SESSION(session
,
1499 "Completed creating transport session\n");
1504 iscsi_if_create_conn(struct iscsi_transport
*transport
, struct iscsi_uevent
*ev
)
1506 struct iscsi_cls_conn
*conn
;
1507 struct iscsi_cls_session
*session
;
1509 session
= iscsi_session_lookup(ev
->u
.c_conn
.sid
);
1511 printk(KERN_ERR
"iscsi: invalid session %d.\n",
1516 conn
= transport
->create_conn(session
, ev
->u
.c_conn
.cid
);
1518 iscsi_cls_session_printk(KERN_ERR
, session
,
1519 "couldn't create a new connection.");
1523 ev
->r
.c_conn_ret
.sid
= session
->sid
;
1524 ev
->r
.c_conn_ret
.cid
= conn
->cid
;
1526 ISCSI_DBG_TRANS_CONN(conn
, "Completed creating transport conn\n");
1531 iscsi_if_destroy_conn(struct iscsi_transport
*transport
, struct iscsi_uevent
*ev
)
1533 struct iscsi_cls_conn
*conn
;
1535 conn
= iscsi_conn_lookup(ev
->u
.d_conn
.sid
, ev
->u
.d_conn
.cid
);
1539 ISCSI_DBG_TRANS_CONN(conn
, "Destroying transport conn\n");
1540 if (transport
->destroy_conn
)
1541 transport
->destroy_conn(conn
);
1547 iscsi_set_param(struct iscsi_transport
*transport
, struct iscsi_uevent
*ev
)
1549 char *data
= (char*)ev
+ sizeof(*ev
);
1550 struct iscsi_cls_conn
*conn
;
1551 struct iscsi_cls_session
*session
;
1552 int err
= 0, value
= 0;
1554 session
= iscsi_session_lookup(ev
->u
.set_param
.sid
);
1555 conn
= iscsi_conn_lookup(ev
->u
.set_param
.sid
, ev
->u
.set_param
.cid
);
1556 if (!conn
|| !session
)
1559 switch (ev
->u
.set_param
.param
) {
1560 case ISCSI_PARAM_SESS_RECOVERY_TMO
:
1561 sscanf(data
, "%d", &value
);
1562 session
->recovery_tmo
= value
;
1565 err
= transport
->set_param(conn
, ev
->u
.set_param
.param
,
1566 data
, ev
->u
.set_param
.len
);
1572 static int iscsi_if_ep_connect(struct iscsi_transport
*transport
,
1573 struct iscsi_uevent
*ev
, int msg_type
)
1575 struct iscsi_endpoint
*ep
;
1576 struct sockaddr
*dst_addr
;
1577 struct Scsi_Host
*shost
= NULL
;
1578 int non_blocking
, err
= 0;
1580 if (!transport
->ep_connect
)
1583 if (msg_type
== ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST
) {
1584 shost
= scsi_host_lookup(ev
->u
.ep_connect_through_host
.host_no
);
1586 printk(KERN_ERR
"ep connect failed. Could not find "
1588 ev
->u
.ep_connect_through_host
.host_no
);
1591 non_blocking
= ev
->u
.ep_connect_through_host
.non_blocking
;
1593 non_blocking
= ev
->u
.ep_connect
.non_blocking
;
1595 dst_addr
= (struct sockaddr
*)((char*)ev
+ sizeof(*ev
));
1596 ep
= transport
->ep_connect(shost
, dst_addr
, non_blocking
);
1602 ev
->r
.ep_connect_ret
.handle
= ep
->id
;
1605 scsi_host_put(shost
);
1609 static int iscsi_if_ep_disconnect(struct iscsi_transport
*transport
,
1612 struct iscsi_cls_conn
*conn
;
1613 struct iscsi_endpoint
*ep
;
1615 if (!transport
->ep_disconnect
)
1618 ep
= iscsi_lookup_endpoint(ep_handle
);
1623 mutex_lock(&conn
->ep_mutex
);
1625 mutex_unlock(&conn
->ep_mutex
);
1628 transport
->ep_disconnect(ep
);
1633 iscsi_if_transport_ep(struct iscsi_transport
*transport
,
1634 struct iscsi_uevent
*ev
, int msg_type
)
1636 struct iscsi_endpoint
*ep
;
1640 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST
:
1641 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT
:
1642 rc
= iscsi_if_ep_connect(transport
, ev
, msg_type
);
1644 case ISCSI_UEVENT_TRANSPORT_EP_POLL
:
1645 if (!transport
->ep_poll
)
1648 ep
= iscsi_lookup_endpoint(ev
->u
.ep_poll
.ep_handle
);
1652 ev
->r
.retcode
= transport
->ep_poll(ep
,
1653 ev
->u
.ep_poll
.timeout_ms
);
1655 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT
:
1656 rc
= iscsi_if_ep_disconnect(transport
,
1657 ev
->u
.ep_disconnect
.ep_handle
);
1664 iscsi_tgt_dscvr(struct iscsi_transport
*transport
,
1665 struct iscsi_uevent
*ev
)
1667 struct Scsi_Host
*shost
;
1668 struct sockaddr
*dst_addr
;
1671 if (!transport
->tgt_dscvr
)
1674 shost
= scsi_host_lookup(ev
->u
.tgt_dscvr
.host_no
);
1676 printk(KERN_ERR
"target discovery could not find host no %u\n",
1677 ev
->u
.tgt_dscvr
.host_no
);
1682 dst_addr
= (struct sockaddr
*)((char*)ev
+ sizeof(*ev
));
1683 err
= transport
->tgt_dscvr(shost
, ev
->u
.tgt_dscvr
.type
,
1684 ev
->u
.tgt_dscvr
.enable
, dst_addr
);
1685 scsi_host_put(shost
);
1690 iscsi_set_host_param(struct iscsi_transport
*transport
,
1691 struct iscsi_uevent
*ev
)
1693 char *data
= (char*)ev
+ sizeof(*ev
);
1694 struct Scsi_Host
*shost
;
1697 if (!transport
->set_host_param
)
1700 shost
= scsi_host_lookup(ev
->u
.set_host_param
.host_no
);
1702 printk(KERN_ERR
"set_host_param could not find host no %u\n",
1703 ev
->u
.set_host_param
.host_no
);
1707 err
= transport
->set_host_param(shost
, ev
->u
.set_host_param
.param
,
1708 data
, ev
->u
.set_host_param
.len
);
1709 scsi_host_put(shost
);
1714 iscsi_set_path(struct iscsi_transport
*transport
, struct iscsi_uevent
*ev
)
1716 struct Scsi_Host
*shost
;
1717 struct iscsi_path
*params
;
1720 if (!transport
->set_path
)
1723 shost
= scsi_host_lookup(ev
->u
.set_path
.host_no
);
1725 printk(KERN_ERR
"set path could not find host no %u\n",
1726 ev
->u
.set_path
.host_no
);
1730 params
= (struct iscsi_path
*)((char *)ev
+ sizeof(*ev
));
1731 err
= transport
->set_path(shost
, params
);
1733 scsi_host_put(shost
);
1738 iscsi_set_iface_params(struct iscsi_transport
*transport
,
1739 struct iscsi_uevent
*ev
)
1741 char *data
= (char *)ev
+ sizeof(*ev
);
1742 struct Scsi_Host
*shost
;
1745 if (!transport
->set_iface_param
)
1748 shost
= scsi_host_lookup(ev
->u
.set_iface_params
.host_no
);
1750 printk(KERN_ERR
"set_iface_params could not find host no %u\n",
1751 ev
->u
.set_iface_params
.host_no
);
1755 err
= transport
->set_iface_param(shost
, data
,
1756 ev
->u
.set_iface_params
.count
);
1757 scsi_host_put(shost
);
1762 iscsi_if_recv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, uint32_t *group
)
1765 struct iscsi_uevent
*ev
= NLMSG_DATA(nlh
);
1766 struct iscsi_transport
*transport
= NULL
;
1767 struct iscsi_internal
*priv
;
1768 struct iscsi_cls_session
*session
;
1769 struct iscsi_cls_conn
*conn
;
1770 struct iscsi_endpoint
*ep
= NULL
;
1772 if (nlh
->nlmsg_type
== ISCSI_UEVENT_PATH_UPDATE
)
1773 *group
= ISCSI_NL_GRP_UIP
;
1775 *group
= ISCSI_NL_GRP_ISCSID
;
1777 priv
= iscsi_if_transport_lookup(iscsi_ptr(ev
->transport_handle
));
1780 transport
= priv
->iscsi_transport
;
1782 if (!try_module_get(transport
->owner
))
1785 switch (nlh
->nlmsg_type
) {
1786 case ISCSI_UEVENT_CREATE_SESSION
:
1787 err
= iscsi_if_create_session(priv
, ep
, ev
,
1788 ev
->u
.c_session
.initial_cmdsn
,
1789 ev
->u
.c_session
.cmds_max
,
1790 ev
->u
.c_session
.queue_depth
);
1792 case ISCSI_UEVENT_CREATE_BOUND_SESSION
:
1793 ep
= iscsi_lookup_endpoint(ev
->u
.c_bound_session
.ep_handle
);
1799 err
= iscsi_if_create_session(priv
, ep
, ev
,
1800 ev
->u
.c_bound_session
.initial_cmdsn
,
1801 ev
->u
.c_bound_session
.cmds_max
,
1802 ev
->u
.c_bound_session
.queue_depth
);
1804 case ISCSI_UEVENT_DESTROY_SESSION
:
1805 session
= iscsi_session_lookup(ev
->u
.d_session
.sid
);
1807 transport
->destroy_session(session
);
1811 case ISCSI_UEVENT_UNBIND_SESSION
:
1812 session
= iscsi_session_lookup(ev
->u
.d_session
.sid
);
1814 scsi_queue_work(iscsi_session_to_shost(session
),
1815 &session
->unbind_work
);
1819 case ISCSI_UEVENT_CREATE_CONN
:
1820 err
= iscsi_if_create_conn(transport
, ev
);
1822 case ISCSI_UEVENT_DESTROY_CONN
:
1823 err
= iscsi_if_destroy_conn(transport
, ev
);
1825 case ISCSI_UEVENT_BIND_CONN
:
1826 session
= iscsi_session_lookup(ev
->u
.b_conn
.sid
);
1827 conn
= iscsi_conn_lookup(ev
->u
.b_conn
.sid
, ev
->u
.b_conn
.cid
);
1829 if (conn
&& conn
->ep
)
1830 iscsi_if_ep_disconnect(transport
, conn
->ep
->id
);
1832 if (!session
|| !conn
) {
1837 ev
->r
.retcode
= transport
->bind_conn(session
, conn
,
1838 ev
->u
.b_conn
.transport_eph
,
1839 ev
->u
.b_conn
.is_leading
);
1840 if (ev
->r
.retcode
|| !transport
->ep_connect
)
1843 ep
= iscsi_lookup_endpoint(ev
->u
.b_conn
.transport_eph
);
1847 mutex_lock(&conn
->ep_mutex
);
1849 mutex_unlock(&conn
->ep_mutex
);
1851 iscsi_cls_conn_printk(KERN_ERR
, conn
,
1852 "Could not set ep conn "
1855 case ISCSI_UEVENT_SET_PARAM
:
1856 err
= iscsi_set_param(transport
, ev
);
1858 case ISCSI_UEVENT_START_CONN
:
1859 conn
= iscsi_conn_lookup(ev
->u
.start_conn
.sid
, ev
->u
.start_conn
.cid
);
1861 ev
->r
.retcode
= transport
->start_conn(conn
);
1865 case ISCSI_UEVENT_STOP_CONN
:
1866 conn
= iscsi_conn_lookup(ev
->u
.stop_conn
.sid
, ev
->u
.stop_conn
.cid
);
1868 transport
->stop_conn(conn
, ev
->u
.stop_conn
.flag
);
1872 case ISCSI_UEVENT_SEND_PDU
:
1873 conn
= iscsi_conn_lookup(ev
->u
.send_pdu
.sid
, ev
->u
.send_pdu
.cid
);
1875 ev
->r
.retcode
= transport
->send_pdu(conn
,
1876 (struct iscsi_hdr
*)((char*)ev
+ sizeof(*ev
)),
1877 (char*)ev
+ sizeof(*ev
) + ev
->u
.send_pdu
.hdr_size
,
1878 ev
->u
.send_pdu
.data_size
);
1882 case ISCSI_UEVENT_GET_STATS
:
1883 err
= iscsi_if_get_stats(transport
, nlh
);
1885 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT
:
1886 case ISCSI_UEVENT_TRANSPORT_EP_POLL
:
1887 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT
:
1888 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST
:
1889 err
= iscsi_if_transport_ep(transport
, ev
, nlh
->nlmsg_type
);
1891 case ISCSI_UEVENT_TGT_DSCVR
:
1892 err
= iscsi_tgt_dscvr(transport
, ev
);
1894 case ISCSI_UEVENT_SET_HOST_PARAM
:
1895 err
= iscsi_set_host_param(transport
, ev
);
1897 case ISCSI_UEVENT_PATH_UPDATE
:
1898 err
= iscsi_set_path(transport
, ev
);
1900 case ISCSI_UEVENT_SET_IFACE_PARAMS
:
1901 err
= iscsi_set_iface_params(transport
, ev
);
1908 module_put(transport
->owner
);
1913 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1914 * Malformed skbs with wrong lengths or invalid creds are not processed.
1917 iscsi_if_rx(struct sk_buff
*skb
)
1919 mutex_lock(&rx_queue_mutex
);
1920 while (skb
->len
>= NLMSG_SPACE(0)) {
1923 struct nlmsghdr
*nlh
;
1924 struct iscsi_uevent
*ev
;
1927 nlh
= nlmsg_hdr(skb
);
1928 if (nlh
->nlmsg_len
< sizeof(*nlh
) ||
1929 skb
->len
< nlh
->nlmsg_len
) {
1933 ev
= NLMSG_DATA(nlh
);
1934 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
1935 if (rlen
> skb
->len
)
1938 err
= iscsi_if_recv_msg(skb
, nlh
, &group
);
1940 ev
->type
= ISCSI_KEVENT_IF_ERROR
;
1945 * special case for GET_STATS:
1946 * on success - sending reply and stats from
1947 * inside of if_recv_msg(),
1948 * on error - fall through.
1950 if (ev
->type
== ISCSI_UEVENT_GET_STATS
&& !err
)
1952 err
= iscsi_if_send_reply(group
, nlh
->nlmsg_seq
,
1953 nlh
->nlmsg_type
, 0, 0, ev
, sizeof(*ev
));
1954 } while (err
< 0 && err
!= -ECONNREFUSED
);
1955 skb_pull(skb
, rlen
);
1957 mutex_unlock(&rx_queue_mutex
);
1960 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1961 struct device_attribute dev_attr_##_prefix##_##_name = \
1962 __ATTR(_name,_mode,_show,_store)
1965 * iSCSI connection attrs
1967 #define iscsi_conn_attr_show(param) \
1969 show_conn_param_##param(struct device *dev, \
1970 struct device_attribute *attr, char *buf) \
1972 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
1973 struct iscsi_transport *t = conn->transport; \
1974 return t->get_conn_param(conn, param, buf); \
1977 #define iscsi_conn_attr(field, param) \
1978 iscsi_conn_attr_show(param) \
1979 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
1982 iscsi_conn_attr(max_recv_dlength
, ISCSI_PARAM_MAX_RECV_DLENGTH
);
1983 iscsi_conn_attr(max_xmit_dlength
, ISCSI_PARAM_MAX_XMIT_DLENGTH
);
1984 iscsi_conn_attr(header_digest
, ISCSI_PARAM_HDRDGST_EN
);
1985 iscsi_conn_attr(data_digest
, ISCSI_PARAM_DATADGST_EN
);
1986 iscsi_conn_attr(ifmarker
, ISCSI_PARAM_IFMARKER_EN
);
1987 iscsi_conn_attr(ofmarker
, ISCSI_PARAM_OFMARKER_EN
);
1988 iscsi_conn_attr(persistent_port
, ISCSI_PARAM_PERSISTENT_PORT
);
1989 iscsi_conn_attr(exp_statsn
, ISCSI_PARAM_EXP_STATSN
);
1990 iscsi_conn_attr(persistent_address
, ISCSI_PARAM_PERSISTENT_ADDRESS
);
1991 iscsi_conn_attr(ping_tmo
, ISCSI_PARAM_PING_TMO
);
1992 iscsi_conn_attr(recv_tmo
, ISCSI_PARAM_RECV_TMO
);
1994 #define iscsi_conn_ep_attr_show(param) \
1995 static ssize_t show_conn_ep_param_##param(struct device *dev, \
1996 struct device_attribute *attr,\
1999 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
2000 struct iscsi_transport *t = conn->transport; \
2001 struct iscsi_endpoint *ep; \
2005 * Need to make sure ep_disconnect does not free the LLD's \
2006 * interconnect resources while we are trying to read them. \
2008 mutex_lock(&conn->ep_mutex); \
2010 if (!ep && t->ep_connect) { \
2011 mutex_unlock(&conn->ep_mutex); \
2016 rc = t->get_ep_param(ep, param, buf); \
2018 rc = t->get_conn_param(conn, param, buf); \
2019 mutex_unlock(&conn->ep_mutex); \
2023 #define iscsi_conn_ep_attr(field, param) \
2024 iscsi_conn_ep_attr_show(param) \
2025 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
2026 show_conn_ep_param_##param, NULL);
2028 iscsi_conn_ep_attr(address
, ISCSI_PARAM_CONN_ADDRESS
);
2029 iscsi_conn_ep_attr(port
, ISCSI_PARAM_CONN_PORT
);
2031 static struct attribute
*iscsi_conn_attrs
[] = {
2032 &dev_attr_conn_max_recv_dlength
.attr
,
2033 &dev_attr_conn_max_xmit_dlength
.attr
,
2034 &dev_attr_conn_header_digest
.attr
,
2035 &dev_attr_conn_data_digest
.attr
,
2036 &dev_attr_conn_ifmarker
.attr
,
2037 &dev_attr_conn_ofmarker
.attr
,
2038 &dev_attr_conn_address
.attr
,
2039 &dev_attr_conn_port
.attr
,
2040 &dev_attr_conn_exp_statsn
.attr
,
2041 &dev_attr_conn_persistent_address
.attr
,
2042 &dev_attr_conn_persistent_port
.attr
,
2043 &dev_attr_conn_ping_tmo
.attr
,
2044 &dev_attr_conn_recv_tmo
.attr
,
2048 static mode_t
iscsi_conn_attr_is_visible(struct kobject
*kobj
,
2049 struct attribute
*attr
, int i
)
2051 struct device
*cdev
= container_of(kobj
, struct device
, kobj
);
2052 struct iscsi_cls_conn
*conn
= transport_class_to_conn(cdev
);
2053 struct iscsi_transport
*t
= conn
->transport
;
2056 if (attr
== &dev_attr_conn_max_recv_dlength
.attr
)
2057 param
= ISCSI_PARAM_MAX_RECV_DLENGTH
;
2058 else if (attr
== &dev_attr_conn_max_xmit_dlength
.attr
)
2059 param
= ISCSI_PARAM_MAX_XMIT_DLENGTH
;
2060 else if (attr
== &dev_attr_conn_header_digest
.attr
)
2061 param
= ISCSI_PARAM_HDRDGST_EN
;
2062 else if (attr
== &dev_attr_conn_data_digest
.attr
)
2063 param
= ISCSI_PARAM_DATADGST_EN
;
2064 else if (attr
== &dev_attr_conn_ifmarker
.attr
)
2065 param
= ISCSI_PARAM_IFMARKER_EN
;
2066 else if (attr
== &dev_attr_conn_ofmarker
.attr
)
2067 param
= ISCSI_PARAM_OFMARKER_EN
;
2068 else if (attr
== &dev_attr_conn_address
.attr
)
2069 param
= ISCSI_PARAM_CONN_ADDRESS
;
2070 else if (attr
== &dev_attr_conn_port
.attr
)
2071 param
= ISCSI_PARAM_CONN_PORT
;
2072 else if (attr
== &dev_attr_conn_exp_statsn
.attr
)
2073 param
= ISCSI_PARAM_EXP_STATSN
;
2074 else if (attr
== &dev_attr_conn_persistent_address
.attr
)
2075 param
= ISCSI_PARAM_PERSISTENT_ADDRESS
;
2076 else if (attr
== &dev_attr_conn_persistent_port
.attr
)
2077 param
= ISCSI_PARAM_PERSISTENT_PORT
;
2078 else if (attr
== &dev_attr_conn_ping_tmo
.attr
)
2079 param
= ISCSI_PARAM_PING_TMO
;
2080 else if (attr
== &dev_attr_conn_recv_tmo
.attr
)
2081 param
= ISCSI_PARAM_RECV_TMO
;
2083 WARN_ONCE(1, "Invalid conn attr");
2087 return t
->attr_is_visible(ISCSI_PARAM
, param
);
2090 static struct attribute_group iscsi_conn_group
= {
2091 .attrs
= iscsi_conn_attrs
,
2092 .is_visible
= iscsi_conn_attr_is_visible
,
2096 * iSCSI session attrs
2098 #define iscsi_session_attr_show(param, perm) \
2100 show_session_param_##param(struct device *dev, \
2101 struct device_attribute *attr, char *buf) \
2103 struct iscsi_cls_session *session = \
2104 iscsi_dev_to_session(dev->parent); \
2105 struct iscsi_transport *t = session->transport; \
2107 if (perm && !capable(CAP_SYS_ADMIN)) \
2109 return t->get_session_param(session, param, buf); \
2112 #define iscsi_session_attr(field, param, perm) \
2113 iscsi_session_attr_show(param, perm) \
2114 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
2116 iscsi_session_attr(targetname
, ISCSI_PARAM_TARGET_NAME
, 0);
2117 iscsi_session_attr(initial_r2t
, ISCSI_PARAM_INITIAL_R2T_EN
, 0);
2118 iscsi_session_attr(max_outstanding_r2t
, ISCSI_PARAM_MAX_R2T
, 0);
2119 iscsi_session_attr(immediate_data
, ISCSI_PARAM_IMM_DATA_EN
, 0);
2120 iscsi_session_attr(first_burst_len
, ISCSI_PARAM_FIRST_BURST
, 0);
2121 iscsi_session_attr(max_burst_len
, ISCSI_PARAM_MAX_BURST
, 0);
2122 iscsi_session_attr(data_pdu_in_order
, ISCSI_PARAM_PDU_INORDER_EN
, 0);
2123 iscsi_session_attr(data_seq_in_order
, ISCSI_PARAM_DATASEQ_INORDER_EN
, 0);
2124 iscsi_session_attr(erl
, ISCSI_PARAM_ERL
, 0);
2125 iscsi_session_attr(tpgt
, ISCSI_PARAM_TPGT
, 0);
2126 iscsi_session_attr(username
, ISCSI_PARAM_USERNAME
, 1);
2127 iscsi_session_attr(username_in
, ISCSI_PARAM_USERNAME_IN
, 1);
2128 iscsi_session_attr(password
, ISCSI_PARAM_PASSWORD
, 1);
2129 iscsi_session_attr(password_in
, ISCSI_PARAM_PASSWORD_IN
, 1);
2130 iscsi_session_attr(fast_abort
, ISCSI_PARAM_FAST_ABORT
, 0);
2131 iscsi_session_attr(abort_tmo
, ISCSI_PARAM_ABORT_TMO
, 0);
2132 iscsi_session_attr(lu_reset_tmo
, ISCSI_PARAM_LU_RESET_TMO
, 0);
2133 iscsi_session_attr(tgt_reset_tmo
, ISCSI_PARAM_TGT_RESET_TMO
, 0);
2134 iscsi_session_attr(ifacename
, ISCSI_PARAM_IFACE_NAME
, 0);
2135 iscsi_session_attr(initiatorname
, ISCSI_PARAM_INITIATOR_NAME
, 0);
2136 iscsi_session_attr(targetalias
, ISCSI_PARAM_TARGET_ALIAS
, 0);
2139 show_priv_session_state(struct device
*dev
, struct device_attribute
*attr
,
2142 struct iscsi_cls_session
*session
= iscsi_dev_to_session(dev
->parent
);
2143 return sprintf(buf
, "%s\n", iscsi_session_state_name(session
->state
));
2145 static ISCSI_CLASS_ATTR(priv_sess
, state
, S_IRUGO
, show_priv_session_state
,
2148 #define iscsi_priv_session_attr_show(field, format) \
2150 show_priv_session_##field(struct device *dev, \
2151 struct device_attribute *attr, char *buf) \
2153 struct iscsi_cls_session *session = \
2154 iscsi_dev_to_session(dev->parent); \
2155 if (session->field == -1) \
2156 return sprintf(buf, "off\n"); \
2157 return sprintf(buf, format"\n", session->field); \
2160 #define iscsi_priv_session_attr_store(field) \
2162 store_priv_session_##field(struct device *dev, \
2163 struct device_attribute *attr, \
2164 const char *buf, size_t count) \
2168 struct iscsi_cls_session *session = \
2169 iscsi_dev_to_session(dev->parent); \
2170 if ((session->state == ISCSI_SESSION_FREE) || \
2171 (session->state == ISCSI_SESSION_FAILED)) \
2173 if (strncmp(buf, "off", 3) == 0) \
2174 session->field = -1; \
2176 val = simple_strtoul(buf, &cp, 0); \
2177 if (*cp != '\0' && *cp != '\n') \
2179 session->field = val; \
2184 #define iscsi_priv_session_rw_attr(field, format) \
2185 iscsi_priv_session_attr_show(field, format) \
2186 iscsi_priv_session_attr_store(field) \
2187 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
2188 show_priv_session_##field, \
2189 store_priv_session_##field)
2190 iscsi_priv_session_rw_attr(recovery_tmo
, "%d");
2192 static struct attribute
*iscsi_session_attrs
[] = {
2193 &dev_attr_sess_initial_r2t
.attr
,
2194 &dev_attr_sess_max_outstanding_r2t
.attr
,
2195 &dev_attr_sess_immediate_data
.attr
,
2196 &dev_attr_sess_first_burst_len
.attr
,
2197 &dev_attr_sess_max_burst_len
.attr
,
2198 &dev_attr_sess_data_pdu_in_order
.attr
,
2199 &dev_attr_sess_data_seq_in_order
.attr
,
2200 &dev_attr_sess_erl
.attr
,
2201 &dev_attr_sess_targetname
.attr
,
2202 &dev_attr_sess_tpgt
.attr
,
2203 &dev_attr_sess_password
.attr
,
2204 &dev_attr_sess_password_in
.attr
,
2205 &dev_attr_sess_username
.attr
,
2206 &dev_attr_sess_username_in
.attr
,
2207 &dev_attr_sess_fast_abort
.attr
,
2208 &dev_attr_sess_abort_tmo
.attr
,
2209 &dev_attr_sess_lu_reset_tmo
.attr
,
2210 &dev_attr_sess_tgt_reset_tmo
.attr
,
2211 &dev_attr_sess_ifacename
.attr
,
2212 &dev_attr_sess_initiatorname
.attr
,
2213 &dev_attr_sess_targetalias
.attr
,
2214 &dev_attr_priv_sess_recovery_tmo
.attr
,
2215 &dev_attr_priv_sess_state
.attr
,
2219 static mode_t
iscsi_session_attr_is_visible(struct kobject
*kobj
,
2220 struct attribute
*attr
, int i
)
2222 struct device
*cdev
= container_of(kobj
, struct device
, kobj
);
2223 struct iscsi_cls_session
*session
= transport_class_to_session(cdev
);
2224 struct iscsi_transport
*t
= session
->transport
;
2227 if (attr
== &dev_attr_sess_initial_r2t
.attr
)
2228 param
= ISCSI_PARAM_INITIAL_R2T_EN
;
2229 else if (attr
== &dev_attr_sess_max_outstanding_r2t
.attr
)
2230 param
= ISCSI_PARAM_MAX_R2T
;
2231 else if (attr
== &dev_attr_sess_immediate_data
.attr
)
2232 param
= ISCSI_PARAM_IMM_DATA_EN
;
2233 else if (attr
== &dev_attr_sess_first_burst_len
.attr
)
2234 param
= ISCSI_PARAM_FIRST_BURST
;
2235 else if (attr
== &dev_attr_sess_max_burst_len
.attr
)
2236 param
= ISCSI_PARAM_MAX_BURST
;
2237 else if (attr
== &dev_attr_sess_data_pdu_in_order
.attr
)
2238 param
= ISCSI_PARAM_PDU_INORDER_EN
;
2239 else if (attr
== &dev_attr_sess_data_seq_in_order
.attr
)
2240 param
= ISCSI_PARAM_DATASEQ_INORDER_EN
;
2241 else if (attr
== &dev_attr_sess_erl
.attr
)
2242 param
= ISCSI_PARAM_ERL
;
2243 else if (attr
== &dev_attr_sess_targetname
.attr
)
2244 param
= ISCSI_PARAM_TARGET_NAME
;
2245 else if (attr
== &dev_attr_sess_tpgt
.attr
)
2246 param
= ISCSI_PARAM_TPGT
;
2247 else if (attr
== &dev_attr_sess_password
.attr
)
2248 param
= ISCSI_PARAM_USERNAME
;
2249 else if (attr
== &dev_attr_sess_password_in
.attr
)
2250 param
= ISCSI_PARAM_USERNAME_IN
;
2251 else if (attr
== &dev_attr_sess_username
.attr
)
2252 param
= ISCSI_PARAM_PASSWORD
;
2253 else if (attr
== &dev_attr_sess_username_in
.attr
)
2254 param
= ISCSI_PARAM_PASSWORD_IN
;
2255 else if (attr
== &dev_attr_sess_fast_abort
.attr
)
2256 param
= ISCSI_PARAM_FAST_ABORT
;
2257 else if (attr
== &dev_attr_sess_abort_tmo
.attr
)
2258 param
= ISCSI_PARAM_ABORT_TMO
;
2259 else if (attr
== &dev_attr_sess_lu_reset_tmo
.attr
)
2260 param
= ISCSI_PARAM_LU_RESET_TMO
;
2261 else if (attr
== &dev_attr_sess_tgt_reset_tmo
.attr
)
2262 param
= ISCSI_PARAM_TGT_RESET_TMO
;
2263 else if (attr
== &dev_attr_sess_ifacename
.attr
)
2264 param
= ISCSI_PARAM_IFACE_NAME
;
2265 else if (attr
== &dev_attr_sess_initiatorname
.attr
)
2266 param
= ISCSI_PARAM_INITIATOR_NAME
;
2267 else if (attr
== &dev_attr_sess_targetalias
.attr
)
2268 param
= ISCSI_PARAM_TARGET_ALIAS
;
2269 else if (attr
== &dev_attr_priv_sess_recovery_tmo
.attr
)
2270 return S_IRUGO
| S_IWUSR
;
2271 else if (attr
== &dev_attr_priv_sess_state
.attr
)
2274 WARN_ONCE(1, "Invalid session attr");
2278 return t
->attr_is_visible(ISCSI_PARAM
, param
);
2281 static struct attribute_group iscsi_session_group
= {
2282 .attrs
= iscsi_session_attrs
,
2283 .is_visible
= iscsi_session_attr_is_visible
,
2289 #define iscsi_host_attr_show(param) \
2291 show_host_param_##param(struct device *dev, \
2292 struct device_attribute *attr, char *buf) \
2294 struct Scsi_Host *shost = transport_class_to_shost(dev); \
2295 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
2296 return priv->iscsi_transport->get_host_param(shost, param, buf); \
2299 #define iscsi_host_attr(field, param) \
2300 iscsi_host_attr_show(param) \
2301 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
2304 iscsi_host_attr(netdev
, ISCSI_HOST_PARAM_NETDEV_NAME
);
2305 iscsi_host_attr(hwaddress
, ISCSI_HOST_PARAM_HWADDRESS
);
2306 iscsi_host_attr(ipaddress
, ISCSI_HOST_PARAM_IPADDRESS
);
2307 iscsi_host_attr(initiatorname
, ISCSI_HOST_PARAM_INITIATOR_NAME
);
2309 static struct attribute
*iscsi_host_attrs
[] = {
2310 &dev_attr_host_netdev
.attr
,
2311 &dev_attr_host_hwaddress
.attr
,
2312 &dev_attr_host_ipaddress
.attr
,
2313 &dev_attr_host_initiatorname
.attr
,
2317 static mode_t
iscsi_host_attr_is_visible(struct kobject
*kobj
,
2318 struct attribute
*attr
, int i
)
2320 struct device
*cdev
= container_of(kobj
, struct device
, kobj
);
2321 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
2322 struct iscsi_internal
*priv
= to_iscsi_internal(shost
->transportt
);
2325 if (attr
== &dev_attr_host_netdev
.attr
)
2326 param
= ISCSI_HOST_PARAM_NETDEV_NAME
;
2327 else if (attr
== &dev_attr_host_hwaddress
.attr
)
2328 param
= ISCSI_HOST_PARAM_HWADDRESS
;
2329 else if (attr
== &dev_attr_host_ipaddress
.attr
)
2330 param
= ISCSI_HOST_PARAM_IPADDRESS
;
2331 else if (attr
== &dev_attr_host_initiatorname
.attr
)
2332 param
= ISCSI_HOST_PARAM_INITIATOR_NAME
;
2334 WARN_ONCE(1, "Invalid host attr");
2338 return priv
->iscsi_transport
->attr_is_visible(ISCSI_HOST_PARAM
, param
);
2341 static struct attribute_group iscsi_host_group
= {
2342 .attrs
= iscsi_host_attrs
,
2343 .is_visible
= iscsi_host_attr_is_visible
,
2346 static int iscsi_session_match(struct attribute_container
*cont
,
2349 struct iscsi_cls_session
*session
;
2350 struct Scsi_Host
*shost
;
2351 struct iscsi_internal
*priv
;
2353 if (!iscsi_is_session_dev(dev
))
2356 session
= iscsi_dev_to_session(dev
);
2357 shost
= iscsi_session_to_shost(session
);
2358 if (!shost
->transportt
)
2361 priv
= to_iscsi_internal(shost
->transportt
);
2362 if (priv
->session_cont
.ac
.class != &iscsi_session_class
.class)
2365 return &priv
->session_cont
.ac
== cont
;
2368 static int iscsi_conn_match(struct attribute_container
*cont
,
2371 struct iscsi_cls_session
*session
;
2372 struct iscsi_cls_conn
*conn
;
2373 struct Scsi_Host
*shost
;
2374 struct iscsi_internal
*priv
;
2376 if (!iscsi_is_conn_dev(dev
))
2379 conn
= iscsi_dev_to_conn(dev
);
2380 session
= iscsi_dev_to_session(conn
->dev
.parent
);
2381 shost
= iscsi_session_to_shost(session
);
2383 if (!shost
->transportt
)
2386 priv
= to_iscsi_internal(shost
->transportt
);
2387 if (priv
->conn_cont
.ac
.class != &iscsi_connection_class
.class)
2390 return &priv
->conn_cont
.ac
== cont
;
2393 static int iscsi_host_match(struct attribute_container
*cont
,
2396 struct Scsi_Host
*shost
;
2397 struct iscsi_internal
*priv
;
2399 if (!scsi_is_host_device(dev
))
2402 shost
= dev_to_shost(dev
);
2403 if (!shost
->transportt
||
2404 shost
->transportt
->host_attrs
.ac
.class != &iscsi_host_class
.class)
2407 priv
= to_iscsi_internal(shost
->transportt
);
2408 return &priv
->t
.host_attrs
.ac
== cont
;
2411 struct scsi_transport_template
*
2412 iscsi_register_transport(struct iscsi_transport
*tt
)
2414 struct iscsi_internal
*priv
;
2415 unsigned long flags
;
2420 priv
= iscsi_if_transport_lookup(tt
);
2424 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
2427 INIT_LIST_HEAD(&priv
->list
);
2428 priv
->iscsi_transport
= tt
;
2429 priv
->t
.user_scan
= iscsi_user_scan
;
2430 priv
->t
.create_work_queue
= 1;
2432 priv
->dev
.class = &iscsi_transport_class
;
2433 dev_set_name(&priv
->dev
, "%s", tt
->name
);
2434 err
= device_register(&priv
->dev
);
2438 err
= sysfs_create_group(&priv
->dev
.kobj
, &iscsi_transport_group
);
2440 goto unregister_dev
;
2442 /* host parameters */
2443 priv
->t
.host_attrs
.ac
.class = &iscsi_host_class
.class;
2444 priv
->t
.host_attrs
.ac
.match
= iscsi_host_match
;
2445 priv
->t
.host_attrs
.ac
.grp
= &iscsi_host_group
;
2446 priv
->t
.host_size
= sizeof(struct iscsi_cls_host
);
2447 transport_container_register(&priv
->t
.host_attrs
);
2449 /* connection parameters */
2450 priv
->conn_cont
.ac
.class = &iscsi_connection_class
.class;
2451 priv
->conn_cont
.ac
.match
= iscsi_conn_match
;
2452 priv
->conn_cont
.ac
.grp
= &iscsi_conn_group
;
2453 transport_container_register(&priv
->conn_cont
);
2455 /* session parameters */
2456 priv
->session_cont
.ac
.class = &iscsi_session_class
.class;
2457 priv
->session_cont
.ac
.match
= iscsi_session_match
;
2458 priv
->session_cont
.ac
.grp
= &iscsi_session_group
;
2459 transport_container_register(&priv
->session_cont
);
2461 spin_lock_irqsave(&iscsi_transport_lock
, flags
);
2462 list_add(&priv
->list
, &iscsi_transports
);
2463 spin_unlock_irqrestore(&iscsi_transport_lock
, flags
);
2465 printk(KERN_NOTICE
"iscsi: registered transport (%s)\n", tt
->name
);
2469 device_unregister(&priv
->dev
);
2475 EXPORT_SYMBOL_GPL(iscsi_register_transport
);
2477 int iscsi_unregister_transport(struct iscsi_transport
*tt
)
2479 struct iscsi_internal
*priv
;
2480 unsigned long flags
;
2484 mutex_lock(&rx_queue_mutex
);
2486 priv
= iscsi_if_transport_lookup(tt
);
2489 spin_lock_irqsave(&iscsi_transport_lock
, flags
);
2490 list_del(&priv
->list
);
2491 spin_unlock_irqrestore(&iscsi_transport_lock
, flags
);
2493 transport_container_unregister(&priv
->conn_cont
);
2494 transport_container_unregister(&priv
->session_cont
);
2495 transport_container_unregister(&priv
->t
.host_attrs
);
2497 sysfs_remove_group(&priv
->dev
.kobj
, &iscsi_transport_group
);
2498 device_unregister(&priv
->dev
);
2499 mutex_unlock(&rx_queue_mutex
);
2503 EXPORT_SYMBOL_GPL(iscsi_unregister_transport
);
2505 static __init
int iscsi_transport_init(void)
2509 printk(KERN_INFO
"Loading iSCSI transport class v%s.\n",
2510 ISCSI_TRANSPORT_VERSION
);
2512 atomic_set(&iscsi_session_nr
, 0);
2514 err
= class_register(&iscsi_transport_class
);
2518 err
= class_register(&iscsi_endpoint_class
);
2520 goto unregister_transport_class
;
2522 err
= class_register(&iscsi_iface_class
);
2524 goto unregister_endpoint_class
;
2526 err
= transport_class_register(&iscsi_host_class
);
2528 goto unregister_iface_class
;
2530 err
= transport_class_register(&iscsi_connection_class
);
2532 goto unregister_host_class
;
2534 err
= transport_class_register(&iscsi_session_class
);
2536 goto unregister_conn_class
;
2538 nls
= netlink_kernel_create(&init_net
, NETLINK_ISCSI
, 1, iscsi_if_rx
,
2542 goto unregister_session_class
;
2545 iscsi_eh_timer_workq
= create_singlethread_workqueue("iscsi_eh");
2546 if (!iscsi_eh_timer_workq
)
2552 netlink_kernel_release(nls
);
2553 unregister_session_class
:
2554 transport_class_unregister(&iscsi_session_class
);
2555 unregister_conn_class
:
2556 transport_class_unregister(&iscsi_connection_class
);
2557 unregister_host_class
:
2558 transport_class_unregister(&iscsi_host_class
);
2559 unregister_iface_class
:
2560 class_unregister(&iscsi_iface_class
);
2561 unregister_endpoint_class
:
2562 class_unregister(&iscsi_endpoint_class
);
2563 unregister_transport_class
:
2564 class_unregister(&iscsi_transport_class
);
2568 static void __exit
iscsi_transport_exit(void)
2570 destroy_workqueue(iscsi_eh_timer_workq
);
2571 netlink_kernel_release(nls
);
2572 transport_class_unregister(&iscsi_connection_class
);
2573 transport_class_unregister(&iscsi_session_class
);
2574 transport_class_unregister(&iscsi_host_class
);
2575 class_unregister(&iscsi_endpoint_class
);
2576 class_unregister(&iscsi_iface_class
);
2577 class_unregister(&iscsi_transport_class
);
2580 module_init(iscsi_transport_init
);
2581 module_exit(iscsi_transport_exit
);
2583 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2584 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2585 "Alex Aizman <itn780@yahoo.com>");
2586 MODULE_DESCRIPTION("iSCSI Transport Interface");
2587 MODULE_LICENSE("GPL");
2588 MODULE_VERSION(ISCSI_TRANSPORT_VERSION
);
2589 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_ISCSI
);