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 * 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
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
);
80 * iscsi_transport_class represents the iscsi_transports that are
83 static struct class iscsi_transport_class
= {
84 .name
= "iscsi_transport",
85 .release
= iscsi_transport_release
,
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) \
98 show_transport_##name(struct class_device *cdev, char *buf) \
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
,
119 static struct attribute_group iscsi_transport_group
= {
120 .attrs
= iscsi_transport_attrs
,
123 static DECLARE_TRANSPORT_CLASS(iscsi_session_class
,
129 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class
,
135 static struct sock
*nls
;
136 static int daemon_pid
;
137 static DEFINE_MUTEX(rx_queue_mutex
);
139 struct mempool_zone
{
144 struct list_head freequeue
;
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
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
)
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
);
180 spin_unlock_irqrestore(&sesslock
, flags
);
184 static struct iscsi_cls_conn
*iscsi_conn_lookup(uint64_t handle
)
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
);
196 spin_unlock_irqrestore(&connlock
, flags
);
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
);
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
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
;
234 if (!try_module_get(transport
->owner
))
237 session
= kzalloc(sizeof(*session
), GFP_KERNEL
);
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
);
249 dev_printk(KERN_ERR
, &session
->dev
, "iscsi: could not "
250 "register session's dev\n");
253 transport_register_device(&session
->dev
);
260 module_put(transport
->owner
);
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
);
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
;
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
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
;
313 conn
= kzalloc(sizeof(*conn
) + transport
->conndata_size
, GFP_KERNEL
);
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
))
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
);
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
);
340 put_device(&session
->dev
);
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
);
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.
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
;
400 shost
= scsi_host_alloc(transport
->host_template
,
401 hostdata_privsize(transport
));
403 printk(KERN_ERR
"iscsi: can not allocate SCSI host for "
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
))
418 session
= iscsi_create_session(shost
, transport
);
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
);
429 scsi_remove_host(shost
);
431 scsi_host_put(shost
);
435 EXPORT_SYMBOL_GPL(iscsi_transport_create_session
);
438 * iscsi_transport_destroy_session - destroy session and 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
;
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
);
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
;
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
);
478 spin_unlock_irqrestore(&iscsi_transport_lock
, flags
);
482 static inline struct list_head
*skb_to_lh(struct sk_buff
*skb
)
484 return (struct list_head
*)&skb
->cb
;
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
);
496 mempool_zone_free_skb(void *element
, void *pool_data
)
502 mempool_zone_complete(struct mempool_zone
*zone
)
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
);
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
);
545 static void mempool_zone_destroy(struct mempool_zone
*zp
)
547 mempool_destroy(zp
->pool
);
551 static struct sk_buff
*
552 mempool_zone_get_skb(struct mempool_zone
*zone
)
556 skb
= mempool_alloc(zone
->pool
, GFP_ATOMIC
);
558 atomic_inc(&zone
->allocated
);
563 iscsi_unicast_skb(struct mempool_zone
*zone
, struct sk_buff
*skb
)
569 rc
= netlink_unicast(nls
, skb
, daemon_pid
, MSG_DONTWAIT
);
571 mempool_free(skb
, zone
->pool
);
572 printk(KERN_ERR
"iscsi: can not unicast skb (%d)\n", rc
);
576 spin_lock_irqsave(&zone
->freelock
, flags
);
577 list_add(skb_to_lh(skb
), &zone
->freequeue
);
578 spin_unlock_irqrestore(&zone
->freelock
, flags
);
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
;
588 struct iscsi_uevent
*ev
;
590 int len
= NLMSG_SPACE(sizeof(*ev
) + sizeof(struct iscsi_hdr
) +
593 mempool_zone_complete(conn
->z_pdu
);
595 skb
= mempool_zone_get_skb(conn
->z_pdu
);
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");
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
;
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
);
630 dev_printk(KERN_ERR
, &conn
->dev
, "iscsi: gracefully ignored "
631 "conn error (%d)\n", error
);
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",
649 EXPORT_SYMBOL_GPL(iscsi_conn_error
);
652 iscsi_if_send_reply(int pid
, int seq
, int type
, int done
, int multi
,
653 void *payload
, int size
)
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
);
666 * user is supposed to react on iferror == -ENOMEM;
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
);
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
);
692 conn
= iscsi_conn_lookup(ev
->u
.get_stats
.conn_handle
);
699 mempool_zone_complete(conn
->z_pdu
);
701 skbstat
= mempool_zone_get_skb(conn
->z_pdu
);
703 dev_printk(KERN_ERR
, &conn
->dev
, "iscsi: can not "
704 "deliver stats: OOM\n");
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
);
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
;
745 session
= transport
->create_session(&priv
->t
,
746 ev
->u
.c_session
.initial_cmdsn
,
751 ev
->r
.c_session_ret
.session_handle
= iscsi_handle(session
);
752 ev
->r
.c_session_ret
.sid
= sid
;
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
;
763 session
= iscsi_session_lookup(ev
->u
.c_conn
.session_handle
);
767 conn
= transport
->create_conn(session
, ev
->u
.c_conn
.cid
);
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
),
777 dev_printk(KERN_ERR
, &conn
->dev
, "iscsi: can not allocate "
778 "pdu zone for new conn\n");
782 conn
->z_error
= mempool_zone_init(Z_MAX_ERROR
,
783 NLMSG_SPACE(sizeof(struct iscsi_uevent
)),
785 if (!conn
->z_error
) {
786 dev_printk(KERN_ERR
, &conn
->dev
, "iscsi: can not allocate "
787 "error zone for new conn\n");
791 ev
->r
.handle
= iscsi_handle(conn
);
793 spin_lock_irqsave(&connlock
, flags
);
794 list_add(&conn
->conn_list
, &connlist
);
796 spin_unlock_irqrestore(&connlock
, flags
);
801 mempool_zone_destroy(conn
->z_pdu
);
803 if (transport
->destroy_conn
)
804 transport
->destroy_conn(conn
->dd_data
);
809 iscsi_if_destroy_conn(struct iscsi_transport
*transport
, struct iscsi_uevent
*ev
)
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
);
818 spin_lock_irqsave(&connlock
, flags
);
820 list_del(&conn
->conn_list
);
821 spin_unlock_irqrestore(&connlock
, flags
);
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
);
836 iscsi_if_recv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
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
));
848 transport
= priv
->iscsi_transport
;
850 if (!try_module_get(transport
->owner
))
853 switch (nlh
->nlmsg_type
) {
854 case ISCSI_UEVENT_CREATE_SESSION
:
855 err
= iscsi_if_create_session(priv
, ev
);
857 case ISCSI_UEVENT_DESTROY_SESSION
:
858 session
= iscsi_session_lookup(ev
->u
.d_session
.session_handle
);
860 transport
->destroy_session(session
);
864 case ISCSI_UEVENT_CREATE_CONN
:
865 err
= iscsi_if_create_conn(transport
, ev
);
867 case ISCSI_UEVENT_DESTROY_CONN
:
868 err
= iscsi_if_destroy_conn(transport
, ev
);
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
);
875 ev
->r
.retcode
= transport
->bind_conn(session
, conn
,
876 ev
->u
.b_conn
.transport_fd
,
877 ev
->u
.b_conn
.is_leading
);
881 case ISCSI_UEVENT_SET_PARAM
:
882 conn
= iscsi_conn_lookup(ev
->u
.set_param
.conn_handle
);
884 ev
->r
.retcode
= transport
->set_param(conn
,
885 ev
->u
.set_param
.param
, ev
->u
.set_param
.value
);
889 case ISCSI_UEVENT_START_CONN
:
890 conn
= iscsi_conn_lookup(ev
->u
.start_conn
.conn_handle
);
892 ev
->r
.retcode
= transport
->start_conn(conn
);
897 case ISCSI_UEVENT_STOP_CONN
:
898 conn
= iscsi_conn_lookup(ev
->u
.stop_conn
.conn_handle
);
900 transport
->stop_conn(conn
, ev
->u
.stop_conn
.flag
);
904 case ISCSI_UEVENT_SEND_PDU
:
905 conn
= iscsi_conn_lookup(ev
->u
.send_pdu
.conn_handle
);
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
);
914 case ISCSI_UEVENT_GET_STATS
:
915 err
= iscsi_if_get_stats(transport
, nlh
);
922 module_put(transport
->owner
);
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. */
930 iscsi_if_rx(struct sock
*sk
, int len
)
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
);
940 daemon_pid
= NETLINK_CREDS(skb
)->pid
;
942 while (skb
->len
>= NLMSG_SPACE(0)) {
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
) {
954 ev
= NLMSG_DATA(nlh
);
955 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
959 err
= iscsi_if_recv_msg(skb
, nlh
);
961 ev
->type
= ISCSI_KEVENT_IF_ERROR
;
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
)
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
) >=
978 ev
->iferror
= -ENOMEM
;
979 } while (err
< 0 && err
!= -ECONNREFUSED
);
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) \
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) \
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;\
1054 #define SETUP_CONN_RD_ATTR(field, param) \
1055 if (priv->param_mask & (1 << param)) { \
1056 priv->conn_attrs[count] = &class_device_attr_##field; \
1060 static int iscsi_session_match(struct attribute_container
*cont
,
1063 struct iscsi_cls_session
*session
;
1064 struct Scsi_Host
*shost
;
1065 struct iscsi_internal
*priv
;
1067 if (!iscsi_is_session_dev(dev
))
1070 session
= iscsi_dev_to_session(dev
);
1071 shost
= iscsi_session_to_shost(session
);
1072 if (!shost
->transportt
)
1075 priv
= to_iscsi_internal(shost
->transportt
);
1076 if (priv
->session_cont
.ac
.class != &iscsi_session_class
.class)
1079 return &priv
->session_cont
.ac
== cont
;
1082 static int iscsi_conn_match(struct attribute_container
*cont
,
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
))
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
)
1100 priv
= to_iscsi_internal(shost
->transportt
);
1101 if (priv
->conn_cont
.ac
.class != &iscsi_connection_class
.class)
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
;
1116 priv
= iscsi_if_transport_lookup(tt
);
1120 priv
= kmalloc(sizeof(*priv
), GFP_KERNEL
);
1123 memset(priv
, 0, sizeof(*priv
));
1124 INIT_LIST_HEAD(&priv
->list
);
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");