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 21
34 #define ISCSI_CONN_ATTRS 13
35 #define ISCSI_HOST_ATTRS 4
37 #define ISCSI_TRANSPORT_VERSION "2.0-870"
39 struct iscsi_internal {
41 struct scsi_transport_template t;
42 struct iscsi_transport *iscsi_transport;
43 struct list_head list;
46 struct device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
47 struct transport_container conn_cont;
48 struct device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
49 struct transport_container session_cont;
50 struct device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
53 static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
54 static struct workqueue_struct *iscsi_eh_timer_workq;
57 * list of registered transports and lock that must
58 * be held while accessing list. The iscsi_transport_lock must
59 * be acquired after the rx_queue_mutex.
61 static LIST_HEAD(iscsi_transports);
62 static DEFINE_SPINLOCK(iscsi_transport_lock);
64 #define to_iscsi_internal(tmpl) \
65 container_of(tmpl, struct iscsi_internal, t)
67 #define dev_to_iscsi_internal(_dev) \
68 container_of(_dev, struct iscsi_internal, dev)
70 static void iscsi_transport_release(struct device *dev)
72 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
77 * iscsi_transport_class represents the iscsi_transports that are
80 static struct class iscsi_transport_class = {
81 .name = "iscsi_transport",
82 .dev_release = iscsi_transport_release,
86 show_transport_handle(struct device *dev, struct device_attribute *attr,
89 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
90 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
92 static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
94 #define show_transport_attr(name, format) \
96 show_transport_##name(struct device *dev, \
97 struct device_attribute *attr,char *buf) \
99 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
100 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
102 static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
104 show_transport_attr(caps, "0x%x");
106 static struct attribute *iscsi_transport_attrs[] = {
107 &dev_attr_handle.attr,
112 static struct attribute_group iscsi_transport_group = {
113 .attrs = iscsi_transport_attrs,
117 * iSCSI endpoint attrs
119 #define iscsi_dev_to_endpoint(_dev) \
120 container_of(_dev, struct iscsi_endpoint, dev)
122 #define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
123 struct device_attribute dev_attr_##_prefix##_##_name = \
124 __ATTR(_name,_mode,_show,_store)
126 static void iscsi_endpoint_release(struct device *dev)
128 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
132 static struct class iscsi_endpoint_class = {
133 .name = "iscsi_endpoint",
134 .dev_release = iscsi_endpoint_release,
138 show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
140 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
141 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
143 static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
145 static struct attribute *iscsi_endpoint_attrs[] = {
146 &dev_attr_ep_handle.attr,
150 static struct attribute_group iscsi_endpoint_group = {
151 .attrs = iscsi_endpoint_attrs,
154 #define ISCSI_MAX_EPID -1
156 static int iscsi_match_epid(struct device *dev, void *data)
158 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
159 uint64_t *epid = (uint64_t *) data;
161 return *epid == ep->id;
164 struct iscsi_endpoint *
165 iscsi_create_endpoint(int dd_size)
168 struct iscsi_endpoint *ep;
172 for (id = 1; id < ISCSI_MAX_EPID; id++) {
173 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
178 if (id == ISCSI_MAX_EPID) {
179 printk(KERN_ERR "Too many connections. Max supported %u\n",
184 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
189 ep->dev.class = &iscsi_endpoint_class;
190 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
191 err = device_register(&ep->dev);
195 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
200 ep->dd_data = &ep[1];
204 device_unregister(&ep->dev);
211 EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
213 void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
215 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
216 device_unregister(&ep->dev);
218 EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
220 struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
222 struct iscsi_endpoint *ep;
225 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
230 ep = iscsi_dev_to_endpoint(dev);
232 * we can drop this now because the interface will prevent
233 * removals and lookups from racing.
238 EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
240 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
243 struct Scsi_Host *shost = dev_to_shost(dev);
244 struct iscsi_cls_host *ihost = shost->shost_data;
246 memset(ihost, 0, sizeof(*ihost));
247 atomic_set(&ihost->nr_scans, 0);
248 mutex_init(&ihost->mutex);
252 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
258 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
264 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
270 static struct sock *nls;
271 static DEFINE_MUTEX(rx_queue_mutex);
273 static LIST_HEAD(sesslist);
274 static DEFINE_SPINLOCK(sesslock);
275 static LIST_HEAD(connlist);
276 static DEFINE_SPINLOCK(connlock);
278 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
280 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
285 * Returns the matching session to a given sid
287 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
290 struct iscsi_cls_session *sess;
292 spin_lock_irqsave(&sesslock, flags);
293 list_for_each_entry(sess, &sesslist, sess_list) {
294 if (sess->sid == sid) {
295 spin_unlock_irqrestore(&sesslock, flags);
299 spin_unlock_irqrestore(&sesslock, flags);
304 * Returns the matching connection to a given sid / cid tuple
306 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
309 struct iscsi_cls_conn *conn;
311 spin_lock_irqsave(&connlock, flags);
312 list_for_each_entry(conn, &connlist, conn_list) {
313 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
314 spin_unlock_irqrestore(&connlock, flags);
318 spin_unlock_irqrestore(&connlock, flags);
323 * The following functions can be used by LLDs that allocate
324 * their own scsi_hosts or by software iscsi LLDs
329 } iscsi_session_state_names[] = {
330 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
331 { ISCSI_SESSION_FAILED, "FAILED" },
332 { ISCSI_SESSION_FREE, "FREE" },
335 static const char *iscsi_session_state_name(int state)
340 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
341 if (iscsi_session_state_names[i].value == state) {
342 name = iscsi_session_state_names[i].name;
349 int iscsi_session_chkready(struct iscsi_cls_session *session)
354 spin_lock_irqsave(&session->lock, flags);
355 switch (session->state) {
356 case ISCSI_SESSION_LOGGED_IN:
359 case ISCSI_SESSION_FAILED:
360 err = DID_IMM_RETRY << 16;
362 case ISCSI_SESSION_FREE:
363 err = DID_TRANSPORT_FAILFAST << 16;
366 err = DID_NO_CONNECT << 16;
369 spin_unlock_irqrestore(&session->lock, flags);
372 EXPORT_SYMBOL_GPL(iscsi_session_chkready);
374 static void iscsi_session_release(struct device *dev)
376 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
377 struct Scsi_Host *shost;
379 shost = iscsi_session_to_shost(session);
380 scsi_host_put(shost);
384 static int iscsi_is_session_dev(const struct device *dev)
386 return dev->release == iscsi_session_release;
389 static int iscsi_iter_session_fn(struct device *dev, void *data)
391 void (* fn) (struct iscsi_cls_session *) = data;
393 if (!iscsi_is_session_dev(dev))
395 fn(iscsi_dev_to_session(dev));
399 void iscsi_host_for_each_session(struct Scsi_Host *shost,
400 void (*fn)(struct iscsi_cls_session *))
402 device_for_each_child(&shost->shost_gendev, fn,
403 iscsi_iter_session_fn);
405 EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
408 * iscsi_scan_finished - helper to report when running scans are done
410 * @time: scan run time
412 * This function can be used by drives like qla4xxx to report to the scsi
413 * layer when the scans it kicked off at module load time are done.
415 int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
417 struct iscsi_cls_host *ihost = shost->shost_data;
419 * qla4xxx will have kicked off some session unblocks before calling
420 * scsi_scan_host, so just wait for them to complete.
422 return !atomic_read(&ihost->nr_scans);
424 EXPORT_SYMBOL_GPL(iscsi_scan_finished);
426 struct iscsi_scan_data {
427 unsigned int channel;
432 static int iscsi_user_scan_session(struct device *dev, void *data)
434 struct iscsi_scan_data *scan_data = data;
435 struct iscsi_cls_session *session;
436 struct Scsi_Host *shost;
437 struct iscsi_cls_host *ihost;
441 if (!iscsi_is_session_dev(dev))
444 session = iscsi_dev_to_session(dev);
445 shost = iscsi_session_to_shost(session);
446 ihost = shost->shost_data;
448 mutex_lock(&ihost->mutex);
449 spin_lock_irqsave(&session->lock, flags);
450 if (session->state != ISCSI_SESSION_LOGGED_IN) {
451 spin_unlock_irqrestore(&session->lock, flags);
452 mutex_unlock(&ihost->mutex);
455 id = session->target_id;
456 spin_unlock_irqrestore(&session->lock, flags);
458 if (id != ISCSI_MAX_TARGET) {
459 if ((scan_data->channel == SCAN_WILD_CARD ||
460 scan_data->channel == 0) &&
461 (scan_data->id == SCAN_WILD_CARD ||
462 scan_data->id == id))
463 scsi_scan_target(&session->dev, 0, id,
466 mutex_unlock(&ihost->mutex);
470 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
473 struct iscsi_scan_data scan_data;
475 scan_data.channel = channel;
479 return device_for_each_child(&shost->shost_gendev, &scan_data,
480 iscsi_user_scan_session);
483 static void iscsi_scan_session(struct work_struct *work)
485 struct iscsi_cls_session *session =
486 container_of(work, struct iscsi_cls_session, scan_work);
487 struct Scsi_Host *shost = iscsi_session_to_shost(session);
488 struct iscsi_cls_host *ihost = shost->shost_data;
489 struct iscsi_scan_data scan_data;
491 scan_data.channel = 0;
492 scan_data.id = SCAN_WILD_CARD;
493 scan_data.lun = SCAN_WILD_CARD;
495 iscsi_user_scan_session(&session->dev, &scan_data);
496 atomic_dec(&ihost->nr_scans);
499 static void session_recovery_timedout(struct work_struct *work)
501 struct iscsi_cls_session *session =
502 container_of(work, struct iscsi_cls_session,
506 iscsi_cls_session_printk(KERN_INFO, session,
507 "session recovery timed out after %d secs\n",
508 session->recovery_tmo);
510 spin_lock_irqsave(&session->lock, flags);
511 switch (session->state) {
512 case ISCSI_SESSION_FAILED:
513 session->state = ISCSI_SESSION_FREE;
515 case ISCSI_SESSION_LOGGED_IN:
516 case ISCSI_SESSION_FREE:
517 /* we raced with the unblock's flush */
518 spin_unlock_irqrestore(&session->lock, flags);
521 spin_unlock_irqrestore(&session->lock, flags);
523 if (session->transport->session_recovery_timedout)
524 session->transport->session_recovery_timedout(session);
526 scsi_target_unblock(&session->dev);
529 static void __iscsi_unblock_session(struct work_struct *work)
531 struct iscsi_cls_session *session =
532 container_of(work, struct iscsi_cls_session,
534 struct Scsi_Host *shost = iscsi_session_to_shost(session);
535 struct iscsi_cls_host *ihost = shost->shost_data;
539 * The recovery and unblock work get run from the same workqueue,
540 * so try to cancel it if it was going to run after this unblock.
542 cancel_delayed_work(&session->recovery_work);
543 spin_lock_irqsave(&session->lock, flags);
544 session->state = ISCSI_SESSION_LOGGED_IN;
545 spin_unlock_irqrestore(&session->lock, flags);
547 scsi_target_unblock(&session->dev);
549 * Only do kernel scanning if the driver is properly hooked into
550 * the async scanning code (drivers like iscsi_tcp do login and
551 * scanning from userspace).
553 if (shost->hostt->scan_finished) {
554 if (scsi_queue_work(shost, &session->scan_work))
555 atomic_inc(&ihost->nr_scans);
560 * iscsi_unblock_session - set a session as logged in and start IO.
561 * @session: iscsi session
563 * Mark a session as ready to accept IO.
565 void iscsi_unblock_session(struct iscsi_cls_session *session)
567 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
569 * make sure all the events have completed before tell the driver
572 flush_workqueue(iscsi_eh_timer_workq);
574 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
576 static void __iscsi_block_session(struct work_struct *work)
578 struct iscsi_cls_session *session =
579 container_of(work, struct iscsi_cls_session,
583 spin_lock_irqsave(&session->lock, flags);
584 session->state = ISCSI_SESSION_FAILED;
585 spin_unlock_irqrestore(&session->lock, flags);
586 scsi_target_block(&session->dev);
587 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
588 session->recovery_tmo * HZ);
591 void iscsi_block_session(struct iscsi_cls_session *session)
593 queue_work(iscsi_eh_timer_workq, &session->block_work);
595 EXPORT_SYMBOL_GPL(iscsi_block_session);
597 static void __iscsi_unbind_session(struct work_struct *work)
599 struct iscsi_cls_session *session =
600 container_of(work, struct iscsi_cls_session,
602 struct Scsi_Host *shost = iscsi_session_to_shost(session);
603 struct iscsi_cls_host *ihost = shost->shost_data;
606 /* Prevent new scans and make sure scanning is not in progress */
607 mutex_lock(&ihost->mutex);
608 spin_lock_irqsave(&session->lock, flags);
609 if (session->target_id == ISCSI_MAX_TARGET) {
610 spin_unlock_irqrestore(&session->lock, flags);
611 mutex_unlock(&ihost->mutex);
614 session->target_id = ISCSI_MAX_TARGET;
615 spin_unlock_irqrestore(&session->lock, flags);
616 mutex_unlock(&ihost->mutex);
618 scsi_remove_target(&session->dev);
619 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
622 struct iscsi_cls_session *
623 iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
626 struct iscsi_cls_session *session;
628 session = kzalloc(sizeof(*session) + dd_size,
633 session->transport = transport;
634 session->recovery_tmo = 120;
635 session->state = ISCSI_SESSION_FREE;
636 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
637 INIT_LIST_HEAD(&session->sess_list);
638 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
639 INIT_WORK(&session->block_work, __iscsi_block_session);
640 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
641 INIT_WORK(&session->scan_work, iscsi_scan_session);
642 spin_lock_init(&session->lock);
644 /* this is released in the dev's release function */
645 scsi_host_get(shost);
646 session->dev.parent = &shost->shost_gendev;
647 session->dev.release = iscsi_session_release;
648 device_initialize(&session->dev);
650 session->dd_data = &session[1];
653 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
655 static int iscsi_get_next_target_id(struct device *dev, void *data)
657 struct iscsi_cls_session *session;
661 if (!iscsi_is_session_dev(dev))
664 session = iscsi_dev_to_session(dev);
665 spin_lock_irqsave(&session->lock, flags);
666 if (*((unsigned int *) data) == session->target_id)
668 spin_unlock_irqrestore(&session->lock, flags);
672 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
674 struct Scsi_Host *shost = iscsi_session_to_shost(session);
675 struct iscsi_cls_host *ihost;
677 unsigned int id = target_id;
680 ihost = shost->shost_data;
681 session->sid = atomic_add_return(1, &iscsi_session_nr);
683 if (id == ISCSI_MAX_TARGET) {
684 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
685 err = device_for_each_child(&shost->shost_gendev, &id,
686 iscsi_get_next_target_id);
691 if (id == ISCSI_MAX_TARGET) {
692 iscsi_cls_session_printk(KERN_ERR, session,
693 "Too many iscsi targets. Max "
694 "number of targets is %d.\n",
695 ISCSI_MAX_TARGET - 1);
699 session->target_id = id;
701 dev_set_name(&session->dev, "session%u", session->sid);
702 err = device_add(&session->dev);
704 iscsi_cls_session_printk(KERN_ERR, session,
705 "could not register session's dev\n");
708 transport_register_device(&session->dev);
710 spin_lock_irqsave(&sesslock, flags);
711 list_add(&session->sess_list, &sesslist);
712 spin_unlock_irqrestore(&sesslock, flags);
714 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
718 scsi_host_put(shost);
721 EXPORT_SYMBOL_GPL(iscsi_add_session);
724 * iscsi_create_session - create iscsi class session
726 * @transport: iscsi transport
727 * @dd_size: private driver data size
728 * @target_id: which target
730 * This can be called from a LLD or iscsi_transport.
732 struct iscsi_cls_session *
733 iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
734 int dd_size, unsigned int target_id)
736 struct iscsi_cls_session *session;
738 session = iscsi_alloc_session(shost, transport, dd_size);
742 if (iscsi_add_session(session, target_id)) {
743 iscsi_free_session(session);
748 EXPORT_SYMBOL_GPL(iscsi_create_session);
750 static void iscsi_conn_release(struct device *dev)
752 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
753 struct device *parent = conn->dev.parent;
759 static int iscsi_is_conn_dev(const struct device *dev)
761 return dev->release == iscsi_conn_release;
764 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
766 if (!iscsi_is_conn_dev(dev))
768 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
771 void iscsi_remove_session(struct iscsi_cls_session *session)
773 struct Scsi_Host *shost = iscsi_session_to_shost(session);
777 spin_lock_irqsave(&sesslock, flags);
778 list_del(&session->sess_list);
779 spin_unlock_irqrestore(&sesslock, flags);
781 /* make sure there are no blocks/unblocks queued */
782 flush_workqueue(iscsi_eh_timer_workq);
783 /* make sure the timedout callout is not running */
784 if (!cancel_delayed_work(&session->recovery_work))
785 flush_workqueue(iscsi_eh_timer_workq);
787 * If we are blocked let commands flow again. The lld or iscsi
788 * layer should set up the queuecommand to fail commands.
789 * We assume that LLD will not be calling block/unblock while
790 * removing the session.
792 spin_lock_irqsave(&session->lock, flags);
793 session->state = ISCSI_SESSION_FREE;
794 spin_unlock_irqrestore(&session->lock, flags);
796 scsi_target_unblock(&session->dev);
797 /* flush running scans then delete devices */
798 scsi_flush_work(shost);
799 __iscsi_unbind_session(&session->unbind_work);
801 /* hw iscsi may not have removed all connections from session */
802 err = device_for_each_child(&session->dev, NULL,
803 iscsi_iter_destroy_conn_fn);
805 iscsi_cls_session_printk(KERN_ERR, session,
806 "Could not delete all connections "
807 "for session. Error %d.\n", err);
809 transport_unregister_device(&session->dev);
810 device_del(&session->dev);
812 EXPORT_SYMBOL_GPL(iscsi_remove_session);
814 void iscsi_free_session(struct iscsi_cls_session *session)
816 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
817 put_device(&session->dev);
819 EXPORT_SYMBOL_GPL(iscsi_free_session);
822 * iscsi_destroy_session - destroy iscsi session
823 * @session: iscsi_session
825 * Can be called by a LLD or iscsi_transport. There must not be
826 * any running connections.
828 int iscsi_destroy_session(struct iscsi_cls_session *session)
830 iscsi_remove_session(session);
831 iscsi_free_session(session);
834 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
837 * iscsi_create_conn - create iscsi class connection
838 * @session: iscsi cls session
839 * @dd_size: private driver data size
840 * @cid: connection id
842 * This can be called from a LLD or iscsi_transport. The connection
843 * is child of the session so cid must be unique for all connections
846 * Since we do not support MCS, cid will normally be zero. In some cases
847 * for software iscsi we could be trying to preallocate a connection struct
848 * in which case there could be two connection structs and cid would be
851 struct iscsi_cls_conn *
852 iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
854 struct iscsi_transport *transport = session->transport;
855 struct iscsi_cls_conn *conn;
859 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
863 conn->dd_data = &conn[1];
865 INIT_LIST_HEAD(&conn->conn_list);
866 conn->transport = transport;
869 /* this is released in the dev's release function */
870 if (!get_device(&session->dev))
873 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
874 conn->dev.parent = &session->dev;
875 conn->dev.release = iscsi_conn_release;
876 err = device_register(&conn->dev);
878 iscsi_cls_session_printk(KERN_ERR, session, "could not "
879 "register connection's dev\n");
880 goto release_parent_ref;
882 transport_register_device(&conn->dev);
884 spin_lock_irqsave(&connlock, flags);
885 list_add(&conn->conn_list, &connlist);
887 spin_unlock_irqrestore(&connlock, flags);
891 put_device(&session->dev);
897 EXPORT_SYMBOL_GPL(iscsi_create_conn);
900 * iscsi_destroy_conn - destroy iscsi class connection
901 * @conn: iscsi cls session
903 * This can be called from a LLD or iscsi_transport.
905 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
909 spin_lock_irqsave(&connlock, flags);
911 list_del(&conn->conn_list);
912 spin_unlock_irqrestore(&connlock, flags);
914 transport_unregister_device(&conn->dev);
915 device_unregister(&conn->dev);
918 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
921 * iscsi interface functions
923 static struct iscsi_internal *
924 iscsi_if_transport_lookup(struct iscsi_transport *tt)
926 struct iscsi_internal *priv;
929 spin_lock_irqsave(&iscsi_transport_lock, flags);
930 list_for_each_entry(priv, &iscsi_transports, list) {
931 if (tt == priv->iscsi_transport) {
932 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
936 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
941 iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
943 return netlink_broadcast(nls, skb, 0, 1, gfp);
947 iscsi_unicast_skb(struct sk_buff *skb, int pid)
951 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
953 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
960 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
961 char *data, uint32_t data_size)
963 struct nlmsghdr *nlh;
965 struct iscsi_uevent *ev;
967 struct iscsi_internal *priv;
968 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
971 priv = iscsi_if_transport_lookup(conn->transport);
975 skb = alloc_skb(len, GFP_ATOMIC);
977 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
978 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
979 "control PDU: OOM\n");
983 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
984 ev = NLMSG_DATA(nlh);
985 memset(ev, 0, sizeof(*ev));
986 ev->transport_handle = iscsi_handle(conn->transport);
987 ev->type = ISCSI_KEVENT_RECV_PDU;
988 ev->r.recv_req.cid = conn->cid;
989 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
990 pdu = (char*)ev + sizeof(*ev);
991 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
992 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
994 return iscsi_unicast_skb(skb, priv->daemon_pid);
996 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
998 void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
1000 struct nlmsghdr *nlh;
1001 struct sk_buff *skb;
1002 struct iscsi_uevent *ev;
1003 struct iscsi_internal *priv;
1004 int len = NLMSG_SPACE(sizeof(*ev));
1006 priv = iscsi_if_transport_lookup(conn->transport);
1010 skb = alloc_skb(len, GFP_ATOMIC);
1012 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1013 "conn error (%d)\n", error);
1017 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
1018 ev = NLMSG_DATA(nlh);
1019 ev->transport_handle = iscsi_handle(conn->transport);
1020 ev->type = ISCSI_KEVENT_CONN_ERROR;
1021 ev->r.connerror.error = error;
1022 ev->r.connerror.cid = conn->cid;
1023 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
1025 iscsi_broadcast_skb(skb, GFP_ATOMIC);
1027 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1030 EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
1033 iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
1034 void *payload, int size)
1036 struct sk_buff *skb;
1037 struct nlmsghdr *nlh;
1038 int len = NLMSG_SPACE(size);
1039 int flags = multi ? NLM_F_MULTI : 0;
1040 int t = done ? NLMSG_DONE : type;
1042 skb = alloc_skb(len, GFP_ATOMIC);
1044 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1048 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
1049 nlh->nlmsg_flags = flags;
1050 memcpy(NLMSG_DATA(nlh), payload, size);
1051 return iscsi_unicast_skb(skb, pid);
1055 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
1057 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1058 struct iscsi_stats *stats;
1059 struct sk_buff *skbstat;
1060 struct iscsi_cls_conn *conn;
1061 struct nlmsghdr *nlhstat;
1062 struct iscsi_uevent *evstat;
1063 struct iscsi_internal *priv;
1064 int len = NLMSG_SPACE(sizeof(*ev) +
1065 sizeof(struct iscsi_stats) +
1066 sizeof(struct iscsi_stats_custom) *
1067 ISCSI_STATS_CUSTOM_MAX);
1070 priv = iscsi_if_transport_lookup(transport);
1074 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
1081 skbstat = alloc_skb(len, GFP_ATOMIC);
1083 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1084 "deliver stats: OOM\n");
1088 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
1089 (len - sizeof(*nlhstat)), 0);
1090 evstat = NLMSG_DATA(nlhstat);
1091 memset(evstat, 0, sizeof(*evstat));
1092 evstat->transport_handle = iscsi_handle(conn->transport);
1093 evstat->type = nlh->nlmsg_type;
1094 evstat->u.get_stats.cid =
1095 ev->u.get_stats.cid;
1096 evstat->u.get_stats.sid =
1097 ev->u.get_stats.sid;
1098 stats = (struct iscsi_stats *)
1099 ((char*)evstat + sizeof(*evstat));
1100 memset(stats, 0, sizeof(*stats));
1102 transport->get_stats(conn, stats);
1103 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1104 sizeof(struct iscsi_stats) +
1105 sizeof(struct iscsi_stats_custom) *
1106 stats->custom_length);
1107 actual_size -= sizeof(*nlhstat);
1108 actual_size = NLMSG_LENGTH(actual_size);
1109 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
1110 nlhstat->nlmsg_len = actual_size;
1112 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
1113 } while (err < 0 && err != -ECONNREFUSED);
1119 * iscsi_session_event - send session destr. completion event
1120 * @session: iscsi class session
1121 * @event: type of event
1123 int iscsi_session_event(struct iscsi_cls_session *session,
1124 enum iscsi_uevent_e event)
1126 struct iscsi_internal *priv;
1127 struct Scsi_Host *shost;
1128 struct iscsi_uevent *ev;
1129 struct sk_buff *skb;
1130 struct nlmsghdr *nlh;
1131 int rc, len = NLMSG_SPACE(sizeof(*ev));
1133 priv = iscsi_if_transport_lookup(session->transport);
1136 shost = iscsi_session_to_shost(session);
1138 skb = alloc_skb(len, GFP_KERNEL);
1140 iscsi_cls_session_printk(KERN_ERR, session,
1141 "Cannot notify userspace of session "
1142 "event %u\n", event);
1146 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
1147 ev = NLMSG_DATA(nlh);
1148 ev->transport_handle = iscsi_handle(session->transport);
1152 case ISCSI_KEVENT_DESTROY_SESSION:
1153 ev->r.d_session.host_no = shost->host_no;
1154 ev->r.d_session.sid = session->sid;
1156 case ISCSI_KEVENT_CREATE_SESSION:
1157 ev->r.c_session_ret.host_no = shost->host_no;
1158 ev->r.c_session_ret.sid = session->sid;
1160 case ISCSI_KEVENT_UNBIND_SESSION:
1161 ev->r.unbind_session.host_no = shost->host_no;
1162 ev->r.unbind_session.sid = session->sid;
1165 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1172 * this will occur if the daemon is not up, so we just warn
1173 * the user and when the daemon is restarted it will handle it
1175 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
1177 iscsi_cls_session_printk(KERN_ERR, session,
1178 "Cannot notify userspace of session "
1179 "event %u. Check iscsi daemon\n",
1183 EXPORT_SYMBOL_GPL(iscsi_session_event);
1186 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1187 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
1188 uint16_t cmds_max, uint16_t queue_depth)
1190 struct iscsi_transport *transport = priv->iscsi_transport;
1191 struct iscsi_cls_session *session;
1192 struct Scsi_Host *shost;
1194 session = transport->create_session(ep, cmds_max, queue_depth,
1199 shost = iscsi_session_to_shost(session);
1200 ev->r.c_session_ret.host_no = shost->host_no;
1201 ev->r.c_session_ret.sid = session->sid;
1206 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1208 struct iscsi_cls_conn *conn;
1209 struct iscsi_cls_session *session;
1211 session = iscsi_session_lookup(ev->u.c_conn.sid);
1213 printk(KERN_ERR "iscsi: invalid session %d.\n",
1218 conn = transport->create_conn(session, ev->u.c_conn.cid);
1220 iscsi_cls_session_printk(KERN_ERR, session,
1221 "couldn't create a new connection.");
1225 ev->r.c_conn_ret.sid = session->sid;
1226 ev->r.c_conn_ret.cid = conn->cid;
1231 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1233 struct iscsi_cls_conn *conn;
1235 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
1239 if (transport->destroy_conn)
1240 transport->destroy_conn(conn);
1245 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1247 char *data = (char*)ev + sizeof(*ev);
1248 struct iscsi_cls_conn *conn;
1249 struct iscsi_cls_session *session;
1250 int err = 0, value = 0;
1252 session = iscsi_session_lookup(ev->u.set_param.sid);
1253 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1254 if (!conn || !session)
1257 switch (ev->u.set_param.param) {
1258 case ISCSI_PARAM_SESS_RECOVERY_TMO:
1259 sscanf(data, "%d", &value);
1261 session->recovery_tmo = value;
1264 err = transport->set_param(conn, ev->u.set_param.param,
1265 data, ev->u.set_param.len);
1271 static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1272 struct iscsi_uevent *ev, int msg_type)
1274 struct iscsi_endpoint *ep;
1275 struct sockaddr *dst_addr;
1276 struct Scsi_Host *shost = NULL;
1277 int non_blocking, err = 0;
1279 if (!transport->ep_connect)
1282 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1283 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1285 printk(KERN_ERR "ep connect failed. Could not find "
1287 ev->u.ep_connect_through_host.host_no);
1290 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1292 non_blocking = ev->u.ep_connect.non_blocking;
1294 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1295 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1301 ev->r.ep_connect_ret.handle = ep->id;
1304 scsi_host_put(shost);
1309 iscsi_if_transport_ep(struct iscsi_transport *transport,
1310 struct iscsi_uevent *ev, int msg_type)
1312 struct iscsi_endpoint *ep;
1316 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
1317 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1318 rc = iscsi_if_ep_connect(transport, ev, msg_type);
1320 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1321 if (!transport->ep_poll)
1324 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1328 ev->r.retcode = transport->ep_poll(ep,
1329 ev->u.ep_poll.timeout_ms);
1331 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1332 if (!transport->ep_disconnect)
1335 ep = iscsi_lookup_endpoint(ev->u.ep_disconnect.ep_handle);
1339 transport->ep_disconnect(ep);
1346 iscsi_tgt_dscvr(struct iscsi_transport *transport,
1347 struct iscsi_uevent *ev)
1349 struct Scsi_Host *shost;
1350 struct sockaddr *dst_addr;
1353 if (!transport->tgt_dscvr)
1356 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1358 printk(KERN_ERR "target discovery could not find host no %u\n",
1359 ev->u.tgt_dscvr.host_no);
1364 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1365 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1366 ev->u.tgt_dscvr.enable, dst_addr);
1367 scsi_host_put(shost);
1372 iscsi_set_host_param(struct iscsi_transport *transport,
1373 struct iscsi_uevent *ev)
1375 char *data = (char*)ev + sizeof(*ev);
1376 struct Scsi_Host *shost;
1379 if (!transport->set_host_param)
1382 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1384 printk(KERN_ERR "set_host_param could not find host no %u\n",
1385 ev->u.set_host_param.host_no);
1389 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1390 data, ev->u.set_host_param.len);
1391 scsi_host_put(shost);
1396 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1399 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1400 struct iscsi_transport *transport = NULL;
1401 struct iscsi_internal *priv;
1402 struct iscsi_cls_session *session;
1403 struct iscsi_cls_conn *conn;
1404 struct iscsi_endpoint *ep = NULL;
1406 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1409 transport = priv->iscsi_transport;
1411 if (!try_module_get(transport->owner))
1414 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1416 switch (nlh->nlmsg_type) {
1417 case ISCSI_UEVENT_CREATE_SESSION:
1418 err = iscsi_if_create_session(priv, ep, ev,
1419 ev->u.c_session.initial_cmdsn,
1420 ev->u.c_session.cmds_max,
1421 ev->u.c_session.queue_depth);
1423 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
1424 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
1430 err = iscsi_if_create_session(priv, ep, ev,
1431 ev->u.c_bound_session.initial_cmdsn,
1432 ev->u.c_bound_session.cmds_max,
1433 ev->u.c_bound_session.queue_depth);
1435 case ISCSI_UEVENT_DESTROY_SESSION:
1436 session = iscsi_session_lookup(ev->u.d_session.sid);
1438 transport->destroy_session(session);
1442 case ISCSI_UEVENT_UNBIND_SESSION:
1443 session = iscsi_session_lookup(ev->u.d_session.sid);
1445 scsi_queue_work(iscsi_session_to_shost(session),
1446 &session->unbind_work);
1450 case ISCSI_UEVENT_CREATE_CONN:
1451 err = iscsi_if_create_conn(transport, ev);
1453 case ISCSI_UEVENT_DESTROY_CONN:
1454 err = iscsi_if_destroy_conn(transport, ev);
1456 case ISCSI_UEVENT_BIND_CONN:
1457 session = iscsi_session_lookup(ev->u.b_conn.sid);
1458 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1460 if (session && conn)
1461 ev->r.retcode = transport->bind_conn(session, conn,
1462 ev->u.b_conn.transport_eph,
1463 ev->u.b_conn.is_leading);
1467 case ISCSI_UEVENT_SET_PARAM:
1468 err = iscsi_set_param(transport, ev);
1470 case ISCSI_UEVENT_START_CONN:
1471 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
1473 ev->r.retcode = transport->start_conn(conn);
1477 case ISCSI_UEVENT_STOP_CONN:
1478 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
1480 transport->stop_conn(conn, ev->u.stop_conn.flag);
1484 case ISCSI_UEVENT_SEND_PDU:
1485 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
1487 ev->r.retcode = transport->send_pdu(conn,
1488 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1489 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1490 ev->u.send_pdu.data_size);
1494 case ISCSI_UEVENT_GET_STATS:
1495 err = iscsi_if_get_stats(transport, nlh);
1497 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1498 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1499 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1500 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
1501 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1503 case ISCSI_UEVENT_TGT_DSCVR:
1504 err = iscsi_tgt_dscvr(transport, ev);
1506 case ISCSI_UEVENT_SET_HOST_PARAM:
1507 err = iscsi_set_host_param(transport, ev);
1514 module_put(transport->owner);
1519 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1520 * Malformed skbs with wrong lengths or invalid creds are not processed.
1523 iscsi_if_rx(struct sk_buff *skb)
1525 mutex_lock(&rx_queue_mutex);
1526 while (skb->len >= NLMSG_SPACE(0)) {
1529 struct nlmsghdr *nlh;
1530 struct iscsi_uevent *ev;
1532 nlh = nlmsg_hdr(skb);
1533 if (nlh->nlmsg_len < sizeof(*nlh) ||
1534 skb->len < nlh->nlmsg_len) {
1538 ev = NLMSG_DATA(nlh);
1539 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1540 if (rlen > skb->len)
1543 err = iscsi_if_recv_msg(skb, nlh);
1545 ev->type = ISCSI_KEVENT_IF_ERROR;
1550 * special case for GET_STATS:
1551 * on success - sending reply and stats from
1552 * inside of if_recv_msg(),
1553 * on error - fall through.
1555 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1557 err = iscsi_if_send_reply(
1558 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1559 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1560 } while (err < 0 && err != -ECONNREFUSED);
1561 skb_pull(skb, rlen);
1563 mutex_unlock(&rx_queue_mutex);
1566 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1567 struct device_attribute dev_attr_##_prefix##_##_name = \
1568 __ATTR(_name,_mode,_show,_store)
1571 * iSCSI connection attrs
1573 #define iscsi_conn_attr_show(param) \
1575 show_conn_param_##param(struct device *dev, \
1576 struct device_attribute *attr, char *buf) \
1578 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
1579 struct iscsi_transport *t = conn->transport; \
1580 return t->get_conn_param(conn, param, buf); \
1583 #define iscsi_conn_attr(field, param) \
1584 iscsi_conn_attr_show(param) \
1585 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
1588 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1589 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1590 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1591 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1592 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1593 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1594 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1595 iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1596 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1597 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1598 iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
1599 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1600 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
1603 * iSCSI session attrs
1605 #define iscsi_session_attr_show(param, perm) \
1607 show_session_param_##param(struct device *dev, \
1608 struct device_attribute *attr, char *buf) \
1610 struct iscsi_cls_session *session = \
1611 iscsi_dev_to_session(dev->parent); \
1612 struct iscsi_transport *t = session->transport; \
1614 if (perm && !capable(CAP_SYS_ADMIN)) \
1616 return t->get_session_param(session, param, buf); \
1619 #define iscsi_session_attr(field, param, perm) \
1620 iscsi_session_attr_show(param, perm) \
1621 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
1624 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1625 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1626 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1627 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1628 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1629 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1630 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1631 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1632 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1633 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1634 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1635 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1636 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1637 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
1638 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1639 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1640 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
1641 iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
1642 iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0)
1645 show_priv_session_state(struct device *dev, struct device_attribute *attr,
1648 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
1649 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1651 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1654 #define iscsi_priv_session_attr_show(field, format) \
1656 show_priv_session_##field(struct device *dev, \
1657 struct device_attribute *attr, char *buf) \
1659 struct iscsi_cls_session *session = \
1660 iscsi_dev_to_session(dev->parent); \
1661 return sprintf(buf, format"\n", session->field); \
1664 #define iscsi_priv_session_attr(field, format) \
1665 iscsi_priv_session_attr_show(field, format) \
1666 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1668 iscsi_priv_session_attr(recovery_tmo, "%d");
1673 #define iscsi_host_attr_show(param) \
1675 show_host_param_##param(struct device *dev, \
1676 struct device_attribute *attr, char *buf) \
1678 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1679 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1680 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1683 #define iscsi_host_attr(field, param) \
1684 iscsi_host_attr_show(param) \
1685 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1688 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
1689 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
1690 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
1691 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
1693 #define SETUP_PRIV_SESSION_RD_ATTR(field) \
1695 priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
1700 #define SETUP_SESSION_RD_ATTR(field, param_flag) \
1702 if (tt->param_mask & param_flag) { \
1703 priv->session_attrs[count] = &dev_attr_sess_##field; \
1708 #define SETUP_CONN_RD_ATTR(field, param_flag) \
1710 if (tt->param_mask & param_flag) { \
1711 priv->conn_attrs[count] = &dev_attr_conn_##field; \
1716 #define SETUP_HOST_RD_ATTR(field, param_flag) \
1718 if (tt->host_param_mask & param_flag) { \
1719 priv->host_attrs[count] = &dev_attr_host_##field; \
1724 static int iscsi_session_match(struct attribute_container *cont,
1727 struct iscsi_cls_session *session;
1728 struct Scsi_Host *shost;
1729 struct iscsi_internal *priv;
1731 if (!iscsi_is_session_dev(dev))
1734 session = iscsi_dev_to_session(dev);
1735 shost = iscsi_session_to_shost(session);
1736 if (!shost->transportt)
1739 priv = to_iscsi_internal(shost->transportt);
1740 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1743 return &priv->session_cont.ac == cont;
1746 static int iscsi_conn_match(struct attribute_container *cont,
1749 struct iscsi_cls_session *session;
1750 struct iscsi_cls_conn *conn;
1751 struct Scsi_Host *shost;
1752 struct iscsi_internal *priv;
1754 if (!iscsi_is_conn_dev(dev))
1757 conn = iscsi_dev_to_conn(dev);
1758 session = iscsi_dev_to_session(conn->dev.parent);
1759 shost = iscsi_session_to_shost(session);
1761 if (!shost->transportt)
1764 priv = to_iscsi_internal(shost->transportt);
1765 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1768 return &priv->conn_cont.ac == cont;
1771 static int iscsi_host_match(struct attribute_container *cont,
1774 struct Scsi_Host *shost;
1775 struct iscsi_internal *priv;
1777 if (!scsi_is_host_device(dev))
1780 shost = dev_to_shost(dev);
1781 if (!shost->transportt ||
1782 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1785 priv = to_iscsi_internal(shost->transportt);
1786 return &priv->t.host_attrs.ac == cont;
1789 struct scsi_transport_template *
1790 iscsi_register_transport(struct iscsi_transport *tt)
1792 struct iscsi_internal *priv;
1793 unsigned long flags;
1798 priv = iscsi_if_transport_lookup(tt);
1802 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1805 INIT_LIST_HEAD(&priv->list);
1806 priv->daemon_pid = -1;
1807 priv->iscsi_transport = tt;
1808 priv->t.user_scan = iscsi_user_scan;
1809 priv->t.create_work_queue = 1;
1811 priv->dev.class = &iscsi_transport_class;
1812 dev_set_name(&priv->dev, "%s", tt->name);
1813 err = device_register(&priv->dev);
1817 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
1819 goto unregister_dev;
1821 /* host parameters */
1822 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1823 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1824 priv->t.host_attrs.ac.match = iscsi_host_match;
1825 priv->t.host_size = sizeof(struct iscsi_cls_host);
1826 transport_container_register(&priv->t.host_attrs);
1828 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
1829 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
1830 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
1831 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
1832 BUG_ON(count > ISCSI_HOST_ATTRS);
1833 priv->host_attrs[count] = NULL;
1836 /* connection parameters */
1837 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1838 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1839 priv->conn_cont.ac.match = iscsi_conn_match;
1840 transport_container_register(&priv->conn_cont);
1842 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1843 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1844 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1845 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1846 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1847 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1848 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1849 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
1850 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
1851 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1852 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
1853 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1854 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
1856 BUG_ON(count > ISCSI_CONN_ATTRS);
1857 priv->conn_attrs[count] = NULL;
1860 /* session parameters */
1861 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1862 priv->session_cont.ac.class = &iscsi_session_class.class;
1863 priv->session_cont.ac.match = iscsi_session_match;
1864 transport_container_register(&priv->session_cont);
1866 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1867 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1868 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1869 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1870 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1871 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1872 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1873 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
1874 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1875 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
1876 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1877 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1878 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1879 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
1880 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
1881 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1882 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
1883 SETUP_SESSION_RD_ATTR(ifacename, ISCSI_IFACE_NAME);
1884 SETUP_SESSION_RD_ATTR(initiatorname, ISCSI_INITIATOR_NAME);
1885 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
1886 SETUP_PRIV_SESSION_RD_ATTR(state);
1888 BUG_ON(count > ISCSI_SESSION_ATTRS);
1889 priv->session_attrs[count] = NULL;
1891 spin_lock_irqsave(&iscsi_transport_lock, flags);
1892 list_add(&priv->list, &iscsi_transports);
1893 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1895 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1899 device_unregister(&priv->dev);
1905 EXPORT_SYMBOL_GPL(iscsi_register_transport);
1907 int iscsi_unregister_transport(struct iscsi_transport *tt)
1909 struct iscsi_internal *priv;
1910 unsigned long flags;
1914 mutex_lock(&rx_queue_mutex);
1916 priv = iscsi_if_transport_lookup(tt);
1919 spin_lock_irqsave(&iscsi_transport_lock, flags);
1920 list_del(&priv->list);
1921 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1923 transport_container_unregister(&priv->conn_cont);
1924 transport_container_unregister(&priv->session_cont);
1925 transport_container_unregister(&priv->t.host_attrs);
1927 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
1928 device_unregister(&priv->dev);
1929 mutex_unlock(&rx_queue_mutex);
1933 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1935 static __init int iscsi_transport_init(void)
1939 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
1940 ISCSI_TRANSPORT_VERSION);
1942 atomic_set(&iscsi_session_nr, 0);
1944 err = class_register(&iscsi_transport_class);
1948 err = class_register(&iscsi_endpoint_class);
1950 goto unregister_transport_class;
1952 err = transport_class_register(&iscsi_host_class);
1954 goto unregister_endpoint_class;
1956 err = transport_class_register(&iscsi_connection_class);
1958 goto unregister_host_class;
1960 err = transport_class_register(&iscsi_session_class);
1962 goto unregister_conn_class;
1964 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
1968 goto unregister_session_class;
1971 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1972 if (!iscsi_eh_timer_workq)
1978 netlink_kernel_release(nls);
1979 unregister_session_class:
1980 transport_class_unregister(&iscsi_session_class);
1981 unregister_conn_class:
1982 transport_class_unregister(&iscsi_connection_class);
1983 unregister_host_class:
1984 transport_class_unregister(&iscsi_host_class);
1985 unregister_endpoint_class:
1986 class_unregister(&iscsi_endpoint_class);
1987 unregister_transport_class:
1988 class_unregister(&iscsi_transport_class);
1992 static void __exit iscsi_transport_exit(void)
1994 destroy_workqueue(iscsi_eh_timer_workq);
1995 netlink_kernel_release(nls);
1996 transport_class_unregister(&iscsi_connection_class);
1997 transport_class_unregister(&iscsi_session_class);
1998 transport_class_unregister(&iscsi_host_class);
1999 class_unregister(&iscsi_endpoint_class);
2000 class_unregister(&iscsi_transport_class);
2003 module_init(iscsi_transport_init);
2004 module_exit(iscsi_transport_exit);
2006 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2007 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2008 "Alex Aizman <itn780@yahoo.com>");
2009 MODULE_DESCRIPTION("iSCSI Transport Interface");
2010 MODULE_LICENSE("GPL");
2011 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);