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/mempool.h>
25 #include <linux/mutex.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_transport.h>
31 #include <scsi/scsi_transport_iscsi.h>
32 #include <scsi/iscsi_if.h>
34 #define ISCSI_SESSION_ATTRS 11
35 #define ISCSI_CONN_ATTRS 11
36 #define ISCSI_HOST_ATTRS 0
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 int iscsi_session_nr; /* sysfs session id for next new session */
55 * list of registered transports and lock that must
56 * be held while accessing list. The iscsi_transport_lock must
57 * be acquired after the rx_queue_mutex.
59 static LIST_HEAD(iscsi_transports);
60 static DEFINE_SPINLOCK(iscsi_transport_lock);
62 #define to_iscsi_internal(tmpl) \
63 container_of(tmpl, struct iscsi_internal, t)
65 #define cdev_to_iscsi_internal(_cdev) \
66 container_of(_cdev, struct iscsi_internal, cdev)
68 static void iscsi_transport_release(struct class_device *cdev)
70 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
75 * iscsi_transport_class represents the iscsi_transports that are
78 static struct class iscsi_transport_class = {
79 .name = "iscsi_transport",
80 .release = iscsi_transport_release,
84 show_transport_handle(struct class_device *cdev, char *buf)
86 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
87 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
89 static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
91 #define show_transport_attr(name, format) \
93 show_transport_##name(struct class_device *cdev, char *buf) \
95 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
96 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
98 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
100 show_transport_attr(caps, "0x%x");
101 show_transport_attr(max_lun, "%d");
102 show_transport_attr(max_conn, "%d");
103 show_transport_attr(max_cmd_len, "%d");
105 static struct attribute *iscsi_transport_attrs[] = {
106 &class_device_attr_handle.attr,
107 &class_device_attr_caps.attr,
108 &class_device_attr_max_lun.attr,
109 &class_device_attr_max_conn.attr,
110 &class_device_attr_max_cmd_len.attr,
114 static struct attribute_group iscsi_transport_group = {
115 .attrs = iscsi_transport_attrs,
118 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
119 struct class_device *cdev)
121 struct Scsi_Host *shost = dev_to_shost(dev);
122 struct iscsi_host *ihost = shost->shost_data;
124 memset(ihost, 0, sizeof(*ihost));
125 INIT_LIST_HEAD(&ihost->sessions);
126 mutex_init(&ihost->mutex);
130 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
136 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
142 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
148 static struct sock *nls;
149 static DEFINE_MUTEX(rx_queue_mutex);
151 struct mempool_zone {
156 struct list_head freequeue;
160 static struct mempool_zone *z_reply;
163 * Z_MAX_* - actual mempool size allocated at the mempool_zone_init() time
164 * Z_HIWAT_* - zone's high watermark when if_error bit will be set to -ENOMEM
165 * so daemon will notice OOM on NETLINK tranposrt level and will
166 * be able to predict or change operational behavior
168 #define Z_MAX_REPLY 8
169 #define Z_HIWAT_REPLY 6
171 #define Z_HIWAT_PDU 6
172 #define Z_MAX_ERROR 16
173 #define Z_HIWAT_ERROR 12
175 static LIST_HEAD(sesslist);
176 static DEFINE_SPINLOCK(sesslock);
177 static LIST_HEAD(connlist);
178 static DEFINE_SPINLOCK(connlock);
180 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
182 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
187 * Returns the matching session to a given sid
189 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
192 struct iscsi_cls_session *sess;
194 spin_lock_irqsave(&sesslock, flags);
195 list_for_each_entry(sess, &sesslist, sess_list) {
196 if (sess->sid == sid) {
197 spin_unlock_irqrestore(&sesslock, flags);
201 spin_unlock_irqrestore(&sesslock, flags);
206 * Returns the matching connection to a given sid / cid tuple
208 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
211 struct iscsi_cls_conn *conn;
213 spin_lock_irqsave(&connlock, flags);
214 list_for_each_entry(conn, &connlist, conn_list) {
215 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
216 spin_unlock_irqrestore(&connlock, flags);
220 spin_unlock_irqrestore(&connlock, flags);
225 * The following functions can be used by LLDs that allocate
226 * their own scsi_hosts or by software iscsi LLDs
228 static void iscsi_session_release(struct device *dev)
230 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
231 struct Scsi_Host *shost;
233 shost = iscsi_session_to_shost(session);
234 scsi_host_put(shost);
238 static int iscsi_is_session_dev(const struct device *dev)
240 return dev->release == iscsi_session_release;
243 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
246 struct iscsi_host *ihost = shost->shost_data;
247 struct iscsi_cls_session *session;
249 mutex_lock(&ihost->mutex);
250 list_for_each_entry(session, &ihost->sessions, host_list) {
251 if ((channel == SCAN_WILD_CARD || channel == 0) &&
252 (id == SCAN_WILD_CARD || id == session->target_id))
253 scsi_scan_target(&session->dev, 0,
254 session->target_id, lun, 1);
256 mutex_unlock(&ihost->mutex);
261 static void session_recovery_timedout(void *data)
263 struct iscsi_cls_session *session = data;
265 dev_printk(KERN_INFO, &session->dev, "iscsi: session recovery timed "
266 "out after %d secs\n", session->recovery_tmo);
268 if (session->transport->session_recovery_timedout)
269 session->transport->session_recovery_timedout(session);
271 scsi_target_unblock(&session->dev);
274 void iscsi_unblock_session(struct iscsi_cls_session *session)
276 if (!cancel_delayed_work(&session->recovery_work))
277 flush_scheduled_work();
278 scsi_target_unblock(&session->dev);
280 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
282 void iscsi_block_session(struct iscsi_cls_session *session)
284 scsi_target_block(&session->dev);
285 schedule_delayed_work(&session->recovery_work,
286 session->recovery_tmo * HZ);
288 EXPORT_SYMBOL_GPL(iscsi_block_session);
290 struct iscsi_cls_session *
291 iscsi_alloc_session(struct Scsi_Host *shost,
292 struct iscsi_transport *transport)
294 struct iscsi_cls_session *session;
296 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
301 session->transport = transport;
302 session->recovery_tmo = 120;
303 INIT_WORK(&session->recovery_work, session_recovery_timedout, session);
304 INIT_LIST_HEAD(&session->host_list);
305 INIT_LIST_HEAD(&session->sess_list);
307 /* this is released in the dev's release function */
308 scsi_host_get(shost);
309 session->dev.parent = &shost->shost_gendev;
310 session->dev.release = iscsi_session_release;
311 device_initialize(&session->dev);
312 if (transport->sessiondata_size)
313 session->dd_data = &session[1];
316 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
318 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
320 struct Scsi_Host *shost = iscsi_session_to_shost(session);
321 struct iscsi_host *ihost;
324 ihost = shost->shost_data;
325 session->sid = iscsi_session_nr++;
326 session->target_id = target_id;
328 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
330 err = device_add(&session->dev);
332 dev_printk(KERN_ERR, &session->dev, "iscsi: could not "
333 "register session's dev\n");
336 transport_register_device(&session->dev);
338 mutex_lock(&ihost->mutex);
339 list_add(&session->host_list, &ihost->sessions);
340 mutex_unlock(&ihost->mutex);
344 scsi_host_put(shost);
347 EXPORT_SYMBOL_GPL(iscsi_add_session);
350 * iscsi_create_session - create iscsi class session
352 * @transport: iscsi transport
354 * This can be called from a LLD or iscsi_transport.
356 struct iscsi_cls_session *
357 iscsi_create_session(struct Scsi_Host *shost,
358 struct iscsi_transport *transport,
359 unsigned int target_id)
361 struct iscsi_cls_session *session;
363 session = iscsi_alloc_session(shost, transport);
367 if (iscsi_add_session(session, target_id)) {
368 iscsi_free_session(session);
373 EXPORT_SYMBOL_GPL(iscsi_create_session);
375 void iscsi_remove_session(struct iscsi_cls_session *session)
377 struct Scsi_Host *shost = iscsi_session_to_shost(session);
378 struct iscsi_host *ihost = shost->shost_data;
380 if (!cancel_delayed_work(&session->recovery_work))
381 flush_scheduled_work();
383 mutex_lock(&ihost->mutex);
384 list_del(&session->host_list);
385 mutex_unlock(&ihost->mutex);
387 scsi_remove_target(&session->dev);
389 transport_unregister_device(&session->dev);
390 device_del(&session->dev);
392 EXPORT_SYMBOL_GPL(iscsi_remove_session);
394 void iscsi_free_session(struct iscsi_cls_session *session)
396 put_device(&session->dev);
399 EXPORT_SYMBOL_GPL(iscsi_free_session);
402 * iscsi_destroy_session - destroy iscsi session
403 * @session: iscsi_session
405 * Can be called by a LLD or iscsi_transport. There must not be
406 * any running connections.
408 int iscsi_destroy_session(struct iscsi_cls_session *session)
410 iscsi_remove_session(session);
411 iscsi_free_session(session);
414 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
416 static void mempool_zone_destroy(struct mempool_zone *zp)
418 mempool_destroy(zp->pool);
423 mempool_zone_alloc_skb(gfp_t gfp_mask, void *pool_data)
425 struct mempool_zone *zone = pool_data;
427 return alloc_skb(zone->size, gfp_mask);
431 mempool_zone_free_skb(void *element, void *pool_data)
436 static struct mempool_zone *
437 mempool_zone_init(unsigned max, unsigned size, unsigned hiwat)
439 struct mempool_zone *zp;
441 zp = kzalloc(sizeof(*zp), GFP_KERNEL);
447 INIT_LIST_HEAD(&zp->freequeue);
448 spin_lock_init(&zp->freelock);
449 atomic_set(&zp->allocated, 0);
451 zp->pool = mempool_create(max, mempool_zone_alloc_skb,
452 mempool_zone_free_skb, zp);
461 static void iscsi_conn_release(struct device *dev)
463 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
464 struct device *parent = conn->dev.parent;
466 mempool_zone_destroy(conn->z_pdu);
467 mempool_zone_destroy(conn->z_error);
473 static int iscsi_is_conn_dev(const struct device *dev)
475 return dev->release == iscsi_conn_release;
478 static int iscsi_create_event_pools(struct iscsi_cls_conn *conn)
480 conn->z_pdu = mempool_zone_init(Z_MAX_PDU,
481 NLMSG_SPACE(sizeof(struct iscsi_uevent) +
482 sizeof(struct iscsi_hdr) +
483 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH),
486 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
487 "pdu zone for new conn\n");
491 conn->z_error = mempool_zone_init(Z_MAX_ERROR,
492 NLMSG_SPACE(sizeof(struct iscsi_uevent)),
494 if (!conn->z_error) {
495 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
496 "error zone for new conn\n");
497 mempool_zone_destroy(conn->z_pdu);
504 * iscsi_create_conn - create iscsi class connection
505 * @session: iscsi cls session
506 * @cid: connection id
508 * This can be called from a LLD or iscsi_transport. The connection
509 * is child of the session so cid must be unique for all connections
512 * Since we do not support MCS, cid will normally be zero. In some cases
513 * for software iscsi we could be trying to preallocate a connection struct
514 * in which case there could be two connection structs and cid would be
517 struct iscsi_cls_conn *
518 iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
520 struct iscsi_transport *transport = session->transport;
521 struct iscsi_cls_conn *conn;
524 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
528 if (transport->conndata_size)
529 conn->dd_data = &conn[1];
531 INIT_LIST_HEAD(&conn->conn_list);
532 conn->transport = transport;
535 if (iscsi_create_event_pools(conn))
538 /* this is released in the dev's release function */
539 if (!get_device(&session->dev))
540 goto free_conn_pools;
542 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
544 conn->dev.parent = &session->dev;
545 conn->dev.release = iscsi_conn_release;
546 err = device_register(&conn->dev);
548 dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register "
549 "connection's dev\n");
550 goto release_parent_ref;
552 transport_register_device(&conn->dev);
556 put_device(&session->dev);
564 EXPORT_SYMBOL_GPL(iscsi_create_conn);
567 * iscsi_destroy_conn - destroy iscsi class connection
568 * @session: iscsi cls session
570 * This can be called from a LLD or iscsi_transport.
572 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
574 transport_unregister_device(&conn->dev);
575 device_unregister(&conn->dev);
579 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
582 * iscsi interface functions
584 static struct iscsi_internal *
585 iscsi_if_transport_lookup(struct iscsi_transport *tt)
587 struct iscsi_internal *priv;
590 spin_lock_irqsave(&iscsi_transport_lock, flags);
591 list_for_each_entry(priv, &iscsi_transports, list) {
592 if (tt == priv->iscsi_transport) {
593 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
597 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
601 static inline struct list_head *skb_to_lh(struct sk_buff *skb)
603 return (struct list_head *)&skb->cb;
607 mempool_zone_complete(struct mempool_zone *zone)
610 struct list_head *lh, *n;
612 spin_lock_irqsave(&zone->freelock, flags);
613 list_for_each_safe(lh, n, &zone->freequeue) {
614 struct sk_buff *skb = (struct sk_buff *)((char *)lh -
615 offsetof(struct sk_buff, cb));
616 if (!skb_shared(skb)) {
617 list_del(skb_to_lh(skb));
618 mempool_free(skb, zone->pool);
619 atomic_dec(&zone->allocated);
622 spin_unlock_irqrestore(&zone->freelock, flags);
625 static struct sk_buff*
626 mempool_zone_get_skb(struct mempool_zone *zone)
630 skb = mempool_alloc(zone->pool, GFP_ATOMIC);
632 atomic_inc(&zone->allocated);
637 iscsi_broadcast_skb(struct mempool_zone *zone, struct sk_buff *skb)
643 rc = netlink_broadcast(nls, skb, 0, 1, GFP_KERNEL);
645 mempool_free(skb, zone->pool);
646 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
650 spin_lock_irqsave(&zone->freelock, flags);
651 INIT_LIST_HEAD(skb_to_lh(skb));
652 list_add(skb_to_lh(skb), &zone->freequeue);
653 spin_unlock_irqrestore(&zone->freelock, flags);
658 iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb, int pid)
664 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
666 mempool_free(skb, zone->pool);
667 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
671 spin_lock_irqsave(&zone->freelock, flags);
672 INIT_LIST_HEAD(skb_to_lh(skb));
673 list_add(skb_to_lh(skb), &zone->freequeue);
674 spin_unlock_irqrestore(&zone->freelock, flags);
679 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
680 char *data, uint32_t data_size)
682 struct nlmsghdr *nlh;
684 struct iscsi_uevent *ev;
686 struct iscsi_internal *priv;
687 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
690 priv = iscsi_if_transport_lookup(conn->transport);
694 mempool_zone_complete(conn->z_pdu);
696 skb = mempool_zone_get_skb(conn->z_pdu);
698 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
699 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver "
700 "control PDU: OOM\n");
704 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
705 ev = NLMSG_DATA(nlh);
706 memset(ev, 0, sizeof(*ev));
707 ev->transport_handle = iscsi_handle(conn->transport);
708 ev->type = ISCSI_KEVENT_RECV_PDU;
709 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
710 ev->iferror = -ENOMEM;
711 ev->r.recv_req.cid = conn->cid;
712 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
713 pdu = (char*)ev + sizeof(*ev);
714 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
715 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
717 return iscsi_unicast_skb(conn->z_pdu, skb, priv->daemon_pid);
719 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
721 void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
723 struct nlmsghdr *nlh;
725 struct iscsi_uevent *ev;
726 struct iscsi_internal *priv;
727 int len = NLMSG_SPACE(sizeof(*ev));
729 priv = iscsi_if_transport_lookup(conn->transport);
733 mempool_zone_complete(conn->z_error);
735 skb = mempool_zone_get_skb(conn->z_error);
737 dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored "
738 "conn error (%d)\n", error);
742 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
743 ev = NLMSG_DATA(nlh);
744 ev->transport_handle = iscsi_handle(conn->transport);
745 ev->type = ISCSI_KEVENT_CONN_ERROR;
746 if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat)
747 ev->iferror = -ENOMEM;
748 ev->r.connerror.error = error;
749 ev->r.connerror.cid = conn->cid;
750 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
752 iscsi_broadcast_skb(conn->z_error, skb);
754 dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n",
757 EXPORT_SYMBOL_GPL(iscsi_conn_error);
760 iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
761 void *payload, int size)
764 struct nlmsghdr *nlh;
765 int len = NLMSG_SPACE(size);
766 int flags = multi ? NLM_F_MULTI : 0;
767 int t = done ? NLMSG_DONE : type;
769 mempool_zone_complete(z_reply);
771 skb = mempool_zone_get_skb(z_reply);
774 * user is supposed to react on iferror == -ENOMEM;
779 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
780 nlh->nlmsg_flags = flags;
781 memcpy(NLMSG_DATA(nlh), payload, size);
782 return iscsi_unicast_skb(z_reply, skb, pid);
786 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
788 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
789 struct iscsi_stats *stats;
790 struct sk_buff *skbstat;
791 struct iscsi_cls_conn *conn;
792 struct nlmsghdr *nlhstat;
793 struct iscsi_uevent *evstat;
794 struct iscsi_internal *priv;
795 int len = NLMSG_SPACE(sizeof(*ev) +
796 sizeof(struct iscsi_stats) +
797 sizeof(struct iscsi_stats_custom) *
798 ISCSI_STATS_CUSTOM_MAX);
801 priv = iscsi_if_transport_lookup(transport);
805 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
812 mempool_zone_complete(conn->z_pdu);
814 skbstat = mempool_zone_get_skb(conn->z_pdu);
816 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not "
817 "deliver stats: OOM\n");
821 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
822 (len - sizeof(*nlhstat)), 0);
823 evstat = NLMSG_DATA(nlhstat);
824 memset(evstat, 0, sizeof(*evstat));
825 evstat->transport_handle = iscsi_handle(conn->transport);
826 evstat->type = nlh->nlmsg_type;
827 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
828 evstat->iferror = -ENOMEM;
829 evstat->u.get_stats.cid =
831 evstat->u.get_stats.sid =
833 stats = (struct iscsi_stats *)
834 ((char*)evstat + sizeof(*evstat));
835 memset(stats, 0, sizeof(*stats));
837 transport->get_stats(conn, stats);
838 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
839 sizeof(struct iscsi_stats) +
840 sizeof(struct iscsi_stats_custom) *
841 stats->custom_length);
842 actual_size -= sizeof(*nlhstat);
843 actual_size = NLMSG_LENGTH(actual_size);
844 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
845 nlhstat->nlmsg_len = actual_size;
847 err = iscsi_unicast_skb(conn->z_pdu, skbstat, priv->daemon_pid);
848 } while (err < 0 && err != -ECONNREFUSED);
854 * iscsi_if_destroy_session_done - send session destr. completion event
855 * @conn: last connection for session
857 * This is called by HW iscsi LLDs to notify userpsace that its HW has
860 int iscsi_if_destroy_session_done(struct iscsi_cls_conn *conn)
862 struct iscsi_internal *priv;
863 struct iscsi_cls_session *session;
864 struct Scsi_Host *shost;
865 struct iscsi_uevent *ev;
867 struct nlmsghdr *nlh;
869 int rc, len = NLMSG_SPACE(sizeof(*ev));
871 priv = iscsi_if_transport_lookup(conn->transport);
875 session = iscsi_dev_to_session(conn->dev.parent);
876 shost = iscsi_session_to_shost(session);
878 mempool_zone_complete(conn->z_pdu);
880 skb = mempool_zone_get_skb(conn->z_pdu);
882 dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of "
883 "session creation event\n");
887 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
888 ev = NLMSG_DATA(nlh);
889 ev->transport_handle = iscsi_handle(conn->transport);
890 ev->type = ISCSI_KEVENT_DESTROY_SESSION;
891 ev->r.d_session.host_no = shost->host_no;
892 ev->r.d_session.sid = session->sid;
895 * this will occur if the daemon is not up, so we just warn
896 * the user and when the daemon is restarted it will handle it
898 rc = iscsi_broadcast_skb(conn->z_pdu, skb);
900 dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of "
901 "session destruction event. Check iscsi daemon\n");
903 spin_lock_irqsave(&sesslock, flags);
904 list_del(&session->sess_list);
905 spin_unlock_irqrestore(&sesslock, flags);
907 spin_lock_irqsave(&connlock, flags);
909 list_del(&conn->conn_list);
910 spin_unlock_irqrestore(&connlock, flags);
914 EXPORT_SYMBOL_GPL(iscsi_if_destroy_session_done);
917 * iscsi_if_create_session_done - send session creation completion event
918 * @conn: leading connection for session
920 * This is called by HW iscsi LLDs to notify userpsace that its HW has
921 * created a session or a existing session is back in the logged in state.
923 int iscsi_if_create_session_done(struct iscsi_cls_conn *conn)
925 struct iscsi_internal *priv;
926 struct iscsi_cls_session *session;
927 struct Scsi_Host *shost;
928 struct iscsi_uevent *ev;
930 struct nlmsghdr *nlh;
932 int rc, len = NLMSG_SPACE(sizeof(*ev));
934 priv = iscsi_if_transport_lookup(conn->transport);
938 session = iscsi_dev_to_session(conn->dev.parent);
939 shost = iscsi_session_to_shost(session);
941 mempool_zone_complete(conn->z_pdu);
943 skb = mempool_zone_get_skb(conn->z_pdu);
945 dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of "
946 "session creation event\n");
950 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
951 ev = NLMSG_DATA(nlh);
952 ev->transport_handle = iscsi_handle(conn->transport);
953 ev->type = ISCSI_UEVENT_CREATE_SESSION;
954 ev->r.c_session_ret.host_no = shost->host_no;
955 ev->r.c_session_ret.sid = session->sid;
958 * this will occur if the daemon is not up, so we just warn
959 * the user and when the daemon is restarted it will handle it
961 rc = iscsi_broadcast_skb(conn->z_pdu, skb);
963 dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of "
964 "session creation event. Check iscsi daemon\n");
966 spin_lock_irqsave(&sesslock, flags);
967 list_add(&session->sess_list, &sesslist);
968 spin_unlock_irqrestore(&sesslock, flags);
970 spin_lock_irqsave(&connlock, flags);
971 list_add(&conn->conn_list, &connlist);
973 spin_unlock_irqrestore(&connlock, flags);
976 EXPORT_SYMBOL_GPL(iscsi_if_create_session_done);
979 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
981 struct iscsi_transport *transport = priv->iscsi_transport;
982 struct iscsi_cls_session *session;
986 session = transport->create_session(transport, &priv->t,
987 ev->u.c_session.initial_cmdsn,
992 spin_lock_irqsave(&sesslock, flags);
993 list_add(&session->sess_list, &sesslist);
994 spin_unlock_irqrestore(&sesslock, flags);
996 ev->r.c_session_ret.host_no = hostno;
997 ev->r.c_session_ret.sid = session->sid;
1002 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1004 struct iscsi_cls_conn *conn;
1005 struct iscsi_cls_session *session;
1006 unsigned long flags;
1008 session = iscsi_session_lookup(ev->u.c_conn.sid);
1010 printk(KERN_ERR "iscsi: invalid session %d\n",
1015 conn = transport->create_conn(session, ev->u.c_conn.cid);
1017 printk(KERN_ERR "iscsi: couldn't create a new "
1018 "connection for session %d\n",
1023 ev->r.c_conn_ret.sid = session->sid;
1024 ev->r.c_conn_ret.cid = conn->cid;
1026 spin_lock_irqsave(&connlock, flags);
1027 list_add(&conn->conn_list, &connlist);
1029 spin_unlock_irqrestore(&connlock, flags);
1035 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1037 unsigned long flags;
1038 struct iscsi_cls_conn *conn;
1040 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
1043 spin_lock_irqsave(&connlock, flags);
1045 list_del(&conn->conn_list);
1046 spin_unlock_irqrestore(&connlock, flags);
1048 if (transport->destroy_conn)
1049 transport->destroy_conn(conn);
1054 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1056 char *data = (char*)ev + sizeof(*ev);
1057 struct iscsi_cls_conn *conn;
1058 struct iscsi_cls_session *session;
1059 int err = 0, value = 0;
1061 session = iscsi_session_lookup(ev->u.set_param.sid);
1062 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1063 if (!conn || !session)
1066 switch (ev->u.set_param.param) {
1067 case ISCSI_PARAM_SESS_RECOVERY_TMO:
1068 sscanf(data, "%d", &value);
1070 session->recovery_tmo = value;
1073 err = transport->set_param(conn, ev->u.set_param.param,
1074 data, ev->u.set_param.len);
1081 iscsi_if_transport_ep(struct iscsi_transport *transport,
1082 struct iscsi_uevent *ev, int msg_type)
1084 struct sockaddr *dst_addr;
1088 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1089 if (!transport->ep_connect)
1092 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1093 rc = transport->ep_connect(dst_addr,
1094 ev->u.ep_connect.non_blocking,
1095 &ev->r.ep_connect_ret.handle);
1097 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1098 if (!transport->ep_poll)
1101 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
1102 ev->u.ep_poll.timeout_ms);
1104 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1105 if (!transport->ep_disconnect)
1108 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
1115 iscsi_tgt_dscvr(struct iscsi_transport *transport,
1116 struct iscsi_uevent *ev)
1118 struct sockaddr *dst_addr;
1120 if (!transport->tgt_dscvr)
1123 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1124 return transport->tgt_dscvr(ev->u.tgt_dscvr.type,
1125 ev->u.tgt_dscvr.host_no,
1126 ev->u.tgt_dscvr.enable, dst_addr);
1130 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1133 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1134 struct iscsi_transport *transport = NULL;
1135 struct iscsi_internal *priv;
1136 struct iscsi_cls_session *session;
1137 struct iscsi_cls_conn *conn;
1138 unsigned long flags;
1140 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1143 transport = priv->iscsi_transport;
1145 if (!try_module_get(transport->owner))
1148 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1150 switch (nlh->nlmsg_type) {
1151 case ISCSI_UEVENT_CREATE_SESSION:
1152 err = iscsi_if_create_session(priv, ev);
1154 case ISCSI_UEVENT_DESTROY_SESSION:
1155 session = iscsi_session_lookup(ev->u.d_session.sid);
1157 spin_lock_irqsave(&sesslock, flags);
1158 list_del(&session->sess_list);
1159 spin_unlock_irqrestore(&sesslock, flags);
1161 transport->destroy_session(session);
1165 case ISCSI_UEVENT_CREATE_CONN:
1166 err = iscsi_if_create_conn(transport, ev);
1168 case ISCSI_UEVENT_DESTROY_CONN:
1169 err = iscsi_if_destroy_conn(transport, ev);
1171 case ISCSI_UEVENT_BIND_CONN:
1172 session = iscsi_session_lookup(ev->u.b_conn.sid);
1173 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1175 if (session && conn)
1176 ev->r.retcode = transport->bind_conn(session, conn,
1177 ev->u.b_conn.transport_eph,
1178 ev->u.b_conn.is_leading);
1182 case ISCSI_UEVENT_SET_PARAM:
1183 err = iscsi_set_param(transport, ev);
1185 case ISCSI_UEVENT_START_CONN:
1186 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
1188 ev->r.retcode = transport->start_conn(conn);
1192 case ISCSI_UEVENT_STOP_CONN:
1193 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
1195 transport->stop_conn(conn, ev->u.stop_conn.flag);
1199 case ISCSI_UEVENT_SEND_PDU:
1200 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
1202 ev->r.retcode = transport->send_pdu(conn,
1203 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1204 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1205 ev->u.send_pdu.data_size);
1209 case ISCSI_UEVENT_GET_STATS:
1210 err = iscsi_if_get_stats(transport, nlh);
1212 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1213 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1214 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1215 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1217 case ISCSI_UEVENT_TGT_DSCVR:
1218 err = iscsi_tgt_dscvr(transport, ev);
1225 module_put(transport->owner);
1230 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
1231 * processed by iscsi_if_recv_msg. Malformed skbs with wrong lengths or
1232 * invalid creds are discarded silently.
1235 iscsi_if_rx(struct sock *sk, int len)
1237 struct sk_buff *skb;
1239 mutex_lock(&rx_queue_mutex);
1240 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
1241 if (NETLINK_CREDS(skb)->uid) {
1242 skb_pull(skb, skb->len);
1246 while (skb->len >= NLMSG_SPACE(0)) {
1249 struct nlmsghdr *nlh;
1250 struct iscsi_uevent *ev;
1252 nlh = (struct nlmsghdr *)skb->data;
1253 if (nlh->nlmsg_len < sizeof(*nlh) ||
1254 skb->len < nlh->nlmsg_len) {
1258 ev = NLMSG_DATA(nlh);
1259 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1260 if (rlen > skb->len)
1263 err = iscsi_if_recv_msg(skb, nlh);
1265 ev->type = ISCSI_KEVENT_IF_ERROR;
1270 * special case for GET_STATS:
1271 * on success - sending reply and stats from
1272 * inside of if_recv_msg(),
1273 * on error - fall through.
1275 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1277 err = iscsi_if_send_reply(
1278 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1279 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1280 if (atomic_read(&z_reply->allocated) >=
1282 ev->iferror = -ENOMEM;
1283 } while (err < 0 && err != -ECONNREFUSED);
1284 skb_pull(skb, rlen);
1289 mutex_unlock(&rx_queue_mutex);
1292 #define iscsi_cdev_to_conn(_cdev) \
1293 iscsi_dev_to_conn(_cdev->dev)
1295 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1296 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1297 __ATTR(_name,_mode,_show,_store)
1300 * iSCSI connection attrs
1302 #define iscsi_conn_attr_show(param) \
1304 show_conn_param_##param(struct class_device *cdev, char *buf) \
1306 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1307 struct iscsi_transport *t = conn->transport; \
1308 return t->get_conn_param(conn, param, buf); \
1311 #define iscsi_conn_attr(field, param) \
1312 iscsi_conn_attr_show(param) \
1313 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
1316 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1317 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1318 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1319 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1320 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1321 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1322 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1323 iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1324 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1325 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1326 iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
1328 #define iscsi_cdev_to_session(_cdev) \
1329 iscsi_dev_to_session(_cdev->dev)
1332 * iSCSI session attrs
1334 #define iscsi_session_attr_show(param) \
1336 show_session_param_##param(struct class_device *cdev, char *buf) \
1338 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1339 struct iscsi_transport *t = session->transport; \
1340 return t->get_session_param(session, param, buf); \
1343 #define iscsi_session_attr(field, param) \
1344 iscsi_session_attr_show(param) \
1345 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
1348 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME);
1349 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN);
1350 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T);
1351 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN);
1352 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST);
1353 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST);
1354 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN);
1355 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN);
1356 iscsi_session_attr(erl, ISCSI_PARAM_ERL);
1357 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT);
1359 #define iscsi_priv_session_attr_show(field, format) \
1361 show_priv_session_##field(struct class_device *cdev, char *buf) \
1363 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);\
1364 return sprintf(buf, format"\n", session->field); \
1367 #define iscsi_priv_session_attr(field, format) \
1368 iscsi_priv_session_attr_show(field, format) \
1369 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1371 iscsi_priv_session_attr(recovery_tmo, "%d");
1373 #define SETUP_PRIV_SESSION_RD_ATTR(field) \
1375 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1380 #define SETUP_SESSION_RD_ATTR(field, param_flag) \
1382 if (tt->param_mask & param_flag) { \
1383 priv->session_attrs[count] = &class_device_attr_sess_##field; \
1388 #define SETUP_CONN_RD_ATTR(field, param_flag) \
1390 if (tt->param_mask & param_flag) { \
1391 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1396 static int iscsi_session_match(struct attribute_container *cont,
1399 struct iscsi_cls_session *session;
1400 struct Scsi_Host *shost;
1401 struct iscsi_internal *priv;
1403 if (!iscsi_is_session_dev(dev))
1406 session = iscsi_dev_to_session(dev);
1407 shost = iscsi_session_to_shost(session);
1408 if (!shost->transportt)
1411 priv = to_iscsi_internal(shost->transportt);
1412 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1415 return &priv->session_cont.ac == cont;
1418 static int iscsi_conn_match(struct attribute_container *cont,
1421 struct iscsi_cls_session *session;
1422 struct iscsi_cls_conn *conn;
1423 struct Scsi_Host *shost;
1424 struct iscsi_internal *priv;
1426 if (!iscsi_is_conn_dev(dev))
1429 conn = iscsi_dev_to_conn(dev);
1430 session = iscsi_dev_to_session(conn->dev.parent);
1431 shost = iscsi_session_to_shost(session);
1433 if (!shost->transportt)
1436 priv = to_iscsi_internal(shost->transportt);
1437 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1440 return &priv->conn_cont.ac == cont;
1443 static int iscsi_host_match(struct attribute_container *cont,
1446 struct Scsi_Host *shost;
1447 struct iscsi_internal *priv;
1449 if (!scsi_is_host_device(dev))
1452 shost = dev_to_shost(dev);
1453 if (!shost->transportt ||
1454 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1457 priv = to_iscsi_internal(shost->transportt);
1458 return &priv->t.host_attrs.ac == cont;
1461 struct scsi_transport_template *
1462 iscsi_register_transport(struct iscsi_transport *tt)
1464 struct iscsi_internal *priv;
1465 unsigned long flags;
1470 priv = iscsi_if_transport_lookup(tt);
1474 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1477 INIT_LIST_HEAD(&priv->list);
1478 priv->daemon_pid = -1;
1479 priv->iscsi_transport = tt;
1480 priv->t.user_scan = iscsi_user_scan;
1482 priv->cdev.class = &iscsi_transport_class;
1483 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1484 err = class_device_register(&priv->cdev);
1488 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1490 goto unregister_cdev;
1492 /* host parameters */
1493 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1494 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1495 priv->t.host_attrs.ac.match = iscsi_host_match;
1496 priv->t.host_size = sizeof(struct iscsi_host);
1497 priv->host_attrs[0] = NULL;
1498 transport_container_register(&priv->t.host_attrs);
1500 /* connection parameters */
1501 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1502 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1503 priv->conn_cont.ac.match = iscsi_conn_match;
1504 transport_container_register(&priv->conn_cont);
1506 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1507 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1508 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1509 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1510 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1511 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1512 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1513 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
1514 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
1515 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1516 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
1518 BUG_ON(count > ISCSI_CONN_ATTRS);
1519 priv->conn_attrs[count] = NULL;
1522 /* session parameters */
1523 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1524 priv->session_cont.ac.class = &iscsi_session_class.class;
1525 priv->session_cont.ac.match = iscsi_session_match;
1526 transport_container_register(&priv->session_cont);
1528 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1529 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1530 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1531 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1532 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1533 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1534 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1535 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
1536 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1537 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
1538 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
1540 BUG_ON(count > ISCSI_SESSION_ATTRS);
1541 priv->session_attrs[count] = NULL;
1543 spin_lock_irqsave(&iscsi_transport_lock, flags);
1544 list_add(&priv->list, &iscsi_transports);
1545 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1547 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1551 class_device_unregister(&priv->cdev);
1556 EXPORT_SYMBOL_GPL(iscsi_register_transport);
1558 int iscsi_unregister_transport(struct iscsi_transport *tt)
1560 struct iscsi_internal *priv;
1561 unsigned long flags;
1565 mutex_lock(&rx_queue_mutex);
1567 priv = iscsi_if_transport_lookup(tt);
1570 spin_lock_irqsave(&iscsi_transport_lock, flags);
1571 list_del(&priv->list);
1572 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1574 transport_container_unregister(&priv->conn_cont);
1575 transport_container_unregister(&priv->session_cont);
1576 transport_container_unregister(&priv->t.host_attrs);
1578 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1579 class_device_unregister(&priv->cdev);
1580 mutex_unlock(&rx_queue_mutex);
1584 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1587 iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr)
1589 struct netlink_notify *n = ptr;
1591 if (event == NETLINK_URELEASE &&
1592 n->protocol == NETLINK_ISCSI && n->pid) {
1593 struct iscsi_cls_conn *conn;
1594 unsigned long flags;
1596 mempool_zone_complete(z_reply);
1597 spin_lock_irqsave(&connlock, flags);
1598 list_for_each_entry(conn, &connlist, conn_list) {
1599 mempool_zone_complete(conn->z_error);
1600 mempool_zone_complete(conn->z_pdu);
1602 spin_unlock_irqrestore(&connlock, flags);
1608 static struct notifier_block iscsi_nl_notifier = {
1609 .notifier_call = iscsi_rcv_nl_event,
1612 static __init int iscsi_transport_init(void)
1616 err = class_register(&iscsi_transport_class);
1620 err = transport_class_register(&iscsi_host_class);
1622 goto unregister_transport_class;
1624 err = transport_class_register(&iscsi_connection_class);
1626 goto unregister_host_class;
1628 err = transport_class_register(&iscsi_session_class);
1630 goto unregister_conn_class;
1632 err = netlink_register_notifier(&iscsi_nl_notifier);
1634 goto unregister_session_class;
1636 nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx,
1640 goto unregister_notifier;
1643 z_reply = mempool_zone_init(Z_MAX_REPLY,
1644 NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY);
1648 sock_release(nls->sk_socket);
1649 unregister_notifier:
1650 netlink_unregister_notifier(&iscsi_nl_notifier);
1651 unregister_session_class:
1652 transport_class_unregister(&iscsi_session_class);
1653 unregister_conn_class:
1654 transport_class_unregister(&iscsi_connection_class);
1655 unregister_host_class:
1656 transport_class_unregister(&iscsi_host_class);
1657 unregister_transport_class:
1658 class_unregister(&iscsi_transport_class);
1662 static void __exit iscsi_transport_exit(void)
1664 mempool_zone_destroy(z_reply);
1665 sock_release(nls->sk_socket);
1666 netlink_unregister_notifier(&iscsi_nl_notifier);
1667 transport_class_unregister(&iscsi_connection_class);
1668 transport_class_unregister(&iscsi_session_class);
1669 transport_class_unregister(&iscsi_host_class);
1670 class_unregister(&iscsi_transport_class);
1673 module_init(iscsi_transport_init);
1674 module_exit(iscsi_transport_exit);
1676 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1677 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1678 "Alex Aizman <itn780@yahoo.com>");
1679 MODULE_DESCRIPTION("iSCSI Transport Interface");
1680 MODULE_LICENSE("GPL");