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 snprintf(ep->dev.bus_id, BUS_ID_SIZE, "ep-%llu",
191 (unsigned long long) id);
192 err = device_register(&ep->dev);
196 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
201 ep->dd_data = &ep[1];
205 device_unregister(&ep->dev);
212 EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
214 void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
216 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
217 device_unregister(&ep->dev);
219 EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
221 struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
223 struct iscsi_endpoint *ep;
226 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
231 ep = iscsi_dev_to_endpoint(dev);
233 * we can drop this now because the interface will prevent
234 * removals and lookups from racing.
239 EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
241 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
244 struct Scsi_Host *shost = dev_to_shost(dev);
245 struct iscsi_cls_host *ihost = shost->shost_data;
247 memset(ihost, 0, sizeof(*ihost));
248 atomic_set(&ihost->nr_scans, 0);
249 mutex_init(&ihost->mutex);
251 snprintf(ihost->scan_workq_name, sizeof(ihost->scan_workq_name),
252 "iscsi_scan_%d", shost->host_no);
253 ihost->scan_workq = create_singlethread_workqueue(
254 ihost->scan_workq_name);
255 if (!ihost->scan_workq)
260 static int iscsi_remove_host(struct transport_container *tc, struct device *dev,
263 struct Scsi_Host *shost = dev_to_shost(dev);
264 struct iscsi_cls_host *ihost = shost->shost_data;
266 destroy_workqueue(ihost->scan_workq);
270 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
276 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
282 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
288 static struct sock *nls;
289 static DEFINE_MUTEX(rx_queue_mutex);
291 static LIST_HEAD(sesslist);
292 static DEFINE_SPINLOCK(sesslock);
293 static LIST_HEAD(connlist);
294 static DEFINE_SPINLOCK(connlock);
296 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
298 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
303 * Returns the matching session to a given sid
305 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
308 struct iscsi_cls_session *sess;
310 spin_lock_irqsave(&sesslock, flags);
311 list_for_each_entry(sess, &sesslist, sess_list) {
312 if (sess->sid == sid) {
313 spin_unlock_irqrestore(&sesslock, flags);
317 spin_unlock_irqrestore(&sesslock, flags);
322 * Returns the matching connection to a given sid / cid tuple
324 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
327 struct iscsi_cls_conn *conn;
329 spin_lock_irqsave(&connlock, flags);
330 list_for_each_entry(conn, &connlist, conn_list) {
331 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
332 spin_unlock_irqrestore(&connlock, flags);
336 spin_unlock_irqrestore(&connlock, flags);
341 * The following functions can be used by LLDs that allocate
342 * their own scsi_hosts or by software iscsi LLDs
347 } iscsi_session_state_names[] = {
348 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
349 { ISCSI_SESSION_FAILED, "FAILED" },
350 { ISCSI_SESSION_FREE, "FREE" },
353 static const char *iscsi_session_state_name(int state)
358 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
359 if (iscsi_session_state_names[i].value == state) {
360 name = iscsi_session_state_names[i].name;
367 int iscsi_session_chkready(struct iscsi_cls_session *session)
372 spin_lock_irqsave(&session->lock, flags);
373 switch (session->state) {
374 case ISCSI_SESSION_LOGGED_IN:
377 case ISCSI_SESSION_FAILED:
378 err = DID_TRANSPORT_DISRUPTED << 16;
380 case ISCSI_SESSION_FREE:
381 err = DID_TRANSPORT_FAILFAST << 16;
384 err = DID_NO_CONNECT << 16;
387 spin_unlock_irqrestore(&session->lock, flags);
390 EXPORT_SYMBOL_GPL(iscsi_session_chkready);
392 static void iscsi_session_release(struct device *dev)
394 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
395 struct Scsi_Host *shost;
397 shost = iscsi_session_to_shost(session);
398 scsi_host_put(shost);
402 static int iscsi_is_session_dev(const struct device *dev)
404 return dev->release == iscsi_session_release;
407 static int iscsi_iter_session_fn(struct device *dev, void *data)
409 void (* fn) (struct iscsi_cls_session *) = data;
411 if (!iscsi_is_session_dev(dev))
413 fn(iscsi_dev_to_session(dev));
417 void iscsi_host_for_each_session(struct Scsi_Host *shost,
418 void (*fn)(struct iscsi_cls_session *))
420 device_for_each_child(&shost->shost_gendev, fn,
421 iscsi_iter_session_fn);
423 EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
426 * iscsi_scan_finished - helper to report when running scans are done
428 * @time: scan run time
430 * This function can be used by drives like qla4xxx to report to the scsi
431 * layer when the scans it kicked off at module load time are done.
433 int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
435 struct iscsi_cls_host *ihost = shost->shost_data;
437 * qla4xxx will have kicked off some session unblocks before calling
438 * scsi_scan_host, so just wait for them to complete.
440 return !atomic_read(&ihost->nr_scans);
442 EXPORT_SYMBOL_GPL(iscsi_scan_finished);
444 struct iscsi_scan_data {
445 unsigned int channel;
450 static int iscsi_user_scan_session(struct device *dev, void *data)
452 struct iscsi_scan_data *scan_data = data;
453 struct iscsi_cls_session *session;
454 struct Scsi_Host *shost;
455 struct iscsi_cls_host *ihost;
459 if (!iscsi_is_session_dev(dev))
462 session = iscsi_dev_to_session(dev);
463 shost = iscsi_session_to_shost(session);
464 ihost = shost->shost_data;
466 mutex_lock(&ihost->mutex);
467 spin_lock_irqsave(&session->lock, flags);
468 if (session->state != ISCSI_SESSION_LOGGED_IN) {
469 spin_unlock_irqrestore(&session->lock, flags);
470 mutex_unlock(&ihost->mutex);
473 id = session->target_id;
474 spin_unlock_irqrestore(&session->lock, flags);
476 if (id != ISCSI_MAX_TARGET) {
477 if ((scan_data->channel == SCAN_WILD_CARD ||
478 scan_data->channel == 0) &&
479 (scan_data->id == SCAN_WILD_CARD ||
480 scan_data->id == id))
481 scsi_scan_target(&session->dev, 0, id,
484 mutex_unlock(&ihost->mutex);
488 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
491 struct iscsi_scan_data scan_data;
493 scan_data.channel = channel;
497 return device_for_each_child(&shost->shost_gendev, &scan_data,
498 iscsi_user_scan_session);
501 static void iscsi_scan_session(struct work_struct *work)
503 struct iscsi_cls_session *session =
504 container_of(work, struct iscsi_cls_session, scan_work);
505 struct Scsi_Host *shost = iscsi_session_to_shost(session);
506 struct iscsi_cls_host *ihost = shost->shost_data;
507 struct iscsi_scan_data scan_data;
509 scan_data.channel = 0;
510 scan_data.id = SCAN_WILD_CARD;
511 scan_data.lun = SCAN_WILD_CARD;
513 iscsi_user_scan_session(&session->dev, &scan_data);
514 atomic_dec(&ihost->nr_scans);
517 static void session_recovery_timedout(struct work_struct *work)
519 struct iscsi_cls_session *session =
520 container_of(work, struct iscsi_cls_session,
524 iscsi_cls_session_printk(KERN_INFO, session,
525 "session recovery timed out after %d secs\n",
526 session->recovery_tmo);
528 spin_lock_irqsave(&session->lock, flags);
529 switch (session->state) {
530 case ISCSI_SESSION_FAILED:
531 session->state = ISCSI_SESSION_FREE;
533 case ISCSI_SESSION_LOGGED_IN:
534 case ISCSI_SESSION_FREE:
535 /* we raced with the unblock's flush */
536 spin_unlock_irqrestore(&session->lock, flags);
539 spin_unlock_irqrestore(&session->lock, flags);
541 if (session->transport->session_recovery_timedout)
542 session->transport->session_recovery_timedout(session);
544 scsi_target_unblock(&session->dev);
547 static void __iscsi_unblock_session(struct work_struct *work)
549 struct iscsi_cls_session *session =
550 container_of(work, struct iscsi_cls_session,
552 struct Scsi_Host *shost = iscsi_session_to_shost(session);
553 struct iscsi_cls_host *ihost = shost->shost_data;
557 * The recovery and unblock work get run from the same workqueue,
558 * so try to cancel it if it was going to run after this unblock.
560 cancel_delayed_work(&session->recovery_work);
561 spin_lock_irqsave(&session->lock, flags);
562 session->state = ISCSI_SESSION_LOGGED_IN;
563 spin_unlock_irqrestore(&session->lock, flags);
565 scsi_target_unblock(&session->dev);
567 * Only do kernel scanning if the driver is properly hooked into
568 * the async scanning code (drivers like iscsi_tcp do login and
569 * scanning from userspace).
571 if (shost->hostt->scan_finished) {
572 if (queue_work(ihost->scan_workq, &session->scan_work))
573 atomic_inc(&ihost->nr_scans);
578 * iscsi_unblock_session - set a session as logged in and start IO.
579 * @session: iscsi session
581 * Mark a session as ready to accept IO.
583 void iscsi_unblock_session(struct iscsi_cls_session *session)
585 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
587 * make sure all the events have completed before tell the driver
590 flush_workqueue(iscsi_eh_timer_workq);
592 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
594 static void __iscsi_block_session(struct work_struct *work)
596 struct iscsi_cls_session *session =
597 container_of(work, struct iscsi_cls_session,
601 spin_lock_irqsave(&session->lock, flags);
602 session->state = ISCSI_SESSION_FAILED;
603 spin_unlock_irqrestore(&session->lock, flags);
604 scsi_target_block(&session->dev);
605 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
606 session->recovery_tmo * HZ);
609 void iscsi_block_session(struct iscsi_cls_session *session)
611 queue_work(iscsi_eh_timer_workq, &session->block_work);
613 EXPORT_SYMBOL_GPL(iscsi_block_session);
615 static void __iscsi_unbind_session(struct work_struct *work)
617 struct iscsi_cls_session *session =
618 container_of(work, struct iscsi_cls_session,
620 struct Scsi_Host *shost = iscsi_session_to_shost(session);
621 struct iscsi_cls_host *ihost = shost->shost_data;
624 /* Prevent new scans and make sure scanning is not in progress */
625 mutex_lock(&ihost->mutex);
626 spin_lock_irqsave(&session->lock, flags);
627 if (session->target_id == ISCSI_MAX_TARGET) {
628 spin_unlock_irqrestore(&session->lock, flags);
629 mutex_unlock(&ihost->mutex);
632 session->target_id = ISCSI_MAX_TARGET;
633 spin_unlock_irqrestore(&session->lock, flags);
634 mutex_unlock(&ihost->mutex);
636 scsi_remove_target(&session->dev);
637 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
640 static int iscsi_unbind_session(struct iscsi_cls_session *session)
642 struct Scsi_Host *shost = iscsi_session_to_shost(session);
643 struct iscsi_cls_host *ihost = shost->shost_data;
645 return queue_work(ihost->scan_workq, &session->unbind_work);
648 struct iscsi_cls_session *
649 iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
652 struct iscsi_cls_session *session;
654 session = kzalloc(sizeof(*session) + dd_size,
659 session->transport = transport;
660 session->recovery_tmo = 120;
661 session->state = ISCSI_SESSION_FREE;
662 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
663 INIT_LIST_HEAD(&session->sess_list);
664 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
665 INIT_WORK(&session->block_work, __iscsi_block_session);
666 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
667 INIT_WORK(&session->scan_work, iscsi_scan_session);
668 spin_lock_init(&session->lock);
670 /* this is released in the dev's release function */
671 scsi_host_get(shost);
672 session->dev.parent = &shost->shost_gendev;
673 session->dev.release = iscsi_session_release;
674 device_initialize(&session->dev);
676 session->dd_data = &session[1];
679 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
681 static int iscsi_get_next_target_id(struct device *dev, void *data)
683 struct iscsi_cls_session *session;
687 if (!iscsi_is_session_dev(dev))
690 session = iscsi_dev_to_session(dev);
691 spin_lock_irqsave(&session->lock, flags);
692 if (*((unsigned int *) data) == session->target_id)
694 spin_unlock_irqrestore(&session->lock, flags);
698 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
700 struct Scsi_Host *shost = iscsi_session_to_shost(session);
701 struct iscsi_cls_host *ihost;
703 unsigned int id = target_id;
706 ihost = shost->shost_data;
707 session->sid = atomic_add_return(1, &iscsi_session_nr);
709 if (id == ISCSI_MAX_TARGET) {
710 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
711 err = device_for_each_child(&shost->shost_gendev, &id,
712 iscsi_get_next_target_id);
717 if (id == ISCSI_MAX_TARGET) {
718 iscsi_cls_session_printk(KERN_ERR, session,
719 "Too many iscsi targets. Max "
720 "number of targets is %d.\n",
721 ISCSI_MAX_TARGET - 1);
725 session->target_id = id;
727 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
729 err = device_add(&session->dev);
731 iscsi_cls_session_printk(KERN_ERR, session,
732 "could not register session's dev\n");
735 transport_register_device(&session->dev);
737 spin_lock_irqsave(&sesslock, flags);
738 list_add(&session->sess_list, &sesslist);
739 spin_unlock_irqrestore(&sesslock, flags);
741 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
745 scsi_host_put(shost);
748 EXPORT_SYMBOL_GPL(iscsi_add_session);
751 * iscsi_create_session - create iscsi class session
753 * @transport: iscsi transport
754 * @dd_size: private driver data size
755 * @target_id: which target
757 * This can be called from a LLD or iscsi_transport.
759 struct iscsi_cls_session *
760 iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
761 int dd_size, unsigned int target_id)
763 struct iscsi_cls_session *session;
765 session = iscsi_alloc_session(shost, transport, dd_size);
769 if (iscsi_add_session(session, target_id)) {
770 iscsi_free_session(session);
775 EXPORT_SYMBOL_GPL(iscsi_create_session);
777 static void iscsi_conn_release(struct device *dev)
779 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
780 struct device *parent = conn->dev.parent;
786 static int iscsi_is_conn_dev(const struct device *dev)
788 return dev->release == iscsi_conn_release;
791 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
793 if (!iscsi_is_conn_dev(dev))
795 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
798 void iscsi_remove_session(struct iscsi_cls_session *session)
800 struct Scsi_Host *shost = iscsi_session_to_shost(session);
801 struct iscsi_cls_host *ihost = shost->shost_data;
805 spin_lock_irqsave(&sesslock, flags);
806 list_del(&session->sess_list);
807 spin_unlock_irqrestore(&sesslock, flags);
809 /* make sure there are no blocks/unblocks queued */
810 flush_workqueue(iscsi_eh_timer_workq);
811 /* make sure the timedout callout is not running */
812 if (!cancel_delayed_work(&session->recovery_work))
813 flush_workqueue(iscsi_eh_timer_workq);
815 * If we are blocked let commands flow again. The lld or iscsi
816 * layer should set up the queuecommand to fail commands.
817 * We assume that LLD will not be calling block/unblock while
818 * removing the session.
820 spin_lock_irqsave(&session->lock, flags);
821 session->state = ISCSI_SESSION_FREE;
822 spin_unlock_irqrestore(&session->lock, flags);
824 scsi_target_unblock(&session->dev);
825 /* flush running scans then delete devices */
826 flush_workqueue(ihost->scan_workq);
827 __iscsi_unbind_session(&session->unbind_work);
829 /* hw iscsi may not have removed all connections from session */
830 err = device_for_each_child(&session->dev, NULL,
831 iscsi_iter_destroy_conn_fn);
833 iscsi_cls_session_printk(KERN_ERR, session,
834 "Could not delete all connections "
835 "for session. Error %d.\n", err);
837 transport_unregister_device(&session->dev);
838 device_del(&session->dev);
840 EXPORT_SYMBOL_GPL(iscsi_remove_session);
842 void iscsi_free_session(struct iscsi_cls_session *session)
844 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
845 put_device(&session->dev);
847 EXPORT_SYMBOL_GPL(iscsi_free_session);
850 * iscsi_destroy_session - destroy iscsi session
851 * @session: iscsi_session
853 * Can be called by a LLD or iscsi_transport. There must not be
854 * any running connections.
856 int iscsi_destroy_session(struct iscsi_cls_session *session)
858 iscsi_remove_session(session);
859 iscsi_free_session(session);
862 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
865 * iscsi_create_conn - create iscsi class connection
866 * @session: iscsi cls session
867 * @dd_size: private driver data size
868 * @cid: connection id
870 * This can be called from a LLD or iscsi_transport. The connection
871 * is child of the session so cid must be unique for all connections
874 * Since we do not support MCS, cid will normally be zero. In some cases
875 * for software iscsi we could be trying to preallocate a connection struct
876 * in which case there could be two connection structs and cid would be
879 struct iscsi_cls_conn *
880 iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
882 struct iscsi_transport *transport = session->transport;
883 struct iscsi_cls_conn *conn;
887 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
891 conn->dd_data = &conn[1];
893 INIT_LIST_HEAD(&conn->conn_list);
894 conn->transport = transport;
897 /* this is released in the dev's release function */
898 if (!get_device(&session->dev))
901 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
903 conn->dev.parent = &session->dev;
904 conn->dev.release = iscsi_conn_release;
905 err = device_register(&conn->dev);
907 iscsi_cls_session_printk(KERN_ERR, session, "could not "
908 "register connection's dev\n");
909 goto release_parent_ref;
911 transport_register_device(&conn->dev);
913 spin_lock_irqsave(&connlock, flags);
914 list_add(&conn->conn_list, &connlist);
916 spin_unlock_irqrestore(&connlock, flags);
920 put_device(&session->dev);
926 EXPORT_SYMBOL_GPL(iscsi_create_conn);
929 * iscsi_destroy_conn - destroy iscsi class connection
930 * @conn: iscsi cls session
932 * This can be called from a LLD or iscsi_transport.
934 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
938 spin_lock_irqsave(&connlock, flags);
940 list_del(&conn->conn_list);
941 spin_unlock_irqrestore(&connlock, flags);
943 transport_unregister_device(&conn->dev);
944 device_unregister(&conn->dev);
947 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
950 * iscsi interface functions
952 static struct iscsi_internal *
953 iscsi_if_transport_lookup(struct iscsi_transport *tt)
955 struct iscsi_internal *priv;
958 spin_lock_irqsave(&iscsi_transport_lock, flags);
959 list_for_each_entry(priv, &iscsi_transports, list) {
960 if (tt == priv->iscsi_transport) {
961 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
965 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
970 iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
974 rc = netlink_broadcast(nls, skb, 0, 1, gfp);
976 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
984 iscsi_unicast_skb(struct sk_buff *skb, int pid)
988 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
990 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
997 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
998 char *data, uint32_t data_size)
1000 struct nlmsghdr *nlh;
1001 struct sk_buff *skb;
1002 struct iscsi_uevent *ev;
1004 struct iscsi_internal *priv;
1005 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1008 priv = iscsi_if_transport_lookup(conn->transport);
1012 skb = alloc_skb(len, GFP_ATOMIC);
1014 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
1015 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1016 "control PDU: OOM\n");
1020 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
1021 ev = NLMSG_DATA(nlh);
1022 memset(ev, 0, sizeof(*ev));
1023 ev->transport_handle = iscsi_handle(conn->transport);
1024 ev->type = ISCSI_KEVENT_RECV_PDU;
1025 ev->r.recv_req.cid = conn->cid;
1026 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
1027 pdu = (char*)ev + sizeof(*ev);
1028 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1029 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1031 return iscsi_unicast_skb(skb, priv->daemon_pid);
1033 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1035 void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
1037 struct nlmsghdr *nlh;
1038 struct sk_buff *skb;
1039 struct iscsi_uevent *ev;
1040 struct iscsi_internal *priv;
1041 int len = NLMSG_SPACE(sizeof(*ev));
1043 priv = iscsi_if_transport_lookup(conn->transport);
1047 skb = alloc_skb(len, GFP_ATOMIC);
1049 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1050 "conn error (%d)\n", error);
1054 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
1055 ev = NLMSG_DATA(nlh);
1056 ev->transport_handle = iscsi_handle(conn->transport);
1057 ev->type = ISCSI_KEVENT_CONN_ERROR;
1058 ev->r.connerror.error = error;
1059 ev->r.connerror.cid = conn->cid;
1060 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
1062 iscsi_broadcast_skb(skb, GFP_ATOMIC);
1064 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1067 EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
1070 iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
1071 void *payload, int size)
1073 struct sk_buff *skb;
1074 struct nlmsghdr *nlh;
1075 int len = NLMSG_SPACE(size);
1076 int flags = multi ? NLM_F_MULTI : 0;
1077 int t = done ? NLMSG_DONE : type;
1079 skb = alloc_skb(len, GFP_ATOMIC);
1081 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1085 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
1086 nlh->nlmsg_flags = flags;
1087 memcpy(NLMSG_DATA(nlh), payload, size);
1088 return iscsi_unicast_skb(skb, pid);
1092 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
1094 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1095 struct iscsi_stats *stats;
1096 struct sk_buff *skbstat;
1097 struct iscsi_cls_conn *conn;
1098 struct nlmsghdr *nlhstat;
1099 struct iscsi_uevent *evstat;
1100 struct iscsi_internal *priv;
1101 int len = NLMSG_SPACE(sizeof(*ev) +
1102 sizeof(struct iscsi_stats) +
1103 sizeof(struct iscsi_stats_custom) *
1104 ISCSI_STATS_CUSTOM_MAX);
1107 priv = iscsi_if_transport_lookup(transport);
1111 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
1118 skbstat = alloc_skb(len, GFP_ATOMIC);
1120 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1121 "deliver stats: OOM\n");
1125 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
1126 (len - sizeof(*nlhstat)), 0);
1127 evstat = NLMSG_DATA(nlhstat);
1128 memset(evstat, 0, sizeof(*evstat));
1129 evstat->transport_handle = iscsi_handle(conn->transport);
1130 evstat->type = nlh->nlmsg_type;
1131 evstat->u.get_stats.cid =
1132 ev->u.get_stats.cid;
1133 evstat->u.get_stats.sid =
1134 ev->u.get_stats.sid;
1135 stats = (struct iscsi_stats *)
1136 ((char*)evstat + sizeof(*evstat));
1137 memset(stats, 0, sizeof(*stats));
1139 transport->get_stats(conn, stats);
1140 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1141 sizeof(struct iscsi_stats) +
1142 sizeof(struct iscsi_stats_custom) *
1143 stats->custom_length);
1144 actual_size -= sizeof(*nlhstat);
1145 actual_size = NLMSG_LENGTH(actual_size);
1146 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
1147 nlhstat->nlmsg_len = actual_size;
1149 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
1150 } while (err < 0 && err != -ECONNREFUSED);
1156 * iscsi_session_event - send session destr. completion event
1157 * @session: iscsi class session
1158 * @event: type of event
1160 int iscsi_session_event(struct iscsi_cls_session *session,
1161 enum iscsi_uevent_e event)
1163 struct iscsi_internal *priv;
1164 struct Scsi_Host *shost;
1165 struct iscsi_uevent *ev;
1166 struct sk_buff *skb;
1167 struct nlmsghdr *nlh;
1168 int rc, len = NLMSG_SPACE(sizeof(*ev));
1170 priv = iscsi_if_transport_lookup(session->transport);
1173 shost = iscsi_session_to_shost(session);
1175 skb = alloc_skb(len, GFP_KERNEL);
1177 iscsi_cls_session_printk(KERN_ERR, session,
1178 "Cannot notify userspace of session "
1179 "event %u\n", event);
1183 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
1184 ev = NLMSG_DATA(nlh);
1185 ev->transport_handle = iscsi_handle(session->transport);
1189 case ISCSI_KEVENT_DESTROY_SESSION:
1190 ev->r.d_session.host_no = shost->host_no;
1191 ev->r.d_session.sid = session->sid;
1193 case ISCSI_KEVENT_CREATE_SESSION:
1194 ev->r.c_session_ret.host_no = shost->host_no;
1195 ev->r.c_session_ret.sid = session->sid;
1197 case ISCSI_KEVENT_UNBIND_SESSION:
1198 ev->r.unbind_session.host_no = shost->host_no;
1199 ev->r.unbind_session.sid = session->sid;
1202 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1209 * this will occur if the daemon is not up, so we just warn
1210 * the user and when the daemon is restarted it will handle it
1212 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
1214 iscsi_cls_session_printk(KERN_ERR, session,
1215 "Cannot notify userspace of session "
1216 "event %u. Check iscsi daemon\n",
1220 EXPORT_SYMBOL_GPL(iscsi_session_event);
1223 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1224 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
1225 uint16_t cmds_max, uint16_t queue_depth)
1227 struct iscsi_transport *transport = priv->iscsi_transport;
1228 struct iscsi_cls_session *session;
1231 session = transport->create_session(ep, cmds_max, queue_depth,
1232 initial_cmdsn, &host_no);
1236 ev->r.c_session_ret.host_no = host_no;
1237 ev->r.c_session_ret.sid = session->sid;
1242 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1244 struct iscsi_cls_conn *conn;
1245 struct iscsi_cls_session *session;
1247 session = iscsi_session_lookup(ev->u.c_conn.sid);
1249 printk(KERN_ERR "iscsi: invalid session %d.\n",
1254 conn = transport->create_conn(session, ev->u.c_conn.cid);
1256 iscsi_cls_session_printk(KERN_ERR, session,
1257 "couldn't create a new connection.");
1261 ev->r.c_conn_ret.sid = session->sid;
1262 ev->r.c_conn_ret.cid = conn->cid;
1267 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1269 struct iscsi_cls_conn *conn;
1271 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
1275 if (transport->destroy_conn)
1276 transport->destroy_conn(conn);
1281 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1283 char *data = (char*)ev + sizeof(*ev);
1284 struct iscsi_cls_conn *conn;
1285 struct iscsi_cls_session *session;
1286 int err = 0, value = 0;
1288 session = iscsi_session_lookup(ev->u.set_param.sid);
1289 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1290 if (!conn || !session)
1293 switch (ev->u.set_param.param) {
1294 case ISCSI_PARAM_SESS_RECOVERY_TMO:
1295 sscanf(data, "%d", &value);
1297 session->recovery_tmo = value;
1300 err = transport->set_param(conn, ev->u.set_param.param,
1301 data, ev->u.set_param.len);
1308 iscsi_if_transport_ep(struct iscsi_transport *transport,
1309 struct iscsi_uevent *ev, int msg_type)
1311 struct iscsi_endpoint *ep;
1312 struct sockaddr *dst_addr;
1316 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1317 if (!transport->ep_connect)
1320 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1321 ep = transport->ep_connect(dst_addr,
1322 ev->u.ep_connect.non_blocking);
1326 ev->r.ep_connect_ret.handle = ep->id;
1328 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1329 if (!transport->ep_poll)
1332 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1336 ev->r.retcode = transport->ep_poll(ep,
1337 ev->u.ep_poll.timeout_ms);
1339 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1340 if (!transport->ep_disconnect)
1343 ep = iscsi_lookup_endpoint(ev->u.ep_disconnect.ep_handle);
1347 transport->ep_disconnect(ep);
1354 iscsi_tgt_dscvr(struct iscsi_transport *transport,
1355 struct iscsi_uevent *ev)
1357 struct Scsi_Host *shost;
1358 struct sockaddr *dst_addr;
1361 if (!transport->tgt_dscvr)
1364 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1366 printk(KERN_ERR "target discovery could not find host no %u\n",
1367 ev->u.tgt_dscvr.host_no);
1372 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1373 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1374 ev->u.tgt_dscvr.enable, dst_addr);
1375 scsi_host_put(shost);
1380 iscsi_set_host_param(struct iscsi_transport *transport,
1381 struct iscsi_uevent *ev)
1383 char *data = (char*)ev + sizeof(*ev);
1384 struct Scsi_Host *shost;
1387 if (!transport->set_host_param)
1390 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1392 printk(KERN_ERR "set_host_param could not find host no %u\n",
1393 ev->u.set_host_param.host_no);
1397 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1398 data, ev->u.set_host_param.len);
1399 scsi_host_put(shost);
1404 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1407 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1408 struct iscsi_transport *transport = NULL;
1409 struct iscsi_internal *priv;
1410 struct iscsi_cls_session *session;
1411 struct iscsi_cls_conn *conn;
1412 struct iscsi_endpoint *ep = NULL;
1414 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1417 transport = priv->iscsi_transport;
1419 if (!try_module_get(transport->owner))
1422 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1424 switch (nlh->nlmsg_type) {
1425 case ISCSI_UEVENT_CREATE_SESSION:
1426 err = iscsi_if_create_session(priv, ep, ev,
1427 ev->u.c_session.initial_cmdsn,
1428 ev->u.c_session.cmds_max,
1429 ev->u.c_session.queue_depth);
1431 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
1432 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
1438 err = iscsi_if_create_session(priv, ep, ev,
1439 ev->u.c_bound_session.initial_cmdsn,
1440 ev->u.c_bound_session.cmds_max,
1441 ev->u.c_bound_session.queue_depth);
1443 case ISCSI_UEVENT_DESTROY_SESSION:
1444 session = iscsi_session_lookup(ev->u.d_session.sid);
1446 transport->destroy_session(session);
1450 case ISCSI_UEVENT_UNBIND_SESSION:
1451 session = iscsi_session_lookup(ev->u.d_session.sid);
1453 iscsi_unbind_session(session);
1457 case ISCSI_UEVENT_CREATE_CONN:
1458 err = iscsi_if_create_conn(transport, ev);
1460 case ISCSI_UEVENT_DESTROY_CONN:
1461 err = iscsi_if_destroy_conn(transport, ev);
1463 case ISCSI_UEVENT_BIND_CONN:
1464 session = iscsi_session_lookup(ev->u.b_conn.sid);
1465 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1467 if (session && conn)
1468 ev->r.retcode = transport->bind_conn(session, conn,
1469 ev->u.b_conn.transport_eph,
1470 ev->u.b_conn.is_leading);
1474 case ISCSI_UEVENT_SET_PARAM:
1475 err = iscsi_set_param(transport, ev);
1477 case ISCSI_UEVENT_START_CONN:
1478 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
1480 ev->r.retcode = transport->start_conn(conn);
1484 case ISCSI_UEVENT_STOP_CONN:
1485 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
1487 transport->stop_conn(conn, ev->u.stop_conn.flag);
1491 case ISCSI_UEVENT_SEND_PDU:
1492 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
1494 ev->r.retcode = transport->send_pdu(conn,
1495 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1496 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1497 ev->u.send_pdu.data_size);
1501 case ISCSI_UEVENT_GET_STATS:
1502 err = iscsi_if_get_stats(transport, nlh);
1504 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1505 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1506 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1507 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1509 case ISCSI_UEVENT_TGT_DSCVR:
1510 err = iscsi_tgt_dscvr(transport, ev);
1512 case ISCSI_UEVENT_SET_HOST_PARAM:
1513 err = iscsi_set_host_param(transport, ev);
1520 module_put(transport->owner);
1525 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1526 * Malformed skbs with wrong lengths or invalid creds are not processed.
1529 iscsi_if_rx(struct sk_buff *skb)
1531 mutex_lock(&rx_queue_mutex);
1532 while (skb->len >= NLMSG_SPACE(0)) {
1535 struct nlmsghdr *nlh;
1536 struct iscsi_uevent *ev;
1538 nlh = nlmsg_hdr(skb);
1539 if (nlh->nlmsg_len < sizeof(*nlh) ||
1540 skb->len < nlh->nlmsg_len) {
1544 ev = NLMSG_DATA(nlh);
1545 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1546 if (rlen > skb->len)
1549 err = iscsi_if_recv_msg(skb, nlh);
1551 ev->type = ISCSI_KEVENT_IF_ERROR;
1556 * special case for GET_STATS:
1557 * on success - sending reply and stats from
1558 * inside of if_recv_msg(),
1559 * on error - fall through.
1561 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1563 err = iscsi_if_send_reply(
1564 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1565 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1566 } while (err < 0 && err != -ECONNREFUSED);
1567 skb_pull(skb, rlen);
1569 mutex_unlock(&rx_queue_mutex);
1572 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1573 struct device_attribute dev_attr_##_prefix##_##_name = \
1574 __ATTR(_name,_mode,_show,_store)
1577 * iSCSI connection attrs
1579 #define iscsi_conn_attr_show(param) \
1581 show_conn_param_##param(struct device *dev, \
1582 struct device_attribute *attr, char *buf) \
1584 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
1585 struct iscsi_transport *t = conn->transport; \
1586 return t->get_conn_param(conn, param, buf); \
1589 #define iscsi_conn_attr(field, param) \
1590 iscsi_conn_attr_show(param) \
1591 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
1594 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1595 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1596 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1597 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1598 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1599 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1600 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1601 iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1602 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1603 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1604 iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
1605 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1606 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
1609 * iSCSI session attrs
1611 #define iscsi_session_attr_show(param, perm) \
1613 show_session_param_##param(struct device *dev, \
1614 struct device_attribute *attr, char *buf) \
1616 struct iscsi_cls_session *session = \
1617 iscsi_dev_to_session(dev->parent); \
1618 struct iscsi_transport *t = session->transport; \
1620 if (perm && !capable(CAP_SYS_ADMIN)) \
1622 return t->get_session_param(session, param, buf); \
1625 #define iscsi_session_attr(field, param, perm) \
1626 iscsi_session_attr_show(param, perm) \
1627 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
1630 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1631 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1632 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1633 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1634 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1635 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1636 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1637 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1638 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1639 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1640 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1641 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1642 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1643 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
1644 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1645 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1646 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
1647 iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
1648 iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0)
1651 show_priv_session_state(struct device *dev, struct device_attribute *attr,
1654 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
1655 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1657 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1660 #define iscsi_priv_session_attr_show(field, format) \
1662 show_priv_session_##field(struct device *dev, \
1663 struct device_attribute *attr, char *buf) \
1665 struct iscsi_cls_session *session = \
1666 iscsi_dev_to_session(dev->parent); \
1667 return sprintf(buf, format"\n", session->field); \
1670 #define iscsi_priv_session_attr(field, format) \
1671 iscsi_priv_session_attr_show(field, format) \
1672 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1674 iscsi_priv_session_attr(recovery_tmo, "%d");
1679 #define iscsi_host_attr_show(param) \
1681 show_host_param_##param(struct device *dev, \
1682 struct device_attribute *attr, char *buf) \
1684 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1685 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1686 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1689 #define iscsi_host_attr(field, param) \
1690 iscsi_host_attr_show(param) \
1691 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1694 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
1695 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
1696 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
1697 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
1699 #define SETUP_PRIV_SESSION_RD_ATTR(field) \
1701 priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
1706 #define SETUP_SESSION_RD_ATTR(field, param_flag) \
1708 if (tt->param_mask & param_flag) { \
1709 priv->session_attrs[count] = &dev_attr_sess_##field; \
1714 #define SETUP_CONN_RD_ATTR(field, param_flag) \
1716 if (tt->param_mask & param_flag) { \
1717 priv->conn_attrs[count] = &dev_attr_conn_##field; \
1722 #define SETUP_HOST_RD_ATTR(field, param_flag) \
1724 if (tt->host_param_mask & param_flag) { \
1725 priv->host_attrs[count] = &dev_attr_host_##field; \
1730 static int iscsi_session_match(struct attribute_container *cont,
1733 struct iscsi_cls_session *session;
1734 struct Scsi_Host *shost;
1735 struct iscsi_internal *priv;
1737 if (!iscsi_is_session_dev(dev))
1740 session = iscsi_dev_to_session(dev);
1741 shost = iscsi_session_to_shost(session);
1742 if (!shost->transportt)
1745 priv = to_iscsi_internal(shost->transportt);
1746 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1749 return &priv->session_cont.ac == cont;
1752 static int iscsi_conn_match(struct attribute_container *cont,
1755 struct iscsi_cls_session *session;
1756 struct iscsi_cls_conn *conn;
1757 struct Scsi_Host *shost;
1758 struct iscsi_internal *priv;
1760 if (!iscsi_is_conn_dev(dev))
1763 conn = iscsi_dev_to_conn(dev);
1764 session = iscsi_dev_to_session(conn->dev.parent);
1765 shost = iscsi_session_to_shost(session);
1767 if (!shost->transportt)
1770 priv = to_iscsi_internal(shost->transportt);
1771 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1774 return &priv->conn_cont.ac == cont;
1777 static int iscsi_host_match(struct attribute_container *cont,
1780 struct Scsi_Host *shost;
1781 struct iscsi_internal *priv;
1783 if (!scsi_is_host_device(dev))
1786 shost = dev_to_shost(dev);
1787 if (!shost->transportt ||
1788 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1791 priv = to_iscsi_internal(shost->transportt);
1792 return &priv->t.host_attrs.ac == cont;
1795 struct scsi_transport_template *
1796 iscsi_register_transport(struct iscsi_transport *tt)
1798 struct iscsi_internal *priv;
1799 unsigned long flags;
1804 priv = iscsi_if_transport_lookup(tt);
1808 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1811 INIT_LIST_HEAD(&priv->list);
1812 priv->daemon_pid = -1;
1813 priv->iscsi_transport = tt;
1814 priv->t.user_scan = iscsi_user_scan;
1815 if (!(tt->caps & CAP_DATA_PATH_OFFLOAD))
1816 priv->t.create_work_queue = 1;
1818 priv->dev.class = &iscsi_transport_class;
1819 snprintf(priv->dev.bus_id, BUS_ID_SIZE, "%s", tt->name);
1820 err = device_register(&priv->dev);
1824 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
1826 goto unregister_dev;
1828 /* host parameters */
1829 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1830 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1831 priv->t.host_attrs.ac.match = iscsi_host_match;
1832 priv->t.host_size = sizeof(struct iscsi_cls_host);
1833 transport_container_register(&priv->t.host_attrs);
1835 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
1836 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
1837 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
1838 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
1839 BUG_ON(count > ISCSI_HOST_ATTRS);
1840 priv->host_attrs[count] = NULL;
1843 /* connection parameters */
1844 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1845 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1846 priv->conn_cont.ac.match = iscsi_conn_match;
1847 transport_container_register(&priv->conn_cont);
1849 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1850 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1851 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1852 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1853 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1854 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1855 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1856 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
1857 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
1858 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1859 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
1860 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1861 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
1863 BUG_ON(count > ISCSI_CONN_ATTRS);
1864 priv->conn_attrs[count] = NULL;
1867 /* session parameters */
1868 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1869 priv->session_cont.ac.class = &iscsi_session_class.class;
1870 priv->session_cont.ac.match = iscsi_session_match;
1871 transport_container_register(&priv->session_cont);
1873 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1874 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1875 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1876 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1877 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1878 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1879 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1880 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
1881 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1882 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
1883 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1884 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1885 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1886 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
1887 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
1888 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1889 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
1890 SETUP_SESSION_RD_ATTR(ifacename, ISCSI_IFACE_NAME);
1891 SETUP_SESSION_RD_ATTR(initiatorname, ISCSI_INITIATOR_NAME);
1892 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
1893 SETUP_PRIV_SESSION_RD_ATTR(state);
1895 BUG_ON(count > ISCSI_SESSION_ATTRS);
1896 priv->session_attrs[count] = NULL;
1898 spin_lock_irqsave(&iscsi_transport_lock, flags);
1899 list_add(&priv->list, &iscsi_transports);
1900 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1902 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1906 device_unregister(&priv->dev);
1912 EXPORT_SYMBOL_GPL(iscsi_register_transport);
1914 int iscsi_unregister_transport(struct iscsi_transport *tt)
1916 struct iscsi_internal *priv;
1917 unsigned long flags;
1921 mutex_lock(&rx_queue_mutex);
1923 priv = iscsi_if_transport_lookup(tt);
1926 spin_lock_irqsave(&iscsi_transport_lock, flags);
1927 list_del(&priv->list);
1928 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1930 transport_container_unregister(&priv->conn_cont);
1931 transport_container_unregister(&priv->session_cont);
1932 transport_container_unregister(&priv->t.host_attrs);
1934 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
1935 device_unregister(&priv->dev);
1936 mutex_unlock(&rx_queue_mutex);
1940 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1942 static __init int iscsi_transport_init(void)
1946 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
1947 ISCSI_TRANSPORT_VERSION);
1949 atomic_set(&iscsi_session_nr, 0);
1951 err = class_register(&iscsi_transport_class);
1955 err = class_register(&iscsi_endpoint_class);
1957 goto unregister_transport_class;
1959 err = transport_class_register(&iscsi_host_class);
1961 goto unregister_endpoint_class;
1963 err = transport_class_register(&iscsi_connection_class);
1965 goto unregister_host_class;
1967 err = transport_class_register(&iscsi_session_class);
1969 goto unregister_conn_class;
1971 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
1975 goto unregister_session_class;
1978 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1979 if (!iscsi_eh_timer_workq)
1985 netlink_kernel_release(nls);
1986 unregister_session_class:
1987 transport_class_unregister(&iscsi_session_class);
1988 unregister_conn_class:
1989 transport_class_unregister(&iscsi_connection_class);
1990 unregister_host_class:
1991 transport_class_unregister(&iscsi_host_class);
1992 unregister_endpoint_class:
1993 class_unregister(&iscsi_endpoint_class);
1994 unregister_transport_class:
1995 class_unregister(&iscsi_transport_class);
1999 static void __exit iscsi_transport_exit(void)
2001 destroy_workqueue(iscsi_eh_timer_workq);
2002 netlink_kernel_release(nls);
2003 transport_class_unregister(&iscsi_connection_class);
2004 transport_class_unregister(&iscsi_session_class);
2005 transport_class_unregister(&iscsi_host_class);
2006 class_unregister(&iscsi_endpoint_class);
2007 class_unregister(&iscsi_transport_class);
2010 module_init(iscsi_transport_init);
2011 module_exit(iscsi_transport_exit);
2013 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2014 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2015 "Alex Aizman <itn780@yahoo.com>");
2016 MODULE_DESCRIPTION("iSCSI Transport Interface");
2017 MODULE_LICENSE("GPL");
2018 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);