[SCSI] fcoe: use kthread_create_on_node
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / scsi_transport_iscsi.c
blobc58f6766f4c78a633a388a54b4c83c366cc3eab0
1 /*
2 * iSCSI transport class definitions
4 * Copyright (C) IBM Corporation, 2004
5 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <linux/slab.h>
26 #include <linux/bsg-lib.h>
27 #include <net/tcp.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_transport.h>
32 #include <scsi/scsi_transport_iscsi.h>
33 #include <scsi/iscsi_if.h>
34 #include <scsi/scsi_cmnd.h>
35 #include <scsi/scsi_bsg_iscsi.h>
37 #define ISCSI_TRANSPORT_VERSION "2.0-870"
39 static int dbg_session;
40 module_param_named(debug_session, dbg_session, int,
41 S_IRUGO | S_IWUSR);
42 MODULE_PARM_DESC(debug_session,
43 "Turn on debugging for sessions in scsi_transport_iscsi "
44 "module. Set to 1 to turn on, and zero to turn off. Default "
45 "is off.");
47 static int dbg_conn;
48 module_param_named(debug_conn, dbg_conn, int,
49 S_IRUGO | S_IWUSR);
50 MODULE_PARM_DESC(debug_conn,
51 "Turn on debugging for connections in scsi_transport_iscsi "
52 "module. Set to 1 to turn on, and zero to turn off. Default "
53 "is off.");
55 #define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
56 do { \
57 if (dbg_session) \
58 iscsi_cls_session_printk(KERN_INFO, _session, \
59 "%s: " dbg_fmt, \
60 __func__, ##arg); \
61 } while (0);
63 #define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
64 do { \
65 if (dbg_conn) \
66 iscsi_cls_conn_printk(KERN_INFO, _conn, \
67 "%s: " dbg_fmt, \
68 __func__, ##arg); \
69 } while (0);
71 struct iscsi_internal {
72 struct scsi_transport_template t;
73 struct iscsi_transport *iscsi_transport;
74 struct list_head list;
75 struct device dev;
77 struct transport_container conn_cont;
78 struct transport_container session_cont;
81 static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
82 static struct workqueue_struct *iscsi_eh_timer_workq;
85 * list of registered transports and lock that must
86 * be held while accessing list. The iscsi_transport_lock must
87 * be acquired after the rx_queue_mutex.
89 static LIST_HEAD(iscsi_transports);
90 static DEFINE_SPINLOCK(iscsi_transport_lock);
92 #define to_iscsi_internal(tmpl) \
93 container_of(tmpl, struct iscsi_internal, t)
95 #define dev_to_iscsi_internal(_dev) \
96 container_of(_dev, struct iscsi_internal, dev)
98 static void iscsi_transport_release(struct device *dev)
100 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
101 kfree(priv);
105 * iscsi_transport_class represents the iscsi_transports that are
106 * registered.
108 static struct class iscsi_transport_class = {
109 .name = "iscsi_transport",
110 .dev_release = iscsi_transport_release,
113 static ssize_t
114 show_transport_handle(struct device *dev, struct device_attribute *attr,
115 char *buf)
117 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
118 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
120 static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
122 #define show_transport_attr(name, format) \
123 static ssize_t \
124 show_transport_##name(struct device *dev, \
125 struct device_attribute *attr,char *buf) \
127 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
128 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
130 static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
132 show_transport_attr(caps, "0x%x");
134 static struct attribute *iscsi_transport_attrs[] = {
135 &dev_attr_handle.attr,
136 &dev_attr_caps.attr,
137 NULL,
140 static struct attribute_group iscsi_transport_group = {
141 .attrs = iscsi_transport_attrs,
145 * iSCSI endpoint attrs
147 #define iscsi_dev_to_endpoint(_dev) \
148 container_of(_dev, struct iscsi_endpoint, dev)
150 #define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
151 struct device_attribute dev_attr_##_prefix##_##_name = \
152 __ATTR(_name,_mode,_show,_store)
154 static void iscsi_endpoint_release(struct device *dev)
156 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
157 kfree(ep);
160 static struct class iscsi_endpoint_class = {
161 .name = "iscsi_endpoint",
162 .dev_release = iscsi_endpoint_release,
165 static ssize_t
166 show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
168 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
169 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
171 static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
173 static struct attribute *iscsi_endpoint_attrs[] = {
174 &dev_attr_ep_handle.attr,
175 NULL,
178 static struct attribute_group iscsi_endpoint_group = {
179 .attrs = iscsi_endpoint_attrs,
182 #define ISCSI_MAX_EPID -1
184 static int iscsi_match_epid(struct device *dev, void *data)
186 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
187 uint64_t *epid = (uint64_t *) data;
189 return *epid == ep->id;
192 struct iscsi_endpoint *
193 iscsi_create_endpoint(int dd_size)
195 struct device *dev;
196 struct iscsi_endpoint *ep;
197 uint64_t id;
198 int err;
200 for (id = 1; id < ISCSI_MAX_EPID; id++) {
201 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
202 iscsi_match_epid);
203 if (!dev)
204 break;
206 if (id == ISCSI_MAX_EPID) {
207 printk(KERN_ERR "Too many connections. Max supported %u\n",
208 ISCSI_MAX_EPID - 1);
209 return NULL;
212 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
213 if (!ep)
214 return NULL;
216 ep->id = id;
217 ep->dev.class = &iscsi_endpoint_class;
218 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
219 err = device_register(&ep->dev);
220 if (err)
221 goto free_ep;
223 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
224 if (err)
225 goto unregister_dev;
227 if (dd_size)
228 ep->dd_data = &ep[1];
229 return ep;
231 unregister_dev:
232 device_unregister(&ep->dev);
233 return NULL;
235 free_ep:
236 kfree(ep);
237 return NULL;
239 EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
241 void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
243 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
244 device_unregister(&ep->dev);
246 EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
248 struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
250 struct iscsi_endpoint *ep;
251 struct device *dev;
253 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
254 iscsi_match_epid);
255 if (!dev)
256 return NULL;
258 ep = iscsi_dev_to_endpoint(dev);
260 * we can drop this now because the interface will prevent
261 * removals and lookups from racing.
263 put_device(dev);
264 return ep;
266 EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
269 * Interface to display network param to sysfs
272 static void iscsi_iface_release(struct device *dev)
274 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
275 struct device *parent = iface->dev.parent;
277 kfree(iface);
278 put_device(parent);
282 static struct class iscsi_iface_class = {
283 .name = "iscsi_iface",
284 .dev_release = iscsi_iface_release,
287 #define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
288 struct device_attribute dev_attr_##_prefix##_##_name = \
289 __ATTR(_name, _mode, _show, _store)
291 /* iface attrs show */
292 #define iscsi_iface_attr_show(type, name, param_type, param) \
293 static ssize_t \
294 show_##type##_##name(struct device *dev, struct device_attribute *attr, \
295 char *buf) \
297 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
298 struct iscsi_transport *t = iface->transport; \
299 return t->get_iface_param(iface, param_type, param, buf); \
302 #define iscsi_iface_net_attr(type, name, param) \
303 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
304 static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
306 /* generic read only ipvi4 attribute */
307 iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
308 iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
309 iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
310 iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
312 /* generic read only ipv6 attribute */
313 iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
314 iscsi_iface_net_attr(ipv6_iface, link_local_addr, ISCSI_NET_PARAM_IPV6_LINKLOCAL);
315 iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
316 iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
317 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
318 iscsi_iface_net_attr(ipv6_iface, linklocal_autocfg,
319 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
321 /* common read only iface attribute */
322 iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
323 iscsi_iface_net_attr(iface, vlan, ISCSI_NET_PARAM_VLAN_ID);
324 iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
325 iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
326 iscsi_iface_net_attr(iface, mtu, ISCSI_NET_PARAM_MTU);
327 iscsi_iface_net_attr(iface, port, ISCSI_NET_PARAM_PORT);
329 static mode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
330 struct attribute *attr, int i)
332 struct device *dev = container_of(kobj, struct device, kobj);
333 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
334 struct iscsi_transport *t = iface->transport;
335 int param;
337 if (attr == &dev_attr_iface_enabled.attr)
338 param = ISCSI_NET_PARAM_IFACE_ENABLE;
339 else if (attr == &dev_attr_iface_vlan.attr)
340 param = ISCSI_NET_PARAM_VLAN_ID;
341 else if (attr == &dev_attr_iface_vlan_priority.attr)
342 param = ISCSI_NET_PARAM_VLAN_PRIORITY;
343 else if (attr == &dev_attr_iface_vlan_enabled.attr)
344 param = ISCSI_NET_PARAM_VLAN_ENABLED;
345 else if (attr == &dev_attr_iface_mtu.attr)
346 param = ISCSI_NET_PARAM_MTU;
347 else if (attr == &dev_attr_iface_port.attr)
348 param = ISCSI_NET_PARAM_PORT;
349 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
350 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
351 param = ISCSI_NET_PARAM_IPV4_ADDR;
352 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
353 param = ISCSI_NET_PARAM_IPV4_GW;
354 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
355 param = ISCSI_NET_PARAM_IPV4_SUBNET;
356 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
357 param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
358 else
359 return 0;
360 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
361 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
362 param = ISCSI_NET_PARAM_IPV6_ADDR;
363 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
364 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
365 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
366 param = ISCSI_NET_PARAM_IPV6_ROUTER;
367 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
368 param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
369 else if (attr == &dev_attr_ipv6_iface_linklocal_autocfg.attr)
370 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
371 else
372 return 0;
373 } else {
374 WARN_ONCE(1, "Invalid iface attr");
375 return 0;
378 return t->attr_is_visible(ISCSI_NET_PARAM, param);
381 static struct attribute *iscsi_iface_attrs[] = {
382 &dev_attr_iface_enabled.attr,
383 &dev_attr_iface_vlan.attr,
384 &dev_attr_iface_vlan_priority.attr,
385 &dev_attr_iface_vlan_enabled.attr,
386 &dev_attr_ipv4_iface_ipaddress.attr,
387 &dev_attr_ipv4_iface_gateway.attr,
388 &dev_attr_ipv4_iface_subnet.attr,
389 &dev_attr_ipv4_iface_bootproto.attr,
390 &dev_attr_ipv6_iface_ipaddress.attr,
391 &dev_attr_ipv6_iface_link_local_addr.attr,
392 &dev_attr_ipv6_iface_router_addr.attr,
393 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
394 &dev_attr_ipv6_iface_linklocal_autocfg.attr,
395 &dev_attr_iface_mtu.attr,
396 &dev_attr_iface_port.attr,
397 NULL,
400 static struct attribute_group iscsi_iface_group = {
401 .attrs = iscsi_iface_attrs,
402 .is_visible = iscsi_iface_attr_is_visible,
405 struct iscsi_iface *
406 iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
407 uint32_t iface_type, uint32_t iface_num, int dd_size)
409 struct iscsi_iface *iface;
410 int err;
412 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
413 if (!iface)
414 return NULL;
416 iface->transport = transport;
417 iface->iface_type = iface_type;
418 iface->iface_num = iface_num;
419 iface->dev.release = iscsi_iface_release;
420 iface->dev.class = &iscsi_iface_class;
421 /* parent reference released in iscsi_iface_release */
422 iface->dev.parent = get_device(&shost->shost_gendev);
423 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
424 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
425 iface_num);
426 else
427 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
428 iface_num);
430 err = device_register(&iface->dev);
431 if (err)
432 goto free_iface;
434 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
435 if (err)
436 goto unreg_iface;
438 if (dd_size)
439 iface->dd_data = &iface[1];
440 return iface;
442 unreg_iface:
443 device_unregister(&iface->dev);
444 return NULL;
446 free_iface:
447 put_device(iface->dev.parent);
448 kfree(iface);
449 return NULL;
451 EXPORT_SYMBOL_GPL(iscsi_create_iface);
453 void iscsi_destroy_iface(struct iscsi_iface *iface)
455 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
456 device_unregister(&iface->dev);
458 EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
461 * BSG support
464 * iscsi_bsg_host_dispatch - Dispatch command to LLD.
465 * @job: bsg job to be processed
467 static int iscsi_bsg_host_dispatch(struct bsg_job *job)
469 struct Scsi_Host *shost = iscsi_job_to_shost(job);
470 struct iscsi_bsg_request *req = job->request;
471 struct iscsi_bsg_reply *reply = job->reply;
472 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
473 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
474 int ret;
476 /* check if we have the msgcode value at least */
477 if (job->request_len < sizeof(uint32_t)) {
478 ret = -ENOMSG;
479 goto fail_host_msg;
482 /* Validate the host command */
483 switch (req->msgcode) {
484 case ISCSI_BSG_HST_VENDOR:
485 cmdlen += sizeof(struct iscsi_bsg_host_vendor);
486 if ((shost->hostt->vendor_id == 0L) ||
487 (req->rqst_data.h_vendor.vendor_id !=
488 shost->hostt->vendor_id)) {
489 ret = -ESRCH;
490 goto fail_host_msg;
492 break;
493 default:
494 ret = -EBADR;
495 goto fail_host_msg;
498 /* check if we really have all the request data needed */
499 if (job->request_len < cmdlen) {
500 ret = -ENOMSG;
501 goto fail_host_msg;
504 ret = i->iscsi_transport->bsg_request(job);
505 if (!ret)
506 return 0;
508 fail_host_msg:
509 /* return the errno failure code as the only status */
510 BUG_ON(job->reply_len < sizeof(uint32_t));
511 reply->reply_payload_rcv_len = 0;
512 reply->result = ret;
513 job->reply_len = sizeof(uint32_t);
514 bsg_job_done(job, ret, 0);
515 return 0;
519 * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
520 * @shost: shost for iscsi_host
521 * @cls_host: iscsi_cls_host adding the structures to
523 static int
524 iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
526 struct device *dev = &shost->shost_gendev;
527 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
528 struct request_queue *q;
529 char bsg_name[20];
530 int ret;
532 if (!i->iscsi_transport->bsg_request)
533 return -ENOTSUPP;
535 snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
537 q = __scsi_alloc_queue(shost, bsg_request_fn);
538 if (!q)
539 return -ENOMEM;
541 ret = bsg_setup_queue(dev, q, bsg_name, iscsi_bsg_host_dispatch, 0);
542 if (ret) {
543 shost_printk(KERN_ERR, shost, "bsg interface failed to "
544 "initialize - no request queue\n");
545 blk_cleanup_queue(q);
546 return ret;
549 ihost->bsg_q = q;
550 return 0;
553 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
554 struct device *cdev)
556 struct Scsi_Host *shost = dev_to_shost(dev);
557 struct iscsi_cls_host *ihost = shost->shost_data;
559 memset(ihost, 0, sizeof(*ihost));
560 atomic_set(&ihost->nr_scans, 0);
561 mutex_init(&ihost->mutex);
563 iscsi_bsg_host_add(shost, ihost);
564 /* ignore any bsg add error - we just can't do sgio */
566 return 0;
569 static int iscsi_remove_host(struct transport_container *tc,
570 struct device *dev, struct device *cdev)
572 struct Scsi_Host *shost = dev_to_shost(dev);
573 struct iscsi_cls_host *ihost = shost->shost_data;
575 if (ihost->bsg_q) {
576 bsg_remove_queue(ihost->bsg_q);
577 blk_cleanup_queue(ihost->bsg_q);
579 return 0;
582 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
583 "iscsi_host",
584 iscsi_setup_host,
585 iscsi_remove_host,
586 NULL);
588 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
589 "iscsi_session",
590 NULL,
591 NULL,
592 NULL);
594 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
595 "iscsi_connection",
596 NULL,
597 NULL,
598 NULL);
600 static struct sock *nls;
601 static DEFINE_MUTEX(rx_queue_mutex);
603 static LIST_HEAD(sesslist);
604 static DEFINE_SPINLOCK(sesslock);
605 static LIST_HEAD(connlist);
606 static DEFINE_SPINLOCK(connlock);
608 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
610 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
611 return sess->sid;
615 * Returns the matching session to a given sid
617 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
619 unsigned long flags;
620 struct iscsi_cls_session *sess;
622 spin_lock_irqsave(&sesslock, flags);
623 list_for_each_entry(sess, &sesslist, sess_list) {
624 if (sess->sid == sid) {
625 spin_unlock_irqrestore(&sesslock, flags);
626 return sess;
629 spin_unlock_irqrestore(&sesslock, flags);
630 return NULL;
634 * Returns the matching connection to a given sid / cid tuple
636 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
638 unsigned long flags;
639 struct iscsi_cls_conn *conn;
641 spin_lock_irqsave(&connlock, flags);
642 list_for_each_entry(conn, &connlist, conn_list) {
643 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
644 spin_unlock_irqrestore(&connlock, flags);
645 return conn;
648 spin_unlock_irqrestore(&connlock, flags);
649 return NULL;
653 * The following functions can be used by LLDs that allocate
654 * their own scsi_hosts or by software iscsi LLDs
656 static struct {
657 int value;
658 char *name;
659 } iscsi_session_state_names[] = {
660 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
661 { ISCSI_SESSION_FAILED, "FAILED" },
662 { ISCSI_SESSION_FREE, "FREE" },
665 static const char *iscsi_session_state_name(int state)
667 int i;
668 char *name = NULL;
670 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
671 if (iscsi_session_state_names[i].value == state) {
672 name = iscsi_session_state_names[i].name;
673 break;
676 return name;
679 int iscsi_session_chkready(struct iscsi_cls_session *session)
681 unsigned long flags;
682 int err;
684 spin_lock_irqsave(&session->lock, flags);
685 switch (session->state) {
686 case ISCSI_SESSION_LOGGED_IN:
687 err = 0;
688 break;
689 case ISCSI_SESSION_FAILED:
690 err = DID_IMM_RETRY << 16;
691 break;
692 case ISCSI_SESSION_FREE:
693 err = DID_TRANSPORT_FAILFAST << 16;
694 break;
695 default:
696 err = DID_NO_CONNECT << 16;
697 break;
699 spin_unlock_irqrestore(&session->lock, flags);
700 return err;
702 EXPORT_SYMBOL_GPL(iscsi_session_chkready);
704 int iscsi_is_session_online(struct iscsi_cls_session *session)
706 unsigned long flags;
707 int ret = 0;
709 spin_lock_irqsave(&session->lock, flags);
710 if (session->state == ISCSI_SESSION_LOGGED_IN)
711 ret = 1;
712 spin_unlock_irqrestore(&session->lock, flags);
713 return ret;
715 EXPORT_SYMBOL_GPL(iscsi_is_session_online);
717 static void iscsi_session_release(struct device *dev)
719 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
720 struct Scsi_Host *shost;
722 shost = iscsi_session_to_shost(session);
723 scsi_host_put(shost);
724 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
725 kfree(session);
728 static int iscsi_is_session_dev(const struct device *dev)
730 return dev->release == iscsi_session_release;
733 static int iscsi_iter_session_fn(struct device *dev, void *data)
735 void (* fn) (struct iscsi_cls_session *) = data;
737 if (!iscsi_is_session_dev(dev))
738 return 0;
739 fn(iscsi_dev_to_session(dev));
740 return 0;
743 void iscsi_host_for_each_session(struct Scsi_Host *shost,
744 void (*fn)(struct iscsi_cls_session *))
746 device_for_each_child(&shost->shost_gendev, fn,
747 iscsi_iter_session_fn);
749 EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
752 * iscsi_scan_finished - helper to report when running scans are done
753 * @shost: scsi host
754 * @time: scan run time
756 * This function can be used by drives like qla4xxx to report to the scsi
757 * layer when the scans it kicked off at module load time are done.
759 int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
761 struct iscsi_cls_host *ihost = shost->shost_data;
763 * qla4xxx will have kicked off some session unblocks before calling
764 * scsi_scan_host, so just wait for them to complete.
766 return !atomic_read(&ihost->nr_scans);
768 EXPORT_SYMBOL_GPL(iscsi_scan_finished);
770 struct iscsi_scan_data {
771 unsigned int channel;
772 unsigned int id;
773 unsigned int lun;
776 static int iscsi_user_scan_session(struct device *dev, void *data)
778 struct iscsi_scan_data *scan_data = data;
779 struct iscsi_cls_session *session;
780 struct Scsi_Host *shost;
781 struct iscsi_cls_host *ihost;
782 unsigned long flags;
783 unsigned int id;
785 if (!iscsi_is_session_dev(dev))
786 return 0;
788 session = iscsi_dev_to_session(dev);
790 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
792 shost = iscsi_session_to_shost(session);
793 ihost = shost->shost_data;
795 mutex_lock(&ihost->mutex);
796 spin_lock_irqsave(&session->lock, flags);
797 if (session->state != ISCSI_SESSION_LOGGED_IN) {
798 spin_unlock_irqrestore(&session->lock, flags);
799 goto user_scan_exit;
801 id = session->target_id;
802 spin_unlock_irqrestore(&session->lock, flags);
804 if (id != ISCSI_MAX_TARGET) {
805 if ((scan_data->channel == SCAN_WILD_CARD ||
806 scan_data->channel == 0) &&
807 (scan_data->id == SCAN_WILD_CARD ||
808 scan_data->id == id))
809 scsi_scan_target(&session->dev, 0, id,
810 scan_data->lun, 1);
813 user_scan_exit:
814 mutex_unlock(&ihost->mutex);
815 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
816 return 0;
819 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
820 uint id, uint lun)
822 struct iscsi_scan_data scan_data;
824 scan_data.channel = channel;
825 scan_data.id = id;
826 scan_data.lun = lun;
828 return device_for_each_child(&shost->shost_gendev, &scan_data,
829 iscsi_user_scan_session);
832 static void iscsi_scan_session(struct work_struct *work)
834 struct iscsi_cls_session *session =
835 container_of(work, struct iscsi_cls_session, scan_work);
836 struct Scsi_Host *shost = iscsi_session_to_shost(session);
837 struct iscsi_cls_host *ihost = shost->shost_data;
838 struct iscsi_scan_data scan_data;
840 scan_data.channel = 0;
841 scan_data.id = SCAN_WILD_CARD;
842 scan_data.lun = SCAN_WILD_CARD;
844 iscsi_user_scan_session(&session->dev, &scan_data);
845 atomic_dec(&ihost->nr_scans);
849 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
850 * @cmd: scsi cmd passed to scsi eh handler
852 * If the session is down this function will wait for the recovery
853 * timer to fire or for the session to be logged back in. If the
854 * recovery timer fires then FAST_IO_FAIL is returned. The caller
855 * should pass this error value to the scsi eh.
857 int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
859 struct iscsi_cls_session *session =
860 starget_to_session(scsi_target(cmd->device));
861 unsigned long flags;
862 int ret = 0;
864 spin_lock_irqsave(&session->lock, flags);
865 while (session->state != ISCSI_SESSION_LOGGED_IN) {
866 if (session->state == ISCSI_SESSION_FREE) {
867 ret = FAST_IO_FAIL;
868 break;
870 spin_unlock_irqrestore(&session->lock, flags);
871 msleep(1000);
872 spin_lock_irqsave(&session->lock, flags);
874 spin_unlock_irqrestore(&session->lock, flags);
875 return ret;
877 EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
879 static void session_recovery_timedout(struct work_struct *work)
881 struct iscsi_cls_session *session =
882 container_of(work, struct iscsi_cls_session,
883 recovery_work.work);
884 unsigned long flags;
886 iscsi_cls_session_printk(KERN_INFO, session,
887 "session recovery timed out after %d secs\n",
888 session->recovery_tmo);
890 spin_lock_irqsave(&session->lock, flags);
891 switch (session->state) {
892 case ISCSI_SESSION_FAILED:
893 session->state = ISCSI_SESSION_FREE;
894 break;
895 case ISCSI_SESSION_LOGGED_IN:
896 case ISCSI_SESSION_FREE:
897 /* we raced with the unblock's flush */
898 spin_unlock_irqrestore(&session->lock, flags);
899 return;
901 spin_unlock_irqrestore(&session->lock, flags);
903 if (session->transport->session_recovery_timedout)
904 session->transport->session_recovery_timedout(session);
906 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
907 scsi_target_unblock(&session->dev);
908 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
911 static void __iscsi_unblock_session(struct work_struct *work)
913 struct iscsi_cls_session *session =
914 container_of(work, struct iscsi_cls_session,
915 unblock_work);
916 struct Scsi_Host *shost = iscsi_session_to_shost(session);
917 struct iscsi_cls_host *ihost = shost->shost_data;
918 unsigned long flags;
920 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
922 * The recovery and unblock work get run from the same workqueue,
923 * so try to cancel it if it was going to run after this unblock.
925 cancel_delayed_work(&session->recovery_work);
926 spin_lock_irqsave(&session->lock, flags);
927 session->state = ISCSI_SESSION_LOGGED_IN;
928 spin_unlock_irqrestore(&session->lock, flags);
929 /* start IO */
930 scsi_target_unblock(&session->dev);
932 * Only do kernel scanning if the driver is properly hooked into
933 * the async scanning code (drivers like iscsi_tcp do login and
934 * scanning from userspace).
936 if (shost->hostt->scan_finished) {
937 if (scsi_queue_work(shost, &session->scan_work))
938 atomic_inc(&ihost->nr_scans);
940 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
944 * iscsi_unblock_session - set a session as logged in and start IO.
945 * @session: iscsi session
947 * Mark a session as ready to accept IO.
949 void iscsi_unblock_session(struct iscsi_cls_session *session)
951 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
953 * make sure all the events have completed before tell the driver
954 * it is safe
956 flush_workqueue(iscsi_eh_timer_workq);
958 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
960 static void __iscsi_block_session(struct work_struct *work)
962 struct iscsi_cls_session *session =
963 container_of(work, struct iscsi_cls_session,
964 block_work);
965 unsigned long flags;
967 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
968 spin_lock_irqsave(&session->lock, flags);
969 session->state = ISCSI_SESSION_FAILED;
970 spin_unlock_irqrestore(&session->lock, flags);
971 scsi_target_block(&session->dev);
972 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
973 if (session->recovery_tmo >= 0)
974 queue_delayed_work(iscsi_eh_timer_workq,
975 &session->recovery_work,
976 session->recovery_tmo * HZ);
979 void iscsi_block_session(struct iscsi_cls_session *session)
981 queue_work(iscsi_eh_timer_workq, &session->block_work);
983 EXPORT_SYMBOL_GPL(iscsi_block_session);
985 static void __iscsi_unbind_session(struct work_struct *work)
987 struct iscsi_cls_session *session =
988 container_of(work, struct iscsi_cls_session,
989 unbind_work);
990 struct Scsi_Host *shost = iscsi_session_to_shost(session);
991 struct iscsi_cls_host *ihost = shost->shost_data;
992 unsigned long flags;
994 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
996 /* Prevent new scans and make sure scanning is not in progress */
997 mutex_lock(&ihost->mutex);
998 spin_lock_irqsave(&session->lock, flags);
999 if (session->target_id == ISCSI_MAX_TARGET) {
1000 spin_unlock_irqrestore(&session->lock, flags);
1001 mutex_unlock(&ihost->mutex);
1002 return;
1004 session->target_id = ISCSI_MAX_TARGET;
1005 spin_unlock_irqrestore(&session->lock, flags);
1006 mutex_unlock(&ihost->mutex);
1008 scsi_remove_target(&session->dev);
1009 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
1010 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
1013 struct iscsi_cls_session *
1014 iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1015 int dd_size)
1017 struct iscsi_cls_session *session;
1019 session = kzalloc(sizeof(*session) + dd_size,
1020 GFP_KERNEL);
1021 if (!session)
1022 return NULL;
1024 session->transport = transport;
1025 session->recovery_tmo = 120;
1026 session->state = ISCSI_SESSION_FREE;
1027 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
1028 INIT_LIST_HEAD(&session->sess_list);
1029 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
1030 INIT_WORK(&session->block_work, __iscsi_block_session);
1031 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
1032 INIT_WORK(&session->scan_work, iscsi_scan_session);
1033 spin_lock_init(&session->lock);
1035 /* this is released in the dev's release function */
1036 scsi_host_get(shost);
1037 session->dev.parent = &shost->shost_gendev;
1038 session->dev.release = iscsi_session_release;
1039 device_initialize(&session->dev);
1040 if (dd_size)
1041 session->dd_data = &session[1];
1043 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
1044 return session;
1046 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
1048 static int iscsi_get_next_target_id(struct device *dev, void *data)
1050 struct iscsi_cls_session *session;
1051 unsigned long flags;
1052 int err = 0;
1054 if (!iscsi_is_session_dev(dev))
1055 return 0;
1057 session = iscsi_dev_to_session(dev);
1058 spin_lock_irqsave(&session->lock, flags);
1059 if (*((unsigned int *) data) == session->target_id)
1060 err = -EEXIST;
1061 spin_unlock_irqrestore(&session->lock, flags);
1062 return err;
1065 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
1067 struct Scsi_Host *shost = iscsi_session_to_shost(session);
1068 struct iscsi_cls_host *ihost;
1069 unsigned long flags;
1070 unsigned int id = target_id;
1071 int err;
1073 ihost = shost->shost_data;
1074 session->sid = atomic_add_return(1, &iscsi_session_nr);
1076 if (id == ISCSI_MAX_TARGET) {
1077 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
1078 err = device_for_each_child(&shost->shost_gendev, &id,
1079 iscsi_get_next_target_id);
1080 if (!err)
1081 break;
1084 if (id == ISCSI_MAX_TARGET) {
1085 iscsi_cls_session_printk(KERN_ERR, session,
1086 "Too many iscsi targets. Max "
1087 "number of targets is %d.\n",
1088 ISCSI_MAX_TARGET - 1);
1089 err = -EOVERFLOW;
1090 goto release_host;
1093 session->target_id = id;
1095 dev_set_name(&session->dev, "session%u", session->sid);
1096 err = device_add(&session->dev);
1097 if (err) {
1098 iscsi_cls_session_printk(KERN_ERR, session,
1099 "could not register session's dev\n");
1100 goto release_host;
1102 transport_register_device(&session->dev);
1104 spin_lock_irqsave(&sesslock, flags);
1105 list_add(&session->sess_list, &sesslist);
1106 spin_unlock_irqrestore(&sesslock, flags);
1108 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
1109 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
1110 return 0;
1112 release_host:
1113 scsi_host_put(shost);
1114 return err;
1116 EXPORT_SYMBOL_GPL(iscsi_add_session);
1119 * iscsi_create_session - create iscsi class session
1120 * @shost: scsi host
1121 * @transport: iscsi transport
1122 * @dd_size: private driver data size
1123 * @target_id: which target
1125 * This can be called from a LLD or iscsi_transport.
1127 struct iscsi_cls_session *
1128 iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1129 int dd_size, unsigned int target_id)
1131 struct iscsi_cls_session *session;
1133 session = iscsi_alloc_session(shost, transport, dd_size);
1134 if (!session)
1135 return NULL;
1137 if (iscsi_add_session(session, target_id)) {
1138 iscsi_free_session(session);
1139 return NULL;
1141 return session;
1143 EXPORT_SYMBOL_GPL(iscsi_create_session);
1145 static void iscsi_conn_release(struct device *dev)
1147 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
1148 struct device *parent = conn->dev.parent;
1150 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
1151 kfree(conn);
1152 put_device(parent);
1155 static int iscsi_is_conn_dev(const struct device *dev)
1157 return dev->release == iscsi_conn_release;
1160 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
1162 if (!iscsi_is_conn_dev(dev))
1163 return 0;
1164 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
1167 void iscsi_remove_session(struct iscsi_cls_session *session)
1169 struct Scsi_Host *shost = iscsi_session_to_shost(session);
1170 unsigned long flags;
1171 int err;
1173 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
1175 spin_lock_irqsave(&sesslock, flags);
1176 list_del(&session->sess_list);
1177 spin_unlock_irqrestore(&sesslock, flags);
1179 /* make sure there are no blocks/unblocks queued */
1180 flush_workqueue(iscsi_eh_timer_workq);
1181 /* make sure the timedout callout is not running */
1182 if (!cancel_delayed_work(&session->recovery_work))
1183 flush_workqueue(iscsi_eh_timer_workq);
1185 * If we are blocked let commands flow again. The lld or iscsi
1186 * layer should set up the queuecommand to fail commands.
1187 * We assume that LLD will not be calling block/unblock while
1188 * removing the session.
1190 spin_lock_irqsave(&session->lock, flags);
1191 session->state = ISCSI_SESSION_FREE;
1192 spin_unlock_irqrestore(&session->lock, flags);
1194 scsi_target_unblock(&session->dev);
1195 /* flush running scans then delete devices */
1196 scsi_flush_work(shost);
1197 __iscsi_unbind_session(&session->unbind_work);
1199 /* hw iscsi may not have removed all connections from session */
1200 err = device_for_each_child(&session->dev, NULL,
1201 iscsi_iter_destroy_conn_fn);
1202 if (err)
1203 iscsi_cls_session_printk(KERN_ERR, session,
1204 "Could not delete all connections "
1205 "for session. Error %d.\n", err);
1207 transport_unregister_device(&session->dev);
1209 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
1210 device_del(&session->dev);
1212 EXPORT_SYMBOL_GPL(iscsi_remove_session);
1214 void iscsi_free_session(struct iscsi_cls_session *session)
1216 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
1217 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
1218 put_device(&session->dev);
1220 EXPORT_SYMBOL_GPL(iscsi_free_session);
1223 * iscsi_destroy_session - destroy iscsi session
1224 * @session: iscsi_session
1226 * Can be called by a LLD or iscsi_transport. There must not be
1227 * any running connections.
1229 int iscsi_destroy_session(struct iscsi_cls_session *session)
1231 iscsi_remove_session(session);
1232 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
1233 iscsi_free_session(session);
1234 return 0;
1236 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
1239 * iscsi_create_conn - create iscsi class connection
1240 * @session: iscsi cls session
1241 * @dd_size: private driver data size
1242 * @cid: connection id
1244 * This can be called from a LLD or iscsi_transport. The connection
1245 * is child of the session so cid must be unique for all connections
1246 * on the session.
1248 * Since we do not support MCS, cid will normally be zero. In some cases
1249 * for software iscsi we could be trying to preallocate a connection struct
1250 * in which case there could be two connection structs and cid would be
1251 * non-zero.
1253 struct iscsi_cls_conn *
1254 iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
1256 struct iscsi_transport *transport = session->transport;
1257 struct iscsi_cls_conn *conn;
1258 unsigned long flags;
1259 int err;
1261 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
1262 if (!conn)
1263 return NULL;
1264 if (dd_size)
1265 conn->dd_data = &conn[1];
1267 mutex_init(&conn->ep_mutex);
1268 INIT_LIST_HEAD(&conn->conn_list);
1269 conn->transport = transport;
1270 conn->cid = cid;
1272 /* this is released in the dev's release function */
1273 if (!get_device(&session->dev))
1274 goto free_conn;
1276 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
1277 conn->dev.parent = &session->dev;
1278 conn->dev.release = iscsi_conn_release;
1279 err = device_register(&conn->dev);
1280 if (err) {
1281 iscsi_cls_session_printk(KERN_ERR, session, "could not "
1282 "register connection's dev\n");
1283 goto release_parent_ref;
1285 transport_register_device(&conn->dev);
1287 spin_lock_irqsave(&connlock, flags);
1288 list_add(&conn->conn_list, &connlist);
1289 spin_unlock_irqrestore(&connlock, flags);
1291 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
1292 return conn;
1294 release_parent_ref:
1295 put_device(&session->dev);
1296 free_conn:
1297 kfree(conn);
1298 return NULL;
1301 EXPORT_SYMBOL_GPL(iscsi_create_conn);
1304 * iscsi_destroy_conn - destroy iscsi class connection
1305 * @conn: iscsi cls session
1307 * This can be called from a LLD or iscsi_transport.
1309 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1311 unsigned long flags;
1313 spin_lock_irqsave(&connlock, flags);
1314 list_del(&conn->conn_list);
1315 spin_unlock_irqrestore(&connlock, flags);
1317 transport_unregister_device(&conn->dev);
1318 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
1319 device_unregister(&conn->dev);
1320 return 0;
1322 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1325 * iscsi interface functions
1327 static struct iscsi_internal *
1328 iscsi_if_transport_lookup(struct iscsi_transport *tt)
1330 struct iscsi_internal *priv;
1331 unsigned long flags;
1333 spin_lock_irqsave(&iscsi_transport_lock, flags);
1334 list_for_each_entry(priv, &iscsi_transports, list) {
1335 if (tt == priv->iscsi_transport) {
1336 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1337 return priv;
1340 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1341 return NULL;
1344 static int
1345 iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
1347 return nlmsg_multicast(nls, skb, 0, group, gfp);
1350 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
1351 char *data, uint32_t data_size)
1353 struct nlmsghdr *nlh;
1354 struct sk_buff *skb;
1355 struct iscsi_uevent *ev;
1356 char *pdu;
1357 struct iscsi_internal *priv;
1358 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1359 data_size);
1361 priv = iscsi_if_transport_lookup(conn->transport);
1362 if (!priv)
1363 return -EINVAL;
1365 skb = alloc_skb(len, GFP_ATOMIC);
1366 if (!skb) {
1367 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
1368 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1369 "control PDU: OOM\n");
1370 return -ENOMEM;
1373 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1374 ev = NLMSG_DATA(nlh);
1375 memset(ev, 0, sizeof(*ev));
1376 ev->transport_handle = iscsi_handle(conn->transport);
1377 ev->type = ISCSI_KEVENT_RECV_PDU;
1378 ev->r.recv_req.cid = conn->cid;
1379 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
1380 pdu = (char*)ev + sizeof(*ev);
1381 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1382 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1384 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
1386 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1388 int iscsi_offload_mesg(struct Scsi_Host *shost,
1389 struct iscsi_transport *transport, uint32_t type,
1390 char *data, uint16_t data_size)
1392 struct nlmsghdr *nlh;
1393 struct sk_buff *skb;
1394 struct iscsi_uevent *ev;
1395 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
1397 skb = alloc_skb(len, GFP_ATOMIC);
1398 if (!skb) {
1399 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
1400 return -ENOMEM;
1403 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1404 ev = NLMSG_DATA(nlh);
1405 memset(ev, 0, sizeof(*ev));
1406 ev->type = type;
1407 ev->transport_handle = iscsi_handle(transport);
1408 switch (type) {
1409 case ISCSI_KEVENT_PATH_REQ:
1410 ev->r.req_path.host_no = shost->host_no;
1411 break;
1412 case ISCSI_KEVENT_IF_DOWN:
1413 ev->r.notify_if_down.host_no = shost->host_no;
1414 break;
1417 memcpy((char *)ev + sizeof(*ev), data, data_size);
1419 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
1421 EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1423 void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
1425 struct nlmsghdr *nlh;
1426 struct sk_buff *skb;
1427 struct iscsi_uevent *ev;
1428 struct iscsi_internal *priv;
1429 int len = NLMSG_SPACE(sizeof(*ev));
1431 priv = iscsi_if_transport_lookup(conn->transport);
1432 if (!priv)
1433 return;
1435 skb = alloc_skb(len, GFP_ATOMIC);
1436 if (!skb) {
1437 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1438 "conn error (%d)\n", error);
1439 return;
1442 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1443 ev = NLMSG_DATA(nlh);
1444 ev->transport_handle = iscsi_handle(conn->transport);
1445 ev->type = ISCSI_KEVENT_CONN_ERROR;
1446 ev->r.connerror.error = error;
1447 ev->r.connerror.cid = conn->cid;
1448 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
1450 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
1452 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1453 error);
1455 EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
1457 void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
1458 enum iscsi_conn_state state)
1460 struct nlmsghdr *nlh;
1461 struct sk_buff *skb;
1462 struct iscsi_uevent *ev;
1463 struct iscsi_internal *priv;
1464 int len = NLMSG_SPACE(sizeof(*ev));
1466 priv = iscsi_if_transport_lookup(conn->transport);
1467 if (!priv)
1468 return;
1470 skb = alloc_skb(len, GFP_ATOMIC);
1471 if (!skb) {
1472 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1473 "conn login (%d)\n", state);
1474 return;
1477 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1478 ev = NLMSG_DATA(nlh);
1479 ev->transport_handle = iscsi_handle(conn->transport);
1480 ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
1481 ev->r.conn_login.state = state;
1482 ev->r.conn_login.cid = conn->cid;
1483 ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
1484 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
1486 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
1487 state);
1489 EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
1491 static int
1492 iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1493 void *payload, int size)
1495 struct sk_buff *skb;
1496 struct nlmsghdr *nlh;
1497 int len = NLMSG_SPACE(size);
1498 int flags = multi ? NLM_F_MULTI : 0;
1499 int t = done ? NLMSG_DONE : type;
1501 skb = alloc_skb(len, GFP_ATOMIC);
1502 if (!skb) {
1503 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1504 return -ENOMEM;
1507 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
1508 nlh->nlmsg_flags = flags;
1509 memcpy(NLMSG_DATA(nlh), payload, size);
1510 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
1513 static int
1514 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
1516 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1517 struct iscsi_stats *stats;
1518 struct sk_buff *skbstat;
1519 struct iscsi_cls_conn *conn;
1520 struct nlmsghdr *nlhstat;
1521 struct iscsi_uevent *evstat;
1522 struct iscsi_internal *priv;
1523 int len = NLMSG_SPACE(sizeof(*ev) +
1524 sizeof(struct iscsi_stats) +
1525 sizeof(struct iscsi_stats_custom) *
1526 ISCSI_STATS_CUSTOM_MAX);
1527 int err = 0;
1529 priv = iscsi_if_transport_lookup(transport);
1530 if (!priv)
1531 return -EINVAL;
1533 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
1534 if (!conn)
1535 return -EEXIST;
1537 do {
1538 int actual_size;
1540 skbstat = alloc_skb(len, GFP_ATOMIC);
1541 if (!skbstat) {
1542 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1543 "deliver stats: OOM\n");
1544 return -ENOMEM;
1547 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
1548 (len - sizeof(*nlhstat)), 0);
1549 evstat = NLMSG_DATA(nlhstat);
1550 memset(evstat, 0, sizeof(*evstat));
1551 evstat->transport_handle = iscsi_handle(conn->transport);
1552 evstat->type = nlh->nlmsg_type;
1553 evstat->u.get_stats.cid =
1554 ev->u.get_stats.cid;
1555 evstat->u.get_stats.sid =
1556 ev->u.get_stats.sid;
1557 stats = (struct iscsi_stats *)
1558 ((char*)evstat + sizeof(*evstat));
1559 memset(stats, 0, sizeof(*stats));
1561 transport->get_stats(conn, stats);
1562 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1563 sizeof(struct iscsi_stats) +
1564 sizeof(struct iscsi_stats_custom) *
1565 stats->custom_length);
1566 actual_size -= sizeof(*nlhstat);
1567 actual_size = NLMSG_LENGTH(actual_size);
1568 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
1569 nlhstat->nlmsg_len = actual_size;
1571 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1572 GFP_ATOMIC);
1573 } while (err < 0 && err != -ECONNREFUSED);
1575 return err;
1579 * iscsi_session_event - send session destr. completion event
1580 * @session: iscsi class session
1581 * @event: type of event
1583 int iscsi_session_event(struct iscsi_cls_session *session,
1584 enum iscsi_uevent_e event)
1586 struct iscsi_internal *priv;
1587 struct Scsi_Host *shost;
1588 struct iscsi_uevent *ev;
1589 struct sk_buff *skb;
1590 struct nlmsghdr *nlh;
1591 int rc, len = NLMSG_SPACE(sizeof(*ev));
1593 priv = iscsi_if_transport_lookup(session->transport);
1594 if (!priv)
1595 return -EINVAL;
1596 shost = iscsi_session_to_shost(session);
1598 skb = alloc_skb(len, GFP_KERNEL);
1599 if (!skb) {
1600 iscsi_cls_session_printk(KERN_ERR, session,
1601 "Cannot notify userspace of session "
1602 "event %u\n", event);
1603 return -ENOMEM;
1606 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1607 ev = NLMSG_DATA(nlh);
1608 ev->transport_handle = iscsi_handle(session->transport);
1610 ev->type = event;
1611 switch (event) {
1612 case ISCSI_KEVENT_DESTROY_SESSION:
1613 ev->r.d_session.host_no = shost->host_no;
1614 ev->r.d_session.sid = session->sid;
1615 break;
1616 case ISCSI_KEVENT_CREATE_SESSION:
1617 ev->r.c_session_ret.host_no = shost->host_no;
1618 ev->r.c_session_ret.sid = session->sid;
1619 break;
1620 case ISCSI_KEVENT_UNBIND_SESSION:
1621 ev->r.unbind_session.host_no = shost->host_no;
1622 ev->r.unbind_session.sid = session->sid;
1623 break;
1624 default:
1625 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1626 "%u.\n", event);
1627 kfree_skb(skb);
1628 return -EINVAL;
1632 * this will occur if the daemon is not up, so we just warn
1633 * the user and when the daemon is restarted it will handle it
1635 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
1636 if (rc == -ESRCH)
1637 iscsi_cls_session_printk(KERN_ERR, session,
1638 "Cannot notify userspace of session "
1639 "event %u. Check iscsi daemon\n",
1640 event);
1642 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
1643 event, rc);
1644 return rc;
1646 EXPORT_SYMBOL_GPL(iscsi_session_event);
1648 static int
1649 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1650 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
1651 uint16_t cmds_max, uint16_t queue_depth)
1653 struct iscsi_transport *transport = priv->iscsi_transport;
1654 struct iscsi_cls_session *session;
1655 struct Scsi_Host *shost;
1657 session = transport->create_session(ep, cmds_max, queue_depth,
1658 initial_cmdsn);
1659 if (!session)
1660 return -ENOMEM;
1662 shost = iscsi_session_to_shost(session);
1663 ev->r.c_session_ret.host_no = shost->host_no;
1664 ev->r.c_session_ret.sid = session->sid;
1665 ISCSI_DBG_TRANS_SESSION(session,
1666 "Completed creating transport session\n");
1667 return 0;
1670 static int
1671 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1673 struct iscsi_cls_conn *conn;
1674 struct iscsi_cls_session *session;
1676 session = iscsi_session_lookup(ev->u.c_conn.sid);
1677 if (!session) {
1678 printk(KERN_ERR "iscsi: invalid session %d.\n",
1679 ev->u.c_conn.sid);
1680 return -EINVAL;
1683 conn = transport->create_conn(session, ev->u.c_conn.cid);
1684 if (!conn) {
1685 iscsi_cls_session_printk(KERN_ERR, session,
1686 "couldn't create a new connection.");
1687 return -ENOMEM;
1690 ev->r.c_conn_ret.sid = session->sid;
1691 ev->r.c_conn_ret.cid = conn->cid;
1693 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
1694 return 0;
1697 static int
1698 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1700 struct iscsi_cls_conn *conn;
1702 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
1703 if (!conn)
1704 return -EINVAL;
1706 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
1707 if (transport->destroy_conn)
1708 transport->destroy_conn(conn);
1710 return 0;
1713 static int
1714 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1716 char *data = (char*)ev + sizeof(*ev);
1717 struct iscsi_cls_conn *conn;
1718 struct iscsi_cls_session *session;
1719 int err = 0, value = 0;
1721 session = iscsi_session_lookup(ev->u.set_param.sid);
1722 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1723 if (!conn || !session)
1724 return -EINVAL;
1726 switch (ev->u.set_param.param) {
1727 case ISCSI_PARAM_SESS_RECOVERY_TMO:
1728 sscanf(data, "%d", &value);
1729 session->recovery_tmo = value;
1730 break;
1731 default:
1732 err = transport->set_param(conn, ev->u.set_param.param,
1733 data, ev->u.set_param.len);
1736 return err;
1739 static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1740 struct iscsi_uevent *ev, int msg_type)
1742 struct iscsi_endpoint *ep;
1743 struct sockaddr *dst_addr;
1744 struct Scsi_Host *shost = NULL;
1745 int non_blocking, err = 0;
1747 if (!transport->ep_connect)
1748 return -EINVAL;
1750 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1751 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1752 if (!shost) {
1753 printk(KERN_ERR "ep connect failed. Could not find "
1754 "host no %u\n",
1755 ev->u.ep_connect_through_host.host_no);
1756 return -ENODEV;
1758 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1759 } else
1760 non_blocking = ev->u.ep_connect.non_blocking;
1762 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1763 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1764 if (IS_ERR(ep)) {
1765 err = PTR_ERR(ep);
1766 goto release_host;
1769 ev->r.ep_connect_ret.handle = ep->id;
1770 release_host:
1771 if (shost)
1772 scsi_host_put(shost);
1773 return err;
1776 static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
1777 u64 ep_handle)
1779 struct iscsi_cls_conn *conn;
1780 struct iscsi_endpoint *ep;
1782 if (!transport->ep_disconnect)
1783 return -EINVAL;
1785 ep = iscsi_lookup_endpoint(ep_handle);
1786 if (!ep)
1787 return -EINVAL;
1788 conn = ep->conn;
1789 if (conn) {
1790 mutex_lock(&conn->ep_mutex);
1791 conn->ep = NULL;
1792 mutex_unlock(&conn->ep_mutex);
1795 transport->ep_disconnect(ep);
1796 return 0;
1799 static int
1800 iscsi_if_transport_ep(struct iscsi_transport *transport,
1801 struct iscsi_uevent *ev, int msg_type)
1803 struct iscsi_endpoint *ep;
1804 int rc = 0;
1806 switch (msg_type) {
1807 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
1808 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1809 rc = iscsi_if_ep_connect(transport, ev, msg_type);
1810 break;
1811 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1812 if (!transport->ep_poll)
1813 return -EINVAL;
1815 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1816 if (!ep)
1817 return -EINVAL;
1819 ev->r.retcode = transport->ep_poll(ep,
1820 ev->u.ep_poll.timeout_ms);
1821 break;
1822 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1823 rc = iscsi_if_ep_disconnect(transport,
1824 ev->u.ep_disconnect.ep_handle);
1825 break;
1827 return rc;
1830 static int
1831 iscsi_tgt_dscvr(struct iscsi_transport *transport,
1832 struct iscsi_uevent *ev)
1834 struct Scsi_Host *shost;
1835 struct sockaddr *dst_addr;
1836 int err;
1838 if (!transport->tgt_dscvr)
1839 return -EINVAL;
1841 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1842 if (!shost) {
1843 printk(KERN_ERR "target discovery could not find host no %u\n",
1844 ev->u.tgt_dscvr.host_no);
1845 return -ENODEV;
1849 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1850 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1851 ev->u.tgt_dscvr.enable, dst_addr);
1852 scsi_host_put(shost);
1853 return err;
1856 static int
1857 iscsi_set_host_param(struct iscsi_transport *transport,
1858 struct iscsi_uevent *ev)
1860 char *data = (char*)ev + sizeof(*ev);
1861 struct Scsi_Host *shost;
1862 int err;
1864 if (!transport->set_host_param)
1865 return -ENOSYS;
1867 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1868 if (!shost) {
1869 printk(KERN_ERR "set_host_param could not find host no %u\n",
1870 ev->u.set_host_param.host_no);
1871 return -ENODEV;
1874 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1875 data, ev->u.set_host_param.len);
1876 scsi_host_put(shost);
1877 return err;
1880 static int
1881 iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1883 struct Scsi_Host *shost;
1884 struct iscsi_path *params;
1885 int err;
1887 if (!transport->set_path)
1888 return -ENOSYS;
1890 shost = scsi_host_lookup(ev->u.set_path.host_no);
1891 if (!shost) {
1892 printk(KERN_ERR "set path could not find host no %u\n",
1893 ev->u.set_path.host_no);
1894 return -ENODEV;
1897 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1898 err = transport->set_path(shost, params);
1900 scsi_host_put(shost);
1901 return err;
1904 static int
1905 iscsi_set_iface_params(struct iscsi_transport *transport,
1906 struct iscsi_uevent *ev)
1908 char *data = (char *)ev + sizeof(*ev);
1909 struct Scsi_Host *shost;
1910 int err;
1912 if (!transport->set_iface_param)
1913 return -ENOSYS;
1915 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
1916 if (!shost) {
1917 printk(KERN_ERR "set_iface_params could not find host no %u\n",
1918 ev->u.set_iface_params.host_no);
1919 return -ENODEV;
1922 err = transport->set_iface_param(shost, data,
1923 ev->u.set_iface_params.count);
1924 scsi_host_put(shost);
1925 return err;
1928 static int
1929 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
1931 int err = 0;
1932 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1933 struct iscsi_transport *transport = NULL;
1934 struct iscsi_internal *priv;
1935 struct iscsi_cls_session *session;
1936 struct iscsi_cls_conn *conn;
1937 struct iscsi_endpoint *ep = NULL;
1939 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1940 *group = ISCSI_NL_GRP_UIP;
1941 else
1942 *group = ISCSI_NL_GRP_ISCSID;
1944 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1945 if (!priv)
1946 return -EINVAL;
1947 transport = priv->iscsi_transport;
1949 if (!try_module_get(transport->owner))
1950 return -EINVAL;
1952 switch (nlh->nlmsg_type) {
1953 case ISCSI_UEVENT_CREATE_SESSION:
1954 err = iscsi_if_create_session(priv, ep, ev,
1955 ev->u.c_session.initial_cmdsn,
1956 ev->u.c_session.cmds_max,
1957 ev->u.c_session.queue_depth);
1958 break;
1959 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
1960 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
1961 if (!ep) {
1962 err = -EINVAL;
1963 break;
1966 err = iscsi_if_create_session(priv, ep, ev,
1967 ev->u.c_bound_session.initial_cmdsn,
1968 ev->u.c_bound_session.cmds_max,
1969 ev->u.c_bound_session.queue_depth);
1970 break;
1971 case ISCSI_UEVENT_DESTROY_SESSION:
1972 session = iscsi_session_lookup(ev->u.d_session.sid);
1973 if (session)
1974 transport->destroy_session(session);
1975 else
1976 err = -EINVAL;
1977 break;
1978 case ISCSI_UEVENT_UNBIND_SESSION:
1979 session = iscsi_session_lookup(ev->u.d_session.sid);
1980 if (session)
1981 scsi_queue_work(iscsi_session_to_shost(session),
1982 &session->unbind_work);
1983 else
1984 err = -EINVAL;
1985 break;
1986 case ISCSI_UEVENT_CREATE_CONN:
1987 err = iscsi_if_create_conn(transport, ev);
1988 break;
1989 case ISCSI_UEVENT_DESTROY_CONN:
1990 err = iscsi_if_destroy_conn(transport, ev);
1991 break;
1992 case ISCSI_UEVENT_BIND_CONN:
1993 session = iscsi_session_lookup(ev->u.b_conn.sid);
1994 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1996 if (conn && conn->ep)
1997 iscsi_if_ep_disconnect(transport, conn->ep->id);
1999 if (!session || !conn) {
2000 err = -EINVAL;
2001 break;
2004 ev->r.retcode = transport->bind_conn(session, conn,
2005 ev->u.b_conn.transport_eph,
2006 ev->u.b_conn.is_leading);
2007 if (ev->r.retcode || !transport->ep_connect)
2008 break;
2010 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
2011 if (ep) {
2012 ep->conn = conn;
2014 mutex_lock(&conn->ep_mutex);
2015 conn->ep = ep;
2016 mutex_unlock(&conn->ep_mutex);
2017 } else
2018 iscsi_cls_conn_printk(KERN_ERR, conn,
2019 "Could not set ep conn "
2020 "binding\n");
2021 break;
2022 case ISCSI_UEVENT_SET_PARAM:
2023 err = iscsi_set_param(transport, ev);
2024 break;
2025 case ISCSI_UEVENT_START_CONN:
2026 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
2027 if (conn)
2028 ev->r.retcode = transport->start_conn(conn);
2029 else
2030 err = -EINVAL;
2031 break;
2032 case ISCSI_UEVENT_STOP_CONN:
2033 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
2034 if (conn)
2035 transport->stop_conn(conn, ev->u.stop_conn.flag);
2036 else
2037 err = -EINVAL;
2038 break;
2039 case ISCSI_UEVENT_SEND_PDU:
2040 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
2041 if (conn)
2042 ev->r.retcode = transport->send_pdu(conn,
2043 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
2044 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
2045 ev->u.send_pdu.data_size);
2046 else
2047 err = -EINVAL;
2048 break;
2049 case ISCSI_UEVENT_GET_STATS:
2050 err = iscsi_if_get_stats(transport, nlh);
2051 break;
2052 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
2053 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
2054 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
2055 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
2056 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
2057 break;
2058 case ISCSI_UEVENT_TGT_DSCVR:
2059 err = iscsi_tgt_dscvr(transport, ev);
2060 break;
2061 case ISCSI_UEVENT_SET_HOST_PARAM:
2062 err = iscsi_set_host_param(transport, ev);
2063 break;
2064 case ISCSI_UEVENT_PATH_UPDATE:
2065 err = iscsi_set_path(transport, ev);
2066 break;
2067 case ISCSI_UEVENT_SET_IFACE_PARAMS:
2068 err = iscsi_set_iface_params(transport, ev);
2069 break;
2070 default:
2071 err = -ENOSYS;
2072 break;
2075 module_put(transport->owner);
2076 return err;
2080 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
2081 * Malformed skbs with wrong lengths or invalid creds are not processed.
2083 static void
2084 iscsi_if_rx(struct sk_buff *skb)
2086 mutex_lock(&rx_queue_mutex);
2087 while (skb->len >= NLMSG_SPACE(0)) {
2088 int err;
2089 uint32_t rlen;
2090 struct nlmsghdr *nlh;
2091 struct iscsi_uevent *ev;
2092 uint32_t group;
2094 nlh = nlmsg_hdr(skb);
2095 if (nlh->nlmsg_len < sizeof(*nlh) ||
2096 skb->len < nlh->nlmsg_len) {
2097 break;
2100 ev = NLMSG_DATA(nlh);
2101 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
2102 if (rlen > skb->len)
2103 rlen = skb->len;
2105 err = iscsi_if_recv_msg(skb, nlh, &group);
2106 if (err) {
2107 ev->type = ISCSI_KEVENT_IF_ERROR;
2108 ev->iferror = err;
2110 do {
2112 * special case for GET_STATS:
2113 * on success - sending reply and stats from
2114 * inside of if_recv_msg(),
2115 * on error - fall through.
2117 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
2118 break;
2119 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
2120 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
2121 } while (err < 0 && err != -ECONNREFUSED);
2122 skb_pull(skb, rlen);
2124 mutex_unlock(&rx_queue_mutex);
2127 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
2128 struct device_attribute dev_attr_##_prefix##_##_name = \
2129 __ATTR(_name,_mode,_show,_store)
2132 * iSCSI connection attrs
2134 #define iscsi_conn_attr_show(param) \
2135 static ssize_t \
2136 show_conn_param_##param(struct device *dev, \
2137 struct device_attribute *attr, char *buf) \
2139 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
2140 struct iscsi_transport *t = conn->transport; \
2141 return t->get_conn_param(conn, param, buf); \
2144 #define iscsi_conn_attr(field, param) \
2145 iscsi_conn_attr_show(param) \
2146 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
2147 NULL);
2149 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
2150 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
2151 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
2152 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
2153 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
2154 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
2155 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
2156 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
2157 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
2158 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
2159 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
2161 #define iscsi_conn_ep_attr_show(param) \
2162 static ssize_t show_conn_ep_param_##param(struct device *dev, \
2163 struct device_attribute *attr,\
2164 char *buf) \
2166 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
2167 struct iscsi_transport *t = conn->transport; \
2168 struct iscsi_endpoint *ep; \
2169 ssize_t rc; \
2171 /* \
2172 * Need to make sure ep_disconnect does not free the LLD's \
2173 * interconnect resources while we are trying to read them. \
2174 */ \
2175 mutex_lock(&conn->ep_mutex); \
2176 ep = conn->ep; \
2177 if (!ep && t->ep_connect) { \
2178 mutex_unlock(&conn->ep_mutex); \
2179 return -ENOTCONN; \
2182 if (ep) \
2183 rc = t->get_ep_param(ep, param, buf); \
2184 else \
2185 rc = t->get_conn_param(conn, param, buf); \
2186 mutex_unlock(&conn->ep_mutex); \
2187 return rc; \
2190 #define iscsi_conn_ep_attr(field, param) \
2191 iscsi_conn_ep_attr_show(param) \
2192 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
2193 show_conn_ep_param_##param, NULL);
2195 iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
2196 iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
2198 static struct attribute *iscsi_conn_attrs[] = {
2199 &dev_attr_conn_max_recv_dlength.attr,
2200 &dev_attr_conn_max_xmit_dlength.attr,
2201 &dev_attr_conn_header_digest.attr,
2202 &dev_attr_conn_data_digest.attr,
2203 &dev_attr_conn_ifmarker.attr,
2204 &dev_attr_conn_ofmarker.attr,
2205 &dev_attr_conn_address.attr,
2206 &dev_attr_conn_port.attr,
2207 &dev_attr_conn_exp_statsn.attr,
2208 &dev_attr_conn_persistent_address.attr,
2209 &dev_attr_conn_persistent_port.attr,
2210 &dev_attr_conn_ping_tmo.attr,
2211 &dev_attr_conn_recv_tmo.attr,
2212 NULL,
2215 static mode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
2216 struct attribute *attr, int i)
2218 struct device *cdev = container_of(kobj, struct device, kobj);
2219 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
2220 struct iscsi_transport *t = conn->transport;
2221 int param;
2223 if (attr == &dev_attr_conn_max_recv_dlength.attr)
2224 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
2225 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
2226 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
2227 else if (attr == &dev_attr_conn_header_digest.attr)
2228 param = ISCSI_PARAM_HDRDGST_EN;
2229 else if (attr == &dev_attr_conn_data_digest.attr)
2230 param = ISCSI_PARAM_DATADGST_EN;
2231 else if (attr == &dev_attr_conn_ifmarker.attr)
2232 param = ISCSI_PARAM_IFMARKER_EN;
2233 else if (attr == &dev_attr_conn_ofmarker.attr)
2234 param = ISCSI_PARAM_OFMARKER_EN;
2235 else if (attr == &dev_attr_conn_address.attr)
2236 param = ISCSI_PARAM_CONN_ADDRESS;
2237 else if (attr == &dev_attr_conn_port.attr)
2238 param = ISCSI_PARAM_CONN_PORT;
2239 else if (attr == &dev_attr_conn_exp_statsn.attr)
2240 param = ISCSI_PARAM_EXP_STATSN;
2241 else if (attr == &dev_attr_conn_persistent_address.attr)
2242 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
2243 else if (attr == &dev_attr_conn_persistent_port.attr)
2244 param = ISCSI_PARAM_PERSISTENT_PORT;
2245 else if (attr == &dev_attr_conn_ping_tmo.attr)
2246 param = ISCSI_PARAM_PING_TMO;
2247 else if (attr == &dev_attr_conn_recv_tmo.attr)
2248 param = ISCSI_PARAM_RECV_TMO;
2249 else {
2250 WARN_ONCE(1, "Invalid conn attr");
2251 return 0;
2254 return t->attr_is_visible(ISCSI_PARAM, param);
2257 static struct attribute_group iscsi_conn_group = {
2258 .attrs = iscsi_conn_attrs,
2259 .is_visible = iscsi_conn_attr_is_visible,
2263 * iSCSI session attrs
2265 #define iscsi_session_attr_show(param, perm) \
2266 static ssize_t \
2267 show_session_param_##param(struct device *dev, \
2268 struct device_attribute *attr, char *buf) \
2270 struct iscsi_cls_session *session = \
2271 iscsi_dev_to_session(dev->parent); \
2272 struct iscsi_transport *t = session->transport; \
2274 if (perm && !capable(CAP_SYS_ADMIN)) \
2275 return -EACCES; \
2276 return t->get_session_param(session, param, buf); \
2279 #define iscsi_session_attr(field, param, perm) \
2280 iscsi_session_attr_show(param, perm) \
2281 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
2282 NULL);
2283 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
2284 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
2285 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
2286 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
2287 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
2288 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
2289 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
2290 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
2291 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
2292 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
2293 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
2294 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
2295 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
2296 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
2297 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
2298 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
2299 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
2300 iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
2301 iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
2302 iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
2303 iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
2305 static ssize_t
2306 show_priv_session_state(struct device *dev, struct device_attribute *attr,
2307 char *buf)
2309 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
2310 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
2312 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
2313 NULL);
2315 #define iscsi_priv_session_attr_show(field, format) \
2316 static ssize_t \
2317 show_priv_session_##field(struct device *dev, \
2318 struct device_attribute *attr, char *buf) \
2320 struct iscsi_cls_session *session = \
2321 iscsi_dev_to_session(dev->parent); \
2322 if (session->field == -1) \
2323 return sprintf(buf, "off\n"); \
2324 return sprintf(buf, format"\n", session->field); \
2327 #define iscsi_priv_session_attr_store(field) \
2328 static ssize_t \
2329 store_priv_session_##field(struct device *dev, \
2330 struct device_attribute *attr, \
2331 const char *buf, size_t count) \
2333 int val; \
2334 char *cp; \
2335 struct iscsi_cls_session *session = \
2336 iscsi_dev_to_session(dev->parent); \
2337 if ((session->state == ISCSI_SESSION_FREE) || \
2338 (session->state == ISCSI_SESSION_FAILED)) \
2339 return -EBUSY; \
2340 if (strncmp(buf, "off", 3) == 0) \
2341 session->field = -1; \
2342 else { \
2343 val = simple_strtoul(buf, &cp, 0); \
2344 if (*cp != '\0' && *cp != '\n') \
2345 return -EINVAL; \
2346 session->field = val; \
2348 return count; \
2351 #define iscsi_priv_session_rw_attr(field, format) \
2352 iscsi_priv_session_attr_show(field, format) \
2353 iscsi_priv_session_attr_store(field) \
2354 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
2355 show_priv_session_##field, \
2356 store_priv_session_##field)
2357 iscsi_priv_session_rw_attr(recovery_tmo, "%d");
2359 static struct attribute *iscsi_session_attrs[] = {
2360 &dev_attr_sess_initial_r2t.attr,
2361 &dev_attr_sess_max_outstanding_r2t.attr,
2362 &dev_attr_sess_immediate_data.attr,
2363 &dev_attr_sess_first_burst_len.attr,
2364 &dev_attr_sess_max_burst_len.attr,
2365 &dev_attr_sess_data_pdu_in_order.attr,
2366 &dev_attr_sess_data_seq_in_order.attr,
2367 &dev_attr_sess_erl.attr,
2368 &dev_attr_sess_targetname.attr,
2369 &dev_attr_sess_tpgt.attr,
2370 &dev_attr_sess_password.attr,
2371 &dev_attr_sess_password_in.attr,
2372 &dev_attr_sess_username.attr,
2373 &dev_attr_sess_username_in.attr,
2374 &dev_attr_sess_fast_abort.attr,
2375 &dev_attr_sess_abort_tmo.attr,
2376 &dev_attr_sess_lu_reset_tmo.attr,
2377 &dev_attr_sess_tgt_reset_tmo.attr,
2378 &dev_attr_sess_ifacename.attr,
2379 &dev_attr_sess_initiatorname.attr,
2380 &dev_attr_sess_targetalias.attr,
2381 &dev_attr_priv_sess_recovery_tmo.attr,
2382 &dev_attr_priv_sess_state.attr,
2383 NULL,
2386 static mode_t iscsi_session_attr_is_visible(struct kobject *kobj,
2387 struct attribute *attr, int i)
2389 struct device *cdev = container_of(kobj, struct device, kobj);
2390 struct iscsi_cls_session *session = transport_class_to_session(cdev);
2391 struct iscsi_transport *t = session->transport;
2392 int param;
2394 if (attr == &dev_attr_sess_initial_r2t.attr)
2395 param = ISCSI_PARAM_INITIAL_R2T_EN;
2396 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
2397 param = ISCSI_PARAM_MAX_R2T;
2398 else if (attr == &dev_attr_sess_immediate_data.attr)
2399 param = ISCSI_PARAM_IMM_DATA_EN;
2400 else if (attr == &dev_attr_sess_first_burst_len.attr)
2401 param = ISCSI_PARAM_FIRST_BURST;
2402 else if (attr == &dev_attr_sess_max_burst_len.attr)
2403 param = ISCSI_PARAM_MAX_BURST;
2404 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
2405 param = ISCSI_PARAM_PDU_INORDER_EN;
2406 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
2407 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
2408 else if (attr == &dev_attr_sess_erl.attr)
2409 param = ISCSI_PARAM_ERL;
2410 else if (attr == &dev_attr_sess_targetname.attr)
2411 param = ISCSI_PARAM_TARGET_NAME;
2412 else if (attr == &dev_attr_sess_tpgt.attr)
2413 param = ISCSI_PARAM_TPGT;
2414 else if (attr == &dev_attr_sess_password.attr)
2415 param = ISCSI_PARAM_USERNAME;
2416 else if (attr == &dev_attr_sess_password_in.attr)
2417 param = ISCSI_PARAM_USERNAME_IN;
2418 else if (attr == &dev_attr_sess_username.attr)
2419 param = ISCSI_PARAM_PASSWORD;
2420 else if (attr == &dev_attr_sess_username_in.attr)
2421 param = ISCSI_PARAM_PASSWORD_IN;
2422 else if (attr == &dev_attr_sess_fast_abort.attr)
2423 param = ISCSI_PARAM_FAST_ABORT;
2424 else if (attr == &dev_attr_sess_abort_tmo.attr)
2425 param = ISCSI_PARAM_ABORT_TMO;
2426 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
2427 param = ISCSI_PARAM_LU_RESET_TMO;
2428 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
2429 param = ISCSI_PARAM_TGT_RESET_TMO;
2430 else if (attr == &dev_attr_sess_ifacename.attr)
2431 param = ISCSI_PARAM_IFACE_NAME;
2432 else if (attr == &dev_attr_sess_initiatorname.attr)
2433 param = ISCSI_PARAM_INITIATOR_NAME;
2434 else if (attr == &dev_attr_sess_targetalias.attr)
2435 param = ISCSI_PARAM_TARGET_ALIAS;
2436 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
2437 return S_IRUGO | S_IWUSR;
2438 else if (attr == &dev_attr_priv_sess_state.attr)
2439 return S_IRUGO;
2440 else {
2441 WARN_ONCE(1, "Invalid session attr");
2442 return 0;
2445 return t->attr_is_visible(ISCSI_PARAM, param);
2448 static struct attribute_group iscsi_session_group = {
2449 .attrs = iscsi_session_attrs,
2450 .is_visible = iscsi_session_attr_is_visible,
2454 * iSCSI host attrs
2456 #define iscsi_host_attr_show(param) \
2457 static ssize_t \
2458 show_host_param_##param(struct device *dev, \
2459 struct device_attribute *attr, char *buf) \
2461 struct Scsi_Host *shost = transport_class_to_shost(dev); \
2462 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
2463 return priv->iscsi_transport->get_host_param(shost, param, buf); \
2466 #define iscsi_host_attr(field, param) \
2467 iscsi_host_attr_show(param) \
2468 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
2469 NULL);
2471 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
2472 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
2473 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
2474 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
2476 static struct attribute *iscsi_host_attrs[] = {
2477 &dev_attr_host_netdev.attr,
2478 &dev_attr_host_hwaddress.attr,
2479 &dev_attr_host_ipaddress.attr,
2480 &dev_attr_host_initiatorname.attr,
2481 NULL,
2484 static mode_t iscsi_host_attr_is_visible(struct kobject *kobj,
2485 struct attribute *attr, int i)
2487 struct device *cdev = container_of(kobj, struct device, kobj);
2488 struct Scsi_Host *shost = transport_class_to_shost(cdev);
2489 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
2490 int param;
2492 if (attr == &dev_attr_host_netdev.attr)
2493 param = ISCSI_HOST_PARAM_NETDEV_NAME;
2494 else if (attr == &dev_attr_host_hwaddress.attr)
2495 param = ISCSI_HOST_PARAM_HWADDRESS;
2496 else if (attr == &dev_attr_host_ipaddress.attr)
2497 param = ISCSI_HOST_PARAM_IPADDRESS;
2498 else if (attr == &dev_attr_host_initiatorname.attr)
2499 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
2500 else {
2501 WARN_ONCE(1, "Invalid host attr");
2502 return 0;
2505 return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
2508 static struct attribute_group iscsi_host_group = {
2509 .attrs = iscsi_host_attrs,
2510 .is_visible = iscsi_host_attr_is_visible,
2513 static int iscsi_session_match(struct attribute_container *cont,
2514 struct device *dev)
2516 struct iscsi_cls_session *session;
2517 struct Scsi_Host *shost;
2518 struct iscsi_internal *priv;
2520 if (!iscsi_is_session_dev(dev))
2521 return 0;
2523 session = iscsi_dev_to_session(dev);
2524 shost = iscsi_session_to_shost(session);
2525 if (!shost->transportt)
2526 return 0;
2528 priv = to_iscsi_internal(shost->transportt);
2529 if (priv->session_cont.ac.class != &iscsi_session_class.class)
2530 return 0;
2532 return &priv->session_cont.ac == cont;
2535 static int iscsi_conn_match(struct attribute_container *cont,
2536 struct device *dev)
2538 struct iscsi_cls_session *session;
2539 struct iscsi_cls_conn *conn;
2540 struct Scsi_Host *shost;
2541 struct iscsi_internal *priv;
2543 if (!iscsi_is_conn_dev(dev))
2544 return 0;
2546 conn = iscsi_dev_to_conn(dev);
2547 session = iscsi_dev_to_session(conn->dev.parent);
2548 shost = iscsi_session_to_shost(session);
2550 if (!shost->transportt)
2551 return 0;
2553 priv = to_iscsi_internal(shost->transportt);
2554 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
2555 return 0;
2557 return &priv->conn_cont.ac == cont;
2560 static int iscsi_host_match(struct attribute_container *cont,
2561 struct device *dev)
2563 struct Scsi_Host *shost;
2564 struct iscsi_internal *priv;
2566 if (!scsi_is_host_device(dev))
2567 return 0;
2569 shost = dev_to_shost(dev);
2570 if (!shost->transportt ||
2571 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
2572 return 0;
2574 priv = to_iscsi_internal(shost->transportt);
2575 return &priv->t.host_attrs.ac == cont;
2578 struct scsi_transport_template *
2579 iscsi_register_transport(struct iscsi_transport *tt)
2581 struct iscsi_internal *priv;
2582 unsigned long flags;
2583 int err;
2585 BUG_ON(!tt);
2587 priv = iscsi_if_transport_lookup(tt);
2588 if (priv)
2589 return NULL;
2591 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2592 if (!priv)
2593 return NULL;
2594 INIT_LIST_HEAD(&priv->list);
2595 priv->iscsi_transport = tt;
2596 priv->t.user_scan = iscsi_user_scan;
2597 priv->t.create_work_queue = 1;
2599 priv->dev.class = &iscsi_transport_class;
2600 dev_set_name(&priv->dev, "%s", tt->name);
2601 err = device_register(&priv->dev);
2602 if (err)
2603 goto free_priv;
2605 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
2606 if (err)
2607 goto unregister_dev;
2609 /* host parameters */
2610 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
2611 priv->t.host_attrs.ac.match = iscsi_host_match;
2612 priv->t.host_attrs.ac.grp = &iscsi_host_group;
2613 priv->t.host_size = sizeof(struct iscsi_cls_host);
2614 transport_container_register(&priv->t.host_attrs);
2616 /* connection parameters */
2617 priv->conn_cont.ac.class = &iscsi_connection_class.class;
2618 priv->conn_cont.ac.match = iscsi_conn_match;
2619 priv->conn_cont.ac.grp = &iscsi_conn_group;
2620 transport_container_register(&priv->conn_cont);
2622 /* session parameters */
2623 priv->session_cont.ac.class = &iscsi_session_class.class;
2624 priv->session_cont.ac.match = iscsi_session_match;
2625 priv->session_cont.ac.grp = &iscsi_session_group;
2626 transport_container_register(&priv->session_cont);
2628 spin_lock_irqsave(&iscsi_transport_lock, flags);
2629 list_add(&priv->list, &iscsi_transports);
2630 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2632 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
2633 return &priv->t;
2635 unregister_dev:
2636 device_unregister(&priv->dev);
2637 return NULL;
2638 free_priv:
2639 kfree(priv);
2640 return NULL;
2642 EXPORT_SYMBOL_GPL(iscsi_register_transport);
2644 int iscsi_unregister_transport(struct iscsi_transport *tt)
2646 struct iscsi_internal *priv;
2647 unsigned long flags;
2649 BUG_ON(!tt);
2651 mutex_lock(&rx_queue_mutex);
2653 priv = iscsi_if_transport_lookup(tt);
2654 BUG_ON (!priv);
2656 spin_lock_irqsave(&iscsi_transport_lock, flags);
2657 list_del(&priv->list);
2658 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2660 transport_container_unregister(&priv->conn_cont);
2661 transport_container_unregister(&priv->session_cont);
2662 transport_container_unregister(&priv->t.host_attrs);
2664 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
2665 device_unregister(&priv->dev);
2666 mutex_unlock(&rx_queue_mutex);
2668 return 0;
2670 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
2672 static __init int iscsi_transport_init(void)
2674 int err;
2676 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
2677 ISCSI_TRANSPORT_VERSION);
2679 atomic_set(&iscsi_session_nr, 0);
2681 err = class_register(&iscsi_transport_class);
2682 if (err)
2683 return err;
2685 err = class_register(&iscsi_endpoint_class);
2686 if (err)
2687 goto unregister_transport_class;
2689 err = class_register(&iscsi_iface_class);
2690 if (err)
2691 goto unregister_endpoint_class;
2693 err = transport_class_register(&iscsi_host_class);
2694 if (err)
2695 goto unregister_iface_class;
2697 err = transport_class_register(&iscsi_connection_class);
2698 if (err)
2699 goto unregister_host_class;
2701 err = transport_class_register(&iscsi_session_class);
2702 if (err)
2703 goto unregister_conn_class;
2705 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2706 NULL, THIS_MODULE);
2707 if (!nls) {
2708 err = -ENOBUFS;
2709 goto unregister_session_class;
2712 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2713 if (!iscsi_eh_timer_workq)
2714 goto release_nls;
2716 return 0;
2718 release_nls:
2719 netlink_kernel_release(nls);
2720 unregister_session_class:
2721 transport_class_unregister(&iscsi_session_class);
2722 unregister_conn_class:
2723 transport_class_unregister(&iscsi_connection_class);
2724 unregister_host_class:
2725 transport_class_unregister(&iscsi_host_class);
2726 unregister_iface_class:
2727 class_unregister(&iscsi_iface_class);
2728 unregister_endpoint_class:
2729 class_unregister(&iscsi_endpoint_class);
2730 unregister_transport_class:
2731 class_unregister(&iscsi_transport_class);
2732 return err;
2735 static void __exit iscsi_transport_exit(void)
2737 destroy_workqueue(iscsi_eh_timer_workq);
2738 netlink_kernel_release(nls);
2739 transport_class_unregister(&iscsi_connection_class);
2740 transport_class_unregister(&iscsi_session_class);
2741 transport_class_unregister(&iscsi_host_class);
2742 class_unregister(&iscsi_endpoint_class);
2743 class_unregister(&iscsi_iface_class);
2744 class_unregister(&iscsi_transport_class);
2747 module_init(iscsi_transport_init);
2748 module_exit(iscsi_transport_exit);
2750 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2751 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2752 "Alex Aizman <itn780@yahoo.com>");
2753 MODULE_DESCRIPTION("iSCSI Transport Interface");
2754 MODULE_LICENSE("GPL");
2755 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
2756 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);