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
37 #define ISCSI_TRANSPORT_VERSION "1.1-646"
39 struct iscsi_internal {
41 struct scsi_transport_template t;
42 struct iscsi_transport *iscsi_transport;
43 struct list_head list;
44 struct class_device cdev;
46 struct class_device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
47 struct transport_container conn_cont;
48 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
49 struct transport_container session_cont;
50 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
53 static int iscsi_session_nr; /* sysfs session id for next new session */
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,
119 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
120 struct class_device *cdev)
122 struct Scsi_Host *shost = dev_to_shost(dev);
123 struct iscsi_host *ihost = shost->shost_data;
125 memset(ihost, 0, sizeof(*ihost));
126 INIT_LIST_HEAD(&ihost->sessions);
127 mutex_init(&ihost->mutex);
131 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
137 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
143 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
149 static struct sock *nls;
150 static DEFINE_MUTEX(rx_queue_mutex);
152 struct mempool_zone {
157 struct list_head freequeue;
161 static struct mempool_zone *z_reply;
164 * Z_MAX_* - actual mempool size allocated at the mempool_zone_init() time
165 * Z_HIWAT_* - zone's high watermark when if_error bit will be set to -ENOMEM
166 * so daemon will notice OOM on NETLINK tranposrt level and will
167 * be able to predict or change operational behavior
169 #define Z_MAX_REPLY 8
170 #define Z_HIWAT_REPLY 6
172 #define Z_HIWAT_PDU 6
173 #define Z_MAX_ERROR 16
174 #define Z_HIWAT_ERROR 12
176 static LIST_HEAD(sesslist);
177 static DEFINE_SPINLOCK(sesslock);
178 static LIST_HEAD(connlist);
179 static DEFINE_SPINLOCK(connlock);
181 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
183 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
188 * Returns the matching session to a given sid
190 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
193 struct iscsi_cls_session *sess;
195 spin_lock_irqsave(&sesslock, flags);
196 list_for_each_entry(sess, &sesslist, sess_list) {
197 if (sess->sid == sid) {
198 spin_unlock_irqrestore(&sesslock, flags);
202 spin_unlock_irqrestore(&sesslock, flags);
207 * Returns the matching connection to a given sid / cid tuple
209 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
212 struct iscsi_cls_conn *conn;
214 spin_lock_irqsave(&connlock, flags);
215 list_for_each_entry(conn, &connlist, conn_list) {
216 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
217 spin_unlock_irqrestore(&connlock, flags);
221 spin_unlock_irqrestore(&connlock, flags);
226 * The following functions can be used by LLDs that allocate
227 * their own scsi_hosts or by software iscsi LLDs
229 static void iscsi_session_release(struct device *dev)
231 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
232 struct Scsi_Host *shost;
234 shost = iscsi_session_to_shost(session);
235 scsi_host_put(shost);
239 static int iscsi_is_session_dev(const struct device *dev)
241 return dev->release == iscsi_session_release;
244 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
247 struct iscsi_host *ihost = shost->shost_data;
248 struct iscsi_cls_session *session;
250 mutex_lock(&ihost->mutex);
251 list_for_each_entry(session, &ihost->sessions, host_list) {
252 if ((channel == SCAN_WILD_CARD || channel == 0) &&
253 (id == SCAN_WILD_CARD || id == session->target_id))
254 scsi_scan_target(&session->dev, 0,
255 session->target_id, lun, 1);
257 mutex_unlock(&ihost->mutex);
262 static void session_recovery_timedout(void *data)
264 struct iscsi_cls_session *session = data;
266 dev_printk(KERN_INFO, &session->dev, "iscsi: session recovery timed "
267 "out after %d secs\n", session->recovery_tmo);
269 if (session->transport->session_recovery_timedout)
270 session->transport->session_recovery_timedout(session);
272 scsi_target_unblock(&session->dev);
275 void iscsi_unblock_session(struct iscsi_cls_session *session)
277 if (!cancel_delayed_work(&session->recovery_work))
278 flush_scheduled_work();
279 scsi_target_unblock(&session->dev);
281 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
283 void iscsi_block_session(struct iscsi_cls_session *session)
285 scsi_target_block(&session->dev);
286 schedule_delayed_work(&session->recovery_work,
287 session->recovery_tmo * HZ);
289 EXPORT_SYMBOL_GPL(iscsi_block_session);
291 struct iscsi_cls_session *
292 iscsi_alloc_session(struct Scsi_Host *shost,
293 struct iscsi_transport *transport)
295 struct iscsi_cls_session *session;
297 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
302 session->transport = transport;
303 session->recovery_tmo = 120;
304 INIT_WORK(&session->recovery_work, session_recovery_timedout, session);
305 INIT_LIST_HEAD(&session->host_list);
306 INIT_LIST_HEAD(&session->sess_list);
308 /* this is released in the dev's release function */
309 scsi_host_get(shost);
310 session->dev.parent = &shost->shost_gendev;
311 session->dev.release = iscsi_session_release;
312 device_initialize(&session->dev);
313 if (transport->sessiondata_size)
314 session->dd_data = &session[1];
317 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
319 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
321 struct Scsi_Host *shost = iscsi_session_to_shost(session);
322 struct iscsi_host *ihost;
325 ihost = shost->shost_data;
326 session->sid = iscsi_session_nr++;
327 session->target_id = target_id;
329 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
331 err = device_add(&session->dev);
333 dev_printk(KERN_ERR, &session->dev, "iscsi: could not "
334 "register session's dev\n");
337 transport_register_device(&session->dev);
339 mutex_lock(&ihost->mutex);
340 list_add(&session->host_list, &ihost->sessions);
341 mutex_unlock(&ihost->mutex);
345 scsi_host_put(shost);
348 EXPORT_SYMBOL_GPL(iscsi_add_session);
351 * iscsi_create_session - create iscsi class session
353 * @transport: iscsi transport
355 * This can be called from a LLD or iscsi_transport.
357 struct iscsi_cls_session *
358 iscsi_create_session(struct Scsi_Host *shost,
359 struct iscsi_transport *transport,
360 unsigned int target_id)
362 struct iscsi_cls_session *session;
364 session = iscsi_alloc_session(shost, transport);
368 if (iscsi_add_session(session, target_id)) {
369 iscsi_free_session(session);
374 EXPORT_SYMBOL_GPL(iscsi_create_session);
376 void iscsi_remove_session(struct iscsi_cls_session *session)
378 struct Scsi_Host *shost = iscsi_session_to_shost(session);
379 struct iscsi_host *ihost = shost->shost_data;
381 if (!cancel_delayed_work(&session->recovery_work))
382 flush_scheduled_work();
384 mutex_lock(&ihost->mutex);
385 list_del(&session->host_list);
386 mutex_unlock(&ihost->mutex);
388 scsi_remove_target(&session->dev);
390 transport_unregister_device(&session->dev);
391 device_del(&session->dev);
393 EXPORT_SYMBOL_GPL(iscsi_remove_session);
395 void iscsi_free_session(struct iscsi_cls_session *session)
397 put_device(&session->dev);
400 EXPORT_SYMBOL_GPL(iscsi_free_session);
403 * iscsi_destroy_session - destroy iscsi session
404 * @session: iscsi_session
406 * Can be called by a LLD or iscsi_transport. There must not be
407 * any running connections.
409 int iscsi_destroy_session(struct iscsi_cls_session *session)
411 iscsi_remove_session(session);
412 iscsi_free_session(session);
415 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
417 static void mempool_zone_destroy(struct mempool_zone *zp)
419 mempool_destroy(zp->pool);
424 mempool_zone_alloc_skb(gfp_t gfp_mask, void *pool_data)
426 struct mempool_zone *zone = pool_data;
428 return alloc_skb(zone->size, gfp_mask);
432 mempool_zone_free_skb(void *element, void *pool_data)
437 static struct mempool_zone *
438 mempool_zone_init(unsigned max, unsigned size, unsigned hiwat)
440 struct mempool_zone *zp;
442 zp = kzalloc(sizeof(*zp), GFP_KERNEL);
448 INIT_LIST_HEAD(&zp->freequeue);
449 spin_lock_init(&zp->freelock);
450 atomic_set(&zp->allocated, 0);
452 zp->pool = mempool_create(max, mempool_zone_alloc_skb,
453 mempool_zone_free_skb, zp);
462 static void iscsi_conn_release(struct device *dev)
464 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
465 struct device *parent = conn->dev.parent;
467 mempool_zone_destroy(conn->z_pdu);
468 mempool_zone_destroy(conn->z_error);
474 static int iscsi_is_conn_dev(const struct device *dev)
476 return dev->release == iscsi_conn_release;
479 static int iscsi_create_event_pools(struct iscsi_cls_conn *conn)
481 conn->z_pdu = mempool_zone_init(Z_MAX_PDU,
482 NLMSG_SPACE(sizeof(struct iscsi_uevent) +
483 sizeof(struct iscsi_hdr) +
484 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH),
487 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
488 "pdu zone for new conn\n");
492 conn->z_error = mempool_zone_init(Z_MAX_ERROR,
493 NLMSG_SPACE(sizeof(struct iscsi_uevent)),
495 if (!conn->z_error) {
496 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
497 "error zone for new conn\n");
498 mempool_zone_destroy(conn->z_pdu);
505 * iscsi_create_conn - create iscsi class connection
506 * @session: iscsi cls session
507 * @cid: connection id
509 * This can be called from a LLD or iscsi_transport. The connection
510 * is child of the session so cid must be unique for all connections
513 * Since we do not support MCS, cid will normally be zero. In some cases
514 * for software iscsi we could be trying to preallocate a connection struct
515 * in which case there could be two connection structs and cid would be
518 struct iscsi_cls_conn *
519 iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
521 struct iscsi_transport *transport = session->transport;
522 struct iscsi_cls_conn *conn;
525 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
529 if (transport->conndata_size)
530 conn->dd_data = &conn[1];
532 INIT_LIST_HEAD(&conn->conn_list);
533 conn->transport = transport;
536 if (iscsi_create_event_pools(conn))
539 /* this is released in the dev's release function */
540 if (!get_device(&session->dev))
541 goto free_conn_pools;
543 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
545 conn->dev.parent = &session->dev;
546 conn->dev.release = iscsi_conn_release;
547 err = device_register(&conn->dev);
549 dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register "
550 "connection's dev\n");
551 goto release_parent_ref;
553 transport_register_device(&conn->dev);
557 put_device(&session->dev);
565 EXPORT_SYMBOL_GPL(iscsi_create_conn);
568 * iscsi_destroy_conn - destroy iscsi class connection
569 * @session: iscsi cls session
571 * This can be called from a LLD or iscsi_transport.
573 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
575 transport_unregister_device(&conn->dev);
576 device_unregister(&conn->dev);
580 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
583 * iscsi interface functions
585 static struct iscsi_internal *
586 iscsi_if_transport_lookup(struct iscsi_transport *tt)
588 struct iscsi_internal *priv;
591 spin_lock_irqsave(&iscsi_transport_lock, flags);
592 list_for_each_entry(priv, &iscsi_transports, list) {
593 if (tt == priv->iscsi_transport) {
594 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
598 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
602 static inline struct list_head *skb_to_lh(struct sk_buff *skb)
604 return (struct list_head *)&skb->cb;
608 mempool_zone_complete(struct mempool_zone *zone)
611 struct list_head *lh, *n;
613 spin_lock_irqsave(&zone->freelock, flags);
614 list_for_each_safe(lh, n, &zone->freequeue) {
615 struct sk_buff *skb = (struct sk_buff *)((char *)lh -
616 offsetof(struct sk_buff, cb));
617 if (!skb_shared(skb)) {
618 list_del(skb_to_lh(skb));
619 mempool_free(skb, zone->pool);
620 atomic_dec(&zone->allocated);
623 spin_unlock_irqrestore(&zone->freelock, flags);
626 static struct sk_buff*
627 mempool_zone_get_skb(struct mempool_zone *zone)
631 skb = mempool_alloc(zone->pool, GFP_ATOMIC);
633 atomic_inc(&zone->allocated);
638 iscsi_broadcast_skb(struct mempool_zone *zone, struct sk_buff *skb, gfp_t gfp)
644 rc = netlink_broadcast(nls, skb, 0, 1, gfp);
646 mempool_free(skb, zone->pool);
647 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
651 spin_lock_irqsave(&zone->freelock, flags);
652 INIT_LIST_HEAD(skb_to_lh(skb));
653 list_add(skb_to_lh(skb), &zone->freequeue);
654 spin_unlock_irqrestore(&zone->freelock, flags);
659 iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb, int pid)
665 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
667 mempool_free(skb, zone->pool);
668 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
672 spin_lock_irqsave(&zone->freelock, flags);
673 INIT_LIST_HEAD(skb_to_lh(skb));
674 list_add(skb_to_lh(skb), &zone->freequeue);
675 spin_unlock_irqrestore(&zone->freelock, flags);
680 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
681 char *data, uint32_t data_size)
683 struct nlmsghdr *nlh;
685 struct iscsi_uevent *ev;
687 struct iscsi_internal *priv;
688 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
691 priv = iscsi_if_transport_lookup(conn->transport);
695 mempool_zone_complete(conn->z_pdu);
697 skb = mempool_zone_get_skb(conn->z_pdu);
699 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
700 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver "
701 "control PDU: OOM\n");
705 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
706 ev = NLMSG_DATA(nlh);
707 memset(ev, 0, sizeof(*ev));
708 ev->transport_handle = iscsi_handle(conn->transport);
709 ev->type = ISCSI_KEVENT_RECV_PDU;
710 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
711 ev->iferror = -ENOMEM;
712 ev->r.recv_req.cid = conn->cid;
713 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
714 pdu = (char*)ev + sizeof(*ev);
715 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
716 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
718 return iscsi_unicast_skb(conn->z_pdu, skb, priv->daemon_pid);
720 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
722 void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
724 struct nlmsghdr *nlh;
726 struct iscsi_uevent *ev;
727 struct iscsi_internal *priv;
728 int len = NLMSG_SPACE(sizeof(*ev));
730 priv = iscsi_if_transport_lookup(conn->transport);
734 mempool_zone_complete(conn->z_error);
736 skb = mempool_zone_get_skb(conn->z_error);
738 dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored "
739 "conn error (%d)\n", error);
743 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
744 ev = NLMSG_DATA(nlh);
745 ev->transport_handle = iscsi_handle(conn->transport);
746 ev->type = ISCSI_KEVENT_CONN_ERROR;
747 if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat)
748 ev->iferror = -ENOMEM;
749 ev->r.connerror.error = error;
750 ev->r.connerror.cid = conn->cid;
751 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
753 iscsi_broadcast_skb(conn->z_error, skb, GFP_ATOMIC);
755 dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n",
758 EXPORT_SYMBOL_GPL(iscsi_conn_error);
761 iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
762 void *payload, int size)
765 struct nlmsghdr *nlh;
766 int len = NLMSG_SPACE(size);
767 int flags = multi ? NLM_F_MULTI : 0;
768 int t = done ? NLMSG_DONE : type;
770 mempool_zone_complete(z_reply);
772 skb = mempool_zone_get_skb(z_reply);
775 * user is supposed to react on iferror == -ENOMEM;
780 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
781 nlh->nlmsg_flags = flags;
782 memcpy(NLMSG_DATA(nlh), payload, size);
783 return iscsi_unicast_skb(z_reply, skb, pid);
787 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
789 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
790 struct iscsi_stats *stats;
791 struct sk_buff *skbstat;
792 struct iscsi_cls_conn *conn;
793 struct nlmsghdr *nlhstat;
794 struct iscsi_uevent *evstat;
795 struct iscsi_internal *priv;
796 int len = NLMSG_SPACE(sizeof(*ev) +
797 sizeof(struct iscsi_stats) +
798 sizeof(struct iscsi_stats_custom) *
799 ISCSI_STATS_CUSTOM_MAX);
802 priv = iscsi_if_transport_lookup(transport);
806 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
813 mempool_zone_complete(conn->z_pdu);
815 skbstat = mempool_zone_get_skb(conn->z_pdu);
817 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not "
818 "deliver stats: OOM\n");
822 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
823 (len - sizeof(*nlhstat)), 0);
824 evstat = NLMSG_DATA(nlhstat);
825 memset(evstat, 0, sizeof(*evstat));
826 evstat->transport_handle = iscsi_handle(conn->transport);
827 evstat->type = nlh->nlmsg_type;
828 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
829 evstat->iferror = -ENOMEM;
830 evstat->u.get_stats.cid =
832 evstat->u.get_stats.sid =
834 stats = (struct iscsi_stats *)
835 ((char*)evstat + sizeof(*evstat));
836 memset(stats, 0, sizeof(*stats));
838 transport->get_stats(conn, stats);
839 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
840 sizeof(struct iscsi_stats) +
841 sizeof(struct iscsi_stats_custom) *
842 stats->custom_length);
843 actual_size -= sizeof(*nlhstat);
844 actual_size = NLMSG_LENGTH(actual_size);
845 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
846 nlhstat->nlmsg_len = actual_size;
848 err = iscsi_unicast_skb(conn->z_pdu, skbstat, priv->daemon_pid);
849 } while (err < 0 && err != -ECONNREFUSED);
855 * iscsi_if_destroy_session_done - send session destr. completion event
856 * @conn: last connection for session
858 * This is called by HW iscsi LLDs to notify userpsace that its HW has
861 int iscsi_if_destroy_session_done(struct iscsi_cls_conn *conn)
863 struct iscsi_internal *priv;
864 struct iscsi_cls_session *session;
865 struct Scsi_Host *shost;
866 struct iscsi_uevent *ev;
868 struct nlmsghdr *nlh;
870 int rc, len = NLMSG_SPACE(sizeof(*ev));
872 priv = iscsi_if_transport_lookup(conn->transport);
876 session = iscsi_dev_to_session(conn->dev.parent);
877 shost = iscsi_session_to_shost(session);
879 mempool_zone_complete(conn->z_pdu);
881 skb = mempool_zone_get_skb(conn->z_pdu);
883 dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of "
884 "session creation event\n");
888 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
889 ev = NLMSG_DATA(nlh);
890 ev->transport_handle = iscsi_handle(conn->transport);
891 ev->type = ISCSI_KEVENT_DESTROY_SESSION;
892 ev->r.d_session.host_no = shost->host_no;
893 ev->r.d_session.sid = session->sid;
896 * this will occur if the daemon is not up, so we just warn
897 * the user and when the daemon is restarted it will handle it
899 rc = iscsi_broadcast_skb(conn->z_pdu, skb, GFP_KERNEL);
901 dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of "
902 "session destruction event. Check iscsi daemon\n");
904 spin_lock_irqsave(&sesslock, flags);
905 list_del(&session->sess_list);
906 spin_unlock_irqrestore(&sesslock, flags);
908 spin_lock_irqsave(&connlock, flags);
910 list_del(&conn->conn_list);
911 spin_unlock_irqrestore(&connlock, flags);
915 EXPORT_SYMBOL_GPL(iscsi_if_destroy_session_done);
918 * iscsi_if_create_session_done - send session creation completion event
919 * @conn: leading connection for session
921 * This is called by HW iscsi LLDs to notify userpsace that its HW has
922 * created a session or a existing session is back in the logged in state.
924 int iscsi_if_create_session_done(struct iscsi_cls_conn *conn)
926 struct iscsi_internal *priv;
927 struct iscsi_cls_session *session;
928 struct Scsi_Host *shost;
929 struct iscsi_uevent *ev;
931 struct nlmsghdr *nlh;
933 int rc, len = NLMSG_SPACE(sizeof(*ev));
935 priv = iscsi_if_transport_lookup(conn->transport);
939 session = iscsi_dev_to_session(conn->dev.parent);
940 shost = iscsi_session_to_shost(session);
942 mempool_zone_complete(conn->z_pdu);
944 skb = mempool_zone_get_skb(conn->z_pdu);
946 dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of "
947 "session creation event\n");
951 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
952 ev = NLMSG_DATA(nlh);
953 ev->transport_handle = iscsi_handle(conn->transport);
954 ev->type = ISCSI_UEVENT_CREATE_SESSION;
955 ev->r.c_session_ret.host_no = shost->host_no;
956 ev->r.c_session_ret.sid = session->sid;
959 * this will occur if the daemon is not up, so we just warn
960 * the user and when the daemon is restarted it will handle it
962 rc = iscsi_broadcast_skb(conn->z_pdu, skb, GFP_KERNEL);
964 dev_printk(KERN_ERR, &conn->dev, "Cannot notify userspace of "
965 "session creation event. Check iscsi daemon\n");
967 spin_lock_irqsave(&sesslock, flags);
968 list_add(&session->sess_list, &sesslist);
969 spin_unlock_irqrestore(&sesslock, flags);
971 spin_lock_irqsave(&connlock, flags);
972 list_add(&conn->conn_list, &connlist);
974 spin_unlock_irqrestore(&connlock, flags);
977 EXPORT_SYMBOL_GPL(iscsi_if_create_session_done);
980 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
982 struct iscsi_transport *transport = priv->iscsi_transport;
983 struct iscsi_cls_session *session;
987 session = transport->create_session(transport, &priv->t,
988 ev->u.c_session.initial_cmdsn,
993 spin_lock_irqsave(&sesslock, flags);
994 list_add(&session->sess_list, &sesslist);
995 spin_unlock_irqrestore(&sesslock, flags);
997 ev->r.c_session_ret.host_no = hostno;
998 ev->r.c_session_ret.sid = session->sid;
1003 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1005 struct iscsi_cls_conn *conn;
1006 struct iscsi_cls_session *session;
1007 unsigned long flags;
1009 session = iscsi_session_lookup(ev->u.c_conn.sid);
1011 printk(KERN_ERR "iscsi: invalid session %d\n",
1016 conn = transport->create_conn(session, ev->u.c_conn.cid);
1018 printk(KERN_ERR "iscsi: couldn't create a new "
1019 "connection for session %d\n",
1024 ev->r.c_conn_ret.sid = session->sid;
1025 ev->r.c_conn_ret.cid = conn->cid;
1027 spin_lock_irqsave(&connlock, flags);
1028 list_add(&conn->conn_list, &connlist);
1030 spin_unlock_irqrestore(&connlock, flags);
1036 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1038 unsigned long flags;
1039 struct iscsi_cls_conn *conn;
1041 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
1044 spin_lock_irqsave(&connlock, flags);
1046 list_del(&conn->conn_list);
1047 spin_unlock_irqrestore(&connlock, flags);
1049 if (transport->destroy_conn)
1050 transport->destroy_conn(conn);
1055 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1057 char *data = (char*)ev + sizeof(*ev);
1058 struct iscsi_cls_conn *conn;
1059 struct iscsi_cls_session *session;
1060 int err = 0, value = 0;
1062 session = iscsi_session_lookup(ev->u.set_param.sid);
1063 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1064 if (!conn || !session)
1067 switch (ev->u.set_param.param) {
1068 case ISCSI_PARAM_SESS_RECOVERY_TMO:
1069 sscanf(data, "%d", &value);
1071 session->recovery_tmo = value;
1074 err = transport->set_param(conn, ev->u.set_param.param,
1075 data, ev->u.set_param.len);
1082 iscsi_if_transport_ep(struct iscsi_transport *transport,
1083 struct iscsi_uevent *ev, int msg_type)
1085 struct sockaddr *dst_addr;
1089 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1090 if (!transport->ep_connect)
1093 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1094 rc = transport->ep_connect(dst_addr,
1095 ev->u.ep_connect.non_blocking,
1096 &ev->r.ep_connect_ret.handle);
1098 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1099 if (!transport->ep_poll)
1102 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
1103 ev->u.ep_poll.timeout_ms);
1105 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1106 if (!transport->ep_disconnect)
1109 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
1116 iscsi_tgt_dscvr(struct iscsi_transport *transport,
1117 struct iscsi_uevent *ev)
1119 struct sockaddr *dst_addr;
1121 if (!transport->tgt_dscvr)
1124 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1125 return transport->tgt_dscvr(ev->u.tgt_dscvr.type,
1126 ev->u.tgt_dscvr.host_no,
1127 ev->u.tgt_dscvr.enable, dst_addr);
1131 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1134 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1135 struct iscsi_transport *transport = NULL;
1136 struct iscsi_internal *priv;
1137 struct iscsi_cls_session *session;
1138 struct iscsi_cls_conn *conn;
1139 unsigned long flags;
1141 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1144 transport = priv->iscsi_transport;
1146 if (!try_module_get(transport->owner))
1149 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1151 switch (nlh->nlmsg_type) {
1152 case ISCSI_UEVENT_CREATE_SESSION:
1153 err = iscsi_if_create_session(priv, ev);
1155 case ISCSI_UEVENT_DESTROY_SESSION:
1156 session = iscsi_session_lookup(ev->u.d_session.sid);
1158 spin_lock_irqsave(&sesslock, flags);
1159 list_del(&session->sess_list);
1160 spin_unlock_irqrestore(&sesslock, flags);
1162 transport->destroy_session(session);
1166 case ISCSI_UEVENT_CREATE_CONN:
1167 err = iscsi_if_create_conn(transport, ev);
1169 case ISCSI_UEVENT_DESTROY_CONN:
1170 err = iscsi_if_destroy_conn(transport, ev);
1172 case ISCSI_UEVENT_BIND_CONN:
1173 session = iscsi_session_lookup(ev->u.b_conn.sid);
1174 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1176 if (session && conn)
1177 ev->r.retcode = transport->bind_conn(session, conn,
1178 ev->u.b_conn.transport_eph,
1179 ev->u.b_conn.is_leading);
1183 case ISCSI_UEVENT_SET_PARAM:
1184 err = iscsi_set_param(transport, ev);
1186 case ISCSI_UEVENT_START_CONN:
1187 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
1189 ev->r.retcode = transport->start_conn(conn);
1193 case ISCSI_UEVENT_STOP_CONN:
1194 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
1196 transport->stop_conn(conn, ev->u.stop_conn.flag);
1200 case ISCSI_UEVENT_SEND_PDU:
1201 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
1203 ev->r.retcode = transport->send_pdu(conn,
1204 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1205 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1206 ev->u.send_pdu.data_size);
1210 case ISCSI_UEVENT_GET_STATS:
1211 err = iscsi_if_get_stats(transport, nlh);
1213 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1214 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1215 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1216 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1218 case ISCSI_UEVENT_TGT_DSCVR:
1219 err = iscsi_tgt_dscvr(transport, ev);
1226 module_put(transport->owner);
1231 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
1232 * processed by iscsi_if_recv_msg. Malformed skbs with wrong lengths or
1233 * invalid creds are discarded silently.
1236 iscsi_if_rx(struct sock *sk, int len)
1238 struct sk_buff *skb;
1240 mutex_lock(&rx_queue_mutex);
1241 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
1242 if (NETLINK_CREDS(skb)->uid) {
1243 skb_pull(skb, skb->len);
1247 while (skb->len >= NLMSG_SPACE(0)) {
1250 struct nlmsghdr *nlh;
1251 struct iscsi_uevent *ev;
1253 nlh = (struct nlmsghdr *)skb->data;
1254 if (nlh->nlmsg_len < sizeof(*nlh) ||
1255 skb->len < nlh->nlmsg_len) {
1259 ev = NLMSG_DATA(nlh);
1260 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1261 if (rlen > skb->len)
1264 err = iscsi_if_recv_msg(skb, nlh);
1266 ev->type = ISCSI_KEVENT_IF_ERROR;
1271 * special case for GET_STATS:
1272 * on success - sending reply and stats from
1273 * inside of if_recv_msg(),
1274 * on error - fall through.
1276 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1278 err = iscsi_if_send_reply(
1279 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1280 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1281 if (atomic_read(&z_reply->allocated) >=
1283 ev->iferror = -ENOMEM;
1284 } while (err < 0 && err != -ECONNREFUSED);
1285 skb_pull(skb, rlen);
1290 mutex_unlock(&rx_queue_mutex);
1293 #define iscsi_cdev_to_conn(_cdev) \
1294 iscsi_dev_to_conn(_cdev->dev)
1296 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1297 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1298 __ATTR(_name,_mode,_show,_store)
1301 * iSCSI connection attrs
1303 #define iscsi_conn_attr_show(param) \
1305 show_conn_param_##param(struct class_device *cdev, char *buf) \
1307 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1308 struct iscsi_transport *t = conn->transport; \
1309 return t->get_conn_param(conn, param, buf); \
1312 #define iscsi_conn_attr(field, param) \
1313 iscsi_conn_attr_show(param) \
1314 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
1317 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1318 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1319 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1320 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1321 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1322 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1323 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1324 iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1325 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1326 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1327 iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
1329 #define iscsi_cdev_to_session(_cdev) \
1330 iscsi_dev_to_session(_cdev->dev)
1333 * iSCSI session attrs
1335 #define iscsi_session_attr_show(param) \
1337 show_session_param_##param(struct class_device *cdev, char *buf) \
1339 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1340 struct iscsi_transport *t = session->transport; \
1341 return t->get_session_param(session, param, buf); \
1344 #define iscsi_session_attr(field, param) \
1345 iscsi_session_attr_show(param) \
1346 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
1349 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME);
1350 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN);
1351 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T);
1352 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN);
1353 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST);
1354 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST);
1355 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN);
1356 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN);
1357 iscsi_session_attr(erl, ISCSI_PARAM_ERL);
1358 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT);
1360 #define iscsi_priv_session_attr_show(field, format) \
1362 show_priv_session_##field(struct class_device *cdev, char *buf) \
1364 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);\
1365 return sprintf(buf, format"\n", session->field); \
1368 #define iscsi_priv_session_attr(field, format) \
1369 iscsi_priv_session_attr_show(field, format) \
1370 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1372 iscsi_priv_session_attr(recovery_tmo, "%d");
1374 #define SETUP_PRIV_SESSION_RD_ATTR(field) \
1376 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1381 #define SETUP_SESSION_RD_ATTR(field, param_flag) \
1383 if (tt->param_mask & param_flag) { \
1384 priv->session_attrs[count] = &class_device_attr_sess_##field; \
1389 #define SETUP_CONN_RD_ATTR(field, param_flag) \
1391 if (tt->param_mask & param_flag) { \
1392 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1397 static int iscsi_session_match(struct attribute_container *cont,
1400 struct iscsi_cls_session *session;
1401 struct Scsi_Host *shost;
1402 struct iscsi_internal *priv;
1404 if (!iscsi_is_session_dev(dev))
1407 session = iscsi_dev_to_session(dev);
1408 shost = iscsi_session_to_shost(session);
1409 if (!shost->transportt)
1412 priv = to_iscsi_internal(shost->transportt);
1413 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1416 return &priv->session_cont.ac == cont;
1419 static int iscsi_conn_match(struct attribute_container *cont,
1422 struct iscsi_cls_session *session;
1423 struct iscsi_cls_conn *conn;
1424 struct Scsi_Host *shost;
1425 struct iscsi_internal *priv;
1427 if (!iscsi_is_conn_dev(dev))
1430 conn = iscsi_dev_to_conn(dev);
1431 session = iscsi_dev_to_session(conn->dev.parent);
1432 shost = iscsi_session_to_shost(session);
1434 if (!shost->transportt)
1437 priv = to_iscsi_internal(shost->transportt);
1438 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1441 return &priv->conn_cont.ac == cont;
1444 static int iscsi_host_match(struct attribute_container *cont,
1447 struct Scsi_Host *shost;
1448 struct iscsi_internal *priv;
1450 if (!scsi_is_host_device(dev))
1453 shost = dev_to_shost(dev);
1454 if (!shost->transportt ||
1455 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1458 priv = to_iscsi_internal(shost->transportt);
1459 return &priv->t.host_attrs.ac == cont;
1462 struct scsi_transport_template *
1463 iscsi_register_transport(struct iscsi_transport *tt)
1465 struct iscsi_internal *priv;
1466 unsigned long flags;
1471 priv = iscsi_if_transport_lookup(tt);
1475 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1478 INIT_LIST_HEAD(&priv->list);
1479 priv->daemon_pid = -1;
1480 priv->iscsi_transport = tt;
1481 priv->t.user_scan = iscsi_user_scan;
1483 priv->cdev.class = &iscsi_transport_class;
1484 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1485 err = class_device_register(&priv->cdev);
1489 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1491 goto unregister_cdev;
1493 /* host parameters */
1494 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1495 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1496 priv->t.host_attrs.ac.match = iscsi_host_match;
1497 priv->t.host_size = sizeof(struct iscsi_host);
1498 priv->host_attrs[0] = NULL;
1499 transport_container_register(&priv->t.host_attrs);
1501 /* connection parameters */
1502 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1503 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1504 priv->conn_cont.ac.match = iscsi_conn_match;
1505 transport_container_register(&priv->conn_cont);
1507 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1508 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1509 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1510 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1511 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1512 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1513 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1514 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
1515 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
1516 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1517 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
1519 BUG_ON(count > ISCSI_CONN_ATTRS);
1520 priv->conn_attrs[count] = NULL;
1523 /* session parameters */
1524 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1525 priv->session_cont.ac.class = &iscsi_session_class.class;
1526 priv->session_cont.ac.match = iscsi_session_match;
1527 transport_container_register(&priv->session_cont);
1529 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1530 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1531 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1532 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1533 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1534 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1535 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1536 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
1537 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1538 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
1539 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
1541 BUG_ON(count > ISCSI_SESSION_ATTRS);
1542 priv->session_attrs[count] = NULL;
1544 spin_lock_irqsave(&iscsi_transport_lock, flags);
1545 list_add(&priv->list, &iscsi_transports);
1546 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1548 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1552 class_device_unregister(&priv->cdev);
1557 EXPORT_SYMBOL_GPL(iscsi_register_transport);
1559 int iscsi_unregister_transport(struct iscsi_transport *tt)
1561 struct iscsi_internal *priv;
1562 unsigned long flags;
1566 mutex_lock(&rx_queue_mutex);
1568 priv = iscsi_if_transport_lookup(tt);
1571 spin_lock_irqsave(&iscsi_transport_lock, flags);
1572 list_del(&priv->list);
1573 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1575 transport_container_unregister(&priv->conn_cont);
1576 transport_container_unregister(&priv->session_cont);
1577 transport_container_unregister(&priv->t.host_attrs);
1579 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1580 class_device_unregister(&priv->cdev);
1581 mutex_unlock(&rx_queue_mutex);
1585 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1588 iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr)
1590 struct netlink_notify *n = ptr;
1592 if (event == NETLINK_URELEASE &&
1593 n->protocol == NETLINK_ISCSI && n->pid) {
1594 struct iscsi_cls_conn *conn;
1595 unsigned long flags;
1597 mempool_zone_complete(z_reply);
1598 spin_lock_irqsave(&connlock, flags);
1599 list_for_each_entry(conn, &connlist, conn_list) {
1600 mempool_zone_complete(conn->z_error);
1601 mempool_zone_complete(conn->z_pdu);
1603 spin_unlock_irqrestore(&connlock, flags);
1609 static struct notifier_block iscsi_nl_notifier = {
1610 .notifier_call = iscsi_rcv_nl_event,
1613 static __init int iscsi_transport_init(void)
1617 printk(KERN_INFO "Loading iSCSI transport class v%s.",
1618 ISCSI_TRANSPORT_VERSION);
1620 err = class_register(&iscsi_transport_class);
1624 err = transport_class_register(&iscsi_host_class);
1626 goto unregister_transport_class;
1628 err = transport_class_register(&iscsi_connection_class);
1630 goto unregister_host_class;
1632 err = transport_class_register(&iscsi_session_class);
1634 goto unregister_conn_class;
1636 err = netlink_register_notifier(&iscsi_nl_notifier);
1638 goto unregister_session_class;
1640 nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx,
1644 goto unregister_notifier;
1647 z_reply = mempool_zone_init(Z_MAX_REPLY,
1648 NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY);
1652 sock_release(nls->sk_socket);
1653 unregister_notifier:
1654 netlink_unregister_notifier(&iscsi_nl_notifier);
1655 unregister_session_class:
1656 transport_class_unregister(&iscsi_session_class);
1657 unregister_conn_class:
1658 transport_class_unregister(&iscsi_connection_class);
1659 unregister_host_class:
1660 transport_class_unregister(&iscsi_host_class);
1661 unregister_transport_class:
1662 class_unregister(&iscsi_transport_class);
1666 static void __exit iscsi_transport_exit(void)
1668 mempool_zone_destroy(z_reply);
1669 sock_release(nls->sk_socket);
1670 netlink_unregister_notifier(&iscsi_nl_notifier);
1671 transport_class_unregister(&iscsi_connection_class);
1672 transport_class_unregister(&iscsi_session_class);
1673 transport_class_unregister(&iscsi_host_class);
1674 class_unregister(&iscsi_transport_class);
1677 module_init(iscsi_transport_init);
1678 module_exit(iscsi_transport_exit);
1680 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1681 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1682 "Alex Aizman <itn780@yahoo.com>");
1683 MODULE_DESCRIPTION("iSCSI Transport Interface");
1684 MODULE_LICENSE("GPL");
1685 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);