[ALSA] bt87x: add Voodoo TV 200 whitelist entry
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / scsi_transport_iscsi.c
blob5569fdcfd621d3cee2363bc6415a7409dc501380
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 11
35 #define ISCSI_CONN_ATTRS 11
36 #define ISCSI_HOST_ATTRS 0
38 struct iscsi_internal {
39 int daemon_pid;
40 struct scsi_transport_template t;
41 struct iscsi_transport *iscsi_transport;
42 struct list_head list;
43 struct class_device cdev;
45 struct class_device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
46 struct transport_container conn_cont;
47 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
48 struct transport_container session_cont;
49 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
52 static int iscsi_session_nr; /* sysfs session id for next new session */
55 * list of registered transports and lock that must
56 * be held while accessing list. The iscsi_transport_lock must
57 * be acquired after the rx_queue_mutex.
59 static LIST_HEAD(iscsi_transports);
60 static DEFINE_SPINLOCK(iscsi_transport_lock);
62 #define to_iscsi_internal(tmpl) \
63 container_of(tmpl, struct iscsi_internal, t)
65 #define cdev_to_iscsi_internal(_cdev) \
66 container_of(_cdev, struct iscsi_internal, cdev)
68 static void iscsi_transport_release(struct class_device *cdev)
70 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
71 kfree(priv);
75 * iscsi_transport_class represents the iscsi_transports that are
76 * registered.
78 static struct class iscsi_transport_class = {
79 .name = "iscsi_transport",
80 .release = iscsi_transport_release,
83 static ssize_t
84 show_transport_handle(struct class_device *cdev, char *buf)
86 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
87 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
89 static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
91 #define show_transport_attr(name, format) \
92 static ssize_t \
93 show_transport_##name(struct class_device *cdev, char *buf) \
94 { \
95 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
96 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
97 } \
98 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
100 show_transport_attr(caps, "0x%x");
101 show_transport_attr(max_lun, "%d");
102 show_transport_attr(max_conn, "%d");
103 show_transport_attr(max_cmd_len, "%d");
105 static struct attribute *iscsi_transport_attrs[] = {
106 &class_device_attr_handle.attr,
107 &class_device_attr_caps.attr,
108 &class_device_attr_max_lun.attr,
109 &class_device_attr_max_conn.attr,
110 &class_device_attr_max_cmd_len.attr,
111 NULL,
114 static struct attribute_group iscsi_transport_group = {
115 .attrs = iscsi_transport_attrs,
118 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
119 struct class_device *cdev)
121 struct Scsi_Host *shost = dev_to_shost(dev);
122 struct iscsi_host *ihost = shost->shost_data;
124 memset(ihost, 0, sizeof(*ihost));
125 INIT_LIST_HEAD(&ihost->sessions);
126 mutex_init(&ihost->mutex);
127 return 0;
130 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
131 "iscsi_host",
132 iscsi_setup_host,
133 NULL,
134 NULL);
136 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
137 "iscsi_session",
138 NULL,
139 NULL,
140 NULL);
142 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
143 "iscsi_connection",
144 NULL,
145 NULL,
146 NULL);
148 static struct sock *nls;
149 static DEFINE_MUTEX(rx_queue_mutex);
151 struct mempool_zone {
152 mempool_t *pool;
153 atomic_t allocated;
154 int size;
155 int hiwat;
156 struct list_head freequeue;
157 spinlock_t freelock;
160 static struct mempool_zone *z_reply;
163 * Z_MAX_* - actual mempool size allocated at the mempool_zone_init() time
164 * Z_HIWAT_* - zone's high watermark when if_error bit will be set to -ENOMEM
165 * so daemon will notice OOM on NETLINK tranposrt level and will
166 * be able to predict or change operational behavior
168 #define Z_MAX_REPLY 8
169 #define Z_HIWAT_REPLY 6
170 #define Z_MAX_PDU 8
171 #define Z_HIWAT_PDU 6
172 #define Z_MAX_ERROR 16
173 #define Z_HIWAT_ERROR 12
175 static LIST_HEAD(sesslist);
176 static DEFINE_SPINLOCK(sesslock);
177 static LIST_HEAD(connlist);
178 static DEFINE_SPINLOCK(connlock);
180 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
182 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
183 return sess->sid;
187 * Returns the matching session to a given sid
189 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
191 unsigned long flags;
192 struct iscsi_cls_session *sess;
194 spin_lock_irqsave(&sesslock, flags);
195 list_for_each_entry(sess, &sesslist, sess_list) {
196 if (sess->sid == sid) {
197 spin_unlock_irqrestore(&sesslock, flags);
198 return sess;
201 spin_unlock_irqrestore(&sesslock, flags);
202 return NULL;
206 * Returns the matching connection to a given sid / cid tuple
208 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
210 unsigned long flags;
211 struct iscsi_cls_conn *conn;
213 spin_lock_irqsave(&connlock, flags);
214 list_for_each_entry(conn, &connlist, conn_list) {
215 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
216 spin_unlock_irqrestore(&connlock, flags);
217 return conn;
220 spin_unlock_irqrestore(&connlock, flags);
221 return NULL;
225 * The following functions can be used by LLDs that allocate
226 * their own scsi_hosts or by software iscsi LLDs
228 static void iscsi_session_release(struct device *dev)
230 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
231 struct iscsi_transport *transport = session->transport;
232 struct Scsi_Host *shost;
234 shost = iscsi_session_to_shost(session);
235 scsi_host_put(shost);
236 kfree(session->targetname);
237 kfree(session);
238 module_put(transport->owner);
241 static int iscsi_is_session_dev(const struct device *dev)
243 return dev->release == iscsi_session_release;
246 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
247 uint id, uint lun)
249 struct iscsi_host *ihost = shost->shost_data;
250 struct iscsi_cls_session *session;
252 mutex_lock(&ihost->mutex);
253 list_for_each_entry(session, &ihost->sessions, host_list) {
254 if ((channel == SCAN_WILD_CARD ||
255 channel == session->channel) &&
256 (id == SCAN_WILD_CARD || id == session->target_id))
257 scsi_scan_target(&session->dev, session->channel,
258 session->target_id, lun, 1);
260 mutex_unlock(&ihost->mutex);
262 return 0;
265 static void session_recovery_timedout(void *data)
267 struct iscsi_cls_session *session = data;
269 dev_printk(KERN_INFO, &session->dev, "iscsi: session recovery timed "
270 "out after %d secs\n", session->recovery_tmo);
272 if (session->transport->session_recovery_timedout)
273 session->transport->session_recovery_timedout(session);
275 scsi_target_unblock(&session->dev);
278 void iscsi_unblock_session(struct iscsi_cls_session *session)
280 if (!cancel_delayed_work(&session->recovery_work))
281 flush_scheduled_work();
282 scsi_target_unblock(&session->dev);
284 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
286 void iscsi_block_session(struct iscsi_cls_session *session)
288 scsi_target_block(&session->dev);
289 schedule_delayed_work(&session->recovery_work,
290 session->recovery_tmo * HZ);
292 EXPORT_SYMBOL_GPL(iscsi_block_session);
295 * iscsi_create_session - create iscsi class session
296 * @shost: scsi host
297 * @transport: iscsi transport
299 * This can be called from a LLD or iscsi_transport.
301 struct iscsi_cls_session *
302 iscsi_create_session(struct Scsi_Host *shost,
303 struct iscsi_transport *transport, int channel)
305 struct iscsi_host *ihost;
306 struct iscsi_cls_session *session;
307 int err;
309 if (!try_module_get(transport->owner))
310 return NULL;
312 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
313 GFP_KERNEL);
314 if (!session)
315 goto module_put;
316 session->transport = transport;
317 session->recovery_tmo = 120;
318 INIT_WORK(&session->recovery_work, session_recovery_timedout, session);
319 INIT_LIST_HEAD(&session->host_list);
320 INIT_LIST_HEAD(&session->sess_list);
322 if (transport->sessiondata_size)
323 session->dd_data = &session[1];
325 /* this is released in the dev's release function */
326 scsi_host_get(shost);
327 ihost = shost->shost_data;
329 session->sid = iscsi_session_nr++;
330 session->channel = channel;
331 session->target_id = ihost->next_target_id++;
333 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
334 session->sid);
335 session->dev.parent = &shost->shost_gendev;
336 session->dev.release = iscsi_session_release;
337 err = device_register(&session->dev);
338 if (err) {
339 dev_printk(KERN_ERR, &session->dev, "iscsi: could not "
340 "register session's dev\n");
341 goto free_session;
343 transport_register_device(&session->dev);
345 mutex_lock(&ihost->mutex);
346 list_add(&session->host_list, &ihost->sessions);
347 mutex_unlock(&ihost->mutex);
349 return session;
351 free_session:
352 kfree(session);
353 module_put:
354 module_put(transport->owner);
355 return NULL;
358 EXPORT_SYMBOL_GPL(iscsi_create_session);
361 * iscsi_destroy_session - destroy iscsi session
362 * @session: iscsi_session
364 * Can be called by a LLD or iscsi_transport. There must not be
365 * any running connections.
367 int iscsi_destroy_session(struct iscsi_cls_session *session)
369 struct Scsi_Host *shost = iscsi_session_to_shost(session);
370 struct iscsi_host *ihost = shost->shost_data;
372 if (!cancel_delayed_work(&session->recovery_work))
373 flush_scheduled_work();
375 mutex_lock(&ihost->mutex);
376 list_del(&session->host_list);
377 mutex_unlock(&ihost->mutex);
379 transport_unregister_device(&session->dev);
380 device_unregister(&session->dev);
381 return 0;
384 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
386 static void iscsi_conn_release(struct device *dev)
388 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
389 struct device *parent = conn->dev.parent;
391 kfree(conn->persistent_address);
392 kfree(conn);
393 put_device(parent);
396 static int iscsi_is_conn_dev(const struct device *dev)
398 return dev->release == iscsi_conn_release;
402 * iscsi_create_conn - create iscsi class connection
403 * @session: iscsi cls session
404 * @cid: connection id
406 * This can be called from a LLD or iscsi_transport. The connection
407 * is child of the session so cid must be unique for all connections
408 * on the session.
410 * Since we do not support MCS, cid will normally be zero. In some cases
411 * for software iscsi we could be trying to preallocate a connection struct
412 * in which case there could be two connection structs and cid would be
413 * non-zero.
415 struct iscsi_cls_conn *
416 iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
418 struct iscsi_transport *transport = session->transport;
419 struct iscsi_cls_conn *conn;
420 int err;
422 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
423 if (!conn)
424 return NULL;
426 if (transport->conndata_size)
427 conn->dd_data = &conn[1];
429 INIT_LIST_HEAD(&conn->conn_list);
430 conn->transport = transport;
431 conn->cid = cid;
433 /* this is released in the dev's release function */
434 if (!get_device(&session->dev))
435 goto free_conn;
437 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
438 session->sid, cid);
439 conn->dev.parent = &session->dev;
440 conn->dev.release = iscsi_conn_release;
441 err = device_register(&conn->dev);
442 if (err) {
443 dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register "
444 "connection's dev\n");
445 goto release_parent_ref;
447 transport_register_device(&conn->dev);
448 return conn;
450 release_parent_ref:
451 put_device(&session->dev);
452 free_conn:
453 kfree(conn);
454 return NULL;
457 EXPORT_SYMBOL_GPL(iscsi_create_conn);
460 * iscsi_destroy_conn - destroy iscsi class connection
461 * @session: iscsi cls session
463 * This can be called from a LLD or iscsi_transport.
465 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
467 transport_unregister_device(&conn->dev);
468 device_unregister(&conn->dev);
469 return 0;
472 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
475 * iscsi interface functions
477 static struct iscsi_internal *
478 iscsi_if_transport_lookup(struct iscsi_transport *tt)
480 struct iscsi_internal *priv;
481 unsigned long flags;
483 spin_lock_irqsave(&iscsi_transport_lock, flags);
484 list_for_each_entry(priv, &iscsi_transports, list) {
485 if (tt == priv->iscsi_transport) {
486 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
487 return priv;
490 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
491 return NULL;
494 static inline struct list_head *skb_to_lh(struct sk_buff *skb)
496 return (struct list_head *)&skb->cb;
499 static void*
500 mempool_zone_alloc_skb(gfp_t gfp_mask, void *pool_data)
502 struct mempool_zone *zone = pool_data;
504 return alloc_skb(zone->size, gfp_mask);
507 static void
508 mempool_zone_free_skb(void *element, void *pool_data)
510 kfree_skb(element);
513 static void
514 mempool_zone_complete(struct mempool_zone *zone)
516 unsigned long flags;
517 struct list_head *lh, *n;
519 spin_lock_irqsave(&zone->freelock, flags);
520 list_for_each_safe(lh, n, &zone->freequeue) {
521 struct sk_buff *skb = (struct sk_buff *)((char *)lh -
522 offsetof(struct sk_buff, cb));
523 if (!skb_shared(skb)) {
524 list_del(skb_to_lh(skb));
525 mempool_free(skb, zone->pool);
526 atomic_dec(&zone->allocated);
529 spin_unlock_irqrestore(&zone->freelock, flags);
532 static struct mempool_zone *
533 mempool_zone_init(unsigned max, unsigned size, unsigned hiwat)
535 struct mempool_zone *zp;
537 zp = kzalloc(sizeof(*zp), GFP_KERNEL);
538 if (!zp)
539 return NULL;
541 zp->size = size;
542 zp->hiwat = hiwat;
543 INIT_LIST_HEAD(&zp->freequeue);
544 spin_lock_init(&zp->freelock);
545 atomic_set(&zp->allocated, 0);
547 zp->pool = mempool_create(max, mempool_zone_alloc_skb,
548 mempool_zone_free_skb, zp);
549 if (!zp->pool) {
550 kfree(zp);
551 return NULL;
554 return zp;
557 static void mempool_zone_destroy(struct mempool_zone *zp)
559 mempool_destroy(zp->pool);
560 kfree(zp);
563 static struct sk_buff*
564 mempool_zone_get_skb(struct mempool_zone *zone)
566 struct sk_buff *skb;
568 skb = mempool_alloc(zone->pool, GFP_ATOMIC);
569 if (skb)
570 atomic_inc(&zone->allocated);
571 return skb;
574 static int
575 iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb, int pid)
577 unsigned long flags;
578 int rc;
580 skb_get(skb);
581 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
582 if (rc < 0) {
583 mempool_free(skb, zone->pool);
584 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
585 return rc;
588 spin_lock_irqsave(&zone->freelock, flags);
589 INIT_LIST_HEAD(skb_to_lh(skb));
590 list_add(skb_to_lh(skb), &zone->freequeue);
591 spin_unlock_irqrestore(&zone->freelock, flags);
593 return 0;
596 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
597 char *data, uint32_t data_size)
599 struct nlmsghdr *nlh;
600 struct sk_buff *skb;
601 struct iscsi_uevent *ev;
602 char *pdu;
603 struct iscsi_internal *priv;
604 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
605 data_size);
607 priv = iscsi_if_transport_lookup(conn->transport);
608 if (!priv)
609 return -EINVAL;
611 mempool_zone_complete(conn->z_pdu);
613 skb = mempool_zone_get_skb(conn->z_pdu);
614 if (!skb) {
615 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
616 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver "
617 "control PDU: OOM\n");
618 return -ENOMEM;
621 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
622 ev = NLMSG_DATA(nlh);
623 memset(ev, 0, sizeof(*ev));
624 ev->transport_handle = iscsi_handle(conn->transport);
625 ev->type = ISCSI_KEVENT_RECV_PDU;
626 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
627 ev->iferror = -ENOMEM;
628 ev->r.recv_req.cid = conn->cid;
629 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
630 pdu = (char*)ev + sizeof(*ev);
631 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
632 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
634 return iscsi_unicast_skb(conn->z_pdu, skb, priv->daemon_pid);
636 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
638 void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
640 struct nlmsghdr *nlh;
641 struct sk_buff *skb;
642 struct iscsi_uevent *ev;
643 struct iscsi_internal *priv;
644 int len = NLMSG_SPACE(sizeof(*ev));
646 priv = iscsi_if_transport_lookup(conn->transport);
647 if (!priv)
648 return;
650 mempool_zone_complete(conn->z_error);
652 skb = mempool_zone_get_skb(conn->z_error);
653 if (!skb) {
654 dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored "
655 "conn error (%d)\n", error);
656 return;
659 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
660 ev = NLMSG_DATA(nlh);
661 ev->transport_handle = iscsi_handle(conn->transport);
662 ev->type = ISCSI_KEVENT_CONN_ERROR;
663 if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat)
664 ev->iferror = -ENOMEM;
665 ev->r.connerror.error = error;
666 ev->r.connerror.cid = conn->cid;
667 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
669 iscsi_unicast_skb(conn->z_error, skb, priv->daemon_pid);
671 dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n",
672 error);
674 EXPORT_SYMBOL_GPL(iscsi_conn_error);
676 static int
677 iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
678 void *payload, int size)
680 struct sk_buff *skb;
681 struct nlmsghdr *nlh;
682 int len = NLMSG_SPACE(size);
683 int flags = multi ? NLM_F_MULTI : 0;
684 int t = done ? NLMSG_DONE : type;
686 mempool_zone_complete(z_reply);
688 skb = mempool_zone_get_skb(z_reply);
690 * FIXME:
691 * user is supposed to react on iferror == -ENOMEM;
692 * see iscsi_if_rx().
694 BUG_ON(!skb);
696 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
697 nlh->nlmsg_flags = flags;
698 memcpy(NLMSG_DATA(nlh), payload, size);
699 return iscsi_unicast_skb(z_reply, skb, pid);
702 static int
703 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
705 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
706 struct iscsi_stats *stats;
707 struct sk_buff *skbstat;
708 struct iscsi_cls_conn *conn;
709 struct nlmsghdr *nlhstat;
710 struct iscsi_uevent *evstat;
711 struct iscsi_internal *priv;
712 int len = NLMSG_SPACE(sizeof(*ev) +
713 sizeof(struct iscsi_stats) +
714 sizeof(struct iscsi_stats_custom) *
715 ISCSI_STATS_CUSTOM_MAX);
716 int err = 0;
718 priv = iscsi_if_transport_lookup(transport);
719 if (!priv)
720 return -EINVAL;
722 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
723 if (!conn)
724 return -EEXIST;
726 do {
727 int actual_size;
729 mempool_zone_complete(conn->z_pdu);
731 skbstat = mempool_zone_get_skb(conn->z_pdu);
732 if (!skbstat) {
733 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not "
734 "deliver stats: OOM\n");
735 return -ENOMEM;
738 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
739 (len - sizeof(*nlhstat)), 0);
740 evstat = NLMSG_DATA(nlhstat);
741 memset(evstat, 0, sizeof(*evstat));
742 evstat->transport_handle = iscsi_handle(conn->transport);
743 evstat->type = nlh->nlmsg_type;
744 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
745 evstat->iferror = -ENOMEM;
746 evstat->u.get_stats.cid =
747 ev->u.get_stats.cid;
748 evstat->u.get_stats.sid =
749 ev->u.get_stats.sid;
750 stats = (struct iscsi_stats *)
751 ((char*)evstat + sizeof(*evstat));
752 memset(stats, 0, sizeof(*stats));
754 transport->get_stats(conn, stats);
755 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
756 sizeof(struct iscsi_stats) +
757 sizeof(struct iscsi_stats_custom) *
758 stats->custom_length);
759 actual_size -= sizeof(*nlhstat);
760 actual_size = NLMSG_LENGTH(actual_size);
761 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
762 nlhstat->nlmsg_len = actual_size;
764 err = iscsi_unicast_skb(conn->z_pdu, skbstat, priv->daemon_pid);
765 } while (err < 0 && err != -ECONNREFUSED);
767 return err;
770 static int
771 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
773 struct iscsi_transport *transport = priv->iscsi_transport;
774 struct iscsi_cls_session *session;
775 unsigned long flags;
776 uint32_t hostno;
778 session = transport->create_session(transport, &priv->t,
779 ev->u.c_session.initial_cmdsn,
780 &hostno);
781 if (!session)
782 return -ENOMEM;
784 spin_lock_irqsave(&sesslock, flags);
785 list_add(&session->sess_list, &sesslist);
786 spin_unlock_irqrestore(&sesslock, flags);
788 ev->r.c_session_ret.host_no = hostno;
789 ev->r.c_session_ret.sid = session->sid;
790 return 0;
793 static int
794 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
796 struct iscsi_cls_conn *conn;
797 struct iscsi_cls_session *session;
798 unsigned long flags;
800 session = iscsi_session_lookup(ev->u.c_conn.sid);
801 if (!session) {
802 printk(KERN_ERR "iscsi: invalid session %d\n",
803 ev->u.c_conn.sid);
804 return -EINVAL;
807 conn = transport->create_conn(session, ev->u.c_conn.cid);
808 if (!conn) {
809 printk(KERN_ERR "iscsi: couldn't create a new "
810 "connection for session %d\n",
811 session->sid);
812 return -ENOMEM;
815 conn->z_pdu = mempool_zone_init(Z_MAX_PDU,
816 NLMSG_SPACE(sizeof(struct iscsi_uevent) +
817 sizeof(struct iscsi_hdr) +
818 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH),
819 Z_HIWAT_PDU);
820 if (!conn->z_pdu) {
821 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
822 "pdu zone for new conn\n");
823 goto destroy_conn;
826 conn->z_error = mempool_zone_init(Z_MAX_ERROR,
827 NLMSG_SPACE(sizeof(struct iscsi_uevent)),
828 Z_HIWAT_ERROR);
829 if (!conn->z_error) {
830 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate "
831 "error zone for new conn\n");
832 goto free_pdu_pool;
835 ev->r.c_conn_ret.sid = session->sid;
836 ev->r.c_conn_ret.cid = conn->cid;
838 spin_lock_irqsave(&connlock, flags);
839 list_add(&conn->conn_list, &connlist);
840 conn->active = 1;
841 spin_unlock_irqrestore(&connlock, flags);
843 return 0;
845 free_pdu_pool:
846 mempool_zone_destroy(conn->z_pdu);
847 destroy_conn:
848 if (transport->destroy_conn)
849 transport->destroy_conn(conn->dd_data);
850 return -ENOMEM;
853 static int
854 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
856 unsigned long flags;
857 struct iscsi_cls_conn *conn;
858 struct mempool_zone *z_error, *z_pdu;
860 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
861 if (!conn)
862 return -EINVAL;
863 spin_lock_irqsave(&connlock, flags);
864 conn->active = 0;
865 list_del(&conn->conn_list);
866 spin_unlock_irqrestore(&connlock, flags);
868 z_pdu = conn->z_pdu;
869 z_error = conn->z_error;
871 if (transport->destroy_conn)
872 transport->destroy_conn(conn);
874 mempool_zone_destroy(z_pdu);
875 mempool_zone_destroy(z_error);
877 return 0;
880 static void
881 iscsi_copy_param(struct iscsi_uevent *ev, uint32_t *value, char *data)
883 if (ev->u.set_param.len != sizeof(uint32_t))
884 BUG();
885 memcpy(value, data, min_t(uint32_t, sizeof(uint32_t),
886 ev->u.set_param.len));
889 static int
890 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
892 char *data = (char*)ev + sizeof(*ev);
893 struct iscsi_cls_conn *conn;
894 struct iscsi_cls_session *session;
895 int err = 0;
896 uint32_t value = 0;
898 session = iscsi_session_lookup(ev->u.set_param.sid);
899 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
900 if (!conn || !session)
901 return -EINVAL;
903 switch (ev->u.set_param.param) {
904 case ISCSI_PARAM_SESS_RECOVERY_TMO:
905 iscsi_copy_param(ev, &value, data);
906 if (value != 0)
907 session->recovery_tmo = value;
908 break;
909 case ISCSI_PARAM_TARGET_NAME:
910 /* this should not change between logins */
911 if (session->targetname)
912 return 0;
914 session->targetname = kstrdup(data, GFP_KERNEL);
915 if (!session->targetname)
916 return -ENOMEM;
917 break;
918 case ISCSI_PARAM_TPGT:
919 iscsi_copy_param(ev, &value, data);
920 session->tpgt = value;
921 break;
922 case ISCSI_PARAM_PERSISTENT_PORT:
923 iscsi_copy_param(ev, &value, data);
924 conn->persistent_port = value;
925 break;
926 case ISCSI_PARAM_PERSISTENT_ADDRESS:
928 * this is the address returned in discovery so it should
929 * not change between logins.
931 if (conn->persistent_address)
932 return 0;
934 conn->persistent_address = kstrdup(data, GFP_KERNEL);
935 if (!conn->persistent_address)
936 return -ENOMEM;
937 break;
938 default:
939 iscsi_copy_param(ev, &value, data);
940 err = transport->set_param(conn, ev->u.set_param.param, value);
943 return err;
946 static int
947 iscsi_if_transport_ep(struct iscsi_transport *transport,
948 struct iscsi_uevent *ev, int msg_type)
950 struct sockaddr *dst_addr;
951 int rc = 0;
953 switch (msg_type) {
954 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
955 if (!transport->ep_connect)
956 return -EINVAL;
958 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
959 rc = transport->ep_connect(dst_addr,
960 ev->u.ep_connect.non_blocking,
961 &ev->r.ep_connect_ret.handle);
962 break;
963 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
964 if (!transport->ep_poll)
965 return -EINVAL;
967 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
968 ev->u.ep_poll.timeout_ms);
969 break;
970 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
971 if (!transport->ep_disconnect)
972 return -EINVAL;
974 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
975 break;
977 return rc;
980 static int
981 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
983 int err = 0;
984 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
985 struct iscsi_transport *transport = NULL;
986 struct iscsi_internal *priv;
987 struct iscsi_cls_session *session;
988 struct iscsi_cls_conn *conn;
989 unsigned long flags;
991 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
992 if (!priv)
993 return -EINVAL;
994 transport = priv->iscsi_transport;
996 if (!try_module_get(transport->owner))
997 return -EINVAL;
999 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1001 switch (nlh->nlmsg_type) {
1002 case ISCSI_UEVENT_CREATE_SESSION:
1003 err = iscsi_if_create_session(priv, ev);
1004 break;
1005 case ISCSI_UEVENT_DESTROY_SESSION:
1006 session = iscsi_session_lookup(ev->u.d_session.sid);
1007 if (session) {
1008 spin_lock_irqsave(&sesslock, flags);
1009 list_del(&session->sess_list);
1010 spin_unlock_irqrestore(&sesslock, flags);
1012 transport->destroy_session(session);
1013 } else
1014 err = -EINVAL;
1015 break;
1016 case ISCSI_UEVENT_CREATE_CONN:
1017 err = iscsi_if_create_conn(transport, ev);
1018 break;
1019 case ISCSI_UEVENT_DESTROY_CONN:
1020 err = iscsi_if_destroy_conn(transport, ev);
1021 break;
1022 case ISCSI_UEVENT_BIND_CONN:
1023 session = iscsi_session_lookup(ev->u.b_conn.sid);
1024 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1026 if (session && conn)
1027 ev->r.retcode = transport->bind_conn(session, conn,
1028 ev->u.b_conn.transport_eph,
1029 ev->u.b_conn.is_leading);
1030 else
1031 err = -EINVAL;
1032 break;
1033 case ISCSI_UEVENT_SET_PARAM:
1034 err = iscsi_set_param(transport, ev);
1035 break;
1036 case ISCSI_UEVENT_START_CONN:
1037 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
1038 if (conn)
1039 ev->r.retcode = transport->start_conn(conn);
1040 else
1041 err = -EINVAL;
1042 break;
1043 case ISCSI_UEVENT_STOP_CONN:
1044 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
1045 if (conn)
1046 transport->stop_conn(conn, ev->u.stop_conn.flag);
1047 else
1048 err = -EINVAL;
1049 break;
1050 case ISCSI_UEVENT_SEND_PDU:
1051 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
1052 if (conn)
1053 ev->r.retcode = transport->send_pdu(conn,
1054 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1055 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1056 ev->u.send_pdu.data_size);
1057 else
1058 err = -EINVAL;
1059 break;
1060 case ISCSI_UEVENT_GET_STATS:
1061 err = iscsi_if_get_stats(transport, nlh);
1062 break;
1063 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1064 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1065 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1066 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1067 break;
1068 default:
1069 err = -EINVAL;
1070 break;
1073 module_put(transport->owner);
1074 return err;
1078 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
1079 * processed by iscsi_if_recv_msg. Malformed skbs with wrong lengths or
1080 * invalid creds are discarded silently.
1082 static void
1083 iscsi_if_rx(struct sock *sk, int len)
1085 struct sk_buff *skb;
1087 mutex_lock(&rx_queue_mutex);
1088 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
1089 if (NETLINK_CREDS(skb)->uid) {
1090 skb_pull(skb, skb->len);
1091 goto free_skb;
1094 while (skb->len >= NLMSG_SPACE(0)) {
1095 int err;
1096 uint32_t rlen;
1097 struct nlmsghdr *nlh;
1098 struct iscsi_uevent *ev;
1100 nlh = (struct nlmsghdr *)skb->data;
1101 if (nlh->nlmsg_len < sizeof(*nlh) ||
1102 skb->len < nlh->nlmsg_len) {
1103 break;
1106 ev = NLMSG_DATA(nlh);
1107 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1108 if (rlen > skb->len)
1109 rlen = skb->len;
1111 err = iscsi_if_recv_msg(skb, nlh);
1112 if (err) {
1113 ev->type = ISCSI_KEVENT_IF_ERROR;
1114 ev->iferror = err;
1116 do {
1118 * special case for GET_STATS:
1119 * on success - sending reply and stats from
1120 * inside of if_recv_msg(),
1121 * on error - fall through.
1123 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1124 break;
1125 err = iscsi_if_send_reply(
1126 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1127 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1128 if (atomic_read(&z_reply->allocated) >=
1129 z_reply->hiwat)
1130 ev->iferror = -ENOMEM;
1131 } while (err < 0 && err != -ECONNREFUSED);
1132 skb_pull(skb, rlen);
1134 free_skb:
1135 kfree_skb(skb);
1137 mutex_unlock(&rx_queue_mutex);
1140 #define iscsi_cdev_to_conn(_cdev) \
1141 iscsi_dev_to_conn(_cdev->dev)
1143 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1144 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1145 __ATTR(_name,_mode,_show,_store)
1148 * iSCSI connection attrs
1150 #define iscsi_conn_int_attr_show(param, format) \
1151 static ssize_t \
1152 show_conn_int_param_##param(struct class_device *cdev, char *buf) \
1154 uint32_t value = 0; \
1155 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1156 struct iscsi_transport *t = conn->transport; \
1158 t->get_conn_param(conn, param, &value); \
1159 return snprintf(buf, 20, format"\n", value); \
1162 #define iscsi_conn_int_attr(field, param, format) \
1163 iscsi_conn_int_attr_show(param, format) \
1164 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_int_param_##param, \
1165 NULL);
1167 iscsi_conn_int_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH, "%u");
1168 iscsi_conn_int_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH, "%u");
1169 iscsi_conn_int_attr(header_digest, ISCSI_PARAM_HDRDGST_EN, "%d");
1170 iscsi_conn_int_attr(data_digest, ISCSI_PARAM_DATADGST_EN, "%d");
1171 iscsi_conn_int_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN, "%d");
1172 iscsi_conn_int_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN, "%d");
1173 iscsi_conn_int_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT, "%d");
1174 iscsi_conn_int_attr(port, ISCSI_PARAM_CONN_PORT, "%d");
1175 iscsi_conn_int_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN, "%u");
1177 #define iscsi_conn_str_attr_show(param) \
1178 static ssize_t \
1179 show_conn_str_param_##param(struct class_device *cdev, char *buf) \
1181 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1182 struct iscsi_transport *t = conn->transport; \
1183 return t->get_conn_str_param(conn, param, buf); \
1186 #define iscsi_conn_str_attr(field, param) \
1187 iscsi_conn_str_attr_show(param) \
1188 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_str_param_##param, \
1189 NULL);
1191 iscsi_conn_str_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1192 iscsi_conn_str_attr(address, ISCSI_PARAM_CONN_ADDRESS);
1194 #define iscsi_cdev_to_session(_cdev) \
1195 iscsi_dev_to_session(_cdev->dev)
1198 * iSCSI session attrs
1200 #define iscsi_session_int_attr_show(param, format) \
1201 static ssize_t \
1202 show_session_int_param_##param(struct class_device *cdev, char *buf) \
1204 uint32_t value = 0; \
1205 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1206 struct iscsi_transport *t = session->transport; \
1208 t->get_session_param(session, param, &value); \
1209 return snprintf(buf, 20, format"\n", value); \
1212 #define iscsi_session_int_attr(field, param, format) \
1213 iscsi_session_int_attr_show(param, format) \
1214 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_int_param_##param, \
1215 NULL);
1217 iscsi_session_int_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, "%d");
1218 iscsi_session_int_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, "%hu");
1219 iscsi_session_int_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, "%d");
1220 iscsi_session_int_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, "%u");
1221 iscsi_session_int_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, "%u");
1222 iscsi_session_int_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, "%d");
1223 iscsi_session_int_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, "%d");
1224 iscsi_session_int_attr(erl, ISCSI_PARAM_ERL, "%d");
1225 iscsi_session_int_attr(tpgt, ISCSI_PARAM_TPGT, "%d");
1227 #define iscsi_session_str_attr_show(param) \
1228 static ssize_t \
1229 show_session_str_param_##param(struct class_device *cdev, char *buf) \
1231 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1232 struct iscsi_transport *t = session->transport; \
1233 return t->get_session_str_param(session, param, buf); \
1236 #define iscsi_session_str_attr(field, param) \
1237 iscsi_session_str_attr_show(param) \
1238 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_str_param_##param, \
1239 NULL);
1241 iscsi_session_str_attr(targetname, ISCSI_PARAM_TARGET_NAME);
1244 * Private session and conn attrs. userspace uses several iscsi values
1245 * to identify each session between reboots. Some of these values may not
1246 * be present in the iscsi_transport/LLD driver becuase userspace handles
1247 * login (and failback for login redirect) so for these type of drivers
1248 * the class manages the attrs and values for the iscsi_transport/LLD
1250 #define iscsi_priv_session_attr_show(field, format) \
1251 static ssize_t \
1252 show_priv_session_##field(struct class_device *cdev, char *buf) \
1254 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1255 return sprintf(buf, format"\n", session->field); \
1258 #define iscsi_priv_session_attr(field, format) \
1259 iscsi_priv_session_attr_show(field, format) \
1260 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1261 NULL)
1262 iscsi_priv_session_attr(targetname, "%s");
1263 iscsi_priv_session_attr(tpgt, "%d");
1264 iscsi_priv_session_attr(recovery_tmo, "%d");
1266 #define iscsi_priv_conn_attr_show(field, format) \
1267 static ssize_t \
1268 show_priv_conn_##field(struct class_device *cdev, char *buf) \
1270 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1271 return sprintf(buf, format"\n", conn->field); \
1274 #define iscsi_priv_conn_attr(field, format) \
1275 iscsi_priv_conn_attr_show(field, format) \
1276 static ISCSI_CLASS_ATTR(priv_conn, field, S_IRUGO, show_priv_conn_##field, \
1277 NULL)
1278 iscsi_priv_conn_attr(persistent_address, "%s");
1279 iscsi_priv_conn_attr(persistent_port, "%d");
1281 #define SETUP_PRIV_SESSION_RD_ATTR(field) \
1282 do { \
1283 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1284 count++; \
1285 } while (0)
1287 #define SETUP_SESSION_RD_ATTR(field, param_flag) \
1288 do { \
1289 if (tt->param_mask & param_flag) { \
1290 priv->session_attrs[count] = &class_device_attr_sess_##field; \
1291 count++; \
1293 } while (0)
1295 #define SETUP_PRIV_CONN_RD_ATTR(field) \
1296 do { \
1297 priv->conn_attrs[count] = &class_device_attr_priv_conn_##field; \
1298 count++; \
1299 } while (0)
1301 #define SETUP_CONN_RD_ATTR(field, param_flag) \
1302 do { \
1303 if (tt->param_mask & param_flag) { \
1304 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1305 count++; \
1307 } while (0)
1309 static int iscsi_session_match(struct attribute_container *cont,
1310 struct device *dev)
1312 struct iscsi_cls_session *session;
1313 struct Scsi_Host *shost;
1314 struct iscsi_internal *priv;
1316 if (!iscsi_is_session_dev(dev))
1317 return 0;
1319 session = iscsi_dev_to_session(dev);
1320 shost = iscsi_session_to_shost(session);
1321 if (!shost->transportt)
1322 return 0;
1324 priv = to_iscsi_internal(shost->transportt);
1325 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1326 return 0;
1328 return &priv->session_cont.ac == cont;
1331 static int iscsi_conn_match(struct attribute_container *cont,
1332 struct device *dev)
1334 struct iscsi_cls_session *session;
1335 struct iscsi_cls_conn *conn;
1336 struct Scsi_Host *shost;
1337 struct iscsi_internal *priv;
1339 if (!iscsi_is_conn_dev(dev))
1340 return 0;
1342 conn = iscsi_dev_to_conn(dev);
1343 session = iscsi_dev_to_session(conn->dev.parent);
1344 shost = iscsi_session_to_shost(session);
1346 if (!shost->transportt)
1347 return 0;
1349 priv = to_iscsi_internal(shost->transportt);
1350 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1351 return 0;
1353 return &priv->conn_cont.ac == cont;
1356 static int iscsi_host_match(struct attribute_container *cont,
1357 struct device *dev)
1359 struct Scsi_Host *shost;
1360 struct iscsi_internal *priv;
1362 if (!scsi_is_host_device(dev))
1363 return 0;
1365 shost = dev_to_shost(dev);
1366 if (!shost->transportt ||
1367 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1368 return 0;
1370 priv = to_iscsi_internal(shost->transportt);
1371 return &priv->t.host_attrs.ac == cont;
1374 struct scsi_transport_template *
1375 iscsi_register_transport(struct iscsi_transport *tt)
1377 struct iscsi_internal *priv;
1378 unsigned long flags;
1379 int count = 0, err;
1381 BUG_ON(!tt);
1383 priv = iscsi_if_transport_lookup(tt);
1384 if (priv)
1385 return NULL;
1387 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1388 if (!priv)
1389 return NULL;
1390 INIT_LIST_HEAD(&priv->list);
1391 priv->iscsi_transport = tt;
1392 priv->t.user_scan = iscsi_user_scan;
1394 priv->cdev.class = &iscsi_transport_class;
1395 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1396 err = class_device_register(&priv->cdev);
1397 if (err)
1398 goto free_priv;
1400 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1401 if (err)
1402 goto unregister_cdev;
1404 /* host parameters */
1405 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1406 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1407 priv->t.host_attrs.ac.match = iscsi_host_match;
1408 priv->t.host_size = sizeof(struct iscsi_host);
1409 priv->host_attrs[0] = NULL;
1410 transport_container_register(&priv->t.host_attrs);
1412 /* connection parameters */
1413 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1414 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1415 priv->conn_cont.ac.match = iscsi_conn_match;
1416 transport_container_register(&priv->conn_cont);
1418 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1419 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1420 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1421 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1422 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1423 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1424 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1425 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
1426 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
1428 if (tt->param_mask & ISCSI_PERSISTENT_ADDRESS)
1429 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1430 else
1431 SETUP_PRIV_CONN_RD_ATTR(persistent_address);
1433 if (tt->param_mask & ISCSI_PERSISTENT_PORT)
1434 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
1435 else
1436 SETUP_PRIV_CONN_RD_ATTR(persistent_port);
1438 BUG_ON(count > ISCSI_CONN_ATTRS);
1439 priv->conn_attrs[count] = NULL;
1440 count = 0;
1442 /* session parameters */
1443 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1444 priv->session_cont.ac.class = &iscsi_session_class.class;
1445 priv->session_cont.ac.match = iscsi_session_match;
1446 transport_container_register(&priv->session_cont);
1448 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1449 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1450 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1451 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1452 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1453 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1454 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1455 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
1456 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
1458 if (tt->param_mask & ISCSI_TARGET_NAME)
1459 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1460 else
1461 SETUP_PRIV_SESSION_RD_ATTR(targetname);
1463 if (tt->param_mask & ISCSI_TPGT)
1464 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
1465 else
1466 SETUP_PRIV_SESSION_RD_ATTR(tpgt);
1468 BUG_ON(count > ISCSI_SESSION_ATTRS);
1469 priv->session_attrs[count] = NULL;
1471 spin_lock_irqsave(&iscsi_transport_lock, flags);
1472 list_add(&priv->list, &iscsi_transports);
1473 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1475 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1476 return &priv->t;
1478 unregister_cdev:
1479 class_device_unregister(&priv->cdev);
1480 free_priv:
1481 kfree(priv);
1482 return NULL;
1484 EXPORT_SYMBOL_GPL(iscsi_register_transport);
1486 int iscsi_unregister_transport(struct iscsi_transport *tt)
1488 struct iscsi_internal *priv;
1489 unsigned long flags;
1491 BUG_ON(!tt);
1493 mutex_lock(&rx_queue_mutex);
1495 priv = iscsi_if_transport_lookup(tt);
1496 BUG_ON (!priv);
1498 spin_lock_irqsave(&iscsi_transport_lock, flags);
1499 list_del(&priv->list);
1500 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1502 transport_container_unregister(&priv->conn_cont);
1503 transport_container_unregister(&priv->session_cont);
1504 transport_container_unregister(&priv->t.host_attrs);
1506 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1507 class_device_unregister(&priv->cdev);
1508 mutex_unlock(&rx_queue_mutex);
1510 return 0;
1512 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1514 static int
1515 iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr)
1517 struct netlink_notify *n = ptr;
1519 if (event == NETLINK_URELEASE &&
1520 n->protocol == NETLINK_ISCSI && n->pid) {
1521 struct iscsi_cls_conn *conn;
1522 unsigned long flags;
1524 mempool_zone_complete(z_reply);
1525 spin_lock_irqsave(&connlock, flags);
1526 list_for_each_entry(conn, &connlist, conn_list) {
1527 mempool_zone_complete(conn->z_error);
1528 mempool_zone_complete(conn->z_pdu);
1530 spin_unlock_irqrestore(&connlock, flags);
1533 return NOTIFY_DONE;
1536 static struct notifier_block iscsi_nl_notifier = {
1537 .notifier_call = iscsi_rcv_nl_event,
1540 static __init int iscsi_transport_init(void)
1542 int err;
1544 err = class_register(&iscsi_transport_class);
1545 if (err)
1546 return err;
1548 err = transport_class_register(&iscsi_host_class);
1549 if (err)
1550 goto unregister_transport_class;
1552 err = transport_class_register(&iscsi_connection_class);
1553 if (err)
1554 goto unregister_host_class;
1556 err = transport_class_register(&iscsi_session_class);
1557 if (err)
1558 goto unregister_conn_class;
1560 err = netlink_register_notifier(&iscsi_nl_notifier);
1561 if (err)
1562 goto unregister_session_class;
1564 nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx,
1565 THIS_MODULE);
1566 if (!nls) {
1567 err = -ENOBUFS;
1568 goto unregister_notifier;
1571 z_reply = mempool_zone_init(Z_MAX_REPLY,
1572 NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY);
1573 if (z_reply)
1574 return 0;
1576 sock_release(nls->sk_socket);
1577 unregister_notifier:
1578 netlink_unregister_notifier(&iscsi_nl_notifier);
1579 unregister_session_class:
1580 transport_class_unregister(&iscsi_session_class);
1581 unregister_conn_class:
1582 transport_class_unregister(&iscsi_connection_class);
1583 unregister_host_class:
1584 transport_class_unregister(&iscsi_host_class);
1585 unregister_transport_class:
1586 class_unregister(&iscsi_transport_class);
1587 return err;
1590 static void __exit iscsi_transport_exit(void)
1592 mempool_zone_destroy(z_reply);
1593 sock_release(nls->sk_socket);
1594 netlink_unregister_notifier(&iscsi_nl_notifier);
1595 transport_class_unregister(&iscsi_connection_class);
1596 transport_class_unregister(&iscsi_session_class);
1597 transport_class_unregister(&iscsi_host_class);
1598 class_unregister(&iscsi_transport_class);
1601 module_init(iscsi_transport_init);
1602 module_exit(iscsi_transport_exit);
1604 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1605 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1606 "Alex Aizman <itn780@yahoo.com>");
1607 MODULE_DESCRIPTION("iSCSI Transport Interface");
1608 MODULE_LICENSE("GPL");