Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / scsi / scsi_transport_iscsi.c
blob2730d507e585e511bf368b2d67428a3a516643bd
1 /*
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>
26 #include <net/tcp.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 * based on transport capabilities, at register time we set these
43 * bits to tell the transport class it wants attributes displayed
44 * in sysfs or that it can support different iSCSI Data-Path
45 * capabilities
47 uint32_t param_mask;
49 struct class_device cdev;
51 * We do not have any private or other attrs.
53 struct transport_container conn_cont;
54 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
55 struct transport_container session_cont;
56 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
60 * list of registered transports and lock that must
61 * be held while accessing list. The iscsi_transport_lock must
62 * be acquired after the rx_queue_mutex.
64 static LIST_HEAD(iscsi_transports);
65 static DEFINE_SPINLOCK(iscsi_transport_lock);
67 #define to_iscsi_internal(tmpl) \
68 container_of(tmpl, struct iscsi_internal, t)
70 #define cdev_to_iscsi_internal(_cdev) \
71 container_of(_cdev, struct iscsi_internal, cdev)
73 static void iscsi_transport_release(struct class_device *cdev)
75 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
76 kfree(priv);
80 * iscsi_transport_class represents the iscsi_transports that are
81 * registered.
83 static struct class iscsi_transport_class = {
84 .name = "iscsi_transport",
85 .release = iscsi_transport_release,
88 static ssize_t
89 show_transport_handle(struct class_device *cdev, char *buf)
91 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
92 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
94 static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
96 #define show_transport_attr(name, format) \
97 static ssize_t \
98 show_transport_##name(struct class_device *cdev, char *buf) \
99 { \
100 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
101 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
103 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
105 show_transport_attr(caps, "0x%x");
106 show_transport_attr(max_lun, "%d");
107 show_transport_attr(max_conn, "%d");
108 show_transport_attr(max_cmd_len, "%d");
110 static struct attribute *iscsi_transport_attrs[] = {
111 &class_device_attr_handle.attr,
112 &class_device_attr_caps.attr,
113 &class_device_attr_max_lun.attr,
114 &class_device_attr_max_conn.attr,
115 &class_device_attr_max_cmd_len.attr,
116 NULL,
119 static struct attribute_group iscsi_transport_group = {
120 .attrs = iscsi_transport_attrs,
123 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
124 "iscsi_session",
125 NULL,
126 NULL,
127 NULL);
129 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
130 "iscsi_connection",
131 NULL,
132 NULL,
133 NULL);
135 static struct sock *nls;
136 static int daemon_pid;
137 static DEFINE_MUTEX(rx_queue_mutex);
139 struct mempool_zone {
140 mempool_t *pool;
141 atomic_t allocated;
142 int size;
143 int hiwat;
144 struct list_head freequeue;
145 spinlock_t freelock;
148 static struct mempool_zone *z_reply;
151 * Z_MAX_* - actual mempool size allocated at the mempool_zone_init() time
152 * Z_HIWAT_* - zone's high watermark when if_error bit will be set to -ENOMEM
153 * so daemon will notice OOM on NETLINK tranposrt level and will
154 * be able to predict or change operational behavior
156 #define Z_MAX_REPLY 8
157 #define Z_HIWAT_REPLY 6
158 #define Z_MAX_PDU 8
159 #define Z_HIWAT_PDU 6
160 #define Z_MAX_ERROR 16
161 #define Z_HIWAT_ERROR 12
163 static LIST_HEAD(sesslist);
164 static DEFINE_SPINLOCK(sesslock);
165 static LIST_HEAD(connlist);
166 static DEFINE_SPINLOCK(connlock);
168 static struct iscsi_cls_session *iscsi_session_lookup(uint64_t handle)
170 unsigned long flags;
171 struct iscsi_cls_session *sess;
173 spin_lock_irqsave(&sesslock, flags);
174 list_for_each_entry(sess, &sesslist, sess_list) {
175 if (sess == iscsi_ptr(handle)) {
176 spin_unlock_irqrestore(&sesslock, flags);
177 return sess;
180 spin_unlock_irqrestore(&sesslock, flags);
181 return NULL;
184 static struct iscsi_cls_conn *iscsi_conn_lookup(uint64_t handle)
186 unsigned long flags;
187 struct iscsi_cls_conn *conn;
189 spin_lock_irqsave(&connlock, flags);
190 list_for_each_entry(conn, &connlist, conn_list) {
191 if (conn == iscsi_ptr(handle)) {
192 spin_unlock_irqrestore(&connlock, flags);
193 return conn;
196 spin_unlock_irqrestore(&connlock, flags);
197 return NULL;
201 * The following functions can be used by LLDs that allocate
202 * their own scsi_hosts or by software iscsi LLDs
204 static void iscsi_session_release(struct device *dev)
206 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
207 struct iscsi_transport *transport = session->transport;
208 struct Scsi_Host *shost;
210 shost = iscsi_session_to_shost(session);
211 scsi_host_put(shost);
212 kfree(session);
213 module_put(transport->owner);
216 static int iscsi_is_session_dev(const struct device *dev)
218 return dev->release == iscsi_session_release;
222 * iscsi_create_session - create iscsi class session
223 * @shost: scsi host
224 * @transport: iscsi transport
226 * This can be called from a LLD or iscsi_transport
228 struct iscsi_cls_session *
229 iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport)
231 struct iscsi_cls_session *session;
232 int err;
234 if (!try_module_get(transport->owner))
235 return NULL;
237 session = kzalloc(sizeof(*session), GFP_KERNEL);
238 if (!session)
239 goto module_put;
240 session->transport = transport;
242 /* this is released in the dev's release function */
243 scsi_host_get(shost);
244 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u", shost->host_no);
245 session->dev.parent = &shost->shost_gendev;
246 session->dev.release = iscsi_session_release;
247 err = device_register(&session->dev);
248 if (err) {
249 dev_printk(KERN_ERR, &session->dev, "iscsi: could not "
250 "register session's dev\n");
251 goto free_session;
253 transport_register_device(&session->dev);
255 return session;
257 free_session:
258 kfree(session);
259 module_put:
260 module_put(transport->owner);
261 return NULL;
264 EXPORT_SYMBOL_GPL(iscsi_create_session);
267 * iscsi_destroy_session - destroy iscsi session
268 * @session: iscsi_session
270 * Can be called by a LLD or iscsi_transport. There must not be
271 * any running connections.
273 int iscsi_destroy_session(struct iscsi_cls_session *session)
275 transport_unregister_device(&session->dev);
276 device_unregister(&session->dev);
277 return 0;
280 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
282 static void iscsi_conn_release(struct device *dev)
284 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
285 struct device *parent = conn->dev.parent;
287 kfree(conn);
288 put_device(parent);
291 static int iscsi_is_conn_dev(const struct device *dev)
293 return dev->release == iscsi_conn_release;
297 * iscsi_create_conn - create iscsi class connection
298 * @session: iscsi cls session
299 * @cid: connection id
301 * This can be called from a LLD or iscsi_transport. The connection
302 * is child of the session so cid must be unique for all connections
303 * on the session.
305 struct iscsi_cls_conn *
306 iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
308 struct iscsi_transport *transport = session->transport;
309 struct Scsi_Host *shost = iscsi_session_to_shost(session);
310 struct iscsi_cls_conn *conn;
311 int err;
313 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
314 if (!conn)
315 return NULL;
317 if (transport->conndata_size)
318 conn->dd_data = &conn[1];
320 INIT_LIST_HEAD(&conn->conn_list);
321 conn->transport = transport;
323 /* this is released in the dev's release function */
324 if (!get_device(&session->dev))
325 goto free_conn;
326 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
327 shost->host_no, cid);
328 conn->dev.parent = &session->dev;
329 conn->dev.release = iscsi_conn_release;
330 err = device_register(&conn->dev);
331 if (err) {
332 dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register "
333 "connection's dev\n");
334 goto release_parent_ref;
336 transport_register_device(&conn->dev);
337 return conn;
339 release_parent_ref:
340 put_device(&session->dev);
341 free_conn:
342 kfree(conn);
343 return NULL;
346 EXPORT_SYMBOL_GPL(iscsi_create_conn);
349 * iscsi_destroy_conn - destroy iscsi class connection
350 * @session: iscsi cls session
352 * This can be called from a LLD or iscsi_transport.
354 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
356 transport_unregister_device(&conn->dev);
357 device_unregister(&conn->dev);
358 return 0;
361 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
364 * These functions are used only by software iscsi_transports
365 * which do not allocate and more their scsi_hosts since this
366 * is initiated from userspace.
370 * iSCSI Session's hostdata organization:
372 * *------------------* <== hostdata_session(host->hostdata)
373 * | ptr to class sess|
374 * |------------------| <== iscsi_hostdata(host->hostdata)
375 * | transport's data |
376 * *------------------*
379 #define hostdata_privsize(_t) (sizeof(unsigned long) + _t->hostdata_size + \
380 _t->hostdata_size % sizeof(unsigned long))
382 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
385 * iscsi_transport_create_session - create iscsi cls session and host
386 * scsit: scsi transport template
387 * transport: iscsi transport template
389 * This can be used by software iscsi_transports that allocate
390 * a session per scsi host.
392 struct Scsi_Host *
393 iscsi_transport_create_session(struct scsi_transport_template *scsit,
394 struct iscsi_transport *transport)
396 struct iscsi_cls_session *session;
397 struct Scsi_Host *shost;
398 unsigned long flags;
400 shost = scsi_host_alloc(transport->host_template,
401 hostdata_privsize(transport));
402 if (!shost) {
403 printk(KERN_ERR "iscsi: can not allocate SCSI host for "
404 "session\n");
405 return NULL;
408 shost->max_id = 1;
409 shost->max_channel = 0;
410 shost->max_lun = transport->max_lun;
411 shost->max_cmd_len = transport->max_cmd_len;
412 shost->transportt = scsit;
413 shost->transportt->create_work_queue = 1;
415 if (scsi_add_host(shost, NULL))
416 goto free_host;
418 session = iscsi_create_session(shost, transport);
419 if (!session)
420 goto remove_host;
422 *(unsigned long*)shost->hostdata = (unsigned long)session;
423 spin_lock_irqsave(&sesslock, flags);
424 list_add(&session->sess_list, &sesslist);
425 spin_unlock_irqrestore(&sesslock, flags);
426 return shost;
428 remove_host:
429 scsi_remove_host(shost);
430 free_host:
431 scsi_host_put(shost);
432 return NULL;
435 EXPORT_SYMBOL_GPL(iscsi_transport_create_session);
438 * iscsi_transport_destroy_session - destroy session and scsi host
439 * shost: scsi host
441 * This can be used by software iscsi_transports that allocate
442 * a session per scsi host.
444 int iscsi_transport_destroy_session(struct Scsi_Host *shost)
446 struct iscsi_cls_session *session;
447 unsigned long flags;
449 scsi_remove_host(shost);
450 session = hostdata_session(shost->hostdata);
451 spin_lock_irqsave(&sesslock, flags);
452 list_del(&session->sess_list);
453 spin_unlock_irqrestore(&sesslock, flags);
454 iscsi_destroy_session(session);
455 /* ref from host alloc */
456 scsi_host_put(shost);
457 return 0;
460 EXPORT_SYMBOL_GPL(iscsi_transport_destroy_session);
463 * iscsi interface functions
465 static struct iscsi_internal *
466 iscsi_if_transport_lookup(struct iscsi_transport *tt)
468 struct iscsi_internal *priv;
469 unsigned long flags;
471 spin_lock_irqsave(&iscsi_transport_lock, flags);
472 list_for_each_entry(priv, &iscsi_transports, list) {
473 if (tt == priv->iscsi_transport) {
474 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
475 return priv;
478 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
479 return NULL;
482 static inline struct list_head *skb_to_lh(struct sk_buff *skb)
484 return (struct list_head *)&skb->cb;
487 static void*
488 mempool_zone_alloc_skb(gfp_t gfp_mask, void *pool_data)
490 struct mempool_zone *zone = pool_data;
492 return alloc_skb(zone->size, gfp_mask);
495 static void
496 mempool_zone_free_skb(void *element, void *pool_data)
498 kfree_skb(element);
501 static void
502 mempool_zone_complete(struct mempool_zone *zone)
504 unsigned long flags;
505 struct list_head *lh, *n;
507 spin_lock_irqsave(&zone->freelock, flags);
508 list_for_each_safe(lh, n, &zone->freequeue) {
509 struct sk_buff *skb = (struct sk_buff *)((char *)lh -
510 offsetof(struct sk_buff, cb));
511 if (!skb_shared(skb)) {
512 list_del(skb_to_lh(skb));
513 mempool_free(skb, zone->pool);
514 atomic_dec(&zone->allocated);
517 spin_unlock_irqrestore(&zone->freelock, flags);
520 static struct mempool_zone *
521 mempool_zone_init(unsigned max, unsigned size, unsigned hiwat)
523 struct mempool_zone *zp;
525 zp = kzalloc(sizeof(*zp), GFP_KERNEL);
526 if (!zp)
527 return NULL;
529 zp->size = size;
530 zp->hiwat = hiwat;
531 INIT_LIST_HEAD(&zp->freequeue);
532 spin_lock_init(&zp->freelock);
533 atomic_set(&zp->allocated, 0);
535 zp->pool = mempool_create(max, mempool_zone_alloc_skb,
536 mempool_zone_free_skb, zp);
537 if (!zp->pool) {
538 kfree(zp);
539 return NULL;
542 return zp;
545 static void mempool_zone_destroy(struct mempool_zone *zp)
547 mempool_destroy(zp->pool);
548 kfree(zp);
551 static struct sk_buff*
552 mempool_zone_get_skb(struct mempool_zone *zone)
554 struct sk_buff *skb;
556 skb = mempool_alloc(zone->pool, GFP_ATOMIC);
557 if (skb)
558 atomic_inc(&zone->allocated);
559 return skb;
562 static int
563 iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb)
565 unsigned long flags;
566 int rc;
568 skb_get(skb);
569 rc = netlink_unicast(nls, skb, daemon_pid, MSG_DONTWAIT);
570 if (rc < 0) {
571 mempool_free(skb, zone->pool);
572 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
573 return rc;
576 spin_lock_irqsave(&zone->freelock, flags);
577 list_add(skb_to_lh(skb), &zone->freequeue);
578 spin_unlock_irqrestore(&zone->freelock, flags);
580 return 0;
583 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
584 char *data, uint32_t data_size)
586 struct nlmsghdr *nlh;
587 struct sk_buff *skb;
588 struct iscsi_uevent *ev;
589 char *pdu;
590 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
591 data_size);
593 mempool_zone_complete(conn->z_pdu);
595 skb = mempool_zone_get_skb(conn->z_pdu);
596 if (!skb) {
597 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
598 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver "
599 "control PDU: OOM\n");
600 return -ENOMEM;
603 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
604 ev = NLMSG_DATA(nlh);
605 memset(ev, 0, sizeof(*ev));
606 ev->transport_handle = iscsi_handle(conn->transport);
607 ev->type = ISCSI_KEVENT_RECV_PDU;
608 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
609 ev->iferror = -ENOMEM;
610 ev->r.recv_req.conn_handle = iscsi_handle(conn);
611 pdu = (char*)ev + sizeof(*ev);
612 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
613 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
615 return iscsi_unicast_skb(conn->z_pdu, skb);
617 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
619 void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
621 struct nlmsghdr *nlh;
622 struct sk_buff *skb;
623 struct iscsi_uevent *ev;
624 int len = NLMSG_SPACE(sizeof(*ev));
626 mempool_zone_complete(conn->z_error);
628 skb = mempool_zone_get_skb(conn->z_error);
629 if (!skb) {
630 dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored "
631 "conn error (%d)\n", error);
632 return;
635 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
636 ev = NLMSG_DATA(nlh);
637 ev->transport_handle = iscsi_handle(conn->transport);
638 ev->type = ISCSI_KEVENT_CONN_ERROR;
639 if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat)
640 ev->iferror = -ENOMEM;
641 ev->r.connerror.error = error;
642 ev->r.connerror.conn_handle = iscsi_handle(conn);
644 iscsi_unicast_skb(conn->z_error, skb);
646 dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n",
647 error);
649 EXPORT_SYMBOL_GPL(iscsi_conn_error);
651 static int
652 iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
653 void *payload, int size)
655 struct sk_buff *skb;
656 struct nlmsghdr *nlh;
657 int len = NLMSG_SPACE(size);
658 int flags = multi ? NLM_F_MULTI : 0;
659 int t = done ? NLMSG_DONE : type;
661 mempool_zone_complete(z_reply);
663 skb = mempool_zone_get_skb(z_reply);
665 * FIXME:
666 * user is supposed to react on iferror == -ENOMEM;
667 * see iscsi_if_rx().
669 BUG_ON(!skb);
671 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
672 nlh->nlmsg_flags = flags;
673 memcpy(NLMSG_DATA(nlh), payload, size);
674 return iscsi_unicast_skb(z_reply, skb);
677 static int
678 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
680 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
681 struct iscsi_stats *stats;
682 struct sk_buff *skbstat;
683 struct iscsi_cls_conn *conn;
684 struct nlmsghdr *nlhstat;
685 struct iscsi_uevent *evstat;
686 int len = NLMSG_SPACE(sizeof(*ev) +
687 sizeof(struct iscsi_stats) +
688 sizeof(struct iscsi_stats_custom) *
689 ISCSI_STATS_CUSTOM_MAX);
690 int err = 0;
692 conn = iscsi_conn_lookup(ev->u.get_stats.conn_handle);
693 if (!conn)
694 return -EEXIST;
696 do {
697 int actual_size;
699 mempool_zone_complete(conn->z_pdu);
701 skbstat = mempool_zone_get_skb(conn->z_pdu);
702 if (!skbstat) {
703 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not "
704 "deliver stats: OOM\n");
705 return -ENOMEM;
708 nlhstat = __nlmsg_put(skbstat, daemon_pid, 0, 0,
709 (len - sizeof(*nlhstat)), 0);
710 evstat = NLMSG_DATA(nlhstat);
711 memset(evstat, 0, sizeof(*evstat));
712 evstat->transport_handle = iscsi_handle(conn->transport);
713 evstat->type = nlh->nlmsg_type;
714 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
715 evstat->iferror = -ENOMEM;
716 evstat->u.get_stats.conn_handle =
717 ev->u.get_stats.conn_handle;
718 stats = (struct iscsi_stats *)
719 ((char*)evstat + sizeof(*evstat));
720 memset(stats, 0, sizeof(*stats));
722 transport->get_stats(conn, stats);
723 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
724 sizeof(struct iscsi_stats) +
725 sizeof(struct iscsi_stats_custom) *
726 stats->custom_length);
727 actual_size -= sizeof(*nlhstat);
728 actual_size = NLMSG_LENGTH(actual_size);
729 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
730 nlhstat->nlmsg_len = actual_size;
732 err = iscsi_unicast_skb(conn->z_pdu, skbstat);
733 } while (err < 0 && err != -ECONNREFUSED);
735 return err;
738 static int
739 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
741 struct iscsi_transport *transport = priv->iscsi_transport;
742 struct iscsi_cls_session *session;
743 uint32_t sid;
745 session = transport->create_session(&priv->t,
746 ev->u.c_session.initial_cmdsn,
747 &sid);
748 if (!session)
749 return -ENOMEM;
751 ev->r.c_session_ret.session_handle = iscsi_handle(session);
752 ev->r.c_session_ret.sid = sid;
753 return 0;
756 static int
757 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
759 struct iscsi_cls_conn *conn;
760 struct iscsi_cls_session *session;
761 unsigned long flags;
763 session = iscsi_session_lookup(ev->u.c_conn.session_handle);
764 if (!session)
765 return -EINVAL;
767 conn = transport->create_conn(session, ev->u.c_conn.cid);
768 if (!conn)
769 return -ENOMEM;
771 conn->z_pdu = mempool_zone_init(Z_MAX_PDU,
772 NLMSG_SPACE(sizeof(struct iscsi_uevent) +
773 sizeof(struct iscsi_hdr) +
774 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH),
775 Z_HIWAT_PDU);
776 if (!conn->z_pdu) {
777 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
778 "pdu zone for new conn\n");
779 goto destroy_conn;
782 conn->z_error = mempool_zone_init(Z_MAX_ERROR,
783 NLMSG_SPACE(sizeof(struct iscsi_uevent)),
784 Z_HIWAT_ERROR);
785 if (!conn->z_error) {
786 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
787 "error zone for new conn\n");
788 goto free_pdu_pool;
791 ev->r.handle = iscsi_handle(conn);
793 spin_lock_irqsave(&connlock, flags);
794 list_add(&conn->conn_list, &connlist);
795 conn->active = 1;
796 spin_unlock_irqrestore(&connlock, flags);
798 return 0;
800 free_pdu_pool:
801 mempool_zone_destroy(conn->z_pdu);
802 destroy_conn:
803 if (transport->destroy_conn)
804 transport->destroy_conn(conn->dd_data);
805 return -ENOMEM;
808 static int
809 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
811 unsigned long flags;
812 struct iscsi_cls_conn *conn;
813 struct mempool_zone *z_error, *z_pdu;
815 conn = iscsi_conn_lookup(ev->u.d_conn.conn_handle);
816 if (!conn)
817 return -EINVAL;
818 spin_lock_irqsave(&connlock, flags);
819 conn->active = 0;
820 list_del(&conn->conn_list);
821 spin_unlock_irqrestore(&connlock, flags);
823 z_pdu = conn->z_pdu;
824 z_error = conn->z_error;
826 if (transport->destroy_conn)
827 transport->destroy_conn(conn);
829 mempool_zone_destroy(z_pdu);
830 mempool_zone_destroy(z_error);
832 return 0;
835 static int
836 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
838 int err = 0;
839 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
840 struct iscsi_transport *transport = NULL;
841 struct iscsi_internal *priv;
842 struct iscsi_cls_session *session;
843 struct iscsi_cls_conn *conn;
845 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
846 if (!priv)
847 return -EINVAL;
848 transport = priv->iscsi_transport;
850 if (!try_module_get(transport->owner))
851 return -EINVAL;
853 switch (nlh->nlmsg_type) {
854 case ISCSI_UEVENT_CREATE_SESSION:
855 err = iscsi_if_create_session(priv, ev);
856 break;
857 case ISCSI_UEVENT_DESTROY_SESSION:
858 session = iscsi_session_lookup(ev->u.d_session.session_handle);
859 if (session)
860 transport->destroy_session(session);
861 else
862 err = -EINVAL;
863 break;
864 case ISCSI_UEVENT_CREATE_CONN:
865 err = iscsi_if_create_conn(transport, ev);
866 break;
867 case ISCSI_UEVENT_DESTROY_CONN:
868 err = iscsi_if_destroy_conn(transport, ev);
869 break;
870 case ISCSI_UEVENT_BIND_CONN:
871 session = iscsi_session_lookup(ev->u.b_conn.session_handle);
872 conn = iscsi_conn_lookup(ev->u.b_conn.conn_handle);
874 if (session && conn)
875 ev->r.retcode = transport->bind_conn(session, conn,
876 ev->u.b_conn.transport_fd,
877 ev->u.b_conn.is_leading);
878 else
879 err = -EINVAL;
880 break;
881 case ISCSI_UEVENT_SET_PARAM:
882 conn = iscsi_conn_lookup(ev->u.set_param.conn_handle);
883 if (conn)
884 ev->r.retcode = transport->set_param(conn,
885 ev->u.set_param.param, ev->u.set_param.value);
886 else
887 err = -EINVAL;
888 break;
889 case ISCSI_UEVENT_START_CONN:
890 conn = iscsi_conn_lookup(ev->u.start_conn.conn_handle);
891 if (conn)
892 ev->r.retcode = transport->start_conn(conn);
893 else
894 err = -EINVAL;
896 break;
897 case ISCSI_UEVENT_STOP_CONN:
898 conn = iscsi_conn_lookup(ev->u.stop_conn.conn_handle);
899 if (conn)
900 transport->stop_conn(conn, ev->u.stop_conn.flag);
901 else
902 err = -EINVAL;
903 break;
904 case ISCSI_UEVENT_SEND_PDU:
905 conn = iscsi_conn_lookup(ev->u.send_pdu.conn_handle);
906 if (conn)
907 ev->r.retcode = transport->send_pdu(conn,
908 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
909 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
910 ev->u.send_pdu.data_size);
911 else
912 err = -EINVAL;
913 break;
914 case ISCSI_UEVENT_GET_STATS:
915 err = iscsi_if_get_stats(transport, nlh);
916 break;
917 default:
918 err = -EINVAL;
919 break;
922 module_put(transport->owner);
923 return err;
926 /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
927 * processed by iscsi_if_recv_msg. Malformed skbs with wrong length are
928 * or invalid creds discarded silently. */
929 static void
930 iscsi_if_rx(struct sock *sk, int len)
932 struct sk_buff *skb;
934 mutex_lock(&rx_queue_mutex);
935 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
936 if (NETLINK_CREDS(skb)->uid) {
937 skb_pull(skb, skb->len);
938 goto free_skb;
940 daemon_pid = NETLINK_CREDS(skb)->pid;
942 while (skb->len >= NLMSG_SPACE(0)) {
943 int err;
944 uint32_t rlen;
945 struct nlmsghdr *nlh;
946 struct iscsi_uevent *ev;
948 nlh = (struct nlmsghdr *)skb->data;
949 if (nlh->nlmsg_len < sizeof(*nlh) ||
950 skb->len < nlh->nlmsg_len) {
951 break;
954 ev = NLMSG_DATA(nlh);
955 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
956 if (rlen > skb->len)
957 rlen = skb->len;
959 err = iscsi_if_recv_msg(skb, nlh);
960 if (err) {
961 ev->type = ISCSI_KEVENT_IF_ERROR;
962 ev->iferror = err;
964 do {
966 * special case for GET_STATS:
967 * on success - sending reply and stats from
968 * inside of if_recv_msg(),
969 * on error - fall through.
971 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
972 break;
973 err = iscsi_if_send_reply(
974 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
975 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
976 if (atomic_read(&z_reply->allocated) >=
977 z_reply->hiwat)
978 ev->iferror = -ENOMEM;
979 } while (err < 0 && err != -ECONNREFUSED);
980 skb_pull(skb, rlen);
982 free_skb:
983 kfree_skb(skb);
985 mutex_unlock(&rx_queue_mutex);
988 #define iscsi_cdev_to_conn(_cdev) \
989 iscsi_dev_to_conn(_cdev->dev)
992 * iSCSI connection attrs
994 #define iscsi_conn_int_attr_show(param, format) \
995 static ssize_t \
996 show_conn_int_param_##param(struct class_device *cdev, char *buf) \
998 uint32_t value = 0; \
999 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1000 struct iscsi_transport *t = conn->transport; \
1002 t->get_conn_param(conn, param, &value); \
1003 return snprintf(buf, 20, format"\n", value); \
1006 #define iscsi_conn_int_attr(field, param, format) \
1007 iscsi_conn_int_attr_show(param, format) \
1008 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_conn_int_param_##param, NULL);
1010 iscsi_conn_int_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH, "%u");
1011 iscsi_conn_int_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH, "%u");
1012 iscsi_conn_int_attr(header_digest, ISCSI_PARAM_HDRDGST_EN, "%d");
1013 iscsi_conn_int_attr(data_digest, ISCSI_PARAM_DATADGST_EN, "%d");
1014 iscsi_conn_int_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN, "%d");
1015 iscsi_conn_int_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN, "%d");
1017 #define iscsi_cdev_to_session(_cdev) \
1018 iscsi_dev_to_session(_cdev->dev)
1021 * iSCSI session attrs
1023 #define iscsi_session_int_attr_show(param, format) \
1024 static ssize_t \
1025 show_session_int_param_##param(struct class_device *cdev, char *buf) \
1027 uint32_t value = 0; \
1028 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1029 struct iscsi_transport *t = session->transport; \
1031 t->get_session_param(session, param, &value); \
1032 return snprintf(buf, 20, format"\n", value); \
1035 #define iscsi_session_int_attr(field, param, format) \
1036 iscsi_session_int_attr_show(param, format) \
1037 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_session_int_param_##param, NULL);
1039 iscsi_session_int_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, "%d");
1040 iscsi_session_int_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, "%hu");
1041 iscsi_session_int_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, "%d");
1042 iscsi_session_int_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, "%u");
1043 iscsi_session_int_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, "%u");
1044 iscsi_session_int_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, "%d");
1045 iscsi_session_int_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, "%d");
1046 iscsi_session_int_attr(erl, ISCSI_PARAM_ERL, "%d");
1048 #define SETUP_SESSION_RD_ATTR(field, param) \
1049 if (priv->param_mask & (1 << param)) { \
1050 priv->session_attrs[count] = &class_device_attr_##field;\
1051 count++; \
1054 #define SETUP_CONN_RD_ATTR(field, param) \
1055 if (priv->param_mask & (1 << param)) { \
1056 priv->conn_attrs[count] = &class_device_attr_##field; \
1057 count++; \
1060 static int iscsi_session_match(struct attribute_container *cont,
1061 struct device *dev)
1063 struct iscsi_cls_session *session;
1064 struct Scsi_Host *shost;
1065 struct iscsi_internal *priv;
1067 if (!iscsi_is_session_dev(dev))
1068 return 0;
1070 session = iscsi_dev_to_session(dev);
1071 shost = iscsi_session_to_shost(session);
1072 if (!shost->transportt)
1073 return 0;
1075 priv = to_iscsi_internal(shost->transportt);
1076 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1077 return 0;
1079 return &priv->session_cont.ac == cont;
1082 static int iscsi_conn_match(struct attribute_container *cont,
1083 struct device *dev)
1085 struct iscsi_cls_session *session;
1086 struct iscsi_cls_conn *conn;
1087 struct Scsi_Host *shost;
1088 struct iscsi_internal *priv;
1090 if (!iscsi_is_conn_dev(dev))
1091 return 0;
1093 conn = iscsi_dev_to_conn(dev);
1094 session = iscsi_dev_to_session(conn->dev.parent);
1095 shost = iscsi_session_to_shost(session);
1097 if (!shost->transportt)
1098 return 0;
1100 priv = to_iscsi_internal(shost->transportt);
1101 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1102 return 0;
1104 return &priv->conn_cont.ac == cont;
1107 struct scsi_transport_template *
1108 iscsi_register_transport(struct iscsi_transport *tt)
1110 struct iscsi_internal *priv;
1111 unsigned long flags;
1112 int count = 0, err;
1114 BUG_ON(!tt);
1116 priv = iscsi_if_transport_lookup(tt);
1117 if (priv)
1118 return NULL;
1120 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1121 if (!priv)
1122 return NULL;
1123 INIT_LIST_HEAD(&priv->list);
1124 priv->iscsi_transport = tt;
1126 priv->cdev.class = &iscsi_transport_class;
1127 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1128 err = class_device_register(&priv->cdev);
1129 if (err)
1130 goto free_priv;
1132 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1133 if (err)
1134 goto unregister_cdev;
1136 /* setup parameters mask */
1137 priv->param_mask = 0xFFFFFFFF;
1138 if (!(tt->caps & CAP_MULTI_R2T))
1139 priv->param_mask &= ~(1 << ISCSI_PARAM_MAX_R2T);
1140 if (!(tt->caps & CAP_HDRDGST))
1141 priv->param_mask &= ~(1 << ISCSI_PARAM_HDRDGST_EN);
1142 if (!(tt->caps & CAP_DATADGST))
1143 priv->param_mask &= ~(1 << ISCSI_PARAM_DATADGST_EN);
1144 if (!(tt->caps & CAP_MARKERS)) {
1145 priv->param_mask &= ~(1 << ISCSI_PARAM_IFMARKER_EN);
1146 priv->param_mask &= ~(1 << ISCSI_PARAM_OFMARKER_EN);
1149 /* connection parameters */
1150 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1151 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1152 priv->conn_cont.ac.match = iscsi_conn_match;
1153 transport_container_register(&priv->conn_cont);
1155 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1156 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1157 SETUP_CONN_RD_ATTR(header_digest, ISCSI_PARAM_HDRDGST_EN);
1158 SETUP_CONN_RD_ATTR(data_digest, ISCSI_PARAM_DATADGST_EN);
1159 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1160 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1162 BUG_ON(count > ISCSI_CONN_ATTRS);
1163 priv->conn_attrs[count] = NULL;
1164 count = 0;
1166 /* session parameters */
1167 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1168 priv->session_cont.ac.class = &iscsi_session_class.class;
1169 priv->session_cont.ac.match = iscsi_session_match;
1170 transport_container_register(&priv->session_cont);
1172 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN);
1173 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T);
1174 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_PARAM_IMM_DATA_EN);
1175 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_PARAM_FIRST_BURST);
1176 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_PARAM_MAX_BURST);
1177 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN);
1178 SETUP_SESSION_RD_ATTR(data_seq_in_order,ISCSI_PARAM_DATASEQ_INORDER_EN)
1179 SETUP_SESSION_RD_ATTR(erl, ISCSI_PARAM_ERL);
1181 BUG_ON(count > ISCSI_SESSION_ATTRS);
1182 priv->session_attrs[count] = NULL;
1184 spin_lock_irqsave(&iscsi_transport_lock, flags);
1185 list_add(&priv->list, &iscsi_transports);
1186 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1188 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1189 return &priv->t;
1191 unregister_cdev:
1192 class_device_unregister(&priv->cdev);
1193 free_priv:
1194 kfree(priv);
1195 return NULL;
1197 EXPORT_SYMBOL_GPL(iscsi_register_transport);
1199 int iscsi_unregister_transport(struct iscsi_transport *tt)
1201 struct iscsi_internal *priv;
1202 unsigned long flags;
1204 BUG_ON(!tt);
1206 mutex_lock(&rx_queue_mutex);
1208 priv = iscsi_if_transport_lookup(tt);
1209 BUG_ON (!priv);
1211 spin_lock_irqsave(&iscsi_transport_lock, flags);
1212 list_del(&priv->list);
1213 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1215 transport_container_unregister(&priv->conn_cont);
1216 transport_container_unregister(&priv->session_cont);
1218 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1219 class_device_unregister(&priv->cdev);
1220 mutex_unlock(&rx_queue_mutex);
1222 return 0;
1224 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1226 static int
1227 iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr)
1229 struct netlink_notify *n = ptr;
1231 if (event == NETLINK_URELEASE &&
1232 n->protocol == NETLINK_ISCSI && n->pid) {
1233 struct iscsi_cls_conn *conn;
1234 unsigned long flags;
1236 mempool_zone_complete(z_reply);
1237 spin_lock_irqsave(&connlock, flags);
1238 list_for_each_entry(conn, &connlist, conn_list) {
1239 mempool_zone_complete(conn->z_error);
1240 mempool_zone_complete(conn->z_pdu);
1242 spin_unlock_irqrestore(&connlock, flags);
1245 return NOTIFY_DONE;
1248 static struct notifier_block iscsi_nl_notifier = {
1249 .notifier_call = iscsi_rcv_nl_event,
1252 static __init int iscsi_transport_init(void)
1254 int err;
1256 err = class_register(&iscsi_transport_class);
1257 if (err)
1258 return err;
1260 err = transport_class_register(&iscsi_connection_class);
1261 if (err)
1262 goto unregister_transport_class;
1264 err = transport_class_register(&iscsi_session_class);
1265 if (err)
1266 goto unregister_conn_class;
1268 err = netlink_register_notifier(&iscsi_nl_notifier);
1269 if (err)
1270 goto unregister_session_class;
1272 nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx,
1273 THIS_MODULE);
1274 if (!nls) {
1275 err = -ENOBUFS;
1276 goto unregister_notifier;
1279 z_reply = mempool_zone_init(Z_MAX_REPLY,
1280 NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY);
1281 if (z_reply)
1282 return 0;
1284 sock_release(nls->sk_socket);
1285 unregister_notifier:
1286 netlink_unregister_notifier(&iscsi_nl_notifier);
1287 unregister_session_class:
1288 transport_class_unregister(&iscsi_session_class);
1289 unregister_conn_class:
1290 transport_class_unregister(&iscsi_connection_class);
1291 unregister_transport_class:
1292 class_unregister(&iscsi_transport_class);
1293 return err;
1296 static void __exit iscsi_transport_exit(void)
1298 mempool_zone_destroy(z_reply);
1299 sock_release(nls->sk_socket);
1300 netlink_unregister_notifier(&iscsi_nl_notifier);
1301 transport_class_unregister(&iscsi_connection_class);
1302 transport_class_unregister(&iscsi_session_class);
1303 class_unregister(&iscsi_transport_class);
1306 module_init(iscsi_transport_init);
1307 module_exit(iscsi_transport_exit);
1309 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1310 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1311 "Alex Aizman <itn780@yahoo.com>");
1312 MODULE_DESCRIPTION("iSCSI Transport Interface");
1313 MODULE_LICENSE("GPL");