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/mutex.h>
25 #include <linux/slab.h>
26 #include <linux/bsg-lib.h>
27 #include <linux/idr.h>
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_transport.h>
33 #include <scsi/scsi_transport_iscsi.h>
34 #include <scsi/iscsi_if.h>
35 #include <scsi/scsi_cmnd.h>
36 #include <scsi/scsi_bsg_iscsi.h>
38 #define ISCSI_TRANSPORT_VERSION "2.0-870"
40 static int dbg_session;
41 module_param_named(debug_session, dbg_session, int,
43 MODULE_PARM_DESC(debug_session,
44 "Turn on debugging for sessions in scsi_transport_iscsi "
45 "module. Set to 1 to turn on, and zero to turn off. Default "
49 module_param_named(debug_conn, dbg_conn, int,
51 MODULE_PARM_DESC(debug_conn,
52 "Turn on debugging for connections in scsi_transport_iscsi "
53 "module. Set to 1 to turn on, and zero to turn off. Default "
56 #define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
59 iscsi_cls_session_printk(KERN_INFO, _session, \
64 #define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
67 iscsi_cls_conn_printk(KERN_INFO, _conn, \
72 struct iscsi_internal {
73 struct scsi_transport_template t;
74 struct iscsi_transport *iscsi_transport;
75 struct list_head list;
78 struct transport_container conn_cont;
79 struct transport_container session_cont;
82 static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
83 static struct workqueue_struct *iscsi_eh_timer_workq;
85 static DEFINE_IDA(iscsi_sess_ida);
87 * list of registered transports and lock that must
88 * be held while accessing list. The iscsi_transport_lock must
89 * be acquired after the rx_queue_mutex.
91 static LIST_HEAD(iscsi_transports);
92 static DEFINE_SPINLOCK(iscsi_transport_lock);
94 #define to_iscsi_internal(tmpl) \
95 container_of(tmpl, struct iscsi_internal, t)
97 #define dev_to_iscsi_internal(_dev) \
98 container_of(_dev, struct iscsi_internal, dev)
100 static void iscsi_transport_release(struct device *dev)
102 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
107 * iscsi_transport_class represents the iscsi_transports that are
110 static struct class iscsi_transport_class = {
111 .name = "iscsi_transport",
112 .dev_release = iscsi_transport_release,
116 show_transport_handle(struct device *dev, struct device_attribute *attr,
119 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
120 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
122 static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
124 #define show_transport_attr(name, format) \
126 show_transport_##name(struct device *dev, \
127 struct device_attribute *attr,char *buf) \
129 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
130 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
132 static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
134 show_transport_attr(caps, "0x%x");
136 static struct attribute *iscsi_transport_attrs[] = {
137 &dev_attr_handle.attr,
142 static struct attribute_group iscsi_transport_group = {
143 .attrs = iscsi_transport_attrs,
147 * iSCSI endpoint attrs
149 #define iscsi_dev_to_endpoint(_dev) \
150 container_of(_dev, struct iscsi_endpoint, dev)
152 #define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
153 struct device_attribute dev_attr_##_prefix##_##_name = \
154 __ATTR(_name,_mode,_show,_store)
156 static void iscsi_endpoint_release(struct device *dev)
158 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
162 static struct class iscsi_endpoint_class = {
163 .name = "iscsi_endpoint",
164 .dev_release = iscsi_endpoint_release,
168 show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
170 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
171 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
173 static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
175 static struct attribute *iscsi_endpoint_attrs[] = {
176 &dev_attr_ep_handle.attr,
180 static struct attribute_group iscsi_endpoint_group = {
181 .attrs = iscsi_endpoint_attrs,
184 #define ISCSI_MAX_EPID -1
186 static int iscsi_match_epid(struct device *dev, void *data)
188 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
189 uint64_t *epid = (uint64_t *) data;
191 return *epid == ep->id;
194 struct iscsi_endpoint *
195 iscsi_create_endpoint(int dd_size)
198 struct iscsi_endpoint *ep;
202 for (id = 1; id < ISCSI_MAX_EPID; id++) {
203 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
208 if (id == ISCSI_MAX_EPID) {
209 printk(KERN_ERR "Too many connections. Max supported %u\n",
214 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
219 ep->dev.class = &iscsi_endpoint_class;
220 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
221 err = device_register(&ep->dev);
225 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
230 ep->dd_data = &ep[1];
234 device_unregister(&ep->dev);
241 EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
243 void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
245 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
246 device_unregister(&ep->dev);
248 EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
250 struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
252 struct iscsi_endpoint *ep;
255 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
260 ep = iscsi_dev_to_endpoint(dev);
262 * we can drop this now because the interface will prevent
263 * removals and lookups from racing.
268 EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
271 * Interface to display network param to sysfs
274 static void iscsi_iface_release(struct device *dev)
276 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
277 struct device *parent = iface->dev.parent;
284 static struct class iscsi_iface_class = {
285 .name = "iscsi_iface",
286 .dev_release = iscsi_iface_release,
289 #define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
290 struct device_attribute dev_attr_##_prefix##_##_name = \
291 __ATTR(_name, _mode, _show, _store)
293 /* iface attrs show */
294 #define iscsi_iface_attr_show(type, name, param_type, param) \
296 show_##type##_##name(struct device *dev, struct device_attribute *attr, \
299 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
300 struct iscsi_transport *t = iface->transport; \
301 return t->get_iface_param(iface, param_type, param, buf); \
304 #define iscsi_iface_net_attr(type, name, param) \
305 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
306 static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
308 /* generic read only ipvi4 attribute */
309 iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
310 iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
311 iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
312 iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
314 /* generic read only ipv6 attribute */
315 iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
316 iscsi_iface_net_attr(ipv6_iface, link_local_addr, ISCSI_NET_PARAM_IPV6_LINKLOCAL);
317 iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
318 iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
319 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
320 iscsi_iface_net_attr(ipv6_iface, link_local_autocfg,
321 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
323 /* common read only iface attribute */
324 iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
325 iscsi_iface_net_attr(iface, vlan_id, ISCSI_NET_PARAM_VLAN_ID);
326 iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
327 iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
328 iscsi_iface_net_attr(iface, mtu, ISCSI_NET_PARAM_MTU);
329 iscsi_iface_net_attr(iface, port, ISCSI_NET_PARAM_PORT);
331 static umode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
332 struct attribute *attr, int i)
334 struct device *dev = container_of(kobj, struct device, kobj);
335 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
336 struct iscsi_transport *t = iface->transport;
339 if (attr == &dev_attr_iface_enabled.attr)
340 param = ISCSI_NET_PARAM_IFACE_ENABLE;
341 else if (attr == &dev_attr_iface_vlan_id.attr)
342 param = ISCSI_NET_PARAM_VLAN_ID;
343 else if (attr == &dev_attr_iface_vlan_priority.attr)
344 param = ISCSI_NET_PARAM_VLAN_PRIORITY;
345 else if (attr == &dev_attr_iface_vlan_enabled.attr)
346 param = ISCSI_NET_PARAM_VLAN_ENABLED;
347 else if (attr == &dev_attr_iface_mtu.attr)
348 param = ISCSI_NET_PARAM_MTU;
349 else if (attr == &dev_attr_iface_port.attr)
350 param = ISCSI_NET_PARAM_PORT;
351 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
352 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
353 param = ISCSI_NET_PARAM_IPV4_ADDR;
354 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
355 param = ISCSI_NET_PARAM_IPV4_GW;
356 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
357 param = ISCSI_NET_PARAM_IPV4_SUBNET;
358 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
359 param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
362 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
363 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
364 param = ISCSI_NET_PARAM_IPV6_ADDR;
365 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
366 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
367 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
368 param = ISCSI_NET_PARAM_IPV6_ROUTER;
369 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
370 param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
371 else if (attr == &dev_attr_ipv6_iface_link_local_autocfg.attr)
372 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
376 WARN_ONCE(1, "Invalid iface attr");
380 return t->attr_is_visible(ISCSI_NET_PARAM, param);
383 static struct attribute *iscsi_iface_attrs[] = {
384 &dev_attr_iface_enabled.attr,
385 &dev_attr_iface_vlan_id.attr,
386 &dev_attr_iface_vlan_priority.attr,
387 &dev_attr_iface_vlan_enabled.attr,
388 &dev_attr_ipv4_iface_ipaddress.attr,
389 &dev_attr_ipv4_iface_gateway.attr,
390 &dev_attr_ipv4_iface_subnet.attr,
391 &dev_attr_ipv4_iface_bootproto.attr,
392 &dev_attr_ipv6_iface_ipaddress.attr,
393 &dev_attr_ipv6_iface_link_local_addr.attr,
394 &dev_attr_ipv6_iface_router_addr.attr,
395 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
396 &dev_attr_ipv6_iface_link_local_autocfg.attr,
397 &dev_attr_iface_mtu.attr,
398 &dev_attr_iface_port.attr,
402 static struct attribute_group iscsi_iface_group = {
403 .attrs = iscsi_iface_attrs,
404 .is_visible = iscsi_iface_attr_is_visible,
408 iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
409 uint32_t iface_type, uint32_t iface_num, int dd_size)
411 struct iscsi_iface *iface;
414 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
418 iface->transport = transport;
419 iface->iface_type = iface_type;
420 iface->iface_num = iface_num;
421 iface->dev.release = iscsi_iface_release;
422 iface->dev.class = &iscsi_iface_class;
423 /* parent reference released in iscsi_iface_release */
424 iface->dev.parent = get_device(&shost->shost_gendev);
425 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
426 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
429 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
432 err = device_register(&iface->dev);
436 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
441 iface->dd_data = &iface[1];
445 device_unregister(&iface->dev);
449 put_device(iface->dev.parent);
453 EXPORT_SYMBOL_GPL(iscsi_create_iface);
455 void iscsi_destroy_iface(struct iscsi_iface *iface)
457 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
458 device_unregister(&iface->dev);
460 EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
466 * iscsi_bsg_host_dispatch - Dispatch command to LLD.
467 * @job: bsg job to be processed
469 static int iscsi_bsg_host_dispatch(struct bsg_job *job)
471 struct Scsi_Host *shost = iscsi_job_to_shost(job);
472 struct iscsi_bsg_request *req = job->request;
473 struct iscsi_bsg_reply *reply = job->reply;
474 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
475 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
478 /* check if we have the msgcode value at least */
479 if (job->request_len < sizeof(uint32_t)) {
484 /* Validate the host command */
485 switch (req->msgcode) {
486 case ISCSI_BSG_HST_VENDOR:
487 cmdlen += sizeof(struct iscsi_bsg_host_vendor);
488 if ((shost->hostt->vendor_id == 0L) ||
489 (req->rqst_data.h_vendor.vendor_id !=
490 shost->hostt->vendor_id)) {
500 /* check if we really have all the request data needed */
501 if (job->request_len < cmdlen) {
506 ret = i->iscsi_transport->bsg_request(job);
511 /* return the errno failure code as the only status */
512 BUG_ON(job->reply_len < sizeof(uint32_t));
513 reply->reply_payload_rcv_len = 0;
515 job->reply_len = sizeof(uint32_t);
516 bsg_job_done(job, ret, 0);
521 * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
522 * @shost: shost for iscsi_host
523 * @ihost: iscsi_cls_host adding the structures to
526 iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
528 struct device *dev = &shost->shost_gendev;
529 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
530 struct request_queue *q;
534 if (!i->iscsi_transport->bsg_request)
537 snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
539 q = __scsi_alloc_queue(shost, bsg_request_fn);
543 ret = bsg_setup_queue(dev, q, bsg_name, iscsi_bsg_host_dispatch, 0);
545 shost_printk(KERN_ERR, shost, "bsg interface failed to "
546 "initialize - no request queue\n");
547 blk_cleanup_queue(q);
555 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
558 struct Scsi_Host *shost = dev_to_shost(dev);
559 struct iscsi_cls_host *ihost = shost->shost_data;
561 memset(ihost, 0, sizeof(*ihost));
562 atomic_set(&ihost->nr_scans, 0);
563 mutex_init(&ihost->mutex);
565 iscsi_bsg_host_add(shost, ihost);
566 /* ignore any bsg add error - we just can't do sgio */
571 static int iscsi_remove_host(struct transport_container *tc,
572 struct device *dev, struct device *cdev)
574 struct Scsi_Host *shost = dev_to_shost(dev);
575 struct iscsi_cls_host *ihost = shost->shost_data;
578 bsg_remove_queue(ihost->bsg_q);
579 blk_cleanup_queue(ihost->bsg_q);
584 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
590 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
596 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
602 static struct sock *nls;
603 static DEFINE_MUTEX(rx_queue_mutex);
605 static LIST_HEAD(sesslist);
606 static DEFINE_SPINLOCK(sesslock);
607 static LIST_HEAD(connlist);
608 static DEFINE_SPINLOCK(connlock);
610 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
612 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
617 * Returns the matching session to a given sid
619 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
622 struct iscsi_cls_session *sess;
624 spin_lock_irqsave(&sesslock, flags);
625 list_for_each_entry(sess, &sesslist, sess_list) {
626 if (sess->sid == sid) {
627 spin_unlock_irqrestore(&sesslock, flags);
631 spin_unlock_irqrestore(&sesslock, flags);
636 * Returns the matching connection to a given sid / cid tuple
638 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
641 struct iscsi_cls_conn *conn;
643 spin_lock_irqsave(&connlock, flags);
644 list_for_each_entry(conn, &connlist, conn_list) {
645 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
646 spin_unlock_irqrestore(&connlock, flags);
650 spin_unlock_irqrestore(&connlock, flags);
655 * The following functions can be used by LLDs that allocate
656 * their own scsi_hosts or by software iscsi LLDs
661 } iscsi_session_state_names[] = {
662 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
663 { ISCSI_SESSION_FAILED, "FAILED" },
664 { ISCSI_SESSION_FREE, "FREE" },
667 static const char *iscsi_session_state_name(int state)
672 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
673 if (iscsi_session_state_names[i].value == state) {
674 name = iscsi_session_state_names[i].name;
681 int iscsi_session_chkready(struct iscsi_cls_session *session)
686 spin_lock_irqsave(&session->lock, flags);
687 switch (session->state) {
688 case ISCSI_SESSION_LOGGED_IN:
691 case ISCSI_SESSION_FAILED:
692 err = DID_IMM_RETRY << 16;
694 case ISCSI_SESSION_FREE:
695 err = DID_TRANSPORT_FAILFAST << 16;
698 err = DID_NO_CONNECT << 16;
701 spin_unlock_irqrestore(&session->lock, flags);
704 EXPORT_SYMBOL_GPL(iscsi_session_chkready);
706 int iscsi_is_session_online(struct iscsi_cls_session *session)
711 spin_lock_irqsave(&session->lock, flags);
712 if (session->state == ISCSI_SESSION_LOGGED_IN)
714 spin_unlock_irqrestore(&session->lock, flags);
717 EXPORT_SYMBOL_GPL(iscsi_is_session_online);
719 static void iscsi_session_release(struct device *dev)
721 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
722 struct Scsi_Host *shost;
724 shost = iscsi_session_to_shost(session);
725 scsi_host_put(shost);
726 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
730 static int iscsi_is_session_dev(const struct device *dev)
732 return dev->release == iscsi_session_release;
735 static int iscsi_iter_session_fn(struct device *dev, void *data)
737 void (* fn) (struct iscsi_cls_session *) = data;
739 if (!iscsi_is_session_dev(dev))
741 fn(iscsi_dev_to_session(dev));
745 void iscsi_host_for_each_session(struct Scsi_Host *shost,
746 void (*fn)(struct iscsi_cls_session *))
748 device_for_each_child(&shost->shost_gendev, fn,
749 iscsi_iter_session_fn);
751 EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
754 * iscsi_scan_finished - helper to report when running scans are done
756 * @time: scan run time
758 * This function can be used by drives like qla4xxx to report to the scsi
759 * layer when the scans it kicked off at module load time are done.
761 int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
763 struct iscsi_cls_host *ihost = shost->shost_data;
765 * qla4xxx will have kicked off some session unblocks before calling
766 * scsi_scan_host, so just wait for them to complete.
768 return !atomic_read(&ihost->nr_scans);
770 EXPORT_SYMBOL_GPL(iscsi_scan_finished);
772 struct iscsi_scan_data {
773 unsigned int channel;
778 static int iscsi_user_scan_session(struct device *dev, void *data)
780 struct iscsi_scan_data *scan_data = data;
781 struct iscsi_cls_session *session;
782 struct Scsi_Host *shost;
783 struct iscsi_cls_host *ihost;
787 if (!iscsi_is_session_dev(dev))
790 session = iscsi_dev_to_session(dev);
792 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
794 shost = iscsi_session_to_shost(session);
795 ihost = shost->shost_data;
797 mutex_lock(&ihost->mutex);
798 spin_lock_irqsave(&session->lock, flags);
799 if (session->state != ISCSI_SESSION_LOGGED_IN) {
800 spin_unlock_irqrestore(&session->lock, flags);
803 id = session->target_id;
804 spin_unlock_irqrestore(&session->lock, flags);
806 if (id != ISCSI_MAX_TARGET) {
807 if ((scan_data->channel == SCAN_WILD_CARD ||
808 scan_data->channel == 0) &&
809 (scan_data->id == SCAN_WILD_CARD ||
810 scan_data->id == id))
811 scsi_scan_target(&session->dev, 0, id,
816 mutex_unlock(&ihost->mutex);
817 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
821 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
824 struct iscsi_scan_data scan_data;
826 scan_data.channel = channel;
830 return device_for_each_child(&shost->shost_gendev, &scan_data,
831 iscsi_user_scan_session);
834 static void iscsi_scan_session(struct work_struct *work)
836 struct iscsi_cls_session *session =
837 container_of(work, struct iscsi_cls_session, scan_work);
838 struct Scsi_Host *shost = iscsi_session_to_shost(session);
839 struct iscsi_cls_host *ihost = shost->shost_data;
840 struct iscsi_scan_data scan_data;
842 scan_data.channel = 0;
843 scan_data.id = SCAN_WILD_CARD;
844 scan_data.lun = SCAN_WILD_CARD;
846 iscsi_user_scan_session(&session->dev, &scan_data);
847 atomic_dec(&ihost->nr_scans);
851 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
852 * @cmd: scsi cmd passed to scsi eh handler
854 * If the session is down this function will wait for the recovery
855 * timer to fire or for the session to be logged back in. If the
856 * recovery timer fires then FAST_IO_FAIL is returned. The caller
857 * should pass this error value to the scsi eh.
859 int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
861 struct iscsi_cls_session *session =
862 starget_to_session(scsi_target(cmd->device));
866 spin_lock_irqsave(&session->lock, flags);
867 while (session->state != ISCSI_SESSION_LOGGED_IN) {
868 if (session->state == ISCSI_SESSION_FREE) {
872 spin_unlock_irqrestore(&session->lock, flags);
874 spin_lock_irqsave(&session->lock, flags);
876 spin_unlock_irqrestore(&session->lock, flags);
879 EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
881 static void session_recovery_timedout(struct work_struct *work)
883 struct iscsi_cls_session *session =
884 container_of(work, struct iscsi_cls_session,
888 iscsi_cls_session_printk(KERN_INFO, session,
889 "session recovery timed out after %d secs\n",
890 session->recovery_tmo);
892 spin_lock_irqsave(&session->lock, flags);
893 switch (session->state) {
894 case ISCSI_SESSION_FAILED:
895 session->state = ISCSI_SESSION_FREE;
897 case ISCSI_SESSION_LOGGED_IN:
898 case ISCSI_SESSION_FREE:
899 /* we raced with the unblock's flush */
900 spin_unlock_irqrestore(&session->lock, flags);
903 spin_unlock_irqrestore(&session->lock, flags);
905 if (session->transport->session_recovery_timedout)
906 session->transport->session_recovery_timedout(session);
908 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
909 scsi_target_unblock(&session->dev);
910 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
913 static void __iscsi_unblock_session(struct work_struct *work)
915 struct iscsi_cls_session *session =
916 container_of(work, struct iscsi_cls_session,
918 struct Scsi_Host *shost = iscsi_session_to_shost(session);
919 struct iscsi_cls_host *ihost = shost->shost_data;
922 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
924 * The recovery and unblock work get run from the same workqueue,
925 * so try to cancel it if it was going to run after this unblock.
927 cancel_delayed_work(&session->recovery_work);
928 spin_lock_irqsave(&session->lock, flags);
929 session->state = ISCSI_SESSION_LOGGED_IN;
930 spin_unlock_irqrestore(&session->lock, flags);
932 scsi_target_unblock(&session->dev);
934 * Only do kernel scanning if the driver is properly hooked into
935 * the async scanning code (drivers like iscsi_tcp do login and
936 * scanning from userspace).
938 if (shost->hostt->scan_finished) {
939 if (scsi_queue_work(shost, &session->scan_work))
940 atomic_inc(&ihost->nr_scans);
942 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
946 * iscsi_unblock_session - set a session as logged in and start IO.
947 * @session: iscsi session
949 * Mark a session as ready to accept IO.
951 void iscsi_unblock_session(struct iscsi_cls_session *session)
953 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
955 * make sure all the events have completed before tell the driver
958 flush_workqueue(iscsi_eh_timer_workq);
960 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
962 static void __iscsi_block_session(struct work_struct *work)
964 struct iscsi_cls_session *session =
965 container_of(work, struct iscsi_cls_session,
969 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
970 spin_lock_irqsave(&session->lock, flags);
971 session->state = ISCSI_SESSION_FAILED;
972 spin_unlock_irqrestore(&session->lock, flags);
973 scsi_target_block(&session->dev);
974 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
975 if (session->recovery_tmo >= 0)
976 queue_delayed_work(iscsi_eh_timer_workq,
977 &session->recovery_work,
978 session->recovery_tmo * HZ);
981 void iscsi_block_session(struct iscsi_cls_session *session)
983 queue_work(iscsi_eh_timer_workq, &session->block_work);
985 EXPORT_SYMBOL_GPL(iscsi_block_session);
987 static void __iscsi_unbind_session(struct work_struct *work)
989 struct iscsi_cls_session *session =
990 container_of(work, struct iscsi_cls_session,
992 struct Scsi_Host *shost = iscsi_session_to_shost(session);
993 struct iscsi_cls_host *ihost = shost->shost_data;
995 unsigned int target_id;
997 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
999 /* Prevent new scans and make sure scanning is not in progress */
1000 mutex_lock(&ihost->mutex);
1001 spin_lock_irqsave(&session->lock, flags);
1002 if (session->target_id == ISCSI_MAX_TARGET) {
1003 spin_unlock_irqrestore(&session->lock, flags);
1004 mutex_unlock(&ihost->mutex);
1008 target_id = session->target_id;
1009 session->target_id = ISCSI_MAX_TARGET;
1010 spin_unlock_irqrestore(&session->lock, flags);
1011 mutex_unlock(&ihost->mutex);
1013 if (session->ida_used)
1014 ida_simple_remove(&iscsi_sess_ida, target_id);
1016 scsi_remove_target(&session->dev);
1017 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
1018 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
1021 struct iscsi_cls_session *
1022 iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1025 struct iscsi_cls_session *session;
1027 session = kzalloc(sizeof(*session) + dd_size,
1032 session->transport = transport;
1033 session->creator = -1;
1034 session->recovery_tmo = 120;
1035 session->state = ISCSI_SESSION_FREE;
1036 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
1037 INIT_LIST_HEAD(&session->sess_list);
1038 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
1039 INIT_WORK(&session->block_work, __iscsi_block_session);
1040 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
1041 INIT_WORK(&session->scan_work, iscsi_scan_session);
1042 spin_lock_init(&session->lock);
1044 /* this is released in the dev's release function */
1045 scsi_host_get(shost);
1046 session->dev.parent = &shost->shost_gendev;
1047 session->dev.release = iscsi_session_release;
1048 device_initialize(&session->dev);
1050 session->dd_data = &session[1];
1052 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
1055 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
1057 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
1059 struct Scsi_Host *shost = iscsi_session_to_shost(session);
1060 struct iscsi_cls_host *ihost;
1061 unsigned long flags;
1065 ihost = shost->shost_data;
1066 session->sid = atomic_add_return(1, &iscsi_session_nr);
1068 if (target_id == ISCSI_MAX_TARGET) {
1069 id = ida_simple_get(&iscsi_sess_ida, 0, 0, GFP_KERNEL);
1072 iscsi_cls_session_printk(KERN_ERR, session,
1073 "Failure in Target ID Allocation\n");
1076 session->target_id = (unsigned int)id;
1077 session->ida_used = true;
1079 session->target_id = target_id;
1081 dev_set_name(&session->dev, "session%u", session->sid);
1082 err = device_add(&session->dev);
1084 iscsi_cls_session_printk(KERN_ERR, session,
1085 "could not register session's dev\n");
1088 transport_register_device(&session->dev);
1090 spin_lock_irqsave(&sesslock, flags);
1091 list_add(&session->sess_list, &sesslist);
1092 spin_unlock_irqrestore(&sesslock, flags);
1094 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
1095 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
1099 if (session->ida_used)
1100 ida_simple_remove(&iscsi_sess_ida, session->target_id);
1104 EXPORT_SYMBOL_GPL(iscsi_add_session);
1107 * iscsi_create_session - create iscsi class session
1109 * @transport: iscsi transport
1110 * @dd_size: private driver data size
1111 * @target_id: which target
1113 * This can be called from a LLD or iscsi_transport.
1115 struct iscsi_cls_session *
1116 iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1117 int dd_size, unsigned int target_id)
1119 struct iscsi_cls_session *session;
1121 session = iscsi_alloc_session(shost, transport, dd_size);
1125 if (iscsi_add_session(session, target_id)) {
1126 iscsi_free_session(session);
1131 EXPORT_SYMBOL_GPL(iscsi_create_session);
1133 static void iscsi_conn_release(struct device *dev)
1135 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
1136 struct device *parent = conn->dev.parent;
1138 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
1143 static int iscsi_is_conn_dev(const struct device *dev)
1145 return dev->release == iscsi_conn_release;
1148 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
1150 if (!iscsi_is_conn_dev(dev))
1152 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
1155 void iscsi_remove_session(struct iscsi_cls_session *session)
1157 struct Scsi_Host *shost = iscsi_session_to_shost(session);
1158 unsigned long flags;
1161 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
1163 spin_lock_irqsave(&sesslock, flags);
1164 list_del(&session->sess_list);
1165 spin_unlock_irqrestore(&sesslock, flags);
1167 /* make sure there are no blocks/unblocks queued */
1168 flush_workqueue(iscsi_eh_timer_workq);
1169 /* make sure the timedout callout is not running */
1170 if (!cancel_delayed_work(&session->recovery_work))
1171 flush_workqueue(iscsi_eh_timer_workq);
1173 * If we are blocked let commands flow again. The lld or iscsi
1174 * layer should set up the queuecommand to fail commands.
1175 * We assume that LLD will not be calling block/unblock while
1176 * removing the session.
1178 spin_lock_irqsave(&session->lock, flags);
1179 session->state = ISCSI_SESSION_FREE;
1180 spin_unlock_irqrestore(&session->lock, flags);
1182 scsi_target_unblock(&session->dev);
1183 /* flush running scans then delete devices */
1184 scsi_flush_work(shost);
1185 __iscsi_unbind_session(&session->unbind_work);
1187 /* hw iscsi may not have removed all connections from session */
1188 err = device_for_each_child(&session->dev, NULL,
1189 iscsi_iter_destroy_conn_fn);
1191 iscsi_cls_session_printk(KERN_ERR, session,
1192 "Could not delete all connections "
1193 "for session. Error %d.\n", err);
1195 transport_unregister_device(&session->dev);
1197 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
1198 device_del(&session->dev);
1200 EXPORT_SYMBOL_GPL(iscsi_remove_session);
1202 void iscsi_free_session(struct iscsi_cls_session *session)
1204 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
1205 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
1206 put_device(&session->dev);
1208 EXPORT_SYMBOL_GPL(iscsi_free_session);
1211 * iscsi_destroy_session - destroy iscsi session
1212 * @session: iscsi_session
1214 * Can be called by a LLD or iscsi_transport. There must not be
1215 * any running connections.
1217 int iscsi_destroy_session(struct iscsi_cls_session *session)
1219 iscsi_remove_session(session);
1220 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
1221 iscsi_free_session(session);
1224 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
1227 * iscsi_create_conn - create iscsi class connection
1228 * @session: iscsi cls session
1229 * @dd_size: private driver data size
1230 * @cid: connection id
1232 * This can be called from a LLD or iscsi_transport. The connection
1233 * is child of the session so cid must be unique for all connections
1236 * Since we do not support MCS, cid will normally be zero. In some cases
1237 * for software iscsi we could be trying to preallocate a connection struct
1238 * in which case there could be two connection structs and cid would be
1241 struct iscsi_cls_conn *
1242 iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
1244 struct iscsi_transport *transport = session->transport;
1245 struct iscsi_cls_conn *conn;
1246 unsigned long flags;
1249 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
1253 conn->dd_data = &conn[1];
1255 mutex_init(&conn->ep_mutex);
1256 INIT_LIST_HEAD(&conn->conn_list);
1257 conn->transport = transport;
1260 /* this is released in the dev's release function */
1261 if (!get_device(&session->dev))
1264 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
1265 conn->dev.parent = &session->dev;
1266 conn->dev.release = iscsi_conn_release;
1267 err = device_register(&conn->dev);
1269 iscsi_cls_session_printk(KERN_ERR, session, "could not "
1270 "register connection's dev\n");
1271 goto release_parent_ref;
1273 transport_register_device(&conn->dev);
1275 spin_lock_irqsave(&connlock, flags);
1276 list_add(&conn->conn_list, &connlist);
1277 spin_unlock_irqrestore(&connlock, flags);
1279 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
1283 put_device(&session->dev);
1289 EXPORT_SYMBOL_GPL(iscsi_create_conn);
1292 * iscsi_destroy_conn - destroy iscsi class connection
1293 * @conn: iscsi cls session
1295 * This can be called from a LLD or iscsi_transport.
1297 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1299 unsigned long flags;
1301 spin_lock_irqsave(&connlock, flags);
1302 list_del(&conn->conn_list);
1303 spin_unlock_irqrestore(&connlock, flags);
1305 transport_unregister_device(&conn->dev);
1306 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
1307 device_unregister(&conn->dev);
1310 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1313 * iscsi interface functions
1315 static struct iscsi_internal *
1316 iscsi_if_transport_lookup(struct iscsi_transport *tt)
1318 struct iscsi_internal *priv;
1319 unsigned long flags;
1321 spin_lock_irqsave(&iscsi_transport_lock, flags);
1322 list_for_each_entry(priv, &iscsi_transports, list) {
1323 if (tt == priv->iscsi_transport) {
1324 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1328 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1333 iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
1335 return nlmsg_multicast(nls, skb, 0, group, gfp);
1338 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
1339 char *data, uint32_t data_size)
1341 struct nlmsghdr *nlh;
1342 struct sk_buff *skb;
1343 struct iscsi_uevent *ev;
1345 struct iscsi_internal *priv;
1346 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1349 priv = iscsi_if_transport_lookup(conn->transport);
1353 skb = alloc_skb(len, GFP_ATOMIC);
1355 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
1356 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1357 "control PDU: OOM\n");
1361 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1362 ev = NLMSG_DATA(nlh);
1363 memset(ev, 0, sizeof(*ev));
1364 ev->transport_handle = iscsi_handle(conn->transport);
1365 ev->type = ISCSI_KEVENT_RECV_PDU;
1366 ev->r.recv_req.cid = conn->cid;
1367 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
1368 pdu = (char*)ev + sizeof(*ev);
1369 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1370 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1372 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
1374 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1376 int iscsi_offload_mesg(struct Scsi_Host *shost,
1377 struct iscsi_transport *transport, uint32_t type,
1378 char *data, uint16_t data_size)
1380 struct nlmsghdr *nlh;
1381 struct sk_buff *skb;
1382 struct iscsi_uevent *ev;
1383 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
1385 skb = alloc_skb(len, GFP_ATOMIC);
1387 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
1391 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1392 ev = NLMSG_DATA(nlh);
1393 memset(ev, 0, sizeof(*ev));
1395 ev->transport_handle = iscsi_handle(transport);
1397 case ISCSI_KEVENT_PATH_REQ:
1398 ev->r.req_path.host_no = shost->host_no;
1400 case ISCSI_KEVENT_IF_DOWN:
1401 ev->r.notify_if_down.host_no = shost->host_no;
1405 memcpy((char *)ev + sizeof(*ev), data, data_size);
1407 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
1409 EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1411 void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
1413 struct nlmsghdr *nlh;
1414 struct sk_buff *skb;
1415 struct iscsi_uevent *ev;
1416 struct iscsi_internal *priv;
1417 int len = NLMSG_SPACE(sizeof(*ev));
1419 priv = iscsi_if_transport_lookup(conn->transport);
1423 skb = alloc_skb(len, GFP_ATOMIC);
1425 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1426 "conn error (%d)\n", error);
1430 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1431 ev = NLMSG_DATA(nlh);
1432 ev->transport_handle = iscsi_handle(conn->transport);
1433 ev->type = ISCSI_KEVENT_CONN_ERROR;
1434 ev->r.connerror.error = error;
1435 ev->r.connerror.cid = conn->cid;
1436 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
1438 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
1440 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1443 EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
1445 void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
1446 enum iscsi_conn_state state)
1448 struct nlmsghdr *nlh;
1449 struct sk_buff *skb;
1450 struct iscsi_uevent *ev;
1451 struct iscsi_internal *priv;
1452 int len = NLMSG_SPACE(sizeof(*ev));
1454 priv = iscsi_if_transport_lookup(conn->transport);
1458 skb = alloc_skb(len, GFP_ATOMIC);
1460 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1461 "conn login (%d)\n", state);
1465 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1466 ev = NLMSG_DATA(nlh);
1467 ev->transport_handle = iscsi_handle(conn->transport);
1468 ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
1469 ev->r.conn_login.state = state;
1470 ev->r.conn_login.cid = conn->cid;
1471 ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
1472 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
1474 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
1477 EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
1480 iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1481 void *payload, int size)
1483 struct sk_buff *skb;
1484 struct nlmsghdr *nlh;
1485 int len = NLMSG_SPACE(size);
1486 int flags = multi ? NLM_F_MULTI : 0;
1487 int t = done ? NLMSG_DONE : type;
1489 skb = alloc_skb(len, GFP_ATOMIC);
1491 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1495 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
1496 nlh->nlmsg_flags = flags;
1497 memcpy(NLMSG_DATA(nlh), payload, size);
1498 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
1502 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
1504 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1505 struct iscsi_stats *stats;
1506 struct sk_buff *skbstat;
1507 struct iscsi_cls_conn *conn;
1508 struct nlmsghdr *nlhstat;
1509 struct iscsi_uevent *evstat;
1510 struct iscsi_internal *priv;
1511 int len = NLMSG_SPACE(sizeof(*ev) +
1512 sizeof(struct iscsi_stats) +
1513 sizeof(struct iscsi_stats_custom) *
1514 ISCSI_STATS_CUSTOM_MAX);
1517 priv = iscsi_if_transport_lookup(transport);
1521 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
1528 skbstat = alloc_skb(len, GFP_ATOMIC);
1530 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1531 "deliver stats: OOM\n");
1535 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
1536 (len - sizeof(*nlhstat)), 0);
1537 evstat = NLMSG_DATA(nlhstat);
1538 memset(evstat, 0, sizeof(*evstat));
1539 evstat->transport_handle = iscsi_handle(conn->transport);
1540 evstat->type = nlh->nlmsg_type;
1541 evstat->u.get_stats.cid =
1542 ev->u.get_stats.cid;
1543 evstat->u.get_stats.sid =
1544 ev->u.get_stats.sid;
1545 stats = (struct iscsi_stats *)
1546 ((char*)evstat + sizeof(*evstat));
1547 memset(stats, 0, sizeof(*stats));
1549 transport->get_stats(conn, stats);
1550 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1551 sizeof(struct iscsi_stats) +
1552 sizeof(struct iscsi_stats_custom) *
1553 stats->custom_length);
1554 actual_size -= sizeof(*nlhstat);
1555 actual_size = NLMSG_LENGTH(actual_size);
1556 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
1557 nlhstat->nlmsg_len = actual_size;
1559 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1561 } while (err < 0 && err != -ECONNREFUSED);
1567 * iscsi_session_event - send session destr. completion event
1568 * @session: iscsi class session
1569 * @event: type of event
1571 int iscsi_session_event(struct iscsi_cls_session *session,
1572 enum iscsi_uevent_e event)
1574 struct iscsi_internal *priv;
1575 struct Scsi_Host *shost;
1576 struct iscsi_uevent *ev;
1577 struct sk_buff *skb;
1578 struct nlmsghdr *nlh;
1579 int rc, len = NLMSG_SPACE(sizeof(*ev));
1581 priv = iscsi_if_transport_lookup(session->transport);
1584 shost = iscsi_session_to_shost(session);
1586 skb = alloc_skb(len, GFP_KERNEL);
1588 iscsi_cls_session_printk(KERN_ERR, session,
1589 "Cannot notify userspace of session "
1590 "event %u\n", event);
1594 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1595 ev = NLMSG_DATA(nlh);
1596 ev->transport_handle = iscsi_handle(session->transport);
1600 case ISCSI_KEVENT_DESTROY_SESSION:
1601 ev->r.d_session.host_no = shost->host_no;
1602 ev->r.d_session.sid = session->sid;
1604 case ISCSI_KEVENT_CREATE_SESSION:
1605 ev->r.c_session_ret.host_no = shost->host_no;
1606 ev->r.c_session_ret.sid = session->sid;
1608 case ISCSI_KEVENT_UNBIND_SESSION:
1609 ev->r.unbind_session.host_no = shost->host_no;
1610 ev->r.unbind_session.sid = session->sid;
1613 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1620 * this will occur if the daemon is not up, so we just warn
1621 * the user and when the daemon is restarted it will handle it
1623 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
1625 iscsi_cls_session_printk(KERN_ERR, session,
1626 "Cannot notify userspace of session "
1627 "event %u. Check iscsi daemon\n",
1630 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
1634 EXPORT_SYMBOL_GPL(iscsi_session_event);
1637 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1638 struct iscsi_uevent *ev, pid_t pid,
1639 uint32_t initial_cmdsn, uint16_t cmds_max,
1640 uint16_t queue_depth)
1642 struct iscsi_transport *transport = priv->iscsi_transport;
1643 struct iscsi_cls_session *session;
1644 struct Scsi_Host *shost;
1646 session = transport->create_session(ep, cmds_max, queue_depth,
1651 session->creator = pid;
1652 shost = iscsi_session_to_shost(session);
1653 ev->r.c_session_ret.host_no = shost->host_no;
1654 ev->r.c_session_ret.sid = session->sid;
1655 ISCSI_DBG_TRANS_SESSION(session,
1656 "Completed creating transport session\n");
1661 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1663 struct iscsi_cls_conn *conn;
1664 struct iscsi_cls_session *session;
1666 session = iscsi_session_lookup(ev->u.c_conn.sid);
1668 printk(KERN_ERR "iscsi: invalid session %d.\n",
1673 conn = transport->create_conn(session, ev->u.c_conn.cid);
1675 iscsi_cls_session_printk(KERN_ERR, session,
1676 "couldn't create a new connection.");
1680 ev->r.c_conn_ret.sid = session->sid;
1681 ev->r.c_conn_ret.cid = conn->cid;
1683 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
1688 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1690 struct iscsi_cls_conn *conn;
1692 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
1696 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
1697 if (transport->destroy_conn)
1698 transport->destroy_conn(conn);
1704 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1706 char *data = (char*)ev + sizeof(*ev);
1707 struct iscsi_cls_conn *conn;
1708 struct iscsi_cls_session *session;
1709 int err = 0, value = 0;
1711 session = iscsi_session_lookup(ev->u.set_param.sid);
1712 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1713 if (!conn || !session)
1716 switch (ev->u.set_param.param) {
1717 case ISCSI_PARAM_SESS_RECOVERY_TMO:
1718 sscanf(data, "%d", &value);
1719 session->recovery_tmo = value;
1722 err = transport->set_param(conn, ev->u.set_param.param,
1723 data, ev->u.set_param.len);
1729 static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1730 struct iscsi_uevent *ev, int msg_type)
1732 struct iscsi_endpoint *ep;
1733 struct sockaddr *dst_addr;
1734 struct Scsi_Host *shost = NULL;
1735 int non_blocking, err = 0;
1737 if (!transport->ep_connect)
1740 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1741 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1743 printk(KERN_ERR "ep connect failed. Could not find "
1745 ev->u.ep_connect_through_host.host_no);
1748 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1750 non_blocking = ev->u.ep_connect.non_blocking;
1752 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1753 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1759 ev->r.ep_connect_ret.handle = ep->id;
1762 scsi_host_put(shost);
1766 static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
1769 struct iscsi_cls_conn *conn;
1770 struct iscsi_endpoint *ep;
1772 if (!transport->ep_disconnect)
1775 ep = iscsi_lookup_endpoint(ep_handle);
1780 mutex_lock(&conn->ep_mutex);
1782 mutex_unlock(&conn->ep_mutex);
1785 transport->ep_disconnect(ep);
1790 iscsi_if_transport_ep(struct iscsi_transport *transport,
1791 struct iscsi_uevent *ev, int msg_type)
1793 struct iscsi_endpoint *ep;
1797 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
1798 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1799 rc = iscsi_if_ep_connect(transport, ev, msg_type);
1801 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1802 if (!transport->ep_poll)
1805 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1809 ev->r.retcode = transport->ep_poll(ep,
1810 ev->u.ep_poll.timeout_ms);
1812 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1813 rc = iscsi_if_ep_disconnect(transport,
1814 ev->u.ep_disconnect.ep_handle);
1821 iscsi_tgt_dscvr(struct iscsi_transport *transport,
1822 struct iscsi_uevent *ev)
1824 struct Scsi_Host *shost;
1825 struct sockaddr *dst_addr;
1828 if (!transport->tgt_dscvr)
1831 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1833 printk(KERN_ERR "target discovery could not find host no %u\n",
1834 ev->u.tgt_dscvr.host_no);
1839 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1840 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1841 ev->u.tgt_dscvr.enable, dst_addr);
1842 scsi_host_put(shost);
1847 iscsi_set_host_param(struct iscsi_transport *transport,
1848 struct iscsi_uevent *ev)
1850 char *data = (char*)ev + sizeof(*ev);
1851 struct Scsi_Host *shost;
1854 if (!transport->set_host_param)
1857 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1859 printk(KERN_ERR "set_host_param could not find host no %u\n",
1860 ev->u.set_host_param.host_no);
1864 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1865 data, ev->u.set_host_param.len);
1866 scsi_host_put(shost);
1871 iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1873 struct Scsi_Host *shost;
1874 struct iscsi_path *params;
1877 if (!transport->set_path)
1880 shost = scsi_host_lookup(ev->u.set_path.host_no);
1882 printk(KERN_ERR "set path could not find host no %u\n",
1883 ev->u.set_path.host_no);
1887 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1888 err = transport->set_path(shost, params);
1890 scsi_host_put(shost);
1895 iscsi_set_iface_params(struct iscsi_transport *transport,
1896 struct iscsi_uevent *ev, uint32_t len)
1898 char *data = (char *)ev + sizeof(*ev);
1899 struct Scsi_Host *shost;
1902 if (!transport->set_iface_param)
1905 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
1907 printk(KERN_ERR "set_iface_params could not find host no %u\n",
1908 ev->u.set_iface_params.host_no);
1912 err = transport->set_iface_param(shost, data, len);
1913 scsi_host_put(shost);
1918 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
1921 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1922 struct iscsi_transport *transport = NULL;
1923 struct iscsi_internal *priv;
1924 struct iscsi_cls_session *session;
1925 struct iscsi_cls_conn *conn;
1926 struct iscsi_endpoint *ep = NULL;
1928 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1929 *group = ISCSI_NL_GRP_UIP;
1931 *group = ISCSI_NL_GRP_ISCSID;
1933 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1936 transport = priv->iscsi_transport;
1938 if (!try_module_get(transport->owner))
1941 switch (nlh->nlmsg_type) {
1942 case ISCSI_UEVENT_CREATE_SESSION:
1943 err = iscsi_if_create_session(priv, ep, ev,
1944 NETLINK_CREDS(skb)->pid,
1945 ev->u.c_session.initial_cmdsn,
1946 ev->u.c_session.cmds_max,
1947 ev->u.c_session.queue_depth);
1949 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
1950 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
1956 err = iscsi_if_create_session(priv, ep, ev,
1957 NETLINK_CREDS(skb)->pid,
1958 ev->u.c_bound_session.initial_cmdsn,
1959 ev->u.c_bound_session.cmds_max,
1960 ev->u.c_bound_session.queue_depth);
1962 case ISCSI_UEVENT_DESTROY_SESSION:
1963 session = iscsi_session_lookup(ev->u.d_session.sid);
1965 transport->destroy_session(session);
1969 case ISCSI_UEVENT_UNBIND_SESSION:
1970 session = iscsi_session_lookup(ev->u.d_session.sid);
1972 scsi_queue_work(iscsi_session_to_shost(session),
1973 &session->unbind_work);
1977 case ISCSI_UEVENT_CREATE_CONN:
1978 err = iscsi_if_create_conn(transport, ev);
1980 case ISCSI_UEVENT_DESTROY_CONN:
1981 err = iscsi_if_destroy_conn(transport, ev);
1983 case ISCSI_UEVENT_BIND_CONN:
1984 session = iscsi_session_lookup(ev->u.b_conn.sid);
1985 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1987 if (conn && conn->ep)
1988 iscsi_if_ep_disconnect(transport, conn->ep->id);
1990 if (!session || !conn) {
1995 ev->r.retcode = transport->bind_conn(session, conn,
1996 ev->u.b_conn.transport_eph,
1997 ev->u.b_conn.is_leading);
1998 if (ev->r.retcode || !transport->ep_connect)
2001 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
2005 mutex_lock(&conn->ep_mutex);
2007 mutex_unlock(&conn->ep_mutex);
2009 iscsi_cls_conn_printk(KERN_ERR, conn,
2010 "Could not set ep conn "
2013 case ISCSI_UEVENT_SET_PARAM:
2014 err = iscsi_set_param(transport, ev);
2016 case ISCSI_UEVENT_START_CONN:
2017 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
2019 ev->r.retcode = transport->start_conn(conn);
2023 case ISCSI_UEVENT_STOP_CONN:
2024 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
2026 transport->stop_conn(conn, ev->u.stop_conn.flag);
2030 case ISCSI_UEVENT_SEND_PDU:
2031 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
2033 ev->r.retcode = transport->send_pdu(conn,
2034 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
2035 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
2036 ev->u.send_pdu.data_size);
2040 case ISCSI_UEVENT_GET_STATS:
2041 err = iscsi_if_get_stats(transport, nlh);
2043 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
2044 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
2045 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
2046 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
2047 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
2049 case ISCSI_UEVENT_TGT_DSCVR:
2050 err = iscsi_tgt_dscvr(transport, ev);
2052 case ISCSI_UEVENT_SET_HOST_PARAM:
2053 err = iscsi_set_host_param(transport, ev);
2055 case ISCSI_UEVENT_PATH_UPDATE:
2056 err = iscsi_set_path(transport, ev);
2058 case ISCSI_UEVENT_SET_IFACE_PARAMS:
2059 err = iscsi_set_iface_params(transport, ev,
2060 nlmsg_attrlen(nlh, sizeof(*ev)));
2067 module_put(transport->owner);
2072 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
2073 * Malformed skbs with wrong lengths or invalid creds are not processed.
2076 iscsi_if_rx(struct sk_buff *skb)
2078 mutex_lock(&rx_queue_mutex);
2079 while (skb->len >= NLMSG_SPACE(0)) {
2082 struct nlmsghdr *nlh;
2083 struct iscsi_uevent *ev;
2086 nlh = nlmsg_hdr(skb);
2087 if (nlh->nlmsg_len < sizeof(*nlh) ||
2088 skb->len < nlh->nlmsg_len) {
2092 ev = NLMSG_DATA(nlh);
2093 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
2094 if (rlen > skb->len)
2097 err = iscsi_if_recv_msg(skb, nlh, &group);
2099 ev->type = ISCSI_KEVENT_IF_ERROR;
2104 * special case for GET_STATS:
2105 * on success - sending reply and stats from
2106 * inside of if_recv_msg(),
2107 * on error - fall through.
2109 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
2111 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
2112 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
2113 } while (err < 0 && err != -ECONNREFUSED);
2114 skb_pull(skb, rlen);
2116 mutex_unlock(&rx_queue_mutex);
2119 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
2120 struct device_attribute dev_attr_##_prefix##_##_name = \
2121 __ATTR(_name,_mode,_show,_store)
2124 * iSCSI connection attrs
2126 #define iscsi_conn_attr_show(param) \
2128 show_conn_param_##param(struct device *dev, \
2129 struct device_attribute *attr, char *buf) \
2131 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
2132 struct iscsi_transport *t = conn->transport; \
2133 return t->get_conn_param(conn, param, buf); \
2136 #define iscsi_conn_attr(field, param) \
2137 iscsi_conn_attr_show(param) \
2138 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
2141 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
2142 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
2143 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
2144 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
2145 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
2146 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
2147 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
2148 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
2149 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
2150 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
2151 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
2153 #define iscsi_conn_ep_attr_show(param) \
2154 static ssize_t show_conn_ep_param_##param(struct device *dev, \
2155 struct device_attribute *attr,\
2158 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
2159 struct iscsi_transport *t = conn->transport; \
2160 struct iscsi_endpoint *ep; \
2164 * Need to make sure ep_disconnect does not free the LLD's \
2165 * interconnect resources while we are trying to read them. \
2167 mutex_lock(&conn->ep_mutex); \
2169 if (!ep && t->ep_connect) { \
2170 mutex_unlock(&conn->ep_mutex); \
2175 rc = t->get_ep_param(ep, param, buf); \
2177 rc = t->get_conn_param(conn, param, buf); \
2178 mutex_unlock(&conn->ep_mutex); \
2182 #define iscsi_conn_ep_attr(field, param) \
2183 iscsi_conn_ep_attr_show(param) \
2184 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
2185 show_conn_ep_param_##param, NULL);
2187 iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
2188 iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
2190 static struct attribute *iscsi_conn_attrs[] = {
2191 &dev_attr_conn_max_recv_dlength.attr,
2192 &dev_attr_conn_max_xmit_dlength.attr,
2193 &dev_attr_conn_header_digest.attr,
2194 &dev_attr_conn_data_digest.attr,
2195 &dev_attr_conn_ifmarker.attr,
2196 &dev_attr_conn_ofmarker.attr,
2197 &dev_attr_conn_address.attr,
2198 &dev_attr_conn_port.attr,
2199 &dev_attr_conn_exp_statsn.attr,
2200 &dev_attr_conn_persistent_address.attr,
2201 &dev_attr_conn_persistent_port.attr,
2202 &dev_attr_conn_ping_tmo.attr,
2203 &dev_attr_conn_recv_tmo.attr,
2207 static umode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
2208 struct attribute *attr, int i)
2210 struct device *cdev = container_of(kobj, struct device, kobj);
2211 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
2212 struct iscsi_transport *t = conn->transport;
2215 if (attr == &dev_attr_conn_max_recv_dlength.attr)
2216 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
2217 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
2218 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
2219 else if (attr == &dev_attr_conn_header_digest.attr)
2220 param = ISCSI_PARAM_HDRDGST_EN;
2221 else if (attr == &dev_attr_conn_data_digest.attr)
2222 param = ISCSI_PARAM_DATADGST_EN;
2223 else if (attr == &dev_attr_conn_ifmarker.attr)
2224 param = ISCSI_PARAM_IFMARKER_EN;
2225 else if (attr == &dev_attr_conn_ofmarker.attr)
2226 param = ISCSI_PARAM_OFMARKER_EN;
2227 else if (attr == &dev_attr_conn_address.attr)
2228 param = ISCSI_PARAM_CONN_ADDRESS;
2229 else if (attr == &dev_attr_conn_port.attr)
2230 param = ISCSI_PARAM_CONN_PORT;
2231 else if (attr == &dev_attr_conn_exp_statsn.attr)
2232 param = ISCSI_PARAM_EXP_STATSN;
2233 else if (attr == &dev_attr_conn_persistent_address.attr)
2234 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
2235 else if (attr == &dev_attr_conn_persistent_port.attr)
2236 param = ISCSI_PARAM_PERSISTENT_PORT;
2237 else if (attr == &dev_attr_conn_ping_tmo.attr)
2238 param = ISCSI_PARAM_PING_TMO;
2239 else if (attr == &dev_attr_conn_recv_tmo.attr)
2240 param = ISCSI_PARAM_RECV_TMO;
2242 WARN_ONCE(1, "Invalid conn attr");
2246 return t->attr_is_visible(ISCSI_PARAM, param);
2249 static struct attribute_group iscsi_conn_group = {
2250 .attrs = iscsi_conn_attrs,
2251 .is_visible = iscsi_conn_attr_is_visible,
2255 * iSCSI session attrs
2257 #define iscsi_session_attr_show(param, perm) \
2259 show_session_param_##param(struct device *dev, \
2260 struct device_attribute *attr, char *buf) \
2262 struct iscsi_cls_session *session = \
2263 iscsi_dev_to_session(dev->parent); \
2264 struct iscsi_transport *t = session->transport; \
2266 if (perm && !capable(CAP_SYS_ADMIN)) \
2268 return t->get_session_param(session, param, buf); \
2271 #define iscsi_session_attr(field, param, perm) \
2272 iscsi_session_attr_show(param, perm) \
2273 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
2275 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
2276 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
2277 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
2278 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
2279 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
2280 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
2281 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
2282 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
2283 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
2284 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
2285 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
2286 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
2287 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
2288 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
2289 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
2290 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
2291 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
2292 iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
2293 iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
2294 iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
2295 iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
2298 show_priv_session_state(struct device *dev, struct device_attribute *attr,
2301 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
2302 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
2304 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
2307 show_priv_session_creator(struct device *dev, struct device_attribute *attr,
2310 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
2311 return sprintf(buf, "%d\n", session->creator);
2313 static ISCSI_CLASS_ATTR(priv_sess, creator, S_IRUGO, show_priv_session_creator,
2316 #define iscsi_priv_session_attr_show(field, format) \
2318 show_priv_session_##field(struct device *dev, \
2319 struct device_attribute *attr, char *buf) \
2321 struct iscsi_cls_session *session = \
2322 iscsi_dev_to_session(dev->parent); \
2323 if (session->field == -1) \
2324 return sprintf(buf, "off\n"); \
2325 return sprintf(buf, format"\n", session->field); \
2328 #define iscsi_priv_session_attr_store(field) \
2330 store_priv_session_##field(struct device *dev, \
2331 struct device_attribute *attr, \
2332 const char *buf, size_t count) \
2336 struct iscsi_cls_session *session = \
2337 iscsi_dev_to_session(dev->parent); \
2338 if ((session->state == ISCSI_SESSION_FREE) || \
2339 (session->state == ISCSI_SESSION_FAILED)) \
2341 if (strncmp(buf, "off", 3) == 0) \
2342 session->field = -1; \
2344 val = simple_strtoul(buf, &cp, 0); \
2345 if (*cp != '\0' && *cp != '\n') \
2347 session->field = val; \
2352 #define iscsi_priv_session_rw_attr(field, format) \
2353 iscsi_priv_session_attr_show(field, format) \
2354 iscsi_priv_session_attr_store(field) \
2355 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
2356 show_priv_session_##field, \
2357 store_priv_session_##field)
2358 iscsi_priv_session_rw_attr(recovery_tmo, "%d");
2360 static struct attribute *iscsi_session_attrs[] = {
2361 &dev_attr_sess_initial_r2t.attr,
2362 &dev_attr_sess_max_outstanding_r2t.attr,
2363 &dev_attr_sess_immediate_data.attr,
2364 &dev_attr_sess_first_burst_len.attr,
2365 &dev_attr_sess_max_burst_len.attr,
2366 &dev_attr_sess_data_pdu_in_order.attr,
2367 &dev_attr_sess_data_seq_in_order.attr,
2368 &dev_attr_sess_erl.attr,
2369 &dev_attr_sess_targetname.attr,
2370 &dev_attr_sess_tpgt.attr,
2371 &dev_attr_sess_password.attr,
2372 &dev_attr_sess_password_in.attr,
2373 &dev_attr_sess_username.attr,
2374 &dev_attr_sess_username_in.attr,
2375 &dev_attr_sess_fast_abort.attr,
2376 &dev_attr_sess_abort_tmo.attr,
2377 &dev_attr_sess_lu_reset_tmo.attr,
2378 &dev_attr_sess_tgt_reset_tmo.attr,
2379 &dev_attr_sess_ifacename.attr,
2380 &dev_attr_sess_initiatorname.attr,
2381 &dev_attr_sess_targetalias.attr,
2382 &dev_attr_priv_sess_recovery_tmo.attr,
2383 &dev_attr_priv_sess_state.attr,
2384 &dev_attr_priv_sess_creator.attr,
2388 static umode_t iscsi_session_attr_is_visible(struct kobject *kobj,
2389 struct attribute *attr, int i)
2391 struct device *cdev = container_of(kobj, struct device, kobj);
2392 struct iscsi_cls_session *session = transport_class_to_session(cdev);
2393 struct iscsi_transport *t = session->transport;
2396 if (attr == &dev_attr_sess_initial_r2t.attr)
2397 param = ISCSI_PARAM_INITIAL_R2T_EN;
2398 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
2399 param = ISCSI_PARAM_MAX_R2T;
2400 else if (attr == &dev_attr_sess_immediate_data.attr)
2401 param = ISCSI_PARAM_IMM_DATA_EN;
2402 else if (attr == &dev_attr_sess_first_burst_len.attr)
2403 param = ISCSI_PARAM_FIRST_BURST;
2404 else if (attr == &dev_attr_sess_max_burst_len.attr)
2405 param = ISCSI_PARAM_MAX_BURST;
2406 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
2407 param = ISCSI_PARAM_PDU_INORDER_EN;
2408 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
2409 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
2410 else if (attr == &dev_attr_sess_erl.attr)
2411 param = ISCSI_PARAM_ERL;
2412 else if (attr == &dev_attr_sess_targetname.attr)
2413 param = ISCSI_PARAM_TARGET_NAME;
2414 else if (attr == &dev_attr_sess_tpgt.attr)
2415 param = ISCSI_PARAM_TPGT;
2416 else if (attr == &dev_attr_sess_password.attr)
2417 param = ISCSI_PARAM_USERNAME;
2418 else if (attr == &dev_attr_sess_password_in.attr)
2419 param = ISCSI_PARAM_USERNAME_IN;
2420 else if (attr == &dev_attr_sess_username.attr)
2421 param = ISCSI_PARAM_PASSWORD;
2422 else if (attr == &dev_attr_sess_username_in.attr)
2423 param = ISCSI_PARAM_PASSWORD_IN;
2424 else if (attr == &dev_attr_sess_fast_abort.attr)
2425 param = ISCSI_PARAM_FAST_ABORT;
2426 else if (attr == &dev_attr_sess_abort_tmo.attr)
2427 param = ISCSI_PARAM_ABORT_TMO;
2428 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
2429 param = ISCSI_PARAM_LU_RESET_TMO;
2430 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
2431 param = ISCSI_PARAM_TGT_RESET_TMO;
2432 else if (attr == &dev_attr_sess_ifacename.attr)
2433 param = ISCSI_PARAM_IFACE_NAME;
2434 else if (attr == &dev_attr_sess_initiatorname.attr)
2435 param = ISCSI_PARAM_INITIATOR_NAME;
2436 else if (attr == &dev_attr_sess_targetalias.attr)
2437 param = ISCSI_PARAM_TARGET_ALIAS;
2438 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
2439 return S_IRUGO | S_IWUSR;
2440 else if (attr == &dev_attr_priv_sess_state.attr)
2442 else if (attr == &dev_attr_priv_sess_creator.attr)
2445 WARN_ONCE(1, "Invalid session attr");
2449 return t->attr_is_visible(ISCSI_PARAM, param);
2452 static struct attribute_group iscsi_session_group = {
2453 .attrs = iscsi_session_attrs,
2454 .is_visible = iscsi_session_attr_is_visible,
2460 #define iscsi_host_attr_show(param) \
2462 show_host_param_##param(struct device *dev, \
2463 struct device_attribute *attr, char *buf) \
2465 struct Scsi_Host *shost = transport_class_to_shost(dev); \
2466 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
2467 return priv->iscsi_transport->get_host_param(shost, param, buf); \
2470 #define iscsi_host_attr(field, param) \
2471 iscsi_host_attr_show(param) \
2472 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
2475 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
2476 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
2477 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
2478 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
2480 static struct attribute *iscsi_host_attrs[] = {
2481 &dev_attr_host_netdev.attr,
2482 &dev_attr_host_hwaddress.attr,
2483 &dev_attr_host_ipaddress.attr,
2484 &dev_attr_host_initiatorname.attr,
2488 static umode_t iscsi_host_attr_is_visible(struct kobject *kobj,
2489 struct attribute *attr, int i)
2491 struct device *cdev = container_of(kobj, struct device, kobj);
2492 struct Scsi_Host *shost = transport_class_to_shost(cdev);
2493 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
2496 if (attr == &dev_attr_host_netdev.attr)
2497 param = ISCSI_HOST_PARAM_NETDEV_NAME;
2498 else if (attr == &dev_attr_host_hwaddress.attr)
2499 param = ISCSI_HOST_PARAM_HWADDRESS;
2500 else if (attr == &dev_attr_host_ipaddress.attr)
2501 param = ISCSI_HOST_PARAM_IPADDRESS;
2502 else if (attr == &dev_attr_host_initiatorname.attr)
2503 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
2505 WARN_ONCE(1, "Invalid host attr");
2509 return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
2512 static struct attribute_group iscsi_host_group = {
2513 .attrs = iscsi_host_attrs,
2514 .is_visible = iscsi_host_attr_is_visible,
2517 static int iscsi_session_match(struct attribute_container *cont,
2520 struct iscsi_cls_session *session;
2521 struct Scsi_Host *shost;
2522 struct iscsi_internal *priv;
2524 if (!iscsi_is_session_dev(dev))
2527 session = iscsi_dev_to_session(dev);
2528 shost = iscsi_session_to_shost(session);
2529 if (!shost->transportt)
2532 priv = to_iscsi_internal(shost->transportt);
2533 if (priv->session_cont.ac.class != &iscsi_session_class.class)
2536 return &priv->session_cont.ac == cont;
2539 static int iscsi_conn_match(struct attribute_container *cont,
2542 struct iscsi_cls_session *session;
2543 struct iscsi_cls_conn *conn;
2544 struct Scsi_Host *shost;
2545 struct iscsi_internal *priv;
2547 if (!iscsi_is_conn_dev(dev))
2550 conn = iscsi_dev_to_conn(dev);
2551 session = iscsi_dev_to_session(conn->dev.parent);
2552 shost = iscsi_session_to_shost(session);
2554 if (!shost->transportt)
2557 priv = to_iscsi_internal(shost->transportt);
2558 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
2561 return &priv->conn_cont.ac == cont;
2564 static int iscsi_host_match(struct attribute_container *cont,
2567 struct Scsi_Host *shost;
2568 struct iscsi_internal *priv;
2570 if (!scsi_is_host_device(dev))
2573 shost = dev_to_shost(dev);
2574 if (!shost->transportt ||
2575 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
2578 priv = to_iscsi_internal(shost->transportt);
2579 return &priv->t.host_attrs.ac == cont;
2582 struct scsi_transport_template *
2583 iscsi_register_transport(struct iscsi_transport *tt)
2585 struct iscsi_internal *priv;
2586 unsigned long flags;
2591 priv = iscsi_if_transport_lookup(tt);
2595 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2598 INIT_LIST_HEAD(&priv->list);
2599 priv->iscsi_transport = tt;
2600 priv->t.user_scan = iscsi_user_scan;
2601 priv->t.create_work_queue = 1;
2603 priv->dev.class = &iscsi_transport_class;
2604 dev_set_name(&priv->dev, "%s", tt->name);
2605 err = device_register(&priv->dev);
2609 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
2611 goto unregister_dev;
2613 /* host parameters */
2614 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
2615 priv->t.host_attrs.ac.match = iscsi_host_match;
2616 priv->t.host_attrs.ac.grp = &iscsi_host_group;
2617 priv->t.host_size = sizeof(struct iscsi_cls_host);
2618 transport_container_register(&priv->t.host_attrs);
2620 /* connection parameters */
2621 priv->conn_cont.ac.class = &iscsi_connection_class.class;
2622 priv->conn_cont.ac.match = iscsi_conn_match;
2623 priv->conn_cont.ac.grp = &iscsi_conn_group;
2624 transport_container_register(&priv->conn_cont);
2626 /* session parameters */
2627 priv->session_cont.ac.class = &iscsi_session_class.class;
2628 priv->session_cont.ac.match = iscsi_session_match;
2629 priv->session_cont.ac.grp = &iscsi_session_group;
2630 transport_container_register(&priv->session_cont);
2632 spin_lock_irqsave(&iscsi_transport_lock, flags);
2633 list_add(&priv->list, &iscsi_transports);
2634 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2636 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
2640 device_unregister(&priv->dev);
2646 EXPORT_SYMBOL_GPL(iscsi_register_transport);
2648 int iscsi_unregister_transport(struct iscsi_transport *tt)
2650 struct iscsi_internal *priv;
2651 unsigned long flags;
2655 mutex_lock(&rx_queue_mutex);
2657 priv = iscsi_if_transport_lookup(tt);
2660 spin_lock_irqsave(&iscsi_transport_lock, flags);
2661 list_del(&priv->list);
2662 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2664 transport_container_unregister(&priv->conn_cont);
2665 transport_container_unregister(&priv->session_cont);
2666 transport_container_unregister(&priv->t.host_attrs);
2668 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
2669 device_unregister(&priv->dev);
2670 mutex_unlock(&rx_queue_mutex);
2674 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
2676 static __init int iscsi_transport_init(void)
2680 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
2681 ISCSI_TRANSPORT_VERSION);
2683 atomic_set(&iscsi_session_nr, 0);
2685 err = class_register(&iscsi_transport_class);
2689 err = class_register(&iscsi_endpoint_class);
2691 goto unregister_transport_class;
2693 err = class_register(&iscsi_iface_class);
2695 goto unregister_endpoint_class;
2697 err = transport_class_register(&iscsi_host_class);
2699 goto unregister_iface_class;
2701 err = transport_class_register(&iscsi_connection_class);
2703 goto unregister_host_class;
2705 err = transport_class_register(&iscsi_session_class);
2707 goto unregister_conn_class;
2709 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2713 goto unregister_session_class;
2716 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2717 if (!iscsi_eh_timer_workq)
2723 netlink_kernel_release(nls);
2724 unregister_session_class:
2725 transport_class_unregister(&iscsi_session_class);
2726 unregister_conn_class:
2727 transport_class_unregister(&iscsi_connection_class);
2728 unregister_host_class:
2729 transport_class_unregister(&iscsi_host_class);
2730 unregister_iface_class:
2731 class_unregister(&iscsi_iface_class);
2732 unregister_endpoint_class:
2733 class_unregister(&iscsi_endpoint_class);
2734 unregister_transport_class:
2735 class_unregister(&iscsi_transport_class);
2739 static void __exit iscsi_transport_exit(void)
2741 destroy_workqueue(iscsi_eh_timer_workq);
2742 netlink_kernel_release(nls);
2743 transport_class_unregister(&iscsi_connection_class);
2744 transport_class_unregister(&iscsi_session_class);
2745 transport_class_unregister(&iscsi_host_class);
2746 class_unregister(&iscsi_endpoint_class);
2747 class_unregister(&iscsi_iface_class);
2748 class_unregister(&iscsi_transport_class);
2751 module_init(iscsi_transport_init);
2752 module_exit(iscsi_transport_exit);
2754 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2755 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2756 "Alex Aizman <itn780@yahoo.com>");
2757 MODULE_DESCRIPTION("iSCSI Transport Interface");
2758 MODULE_LICENSE("GPL");
2759 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
2760 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);