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 mode_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->recovery_tmo = 120;
1034 session->state = ISCSI_SESSION_FREE;
1035 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
1036 INIT_LIST_HEAD(&session->sess_list);
1037 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
1038 INIT_WORK(&session->block_work, __iscsi_block_session);
1039 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
1040 INIT_WORK(&session->scan_work, iscsi_scan_session);
1041 spin_lock_init(&session->lock);
1043 /* this is released in the dev's release function */
1044 scsi_host_get(shost);
1045 session->dev.parent = &shost->shost_gendev;
1046 session->dev.release = iscsi_session_release;
1047 device_initialize(&session->dev);
1049 session->dd_data = &session[1];
1051 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
1054 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
1056 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
1058 struct Scsi_Host *shost = iscsi_session_to_shost(session);
1059 struct iscsi_cls_host *ihost;
1060 unsigned long flags;
1064 ihost = shost->shost_data;
1065 session->sid = atomic_add_return(1, &iscsi_session_nr);
1067 if (target_id == ISCSI_MAX_TARGET) {
1068 id = ida_simple_get(&iscsi_sess_ida, 0, 0, GFP_KERNEL);
1071 iscsi_cls_session_printk(KERN_ERR, session,
1072 "Failure in Target ID Allocation\n");
1075 session->target_id = (unsigned int)id;
1076 session->ida_used = true;
1078 session->target_id = target_id;
1080 dev_set_name(&session->dev, "session%u", session->sid);
1081 err = device_add(&session->dev);
1083 iscsi_cls_session_printk(KERN_ERR, session,
1084 "could not register session's dev\n");
1087 transport_register_device(&session->dev);
1089 spin_lock_irqsave(&sesslock, flags);
1090 list_add(&session->sess_list, &sesslist);
1091 spin_unlock_irqrestore(&sesslock, flags);
1093 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
1094 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
1098 if (session->ida_used)
1099 ida_simple_remove(&iscsi_sess_ida, session->target_id);
1103 EXPORT_SYMBOL_GPL(iscsi_add_session);
1106 * iscsi_create_session - create iscsi class session
1108 * @transport: iscsi transport
1109 * @dd_size: private driver data size
1110 * @target_id: which target
1112 * This can be called from a LLD or iscsi_transport.
1114 struct iscsi_cls_session *
1115 iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1116 int dd_size, unsigned int target_id)
1118 struct iscsi_cls_session *session;
1120 session = iscsi_alloc_session(shost, transport, dd_size);
1124 if (iscsi_add_session(session, target_id)) {
1125 iscsi_free_session(session);
1130 EXPORT_SYMBOL_GPL(iscsi_create_session);
1132 static void iscsi_conn_release(struct device *dev)
1134 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
1135 struct device *parent = conn->dev.parent;
1137 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
1142 static int iscsi_is_conn_dev(const struct device *dev)
1144 return dev->release == iscsi_conn_release;
1147 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
1149 if (!iscsi_is_conn_dev(dev))
1151 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
1154 void iscsi_remove_session(struct iscsi_cls_session *session)
1156 struct Scsi_Host *shost = iscsi_session_to_shost(session);
1157 unsigned long flags;
1160 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
1162 spin_lock_irqsave(&sesslock, flags);
1163 list_del(&session->sess_list);
1164 spin_unlock_irqrestore(&sesslock, flags);
1166 /* make sure there are no blocks/unblocks queued */
1167 flush_workqueue(iscsi_eh_timer_workq);
1168 /* make sure the timedout callout is not running */
1169 if (!cancel_delayed_work(&session->recovery_work))
1170 flush_workqueue(iscsi_eh_timer_workq);
1172 * If we are blocked let commands flow again. The lld or iscsi
1173 * layer should set up the queuecommand to fail commands.
1174 * We assume that LLD will not be calling block/unblock while
1175 * removing the session.
1177 spin_lock_irqsave(&session->lock, flags);
1178 session->state = ISCSI_SESSION_FREE;
1179 spin_unlock_irqrestore(&session->lock, flags);
1181 scsi_target_unblock(&session->dev);
1182 /* flush running scans then delete devices */
1183 scsi_flush_work(shost);
1184 __iscsi_unbind_session(&session->unbind_work);
1186 /* hw iscsi may not have removed all connections from session */
1187 err = device_for_each_child(&session->dev, NULL,
1188 iscsi_iter_destroy_conn_fn);
1190 iscsi_cls_session_printk(KERN_ERR, session,
1191 "Could not delete all connections "
1192 "for session. Error %d.\n", err);
1194 transport_unregister_device(&session->dev);
1196 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
1197 device_del(&session->dev);
1199 EXPORT_SYMBOL_GPL(iscsi_remove_session);
1201 void iscsi_free_session(struct iscsi_cls_session *session)
1203 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
1204 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
1205 put_device(&session->dev);
1207 EXPORT_SYMBOL_GPL(iscsi_free_session);
1210 * iscsi_destroy_session - destroy iscsi session
1211 * @session: iscsi_session
1213 * Can be called by a LLD or iscsi_transport. There must not be
1214 * any running connections.
1216 int iscsi_destroy_session(struct iscsi_cls_session *session)
1218 iscsi_remove_session(session);
1219 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
1220 iscsi_free_session(session);
1223 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
1226 * iscsi_create_conn - create iscsi class connection
1227 * @session: iscsi cls session
1228 * @dd_size: private driver data size
1229 * @cid: connection id
1231 * This can be called from a LLD or iscsi_transport. The connection
1232 * is child of the session so cid must be unique for all connections
1235 * Since we do not support MCS, cid will normally be zero. In some cases
1236 * for software iscsi we could be trying to preallocate a connection struct
1237 * in which case there could be two connection structs and cid would be
1240 struct iscsi_cls_conn *
1241 iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
1243 struct iscsi_transport *transport = session->transport;
1244 struct iscsi_cls_conn *conn;
1245 unsigned long flags;
1248 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
1252 conn->dd_data = &conn[1];
1254 mutex_init(&conn->ep_mutex);
1255 INIT_LIST_HEAD(&conn->conn_list);
1256 conn->transport = transport;
1259 /* this is released in the dev's release function */
1260 if (!get_device(&session->dev))
1263 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
1264 conn->dev.parent = &session->dev;
1265 conn->dev.release = iscsi_conn_release;
1266 err = device_register(&conn->dev);
1268 iscsi_cls_session_printk(KERN_ERR, session, "could not "
1269 "register connection's dev\n");
1270 goto release_parent_ref;
1272 transport_register_device(&conn->dev);
1274 spin_lock_irqsave(&connlock, flags);
1275 list_add(&conn->conn_list, &connlist);
1276 spin_unlock_irqrestore(&connlock, flags);
1278 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
1282 put_device(&session->dev);
1288 EXPORT_SYMBOL_GPL(iscsi_create_conn);
1291 * iscsi_destroy_conn - destroy iscsi class connection
1292 * @conn: iscsi cls session
1294 * This can be called from a LLD or iscsi_transport.
1296 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1298 unsigned long flags;
1300 spin_lock_irqsave(&connlock, flags);
1301 list_del(&conn->conn_list);
1302 spin_unlock_irqrestore(&connlock, flags);
1304 transport_unregister_device(&conn->dev);
1305 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
1306 device_unregister(&conn->dev);
1309 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1312 * iscsi interface functions
1314 static struct iscsi_internal *
1315 iscsi_if_transport_lookup(struct iscsi_transport *tt)
1317 struct iscsi_internal *priv;
1318 unsigned long flags;
1320 spin_lock_irqsave(&iscsi_transport_lock, flags);
1321 list_for_each_entry(priv, &iscsi_transports, list) {
1322 if (tt == priv->iscsi_transport) {
1323 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1327 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1332 iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
1334 return nlmsg_multicast(nls, skb, 0, group, gfp);
1337 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
1338 char *data, uint32_t data_size)
1340 struct nlmsghdr *nlh;
1341 struct sk_buff *skb;
1342 struct iscsi_uevent *ev;
1344 struct iscsi_internal *priv;
1345 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1348 priv = iscsi_if_transport_lookup(conn->transport);
1352 skb = alloc_skb(len, GFP_ATOMIC);
1354 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
1355 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1356 "control PDU: OOM\n");
1360 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1361 ev = NLMSG_DATA(nlh);
1362 memset(ev, 0, sizeof(*ev));
1363 ev->transport_handle = iscsi_handle(conn->transport);
1364 ev->type = ISCSI_KEVENT_RECV_PDU;
1365 ev->r.recv_req.cid = conn->cid;
1366 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
1367 pdu = (char*)ev + sizeof(*ev);
1368 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1369 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1371 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
1373 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1375 int iscsi_offload_mesg(struct Scsi_Host *shost,
1376 struct iscsi_transport *transport, uint32_t type,
1377 char *data, uint16_t data_size)
1379 struct nlmsghdr *nlh;
1380 struct sk_buff *skb;
1381 struct iscsi_uevent *ev;
1382 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
1384 skb = alloc_skb(len, GFP_ATOMIC);
1386 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
1390 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1391 ev = NLMSG_DATA(nlh);
1392 memset(ev, 0, sizeof(*ev));
1394 ev->transport_handle = iscsi_handle(transport);
1396 case ISCSI_KEVENT_PATH_REQ:
1397 ev->r.req_path.host_no = shost->host_no;
1399 case ISCSI_KEVENT_IF_DOWN:
1400 ev->r.notify_if_down.host_no = shost->host_no;
1404 memcpy((char *)ev + sizeof(*ev), data, data_size);
1406 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
1408 EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1410 void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
1412 struct nlmsghdr *nlh;
1413 struct sk_buff *skb;
1414 struct iscsi_uevent *ev;
1415 struct iscsi_internal *priv;
1416 int len = NLMSG_SPACE(sizeof(*ev));
1418 priv = iscsi_if_transport_lookup(conn->transport);
1422 skb = alloc_skb(len, GFP_ATOMIC);
1424 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1425 "conn error (%d)\n", error);
1429 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1430 ev = NLMSG_DATA(nlh);
1431 ev->transport_handle = iscsi_handle(conn->transport);
1432 ev->type = ISCSI_KEVENT_CONN_ERROR;
1433 ev->r.connerror.error = error;
1434 ev->r.connerror.cid = conn->cid;
1435 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
1437 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
1439 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1442 EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
1444 void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
1445 enum iscsi_conn_state state)
1447 struct nlmsghdr *nlh;
1448 struct sk_buff *skb;
1449 struct iscsi_uevent *ev;
1450 struct iscsi_internal *priv;
1451 int len = NLMSG_SPACE(sizeof(*ev));
1453 priv = iscsi_if_transport_lookup(conn->transport);
1457 skb = alloc_skb(len, GFP_ATOMIC);
1459 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1460 "conn login (%d)\n", state);
1464 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1465 ev = NLMSG_DATA(nlh);
1466 ev->transport_handle = iscsi_handle(conn->transport);
1467 ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
1468 ev->r.conn_login.state = state;
1469 ev->r.conn_login.cid = conn->cid;
1470 ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
1471 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
1473 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
1476 EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
1479 iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1480 void *payload, int size)
1482 struct sk_buff *skb;
1483 struct nlmsghdr *nlh;
1484 int len = NLMSG_SPACE(size);
1485 int flags = multi ? NLM_F_MULTI : 0;
1486 int t = done ? NLMSG_DONE : type;
1488 skb = alloc_skb(len, GFP_ATOMIC);
1490 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1494 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
1495 nlh->nlmsg_flags = flags;
1496 memcpy(NLMSG_DATA(nlh), payload, size);
1497 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
1501 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
1503 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1504 struct iscsi_stats *stats;
1505 struct sk_buff *skbstat;
1506 struct iscsi_cls_conn *conn;
1507 struct nlmsghdr *nlhstat;
1508 struct iscsi_uevent *evstat;
1509 struct iscsi_internal *priv;
1510 int len = NLMSG_SPACE(sizeof(*ev) +
1511 sizeof(struct iscsi_stats) +
1512 sizeof(struct iscsi_stats_custom) *
1513 ISCSI_STATS_CUSTOM_MAX);
1516 priv = iscsi_if_transport_lookup(transport);
1520 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
1527 skbstat = alloc_skb(len, GFP_ATOMIC);
1529 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1530 "deliver stats: OOM\n");
1534 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
1535 (len - sizeof(*nlhstat)), 0);
1536 evstat = NLMSG_DATA(nlhstat);
1537 memset(evstat, 0, sizeof(*evstat));
1538 evstat->transport_handle = iscsi_handle(conn->transport);
1539 evstat->type = nlh->nlmsg_type;
1540 evstat->u.get_stats.cid =
1541 ev->u.get_stats.cid;
1542 evstat->u.get_stats.sid =
1543 ev->u.get_stats.sid;
1544 stats = (struct iscsi_stats *)
1545 ((char*)evstat + sizeof(*evstat));
1546 memset(stats, 0, sizeof(*stats));
1548 transport->get_stats(conn, stats);
1549 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1550 sizeof(struct iscsi_stats) +
1551 sizeof(struct iscsi_stats_custom) *
1552 stats->custom_length);
1553 actual_size -= sizeof(*nlhstat);
1554 actual_size = NLMSG_LENGTH(actual_size);
1555 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
1556 nlhstat->nlmsg_len = actual_size;
1558 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1560 } while (err < 0 && err != -ECONNREFUSED);
1566 * iscsi_session_event - send session destr. completion event
1567 * @session: iscsi class session
1568 * @event: type of event
1570 int iscsi_session_event(struct iscsi_cls_session *session,
1571 enum iscsi_uevent_e event)
1573 struct iscsi_internal *priv;
1574 struct Scsi_Host *shost;
1575 struct iscsi_uevent *ev;
1576 struct sk_buff *skb;
1577 struct nlmsghdr *nlh;
1578 int rc, len = NLMSG_SPACE(sizeof(*ev));
1580 priv = iscsi_if_transport_lookup(session->transport);
1583 shost = iscsi_session_to_shost(session);
1585 skb = alloc_skb(len, GFP_KERNEL);
1587 iscsi_cls_session_printk(KERN_ERR, session,
1588 "Cannot notify userspace of session "
1589 "event %u\n", event);
1593 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1594 ev = NLMSG_DATA(nlh);
1595 ev->transport_handle = iscsi_handle(session->transport);
1599 case ISCSI_KEVENT_DESTROY_SESSION:
1600 ev->r.d_session.host_no = shost->host_no;
1601 ev->r.d_session.sid = session->sid;
1603 case ISCSI_KEVENT_CREATE_SESSION:
1604 ev->r.c_session_ret.host_no = shost->host_no;
1605 ev->r.c_session_ret.sid = session->sid;
1607 case ISCSI_KEVENT_UNBIND_SESSION:
1608 ev->r.unbind_session.host_no = shost->host_no;
1609 ev->r.unbind_session.sid = session->sid;
1612 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1619 * this will occur if the daemon is not up, so we just warn
1620 * the user and when the daemon is restarted it will handle it
1622 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
1624 iscsi_cls_session_printk(KERN_ERR, session,
1625 "Cannot notify userspace of session "
1626 "event %u. Check iscsi daemon\n",
1629 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
1633 EXPORT_SYMBOL_GPL(iscsi_session_event);
1636 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1637 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
1638 uint16_t cmds_max, uint16_t queue_depth)
1640 struct iscsi_transport *transport = priv->iscsi_transport;
1641 struct iscsi_cls_session *session;
1642 struct Scsi_Host *shost;
1644 session = transport->create_session(ep, cmds_max, queue_depth,
1649 shost = iscsi_session_to_shost(session);
1650 ev->r.c_session_ret.host_no = shost->host_no;
1651 ev->r.c_session_ret.sid = session->sid;
1652 ISCSI_DBG_TRANS_SESSION(session,
1653 "Completed creating transport session\n");
1658 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1660 struct iscsi_cls_conn *conn;
1661 struct iscsi_cls_session *session;
1663 session = iscsi_session_lookup(ev->u.c_conn.sid);
1665 printk(KERN_ERR "iscsi: invalid session %d.\n",
1670 conn = transport->create_conn(session, ev->u.c_conn.cid);
1672 iscsi_cls_session_printk(KERN_ERR, session,
1673 "couldn't create a new connection.");
1677 ev->r.c_conn_ret.sid = session->sid;
1678 ev->r.c_conn_ret.cid = conn->cid;
1680 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
1685 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1687 struct iscsi_cls_conn *conn;
1689 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
1693 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
1694 if (transport->destroy_conn)
1695 transport->destroy_conn(conn);
1701 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1703 char *data = (char*)ev + sizeof(*ev);
1704 struct iscsi_cls_conn *conn;
1705 struct iscsi_cls_session *session;
1706 int err = 0, value = 0;
1708 session = iscsi_session_lookup(ev->u.set_param.sid);
1709 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1710 if (!conn || !session)
1713 switch (ev->u.set_param.param) {
1714 case ISCSI_PARAM_SESS_RECOVERY_TMO:
1715 sscanf(data, "%d", &value);
1716 session->recovery_tmo = value;
1719 err = transport->set_param(conn, ev->u.set_param.param,
1720 data, ev->u.set_param.len);
1726 static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1727 struct iscsi_uevent *ev, int msg_type)
1729 struct iscsi_endpoint *ep;
1730 struct sockaddr *dst_addr;
1731 struct Scsi_Host *shost = NULL;
1732 int non_blocking, err = 0;
1734 if (!transport->ep_connect)
1737 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1738 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1740 printk(KERN_ERR "ep connect failed. Could not find "
1742 ev->u.ep_connect_through_host.host_no);
1745 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1747 non_blocking = ev->u.ep_connect.non_blocking;
1749 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1750 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1756 ev->r.ep_connect_ret.handle = ep->id;
1759 scsi_host_put(shost);
1763 static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
1766 struct iscsi_cls_conn *conn;
1767 struct iscsi_endpoint *ep;
1769 if (!transport->ep_disconnect)
1772 ep = iscsi_lookup_endpoint(ep_handle);
1777 mutex_lock(&conn->ep_mutex);
1779 mutex_unlock(&conn->ep_mutex);
1782 transport->ep_disconnect(ep);
1787 iscsi_if_transport_ep(struct iscsi_transport *transport,
1788 struct iscsi_uevent *ev, int msg_type)
1790 struct iscsi_endpoint *ep;
1794 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
1795 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1796 rc = iscsi_if_ep_connect(transport, ev, msg_type);
1798 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1799 if (!transport->ep_poll)
1802 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1806 ev->r.retcode = transport->ep_poll(ep,
1807 ev->u.ep_poll.timeout_ms);
1809 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1810 rc = iscsi_if_ep_disconnect(transport,
1811 ev->u.ep_disconnect.ep_handle);
1818 iscsi_tgt_dscvr(struct iscsi_transport *transport,
1819 struct iscsi_uevent *ev)
1821 struct Scsi_Host *shost;
1822 struct sockaddr *dst_addr;
1825 if (!transport->tgt_dscvr)
1828 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1830 printk(KERN_ERR "target discovery could not find host no %u\n",
1831 ev->u.tgt_dscvr.host_no);
1836 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1837 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1838 ev->u.tgt_dscvr.enable, dst_addr);
1839 scsi_host_put(shost);
1844 iscsi_set_host_param(struct iscsi_transport *transport,
1845 struct iscsi_uevent *ev)
1847 char *data = (char*)ev + sizeof(*ev);
1848 struct Scsi_Host *shost;
1851 if (!transport->set_host_param)
1854 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1856 printk(KERN_ERR "set_host_param could not find host no %u\n",
1857 ev->u.set_host_param.host_no);
1861 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1862 data, ev->u.set_host_param.len);
1863 scsi_host_put(shost);
1868 iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1870 struct Scsi_Host *shost;
1871 struct iscsi_path *params;
1874 if (!transport->set_path)
1877 shost = scsi_host_lookup(ev->u.set_path.host_no);
1879 printk(KERN_ERR "set path could not find host no %u\n",
1880 ev->u.set_path.host_no);
1884 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1885 err = transport->set_path(shost, params);
1887 scsi_host_put(shost);
1892 iscsi_set_iface_params(struct iscsi_transport *transport,
1893 struct iscsi_uevent *ev, uint32_t len)
1895 char *data = (char *)ev + sizeof(*ev);
1896 struct Scsi_Host *shost;
1899 if (!transport->set_iface_param)
1902 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
1904 printk(KERN_ERR "set_iface_params could not find host no %u\n",
1905 ev->u.set_iface_params.host_no);
1909 err = transport->set_iface_param(shost, data, len);
1910 scsi_host_put(shost);
1915 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
1918 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1919 struct iscsi_transport *transport = NULL;
1920 struct iscsi_internal *priv;
1921 struct iscsi_cls_session *session;
1922 struct iscsi_cls_conn *conn;
1923 struct iscsi_endpoint *ep = NULL;
1925 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1926 *group = ISCSI_NL_GRP_UIP;
1928 *group = ISCSI_NL_GRP_ISCSID;
1930 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1933 transport = priv->iscsi_transport;
1935 if (!try_module_get(transport->owner))
1938 switch (nlh->nlmsg_type) {
1939 case ISCSI_UEVENT_CREATE_SESSION:
1940 err = iscsi_if_create_session(priv, ep, ev,
1941 ev->u.c_session.initial_cmdsn,
1942 ev->u.c_session.cmds_max,
1943 ev->u.c_session.queue_depth);
1945 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
1946 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
1952 err = iscsi_if_create_session(priv, ep, ev,
1953 ev->u.c_bound_session.initial_cmdsn,
1954 ev->u.c_bound_session.cmds_max,
1955 ev->u.c_bound_session.queue_depth);
1957 case ISCSI_UEVENT_DESTROY_SESSION:
1958 session = iscsi_session_lookup(ev->u.d_session.sid);
1960 transport->destroy_session(session);
1964 case ISCSI_UEVENT_UNBIND_SESSION:
1965 session = iscsi_session_lookup(ev->u.d_session.sid);
1967 scsi_queue_work(iscsi_session_to_shost(session),
1968 &session->unbind_work);
1972 case ISCSI_UEVENT_CREATE_CONN:
1973 err = iscsi_if_create_conn(transport, ev);
1975 case ISCSI_UEVENT_DESTROY_CONN:
1976 err = iscsi_if_destroy_conn(transport, ev);
1978 case ISCSI_UEVENT_BIND_CONN:
1979 session = iscsi_session_lookup(ev->u.b_conn.sid);
1980 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1982 if (conn && conn->ep)
1983 iscsi_if_ep_disconnect(transport, conn->ep->id);
1985 if (!session || !conn) {
1990 ev->r.retcode = transport->bind_conn(session, conn,
1991 ev->u.b_conn.transport_eph,
1992 ev->u.b_conn.is_leading);
1993 if (ev->r.retcode || !transport->ep_connect)
1996 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
2000 mutex_lock(&conn->ep_mutex);
2002 mutex_unlock(&conn->ep_mutex);
2004 iscsi_cls_conn_printk(KERN_ERR, conn,
2005 "Could not set ep conn "
2008 case ISCSI_UEVENT_SET_PARAM:
2009 err = iscsi_set_param(transport, ev);
2011 case ISCSI_UEVENT_START_CONN:
2012 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
2014 ev->r.retcode = transport->start_conn(conn);
2018 case ISCSI_UEVENT_STOP_CONN:
2019 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
2021 transport->stop_conn(conn, ev->u.stop_conn.flag);
2025 case ISCSI_UEVENT_SEND_PDU:
2026 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
2028 ev->r.retcode = transport->send_pdu(conn,
2029 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
2030 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
2031 ev->u.send_pdu.data_size);
2035 case ISCSI_UEVENT_GET_STATS:
2036 err = iscsi_if_get_stats(transport, nlh);
2038 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
2039 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
2040 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
2041 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
2042 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
2044 case ISCSI_UEVENT_TGT_DSCVR:
2045 err = iscsi_tgt_dscvr(transport, ev);
2047 case ISCSI_UEVENT_SET_HOST_PARAM:
2048 err = iscsi_set_host_param(transport, ev);
2050 case ISCSI_UEVENT_PATH_UPDATE:
2051 err = iscsi_set_path(transport, ev);
2053 case ISCSI_UEVENT_SET_IFACE_PARAMS:
2054 err = iscsi_set_iface_params(transport, ev,
2055 nlmsg_attrlen(nlh, sizeof(*ev)));
2062 module_put(transport->owner);
2067 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
2068 * Malformed skbs with wrong lengths or invalid creds are not processed.
2071 iscsi_if_rx(struct sk_buff *skb)
2073 mutex_lock(&rx_queue_mutex);
2074 while (skb->len >= NLMSG_SPACE(0)) {
2077 struct nlmsghdr *nlh;
2078 struct iscsi_uevent *ev;
2081 nlh = nlmsg_hdr(skb);
2082 if (nlh->nlmsg_len < sizeof(*nlh) ||
2083 skb->len < nlh->nlmsg_len) {
2087 ev = NLMSG_DATA(nlh);
2088 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
2089 if (rlen > skb->len)
2092 err = iscsi_if_recv_msg(skb, nlh, &group);
2094 ev->type = ISCSI_KEVENT_IF_ERROR;
2099 * special case for GET_STATS:
2100 * on success - sending reply and stats from
2101 * inside of if_recv_msg(),
2102 * on error - fall through.
2104 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
2106 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
2107 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
2108 } while (err < 0 && err != -ECONNREFUSED);
2109 skb_pull(skb, rlen);
2111 mutex_unlock(&rx_queue_mutex);
2114 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
2115 struct device_attribute dev_attr_##_prefix##_##_name = \
2116 __ATTR(_name,_mode,_show,_store)
2119 * iSCSI connection attrs
2121 #define iscsi_conn_attr_show(param) \
2123 show_conn_param_##param(struct device *dev, \
2124 struct device_attribute *attr, char *buf) \
2126 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
2127 struct iscsi_transport *t = conn->transport; \
2128 return t->get_conn_param(conn, param, buf); \
2131 #define iscsi_conn_attr(field, param) \
2132 iscsi_conn_attr_show(param) \
2133 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
2136 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
2137 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
2138 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
2139 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
2140 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
2141 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
2142 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
2143 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
2144 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
2145 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
2146 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
2148 #define iscsi_conn_ep_attr_show(param) \
2149 static ssize_t show_conn_ep_param_##param(struct device *dev, \
2150 struct device_attribute *attr,\
2153 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
2154 struct iscsi_transport *t = conn->transport; \
2155 struct iscsi_endpoint *ep; \
2159 * Need to make sure ep_disconnect does not free the LLD's \
2160 * interconnect resources while we are trying to read them. \
2162 mutex_lock(&conn->ep_mutex); \
2164 if (!ep && t->ep_connect) { \
2165 mutex_unlock(&conn->ep_mutex); \
2170 rc = t->get_ep_param(ep, param, buf); \
2172 rc = t->get_conn_param(conn, param, buf); \
2173 mutex_unlock(&conn->ep_mutex); \
2177 #define iscsi_conn_ep_attr(field, param) \
2178 iscsi_conn_ep_attr_show(param) \
2179 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
2180 show_conn_ep_param_##param, NULL);
2182 iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
2183 iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
2185 static struct attribute *iscsi_conn_attrs[] = {
2186 &dev_attr_conn_max_recv_dlength.attr,
2187 &dev_attr_conn_max_xmit_dlength.attr,
2188 &dev_attr_conn_header_digest.attr,
2189 &dev_attr_conn_data_digest.attr,
2190 &dev_attr_conn_ifmarker.attr,
2191 &dev_attr_conn_ofmarker.attr,
2192 &dev_attr_conn_address.attr,
2193 &dev_attr_conn_port.attr,
2194 &dev_attr_conn_exp_statsn.attr,
2195 &dev_attr_conn_persistent_address.attr,
2196 &dev_attr_conn_persistent_port.attr,
2197 &dev_attr_conn_ping_tmo.attr,
2198 &dev_attr_conn_recv_tmo.attr,
2202 static mode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
2203 struct attribute *attr, int i)
2205 struct device *cdev = container_of(kobj, struct device, kobj);
2206 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
2207 struct iscsi_transport *t = conn->transport;
2210 if (attr == &dev_attr_conn_max_recv_dlength.attr)
2211 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
2212 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
2213 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
2214 else if (attr == &dev_attr_conn_header_digest.attr)
2215 param = ISCSI_PARAM_HDRDGST_EN;
2216 else if (attr == &dev_attr_conn_data_digest.attr)
2217 param = ISCSI_PARAM_DATADGST_EN;
2218 else if (attr == &dev_attr_conn_ifmarker.attr)
2219 param = ISCSI_PARAM_IFMARKER_EN;
2220 else if (attr == &dev_attr_conn_ofmarker.attr)
2221 param = ISCSI_PARAM_OFMARKER_EN;
2222 else if (attr == &dev_attr_conn_address.attr)
2223 param = ISCSI_PARAM_CONN_ADDRESS;
2224 else if (attr == &dev_attr_conn_port.attr)
2225 param = ISCSI_PARAM_CONN_PORT;
2226 else if (attr == &dev_attr_conn_exp_statsn.attr)
2227 param = ISCSI_PARAM_EXP_STATSN;
2228 else if (attr == &dev_attr_conn_persistent_address.attr)
2229 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
2230 else if (attr == &dev_attr_conn_persistent_port.attr)
2231 param = ISCSI_PARAM_PERSISTENT_PORT;
2232 else if (attr == &dev_attr_conn_ping_tmo.attr)
2233 param = ISCSI_PARAM_PING_TMO;
2234 else if (attr == &dev_attr_conn_recv_tmo.attr)
2235 param = ISCSI_PARAM_RECV_TMO;
2237 WARN_ONCE(1, "Invalid conn attr");
2241 return t->attr_is_visible(ISCSI_PARAM, param);
2244 static struct attribute_group iscsi_conn_group = {
2245 .attrs = iscsi_conn_attrs,
2246 .is_visible = iscsi_conn_attr_is_visible,
2250 * iSCSI session attrs
2252 #define iscsi_session_attr_show(param, perm) \
2254 show_session_param_##param(struct device *dev, \
2255 struct device_attribute *attr, char *buf) \
2257 struct iscsi_cls_session *session = \
2258 iscsi_dev_to_session(dev->parent); \
2259 struct iscsi_transport *t = session->transport; \
2261 if (perm && !capable(CAP_SYS_ADMIN)) \
2263 return t->get_session_param(session, param, buf); \
2266 #define iscsi_session_attr(field, param, perm) \
2267 iscsi_session_attr_show(param, perm) \
2268 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
2270 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
2271 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
2272 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
2273 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
2274 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
2275 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
2276 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
2277 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
2278 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
2279 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
2280 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
2281 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
2282 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
2283 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
2284 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
2285 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
2286 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
2287 iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
2288 iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
2289 iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
2290 iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
2293 show_priv_session_state(struct device *dev, struct device_attribute *attr,
2296 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
2297 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
2299 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
2302 #define iscsi_priv_session_attr_show(field, format) \
2304 show_priv_session_##field(struct device *dev, \
2305 struct device_attribute *attr, char *buf) \
2307 struct iscsi_cls_session *session = \
2308 iscsi_dev_to_session(dev->parent); \
2309 if (session->field == -1) \
2310 return sprintf(buf, "off\n"); \
2311 return sprintf(buf, format"\n", session->field); \
2314 #define iscsi_priv_session_attr_store(field) \
2316 store_priv_session_##field(struct device *dev, \
2317 struct device_attribute *attr, \
2318 const char *buf, size_t count) \
2322 struct iscsi_cls_session *session = \
2323 iscsi_dev_to_session(dev->parent); \
2324 if ((session->state == ISCSI_SESSION_FREE) || \
2325 (session->state == ISCSI_SESSION_FAILED)) \
2327 if (strncmp(buf, "off", 3) == 0) \
2328 session->field = -1; \
2330 val = simple_strtoul(buf, &cp, 0); \
2331 if (*cp != '\0' && *cp != '\n') \
2333 session->field = val; \
2338 #define iscsi_priv_session_rw_attr(field, format) \
2339 iscsi_priv_session_attr_show(field, format) \
2340 iscsi_priv_session_attr_store(field) \
2341 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
2342 show_priv_session_##field, \
2343 store_priv_session_##field)
2344 iscsi_priv_session_rw_attr(recovery_tmo, "%d");
2346 static struct attribute *iscsi_session_attrs[] = {
2347 &dev_attr_sess_initial_r2t.attr,
2348 &dev_attr_sess_max_outstanding_r2t.attr,
2349 &dev_attr_sess_immediate_data.attr,
2350 &dev_attr_sess_first_burst_len.attr,
2351 &dev_attr_sess_max_burst_len.attr,
2352 &dev_attr_sess_data_pdu_in_order.attr,
2353 &dev_attr_sess_data_seq_in_order.attr,
2354 &dev_attr_sess_erl.attr,
2355 &dev_attr_sess_targetname.attr,
2356 &dev_attr_sess_tpgt.attr,
2357 &dev_attr_sess_password.attr,
2358 &dev_attr_sess_password_in.attr,
2359 &dev_attr_sess_username.attr,
2360 &dev_attr_sess_username_in.attr,
2361 &dev_attr_sess_fast_abort.attr,
2362 &dev_attr_sess_abort_tmo.attr,
2363 &dev_attr_sess_lu_reset_tmo.attr,
2364 &dev_attr_sess_tgt_reset_tmo.attr,
2365 &dev_attr_sess_ifacename.attr,
2366 &dev_attr_sess_initiatorname.attr,
2367 &dev_attr_sess_targetalias.attr,
2368 &dev_attr_priv_sess_recovery_tmo.attr,
2369 &dev_attr_priv_sess_state.attr,
2373 static mode_t iscsi_session_attr_is_visible(struct kobject *kobj,
2374 struct attribute *attr, int i)
2376 struct device *cdev = container_of(kobj, struct device, kobj);
2377 struct iscsi_cls_session *session = transport_class_to_session(cdev);
2378 struct iscsi_transport *t = session->transport;
2381 if (attr == &dev_attr_sess_initial_r2t.attr)
2382 param = ISCSI_PARAM_INITIAL_R2T_EN;
2383 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
2384 param = ISCSI_PARAM_MAX_R2T;
2385 else if (attr == &dev_attr_sess_immediate_data.attr)
2386 param = ISCSI_PARAM_IMM_DATA_EN;
2387 else if (attr == &dev_attr_sess_first_burst_len.attr)
2388 param = ISCSI_PARAM_FIRST_BURST;
2389 else if (attr == &dev_attr_sess_max_burst_len.attr)
2390 param = ISCSI_PARAM_MAX_BURST;
2391 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
2392 param = ISCSI_PARAM_PDU_INORDER_EN;
2393 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
2394 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
2395 else if (attr == &dev_attr_sess_erl.attr)
2396 param = ISCSI_PARAM_ERL;
2397 else if (attr == &dev_attr_sess_targetname.attr)
2398 param = ISCSI_PARAM_TARGET_NAME;
2399 else if (attr == &dev_attr_sess_tpgt.attr)
2400 param = ISCSI_PARAM_TPGT;
2401 else if (attr == &dev_attr_sess_password.attr)
2402 param = ISCSI_PARAM_USERNAME;
2403 else if (attr == &dev_attr_sess_password_in.attr)
2404 param = ISCSI_PARAM_USERNAME_IN;
2405 else if (attr == &dev_attr_sess_username.attr)
2406 param = ISCSI_PARAM_PASSWORD;
2407 else if (attr == &dev_attr_sess_username_in.attr)
2408 param = ISCSI_PARAM_PASSWORD_IN;
2409 else if (attr == &dev_attr_sess_fast_abort.attr)
2410 param = ISCSI_PARAM_FAST_ABORT;
2411 else if (attr == &dev_attr_sess_abort_tmo.attr)
2412 param = ISCSI_PARAM_ABORT_TMO;
2413 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
2414 param = ISCSI_PARAM_LU_RESET_TMO;
2415 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
2416 param = ISCSI_PARAM_TGT_RESET_TMO;
2417 else if (attr == &dev_attr_sess_ifacename.attr)
2418 param = ISCSI_PARAM_IFACE_NAME;
2419 else if (attr == &dev_attr_sess_initiatorname.attr)
2420 param = ISCSI_PARAM_INITIATOR_NAME;
2421 else if (attr == &dev_attr_sess_targetalias.attr)
2422 param = ISCSI_PARAM_TARGET_ALIAS;
2423 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
2424 return S_IRUGO | S_IWUSR;
2425 else if (attr == &dev_attr_priv_sess_state.attr)
2428 WARN_ONCE(1, "Invalid session attr");
2432 return t->attr_is_visible(ISCSI_PARAM, param);
2435 static struct attribute_group iscsi_session_group = {
2436 .attrs = iscsi_session_attrs,
2437 .is_visible = iscsi_session_attr_is_visible,
2443 #define iscsi_host_attr_show(param) \
2445 show_host_param_##param(struct device *dev, \
2446 struct device_attribute *attr, char *buf) \
2448 struct Scsi_Host *shost = transport_class_to_shost(dev); \
2449 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
2450 return priv->iscsi_transport->get_host_param(shost, param, buf); \
2453 #define iscsi_host_attr(field, param) \
2454 iscsi_host_attr_show(param) \
2455 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
2458 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
2459 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
2460 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
2461 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
2463 static struct attribute *iscsi_host_attrs[] = {
2464 &dev_attr_host_netdev.attr,
2465 &dev_attr_host_hwaddress.attr,
2466 &dev_attr_host_ipaddress.attr,
2467 &dev_attr_host_initiatorname.attr,
2471 static mode_t iscsi_host_attr_is_visible(struct kobject *kobj,
2472 struct attribute *attr, int i)
2474 struct device *cdev = container_of(kobj, struct device, kobj);
2475 struct Scsi_Host *shost = transport_class_to_shost(cdev);
2476 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
2479 if (attr == &dev_attr_host_netdev.attr)
2480 param = ISCSI_HOST_PARAM_NETDEV_NAME;
2481 else if (attr == &dev_attr_host_hwaddress.attr)
2482 param = ISCSI_HOST_PARAM_HWADDRESS;
2483 else if (attr == &dev_attr_host_ipaddress.attr)
2484 param = ISCSI_HOST_PARAM_IPADDRESS;
2485 else if (attr == &dev_attr_host_initiatorname.attr)
2486 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
2488 WARN_ONCE(1, "Invalid host attr");
2492 return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
2495 static struct attribute_group iscsi_host_group = {
2496 .attrs = iscsi_host_attrs,
2497 .is_visible = iscsi_host_attr_is_visible,
2500 static int iscsi_session_match(struct attribute_container *cont,
2503 struct iscsi_cls_session *session;
2504 struct Scsi_Host *shost;
2505 struct iscsi_internal *priv;
2507 if (!iscsi_is_session_dev(dev))
2510 session = iscsi_dev_to_session(dev);
2511 shost = iscsi_session_to_shost(session);
2512 if (!shost->transportt)
2515 priv = to_iscsi_internal(shost->transportt);
2516 if (priv->session_cont.ac.class != &iscsi_session_class.class)
2519 return &priv->session_cont.ac == cont;
2522 static int iscsi_conn_match(struct attribute_container *cont,
2525 struct iscsi_cls_session *session;
2526 struct iscsi_cls_conn *conn;
2527 struct Scsi_Host *shost;
2528 struct iscsi_internal *priv;
2530 if (!iscsi_is_conn_dev(dev))
2533 conn = iscsi_dev_to_conn(dev);
2534 session = iscsi_dev_to_session(conn->dev.parent);
2535 shost = iscsi_session_to_shost(session);
2537 if (!shost->transportt)
2540 priv = to_iscsi_internal(shost->transportt);
2541 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
2544 return &priv->conn_cont.ac == cont;
2547 static int iscsi_host_match(struct attribute_container *cont,
2550 struct Scsi_Host *shost;
2551 struct iscsi_internal *priv;
2553 if (!scsi_is_host_device(dev))
2556 shost = dev_to_shost(dev);
2557 if (!shost->transportt ||
2558 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
2561 priv = to_iscsi_internal(shost->transportt);
2562 return &priv->t.host_attrs.ac == cont;
2565 struct scsi_transport_template *
2566 iscsi_register_transport(struct iscsi_transport *tt)
2568 struct iscsi_internal *priv;
2569 unsigned long flags;
2574 priv = iscsi_if_transport_lookup(tt);
2578 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2581 INIT_LIST_HEAD(&priv->list);
2582 priv->iscsi_transport = tt;
2583 priv->t.user_scan = iscsi_user_scan;
2584 priv->t.create_work_queue = 1;
2586 priv->dev.class = &iscsi_transport_class;
2587 dev_set_name(&priv->dev, "%s", tt->name);
2588 err = device_register(&priv->dev);
2592 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
2594 goto unregister_dev;
2596 /* host parameters */
2597 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
2598 priv->t.host_attrs.ac.match = iscsi_host_match;
2599 priv->t.host_attrs.ac.grp = &iscsi_host_group;
2600 priv->t.host_size = sizeof(struct iscsi_cls_host);
2601 transport_container_register(&priv->t.host_attrs);
2603 /* connection parameters */
2604 priv->conn_cont.ac.class = &iscsi_connection_class.class;
2605 priv->conn_cont.ac.match = iscsi_conn_match;
2606 priv->conn_cont.ac.grp = &iscsi_conn_group;
2607 transport_container_register(&priv->conn_cont);
2609 /* session parameters */
2610 priv->session_cont.ac.class = &iscsi_session_class.class;
2611 priv->session_cont.ac.match = iscsi_session_match;
2612 priv->session_cont.ac.grp = &iscsi_session_group;
2613 transport_container_register(&priv->session_cont);
2615 spin_lock_irqsave(&iscsi_transport_lock, flags);
2616 list_add(&priv->list, &iscsi_transports);
2617 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2619 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
2623 device_unregister(&priv->dev);
2629 EXPORT_SYMBOL_GPL(iscsi_register_transport);
2631 int iscsi_unregister_transport(struct iscsi_transport *tt)
2633 struct iscsi_internal *priv;
2634 unsigned long flags;
2638 mutex_lock(&rx_queue_mutex);
2640 priv = iscsi_if_transport_lookup(tt);
2643 spin_lock_irqsave(&iscsi_transport_lock, flags);
2644 list_del(&priv->list);
2645 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2647 transport_container_unregister(&priv->conn_cont);
2648 transport_container_unregister(&priv->session_cont);
2649 transport_container_unregister(&priv->t.host_attrs);
2651 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
2652 device_unregister(&priv->dev);
2653 mutex_unlock(&rx_queue_mutex);
2657 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
2659 static __init int iscsi_transport_init(void)
2663 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
2664 ISCSI_TRANSPORT_VERSION);
2666 atomic_set(&iscsi_session_nr, 0);
2668 err = class_register(&iscsi_transport_class);
2672 err = class_register(&iscsi_endpoint_class);
2674 goto unregister_transport_class;
2676 err = class_register(&iscsi_iface_class);
2678 goto unregister_endpoint_class;
2680 err = transport_class_register(&iscsi_host_class);
2682 goto unregister_iface_class;
2684 err = transport_class_register(&iscsi_connection_class);
2686 goto unregister_host_class;
2688 err = transport_class_register(&iscsi_session_class);
2690 goto unregister_conn_class;
2692 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2696 goto unregister_session_class;
2699 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2700 if (!iscsi_eh_timer_workq)
2706 netlink_kernel_release(nls);
2707 unregister_session_class:
2708 transport_class_unregister(&iscsi_session_class);
2709 unregister_conn_class:
2710 transport_class_unregister(&iscsi_connection_class);
2711 unregister_host_class:
2712 transport_class_unregister(&iscsi_host_class);
2713 unregister_iface_class:
2714 class_unregister(&iscsi_iface_class);
2715 unregister_endpoint_class:
2716 class_unregister(&iscsi_endpoint_class);
2717 unregister_transport_class:
2718 class_unregister(&iscsi_transport_class);
2722 static void __exit iscsi_transport_exit(void)
2724 destroy_workqueue(iscsi_eh_timer_workq);
2725 netlink_kernel_release(nls);
2726 transport_class_unregister(&iscsi_connection_class);
2727 transport_class_unregister(&iscsi_session_class);
2728 transport_class_unregister(&iscsi_host_class);
2729 class_unregister(&iscsi_endpoint_class);
2730 class_unregister(&iscsi_iface_class);
2731 class_unregister(&iscsi_transport_class);
2734 module_init(iscsi_transport_init);
2735 module_exit(iscsi_transport_exit);
2737 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2738 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2739 "Alex Aizman <itn780@yahoo.com>");
2740 MODULE_DESCRIPTION("iSCSI Transport Interface");
2741 MODULE_LICENSE("GPL");
2742 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
2743 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);