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 8
35 #define ISCSI_CONN_ATTRS 6
37 struct iscsi_internal {
38 struct scsi_transport_template t;
39 struct iscsi_transport *iscsi_transport;
40 struct list_head list;
42 * List of sessions for this transport
44 struct list_head sessions;
46 * based on transport capabilities, at register time we set these
47 * bits to tell the transport class it wants attributes displayed
48 * in sysfs or that it can support different iSCSI Data-Path
53 struct class_device cdev;
55 * We do not have any private or other attrs.
57 struct transport_container conn_cont;
58 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
59 struct transport_container session_cont;
60 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
64 * list of registered transports and lock that must
65 * be held while accessing list. The iscsi_transport_lock must
66 * be acquired after the rx_queue_mutex.
68 static LIST_HEAD(iscsi_transports);
69 static DEFINE_SPINLOCK(iscsi_transport_lock);
71 #define to_iscsi_internal(tmpl) \
72 container_of(tmpl, struct iscsi_internal, t)
74 #define cdev_to_iscsi_internal(_cdev) \
75 container_of(_cdev, struct iscsi_internal, cdev)
77 static void iscsi_transport_release(struct class_device *cdev)
79 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
84 * iscsi_transport_class represents the iscsi_transports that are
87 static struct class iscsi_transport_class = {
88 .name = "iscsi_transport",
89 .release = iscsi_transport_release,
93 show_transport_handle(struct class_device *cdev, char *buf)
95 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
96 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
98 static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
100 #define show_transport_attr(name, format) \
102 show_transport_##name(struct class_device *cdev, char *buf) \
104 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
105 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
107 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
109 show_transport_attr(caps, "0x%x");
110 show_transport_attr(max_lun, "%d");
111 show_transport_attr(max_conn, "%d");
112 show_transport_attr(max_cmd_len, "%d");
114 static struct attribute *iscsi_transport_attrs[] = {
115 &class_device_attr_handle.attr,
116 &class_device_attr_caps.attr,
117 &class_device_attr_max_lun.attr,
118 &class_device_attr_max_conn.attr,
119 &class_device_attr_max_cmd_len.attr,
123 static struct attribute_group iscsi_transport_group = {
124 .attrs = iscsi_transport_attrs,
127 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
133 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
139 static struct sock *nls;
140 static int daemon_pid;
141 static DEFINE_MUTEX(rx_queue_mutex);
143 struct mempool_zone {
148 struct list_head freequeue;
152 static struct mempool_zone *z_reply;
155 * Z_MAX_* - actual mempool size allocated at the mempool_zone_init() time
156 * Z_HIWAT_* - zone's high watermark when if_error bit will be set to -ENOMEM
157 * so daemon will notice OOM on NETLINK tranposrt level and will
158 * be able to predict or change operational behavior
160 #define Z_MAX_REPLY 8
161 #define Z_HIWAT_REPLY 6
163 #define Z_HIWAT_PDU 6
164 #define Z_MAX_ERROR 16
165 #define Z_HIWAT_ERROR 12
167 static LIST_HEAD(connlist);
168 static DEFINE_SPINLOCK(connlock);
171 * The following functions can be used by LLDs that allocate
172 * their own scsi_hosts or by software iscsi LLDs
174 static void iscsi_session_release(struct device *dev)
176 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
177 struct iscsi_transport *transport = session->transport;
178 struct Scsi_Host *shost;
180 shost = iscsi_session_to_shost(session);
181 scsi_host_put(shost);
183 module_put(transport->owner);
186 static int iscsi_is_session_dev(const struct device *dev)
188 return dev->release == iscsi_session_release;
192 * iscsi_create_session - create iscsi class session
194 * @transport: iscsi transport
196 * This can be called from a LLD or iscsi_transport
198 struct iscsi_cls_session *
199 iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport)
201 struct iscsi_cls_session *session;
204 if (!try_module_get(transport->owner))
207 session = kzalloc(sizeof(*session), GFP_KERNEL);
210 session->transport = transport;
212 /* this is released in the dev's release function */
213 scsi_host_get(shost);
214 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u", shost->host_no);
215 session->dev.parent = &shost->shost_gendev;
216 session->dev.release = iscsi_session_release;
217 err = device_register(&session->dev);
219 dev_printk(KERN_ERR, &session->dev, "iscsi: could not "
220 "register session's dev\n");
223 transport_register_device(&session->dev);
230 module_put(transport->owner);
234 EXPORT_SYMBOL_GPL(iscsi_create_session);
237 * iscsi_destroy_session - destroy iscsi session
238 * @session: iscsi_session
240 * Can be called by a LLD or iscsi_transport. There must not be
241 * any running connections.
243 int iscsi_destroy_session(struct iscsi_cls_session *session)
245 transport_unregister_device(&session->dev);
246 device_unregister(&session->dev);
250 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
252 static void iscsi_conn_release(struct device *dev)
254 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
255 struct device *parent = conn->dev.parent;
261 static int iscsi_is_conn_dev(const struct device *dev)
263 return dev->release == iscsi_conn_release;
267 * iscsi_create_conn - create iscsi class connection
268 * @session: iscsi cls session
269 * @cid: connection id
271 * This can be called from a LLD or iscsi_transport. The connection
272 * is child of the session so cid must be unique for all connections
275 struct iscsi_cls_conn *
276 iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
278 struct iscsi_transport *transport = session->transport;
279 struct Scsi_Host *shost = iscsi_session_to_shost(session);
280 struct iscsi_cls_conn *conn;
283 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
287 if (transport->conndata_size)
288 conn->dd_data = &conn[1];
290 INIT_LIST_HEAD(&conn->conn_list);
291 conn->transport = transport;
293 /* this is released in the dev's release function */
294 if (!get_device(&session->dev))
296 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
297 shost->host_no, cid);
298 conn->dev.parent = &session->dev;
299 conn->dev.release = iscsi_conn_release;
300 err = device_register(&conn->dev);
302 dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register "
303 "connection's dev\n");
304 goto release_parent_ref;
306 transport_register_device(&conn->dev);
310 put_device(&session->dev);
316 EXPORT_SYMBOL_GPL(iscsi_create_conn);
319 * iscsi_destroy_conn - destroy iscsi class connection
320 * @session: iscsi cls session
322 * This can be called from a LLD or iscsi_transport.
324 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
326 transport_unregister_device(&conn->dev);
327 device_unregister(&conn->dev);
331 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
334 * These functions are used only by software iscsi_transports
335 * which do not allocate and more their scsi_hosts since this
336 * is initiated from userspace.
340 * iSCSI Session's hostdata organization:
342 * *------------------* <== hostdata_session(host->hostdata)
343 * | ptr to class sess|
344 * |------------------| <== iscsi_hostdata(host->hostdata)
345 * | transport's data |
346 * *------------------*
349 #define hostdata_privsize(_t) (sizeof(unsigned long) + _t->hostdata_size + \
350 _t->hostdata_size % sizeof(unsigned long))
352 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
355 * iscsi_transport_create_session - create iscsi cls session and host
356 * scsit: scsi transport template
357 * transport: iscsi transport template
359 * This can be used by software iscsi_transports that allocate
360 * a session per scsi host.
363 iscsi_transport_create_session(struct scsi_transport_template *scsit,
364 struct iscsi_transport *transport)
366 struct iscsi_cls_session *session;
367 struct Scsi_Host *shost;
369 shost = scsi_host_alloc(transport->host_template,
370 hostdata_privsize(transport));
372 printk(KERN_ERR "iscsi: can not allocate SCSI host for "
378 shost->max_channel = 0;
379 shost->max_lun = transport->max_lun;
380 shost->max_cmd_len = transport->max_cmd_len;
381 shost->transportt = scsit;
382 shost->transportt->create_work_queue = 1;
384 if (scsi_add_host(shost, NULL))
387 session = iscsi_create_session(shost, transport);
391 *(unsigned long*)shost->hostdata = (unsigned long)session;
395 scsi_remove_host(shost);
397 scsi_host_put(shost);
401 EXPORT_SYMBOL_GPL(iscsi_transport_create_session);
404 * iscsi_transport_destroy_session - destroy session and scsi host
407 * This can be used by software iscsi_transports that allocate
408 * a session per scsi host.
410 int iscsi_transport_destroy_session(struct Scsi_Host *shost)
412 struct iscsi_cls_session *session;
414 scsi_remove_host(shost);
415 session = hostdata_session(shost->hostdata);
416 iscsi_destroy_session(session);
417 /* ref from host alloc */
418 scsi_host_put(shost);
422 EXPORT_SYMBOL_GPL(iscsi_transport_destroy_session);
425 * iscsi interface functions
427 static struct iscsi_cls_conn*
428 iscsi_if_find_conn(uint64_t key)
431 struct iscsi_cls_conn *conn;
433 spin_lock_irqsave(&connlock, flags);
434 list_for_each_entry(conn, &connlist, conn_list)
435 if (conn->connh == key) {
436 spin_unlock_irqrestore(&connlock, flags);
439 spin_unlock_irqrestore(&connlock, flags);
443 static struct iscsi_internal *
444 iscsi_if_transport_lookup(struct iscsi_transport *tt)
446 struct iscsi_internal *priv;
449 spin_lock_irqsave(&iscsi_transport_lock, flags);
450 list_for_each_entry(priv, &iscsi_transports, list) {
451 if (tt == priv->iscsi_transport) {
452 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
456 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
460 static inline struct list_head *skb_to_lh(struct sk_buff *skb)
462 return (struct list_head *)&skb->cb;
466 mempool_zone_alloc_skb(unsigned int gfp_mask, void *pool_data)
468 struct mempool_zone *zone = pool_data;
470 return alloc_skb(zone->size, gfp_mask);
474 mempool_zone_free_skb(void *element, void *pool_data)
480 mempool_zone_complete(struct mempool_zone *zone)
483 struct list_head *lh, *n;
485 spin_lock_irqsave(&zone->freelock, flags);
486 list_for_each_safe(lh, n, &zone->freequeue) {
487 struct sk_buff *skb = (struct sk_buff *)((char *)lh -
488 offsetof(struct sk_buff, cb));
489 if (!skb_shared(skb)) {
490 list_del(skb_to_lh(skb));
491 mempool_free(skb, zone->pool);
492 atomic_dec(&zone->allocated);
495 spin_unlock_irqrestore(&zone->freelock, flags);
498 static struct mempool_zone *
499 mempool_zone_init(unsigned max, unsigned size, unsigned hiwat)
501 struct mempool_zone *zp;
503 zp = kzalloc(sizeof(*zp), GFP_KERNEL);
507 zp->pool = mempool_create(max, mempool_zone_alloc_skb,
508 mempool_zone_free_skb, zp);
517 INIT_LIST_HEAD(&zp->freequeue);
518 spin_lock_init(&zp->freelock);
519 atomic_set(&zp->allocated, 0);
524 static void mempool_zone_destroy(struct mempool_zone *zp)
526 mempool_destroy(zp->pool);
530 static struct sk_buff*
531 mempool_zone_get_skb(struct mempool_zone *zone)
535 skb = mempool_alloc(zone->pool, GFP_ATOMIC);
537 atomic_inc(&zone->allocated);
542 iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb)
548 rc = netlink_unicast(nls, skb, daemon_pid, MSG_DONTWAIT);
550 mempool_free(skb, zone->pool);
551 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
555 spin_lock_irqsave(&zone->freelock, flags);
556 list_add(skb_to_lh(skb), &zone->freequeue);
557 spin_unlock_irqrestore(&zone->freelock, flags);
562 int iscsi_recv_pdu(iscsi_connh_t connh, struct iscsi_hdr *hdr,
563 char *data, uint32_t data_size)
565 struct nlmsghdr *nlh;
567 struct iscsi_uevent *ev;
568 struct iscsi_cls_conn *conn;
570 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
573 conn = iscsi_if_find_conn(connh);
576 mempool_zone_complete(conn->z_pdu);
578 skb = mempool_zone_get_skb(conn->z_pdu);
580 iscsi_conn_error(connh, ISCSI_ERR_CONN_FAILED);
581 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver "
582 "control PDU: OOM\n");
586 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
587 ev = NLMSG_DATA(nlh);
588 memset(ev, 0, sizeof(*ev));
589 ev->transport_handle = iscsi_handle(conn->transport);
590 ev->type = ISCSI_KEVENT_RECV_PDU;
591 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
592 ev->iferror = -ENOMEM;
593 ev->r.recv_req.conn_handle = connh;
594 pdu = (char*)ev + sizeof(*ev);
595 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
596 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
598 return iscsi_unicast_skb(conn->z_pdu, skb);
600 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
602 void iscsi_conn_error(iscsi_connh_t connh, enum iscsi_err error)
604 struct nlmsghdr *nlh;
606 struct iscsi_uevent *ev;
607 struct iscsi_cls_conn *conn;
608 int len = NLMSG_SPACE(sizeof(*ev));
610 conn = iscsi_if_find_conn(connh);
613 mempool_zone_complete(conn->z_error);
615 skb = mempool_zone_get_skb(conn->z_error);
617 dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored "
618 "conn error (%d)\n", error);
622 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
623 ev = NLMSG_DATA(nlh);
624 ev->transport_handle = iscsi_handle(conn->transport);
625 ev->type = ISCSI_KEVENT_CONN_ERROR;
626 if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat)
627 ev->iferror = -ENOMEM;
628 ev->r.connerror.error = error;
629 ev->r.connerror.conn_handle = connh;
631 iscsi_unicast_skb(conn->z_error, skb);
633 dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n",
636 EXPORT_SYMBOL_GPL(iscsi_conn_error);
639 iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
640 void *payload, int size)
643 struct nlmsghdr *nlh;
644 int len = NLMSG_SPACE(size);
645 int flags = multi ? NLM_F_MULTI : 0;
646 int t = done ? NLMSG_DONE : type;
648 mempool_zone_complete(z_reply);
650 skb = mempool_zone_get_skb(z_reply);
653 * user is supposed to react on iferror == -ENOMEM;
658 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
659 nlh->nlmsg_flags = flags;
660 memcpy(NLMSG_DATA(nlh), payload, size);
661 return iscsi_unicast_skb(z_reply, skb);
665 iscsi_if_get_stats(struct iscsi_transport *transport, struct sk_buff *skb,
666 struct nlmsghdr *nlh)
668 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
669 struct iscsi_stats *stats;
670 struct sk_buff *skbstat;
671 struct iscsi_cls_conn *conn;
672 struct nlmsghdr *nlhstat;
673 struct iscsi_uevent *evstat;
674 int len = NLMSG_SPACE(sizeof(*ev) +
675 sizeof(struct iscsi_stats) +
676 sizeof(struct iscsi_stats_custom) *
677 ISCSI_STATS_CUSTOM_MAX);
680 conn = iscsi_if_find_conn(ev->u.get_stats.conn_handle);
687 mempool_zone_complete(conn->z_pdu);
689 skbstat = mempool_zone_get_skb(conn->z_pdu);
691 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not "
692 "deliver stats: OOM\n");
696 nlhstat = __nlmsg_put(skbstat, daemon_pid, 0, 0,
697 (len - sizeof(*nlhstat)), 0);
698 evstat = NLMSG_DATA(nlhstat);
699 memset(evstat, 0, sizeof(*evstat));
700 evstat->transport_handle = iscsi_handle(conn->transport);
701 evstat->type = nlh->nlmsg_type;
702 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
703 evstat->iferror = -ENOMEM;
704 evstat->u.get_stats.conn_handle =
705 ev->u.get_stats.conn_handle;
706 stats = (struct iscsi_stats *)
707 ((char*)evstat + sizeof(*evstat));
708 memset(stats, 0, sizeof(*stats));
710 transport->get_stats(ev->u.get_stats.conn_handle, stats);
711 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
712 sizeof(struct iscsi_stats) +
713 sizeof(struct iscsi_stats_custom) *
714 stats->custom_length);
715 actual_size -= sizeof(*nlhstat);
716 actual_size = NLMSG_LENGTH(actual_size);
717 skb_trim(skb, NLMSG_ALIGN(actual_size));
718 nlhstat->nlmsg_len = actual_size;
720 err = iscsi_unicast_skb(conn->z_pdu, skbstat);
721 } while (err < 0 && err != -ECONNREFUSED);
727 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
729 struct iscsi_transport *transport = priv->iscsi_transport;
730 struct Scsi_Host *shost;
732 if (!transport->create_session)
735 shost = transport->create_session(&priv->t,
736 ev->u.c_session.initial_cmdsn);
740 ev->r.c_session_ret.session_handle = iscsi_handle(iscsi_hostdata(shost->hostdata));
741 ev->r.c_session_ret.sid = shost->host_no;
746 iscsi_if_destroy_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
748 struct iscsi_transport *transport = priv->iscsi_transport;
750 struct Scsi_Host *shost;
752 if (!transport->destroy_session)
755 shost = scsi_host_lookup(ev->u.d_session.sid);
756 if (shost == ERR_PTR(-ENXIO))
759 if (transport->destroy_session)
760 transport->destroy_session(shost);
761 /* ref from host lookup */
762 scsi_host_put(shost);
767 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev){
768 struct Scsi_Host *shost;
769 struct iscsi_cls_conn *conn;
772 if (!transport->create_conn)
775 shost = scsi_host_lookup(ev->u.c_conn.sid);
776 if (shost == ERR_PTR(-ENXIO))
779 conn = transport->create_conn(shost, ev->u.c_conn.cid);
783 conn->z_pdu = mempool_zone_init(Z_MAX_PDU,
784 NLMSG_SPACE(sizeof(struct iscsi_uevent) +
785 sizeof(struct iscsi_hdr) +
786 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH),
789 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
790 "pdu zone for new conn\n");
794 conn->z_error = mempool_zone_init(Z_MAX_ERROR,
795 NLMSG_SPACE(sizeof(struct iscsi_uevent)),
797 if (!conn->z_error) {
798 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
799 "error zone for new conn\n");
803 ev->r.handle = conn->connh = iscsi_handle(conn->dd_data);
805 spin_lock_irqsave(&connlock, flags);
806 list_add(&conn->conn_list, &connlist);
808 spin_unlock_irqrestore(&connlock, flags);
810 scsi_host_put(shost);
814 mempool_zone_destroy(conn->z_pdu);
816 if (transport->destroy_conn)
817 transport->destroy_conn(conn->dd_data);
819 scsi_host_put(shost);
824 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
827 struct iscsi_cls_conn *conn;
828 struct mempool_zone *z_error, *z_pdu;
830 conn = iscsi_if_find_conn(ev->u.d_conn.conn_handle);
834 if (!transport->destroy_conn)
837 spin_lock_irqsave(&connlock, flags);
839 list_del(&conn->conn_list);
840 spin_unlock_irqrestore(&connlock, flags);
843 z_error = conn->z_error;
845 if (transport->destroy_conn)
846 transport->destroy_conn(conn);
848 mempool_zone_destroy(z_pdu);
849 mempool_zone_destroy(z_error);
855 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
858 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
859 struct iscsi_transport *transport = NULL;
860 struct iscsi_internal *priv;
862 if (NETLINK_CREDS(skb)->uid)
865 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
868 transport = priv->iscsi_transport;
870 daemon_pid = NETLINK_CREDS(skb)->pid;
872 switch (nlh->nlmsg_type) {
873 case ISCSI_UEVENT_CREATE_SESSION:
874 err = iscsi_if_create_session(priv, ev);
876 case ISCSI_UEVENT_DESTROY_SESSION:
877 err = iscsi_if_destroy_session(priv, ev);
879 case ISCSI_UEVENT_CREATE_CONN:
880 err = iscsi_if_create_conn(transport, ev);
882 case ISCSI_UEVENT_DESTROY_CONN:
883 err = iscsi_if_destroy_conn(transport, ev);
885 case ISCSI_UEVENT_BIND_CONN:
886 if (!iscsi_if_find_conn(ev->u.b_conn.conn_handle))
888 ev->r.retcode = transport->bind_conn(
889 ev->u.b_conn.session_handle,
890 ev->u.b_conn.conn_handle,
891 ev->u.b_conn.transport_fd,
892 ev->u.b_conn.is_leading);
894 case ISCSI_UEVENT_SET_PARAM:
895 if (!iscsi_if_find_conn(ev->u.set_param.conn_handle))
897 ev->r.retcode = transport->set_param(
898 ev->u.set_param.conn_handle,
899 ev->u.set_param.param, ev->u.set_param.value);
901 case ISCSI_UEVENT_START_CONN:
902 if (!iscsi_if_find_conn(ev->u.start_conn.conn_handle))
904 ev->r.retcode = transport->start_conn(
905 ev->u.start_conn.conn_handle);
907 case ISCSI_UEVENT_STOP_CONN:
908 if (!iscsi_if_find_conn(ev->u.stop_conn.conn_handle))
910 transport->stop_conn(ev->u.stop_conn.conn_handle,
911 ev->u.stop_conn.flag);
913 case ISCSI_UEVENT_SEND_PDU:
914 if (!iscsi_if_find_conn(ev->u.send_pdu.conn_handle))
916 ev->r.retcode = transport->send_pdu(
917 ev->u.send_pdu.conn_handle,
918 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
919 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
920 ev->u.send_pdu.data_size);
922 case ISCSI_UEVENT_GET_STATS:
923 err = iscsi_if_get_stats(transport, skb, nlh);
933 /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
934 * processed by iscsi_if_recv_msg. Malformed skbs with wrong length are
935 * discarded silently. */
937 iscsi_if_rx(struct sock *sk, int len)
941 mutex_lock(&rx_queue_mutex);
942 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
943 while (skb->len >= NLMSG_SPACE(0)) {
946 struct nlmsghdr *nlh;
947 struct iscsi_uevent *ev;
949 nlh = (struct nlmsghdr *)skb->data;
950 if (nlh->nlmsg_len < sizeof(*nlh) ||
951 skb->len < nlh->nlmsg_len) {
954 ev = NLMSG_DATA(nlh);
955 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
958 err = iscsi_if_recv_msg(skb, nlh);
960 ev->type = ISCSI_KEVENT_IF_ERROR;
965 * special case for GET_STATS:
966 * on success - sending reply and stats from
967 * inside of if_recv_msg(),
968 * on error - fall through.
970 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
972 err = iscsi_if_send_reply(
973 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
974 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
975 if (atomic_read(&z_reply->allocated) >=
977 ev->iferror = -ENOMEM;
978 } while (err < 0 && err != -ECONNREFUSED);
983 mutex_unlock(&rx_queue_mutex);
986 #define iscsi_cdev_to_conn(_cdev) \
987 iscsi_dev_to_conn(_cdev->dev)
990 * iSCSI connection attrs
992 #define iscsi_conn_int_attr_show(param, format) \
994 show_conn_int_param_##param(struct class_device *cdev, char *buf) \
996 uint32_t value = 0; \
997 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
998 struct iscsi_transport *t = conn->transport; \
1000 t->get_conn_param(conn->dd_data, param, &value); \
1001 return snprintf(buf, 20, format"\n", value); \
1004 #define iscsi_conn_int_attr(field, param, format) \
1005 iscsi_conn_int_attr_show(param, format) \
1006 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_conn_int_param_##param, NULL);
1008 iscsi_conn_int_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH, "%u");
1009 iscsi_conn_int_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH, "%u");
1010 iscsi_conn_int_attr(header_digest, ISCSI_PARAM_HDRDGST_EN, "%d");
1011 iscsi_conn_int_attr(data_digest, ISCSI_PARAM_DATADGST_EN, "%d");
1012 iscsi_conn_int_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN, "%d");
1013 iscsi_conn_int_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN, "%d");
1015 #define iscsi_cdev_to_session(_cdev) \
1016 iscsi_dev_to_session(_cdev->dev)
1019 * iSCSI session attrs
1021 #define iscsi_session_int_attr_show(param, format) \
1023 show_session_int_param_##param(struct class_device *cdev, char *buf) \
1025 uint32_t value = 0; \
1026 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1027 struct Scsi_Host *shost = iscsi_session_to_shost(session); \
1028 struct iscsi_transport *t = session->transport; \
1030 t->get_session_param(shost, param, &value); \
1031 return snprintf(buf, 20, format"\n", value); \
1034 #define iscsi_session_int_attr(field, param, format) \
1035 iscsi_session_int_attr_show(param, format) \
1036 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_session_int_param_##param, NULL);
1038 iscsi_session_int_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, "%d");
1039 iscsi_session_int_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, "%hu");
1040 iscsi_session_int_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, "%d");
1041 iscsi_session_int_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, "%u");
1042 iscsi_session_int_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, "%u");
1043 iscsi_session_int_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, "%d");
1044 iscsi_session_int_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, "%d");
1045 iscsi_session_int_attr(erl, ISCSI_PARAM_ERL, "%d");
1047 #define SETUP_SESSION_RD_ATTR(field, param) \
1048 if (priv->param_mask & (1 << param)) { \
1049 priv->session_attrs[count] = &class_device_attr_##field;\
1053 #define SETUP_CONN_RD_ATTR(field, param) \
1054 if (priv->param_mask & (1 << param)) { \
1055 priv->conn_attrs[count] = &class_device_attr_##field; \
1059 static int iscsi_session_match(struct attribute_container *cont,
1062 struct iscsi_cls_session *session;
1063 struct Scsi_Host *shost;
1064 struct iscsi_internal *priv;
1066 if (!iscsi_is_session_dev(dev))
1069 session = iscsi_dev_to_session(dev);
1070 shost = iscsi_session_to_shost(session);
1071 if (!shost->transportt)
1074 priv = to_iscsi_internal(shost->transportt);
1075 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1078 return &priv->session_cont.ac == cont;
1081 static int iscsi_conn_match(struct attribute_container *cont,
1084 struct iscsi_cls_session *session;
1085 struct iscsi_cls_conn *conn;
1086 struct Scsi_Host *shost;
1087 struct iscsi_internal *priv;
1089 if (!iscsi_is_conn_dev(dev))
1092 conn = iscsi_dev_to_conn(dev);
1093 session = iscsi_dev_to_session(conn->dev.parent);
1094 shost = iscsi_session_to_shost(session);
1096 if (!shost->transportt)
1099 priv = to_iscsi_internal(shost->transportt);
1100 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1103 return &priv->conn_cont.ac == cont;
1106 struct scsi_transport_template *
1107 iscsi_register_transport(struct iscsi_transport *tt)
1109 struct iscsi_internal *priv;
1110 unsigned long flags;
1115 priv = iscsi_if_transport_lookup(tt);
1119 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
1122 memset(priv, 0, sizeof(*priv));
1123 INIT_LIST_HEAD(&priv->list);
1124 INIT_LIST_HEAD(&priv->sessions);
1125 priv->iscsi_transport = tt;
1127 priv->cdev.class = &iscsi_transport_class;
1128 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1129 err = class_device_register(&priv->cdev);
1133 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1135 goto unregister_cdev;
1137 /* setup parameters mask */
1138 priv->param_mask = 0xFFFFFFFF;
1139 if (!(tt->caps & CAP_MULTI_R2T))
1140 priv->param_mask &= ~(1 << ISCSI_PARAM_MAX_R2T);
1141 if (!(tt->caps & CAP_HDRDGST))
1142 priv->param_mask &= ~(1 << ISCSI_PARAM_HDRDGST_EN);
1143 if (!(tt->caps & CAP_DATADGST))
1144 priv->param_mask &= ~(1 << ISCSI_PARAM_DATADGST_EN);
1145 if (!(tt->caps & CAP_MARKERS)) {
1146 priv->param_mask &= ~(1 << ISCSI_PARAM_IFMARKER_EN);
1147 priv->param_mask &= ~(1 << ISCSI_PARAM_OFMARKER_EN);
1150 /* connection parameters */
1151 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1152 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1153 priv->conn_cont.ac.match = iscsi_conn_match;
1154 transport_container_register(&priv->conn_cont);
1156 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1157 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1158 SETUP_CONN_RD_ATTR(header_digest, ISCSI_PARAM_HDRDGST_EN);
1159 SETUP_CONN_RD_ATTR(data_digest, ISCSI_PARAM_DATADGST_EN);
1160 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1161 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1163 BUG_ON(count > ISCSI_CONN_ATTRS);
1164 priv->conn_attrs[count] = NULL;
1167 /* session parameters */
1168 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1169 priv->session_cont.ac.class = &iscsi_session_class.class;
1170 priv->session_cont.ac.match = iscsi_session_match;
1171 transport_container_register(&priv->session_cont);
1173 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN);
1174 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T);
1175 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_PARAM_IMM_DATA_EN);
1176 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_PARAM_FIRST_BURST);
1177 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_PARAM_MAX_BURST);
1178 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN);
1179 SETUP_SESSION_RD_ATTR(data_seq_in_order,ISCSI_PARAM_DATASEQ_INORDER_EN)
1180 SETUP_SESSION_RD_ATTR(erl, ISCSI_PARAM_ERL);
1182 BUG_ON(count > ISCSI_SESSION_ATTRS);
1183 priv->session_attrs[count] = NULL;
1185 spin_lock_irqsave(&iscsi_transport_lock, flags);
1186 list_add(&priv->list, &iscsi_transports);
1187 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1189 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1193 class_device_unregister(&priv->cdev);
1198 EXPORT_SYMBOL_GPL(iscsi_register_transport);
1200 int iscsi_unregister_transport(struct iscsi_transport *tt)
1202 struct iscsi_internal *priv;
1203 unsigned long flags;
1207 mutex_lock(&rx_queue_mutex);
1209 priv = iscsi_if_transport_lookup(tt);
1212 spin_lock_irqsave(&iscsi_transport_lock, flags);
1213 list_del(&priv->list);
1214 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1216 transport_container_unregister(&priv->conn_cont);
1217 transport_container_unregister(&priv->session_cont);
1219 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1220 class_device_unregister(&priv->cdev);
1221 mutex_unlock(&rx_queue_mutex);
1225 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1228 iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr)
1230 struct netlink_notify *n = ptr;
1232 if (event == NETLINK_URELEASE &&
1233 n->protocol == NETLINK_ISCSI && n->pid) {
1234 struct iscsi_cls_conn *conn;
1235 unsigned long flags;
1237 mempool_zone_complete(z_reply);
1238 spin_lock_irqsave(&connlock, flags);
1239 list_for_each_entry(conn, &connlist, conn_list) {
1240 mempool_zone_complete(conn->z_error);
1241 mempool_zone_complete(conn->z_pdu);
1243 spin_unlock_irqrestore(&connlock, flags);
1249 static struct notifier_block iscsi_nl_notifier = {
1250 .notifier_call = iscsi_rcv_nl_event,
1253 static __init int iscsi_transport_init(void)
1257 err = class_register(&iscsi_transport_class);
1261 err = transport_class_register(&iscsi_connection_class);
1263 goto unregister_transport_class;
1265 err = transport_class_register(&iscsi_session_class);
1267 goto unregister_conn_class;
1269 err = netlink_register_notifier(&iscsi_nl_notifier);
1271 goto unregister_session_class;
1273 nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx,
1277 goto unregister_notifier;
1280 z_reply = mempool_zone_init(Z_MAX_REPLY,
1281 NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY);
1285 sock_release(nls->sk_socket);
1286 unregister_notifier:
1287 netlink_unregister_notifier(&iscsi_nl_notifier);
1288 unregister_session_class:
1289 transport_class_unregister(&iscsi_session_class);
1290 unregister_conn_class:
1291 transport_class_unregister(&iscsi_connection_class);
1292 unregister_transport_class:
1293 class_unregister(&iscsi_transport_class);
1297 static void __exit iscsi_transport_exit(void)
1299 mempool_zone_destroy(z_reply);
1300 sock_release(nls->sk_socket);
1301 netlink_unregister_notifier(&iscsi_nl_notifier);
1302 transport_class_unregister(&iscsi_connection_class);
1303 transport_class_unregister(&iscsi_session_class);
1304 class_unregister(&iscsi_transport_class);
1307 module_init(iscsi_transport_init);
1308 module_exit(iscsi_transport_exit);
1310 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1311 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1312 "Alex Aizman <itn780@yahoo.com>");
1313 MODULE_DESCRIPTION("iSCSI Transport Interface");
1314 MODULE_LICENSE("GPL");