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-868"
38 struct iscsi_internal {
40 struct scsi_transport_template t;
41 struct iscsi_transport *iscsi_transport;
42 struct list_head list;
43 struct class_device cdev;
45 struct class_device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
46 struct transport_container conn_cont;
47 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
48 struct transport_container session_cont;
49 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
52 static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
53 static struct workqueue_struct *iscsi_eh_timer_workq;
56 * list of registered transports and lock that must
57 * be held while accessing list. The iscsi_transport_lock must
58 * be acquired after the rx_queue_mutex.
60 static LIST_HEAD(iscsi_transports);
61 static DEFINE_SPINLOCK(iscsi_transport_lock);
63 #define to_iscsi_internal(tmpl) \
64 container_of(tmpl, struct iscsi_internal, t)
66 #define cdev_to_iscsi_internal(_cdev) \
67 container_of(_cdev, struct iscsi_internal, cdev)
69 static void iscsi_transport_release(struct class_device *cdev)
71 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
76 * iscsi_transport_class represents the iscsi_transports that are
79 static struct class iscsi_transport_class = {
80 .name = "iscsi_transport",
81 .release = iscsi_transport_release,
85 show_transport_handle(struct class_device *cdev, char *buf)
87 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
88 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
90 static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
92 #define show_transport_attr(name, format) \
94 show_transport_##name(struct class_device *cdev, char *buf) \
96 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
97 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
99 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
101 show_transport_attr(caps, "0x%x");
102 show_transport_attr(max_lun, "%d");
103 show_transport_attr(max_conn, "%d");
104 show_transport_attr(max_cmd_len, "%d");
106 static struct attribute *iscsi_transport_attrs[] = {
107 &class_device_attr_handle.attr,
108 &class_device_attr_caps.attr,
109 &class_device_attr_max_lun.attr,
110 &class_device_attr_max_conn.attr,
111 &class_device_attr_max_cmd_len.attr,
115 static struct attribute_group iscsi_transport_group = {
116 .attrs = iscsi_transport_attrs,
121 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
122 struct class_device *cdev)
124 struct Scsi_Host *shost = dev_to_shost(dev);
125 struct iscsi_host *ihost = shost->shost_data;
127 memset(ihost, 0, sizeof(*ihost));
128 INIT_LIST_HEAD(&ihost->sessions);
129 mutex_init(&ihost->mutex);
130 atomic_set(&ihost->nr_scans, 0);
132 snprintf(ihost->scan_workq_name, KOBJ_NAME_LEN, "iscsi_scan_%d",
134 ihost->scan_workq = create_singlethread_workqueue(
135 ihost->scan_workq_name);
136 if (!ihost->scan_workq)
141 static int iscsi_remove_host(struct transport_container *tc, struct device *dev,
142 struct class_device *cdev)
144 struct Scsi_Host *shost = dev_to_shost(dev);
145 struct iscsi_host *ihost = shost->shost_data;
147 destroy_workqueue(ihost->scan_workq);
151 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
157 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
163 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
169 static struct sock *nls;
170 static DEFINE_MUTEX(rx_queue_mutex);
172 static LIST_HEAD(sesslist);
173 static DEFINE_SPINLOCK(sesslock);
174 static LIST_HEAD(connlist);
175 static DEFINE_SPINLOCK(connlock);
177 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
179 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
184 * Returns the matching session to a given sid
186 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
189 struct iscsi_cls_session *sess;
191 spin_lock_irqsave(&sesslock, flags);
192 list_for_each_entry(sess, &sesslist, sess_list) {
193 if (sess->sid == sid) {
194 spin_unlock_irqrestore(&sesslock, flags);
198 spin_unlock_irqrestore(&sesslock, flags);
203 * Returns the matching connection to a given sid / cid tuple
205 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
208 struct iscsi_cls_conn *conn;
210 spin_lock_irqsave(&connlock, flags);
211 list_for_each_entry(conn, &connlist, conn_list) {
212 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
213 spin_unlock_irqrestore(&connlock, flags);
217 spin_unlock_irqrestore(&connlock, flags);
222 * The following functions can be used by LLDs that allocate
223 * their own scsi_hosts or by software iscsi LLDs
228 } iscsi_session_state_names[] = {
229 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
230 { ISCSI_SESSION_FAILED, "FAILED" },
231 { ISCSI_SESSION_FREE, "FREE" },
234 const char *iscsi_session_state_name(int state)
239 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
240 if (iscsi_session_state_names[i].value == state) {
241 name = iscsi_session_state_names[i].name;
248 int iscsi_session_chkready(struct iscsi_cls_session *session)
253 spin_lock_irqsave(&session->lock, flags);
254 switch (session->state) {
255 case ISCSI_SESSION_LOGGED_IN:
258 case ISCSI_SESSION_FAILED:
259 err = DID_IMM_RETRY << 16;
261 case ISCSI_SESSION_FREE:
262 err = DID_NO_CONNECT << 16;
265 err = DID_NO_CONNECT << 16;
268 spin_unlock_irqrestore(&session->lock, flags);
271 EXPORT_SYMBOL_GPL(iscsi_session_chkready);
273 static void iscsi_session_release(struct device *dev)
275 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
276 struct Scsi_Host *shost;
278 shost = iscsi_session_to_shost(session);
279 scsi_host_put(shost);
283 static int iscsi_is_session_dev(const struct device *dev)
285 return dev->release == iscsi_session_release;
289 * iscsi_scan_finished - helper to report when running scans are done
291 * @time: scan run time
293 * This function can be used by drives like qla4xxx to report to the scsi
294 * layer when the scans it kicked off at module load time are done.
296 int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
298 struct iscsi_host *ihost = shost->shost_data;
300 * qla4xxx will have kicked off some session unblocks before calling
301 * scsi_scan_host, so just wait for them to complete.
303 return !atomic_read(&ihost->nr_scans);
305 EXPORT_SYMBOL_GPL(iscsi_scan_finished);
307 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
310 struct iscsi_host *ihost = shost->shost_data;
311 struct iscsi_cls_session *session;
313 mutex_lock(&ihost->mutex);
314 list_for_each_entry(session, &ihost->sessions, host_list) {
315 if ((channel == SCAN_WILD_CARD || channel == 0) &&
316 (id == SCAN_WILD_CARD || id == session->target_id))
317 scsi_scan_target(&session->dev, 0,
318 session->target_id, lun, 1);
320 mutex_unlock(&ihost->mutex);
325 static void iscsi_scan_session(struct work_struct *work)
327 struct iscsi_cls_session *session =
328 container_of(work, struct iscsi_cls_session, scan_work);
329 struct Scsi_Host *shost = iscsi_session_to_shost(session);
330 struct iscsi_host *ihost = shost->shost_data;
333 spin_lock_irqsave(&session->lock, flags);
334 if (session->state != ISCSI_SESSION_LOGGED_IN) {
335 spin_unlock_irqrestore(&session->lock, flags);
338 spin_unlock_irqrestore(&session->lock, flags);
340 scsi_scan_target(&session->dev, 0, session->target_id,
343 atomic_dec(&ihost->nr_scans);
346 static void session_recovery_timedout(struct work_struct *work)
348 struct iscsi_cls_session *session =
349 container_of(work, struct iscsi_cls_session,
353 iscsi_cls_session_printk(KERN_INFO, session,
354 "session recovery timed out after %d secs\n",
355 session->recovery_tmo);
357 spin_lock_irqsave(&session->lock, flags);
358 switch (session->state) {
359 case ISCSI_SESSION_FAILED:
360 session->state = ISCSI_SESSION_FREE;
362 case ISCSI_SESSION_LOGGED_IN:
363 case ISCSI_SESSION_FREE:
364 /* we raced with the unblock's flush */
365 spin_unlock_irqrestore(&session->lock, flags);
368 spin_unlock_irqrestore(&session->lock, flags);
370 if (session->transport->session_recovery_timedout)
371 session->transport->session_recovery_timedout(session);
373 scsi_target_unblock(&session->dev);
376 void __iscsi_unblock_session(struct iscsi_cls_session *session)
378 if (!cancel_delayed_work(&session->recovery_work))
379 flush_workqueue(iscsi_eh_timer_workq);
380 scsi_target_unblock(&session->dev);
383 void iscsi_unblock_session(struct iscsi_cls_session *session)
385 struct Scsi_Host *shost = iscsi_session_to_shost(session);
386 struct iscsi_host *ihost = shost->shost_data;
389 spin_lock_irqsave(&session->lock, flags);
390 session->state = ISCSI_SESSION_LOGGED_IN;
391 spin_unlock_irqrestore(&session->lock, flags);
393 __iscsi_unblock_session(session);
395 * Only do kernel scanning if the driver is properly hooked into
396 * the async scanning code (drivers like iscsi_tcp do login and
397 * scanning from userspace).
399 if (shost->hostt->scan_finished) {
400 if (queue_work(ihost->scan_workq, &session->scan_work))
401 atomic_inc(&ihost->nr_scans);
404 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
406 void iscsi_block_session(struct iscsi_cls_session *session)
410 spin_lock_irqsave(&session->lock, flags);
411 session->state = ISCSI_SESSION_FAILED;
412 spin_unlock_irqrestore(&session->lock, flags);
414 scsi_target_block(&session->dev);
415 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
416 session->recovery_tmo * HZ);
418 EXPORT_SYMBOL_GPL(iscsi_block_session);
420 static void __iscsi_unbind_session(struct work_struct *work)
422 struct iscsi_cls_session *session =
423 container_of(work, struct iscsi_cls_session,
425 struct Scsi_Host *shost = iscsi_session_to_shost(session);
426 struct iscsi_host *ihost = shost->shost_data;
428 /* Prevent new scans and make sure scanning is not in progress */
429 mutex_lock(&ihost->mutex);
430 if (list_empty(&session->host_list)) {
431 mutex_unlock(&ihost->mutex);
434 list_del_init(&session->host_list);
435 mutex_unlock(&ihost->mutex);
437 scsi_remove_target(&session->dev);
438 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
441 static int iscsi_unbind_session(struct iscsi_cls_session *session)
443 struct Scsi_Host *shost = iscsi_session_to_shost(session);
444 struct iscsi_host *ihost = shost->shost_data;
446 return queue_work(ihost->scan_workq, &session->unbind_work);
449 struct iscsi_cls_session *
450 iscsi_alloc_session(struct Scsi_Host *shost,
451 struct iscsi_transport *transport)
453 struct iscsi_cls_session *session;
455 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
460 session->transport = transport;
461 session->recovery_tmo = 120;
462 session->state = ISCSI_SESSION_FREE;
463 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
464 INIT_LIST_HEAD(&session->host_list);
465 INIT_LIST_HEAD(&session->sess_list);
466 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
467 INIT_WORK(&session->scan_work, iscsi_scan_session);
468 spin_lock_init(&session->lock);
470 /* this is released in the dev's release function */
471 scsi_host_get(shost);
472 session->dev.parent = &shost->shost_gendev;
473 session->dev.release = iscsi_session_release;
474 device_initialize(&session->dev);
475 if (transport->sessiondata_size)
476 session->dd_data = &session[1];
479 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
481 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
483 struct Scsi_Host *shost = iscsi_session_to_shost(session);
484 struct iscsi_host *ihost;
488 ihost = shost->shost_data;
489 session->sid = atomic_add_return(1, &iscsi_session_nr);
490 session->target_id = target_id;
492 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
494 err = device_add(&session->dev);
496 iscsi_cls_session_printk(KERN_ERR, session,
497 "could not register session's dev\n");
500 transport_register_device(&session->dev);
502 spin_lock_irqsave(&sesslock, flags);
503 list_add(&session->sess_list, &sesslist);
504 spin_unlock_irqrestore(&sesslock, flags);
506 mutex_lock(&ihost->mutex);
507 list_add(&session->host_list, &ihost->sessions);
508 mutex_unlock(&ihost->mutex);
510 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
514 scsi_host_put(shost);
517 EXPORT_SYMBOL_GPL(iscsi_add_session);
520 * iscsi_create_session - create iscsi class session
522 * @transport: iscsi transport
523 * @target_id: which target
525 * This can be called from a LLD or iscsi_transport.
527 struct iscsi_cls_session *
528 iscsi_create_session(struct Scsi_Host *shost,
529 struct iscsi_transport *transport,
530 unsigned int target_id)
532 struct iscsi_cls_session *session;
534 session = iscsi_alloc_session(shost, transport);
538 if (iscsi_add_session(session, target_id)) {
539 iscsi_free_session(session);
544 EXPORT_SYMBOL_GPL(iscsi_create_session);
546 static void iscsi_conn_release(struct device *dev)
548 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
549 struct device *parent = conn->dev.parent;
555 static int iscsi_is_conn_dev(const struct device *dev)
557 return dev->release == iscsi_conn_release;
560 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
562 if (!iscsi_is_conn_dev(dev))
564 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
567 void iscsi_remove_session(struct iscsi_cls_session *session)
569 struct Scsi_Host *shost = iscsi_session_to_shost(session);
570 struct iscsi_host *ihost = shost->shost_data;
574 spin_lock_irqsave(&sesslock, flags);
575 list_del(&session->sess_list);
576 spin_unlock_irqrestore(&sesslock, flags);
579 * If we are blocked let commands flow again. The lld or iscsi
580 * layer should set up the queuecommand to fail commands.
582 spin_lock_irqsave(&session->lock, flags);
583 session->state = ISCSI_SESSION_FREE;
584 spin_unlock_irqrestore(&session->lock, flags);
585 __iscsi_unblock_session(session);
586 __iscsi_unbind_session(&session->unbind_work);
588 /* flush running scans */
589 flush_workqueue(ihost->scan_workq);
591 * If the session dropped while removing devices then we need to make
592 * sure it is not blocked
594 if (!cancel_delayed_work(&session->recovery_work))
595 flush_workqueue(iscsi_eh_timer_workq);
597 /* hw iscsi may not have removed all connections from session */
598 err = device_for_each_child(&session->dev, NULL,
599 iscsi_iter_destroy_conn_fn);
601 iscsi_cls_session_printk(KERN_ERR, session,
602 "Could not delete all connections "
603 "for session. Error %d.\n", err);
605 transport_unregister_device(&session->dev);
606 device_del(&session->dev);
608 EXPORT_SYMBOL_GPL(iscsi_remove_session);
610 void iscsi_free_session(struct iscsi_cls_session *session)
612 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
613 put_device(&session->dev);
615 EXPORT_SYMBOL_GPL(iscsi_free_session);
618 * iscsi_destroy_session - destroy iscsi session
619 * @session: iscsi_session
621 * Can be called by a LLD or iscsi_transport. There must not be
622 * any running connections.
624 int iscsi_destroy_session(struct iscsi_cls_session *session)
626 iscsi_remove_session(session);
627 iscsi_free_session(session);
630 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
633 * iscsi_create_conn - create iscsi class connection
634 * @session: iscsi cls session
635 * @cid: connection id
637 * This can be called from a LLD or iscsi_transport. The connection
638 * is child of the session so cid must be unique for all connections
641 * Since we do not support MCS, cid will normally be zero. In some cases
642 * for software iscsi we could be trying to preallocate a connection struct
643 * in which case there could be two connection structs and cid would be
646 struct iscsi_cls_conn *
647 iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
649 struct iscsi_transport *transport = session->transport;
650 struct iscsi_cls_conn *conn;
654 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
658 if (transport->conndata_size)
659 conn->dd_data = &conn[1];
661 INIT_LIST_HEAD(&conn->conn_list);
662 conn->transport = transport;
665 /* this is released in the dev's release function */
666 if (!get_device(&session->dev))
669 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
671 conn->dev.parent = &session->dev;
672 conn->dev.release = iscsi_conn_release;
673 err = device_register(&conn->dev);
675 iscsi_cls_session_printk(KERN_ERR, session, "could not "
676 "register connection's dev\n");
677 goto release_parent_ref;
679 transport_register_device(&conn->dev);
681 spin_lock_irqsave(&connlock, flags);
682 list_add(&conn->conn_list, &connlist);
684 spin_unlock_irqrestore(&connlock, flags);
688 put_device(&session->dev);
694 EXPORT_SYMBOL_GPL(iscsi_create_conn);
697 * iscsi_destroy_conn - destroy iscsi class connection
698 * @conn: iscsi cls session
700 * This can be called from a LLD or iscsi_transport.
702 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
706 spin_lock_irqsave(&connlock, flags);
708 list_del(&conn->conn_list);
709 spin_unlock_irqrestore(&connlock, flags);
711 transport_unregister_device(&conn->dev);
712 device_unregister(&conn->dev);
715 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
718 * iscsi interface functions
720 static struct iscsi_internal *
721 iscsi_if_transport_lookup(struct iscsi_transport *tt)
723 struct iscsi_internal *priv;
726 spin_lock_irqsave(&iscsi_transport_lock, flags);
727 list_for_each_entry(priv, &iscsi_transports, list) {
728 if (tt == priv->iscsi_transport) {
729 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
733 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
738 iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
742 rc = netlink_broadcast(nls, skb, 0, 1, gfp);
744 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
752 iscsi_unicast_skb(struct sk_buff *skb, int pid)
756 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
758 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
765 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
766 char *data, uint32_t data_size)
768 struct nlmsghdr *nlh;
770 struct iscsi_uevent *ev;
772 struct iscsi_internal *priv;
773 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
776 priv = iscsi_if_transport_lookup(conn->transport);
780 skb = alloc_skb(len, GFP_ATOMIC);
782 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
783 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
784 "control PDU: OOM\n");
788 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
789 ev = NLMSG_DATA(nlh);
790 memset(ev, 0, sizeof(*ev));
791 ev->transport_handle = iscsi_handle(conn->transport);
792 ev->type = ISCSI_KEVENT_RECV_PDU;
793 ev->r.recv_req.cid = conn->cid;
794 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
795 pdu = (char*)ev + sizeof(*ev);
796 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
797 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
799 return iscsi_unicast_skb(skb, priv->daemon_pid);
801 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
803 void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
805 struct iscsi_cls_session *session = iscsi_conn_to_session(conn);
806 struct nlmsghdr *nlh;
808 struct iscsi_uevent *ev;
809 struct iscsi_internal *priv;
810 int len = NLMSG_SPACE(sizeof(*ev));
813 priv = iscsi_if_transport_lookup(conn->transport);
817 spin_lock_irqsave(&session->lock, flags);
818 if (session->state == ISCSI_SESSION_LOGGED_IN)
819 session->state = ISCSI_SESSION_FAILED;
820 spin_unlock_irqrestore(&session->lock, flags);
822 skb = alloc_skb(len, GFP_ATOMIC);
824 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
825 "conn error (%d)\n", error);
829 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
830 ev = NLMSG_DATA(nlh);
831 ev->transport_handle = iscsi_handle(conn->transport);
832 ev->type = ISCSI_KEVENT_CONN_ERROR;
833 ev->r.connerror.error = error;
834 ev->r.connerror.cid = conn->cid;
835 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
837 iscsi_broadcast_skb(skb, GFP_ATOMIC);
839 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
842 EXPORT_SYMBOL_GPL(iscsi_conn_error);
845 iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
846 void *payload, int size)
849 struct nlmsghdr *nlh;
850 int len = NLMSG_SPACE(size);
851 int flags = multi ? NLM_F_MULTI : 0;
852 int t = done ? NLMSG_DONE : type;
854 skb = alloc_skb(len, GFP_ATOMIC);
856 printk(KERN_ERR "Could not allocate skb to send reply.\n");
860 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
861 nlh->nlmsg_flags = flags;
862 memcpy(NLMSG_DATA(nlh), payload, size);
863 return iscsi_unicast_skb(skb, pid);
867 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
869 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
870 struct iscsi_stats *stats;
871 struct sk_buff *skbstat;
872 struct iscsi_cls_conn *conn;
873 struct nlmsghdr *nlhstat;
874 struct iscsi_uevent *evstat;
875 struct iscsi_internal *priv;
876 int len = NLMSG_SPACE(sizeof(*ev) +
877 sizeof(struct iscsi_stats) +
878 sizeof(struct iscsi_stats_custom) *
879 ISCSI_STATS_CUSTOM_MAX);
882 priv = iscsi_if_transport_lookup(transport);
886 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
893 skbstat = alloc_skb(len, GFP_ATOMIC);
895 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
896 "deliver stats: OOM\n");
900 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
901 (len - sizeof(*nlhstat)), 0);
902 evstat = NLMSG_DATA(nlhstat);
903 memset(evstat, 0, sizeof(*evstat));
904 evstat->transport_handle = iscsi_handle(conn->transport);
905 evstat->type = nlh->nlmsg_type;
906 evstat->u.get_stats.cid =
908 evstat->u.get_stats.sid =
910 stats = (struct iscsi_stats *)
911 ((char*)evstat + sizeof(*evstat));
912 memset(stats, 0, sizeof(*stats));
914 transport->get_stats(conn, stats);
915 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
916 sizeof(struct iscsi_stats) +
917 sizeof(struct iscsi_stats_custom) *
918 stats->custom_length);
919 actual_size -= sizeof(*nlhstat);
920 actual_size = NLMSG_LENGTH(actual_size);
921 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
922 nlhstat->nlmsg_len = actual_size;
924 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
925 } while (err < 0 && err != -ECONNREFUSED);
931 * iscsi_session_event - send session destr. completion event
932 * @session: iscsi class session
933 * @event: type of event
935 int iscsi_session_event(struct iscsi_cls_session *session,
936 enum iscsi_uevent_e event)
938 struct iscsi_internal *priv;
939 struct Scsi_Host *shost;
940 struct iscsi_uevent *ev;
942 struct nlmsghdr *nlh;
943 int rc, len = NLMSG_SPACE(sizeof(*ev));
945 priv = iscsi_if_transport_lookup(session->transport);
948 shost = iscsi_session_to_shost(session);
950 skb = alloc_skb(len, GFP_KERNEL);
952 iscsi_cls_session_printk(KERN_ERR, session,
953 "Cannot notify userspace of session "
954 "event %u\n", event);
958 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
959 ev = NLMSG_DATA(nlh);
960 ev->transport_handle = iscsi_handle(session->transport);
964 case ISCSI_KEVENT_DESTROY_SESSION:
965 ev->r.d_session.host_no = shost->host_no;
966 ev->r.d_session.sid = session->sid;
968 case ISCSI_KEVENT_CREATE_SESSION:
969 ev->r.c_session_ret.host_no = shost->host_no;
970 ev->r.c_session_ret.sid = session->sid;
972 case ISCSI_KEVENT_UNBIND_SESSION:
973 ev->r.unbind_session.host_no = shost->host_no;
974 ev->r.unbind_session.sid = session->sid;
977 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
984 * this will occur if the daemon is not up, so we just warn
985 * the user and when the daemon is restarted it will handle it
987 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
989 iscsi_cls_session_printk(KERN_ERR, session,
990 "Cannot notify userspace of session "
991 "event %u. Check iscsi daemon\n",
995 EXPORT_SYMBOL_GPL(iscsi_session_event);
998 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
1000 struct iscsi_transport *transport = priv->iscsi_transport;
1001 struct iscsi_cls_session *session;
1004 session = transport->create_session(transport, &priv->t,
1005 ev->u.c_session.cmds_max,
1006 ev->u.c_session.queue_depth,
1007 ev->u.c_session.initial_cmdsn,
1012 ev->r.c_session_ret.host_no = hostno;
1013 ev->r.c_session_ret.sid = session->sid;
1018 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1020 struct iscsi_cls_conn *conn;
1021 struct iscsi_cls_session *session;
1023 session = iscsi_session_lookup(ev->u.c_conn.sid);
1025 printk(KERN_ERR "iscsi: invalid session %d.\n",
1030 conn = transport->create_conn(session, ev->u.c_conn.cid);
1032 iscsi_cls_session_printk(KERN_ERR, session,
1033 "couldn't create a new connection.");
1037 ev->r.c_conn_ret.sid = session->sid;
1038 ev->r.c_conn_ret.cid = conn->cid;
1043 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1045 struct iscsi_cls_conn *conn;
1047 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
1051 if (transport->destroy_conn)
1052 transport->destroy_conn(conn);
1057 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1059 char *data = (char*)ev + sizeof(*ev);
1060 struct iscsi_cls_conn *conn;
1061 struct iscsi_cls_session *session;
1062 int err = 0, value = 0;
1064 session = iscsi_session_lookup(ev->u.set_param.sid);
1065 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1066 if (!conn || !session)
1069 switch (ev->u.set_param.param) {
1070 case ISCSI_PARAM_SESS_RECOVERY_TMO:
1071 sscanf(data, "%d", &value);
1073 session->recovery_tmo = value;
1076 err = transport->set_param(conn, ev->u.set_param.param,
1077 data, ev->u.set_param.len);
1084 iscsi_if_transport_ep(struct iscsi_transport *transport,
1085 struct iscsi_uevent *ev, int msg_type)
1087 struct sockaddr *dst_addr;
1091 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1092 if (!transport->ep_connect)
1095 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1096 rc = transport->ep_connect(dst_addr,
1097 ev->u.ep_connect.non_blocking,
1098 &ev->r.ep_connect_ret.handle);
1100 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1101 if (!transport->ep_poll)
1104 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
1105 ev->u.ep_poll.timeout_ms);
1107 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1108 if (!transport->ep_disconnect)
1111 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
1118 iscsi_tgt_dscvr(struct iscsi_transport *transport,
1119 struct iscsi_uevent *ev)
1121 struct Scsi_Host *shost;
1122 struct sockaddr *dst_addr;
1125 if (!transport->tgt_dscvr)
1128 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1129 if (IS_ERR(shost)) {
1130 printk(KERN_ERR "target discovery could not find host no %u\n",
1131 ev->u.tgt_dscvr.host_no);
1136 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1137 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1138 ev->u.tgt_dscvr.enable, dst_addr);
1139 scsi_host_put(shost);
1144 iscsi_set_host_param(struct iscsi_transport *transport,
1145 struct iscsi_uevent *ev)
1147 char *data = (char*)ev + sizeof(*ev);
1148 struct Scsi_Host *shost;
1151 if (!transport->set_host_param)
1154 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1155 if (IS_ERR(shost)) {
1156 printk(KERN_ERR "set_host_param could not find host no %u\n",
1157 ev->u.set_host_param.host_no);
1161 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1162 data, ev->u.set_host_param.len);
1163 scsi_host_put(shost);
1168 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1171 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1172 struct iscsi_transport *transport = NULL;
1173 struct iscsi_internal *priv;
1174 struct iscsi_cls_session *session;
1175 struct iscsi_cls_conn *conn;
1177 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1180 transport = priv->iscsi_transport;
1182 if (!try_module_get(transport->owner))
1185 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1187 switch (nlh->nlmsg_type) {
1188 case ISCSI_UEVENT_CREATE_SESSION:
1189 err = iscsi_if_create_session(priv, ev);
1191 case ISCSI_UEVENT_DESTROY_SESSION:
1192 session = iscsi_session_lookup(ev->u.d_session.sid);
1194 transport->destroy_session(session);
1198 case ISCSI_UEVENT_UNBIND_SESSION:
1199 session = iscsi_session_lookup(ev->u.d_session.sid);
1201 iscsi_unbind_session(session);
1205 case ISCSI_UEVENT_CREATE_CONN:
1206 err = iscsi_if_create_conn(transport, ev);
1208 case ISCSI_UEVENT_DESTROY_CONN:
1209 err = iscsi_if_destroy_conn(transport, ev);
1211 case ISCSI_UEVENT_BIND_CONN:
1212 session = iscsi_session_lookup(ev->u.b_conn.sid);
1213 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1215 if (session && conn)
1216 ev->r.retcode = transport->bind_conn(session, conn,
1217 ev->u.b_conn.transport_eph,
1218 ev->u.b_conn.is_leading);
1222 case ISCSI_UEVENT_SET_PARAM:
1223 err = iscsi_set_param(transport, ev);
1225 case ISCSI_UEVENT_START_CONN:
1226 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
1228 ev->r.retcode = transport->start_conn(conn);
1232 case ISCSI_UEVENT_STOP_CONN:
1233 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
1235 transport->stop_conn(conn, ev->u.stop_conn.flag);
1239 case ISCSI_UEVENT_SEND_PDU:
1240 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
1242 ev->r.retcode = transport->send_pdu(conn,
1243 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1244 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1245 ev->u.send_pdu.data_size);
1249 case ISCSI_UEVENT_GET_STATS:
1250 err = iscsi_if_get_stats(transport, nlh);
1252 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1253 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1254 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1255 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1257 case ISCSI_UEVENT_TGT_DSCVR:
1258 err = iscsi_tgt_dscvr(transport, ev);
1260 case ISCSI_UEVENT_SET_HOST_PARAM:
1261 err = iscsi_set_host_param(transport, ev);
1268 module_put(transport->owner);
1273 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1274 * Malformed skbs with wrong lengths or invalid creds are not processed.
1277 iscsi_if_rx(struct sk_buff *skb)
1279 mutex_lock(&rx_queue_mutex);
1280 while (skb->len >= NLMSG_SPACE(0)) {
1283 struct nlmsghdr *nlh;
1284 struct iscsi_uevent *ev;
1286 nlh = nlmsg_hdr(skb);
1287 if (nlh->nlmsg_len < sizeof(*nlh) ||
1288 skb->len < nlh->nlmsg_len) {
1292 ev = NLMSG_DATA(nlh);
1293 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1294 if (rlen > skb->len)
1297 err = iscsi_if_recv_msg(skb, nlh);
1299 ev->type = ISCSI_KEVENT_IF_ERROR;
1304 * special case for GET_STATS:
1305 * on success - sending reply and stats from
1306 * inside of if_recv_msg(),
1307 * on error - fall through.
1309 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1311 err = iscsi_if_send_reply(
1312 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1313 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1314 } while (err < 0 && err != -ECONNREFUSED);
1315 skb_pull(skb, rlen);
1317 mutex_unlock(&rx_queue_mutex);
1320 #define iscsi_cdev_to_conn(_cdev) \
1321 iscsi_dev_to_conn(_cdev->dev)
1323 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1324 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1325 __ATTR(_name,_mode,_show,_store)
1328 * iSCSI connection attrs
1330 #define iscsi_conn_attr_show(param) \
1332 show_conn_param_##param(struct class_device *cdev, char *buf) \
1334 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1335 struct iscsi_transport *t = conn->transport; \
1336 return t->get_conn_param(conn, param, buf); \
1339 #define iscsi_conn_attr(field, param) \
1340 iscsi_conn_attr_show(param) \
1341 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
1344 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1345 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1346 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1347 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1348 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1349 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1350 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1351 iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1352 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1353 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1354 iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
1355 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1356 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
1358 #define iscsi_cdev_to_session(_cdev) \
1359 iscsi_dev_to_session(_cdev->dev)
1362 * iSCSI session attrs
1364 #define iscsi_session_attr_show(param, perm) \
1366 show_session_param_##param(struct class_device *cdev, char *buf) \
1368 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1369 struct iscsi_transport *t = session->transport; \
1371 if (perm && !capable(CAP_SYS_ADMIN)) \
1373 return t->get_session_param(session, param, buf); \
1376 #define iscsi_session_attr(field, param, perm) \
1377 iscsi_session_attr_show(param, perm) \
1378 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
1381 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1382 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1383 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1384 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1385 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1386 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1387 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1388 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1389 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1390 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1391 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1392 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1393 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1394 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
1395 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1396 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1397 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
1400 show_priv_session_state(struct class_device *cdev, char *buf)
1402 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);
1403 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1405 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1408 #define iscsi_priv_session_attr_show(field, format) \
1410 show_priv_session_##field(struct class_device *cdev, char *buf) \
1412 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);\
1413 return sprintf(buf, format"\n", session->field); \
1416 #define iscsi_priv_session_attr(field, format) \
1417 iscsi_priv_session_attr_show(field, format) \
1418 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1420 iscsi_priv_session_attr(recovery_tmo, "%d");
1425 #define iscsi_host_attr_show(param) \
1427 show_host_param_##param(struct class_device *cdev, char *buf) \
1429 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1430 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1431 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1434 #define iscsi_host_attr(field, param) \
1435 iscsi_host_attr_show(param) \
1436 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1439 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
1440 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
1441 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
1442 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
1444 #define SETUP_PRIV_SESSION_RD_ATTR(field) \
1446 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1451 #define SETUP_SESSION_RD_ATTR(field, param_flag) \
1453 if (tt->param_mask & param_flag) { \
1454 priv->session_attrs[count] = &class_device_attr_sess_##field; \
1459 #define SETUP_CONN_RD_ATTR(field, param_flag) \
1461 if (tt->param_mask & param_flag) { \
1462 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1467 #define SETUP_HOST_RD_ATTR(field, param_flag) \
1469 if (tt->host_param_mask & param_flag) { \
1470 priv->host_attrs[count] = &class_device_attr_host_##field; \
1475 static int iscsi_session_match(struct attribute_container *cont,
1478 struct iscsi_cls_session *session;
1479 struct Scsi_Host *shost;
1480 struct iscsi_internal *priv;
1482 if (!iscsi_is_session_dev(dev))
1485 session = iscsi_dev_to_session(dev);
1486 shost = iscsi_session_to_shost(session);
1487 if (!shost->transportt)
1490 priv = to_iscsi_internal(shost->transportt);
1491 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1494 return &priv->session_cont.ac == cont;
1497 static int iscsi_conn_match(struct attribute_container *cont,
1500 struct iscsi_cls_session *session;
1501 struct iscsi_cls_conn *conn;
1502 struct Scsi_Host *shost;
1503 struct iscsi_internal *priv;
1505 if (!iscsi_is_conn_dev(dev))
1508 conn = iscsi_dev_to_conn(dev);
1509 session = iscsi_dev_to_session(conn->dev.parent);
1510 shost = iscsi_session_to_shost(session);
1512 if (!shost->transportt)
1515 priv = to_iscsi_internal(shost->transportt);
1516 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1519 return &priv->conn_cont.ac == cont;
1522 static int iscsi_host_match(struct attribute_container *cont,
1525 struct Scsi_Host *shost;
1526 struct iscsi_internal *priv;
1528 if (!scsi_is_host_device(dev))
1531 shost = dev_to_shost(dev);
1532 if (!shost->transportt ||
1533 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1536 priv = to_iscsi_internal(shost->transportt);
1537 return &priv->t.host_attrs.ac == cont;
1540 struct scsi_transport_template *
1541 iscsi_register_transport(struct iscsi_transport *tt)
1543 struct iscsi_internal *priv;
1544 unsigned long flags;
1549 priv = iscsi_if_transport_lookup(tt);
1553 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1556 INIT_LIST_HEAD(&priv->list);
1557 priv->daemon_pid = -1;
1558 priv->iscsi_transport = tt;
1559 priv->t.user_scan = iscsi_user_scan;
1561 priv->cdev.class = &iscsi_transport_class;
1562 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1563 err = class_device_register(&priv->cdev);
1567 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1569 goto unregister_cdev;
1571 /* host parameters */
1572 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1573 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1574 priv->t.host_attrs.ac.match = iscsi_host_match;
1575 priv->t.host_size = sizeof(struct iscsi_host);
1576 transport_container_register(&priv->t.host_attrs);
1578 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
1579 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
1580 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
1581 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
1582 BUG_ON(count > ISCSI_HOST_ATTRS);
1583 priv->host_attrs[count] = NULL;
1586 /* connection parameters */
1587 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1588 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1589 priv->conn_cont.ac.match = iscsi_conn_match;
1590 transport_container_register(&priv->conn_cont);
1592 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1593 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1594 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1595 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1596 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1597 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1598 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1599 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
1600 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
1601 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1602 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
1603 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1604 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
1606 BUG_ON(count > ISCSI_CONN_ATTRS);
1607 priv->conn_attrs[count] = NULL;
1610 /* session parameters */
1611 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1612 priv->session_cont.ac.class = &iscsi_session_class.class;
1613 priv->session_cont.ac.match = iscsi_session_match;
1614 transport_container_register(&priv->session_cont);
1616 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1617 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1618 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1619 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1620 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1621 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1622 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1623 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
1624 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1625 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
1626 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1627 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1628 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1629 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
1630 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
1631 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1632 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
1633 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
1634 SETUP_PRIV_SESSION_RD_ATTR(state);
1636 BUG_ON(count > ISCSI_SESSION_ATTRS);
1637 priv->session_attrs[count] = NULL;
1639 spin_lock_irqsave(&iscsi_transport_lock, flags);
1640 list_add(&priv->list, &iscsi_transports);
1641 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1643 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1647 class_device_unregister(&priv->cdev);
1652 EXPORT_SYMBOL_GPL(iscsi_register_transport);
1654 int iscsi_unregister_transport(struct iscsi_transport *tt)
1656 struct iscsi_internal *priv;
1657 unsigned long flags;
1661 mutex_lock(&rx_queue_mutex);
1663 priv = iscsi_if_transport_lookup(tt);
1666 spin_lock_irqsave(&iscsi_transport_lock, flags);
1667 list_del(&priv->list);
1668 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1670 transport_container_unregister(&priv->conn_cont);
1671 transport_container_unregister(&priv->session_cont);
1672 transport_container_unregister(&priv->t.host_attrs);
1674 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1675 class_device_unregister(&priv->cdev);
1676 mutex_unlock(&rx_queue_mutex);
1680 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1682 static __init int iscsi_transport_init(void)
1686 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
1687 ISCSI_TRANSPORT_VERSION);
1689 atomic_set(&iscsi_session_nr, 0);
1691 err = class_register(&iscsi_transport_class);
1695 err = transport_class_register(&iscsi_host_class);
1697 goto unregister_transport_class;
1699 err = transport_class_register(&iscsi_connection_class);
1701 goto unregister_host_class;
1703 err = transport_class_register(&iscsi_session_class);
1705 goto unregister_conn_class;
1707 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx, NULL,
1711 goto unregister_session_class;
1714 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1715 if (!iscsi_eh_timer_workq)
1721 netlink_kernel_release(nls);
1722 unregister_session_class:
1723 transport_class_unregister(&iscsi_session_class);
1724 unregister_conn_class:
1725 transport_class_unregister(&iscsi_connection_class);
1726 unregister_host_class:
1727 transport_class_unregister(&iscsi_host_class);
1728 unregister_transport_class:
1729 class_unregister(&iscsi_transport_class);
1733 static void __exit iscsi_transport_exit(void)
1735 destroy_workqueue(iscsi_eh_timer_workq);
1736 netlink_kernel_release(nls);
1737 transport_class_unregister(&iscsi_connection_class);
1738 transport_class_unregister(&iscsi_session_class);
1739 transport_class_unregister(&iscsi_host_class);
1740 class_unregister(&iscsi_transport_class);
1743 module_init(iscsi_transport_init);
1744 module_exit(iscsi_transport_exit);
1746 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1747 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1748 "Alex Aizman <itn780@yahoo.com>");
1749 MODULE_DESCRIPTION("iSCSI Transport Interface");
1750 MODULE_LICENSE("GPL");
1751 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);