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>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_transport.h>
30 #include <scsi/scsi_transport_iscsi.h>
31 #include <scsi/iscsi_if.h>
33 #define ISCSI_SESSION_ATTRS 19
34 #define ISCSI_CONN_ATTRS 13
35 #define ISCSI_HOST_ATTRS 4
36 #define ISCSI_TRANSPORT_VERSION "2.0-869"
38 struct iscsi_internal
{
40 struct scsi_transport_template t
;
41 struct iscsi_transport
*iscsi_transport
;
42 struct list_head list
;
43 struct class_device cdev
;
45 struct class_device_attribute
*host_attrs
[ISCSI_HOST_ATTRS
+ 1];
46 struct transport_container conn_cont
;
47 struct class_device_attribute
*conn_attrs
[ISCSI_CONN_ATTRS
+ 1];
48 struct transport_container session_cont
;
49 struct class_device_attribute
*session_attrs
[ISCSI_SESSION_ATTRS
+ 1];
52 static atomic_t iscsi_session_nr
; /* sysfs session id for next new session */
53 static struct workqueue_struct
*iscsi_eh_timer_workq
;
56 * list of registered transports and lock that must
57 * be held while accessing list. The iscsi_transport_lock must
58 * be acquired after the rx_queue_mutex.
60 static LIST_HEAD(iscsi_transports
);
61 static DEFINE_SPINLOCK(iscsi_transport_lock
);
63 #define to_iscsi_internal(tmpl) \
64 container_of(tmpl, struct iscsi_internal, t)
66 #define cdev_to_iscsi_internal(_cdev) \
67 container_of(_cdev, struct iscsi_internal, cdev)
69 static void iscsi_transport_release(struct class_device
*cdev
)
71 struct iscsi_internal
*priv
= cdev_to_iscsi_internal(cdev
);
76 * iscsi_transport_class represents the iscsi_transports that are
79 static struct class iscsi_transport_class
= {
80 .name
= "iscsi_transport",
81 .release
= iscsi_transport_release
,
85 show_transport_handle(struct class_device
*cdev
, char *buf
)
87 struct iscsi_internal
*priv
= cdev_to_iscsi_internal(cdev
);
88 return sprintf(buf
, "%llu\n", (unsigned long long)iscsi_handle(priv
->iscsi_transport
));
90 static CLASS_DEVICE_ATTR(handle
, S_IRUGO
, show_transport_handle
, NULL
);
92 #define show_transport_attr(name, format) \
94 show_transport_##name(struct class_device *cdev, char *buf) \
96 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
97 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
99 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
101 show_transport_attr(caps
, "0x%x");
102 show_transport_attr(max_lun
, "%d");
103 show_transport_attr(max_conn
, "%d");
104 show_transport_attr(max_cmd_len
, "%d");
106 static struct attribute
*iscsi_transport_attrs
[] = {
107 &class_device_attr_handle
.attr
,
108 &class_device_attr_caps
.attr
,
109 &class_device_attr_max_lun
.attr
,
110 &class_device_attr_max_conn
.attr
,
111 &class_device_attr_max_cmd_len
.attr
,
115 static struct attribute_group iscsi_transport_group
= {
116 .attrs
= iscsi_transport_attrs
,
121 static int iscsi_setup_host(struct transport_container
*tc
, struct device
*dev
,
122 struct class_device
*cdev
)
124 struct Scsi_Host
*shost
= dev_to_shost(dev
);
125 struct iscsi_host
*ihost
= shost
->shost_data
;
127 memset(ihost
, 0, sizeof(*ihost
));
128 INIT_LIST_HEAD(&ihost
->sessions
);
129 mutex_init(&ihost
->mutex
);
130 atomic_set(&ihost
->nr_scans
, 0);
132 snprintf(ihost
->scan_workq_name
, KOBJ_NAME_LEN
, "iscsi_scan_%d",
134 ihost
->scan_workq
= create_singlethread_workqueue(
135 ihost
->scan_workq_name
);
136 if (!ihost
->scan_workq
)
141 static int iscsi_remove_host(struct transport_container
*tc
, struct device
*dev
,
142 struct class_device
*cdev
)
144 struct Scsi_Host
*shost
= dev_to_shost(dev
);
145 struct iscsi_host
*ihost
= shost
->shost_data
;
147 destroy_workqueue(ihost
->scan_workq
);
151 static DECLARE_TRANSPORT_CLASS(iscsi_host_class
,
157 static DECLARE_TRANSPORT_CLASS(iscsi_session_class
,
163 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class
,
169 static struct sock
*nls
;
170 static DEFINE_MUTEX(rx_queue_mutex
);
172 static LIST_HEAD(sesslist
);
173 static DEFINE_SPINLOCK(sesslock
);
174 static LIST_HEAD(connlist
);
175 static DEFINE_SPINLOCK(connlock
);
177 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn
*conn
)
179 struct iscsi_cls_session
*sess
= iscsi_dev_to_session(conn
->dev
.parent
);
184 * Returns the matching session to a given sid
186 static struct iscsi_cls_session
*iscsi_session_lookup(uint32_t sid
)
189 struct iscsi_cls_session
*sess
;
191 spin_lock_irqsave(&sesslock
, flags
);
192 list_for_each_entry(sess
, &sesslist
, sess_list
) {
193 if (sess
->sid
== sid
) {
194 spin_unlock_irqrestore(&sesslock
, flags
);
198 spin_unlock_irqrestore(&sesslock
, flags
);
203 * Returns the matching connection to a given sid / cid tuple
205 static struct iscsi_cls_conn
*iscsi_conn_lookup(uint32_t sid
, uint32_t cid
)
208 struct iscsi_cls_conn
*conn
;
210 spin_lock_irqsave(&connlock
, flags
);
211 list_for_each_entry(conn
, &connlist
, conn_list
) {
212 if ((conn
->cid
== cid
) && (iscsi_conn_get_sid(conn
) == sid
)) {
213 spin_unlock_irqrestore(&connlock
, flags
);
217 spin_unlock_irqrestore(&connlock
, flags
);
222 * The following functions can be used by LLDs that allocate
223 * their own scsi_hosts or by software iscsi LLDs
228 } iscsi_session_state_names
[] = {
229 { ISCSI_SESSION_LOGGED_IN
, "LOGGED_IN" },
230 { ISCSI_SESSION_FAILED
, "FAILED" },
231 { ISCSI_SESSION_FREE
, "FREE" },
234 static const char *iscsi_session_state_name(int state
)
239 for (i
= 0; i
< ARRAY_SIZE(iscsi_session_state_names
); i
++) {
240 if (iscsi_session_state_names
[i
].value
== state
) {
241 name
= iscsi_session_state_names
[i
].name
;
248 int iscsi_session_chkready(struct iscsi_cls_session
*session
)
253 spin_lock_irqsave(&session
->lock
, flags
);
254 switch (session
->state
) {
255 case ISCSI_SESSION_LOGGED_IN
:
258 case ISCSI_SESSION_FAILED
:
259 err
= DID_IMM_RETRY
<< 16;
261 case ISCSI_SESSION_FREE
:
262 err
= DID_NO_CONNECT
<< 16;
265 err
= DID_NO_CONNECT
<< 16;
268 spin_unlock_irqrestore(&session
->lock
, flags
);
271 EXPORT_SYMBOL_GPL(iscsi_session_chkready
);
273 static void iscsi_session_release(struct device
*dev
)
275 struct iscsi_cls_session
*session
= iscsi_dev_to_session(dev
);
276 struct Scsi_Host
*shost
;
278 shost
= iscsi_session_to_shost(session
);
279 scsi_host_put(shost
);
283 static int iscsi_is_session_dev(const struct device
*dev
)
285 return dev
->release
== iscsi_session_release
;
289 * iscsi_scan_finished - helper to report when running scans are done
291 * @time: scan run time
293 * This function can be used by drives like qla4xxx to report to the scsi
294 * layer when the scans it kicked off at module load time are done.
296 int iscsi_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
298 struct iscsi_host
*ihost
= shost
->shost_data
;
300 * qla4xxx will have kicked off some session unblocks before calling
301 * scsi_scan_host, so just wait for them to complete.
303 return !atomic_read(&ihost
->nr_scans
);
305 EXPORT_SYMBOL_GPL(iscsi_scan_finished
);
307 static int iscsi_user_scan(struct Scsi_Host
*shost
, uint channel
,
310 struct iscsi_host
*ihost
= shost
->shost_data
;
311 struct iscsi_cls_session
*session
;
313 mutex_lock(&ihost
->mutex
);
314 list_for_each_entry(session
, &ihost
->sessions
, host_list
) {
315 if ((channel
== SCAN_WILD_CARD
|| channel
== 0) &&
316 (id
== SCAN_WILD_CARD
|| id
== session
->target_id
))
317 scsi_scan_target(&session
->dev
, 0,
318 session
->target_id
, lun
, 1);
320 mutex_unlock(&ihost
->mutex
);
325 static void iscsi_scan_session(struct work_struct
*work
)
327 struct iscsi_cls_session
*session
=
328 container_of(work
, struct iscsi_cls_session
, scan_work
);
329 struct Scsi_Host
*shost
= iscsi_session_to_shost(session
);
330 struct iscsi_host
*ihost
= shost
->shost_data
;
333 spin_lock_irqsave(&session
->lock
, flags
);
334 if (session
->state
!= ISCSI_SESSION_LOGGED_IN
) {
335 spin_unlock_irqrestore(&session
->lock
, flags
);
338 spin_unlock_irqrestore(&session
->lock
, flags
);
340 scsi_scan_target(&session
->dev
, 0, session
->target_id
,
343 atomic_dec(&ihost
->nr_scans
);
346 static void session_recovery_timedout(struct work_struct
*work
)
348 struct iscsi_cls_session
*session
=
349 container_of(work
, struct iscsi_cls_session
,
353 iscsi_cls_session_printk(KERN_INFO
, session
,
354 "session recovery timed out after %d secs\n",
355 session
->recovery_tmo
);
357 spin_lock_irqsave(&session
->lock
, flags
);
358 switch (session
->state
) {
359 case ISCSI_SESSION_FAILED
:
360 session
->state
= ISCSI_SESSION_FREE
;
362 case ISCSI_SESSION_LOGGED_IN
:
363 case ISCSI_SESSION_FREE
:
364 /* we raced with the unblock's flush */
365 spin_unlock_irqrestore(&session
->lock
, flags
);
368 spin_unlock_irqrestore(&session
->lock
, flags
);
370 if (session
->transport
->session_recovery_timedout
)
371 session
->transport
->session_recovery_timedout(session
);
373 scsi_target_unblock(&session
->dev
);
376 static void __iscsi_unblock_session(struct work_struct
*work
)
378 struct iscsi_cls_session
*session
=
379 container_of(work
, struct iscsi_cls_session
,
381 struct Scsi_Host
*shost
= iscsi_session_to_shost(session
);
382 struct iscsi_host
*ihost
= shost
->shost_data
;
386 * The recovery and unblock work get run from the same workqueue,
387 * so try to cancel it if it was going to run after this unblock.
389 cancel_delayed_work(&session
->recovery_work
);
390 spin_lock_irqsave(&session
->lock
, flags
);
391 session
->state
= ISCSI_SESSION_LOGGED_IN
;
392 spin_unlock_irqrestore(&session
->lock
, flags
);
394 scsi_target_unblock(&session
->dev
);
396 * Only do kernel scanning if the driver is properly hooked into
397 * the async scanning code (drivers like iscsi_tcp do login and
398 * scanning from userspace).
400 if (shost
->hostt
->scan_finished
) {
401 if (queue_work(ihost
->scan_workq
, &session
->scan_work
))
402 atomic_inc(&ihost
->nr_scans
);
407 * iscsi_unblock_session - set a session as logged in and start IO.
408 * @session: iscsi session
410 * Mark a session as ready to accept IO.
412 void iscsi_unblock_session(struct iscsi_cls_session
*session
)
414 queue_work(iscsi_eh_timer_workq
, &session
->unblock_work
);
416 * make sure all the events have completed before tell the driver
419 flush_workqueue(iscsi_eh_timer_workq
);
421 EXPORT_SYMBOL_GPL(iscsi_unblock_session
);
423 static void __iscsi_block_session(struct work_struct
*work
)
425 struct iscsi_cls_session
*session
=
426 container_of(work
, struct iscsi_cls_session
,
430 spin_lock_irqsave(&session
->lock
, flags
);
431 session
->state
= ISCSI_SESSION_FAILED
;
432 spin_unlock_irqrestore(&session
->lock
, flags
);
433 scsi_target_block(&session
->dev
);
434 queue_delayed_work(iscsi_eh_timer_workq
, &session
->recovery_work
,
435 session
->recovery_tmo
* HZ
);
438 void iscsi_block_session(struct iscsi_cls_session
*session
)
440 queue_work(iscsi_eh_timer_workq
, &session
->block_work
);
442 EXPORT_SYMBOL_GPL(iscsi_block_session
);
444 static void __iscsi_unbind_session(struct work_struct
*work
)
446 struct iscsi_cls_session
*session
=
447 container_of(work
, struct iscsi_cls_session
,
449 struct Scsi_Host
*shost
= iscsi_session_to_shost(session
);
450 struct iscsi_host
*ihost
= shost
->shost_data
;
452 /* Prevent new scans and make sure scanning is not in progress */
453 mutex_lock(&ihost
->mutex
);
454 if (list_empty(&session
->host_list
)) {
455 mutex_unlock(&ihost
->mutex
);
458 list_del_init(&session
->host_list
);
459 mutex_unlock(&ihost
->mutex
);
461 scsi_remove_target(&session
->dev
);
462 iscsi_session_event(session
, ISCSI_KEVENT_UNBIND_SESSION
);
465 static int iscsi_unbind_session(struct iscsi_cls_session
*session
)
467 struct Scsi_Host
*shost
= iscsi_session_to_shost(session
);
468 struct iscsi_host
*ihost
= shost
->shost_data
;
470 return queue_work(ihost
->scan_workq
, &session
->unbind_work
);
473 struct iscsi_cls_session
*
474 iscsi_alloc_session(struct Scsi_Host
*shost
,
475 struct iscsi_transport
*transport
)
477 struct iscsi_cls_session
*session
;
479 session
= kzalloc(sizeof(*session
) + transport
->sessiondata_size
,
484 session
->transport
= transport
;
485 session
->recovery_tmo
= 120;
486 session
->state
= ISCSI_SESSION_FREE
;
487 INIT_DELAYED_WORK(&session
->recovery_work
, session_recovery_timedout
);
488 INIT_LIST_HEAD(&session
->host_list
);
489 INIT_LIST_HEAD(&session
->sess_list
);
490 INIT_WORK(&session
->unblock_work
, __iscsi_unblock_session
);
491 INIT_WORK(&session
->block_work
, __iscsi_block_session
);
492 INIT_WORK(&session
->unbind_work
, __iscsi_unbind_session
);
493 INIT_WORK(&session
->scan_work
, iscsi_scan_session
);
494 spin_lock_init(&session
->lock
);
496 /* this is released in the dev's release function */
497 scsi_host_get(shost
);
498 session
->dev
.parent
= &shost
->shost_gendev
;
499 session
->dev
.release
= iscsi_session_release
;
500 device_initialize(&session
->dev
);
501 if (transport
->sessiondata_size
)
502 session
->dd_data
= &session
[1];
505 EXPORT_SYMBOL_GPL(iscsi_alloc_session
);
507 int iscsi_add_session(struct iscsi_cls_session
*session
, unsigned int target_id
)
509 struct Scsi_Host
*shost
= iscsi_session_to_shost(session
);
510 struct iscsi_host
*ihost
;
514 ihost
= shost
->shost_data
;
515 session
->sid
= atomic_add_return(1, &iscsi_session_nr
);
516 session
->target_id
= target_id
;
518 snprintf(session
->dev
.bus_id
, BUS_ID_SIZE
, "session%u",
520 err
= device_add(&session
->dev
);
522 iscsi_cls_session_printk(KERN_ERR
, session
,
523 "could not register session's dev\n");
526 transport_register_device(&session
->dev
);
528 spin_lock_irqsave(&sesslock
, flags
);
529 list_add(&session
->sess_list
, &sesslist
);
530 spin_unlock_irqrestore(&sesslock
, flags
);
532 mutex_lock(&ihost
->mutex
);
533 list_add(&session
->host_list
, &ihost
->sessions
);
534 mutex_unlock(&ihost
->mutex
);
536 iscsi_session_event(session
, ISCSI_KEVENT_CREATE_SESSION
);
540 scsi_host_put(shost
);
543 EXPORT_SYMBOL_GPL(iscsi_add_session
);
546 * iscsi_create_session - create iscsi class session
548 * @transport: iscsi transport
549 * @target_id: which target
551 * This can be called from a LLD or iscsi_transport.
553 struct iscsi_cls_session
*
554 iscsi_create_session(struct Scsi_Host
*shost
,
555 struct iscsi_transport
*transport
,
556 unsigned int target_id
)
558 struct iscsi_cls_session
*session
;
560 session
= iscsi_alloc_session(shost
, transport
);
564 if (iscsi_add_session(session
, target_id
)) {
565 iscsi_free_session(session
);
570 EXPORT_SYMBOL_GPL(iscsi_create_session
);
572 static void iscsi_conn_release(struct device
*dev
)
574 struct iscsi_cls_conn
*conn
= iscsi_dev_to_conn(dev
);
575 struct device
*parent
= conn
->dev
.parent
;
581 static int iscsi_is_conn_dev(const struct device
*dev
)
583 return dev
->release
== iscsi_conn_release
;
586 static int iscsi_iter_destroy_conn_fn(struct device
*dev
, void *data
)
588 if (!iscsi_is_conn_dev(dev
))
590 return iscsi_destroy_conn(iscsi_dev_to_conn(dev
));
593 void iscsi_remove_session(struct iscsi_cls_session
*session
)
595 struct Scsi_Host
*shost
= iscsi_session_to_shost(session
);
596 struct iscsi_host
*ihost
= shost
->shost_data
;
600 spin_lock_irqsave(&sesslock
, flags
);
601 list_del(&session
->sess_list
);
602 spin_unlock_irqrestore(&sesslock
, flags
);
604 /* make sure there are no blocks/unblocks queued */
605 flush_workqueue(iscsi_eh_timer_workq
);
606 /* make sure the timedout callout is not running */
607 if (!cancel_delayed_work(&session
->recovery_work
))
608 flush_workqueue(iscsi_eh_timer_workq
);
610 * If we are blocked let commands flow again. The lld or iscsi
611 * layer should set up the queuecommand to fail commands.
612 * We assume that LLD will not be calling block/unblock while
613 * removing the session.
615 spin_lock_irqsave(&session
->lock
, flags
);
616 session
->state
= ISCSI_SESSION_FREE
;
617 spin_unlock_irqrestore(&session
->lock
, flags
);
619 scsi_target_unblock(&session
->dev
);
620 /* flush running scans then delete devices */
621 flush_workqueue(ihost
->scan_workq
);
622 __iscsi_unbind_session(&session
->unbind_work
);
624 /* hw iscsi may not have removed all connections from session */
625 err
= device_for_each_child(&session
->dev
, NULL
,
626 iscsi_iter_destroy_conn_fn
);
628 iscsi_cls_session_printk(KERN_ERR
, session
,
629 "Could not delete all connections "
630 "for session. Error %d.\n", err
);
632 transport_unregister_device(&session
->dev
);
633 device_del(&session
->dev
);
635 EXPORT_SYMBOL_GPL(iscsi_remove_session
);
637 void iscsi_free_session(struct iscsi_cls_session
*session
)
639 iscsi_session_event(session
, ISCSI_KEVENT_DESTROY_SESSION
);
640 put_device(&session
->dev
);
642 EXPORT_SYMBOL_GPL(iscsi_free_session
);
645 * iscsi_destroy_session - destroy iscsi session
646 * @session: iscsi_session
648 * Can be called by a LLD or iscsi_transport. There must not be
649 * any running connections.
651 int iscsi_destroy_session(struct iscsi_cls_session
*session
)
653 iscsi_remove_session(session
);
654 iscsi_free_session(session
);
657 EXPORT_SYMBOL_GPL(iscsi_destroy_session
);
660 * iscsi_create_conn - create iscsi class connection
661 * @session: iscsi cls session
662 * @cid: connection id
664 * This can be called from a LLD or iscsi_transport. The connection
665 * is child of the session so cid must be unique for all connections
668 * Since we do not support MCS, cid will normally be zero. In some cases
669 * for software iscsi we could be trying to preallocate a connection struct
670 * in which case there could be two connection structs and cid would be
673 struct iscsi_cls_conn
*
674 iscsi_create_conn(struct iscsi_cls_session
*session
, uint32_t cid
)
676 struct iscsi_transport
*transport
= session
->transport
;
677 struct iscsi_cls_conn
*conn
;
681 conn
= kzalloc(sizeof(*conn
) + transport
->conndata_size
, GFP_KERNEL
);
685 if (transport
->conndata_size
)
686 conn
->dd_data
= &conn
[1];
688 INIT_LIST_HEAD(&conn
->conn_list
);
689 conn
->transport
= transport
;
692 /* this is released in the dev's release function */
693 if (!get_device(&session
->dev
))
696 snprintf(conn
->dev
.bus_id
, BUS_ID_SIZE
, "connection%d:%u",
698 conn
->dev
.parent
= &session
->dev
;
699 conn
->dev
.release
= iscsi_conn_release
;
700 err
= device_register(&conn
->dev
);
702 iscsi_cls_session_printk(KERN_ERR
, session
, "could not "
703 "register connection's dev\n");
704 goto release_parent_ref
;
706 transport_register_device(&conn
->dev
);
708 spin_lock_irqsave(&connlock
, flags
);
709 list_add(&conn
->conn_list
, &connlist
);
711 spin_unlock_irqrestore(&connlock
, flags
);
715 put_device(&session
->dev
);
721 EXPORT_SYMBOL_GPL(iscsi_create_conn
);
724 * iscsi_destroy_conn - destroy iscsi class connection
725 * @conn: iscsi cls session
727 * This can be called from a LLD or iscsi_transport.
729 int iscsi_destroy_conn(struct iscsi_cls_conn
*conn
)
733 spin_lock_irqsave(&connlock
, flags
);
735 list_del(&conn
->conn_list
);
736 spin_unlock_irqrestore(&connlock
, flags
);
738 transport_unregister_device(&conn
->dev
);
739 device_unregister(&conn
->dev
);
742 EXPORT_SYMBOL_GPL(iscsi_destroy_conn
);
745 * iscsi interface functions
747 static struct iscsi_internal
*
748 iscsi_if_transport_lookup(struct iscsi_transport
*tt
)
750 struct iscsi_internal
*priv
;
753 spin_lock_irqsave(&iscsi_transport_lock
, flags
);
754 list_for_each_entry(priv
, &iscsi_transports
, list
) {
755 if (tt
== priv
->iscsi_transport
) {
756 spin_unlock_irqrestore(&iscsi_transport_lock
, flags
);
760 spin_unlock_irqrestore(&iscsi_transport_lock
, flags
);
765 iscsi_broadcast_skb(struct sk_buff
*skb
, gfp_t gfp
)
769 rc
= netlink_broadcast(nls
, skb
, 0, 1, gfp
);
771 printk(KERN_ERR
"iscsi: can not broadcast skb (%d)\n", rc
);
779 iscsi_unicast_skb(struct sk_buff
*skb
, int pid
)
783 rc
= netlink_unicast(nls
, skb
, pid
, MSG_DONTWAIT
);
785 printk(KERN_ERR
"iscsi: can not unicast skb (%d)\n", rc
);
792 int iscsi_recv_pdu(struct iscsi_cls_conn
*conn
, struct iscsi_hdr
*hdr
,
793 char *data
, uint32_t data_size
)
795 struct nlmsghdr
*nlh
;
797 struct iscsi_uevent
*ev
;
799 struct iscsi_internal
*priv
;
800 int len
= NLMSG_SPACE(sizeof(*ev
) + sizeof(struct iscsi_hdr
) +
803 priv
= iscsi_if_transport_lookup(conn
->transport
);
807 skb
= alloc_skb(len
, GFP_ATOMIC
);
809 iscsi_conn_error(conn
, ISCSI_ERR_CONN_FAILED
);
810 iscsi_cls_conn_printk(KERN_ERR
, conn
, "can not deliver "
811 "control PDU: OOM\n");
815 nlh
= __nlmsg_put(skb
, priv
->daemon_pid
, 0, 0, (len
- sizeof(*nlh
)), 0);
816 ev
= NLMSG_DATA(nlh
);
817 memset(ev
, 0, sizeof(*ev
));
818 ev
->transport_handle
= iscsi_handle(conn
->transport
);
819 ev
->type
= ISCSI_KEVENT_RECV_PDU
;
820 ev
->r
.recv_req
.cid
= conn
->cid
;
821 ev
->r
.recv_req
.sid
= iscsi_conn_get_sid(conn
);
822 pdu
= (char*)ev
+ sizeof(*ev
);
823 memcpy(pdu
, hdr
, sizeof(struct iscsi_hdr
));
824 memcpy(pdu
+ sizeof(struct iscsi_hdr
), data
, data_size
);
826 return iscsi_unicast_skb(skb
, priv
->daemon_pid
);
828 EXPORT_SYMBOL_GPL(iscsi_recv_pdu
);
830 void iscsi_conn_error(struct iscsi_cls_conn
*conn
, enum iscsi_err error
)
832 struct nlmsghdr
*nlh
;
834 struct iscsi_uevent
*ev
;
835 struct iscsi_internal
*priv
;
836 int len
= NLMSG_SPACE(sizeof(*ev
));
838 priv
= iscsi_if_transport_lookup(conn
->transport
);
842 skb
= alloc_skb(len
, GFP_ATOMIC
);
844 iscsi_cls_conn_printk(KERN_ERR
, conn
, "gracefully ignored "
845 "conn error (%d)\n", error
);
849 nlh
= __nlmsg_put(skb
, priv
->daemon_pid
, 0, 0, (len
- sizeof(*nlh
)), 0);
850 ev
= NLMSG_DATA(nlh
);
851 ev
->transport_handle
= iscsi_handle(conn
->transport
);
852 ev
->type
= ISCSI_KEVENT_CONN_ERROR
;
853 ev
->r
.connerror
.error
= error
;
854 ev
->r
.connerror
.cid
= conn
->cid
;
855 ev
->r
.connerror
.sid
= iscsi_conn_get_sid(conn
);
857 iscsi_broadcast_skb(skb
, GFP_ATOMIC
);
859 iscsi_cls_conn_printk(KERN_INFO
, conn
, "detected conn error (%d)\n",
862 EXPORT_SYMBOL_GPL(iscsi_conn_error
);
865 iscsi_if_send_reply(int pid
, int seq
, int type
, int done
, int multi
,
866 void *payload
, int size
)
869 struct nlmsghdr
*nlh
;
870 int len
= NLMSG_SPACE(size
);
871 int flags
= multi
? NLM_F_MULTI
: 0;
872 int t
= done
? NLMSG_DONE
: type
;
874 skb
= alloc_skb(len
, GFP_ATOMIC
);
876 printk(KERN_ERR
"Could not allocate skb to send reply.\n");
880 nlh
= __nlmsg_put(skb
, pid
, seq
, t
, (len
- sizeof(*nlh
)), 0);
881 nlh
->nlmsg_flags
= flags
;
882 memcpy(NLMSG_DATA(nlh
), payload
, size
);
883 return iscsi_unicast_skb(skb
, pid
);
887 iscsi_if_get_stats(struct iscsi_transport
*transport
, struct nlmsghdr
*nlh
)
889 struct iscsi_uevent
*ev
= NLMSG_DATA(nlh
);
890 struct iscsi_stats
*stats
;
891 struct sk_buff
*skbstat
;
892 struct iscsi_cls_conn
*conn
;
893 struct nlmsghdr
*nlhstat
;
894 struct iscsi_uevent
*evstat
;
895 struct iscsi_internal
*priv
;
896 int len
= NLMSG_SPACE(sizeof(*ev
) +
897 sizeof(struct iscsi_stats
) +
898 sizeof(struct iscsi_stats_custom
) *
899 ISCSI_STATS_CUSTOM_MAX
);
902 priv
= iscsi_if_transport_lookup(transport
);
906 conn
= iscsi_conn_lookup(ev
->u
.get_stats
.sid
, ev
->u
.get_stats
.cid
);
913 skbstat
= alloc_skb(len
, GFP_ATOMIC
);
915 iscsi_cls_conn_printk(KERN_ERR
, conn
, "can not "
916 "deliver stats: OOM\n");
920 nlhstat
= __nlmsg_put(skbstat
, priv
->daemon_pid
, 0, 0,
921 (len
- sizeof(*nlhstat
)), 0);
922 evstat
= NLMSG_DATA(nlhstat
);
923 memset(evstat
, 0, sizeof(*evstat
));
924 evstat
->transport_handle
= iscsi_handle(conn
->transport
);
925 evstat
->type
= nlh
->nlmsg_type
;
926 evstat
->u
.get_stats
.cid
=
928 evstat
->u
.get_stats
.sid
=
930 stats
= (struct iscsi_stats
*)
931 ((char*)evstat
+ sizeof(*evstat
));
932 memset(stats
, 0, sizeof(*stats
));
934 transport
->get_stats(conn
, stats
);
935 actual_size
= NLMSG_SPACE(sizeof(struct iscsi_uevent
) +
936 sizeof(struct iscsi_stats
) +
937 sizeof(struct iscsi_stats_custom
) *
938 stats
->custom_length
);
939 actual_size
-= sizeof(*nlhstat
);
940 actual_size
= NLMSG_LENGTH(actual_size
);
941 skb_trim(skbstat
, NLMSG_ALIGN(actual_size
));
942 nlhstat
->nlmsg_len
= actual_size
;
944 err
= iscsi_unicast_skb(skbstat
, priv
->daemon_pid
);
945 } while (err
< 0 && err
!= -ECONNREFUSED
);
951 * iscsi_session_event - send session destr. completion event
952 * @session: iscsi class session
953 * @event: type of event
955 int iscsi_session_event(struct iscsi_cls_session
*session
,
956 enum iscsi_uevent_e event
)
958 struct iscsi_internal
*priv
;
959 struct Scsi_Host
*shost
;
960 struct iscsi_uevent
*ev
;
962 struct nlmsghdr
*nlh
;
963 int rc
, len
= NLMSG_SPACE(sizeof(*ev
));
965 priv
= iscsi_if_transport_lookup(session
->transport
);
968 shost
= iscsi_session_to_shost(session
);
970 skb
= alloc_skb(len
, GFP_KERNEL
);
972 iscsi_cls_session_printk(KERN_ERR
, session
,
973 "Cannot notify userspace of session "
974 "event %u\n", event
);
978 nlh
= __nlmsg_put(skb
, priv
->daemon_pid
, 0, 0, (len
- sizeof(*nlh
)), 0);
979 ev
= NLMSG_DATA(nlh
);
980 ev
->transport_handle
= iscsi_handle(session
->transport
);
984 case ISCSI_KEVENT_DESTROY_SESSION
:
985 ev
->r
.d_session
.host_no
= shost
->host_no
;
986 ev
->r
.d_session
.sid
= session
->sid
;
988 case ISCSI_KEVENT_CREATE_SESSION
:
989 ev
->r
.c_session_ret
.host_no
= shost
->host_no
;
990 ev
->r
.c_session_ret
.sid
= session
->sid
;
992 case ISCSI_KEVENT_UNBIND_SESSION
:
993 ev
->r
.unbind_session
.host_no
= shost
->host_no
;
994 ev
->r
.unbind_session
.sid
= session
->sid
;
997 iscsi_cls_session_printk(KERN_ERR
, session
, "Invalid event "
1004 * this will occur if the daemon is not up, so we just warn
1005 * the user and when the daemon is restarted it will handle it
1007 rc
= iscsi_broadcast_skb(skb
, GFP_KERNEL
);
1009 iscsi_cls_session_printk(KERN_ERR
, session
,
1010 "Cannot notify userspace of session "
1011 "event %u. Check iscsi daemon\n",
1015 EXPORT_SYMBOL_GPL(iscsi_session_event
);
1018 iscsi_if_create_session(struct iscsi_internal
*priv
, struct iscsi_uevent
*ev
)
1020 struct iscsi_transport
*transport
= priv
->iscsi_transport
;
1021 struct iscsi_cls_session
*session
;
1024 session
= transport
->create_session(transport
, &priv
->t
,
1025 ev
->u
.c_session
.cmds_max
,
1026 ev
->u
.c_session
.queue_depth
,
1027 ev
->u
.c_session
.initial_cmdsn
,
1032 ev
->r
.c_session_ret
.host_no
= hostno
;
1033 ev
->r
.c_session_ret
.sid
= session
->sid
;
1038 iscsi_if_create_conn(struct iscsi_transport
*transport
, struct iscsi_uevent
*ev
)
1040 struct iscsi_cls_conn
*conn
;
1041 struct iscsi_cls_session
*session
;
1043 session
= iscsi_session_lookup(ev
->u
.c_conn
.sid
);
1045 printk(KERN_ERR
"iscsi: invalid session %d.\n",
1050 conn
= transport
->create_conn(session
, ev
->u
.c_conn
.cid
);
1052 iscsi_cls_session_printk(KERN_ERR
, session
,
1053 "couldn't create a new connection.");
1057 ev
->r
.c_conn_ret
.sid
= session
->sid
;
1058 ev
->r
.c_conn_ret
.cid
= conn
->cid
;
1063 iscsi_if_destroy_conn(struct iscsi_transport
*transport
, struct iscsi_uevent
*ev
)
1065 struct iscsi_cls_conn
*conn
;
1067 conn
= iscsi_conn_lookup(ev
->u
.d_conn
.sid
, ev
->u
.d_conn
.cid
);
1071 if (transport
->destroy_conn
)
1072 transport
->destroy_conn(conn
);
1077 iscsi_set_param(struct iscsi_transport
*transport
, struct iscsi_uevent
*ev
)
1079 char *data
= (char*)ev
+ sizeof(*ev
);
1080 struct iscsi_cls_conn
*conn
;
1081 struct iscsi_cls_session
*session
;
1082 int err
= 0, value
= 0;
1084 session
= iscsi_session_lookup(ev
->u
.set_param
.sid
);
1085 conn
= iscsi_conn_lookup(ev
->u
.set_param
.sid
, ev
->u
.set_param
.cid
);
1086 if (!conn
|| !session
)
1089 switch (ev
->u
.set_param
.param
) {
1090 case ISCSI_PARAM_SESS_RECOVERY_TMO
:
1091 sscanf(data
, "%d", &value
);
1093 session
->recovery_tmo
= value
;
1096 err
= transport
->set_param(conn
, ev
->u
.set_param
.param
,
1097 data
, ev
->u
.set_param
.len
);
1104 iscsi_if_transport_ep(struct iscsi_transport
*transport
,
1105 struct iscsi_uevent
*ev
, int msg_type
)
1107 struct sockaddr
*dst_addr
;
1111 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT
:
1112 if (!transport
->ep_connect
)
1115 dst_addr
= (struct sockaddr
*)((char*)ev
+ sizeof(*ev
));
1116 rc
= transport
->ep_connect(dst_addr
,
1117 ev
->u
.ep_connect
.non_blocking
,
1118 &ev
->r
.ep_connect_ret
.handle
);
1120 case ISCSI_UEVENT_TRANSPORT_EP_POLL
:
1121 if (!transport
->ep_poll
)
1124 ev
->r
.retcode
= transport
->ep_poll(ev
->u
.ep_poll
.ep_handle
,
1125 ev
->u
.ep_poll
.timeout_ms
);
1127 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT
:
1128 if (!transport
->ep_disconnect
)
1131 transport
->ep_disconnect(ev
->u
.ep_disconnect
.ep_handle
);
1138 iscsi_tgt_dscvr(struct iscsi_transport
*transport
,
1139 struct iscsi_uevent
*ev
)
1141 struct Scsi_Host
*shost
;
1142 struct sockaddr
*dst_addr
;
1145 if (!transport
->tgt_dscvr
)
1148 shost
= scsi_host_lookup(ev
->u
.tgt_dscvr
.host_no
);
1149 if (IS_ERR(shost
)) {
1150 printk(KERN_ERR
"target discovery could not find host no %u\n",
1151 ev
->u
.tgt_dscvr
.host_no
);
1156 dst_addr
= (struct sockaddr
*)((char*)ev
+ sizeof(*ev
));
1157 err
= transport
->tgt_dscvr(shost
, ev
->u
.tgt_dscvr
.type
,
1158 ev
->u
.tgt_dscvr
.enable
, dst_addr
);
1159 scsi_host_put(shost
);
1164 iscsi_set_host_param(struct iscsi_transport
*transport
,
1165 struct iscsi_uevent
*ev
)
1167 char *data
= (char*)ev
+ sizeof(*ev
);
1168 struct Scsi_Host
*shost
;
1171 if (!transport
->set_host_param
)
1174 shost
= scsi_host_lookup(ev
->u
.set_host_param
.host_no
);
1175 if (IS_ERR(shost
)) {
1176 printk(KERN_ERR
"set_host_param could not find host no %u\n",
1177 ev
->u
.set_host_param
.host_no
);
1181 err
= transport
->set_host_param(shost
, ev
->u
.set_host_param
.param
,
1182 data
, ev
->u
.set_host_param
.len
);
1183 scsi_host_put(shost
);
1188 iscsi_if_recv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1191 struct iscsi_uevent
*ev
= NLMSG_DATA(nlh
);
1192 struct iscsi_transport
*transport
= NULL
;
1193 struct iscsi_internal
*priv
;
1194 struct iscsi_cls_session
*session
;
1195 struct iscsi_cls_conn
*conn
;
1197 priv
= iscsi_if_transport_lookup(iscsi_ptr(ev
->transport_handle
));
1200 transport
= priv
->iscsi_transport
;
1202 if (!try_module_get(transport
->owner
))
1205 priv
->daemon_pid
= NETLINK_CREDS(skb
)->pid
;
1207 switch (nlh
->nlmsg_type
) {
1208 case ISCSI_UEVENT_CREATE_SESSION
:
1209 err
= iscsi_if_create_session(priv
, ev
);
1211 case ISCSI_UEVENT_DESTROY_SESSION
:
1212 session
= iscsi_session_lookup(ev
->u
.d_session
.sid
);
1214 transport
->destroy_session(session
);
1218 case ISCSI_UEVENT_UNBIND_SESSION
:
1219 session
= iscsi_session_lookup(ev
->u
.d_session
.sid
);
1221 iscsi_unbind_session(session
);
1225 case ISCSI_UEVENT_CREATE_CONN
:
1226 err
= iscsi_if_create_conn(transport
, ev
);
1228 case ISCSI_UEVENT_DESTROY_CONN
:
1229 err
= iscsi_if_destroy_conn(transport
, ev
);
1231 case ISCSI_UEVENT_BIND_CONN
:
1232 session
= iscsi_session_lookup(ev
->u
.b_conn
.sid
);
1233 conn
= iscsi_conn_lookup(ev
->u
.b_conn
.sid
, ev
->u
.b_conn
.cid
);
1235 if (session
&& conn
)
1236 ev
->r
.retcode
= transport
->bind_conn(session
, conn
,
1237 ev
->u
.b_conn
.transport_eph
,
1238 ev
->u
.b_conn
.is_leading
);
1242 case ISCSI_UEVENT_SET_PARAM
:
1243 err
= iscsi_set_param(transport
, ev
);
1245 case ISCSI_UEVENT_START_CONN
:
1246 conn
= iscsi_conn_lookup(ev
->u
.start_conn
.sid
, ev
->u
.start_conn
.cid
);
1248 ev
->r
.retcode
= transport
->start_conn(conn
);
1252 case ISCSI_UEVENT_STOP_CONN
:
1253 conn
= iscsi_conn_lookup(ev
->u
.stop_conn
.sid
, ev
->u
.stop_conn
.cid
);
1255 transport
->stop_conn(conn
, ev
->u
.stop_conn
.flag
);
1259 case ISCSI_UEVENT_SEND_PDU
:
1260 conn
= iscsi_conn_lookup(ev
->u
.send_pdu
.sid
, ev
->u
.send_pdu
.cid
);
1262 ev
->r
.retcode
= transport
->send_pdu(conn
,
1263 (struct iscsi_hdr
*)((char*)ev
+ sizeof(*ev
)),
1264 (char*)ev
+ sizeof(*ev
) + ev
->u
.send_pdu
.hdr_size
,
1265 ev
->u
.send_pdu
.data_size
);
1269 case ISCSI_UEVENT_GET_STATS
:
1270 err
= iscsi_if_get_stats(transport
, nlh
);
1272 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT
:
1273 case ISCSI_UEVENT_TRANSPORT_EP_POLL
:
1274 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT
:
1275 err
= iscsi_if_transport_ep(transport
, ev
, nlh
->nlmsg_type
);
1277 case ISCSI_UEVENT_TGT_DSCVR
:
1278 err
= iscsi_tgt_dscvr(transport
, ev
);
1280 case ISCSI_UEVENT_SET_HOST_PARAM
:
1281 err
= iscsi_set_host_param(transport
, ev
);
1288 module_put(transport
->owner
);
1293 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1294 * Malformed skbs with wrong lengths or invalid creds are not processed.
1297 iscsi_if_rx(struct sk_buff
*skb
)
1299 mutex_lock(&rx_queue_mutex
);
1300 while (skb
->len
>= NLMSG_SPACE(0)) {
1303 struct nlmsghdr
*nlh
;
1304 struct iscsi_uevent
*ev
;
1306 nlh
= nlmsg_hdr(skb
);
1307 if (nlh
->nlmsg_len
< sizeof(*nlh
) ||
1308 skb
->len
< nlh
->nlmsg_len
) {
1312 ev
= NLMSG_DATA(nlh
);
1313 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
1314 if (rlen
> skb
->len
)
1317 err
= iscsi_if_recv_msg(skb
, nlh
);
1319 ev
->type
= ISCSI_KEVENT_IF_ERROR
;
1324 * special case for GET_STATS:
1325 * on success - sending reply and stats from
1326 * inside of if_recv_msg(),
1327 * on error - fall through.
1329 if (ev
->type
== ISCSI_UEVENT_GET_STATS
&& !err
)
1331 err
= iscsi_if_send_reply(
1332 NETLINK_CREDS(skb
)->pid
, nlh
->nlmsg_seq
,
1333 nlh
->nlmsg_type
, 0, 0, ev
, sizeof(*ev
));
1334 } while (err
< 0 && err
!= -ECONNREFUSED
);
1335 skb_pull(skb
, rlen
);
1337 mutex_unlock(&rx_queue_mutex
);
1340 #define iscsi_cdev_to_conn(_cdev) \
1341 iscsi_dev_to_conn(_cdev->dev)
1343 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1344 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1345 __ATTR(_name,_mode,_show,_store)
1348 * iSCSI connection attrs
1350 #define iscsi_conn_attr_show(param) \
1352 show_conn_param_##param(struct class_device *cdev, char *buf) \
1354 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1355 struct iscsi_transport *t = conn->transport; \
1356 return t->get_conn_param(conn, param, buf); \
1359 #define iscsi_conn_attr(field, param) \
1360 iscsi_conn_attr_show(param) \
1361 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
1364 iscsi_conn_attr(max_recv_dlength
, ISCSI_PARAM_MAX_RECV_DLENGTH
);
1365 iscsi_conn_attr(max_xmit_dlength
, ISCSI_PARAM_MAX_XMIT_DLENGTH
);
1366 iscsi_conn_attr(header_digest
, ISCSI_PARAM_HDRDGST_EN
);
1367 iscsi_conn_attr(data_digest
, ISCSI_PARAM_DATADGST_EN
);
1368 iscsi_conn_attr(ifmarker
, ISCSI_PARAM_IFMARKER_EN
);
1369 iscsi_conn_attr(ofmarker
, ISCSI_PARAM_OFMARKER_EN
);
1370 iscsi_conn_attr(persistent_port
, ISCSI_PARAM_PERSISTENT_PORT
);
1371 iscsi_conn_attr(port
, ISCSI_PARAM_CONN_PORT
);
1372 iscsi_conn_attr(exp_statsn
, ISCSI_PARAM_EXP_STATSN
);
1373 iscsi_conn_attr(persistent_address
, ISCSI_PARAM_PERSISTENT_ADDRESS
);
1374 iscsi_conn_attr(address
, ISCSI_PARAM_CONN_ADDRESS
);
1375 iscsi_conn_attr(ping_tmo
, ISCSI_PARAM_PING_TMO
);
1376 iscsi_conn_attr(recv_tmo
, ISCSI_PARAM_RECV_TMO
);
1378 #define iscsi_cdev_to_session(_cdev) \
1379 iscsi_dev_to_session(_cdev->dev)
1382 * iSCSI session attrs
1384 #define iscsi_session_attr_show(param, perm) \
1386 show_session_param_##param(struct class_device *cdev, char *buf) \
1388 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1389 struct iscsi_transport *t = session->transport; \
1391 if (perm && !capable(CAP_SYS_ADMIN)) \
1393 return t->get_session_param(session, param, buf); \
1396 #define iscsi_session_attr(field, param, perm) \
1397 iscsi_session_attr_show(param, perm) \
1398 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
1401 iscsi_session_attr(targetname
, ISCSI_PARAM_TARGET_NAME
, 0);
1402 iscsi_session_attr(initial_r2t
, ISCSI_PARAM_INITIAL_R2T_EN
, 0);
1403 iscsi_session_attr(max_outstanding_r2t
, ISCSI_PARAM_MAX_R2T
, 0);
1404 iscsi_session_attr(immediate_data
, ISCSI_PARAM_IMM_DATA_EN
, 0);
1405 iscsi_session_attr(first_burst_len
, ISCSI_PARAM_FIRST_BURST
, 0);
1406 iscsi_session_attr(max_burst_len
, ISCSI_PARAM_MAX_BURST
, 0);
1407 iscsi_session_attr(data_pdu_in_order
, ISCSI_PARAM_PDU_INORDER_EN
, 0);
1408 iscsi_session_attr(data_seq_in_order
, ISCSI_PARAM_DATASEQ_INORDER_EN
, 0);
1409 iscsi_session_attr(erl
, ISCSI_PARAM_ERL
, 0);
1410 iscsi_session_attr(tpgt
, ISCSI_PARAM_TPGT
, 0);
1411 iscsi_session_attr(username
, ISCSI_PARAM_USERNAME
, 1);
1412 iscsi_session_attr(username_in
, ISCSI_PARAM_USERNAME_IN
, 1);
1413 iscsi_session_attr(password
, ISCSI_PARAM_PASSWORD
, 1);
1414 iscsi_session_attr(password_in
, ISCSI_PARAM_PASSWORD_IN
, 1);
1415 iscsi_session_attr(fast_abort
, ISCSI_PARAM_FAST_ABORT
, 0);
1416 iscsi_session_attr(abort_tmo
, ISCSI_PARAM_ABORT_TMO
, 0);
1417 iscsi_session_attr(lu_reset_tmo
, ISCSI_PARAM_LU_RESET_TMO
, 0);
1420 show_priv_session_state(struct class_device
*cdev
, char *buf
)
1422 struct iscsi_cls_session
*session
= iscsi_cdev_to_session(cdev
);
1423 return sprintf(buf
, "%s\n", iscsi_session_state_name(session
->state
));
1425 static ISCSI_CLASS_ATTR(priv_sess
, state
, S_IRUGO
, show_priv_session_state
,
1428 #define iscsi_priv_session_attr_show(field, format) \
1430 show_priv_session_##field(struct class_device *cdev, char *buf) \
1432 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);\
1433 return sprintf(buf, format"\n", session->field); \
1436 #define iscsi_priv_session_attr(field, format) \
1437 iscsi_priv_session_attr_show(field, format) \
1438 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1440 iscsi_priv_session_attr(recovery_tmo
, "%d");
1445 #define iscsi_host_attr_show(param) \
1447 show_host_param_##param(struct class_device *cdev, char *buf) \
1449 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1450 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1451 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1454 #define iscsi_host_attr(field, param) \
1455 iscsi_host_attr_show(param) \
1456 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1459 iscsi_host_attr(netdev
, ISCSI_HOST_PARAM_NETDEV_NAME
);
1460 iscsi_host_attr(hwaddress
, ISCSI_HOST_PARAM_HWADDRESS
);
1461 iscsi_host_attr(ipaddress
, ISCSI_HOST_PARAM_IPADDRESS
);
1462 iscsi_host_attr(initiatorname
, ISCSI_HOST_PARAM_INITIATOR_NAME
);
1464 #define SETUP_PRIV_SESSION_RD_ATTR(field) \
1466 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1471 #define SETUP_SESSION_RD_ATTR(field, param_flag) \
1473 if (tt->param_mask & param_flag) { \
1474 priv->session_attrs[count] = &class_device_attr_sess_##field; \
1479 #define SETUP_CONN_RD_ATTR(field, param_flag) \
1481 if (tt->param_mask & param_flag) { \
1482 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1487 #define SETUP_HOST_RD_ATTR(field, param_flag) \
1489 if (tt->host_param_mask & param_flag) { \
1490 priv->host_attrs[count] = &class_device_attr_host_##field; \
1495 static int iscsi_session_match(struct attribute_container
*cont
,
1498 struct iscsi_cls_session
*session
;
1499 struct Scsi_Host
*shost
;
1500 struct iscsi_internal
*priv
;
1502 if (!iscsi_is_session_dev(dev
))
1505 session
= iscsi_dev_to_session(dev
);
1506 shost
= iscsi_session_to_shost(session
);
1507 if (!shost
->transportt
)
1510 priv
= to_iscsi_internal(shost
->transportt
);
1511 if (priv
->session_cont
.ac
.class != &iscsi_session_class
.class)
1514 return &priv
->session_cont
.ac
== cont
;
1517 static int iscsi_conn_match(struct attribute_container
*cont
,
1520 struct iscsi_cls_session
*session
;
1521 struct iscsi_cls_conn
*conn
;
1522 struct Scsi_Host
*shost
;
1523 struct iscsi_internal
*priv
;
1525 if (!iscsi_is_conn_dev(dev
))
1528 conn
= iscsi_dev_to_conn(dev
);
1529 session
= iscsi_dev_to_session(conn
->dev
.parent
);
1530 shost
= iscsi_session_to_shost(session
);
1532 if (!shost
->transportt
)
1535 priv
= to_iscsi_internal(shost
->transportt
);
1536 if (priv
->conn_cont
.ac
.class != &iscsi_connection_class
.class)
1539 return &priv
->conn_cont
.ac
== cont
;
1542 static int iscsi_host_match(struct attribute_container
*cont
,
1545 struct Scsi_Host
*shost
;
1546 struct iscsi_internal
*priv
;
1548 if (!scsi_is_host_device(dev
))
1551 shost
= dev_to_shost(dev
);
1552 if (!shost
->transportt
||
1553 shost
->transportt
->host_attrs
.ac
.class != &iscsi_host_class
.class)
1556 priv
= to_iscsi_internal(shost
->transportt
);
1557 return &priv
->t
.host_attrs
.ac
== cont
;
1560 struct scsi_transport_template
*
1561 iscsi_register_transport(struct iscsi_transport
*tt
)
1563 struct iscsi_internal
*priv
;
1564 unsigned long flags
;
1569 priv
= iscsi_if_transport_lookup(tt
);
1573 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1576 INIT_LIST_HEAD(&priv
->list
);
1577 priv
->daemon_pid
= -1;
1578 priv
->iscsi_transport
= tt
;
1579 priv
->t
.user_scan
= iscsi_user_scan
;
1581 priv
->cdev
.class = &iscsi_transport_class
;
1582 snprintf(priv
->cdev
.class_id
, BUS_ID_SIZE
, "%s", tt
->name
);
1583 err
= class_device_register(&priv
->cdev
);
1587 err
= sysfs_create_group(&priv
->cdev
.kobj
, &iscsi_transport_group
);
1589 goto unregister_cdev
;
1591 /* host parameters */
1592 priv
->t
.host_attrs
.ac
.attrs
= &priv
->host_attrs
[0];
1593 priv
->t
.host_attrs
.ac
.class = &iscsi_host_class
.class;
1594 priv
->t
.host_attrs
.ac
.match
= iscsi_host_match
;
1595 priv
->t
.host_size
= sizeof(struct iscsi_host
);
1596 transport_container_register(&priv
->t
.host_attrs
);
1598 SETUP_HOST_RD_ATTR(netdev
, ISCSI_HOST_NETDEV_NAME
);
1599 SETUP_HOST_RD_ATTR(ipaddress
, ISCSI_HOST_IPADDRESS
);
1600 SETUP_HOST_RD_ATTR(hwaddress
, ISCSI_HOST_HWADDRESS
);
1601 SETUP_HOST_RD_ATTR(initiatorname
, ISCSI_HOST_INITIATOR_NAME
);
1602 BUG_ON(count
> ISCSI_HOST_ATTRS
);
1603 priv
->host_attrs
[count
] = NULL
;
1606 /* connection parameters */
1607 priv
->conn_cont
.ac
.attrs
= &priv
->conn_attrs
[0];
1608 priv
->conn_cont
.ac
.class = &iscsi_connection_class
.class;
1609 priv
->conn_cont
.ac
.match
= iscsi_conn_match
;
1610 transport_container_register(&priv
->conn_cont
);
1612 SETUP_CONN_RD_ATTR(max_recv_dlength
, ISCSI_MAX_RECV_DLENGTH
);
1613 SETUP_CONN_RD_ATTR(max_xmit_dlength
, ISCSI_MAX_XMIT_DLENGTH
);
1614 SETUP_CONN_RD_ATTR(header_digest
, ISCSI_HDRDGST_EN
);
1615 SETUP_CONN_RD_ATTR(data_digest
, ISCSI_DATADGST_EN
);
1616 SETUP_CONN_RD_ATTR(ifmarker
, ISCSI_IFMARKER_EN
);
1617 SETUP_CONN_RD_ATTR(ofmarker
, ISCSI_OFMARKER_EN
);
1618 SETUP_CONN_RD_ATTR(address
, ISCSI_CONN_ADDRESS
);
1619 SETUP_CONN_RD_ATTR(port
, ISCSI_CONN_PORT
);
1620 SETUP_CONN_RD_ATTR(exp_statsn
, ISCSI_EXP_STATSN
);
1621 SETUP_CONN_RD_ATTR(persistent_address
, ISCSI_PERSISTENT_ADDRESS
);
1622 SETUP_CONN_RD_ATTR(persistent_port
, ISCSI_PERSISTENT_PORT
);
1623 SETUP_CONN_RD_ATTR(ping_tmo
, ISCSI_PING_TMO
);
1624 SETUP_CONN_RD_ATTR(recv_tmo
, ISCSI_RECV_TMO
);
1626 BUG_ON(count
> ISCSI_CONN_ATTRS
);
1627 priv
->conn_attrs
[count
] = NULL
;
1630 /* session parameters */
1631 priv
->session_cont
.ac
.attrs
= &priv
->session_attrs
[0];
1632 priv
->session_cont
.ac
.class = &iscsi_session_class
.class;
1633 priv
->session_cont
.ac
.match
= iscsi_session_match
;
1634 transport_container_register(&priv
->session_cont
);
1636 SETUP_SESSION_RD_ATTR(initial_r2t
, ISCSI_INITIAL_R2T_EN
);
1637 SETUP_SESSION_RD_ATTR(max_outstanding_r2t
, ISCSI_MAX_R2T
);
1638 SETUP_SESSION_RD_ATTR(immediate_data
, ISCSI_IMM_DATA_EN
);
1639 SETUP_SESSION_RD_ATTR(first_burst_len
, ISCSI_FIRST_BURST
);
1640 SETUP_SESSION_RD_ATTR(max_burst_len
, ISCSI_MAX_BURST
);
1641 SETUP_SESSION_RD_ATTR(data_pdu_in_order
, ISCSI_PDU_INORDER_EN
);
1642 SETUP_SESSION_RD_ATTR(data_seq_in_order
, ISCSI_DATASEQ_INORDER_EN
);
1643 SETUP_SESSION_RD_ATTR(erl
, ISCSI_ERL
);
1644 SETUP_SESSION_RD_ATTR(targetname
, ISCSI_TARGET_NAME
);
1645 SETUP_SESSION_RD_ATTR(tpgt
, ISCSI_TPGT
);
1646 SETUP_SESSION_RD_ATTR(password
, ISCSI_USERNAME
);
1647 SETUP_SESSION_RD_ATTR(password_in
, ISCSI_USERNAME_IN
);
1648 SETUP_SESSION_RD_ATTR(username
, ISCSI_PASSWORD
);
1649 SETUP_SESSION_RD_ATTR(username_in
, ISCSI_PASSWORD_IN
);
1650 SETUP_SESSION_RD_ATTR(fast_abort
, ISCSI_FAST_ABORT
);
1651 SETUP_SESSION_RD_ATTR(abort_tmo
, ISCSI_ABORT_TMO
);
1652 SETUP_SESSION_RD_ATTR(lu_reset_tmo
,ISCSI_LU_RESET_TMO
);
1653 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo
);
1654 SETUP_PRIV_SESSION_RD_ATTR(state
);
1656 BUG_ON(count
> ISCSI_SESSION_ATTRS
);
1657 priv
->session_attrs
[count
] = NULL
;
1659 spin_lock_irqsave(&iscsi_transport_lock
, flags
);
1660 list_add(&priv
->list
, &iscsi_transports
);
1661 spin_unlock_irqrestore(&iscsi_transport_lock
, flags
);
1663 printk(KERN_NOTICE
"iscsi: registered transport (%s)\n", tt
->name
);
1667 class_device_unregister(&priv
->cdev
);
1672 EXPORT_SYMBOL_GPL(iscsi_register_transport
);
1674 int iscsi_unregister_transport(struct iscsi_transport
*tt
)
1676 struct iscsi_internal
*priv
;
1677 unsigned long flags
;
1681 mutex_lock(&rx_queue_mutex
);
1683 priv
= iscsi_if_transport_lookup(tt
);
1686 spin_lock_irqsave(&iscsi_transport_lock
, flags
);
1687 list_del(&priv
->list
);
1688 spin_unlock_irqrestore(&iscsi_transport_lock
, flags
);
1690 transport_container_unregister(&priv
->conn_cont
);
1691 transport_container_unregister(&priv
->session_cont
);
1692 transport_container_unregister(&priv
->t
.host_attrs
);
1694 sysfs_remove_group(&priv
->cdev
.kobj
, &iscsi_transport_group
);
1695 class_device_unregister(&priv
->cdev
);
1696 mutex_unlock(&rx_queue_mutex
);
1700 EXPORT_SYMBOL_GPL(iscsi_unregister_transport
);
1702 static __init
int iscsi_transport_init(void)
1706 printk(KERN_INFO
"Loading iSCSI transport class v%s.\n",
1707 ISCSI_TRANSPORT_VERSION
);
1709 atomic_set(&iscsi_session_nr
, 0);
1711 err
= class_register(&iscsi_transport_class
);
1715 err
= transport_class_register(&iscsi_host_class
);
1717 goto unregister_transport_class
;
1719 err
= transport_class_register(&iscsi_connection_class
);
1721 goto unregister_host_class
;
1723 err
= transport_class_register(&iscsi_session_class
);
1725 goto unregister_conn_class
;
1727 nls
= netlink_kernel_create(&init_net
, NETLINK_ISCSI
, 1, iscsi_if_rx
, NULL
,
1731 goto unregister_session_class
;
1734 iscsi_eh_timer_workq
= create_singlethread_workqueue("iscsi_eh");
1735 if (!iscsi_eh_timer_workq
)
1741 netlink_kernel_release(nls
);
1742 unregister_session_class
:
1743 transport_class_unregister(&iscsi_session_class
);
1744 unregister_conn_class
:
1745 transport_class_unregister(&iscsi_connection_class
);
1746 unregister_host_class
:
1747 transport_class_unregister(&iscsi_host_class
);
1748 unregister_transport_class
:
1749 class_unregister(&iscsi_transport_class
);
1753 static void __exit
iscsi_transport_exit(void)
1755 destroy_workqueue(iscsi_eh_timer_workq
);
1756 netlink_kernel_release(nls
);
1757 transport_class_unregister(&iscsi_connection_class
);
1758 transport_class_unregister(&iscsi_session_class
);
1759 transport_class_unregister(&iscsi_host_class
);
1760 class_unregister(&iscsi_transport_class
);
1763 module_init(iscsi_transport_init
);
1764 module_exit(iscsi_transport_exit
);
1766 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1767 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1768 "Alex Aizman <itn780@yahoo.com>");
1769 MODULE_DESCRIPTION("iSCSI Transport Interface");
1770 MODULE_LICENSE("GPL");
1771 MODULE_VERSION(ISCSI_TRANSPORT_VERSION
);