2 * iSCSI transport class definitions
4 * Copyright (C) IBM Corporation, 2004
5 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <linux/module.h>
24 #include <linux/mutex.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_transport.h>
30 #include <scsi/scsi_transport_iscsi.h>
31 #include <scsi/iscsi_if.h>
33 #define ISCSI_SESSION_ATTRS 19
34 #define ISCSI_CONN_ATTRS 13
35 #define ISCSI_HOST_ATTRS 4
36 #define ISCSI_TRANSPORT_VERSION "2.0-869"
38 struct iscsi_internal {
40 struct scsi_transport_template t;
41 struct iscsi_transport *iscsi_transport;
42 struct list_head list;
45 struct device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
46 struct transport_container conn_cont;
47 struct device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
48 struct transport_container session_cont;
49 struct device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
52 static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
53 static struct workqueue_struct *iscsi_eh_timer_workq;
56 * list of registered transports and lock that must
57 * be held while accessing list. The iscsi_transport_lock must
58 * be acquired after the rx_queue_mutex.
60 static LIST_HEAD(iscsi_transports);
61 static DEFINE_SPINLOCK(iscsi_transport_lock);
63 #define to_iscsi_internal(tmpl) \
64 container_of(tmpl, struct iscsi_internal, t)
66 #define dev_to_iscsi_internal(_dev) \
67 container_of(_dev, struct iscsi_internal, dev)
69 static void iscsi_transport_release(struct device *dev)
71 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
76 * iscsi_transport_class represents the iscsi_transports that are
79 static struct class iscsi_transport_class = {
80 .name = "iscsi_transport",
81 .dev_release = iscsi_transport_release,
85 show_transport_handle(struct device *dev, struct device_attribute *attr,
88 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
89 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
91 static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
93 #define show_transport_attr(name, format) \
95 show_transport_##name(struct device *dev, \
96 struct device_attribute *attr,char *buf) \
98 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
99 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
101 static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
103 show_transport_attr(caps, "0x%x");
104 show_transport_attr(max_lun, "%d");
105 show_transport_attr(max_conn, "%d");
106 show_transport_attr(max_cmd_len, "%d");
108 static struct attribute *iscsi_transport_attrs[] = {
109 &dev_attr_handle.attr,
111 &dev_attr_max_lun.attr,
112 &dev_attr_max_conn.attr,
113 &dev_attr_max_cmd_len.attr,
117 static struct attribute_group iscsi_transport_group = {
118 .attrs = iscsi_transport_attrs,
123 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
126 struct Scsi_Host *shost = dev_to_shost(dev);
127 struct iscsi_host *ihost = shost->shost_data;
129 memset(ihost, 0, sizeof(*ihost));
130 INIT_LIST_HEAD(&ihost->sessions);
131 mutex_init(&ihost->mutex);
132 atomic_set(&ihost->nr_scans, 0);
134 snprintf(ihost->scan_workq_name, KOBJ_NAME_LEN, "iscsi_scan_%d",
136 ihost->scan_workq = create_singlethread_workqueue(
137 ihost->scan_workq_name);
138 if (!ihost->scan_workq)
143 static int iscsi_remove_host(struct transport_container *tc, struct device *dev,
146 struct Scsi_Host *shost = dev_to_shost(dev);
147 struct iscsi_host *ihost = shost->shost_data;
149 destroy_workqueue(ihost->scan_workq);
153 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
159 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
165 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
171 static struct sock *nls;
172 static DEFINE_MUTEX(rx_queue_mutex);
174 static LIST_HEAD(sesslist);
175 static DEFINE_SPINLOCK(sesslock);
176 static LIST_HEAD(connlist);
177 static DEFINE_SPINLOCK(connlock);
179 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
181 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
186 * Returns the matching session to a given sid
188 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
191 struct iscsi_cls_session *sess;
193 spin_lock_irqsave(&sesslock, flags);
194 list_for_each_entry(sess, &sesslist, sess_list) {
195 if (sess->sid == sid) {
196 spin_unlock_irqrestore(&sesslock, flags);
200 spin_unlock_irqrestore(&sesslock, flags);
205 * Returns the matching connection to a given sid / cid tuple
207 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
210 struct iscsi_cls_conn *conn;
212 spin_lock_irqsave(&connlock, flags);
213 list_for_each_entry(conn, &connlist, conn_list) {
214 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
215 spin_unlock_irqrestore(&connlock, flags);
219 spin_unlock_irqrestore(&connlock, flags);
224 * The following functions can be used by LLDs that allocate
225 * their own scsi_hosts or by software iscsi LLDs
230 } iscsi_session_state_names[] = {
231 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
232 { ISCSI_SESSION_FAILED, "FAILED" },
233 { ISCSI_SESSION_FREE, "FREE" },
236 static const char *iscsi_session_state_name(int state)
241 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
242 if (iscsi_session_state_names[i].value == state) {
243 name = iscsi_session_state_names[i].name;
250 int iscsi_session_chkready(struct iscsi_cls_session *session)
255 spin_lock_irqsave(&session->lock, flags);
256 switch (session->state) {
257 case ISCSI_SESSION_LOGGED_IN:
260 case ISCSI_SESSION_FAILED:
261 err = DID_IMM_RETRY << 16;
263 case ISCSI_SESSION_FREE:
264 err = DID_NO_CONNECT << 16;
267 err = DID_NO_CONNECT << 16;
270 spin_unlock_irqrestore(&session->lock, flags);
273 EXPORT_SYMBOL_GPL(iscsi_session_chkready);
275 static void iscsi_session_release(struct device *dev)
277 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
278 struct Scsi_Host *shost;
280 shost = iscsi_session_to_shost(session);
281 scsi_host_put(shost);
285 static int iscsi_is_session_dev(const struct device *dev)
287 return dev->release == iscsi_session_release;
291 * iscsi_scan_finished - helper to report when running scans are done
293 * @time: scan run time
295 * This function can be used by drives like qla4xxx to report to the scsi
296 * layer when the scans it kicked off at module load time are done.
298 int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
300 struct iscsi_host *ihost = shost->shost_data;
302 * qla4xxx will have kicked off some session unblocks before calling
303 * scsi_scan_host, so just wait for them to complete.
305 return !atomic_read(&ihost->nr_scans);
307 EXPORT_SYMBOL_GPL(iscsi_scan_finished);
309 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
312 struct iscsi_host *ihost = shost->shost_data;
313 struct iscsi_cls_session *session;
315 mutex_lock(&ihost->mutex);
316 list_for_each_entry(session, &ihost->sessions, host_list) {
317 if ((channel == SCAN_WILD_CARD || channel == 0) &&
318 (id == SCAN_WILD_CARD || id == session->target_id))
319 scsi_scan_target(&session->dev, 0,
320 session->target_id, lun, 1);
322 mutex_unlock(&ihost->mutex);
327 static void iscsi_scan_session(struct work_struct *work)
329 struct iscsi_cls_session *session =
330 container_of(work, struct iscsi_cls_session, scan_work);
331 struct Scsi_Host *shost = iscsi_session_to_shost(session);
332 struct iscsi_host *ihost = shost->shost_data;
335 spin_lock_irqsave(&session->lock, flags);
336 if (session->state != ISCSI_SESSION_LOGGED_IN) {
337 spin_unlock_irqrestore(&session->lock, flags);
340 spin_unlock_irqrestore(&session->lock, flags);
342 scsi_scan_target(&session->dev, 0, session->target_id,
345 atomic_dec(&ihost->nr_scans);
348 static void session_recovery_timedout(struct work_struct *work)
350 struct iscsi_cls_session *session =
351 container_of(work, struct iscsi_cls_session,
355 iscsi_cls_session_printk(KERN_INFO, session,
356 "session recovery timed out after %d secs\n",
357 session->recovery_tmo);
359 spin_lock_irqsave(&session->lock, flags);
360 switch (session->state) {
361 case ISCSI_SESSION_FAILED:
362 session->state = ISCSI_SESSION_FREE;
364 case ISCSI_SESSION_LOGGED_IN:
365 case ISCSI_SESSION_FREE:
366 /* we raced with the unblock's flush */
367 spin_unlock_irqrestore(&session->lock, flags);
370 spin_unlock_irqrestore(&session->lock, flags);
372 if (session->transport->session_recovery_timedout)
373 session->transport->session_recovery_timedout(session);
375 scsi_target_unblock(&session->dev);
378 static void __iscsi_unblock_session(struct work_struct *work)
380 struct iscsi_cls_session *session =
381 container_of(work, struct iscsi_cls_session,
383 struct Scsi_Host *shost = iscsi_session_to_shost(session);
384 struct iscsi_host *ihost = shost->shost_data;
388 * The recovery and unblock work get run from the same workqueue,
389 * so try to cancel it if it was going to run after this unblock.
391 cancel_delayed_work(&session->recovery_work);
392 spin_lock_irqsave(&session->lock, flags);
393 session->state = ISCSI_SESSION_LOGGED_IN;
394 spin_unlock_irqrestore(&session->lock, flags);
396 scsi_target_unblock(&session->dev);
398 * Only do kernel scanning if the driver is properly hooked into
399 * the async scanning code (drivers like iscsi_tcp do login and
400 * scanning from userspace).
402 if (shost->hostt->scan_finished) {
403 if (queue_work(ihost->scan_workq, &session->scan_work))
404 atomic_inc(&ihost->nr_scans);
409 * iscsi_unblock_session - set a session as logged in and start IO.
410 * @session: iscsi session
412 * Mark a session as ready to accept IO.
414 void iscsi_unblock_session(struct iscsi_cls_session *session)
416 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
418 * make sure all the events have completed before tell the driver
421 flush_workqueue(iscsi_eh_timer_workq);
423 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
425 static void __iscsi_block_session(struct work_struct *work)
427 struct iscsi_cls_session *session =
428 container_of(work, struct iscsi_cls_session,
432 spin_lock_irqsave(&session->lock, flags);
433 session->state = ISCSI_SESSION_FAILED;
434 spin_unlock_irqrestore(&session->lock, flags);
435 scsi_target_block(&session->dev);
436 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
437 session->recovery_tmo * HZ);
440 void iscsi_block_session(struct iscsi_cls_session *session)
442 queue_work(iscsi_eh_timer_workq, &session->block_work);
444 EXPORT_SYMBOL_GPL(iscsi_block_session);
446 static void __iscsi_unbind_session(struct work_struct *work)
448 struct iscsi_cls_session *session =
449 container_of(work, struct iscsi_cls_session,
451 struct Scsi_Host *shost = iscsi_session_to_shost(session);
452 struct iscsi_host *ihost = shost->shost_data;
454 /* Prevent new scans and make sure scanning is not in progress */
455 mutex_lock(&ihost->mutex);
456 if (list_empty(&session->host_list)) {
457 mutex_unlock(&ihost->mutex);
460 list_del_init(&session->host_list);
461 mutex_unlock(&ihost->mutex);
463 scsi_remove_target(&session->dev);
464 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
467 static int iscsi_unbind_session(struct iscsi_cls_session *session)
469 struct Scsi_Host *shost = iscsi_session_to_shost(session);
470 struct iscsi_host *ihost = shost->shost_data;
472 return queue_work(ihost->scan_workq, &session->unbind_work);
475 struct iscsi_cls_session *
476 iscsi_alloc_session(struct Scsi_Host *shost,
477 struct iscsi_transport *transport)
479 struct iscsi_cls_session *session;
481 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
486 session->transport = transport;
487 session->recovery_tmo = 120;
488 session->state = ISCSI_SESSION_FREE;
489 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
490 INIT_LIST_HEAD(&session->host_list);
491 INIT_LIST_HEAD(&session->sess_list);
492 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
493 INIT_WORK(&session->block_work, __iscsi_block_session);
494 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
495 INIT_WORK(&session->scan_work, iscsi_scan_session);
496 spin_lock_init(&session->lock);
498 /* this is released in the dev's release function */
499 scsi_host_get(shost);
500 session->dev.parent = &shost->shost_gendev;
501 session->dev.release = iscsi_session_release;
502 device_initialize(&session->dev);
503 if (transport->sessiondata_size)
504 session->dd_data = &session[1];
507 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
509 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
511 struct Scsi_Host *shost = iscsi_session_to_shost(session);
512 struct iscsi_host *ihost;
516 ihost = shost->shost_data;
517 session->sid = atomic_add_return(1, &iscsi_session_nr);
518 session->target_id = target_id;
520 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
522 err = device_add(&session->dev);
524 iscsi_cls_session_printk(KERN_ERR, session,
525 "could not register session's dev\n");
528 transport_register_device(&session->dev);
530 spin_lock_irqsave(&sesslock, flags);
531 list_add(&session->sess_list, &sesslist);
532 spin_unlock_irqrestore(&sesslock, flags);
534 mutex_lock(&ihost->mutex);
535 list_add(&session->host_list, &ihost->sessions);
536 mutex_unlock(&ihost->mutex);
538 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
542 scsi_host_put(shost);
545 EXPORT_SYMBOL_GPL(iscsi_add_session);
548 * iscsi_create_session - create iscsi class session
550 * @transport: iscsi transport
551 * @target_id: which target
553 * This can be called from a LLD or iscsi_transport.
555 struct iscsi_cls_session *
556 iscsi_create_session(struct Scsi_Host *shost,
557 struct iscsi_transport *transport,
558 unsigned int target_id)
560 struct iscsi_cls_session *session;
562 session = iscsi_alloc_session(shost, transport);
566 if (iscsi_add_session(session, target_id)) {
567 iscsi_free_session(session);
572 EXPORT_SYMBOL_GPL(iscsi_create_session);
574 static void iscsi_conn_release(struct device *dev)
576 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
577 struct device *parent = conn->dev.parent;
583 static int iscsi_is_conn_dev(const struct device *dev)
585 return dev->release == iscsi_conn_release;
588 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
590 if (!iscsi_is_conn_dev(dev))
592 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
595 void iscsi_remove_session(struct iscsi_cls_session *session)
597 struct Scsi_Host *shost = iscsi_session_to_shost(session);
598 struct iscsi_host *ihost = shost->shost_data;
602 spin_lock_irqsave(&sesslock, flags);
603 list_del(&session->sess_list);
604 spin_unlock_irqrestore(&sesslock, flags);
606 /* make sure there are no blocks/unblocks queued */
607 flush_workqueue(iscsi_eh_timer_workq);
608 /* make sure the timedout callout is not running */
609 if (!cancel_delayed_work(&session->recovery_work))
610 flush_workqueue(iscsi_eh_timer_workq);
612 * If we are blocked let commands flow again. The lld or iscsi
613 * layer should set up the queuecommand to fail commands.
614 * We assume that LLD will not be calling block/unblock while
615 * removing the session.
617 spin_lock_irqsave(&session->lock, flags);
618 session->state = ISCSI_SESSION_FREE;
619 spin_unlock_irqrestore(&session->lock, flags);
621 scsi_target_unblock(&session->dev);
622 /* flush running scans then delete devices */
623 flush_workqueue(ihost->scan_workq);
624 __iscsi_unbind_session(&session->unbind_work);
626 /* hw iscsi may not have removed all connections from session */
627 err = device_for_each_child(&session->dev, NULL,
628 iscsi_iter_destroy_conn_fn);
630 iscsi_cls_session_printk(KERN_ERR, session,
631 "Could not delete all connections "
632 "for session. Error %d.\n", err);
634 transport_unregister_device(&session->dev);
635 device_del(&session->dev);
637 EXPORT_SYMBOL_GPL(iscsi_remove_session);
639 void iscsi_free_session(struct iscsi_cls_session *session)
641 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
642 put_device(&session->dev);
644 EXPORT_SYMBOL_GPL(iscsi_free_session);
647 * iscsi_destroy_session - destroy iscsi session
648 * @session: iscsi_session
650 * Can be called by a LLD or iscsi_transport. There must not be
651 * any running connections.
653 int iscsi_destroy_session(struct iscsi_cls_session *session)
655 iscsi_remove_session(session);
656 iscsi_free_session(session);
659 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
662 * iscsi_create_conn - create iscsi class connection
663 * @session: iscsi cls session
664 * @cid: connection id
666 * This can be called from a LLD or iscsi_transport. The connection
667 * is child of the session so cid must be unique for all connections
670 * Since we do not support MCS, cid will normally be zero. In some cases
671 * for software iscsi we could be trying to preallocate a connection struct
672 * in which case there could be two connection structs and cid would be
675 struct iscsi_cls_conn *
676 iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
678 struct iscsi_transport *transport = session->transport;
679 struct iscsi_cls_conn *conn;
683 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
687 if (transport->conndata_size)
688 conn->dd_data = &conn[1];
690 INIT_LIST_HEAD(&conn->conn_list);
691 conn->transport = transport;
694 /* this is released in the dev's release function */
695 if (!get_device(&session->dev))
698 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
700 conn->dev.parent = &session->dev;
701 conn->dev.release = iscsi_conn_release;
702 err = device_register(&conn->dev);
704 iscsi_cls_session_printk(KERN_ERR, session, "could not "
705 "register connection's dev\n");
706 goto release_parent_ref;
708 transport_register_device(&conn->dev);
710 spin_lock_irqsave(&connlock, flags);
711 list_add(&conn->conn_list, &connlist);
713 spin_unlock_irqrestore(&connlock, flags);
717 put_device(&session->dev);
723 EXPORT_SYMBOL_GPL(iscsi_create_conn);
726 * iscsi_destroy_conn - destroy iscsi class connection
727 * @conn: iscsi cls session
729 * This can be called from a LLD or iscsi_transport.
731 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
735 spin_lock_irqsave(&connlock, flags);
737 list_del(&conn->conn_list);
738 spin_unlock_irqrestore(&connlock, flags);
740 transport_unregister_device(&conn->dev);
741 device_unregister(&conn->dev);
744 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
747 * iscsi interface functions
749 static struct iscsi_internal *
750 iscsi_if_transport_lookup(struct iscsi_transport *tt)
752 struct iscsi_internal *priv;
755 spin_lock_irqsave(&iscsi_transport_lock, flags);
756 list_for_each_entry(priv, &iscsi_transports, list) {
757 if (tt == priv->iscsi_transport) {
758 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
762 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
767 iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
771 rc = netlink_broadcast(nls, skb, 0, 1, gfp);
773 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
781 iscsi_unicast_skb(struct sk_buff *skb, int pid)
785 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
787 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
794 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
795 char *data, uint32_t data_size)
797 struct nlmsghdr *nlh;
799 struct iscsi_uevent *ev;
801 struct iscsi_internal *priv;
802 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
805 priv = iscsi_if_transport_lookup(conn->transport);
809 skb = alloc_skb(len, GFP_ATOMIC);
811 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
812 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
813 "control PDU: OOM\n");
817 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
818 ev = NLMSG_DATA(nlh);
819 memset(ev, 0, sizeof(*ev));
820 ev->transport_handle = iscsi_handle(conn->transport);
821 ev->type = ISCSI_KEVENT_RECV_PDU;
822 ev->r.recv_req.cid = conn->cid;
823 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
824 pdu = (char*)ev + sizeof(*ev);
825 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
826 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
828 return iscsi_unicast_skb(skb, priv->daemon_pid);
830 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
832 void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
834 struct nlmsghdr *nlh;
836 struct iscsi_uevent *ev;
837 struct iscsi_internal *priv;
838 int len = NLMSG_SPACE(sizeof(*ev));
840 priv = iscsi_if_transport_lookup(conn->transport);
844 skb = alloc_skb(len, GFP_ATOMIC);
846 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
847 "conn error (%d)\n", error);
851 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
852 ev = NLMSG_DATA(nlh);
853 ev->transport_handle = iscsi_handle(conn->transport);
854 ev->type = ISCSI_KEVENT_CONN_ERROR;
855 ev->r.connerror.error = error;
856 ev->r.connerror.cid = conn->cid;
857 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
859 iscsi_broadcast_skb(skb, GFP_ATOMIC);
861 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
864 EXPORT_SYMBOL_GPL(iscsi_conn_error);
867 iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
868 void *payload, int size)
871 struct nlmsghdr *nlh;
872 int len = NLMSG_SPACE(size);
873 int flags = multi ? NLM_F_MULTI : 0;
874 int t = done ? NLMSG_DONE : type;
876 skb = alloc_skb(len, GFP_ATOMIC);
878 printk(KERN_ERR "Could not allocate skb to send reply.\n");
882 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
883 nlh->nlmsg_flags = flags;
884 memcpy(NLMSG_DATA(nlh), payload, size);
885 return iscsi_unicast_skb(skb, pid);
889 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
891 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
892 struct iscsi_stats *stats;
893 struct sk_buff *skbstat;
894 struct iscsi_cls_conn *conn;
895 struct nlmsghdr *nlhstat;
896 struct iscsi_uevent *evstat;
897 struct iscsi_internal *priv;
898 int len = NLMSG_SPACE(sizeof(*ev) +
899 sizeof(struct iscsi_stats) +
900 sizeof(struct iscsi_stats_custom) *
901 ISCSI_STATS_CUSTOM_MAX);
904 priv = iscsi_if_transport_lookup(transport);
908 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
915 skbstat = alloc_skb(len, GFP_ATOMIC);
917 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
918 "deliver stats: OOM\n");
922 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
923 (len - sizeof(*nlhstat)), 0);
924 evstat = NLMSG_DATA(nlhstat);
925 memset(evstat, 0, sizeof(*evstat));
926 evstat->transport_handle = iscsi_handle(conn->transport);
927 evstat->type = nlh->nlmsg_type;
928 evstat->u.get_stats.cid =
930 evstat->u.get_stats.sid =
932 stats = (struct iscsi_stats *)
933 ((char*)evstat + sizeof(*evstat));
934 memset(stats, 0, sizeof(*stats));
936 transport->get_stats(conn, stats);
937 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
938 sizeof(struct iscsi_stats) +
939 sizeof(struct iscsi_stats_custom) *
940 stats->custom_length);
941 actual_size -= sizeof(*nlhstat);
942 actual_size = NLMSG_LENGTH(actual_size);
943 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
944 nlhstat->nlmsg_len = actual_size;
946 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
947 } while (err < 0 && err != -ECONNREFUSED);
953 * iscsi_session_event - send session destr. completion event
954 * @session: iscsi class session
955 * @event: type of event
957 int iscsi_session_event(struct iscsi_cls_session *session,
958 enum iscsi_uevent_e event)
960 struct iscsi_internal *priv;
961 struct Scsi_Host *shost;
962 struct iscsi_uevent *ev;
964 struct nlmsghdr *nlh;
965 int rc, len = NLMSG_SPACE(sizeof(*ev));
967 priv = iscsi_if_transport_lookup(session->transport);
970 shost = iscsi_session_to_shost(session);
972 skb = alloc_skb(len, GFP_KERNEL);
974 iscsi_cls_session_printk(KERN_ERR, session,
975 "Cannot notify userspace of session "
976 "event %u\n", event);
980 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
981 ev = NLMSG_DATA(nlh);
982 ev->transport_handle = iscsi_handle(session->transport);
986 case ISCSI_KEVENT_DESTROY_SESSION:
987 ev->r.d_session.host_no = shost->host_no;
988 ev->r.d_session.sid = session->sid;
990 case ISCSI_KEVENT_CREATE_SESSION:
991 ev->r.c_session_ret.host_no = shost->host_no;
992 ev->r.c_session_ret.sid = session->sid;
994 case ISCSI_KEVENT_UNBIND_SESSION:
995 ev->r.unbind_session.host_no = shost->host_no;
996 ev->r.unbind_session.sid = session->sid;
999 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1006 * this will occur if the daemon is not up, so we just warn
1007 * the user and when the daemon is restarted it will handle it
1009 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
1011 iscsi_cls_session_printk(KERN_ERR, session,
1012 "Cannot notify userspace of session "
1013 "event %u. Check iscsi daemon\n",
1017 EXPORT_SYMBOL_GPL(iscsi_session_event);
1020 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
1022 struct iscsi_transport *transport = priv->iscsi_transport;
1023 struct iscsi_cls_session *session;
1026 session = transport->create_session(transport, &priv->t,
1027 ev->u.c_session.cmds_max,
1028 ev->u.c_session.queue_depth,
1029 ev->u.c_session.initial_cmdsn,
1034 ev->r.c_session_ret.host_no = hostno;
1035 ev->r.c_session_ret.sid = session->sid;
1040 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1042 struct iscsi_cls_conn *conn;
1043 struct iscsi_cls_session *session;
1045 session = iscsi_session_lookup(ev->u.c_conn.sid);
1047 printk(KERN_ERR "iscsi: invalid session %d.\n",
1052 conn = transport->create_conn(session, ev->u.c_conn.cid);
1054 iscsi_cls_session_printk(KERN_ERR, session,
1055 "couldn't create a new connection.");
1059 ev->r.c_conn_ret.sid = session->sid;
1060 ev->r.c_conn_ret.cid = conn->cid;
1065 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1067 struct iscsi_cls_conn *conn;
1069 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
1073 if (transport->destroy_conn)
1074 transport->destroy_conn(conn);
1079 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1081 char *data = (char*)ev + sizeof(*ev);
1082 struct iscsi_cls_conn *conn;
1083 struct iscsi_cls_session *session;
1084 int err = 0, value = 0;
1086 session = iscsi_session_lookup(ev->u.set_param.sid);
1087 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1088 if (!conn || !session)
1091 switch (ev->u.set_param.param) {
1092 case ISCSI_PARAM_SESS_RECOVERY_TMO:
1093 sscanf(data, "%d", &value);
1095 session->recovery_tmo = value;
1098 err = transport->set_param(conn, ev->u.set_param.param,
1099 data, ev->u.set_param.len);
1106 iscsi_if_transport_ep(struct iscsi_transport *transport,
1107 struct iscsi_uevent *ev, int msg_type)
1109 struct sockaddr *dst_addr;
1113 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1114 if (!transport->ep_connect)
1117 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1118 rc = transport->ep_connect(dst_addr,
1119 ev->u.ep_connect.non_blocking,
1120 &ev->r.ep_connect_ret.handle);
1122 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1123 if (!transport->ep_poll)
1126 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
1127 ev->u.ep_poll.timeout_ms);
1129 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1130 if (!transport->ep_disconnect)
1133 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
1140 iscsi_tgt_dscvr(struct iscsi_transport *transport,
1141 struct iscsi_uevent *ev)
1143 struct Scsi_Host *shost;
1144 struct sockaddr *dst_addr;
1147 if (!transport->tgt_dscvr)
1150 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1151 if (IS_ERR(shost)) {
1152 printk(KERN_ERR "target discovery could not find host no %u\n",
1153 ev->u.tgt_dscvr.host_no);
1158 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1159 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1160 ev->u.tgt_dscvr.enable, dst_addr);
1161 scsi_host_put(shost);
1166 iscsi_set_host_param(struct iscsi_transport *transport,
1167 struct iscsi_uevent *ev)
1169 char *data = (char*)ev + sizeof(*ev);
1170 struct Scsi_Host *shost;
1173 if (!transport->set_host_param)
1176 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1177 if (IS_ERR(shost)) {
1178 printk(KERN_ERR "set_host_param could not find host no %u\n",
1179 ev->u.set_host_param.host_no);
1183 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1184 data, ev->u.set_host_param.len);
1185 scsi_host_put(shost);
1190 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1193 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1194 struct iscsi_transport *transport = NULL;
1195 struct iscsi_internal *priv;
1196 struct iscsi_cls_session *session;
1197 struct iscsi_cls_conn *conn;
1199 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1202 transport = priv->iscsi_transport;
1204 if (!try_module_get(transport->owner))
1207 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1209 switch (nlh->nlmsg_type) {
1210 case ISCSI_UEVENT_CREATE_SESSION:
1211 err = iscsi_if_create_session(priv, ev);
1213 case ISCSI_UEVENT_DESTROY_SESSION:
1214 session = iscsi_session_lookup(ev->u.d_session.sid);
1216 transport->destroy_session(session);
1220 case ISCSI_UEVENT_UNBIND_SESSION:
1221 session = iscsi_session_lookup(ev->u.d_session.sid);
1223 iscsi_unbind_session(session);
1227 case ISCSI_UEVENT_CREATE_CONN:
1228 err = iscsi_if_create_conn(transport, ev);
1230 case ISCSI_UEVENT_DESTROY_CONN:
1231 err = iscsi_if_destroy_conn(transport, ev);
1233 case ISCSI_UEVENT_BIND_CONN:
1234 session = iscsi_session_lookup(ev->u.b_conn.sid);
1235 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1237 if (session && conn)
1238 ev->r.retcode = transport->bind_conn(session, conn,
1239 ev->u.b_conn.transport_eph,
1240 ev->u.b_conn.is_leading);
1244 case ISCSI_UEVENT_SET_PARAM:
1245 err = iscsi_set_param(transport, ev);
1247 case ISCSI_UEVENT_START_CONN:
1248 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
1250 ev->r.retcode = transport->start_conn(conn);
1254 case ISCSI_UEVENT_STOP_CONN:
1255 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
1257 transport->stop_conn(conn, ev->u.stop_conn.flag);
1261 case ISCSI_UEVENT_SEND_PDU:
1262 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
1264 ev->r.retcode = transport->send_pdu(conn,
1265 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1266 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1267 ev->u.send_pdu.data_size);
1271 case ISCSI_UEVENT_GET_STATS:
1272 err = iscsi_if_get_stats(transport, nlh);
1274 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1275 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1276 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1277 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1279 case ISCSI_UEVENT_TGT_DSCVR:
1280 err = iscsi_tgt_dscvr(transport, ev);
1282 case ISCSI_UEVENT_SET_HOST_PARAM:
1283 err = iscsi_set_host_param(transport, ev);
1290 module_put(transport->owner);
1295 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1296 * Malformed skbs with wrong lengths or invalid creds are not processed.
1299 iscsi_if_rx(struct sk_buff *skb)
1301 mutex_lock(&rx_queue_mutex);
1302 while (skb->len >= NLMSG_SPACE(0)) {
1305 struct nlmsghdr *nlh;
1306 struct iscsi_uevent *ev;
1308 nlh = nlmsg_hdr(skb);
1309 if (nlh->nlmsg_len < sizeof(*nlh) ||
1310 skb->len < nlh->nlmsg_len) {
1314 ev = NLMSG_DATA(nlh);
1315 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1316 if (rlen > skb->len)
1319 err = iscsi_if_recv_msg(skb, nlh);
1321 ev->type = ISCSI_KEVENT_IF_ERROR;
1326 * special case for GET_STATS:
1327 * on success - sending reply and stats from
1328 * inside of if_recv_msg(),
1329 * on error - fall through.
1331 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1333 err = iscsi_if_send_reply(
1334 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1335 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1336 } while (err < 0 && err != -ECONNREFUSED);
1337 skb_pull(skb, rlen);
1339 mutex_unlock(&rx_queue_mutex);
1342 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1343 struct device_attribute dev_attr_##_prefix##_##_name = \
1344 __ATTR(_name,_mode,_show,_store)
1347 * iSCSI connection attrs
1349 #define iscsi_conn_attr_show(param) \
1351 show_conn_param_##param(struct device *dev, \
1352 struct device_attribute *attr, char *buf) \
1354 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
1355 struct iscsi_transport *t = conn->transport; \
1356 return t->get_conn_param(conn, param, buf); \
1359 #define iscsi_conn_attr(field, param) \
1360 iscsi_conn_attr_show(param) \
1361 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
1364 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1365 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1366 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1367 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1368 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1369 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1370 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1371 iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1372 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1373 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1374 iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
1375 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1376 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
1379 * iSCSI session attrs
1381 #define iscsi_session_attr_show(param, perm) \
1383 show_session_param_##param(struct device *dev, \
1384 struct device_attribute *attr, char *buf) \
1386 struct iscsi_cls_session *session = \
1387 iscsi_dev_to_session(dev->parent); \
1388 struct iscsi_transport *t = session->transport; \
1390 if (perm && !capable(CAP_SYS_ADMIN)) \
1392 return t->get_session_param(session, param, buf); \
1395 #define iscsi_session_attr(field, param, perm) \
1396 iscsi_session_attr_show(param, perm) \
1397 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
1400 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1401 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1402 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1403 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1404 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1405 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1406 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1407 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1408 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1409 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1410 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1411 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1412 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1413 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
1414 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1415 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1416 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
1419 show_priv_session_state(struct device *dev, struct device_attribute *attr,
1422 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
1423 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1425 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1428 #define iscsi_priv_session_attr_show(field, format) \
1430 show_priv_session_##field(struct device *dev, \
1431 struct device_attribute *attr, char *buf) \
1433 struct iscsi_cls_session *session = \
1434 iscsi_dev_to_session(dev->parent); \
1435 return sprintf(buf, format"\n", session->field); \
1438 #define iscsi_priv_session_attr(field, format) \
1439 iscsi_priv_session_attr_show(field, format) \
1440 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1442 iscsi_priv_session_attr(recovery_tmo, "%d");
1447 #define iscsi_host_attr_show(param) \
1449 show_host_param_##param(struct device *dev, \
1450 struct device_attribute *attr, char *buf) \
1452 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1453 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1454 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1457 #define iscsi_host_attr(field, param) \
1458 iscsi_host_attr_show(param) \
1459 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1462 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
1463 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
1464 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
1465 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
1467 #define SETUP_PRIV_SESSION_RD_ATTR(field) \
1469 priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
1474 #define SETUP_SESSION_RD_ATTR(field, param_flag) \
1476 if (tt->param_mask & param_flag) { \
1477 priv->session_attrs[count] = &dev_attr_sess_##field; \
1482 #define SETUP_CONN_RD_ATTR(field, param_flag) \
1484 if (tt->param_mask & param_flag) { \
1485 priv->conn_attrs[count] = &dev_attr_conn_##field; \
1490 #define SETUP_HOST_RD_ATTR(field, param_flag) \
1492 if (tt->host_param_mask & param_flag) { \
1493 priv->host_attrs[count] = &dev_attr_host_##field; \
1498 static int iscsi_session_match(struct attribute_container *cont,
1501 struct iscsi_cls_session *session;
1502 struct Scsi_Host *shost;
1503 struct iscsi_internal *priv;
1505 if (!iscsi_is_session_dev(dev))
1508 session = iscsi_dev_to_session(dev);
1509 shost = iscsi_session_to_shost(session);
1510 if (!shost->transportt)
1513 priv = to_iscsi_internal(shost->transportt);
1514 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1517 return &priv->session_cont.ac == cont;
1520 static int iscsi_conn_match(struct attribute_container *cont,
1523 struct iscsi_cls_session *session;
1524 struct iscsi_cls_conn *conn;
1525 struct Scsi_Host *shost;
1526 struct iscsi_internal *priv;
1528 if (!iscsi_is_conn_dev(dev))
1531 conn = iscsi_dev_to_conn(dev);
1532 session = iscsi_dev_to_session(conn->dev.parent);
1533 shost = iscsi_session_to_shost(session);
1535 if (!shost->transportt)
1538 priv = to_iscsi_internal(shost->transportt);
1539 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1542 return &priv->conn_cont.ac == cont;
1545 static int iscsi_host_match(struct attribute_container *cont,
1548 struct Scsi_Host *shost;
1549 struct iscsi_internal *priv;
1551 if (!scsi_is_host_device(dev))
1554 shost = dev_to_shost(dev);
1555 if (!shost->transportt ||
1556 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1559 priv = to_iscsi_internal(shost->transportt);
1560 return &priv->t.host_attrs.ac == cont;
1563 struct scsi_transport_template *
1564 iscsi_register_transport(struct iscsi_transport *tt)
1566 struct iscsi_internal *priv;
1567 unsigned long flags;
1572 priv = iscsi_if_transport_lookup(tt);
1576 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1579 INIT_LIST_HEAD(&priv->list);
1580 priv->daemon_pid = -1;
1581 priv->iscsi_transport = tt;
1582 priv->t.user_scan = iscsi_user_scan;
1584 priv->dev.class = &iscsi_transport_class;
1585 snprintf(priv->dev.bus_id, BUS_ID_SIZE, "%s", tt->name);
1586 err = device_register(&priv->dev);
1590 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
1592 goto unregister_dev;
1594 /* host parameters */
1595 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1596 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1597 priv->t.host_attrs.ac.match = iscsi_host_match;
1598 priv->t.host_size = sizeof(struct iscsi_host);
1599 transport_container_register(&priv->t.host_attrs);
1601 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
1602 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
1603 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
1604 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
1605 BUG_ON(count > ISCSI_HOST_ATTRS);
1606 priv->host_attrs[count] = NULL;
1609 /* connection parameters */
1610 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1611 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1612 priv->conn_cont.ac.match = iscsi_conn_match;
1613 transport_container_register(&priv->conn_cont);
1615 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1616 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1617 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1618 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1619 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1620 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1621 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1622 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
1623 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
1624 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1625 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
1626 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1627 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
1629 BUG_ON(count > ISCSI_CONN_ATTRS);
1630 priv->conn_attrs[count] = NULL;
1633 /* session parameters */
1634 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1635 priv->session_cont.ac.class = &iscsi_session_class.class;
1636 priv->session_cont.ac.match = iscsi_session_match;
1637 transport_container_register(&priv->session_cont);
1639 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1640 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1641 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1642 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1643 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1644 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1645 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1646 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
1647 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1648 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
1649 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1650 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1651 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1652 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
1653 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
1654 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1655 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
1656 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
1657 SETUP_PRIV_SESSION_RD_ATTR(state);
1659 BUG_ON(count > ISCSI_SESSION_ATTRS);
1660 priv->session_attrs[count] = NULL;
1662 spin_lock_irqsave(&iscsi_transport_lock, flags);
1663 list_add(&priv->list, &iscsi_transports);
1664 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1666 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1670 device_unregister(&priv->dev);
1675 EXPORT_SYMBOL_GPL(iscsi_register_transport);
1677 int iscsi_unregister_transport(struct iscsi_transport *tt)
1679 struct iscsi_internal *priv;
1680 unsigned long flags;
1684 mutex_lock(&rx_queue_mutex);
1686 priv = iscsi_if_transport_lookup(tt);
1689 spin_lock_irqsave(&iscsi_transport_lock, flags);
1690 list_del(&priv->list);
1691 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1693 transport_container_unregister(&priv->conn_cont);
1694 transport_container_unregister(&priv->session_cont);
1695 transport_container_unregister(&priv->t.host_attrs);
1697 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
1698 device_unregister(&priv->dev);
1699 mutex_unlock(&rx_queue_mutex);
1703 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1705 static __init int iscsi_transport_init(void)
1709 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
1710 ISCSI_TRANSPORT_VERSION);
1712 atomic_set(&iscsi_session_nr, 0);
1714 err = class_register(&iscsi_transport_class);
1718 err = transport_class_register(&iscsi_host_class);
1720 goto unregister_transport_class;
1722 err = transport_class_register(&iscsi_connection_class);
1724 goto unregister_host_class;
1726 err = transport_class_register(&iscsi_session_class);
1728 goto unregister_conn_class;
1730 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx, NULL,
1734 goto unregister_session_class;
1737 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1738 if (!iscsi_eh_timer_workq)
1744 netlink_kernel_release(nls);
1745 unregister_session_class:
1746 transport_class_unregister(&iscsi_session_class);
1747 unregister_conn_class:
1748 transport_class_unregister(&iscsi_connection_class);
1749 unregister_host_class:
1750 transport_class_unregister(&iscsi_host_class);
1751 unregister_transport_class:
1752 class_unregister(&iscsi_transport_class);
1756 static void __exit iscsi_transport_exit(void)
1758 destroy_workqueue(iscsi_eh_timer_workq);
1759 netlink_kernel_release(nls);
1760 transport_class_unregister(&iscsi_connection_class);
1761 transport_class_unregister(&iscsi_session_class);
1762 transport_class_unregister(&iscsi_host_class);
1763 class_unregister(&iscsi_transport_class);
1766 module_init(iscsi_transport_init);
1767 module_exit(iscsi_transport_exit);
1769 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1770 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1771 "Alex Aizman <itn780@yahoo.com>");
1772 MODULE_DESCRIPTION("iSCSI Transport Interface");
1773 MODULE_LICENSE("GPL");
1774 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);