blob: 75d7f6e93b67ff1b04b3f391bf003c21e1b786a8 [file] [log] [blame]
Alex Aizman0896b752005-08-04 19:33:07 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * iSCSI transport class definitions
3 *
4 * Copyright (C) IBM Corporation, 2004
Alex Aizman0896b752005-08-04 19:33:07 -07005 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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.
13 *
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.
18 *
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.
22 */
23#include <linux/module.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010024#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Alex Aizman0896b752005-08-04 19:33:07 -070026#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <scsi/scsi.h>
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_transport.h>
31#include <scsi/scsi_transport_iscsi.h>
Alex Aizman0896b752005-08-04 19:33:07 -070032#include <scsi/iscsi_if.h>
Mike Christiec01be6d2010-07-22 16:59:49 +053033#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Mike Christied8196ed2007-05-30 12:57:25 -050035#define ISCSI_HOST_ATTRS 4
Mike Christied82ff9be2008-05-21 15:54:13 -050036
Mike Christie4c2133c2008-06-16 10:11:34 -050037#define ISCSI_TRANSPORT_VERSION "2.0-870"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Mike Christie632248a2009-08-20 15:11:01 -050039static int dbg_session;
40module_param_named(debug_session, dbg_session, int,
41 S_IRUGO | S_IWUSR);
42MODULE_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.");
46
47static int dbg_conn;
48module_param_named(debug_conn, dbg_conn, int,
49 S_IRUGO | S_IWUSR);
50MODULE_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.");
54
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);
62
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);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071struct iscsi_internal {
72 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070073 struct iscsi_transport *iscsi_transport;
74 struct list_head list;
Tony Jonesee959b02008-02-22 00:13:36 +010075 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050076
Tony Jonesee959b02008-02-22 00:13:36 +010077 struct device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070078 struct transport_container conn_cont;
Alex Aizman0896b752005-08-04 19:33:07 -070079 struct transport_container session_cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
Mike Christie41be1442007-02-28 17:32:18 -060082static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060083static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050084
Alex Aizman0896b752005-08-04 19:33:07 -070085/*
86 * list of registered transports and lock that must
87 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010088 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070089 */
90static LIST_HEAD(iscsi_transports);
91static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Alex Aizman0896b752005-08-04 19:33:07 -070093#define to_iscsi_internal(tmpl) \
94 container_of(tmpl, struct iscsi_internal, t)
95
Tony Jonesee959b02008-02-22 00:13:36 +010096#define dev_to_iscsi_internal(_dev) \
97 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -070098
Tony Jonesee959b02008-02-22 00:13:36 +010099static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700100{
Tony Jonesee959b02008-02-22 00:13:36 +0100101 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -0700102 kfree(priv);
103}
104
105/*
106 * iscsi_transport_class represents the iscsi_transports that are
107 * registered.
108 */
109static struct class iscsi_transport_class = {
110 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +0100111 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -0700112};
113
114static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100115show_transport_handle(struct device *dev, struct device_attribute *attr,
116 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -0700117{
Tony Jonesee959b02008-02-22 00:13:36 +0100118 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Mike Christie762e2bf2005-09-12 21:01:46 -0500119 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -0700120}
Tony Jonesee959b02008-02-22 00:13:36 +0100121static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700122
123#define show_transport_attr(name, format) \
124static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100125show_transport_##name(struct device *dev, \
126 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -0700127{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100128 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Alex Aizman0896b752005-08-04 19:33:07 -0700129 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
130} \
Tony Jonesee959b02008-02-22 00:13:36 +0100131static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700132
133show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700134
135static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100136 &dev_attr_handle.attr,
137 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700138 NULL,
139};
140
141static struct attribute_group iscsi_transport_group = {
142 .attrs = iscsi_transport_attrs,
143};
144
Mike Christied82ff9be2008-05-21 15:54:13 -0500145/*
146 * iSCSI endpoint attrs
147 */
148#define iscsi_dev_to_endpoint(_dev) \
149 container_of(_dev, struct iscsi_endpoint, dev)
150
151#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
152struct device_attribute dev_attr_##_prefix##_##_name = \
153 __ATTR(_name,_mode,_show,_store)
154
155static void iscsi_endpoint_release(struct device *dev)
156{
157 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
158 kfree(ep);
159}
160
161static struct class iscsi_endpoint_class = {
162 .name = "iscsi_endpoint",
163 .dev_release = iscsi_endpoint_release,
164};
165
166static ssize_t
167show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
168{
169 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500170 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500171}
172static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
173
174static struct attribute *iscsi_endpoint_attrs[] = {
175 &dev_attr_ep_handle.attr,
176 NULL,
177};
178
179static struct attribute_group iscsi_endpoint_group = {
180 .attrs = iscsi_endpoint_attrs,
181};
182
183#define ISCSI_MAX_EPID -1
184
185static int iscsi_match_epid(struct device *dev, void *data)
186{
187 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500188 uint64_t *epid = (uint64_t *) data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500189
190 return *epid == ep->id;
191}
192
193struct iscsi_endpoint *
194iscsi_create_endpoint(int dd_size)
195{
196 struct device *dev;
197 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500198 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500199 int err;
200
201 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400202 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500203 iscsi_match_epid);
204 if (!dev)
205 break;
206 }
207 if (id == ISCSI_MAX_EPID) {
208 printk(KERN_ERR "Too many connections. Max supported %u\n",
209 ISCSI_MAX_EPID - 1);
210 return NULL;
211 }
212
213 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
214 if (!ep)
215 return NULL;
216
217 ep->id = id;
218 ep->dev.class = &iscsi_endpoint_class;
Kay Sievers71610f52008-12-03 22:41:36 +0100219 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500220 err = device_register(&ep->dev);
221 if (err)
222 goto free_ep;
223
224 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
225 if (err)
226 goto unregister_dev;
227
228 if (dd_size)
229 ep->dd_data = &ep[1];
230 return ep;
231
232unregister_dev:
233 device_unregister(&ep->dev);
234 return NULL;
235
236free_ep:
237 kfree(ep);
238 return NULL;
239}
240EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
241
242void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
243{
244 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
245 device_unregister(&ep->dev);
246}
247EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
248
249struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
250{
Mike Christief80f8682008-06-16 10:11:35 -0500251 struct iscsi_endpoint *ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500252 struct device *dev;
253
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400254 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500255 iscsi_match_epid);
256 if (!dev)
257 return NULL;
258
Mike Christief80f8682008-06-16 10:11:35 -0500259 ep = iscsi_dev_to_endpoint(dev);
260 /*
261 * we can drop this now because the interface will prevent
262 * removals and lookups from racing.
263 */
264 put_device(dev);
265 return ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500266}
267EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
268
Mike Christie8d079132011-07-25 13:48:40 -0500269/*
270 * Interface to display network param to sysfs
271 */
272
273static void iscsi_iface_release(struct device *dev)
274{
275 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
276 struct device *parent = iface->dev.parent;
277
278 kfree(iface);
279 put_device(parent);
280}
281
282
283static struct class iscsi_iface_class = {
284 .name = "iscsi_iface",
285 .dev_release = iscsi_iface_release,
286};
287
288#define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
289struct device_attribute dev_attr_##_prefix##_##_name = \
290 __ATTR(_name, _mode, _show, _store)
291
292/* iface attrs show */
293#define iscsi_iface_attr_show(type, name, param_type, param) \
294static ssize_t \
295show_##type##_##name(struct device *dev, struct device_attribute *attr, \
296 char *buf) \
297{ \
298 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
299 struct iscsi_transport *t = iface->transport; \
300 return t->get_iface_param(iface, param_type, param, buf); \
301} \
302
303#define iscsi_iface_net_attr(type, name, param) \
304 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
305static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
306
307/* generic read only ipvi4 attribute */
308iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
309iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
310iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
311iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
312
313/* generic read only ipv6 attribute */
314iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
315iscsi_iface_net_attr(ipv6_iface, link_local_addr, ISCSI_NET_PARAM_IPV6_LINKLOCAL);
316iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
317iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
318 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
319iscsi_iface_net_attr(ipv6_iface, linklocal_autocfg,
320 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
321
322/* common read only iface attribute */
323iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
324iscsi_iface_net_attr(iface, vlan, ISCSI_NET_PARAM_VLAN_ID);
325
326static mode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
327 struct attribute *attr, int i)
328{
329 struct device *dev = container_of(kobj, struct device, kobj);
330 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
331 struct iscsi_transport *t = iface->transport;
332
333 if (attr == &dev_attr_iface_enabled.attr)
334 return (t->iface_param_mask & ISCSI_NET_IFACE_ENABLE) ?
335 S_IRUGO : 0;
336 else if (attr == &dev_attr_iface_vlan.attr)
337 return (t->iface_param_mask & ISCSI_NET_VLAN_ID) ? S_IRUGO : 0;
338
339 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
340 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
341 return (t->iface_param_mask & ISCSI_NET_IPV4_ADDR) ?
342 S_IRUGO : 0;
343 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
344 return (t->iface_param_mask & ISCSI_NET_IPV4_GW) ?
345 S_IRUGO : 0;
346 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
347 return (t->iface_param_mask & ISCSI_NET_IPV4_SUBNET) ?
348 S_IRUGO : 0;
349 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
350 return (t->iface_param_mask & ISCSI_NET_IPV4_BOOTPROTO) ?
351 S_IRUGO : 0;
352 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
353 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
354 return (t->iface_param_mask & ISCSI_NET_IPV6_ADDR) ?
355 S_IRUGO : 0;
356 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
357 return (t->iface_param_mask & ISCSI_NET_IPV6_LINKLOCAL) ?
358 S_IRUGO : 0;
359 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
360 return (t->iface_param_mask & ISCSI_NET_IPV6_ROUTER) ?
361 S_IRUGO : 0;
362 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
363 return (t->iface_param_mask & ISCSI_NET_IPV6_ADDR_AUTOCFG) ?
364 S_IRUGO : 0;
365 else if (attr == &dev_attr_ipv6_iface_linklocal_autocfg.attr)
366 return (t->iface_param_mask & ISCSI_NET_IPV6_LINKLOCAL_AUTOCFG) ?
367 S_IRUGO : 0;
368 }
369
370 return 0;
371}
372
373static struct attribute *iscsi_iface_attrs[] = {
374 &dev_attr_iface_enabled.attr,
375 &dev_attr_iface_vlan.attr,
376 &dev_attr_ipv4_iface_ipaddress.attr,
377 &dev_attr_ipv4_iface_gateway.attr,
378 &dev_attr_ipv4_iface_subnet.attr,
379 &dev_attr_ipv4_iface_bootproto.attr,
380 &dev_attr_ipv6_iface_ipaddress.attr,
381 &dev_attr_ipv6_iface_link_local_addr.attr,
382 &dev_attr_ipv6_iface_router_addr.attr,
383 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
384 &dev_attr_ipv6_iface_linklocal_autocfg.attr,
385 NULL,
386};
387
388static struct attribute_group iscsi_iface_group = {
389 .attrs = iscsi_iface_attrs,
390 .is_visible = iscsi_iface_attr_is_visible,
391};
392
393struct iscsi_iface *
394iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
395 uint32_t iface_type, uint32_t iface_num, int dd_size)
396{
397 struct iscsi_iface *iface;
398 int err;
399
400 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
401 if (!iface)
402 return NULL;
403
404 iface->transport = transport;
405 iface->iface_type = iface_type;
406 iface->iface_num = iface_num;
407 iface->dev.release = iscsi_iface_release;
408 iface->dev.class = &iscsi_iface_class;
409 /* parent reference released in iscsi_iface_release */
410 iface->dev.parent = get_device(&shost->shost_gendev);
411 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
412 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
413 iface_num);
414 else
415 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
416 iface_num);
417
418 err = device_register(&iface->dev);
419 if (err)
420 goto free_iface;
421
422 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
423 if (err)
424 goto unreg_iface;
425
426 if (dd_size)
427 iface->dd_data = &iface[1];
428 return iface;
429
430unreg_iface:
431 device_unregister(&iface->dev);
432 return NULL;
433
434free_iface:
435 put_device(iface->dev.parent);
436 kfree(iface);
437 return NULL;
438}
439EXPORT_SYMBOL_GPL(iscsi_create_iface);
440
441void iscsi_destroy_iface(struct iscsi_iface *iface)
442{
443 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
444 device_unregister(&iface->dev);
445}
446EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
447
Mike Christie30a6c652006-04-06 21:13:39 -0500448static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100449 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500450{
451 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500452 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500453
454 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -0600455 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -0500456 mutex_init(&ihost->mutex);
Mike Christie30a6c652006-04-06 21:13:39 -0500457 return 0;
458}
459
460static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
461 "iscsi_host",
462 iscsi_setup_host,
Mike Christie06d25af2009-03-05 14:46:02 -0600463 NULL,
Mike Christie30a6c652006-04-06 21:13:39 -0500464 NULL);
465
Alex Aizman0896b752005-08-04 19:33:07 -0700466static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
467 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 NULL,
469 NULL,
470 NULL);
471
Alex Aizman0896b752005-08-04 19:33:07 -0700472static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
473 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 NULL,
475 NULL,
476 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700477
478static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100479static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700480
Mike Christie7b7232f2006-02-01 21:06:49 -0600481static LIST_HEAD(sesslist);
482static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700483static LIST_HEAD(connlist);
484static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Mike Christieb5c7a122006-04-06 21:13:33 -0500486static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
487{
488 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
489 return sess->sid;
490}
491
492/*
493 * Returns the matching session to a given sid
494 */
495static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600496{
497 unsigned long flags;
498 struct iscsi_cls_session *sess;
499
500 spin_lock_irqsave(&sesslock, flags);
501 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500502 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600503 spin_unlock_irqrestore(&sesslock, flags);
504 return sess;
505 }
506 }
507 spin_unlock_irqrestore(&sesslock, flags);
508 return NULL;
509}
510
Mike Christieb5c7a122006-04-06 21:13:33 -0500511/*
512 * Returns the matching connection to a given sid / cid tuple
513 */
514static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600515{
516 unsigned long flags;
517 struct iscsi_cls_conn *conn;
518
519 spin_lock_irqsave(&connlock, flags);
520 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500521 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600522 spin_unlock_irqrestore(&connlock, flags);
523 return conn;
524 }
525 }
526 spin_unlock_irqrestore(&connlock, flags);
527 return NULL;
528}
529
Mike Christie7b8631b2006-01-13 18:05:50 -0600530/*
531 * The following functions can be used by LLDs that allocate
532 * their own scsi_hosts or by software iscsi LLDs
533 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600534static struct {
535 int value;
536 char *name;
537} iscsi_session_state_names[] = {
538 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
539 { ISCSI_SESSION_FAILED, "FAILED" },
540 { ISCSI_SESSION_FREE, "FREE" },
541};
542
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200543static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600544{
545 int i;
546 char *name = NULL;
547
548 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
549 if (iscsi_session_state_names[i].value == state) {
550 name = iscsi_session_state_names[i].name;
551 break;
552 }
553 }
554 return name;
555}
556
557int iscsi_session_chkready(struct iscsi_cls_session *session)
558{
559 unsigned long flags;
560 int err;
561
562 spin_lock_irqsave(&session->lock, flags);
563 switch (session->state) {
564 case ISCSI_SESSION_LOGGED_IN:
565 err = 0;
566 break;
567 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -0500568 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600569 break;
570 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500571 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600572 break;
573 default:
574 err = DID_NO_CONNECT << 16;
575 break;
576 }
577 spin_unlock_irqrestore(&session->lock, flags);
578 return err;
579}
580EXPORT_SYMBOL_GPL(iscsi_session_chkready);
581
Mike Christie7b8631b2006-01-13 18:05:50 -0600582static void iscsi_session_release(struct device *dev)
583{
584 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600585 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Mike Christie7b8631b2006-01-13 18:05:50 -0600587 shost = iscsi_session_to_shost(session);
588 scsi_host_put(shost);
Mike Christie632248a2009-08-20 15:11:01 -0500589 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600590 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600591}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Mike Christie7b8631b2006-01-13 18:05:50 -0600593static int iscsi_is_session_dev(const struct device *dev)
594{
595 return dev->release == iscsi_session_release;
596}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Mike Christiea4804cd2008-05-21 15:54:00 -0500598static int iscsi_iter_session_fn(struct device *dev, void *data)
599{
600 void (* fn) (struct iscsi_cls_session *) = data;
601
602 if (!iscsi_is_session_dev(dev))
603 return 0;
604 fn(iscsi_dev_to_session(dev));
605 return 0;
606}
607
608void iscsi_host_for_each_session(struct Scsi_Host *shost,
609 void (*fn)(struct iscsi_cls_session *))
610{
611 device_for_each_child(&shost->shost_gendev, fn,
612 iscsi_iter_session_fn);
613}
614EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
615
Mike Christie8aae18a2008-01-31 13:36:48 -0600616/**
617 * iscsi_scan_finished - helper to report when running scans are done
618 * @shost: scsi host
619 * @time: scan run time
620 *
621 * This function can be used by drives like qla4xxx to report to the scsi
622 * layer when the scans it kicked off at module load time are done.
623 */
624int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
625{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500626 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600627 /*
628 * qla4xxx will have kicked off some session unblocks before calling
629 * scsi_scan_host, so just wait for them to complete.
630 */
631 return !atomic_read(&ihost->nr_scans);
632}
633EXPORT_SYMBOL_GPL(iscsi_scan_finished);
634
Mike Christie79706342008-05-21 15:54:12 -0500635struct iscsi_scan_data {
636 unsigned int channel;
637 unsigned int id;
638 unsigned int lun;
639};
640
641static int iscsi_user_scan_session(struct device *dev, void *data)
642{
643 struct iscsi_scan_data *scan_data = data;
644 struct iscsi_cls_session *session;
645 struct Scsi_Host *shost;
646 struct iscsi_cls_host *ihost;
647 unsigned long flags;
648 unsigned int id;
649
650 if (!iscsi_is_session_dev(dev))
651 return 0;
652
653 session = iscsi_dev_to_session(dev);
Mike Christie632248a2009-08-20 15:11:01 -0500654
655 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
656
Mike Christie79706342008-05-21 15:54:12 -0500657 shost = iscsi_session_to_shost(session);
658 ihost = shost->shost_data;
659
660 mutex_lock(&ihost->mutex);
661 spin_lock_irqsave(&session->lock, flags);
662 if (session->state != ISCSI_SESSION_LOGGED_IN) {
663 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie632248a2009-08-20 15:11:01 -0500664 goto user_scan_exit;
Mike Christie79706342008-05-21 15:54:12 -0500665 }
666 id = session->target_id;
667 spin_unlock_irqrestore(&session->lock, flags);
668
669 if (id != ISCSI_MAX_TARGET) {
670 if ((scan_data->channel == SCAN_WILD_CARD ||
671 scan_data->channel == 0) &&
672 (scan_data->id == SCAN_WILD_CARD ||
673 scan_data->id == id))
674 scsi_scan_target(&session->dev, 0, id,
675 scan_data->lun, 1);
676 }
Mike Christie632248a2009-08-20 15:11:01 -0500677
678user_scan_exit:
Mike Christie79706342008-05-21 15:54:12 -0500679 mutex_unlock(&ihost->mutex);
Mike Christie632248a2009-08-20 15:11:01 -0500680 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
Mike Christie79706342008-05-21 15:54:12 -0500681 return 0;
682}
683
Mike Christie30a6c652006-04-06 21:13:39 -0500684static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
685 uint id, uint lun)
686{
Mike Christie79706342008-05-21 15:54:12 -0500687 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500688
Mike Christie79706342008-05-21 15:54:12 -0500689 scan_data.channel = channel;
690 scan_data.id = id;
691 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -0500692
Mike Christie79706342008-05-21 15:54:12 -0500693 return device_for_each_child(&shost->shost_gendev, &scan_data,
694 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -0500695}
696
Mike Christiebd976f62008-01-31 13:36:46 -0600697static void iscsi_scan_session(struct work_struct *work)
698{
699 struct iscsi_cls_session *session =
700 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600701 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500702 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500703 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600704
Mike Christie79706342008-05-21 15:54:12 -0500705 scan_data.channel = 0;
706 scan_data.id = SCAN_WILD_CARD;
707 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -0600708
Mike Christie79706342008-05-21 15:54:12 -0500709 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -0600710 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600711}
712
Mike Christiec01be6d2010-07-22 16:59:49 +0530713/**
714 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
Randy Dunlape6d4ef42010-08-14 13:05:41 -0700715 * @cmd: scsi cmd passed to scsi eh handler
Mike Christiec01be6d2010-07-22 16:59:49 +0530716 *
717 * If the session is down this function will wait for the recovery
718 * timer to fire or for the session to be logged back in. If the
719 * recovery timer fires then FAST_IO_FAIL is returned. The caller
720 * should pass this error value to the scsi eh.
721 */
722int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
723{
724 struct iscsi_cls_session *session =
725 starget_to_session(scsi_target(cmd->device));
726 unsigned long flags;
727 int ret = 0;
728
729 spin_lock_irqsave(&session->lock, flags);
730 while (session->state != ISCSI_SESSION_LOGGED_IN) {
731 if (session->state == ISCSI_SESSION_FREE) {
732 ret = FAST_IO_FAIL;
733 break;
734 }
735 spin_unlock_irqrestore(&session->lock, flags);
736 msleep(1000);
737 spin_lock_irqsave(&session->lock, flags);
738 }
739 spin_unlock_irqrestore(&session->lock, flags);
740 return ret;
741}
742EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
743
David Howellsc4028952006-11-22 14:57:56 +0000744static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500745{
David Howellsc4028952006-11-22 14:57:56 +0000746 struct iscsi_cls_session *session =
747 container_of(work, struct iscsi_cls_session,
748 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600749 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500750
Mike Christie322d7392008-01-31 13:36:52 -0600751 iscsi_cls_session_printk(KERN_INFO, session,
752 "session recovery timed out after %d secs\n",
753 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500754
Mike Christie6eabafb2008-01-31 13:36:43 -0600755 spin_lock_irqsave(&session->lock, flags);
756 switch (session->state) {
757 case ISCSI_SESSION_FAILED:
758 session->state = ISCSI_SESSION_FREE;
759 break;
760 case ISCSI_SESSION_LOGGED_IN:
761 case ISCSI_SESSION_FREE:
762 /* we raced with the unblock's flush */
763 spin_unlock_irqrestore(&session->lock, flags);
764 return;
765 }
766 spin_unlock_irqrestore(&session->lock, flags);
767
Mike Christie30a6c652006-04-06 21:13:39 -0500768 if (session->transport->session_recovery_timedout)
769 session->transport->session_recovery_timedout(session);
770
Mike Christie632248a2009-08-20 15:11:01 -0500771 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500772 scsi_target_unblock(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500773 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500774}
775
Mike Christie45ab33b2008-03-04 13:26:55 -0600776static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500777{
Mike Christie45ab33b2008-03-04 13:26:55 -0600778 struct iscsi_cls_session *session =
779 container_of(work, struct iscsi_cls_session,
780 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600781 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500782 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600783 unsigned long flags;
784
Mike Christie632248a2009-08-20 15:11:01 -0500785 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
Mike Christie45ab33b2008-03-04 13:26:55 -0600786 /*
787 * The recovery and unblock work get run from the same workqueue,
788 * so try to cancel it if it was going to run after this unblock.
789 */
790 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600791 spin_lock_irqsave(&session->lock, flags);
792 session->state = ISCSI_SESSION_LOGGED_IN;
793 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600794 /* start IO */
795 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600796 /*
797 * Only do kernel scanning if the driver is properly hooked into
798 * the async scanning code (drivers like iscsi_tcp do login and
799 * scanning from userspace).
800 */
801 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -0600802 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -0600803 atomic_inc(&ihost->nr_scans);
804 }
Mike Christie632248a2009-08-20 15:11:01 -0500805 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600806}
Mike Christie45ab33b2008-03-04 13:26:55 -0600807
808/**
809 * iscsi_unblock_session - set a session as logged in and start IO.
810 * @session: iscsi session
811 *
812 * Mark a session as ready to accept IO.
813 */
814void iscsi_unblock_session(struct iscsi_cls_session *session)
815{
816 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
817 /*
818 * make sure all the events have completed before tell the driver
819 * it is safe
820 */
821 flush_workqueue(iscsi_eh_timer_workq);
822}
Mike Christie30a6c652006-04-06 21:13:39 -0500823EXPORT_SYMBOL_GPL(iscsi_unblock_session);
824
Mike Christie45ab33b2008-03-04 13:26:55 -0600825static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500826{
Mike Christie45ab33b2008-03-04 13:26:55 -0600827 struct iscsi_cls_session *session =
828 container_of(work, struct iscsi_cls_session,
829 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600830 unsigned long flags;
831
Mike Christie632248a2009-08-20 15:11:01 -0500832 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600833 spin_lock_irqsave(&session->lock, flags);
834 session->state = ISCSI_SESSION_FAILED;
835 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500836 scsi_target_block(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500837 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
Mike Christiefdd46dc2009-11-11 16:34:34 -0600838 if (session->recovery_tmo >= 0)
839 queue_delayed_work(iscsi_eh_timer_workq,
840 &session->recovery_work,
841 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500842}
Mike Christie45ab33b2008-03-04 13:26:55 -0600843
844void iscsi_block_session(struct iscsi_cls_session *session)
845{
846 queue_work(iscsi_eh_timer_workq, &session->block_work);
847}
Mike Christie30a6c652006-04-06 21:13:39 -0500848EXPORT_SYMBOL_GPL(iscsi_block_session);
849
Mike Christie26974782007-12-13 12:43:29 -0600850static void __iscsi_unbind_session(struct work_struct *work)
851{
852 struct iscsi_cls_session *session =
853 container_of(work, struct iscsi_cls_session,
854 unbind_work);
855 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500856 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500857 unsigned long flags;
Mike Christie26974782007-12-13 12:43:29 -0600858
Mike Christie632248a2009-08-20 15:11:01 -0500859 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
860
Mike Christie26974782007-12-13 12:43:29 -0600861 /* Prevent new scans and make sure scanning is not in progress */
862 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -0500863 spin_lock_irqsave(&session->lock, flags);
864 if (session->target_id == ISCSI_MAX_TARGET) {
865 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600866 mutex_unlock(&ihost->mutex);
867 return;
868 }
Mike Christie79706342008-05-21 15:54:12 -0500869 session->target_id = ISCSI_MAX_TARGET;
870 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600871 mutex_unlock(&ihost->mutex);
872
873 scsi_remove_target(&session->dev);
874 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -0500875 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
Mike Christie26974782007-12-13 12:43:29 -0600876}
877
Mike Christie7b8631b2006-01-13 18:05:50 -0600878struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500879iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
880 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600881{
882 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Mike Christie5d91e202008-05-21 15:54:01 -0500884 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -0500885 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600886 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500887 return NULL;
888
Mike Christie7b8631b2006-01-13 18:05:50 -0600889 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500890 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -0600891 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +0000892 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500893 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -0600894 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
895 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -0600896 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -0600897 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -0600898 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -0600899
Mike Christie6a8a0d32006-06-28 12:00:31 -0500900 /* this is released in the dev's release function */
901 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500902 session->dev.parent = &shost->shost_gendev;
903 session->dev.release = iscsi_session_release;
904 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -0500905 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -0500906 session->dd_data = &session[1];
Mike Christie632248a2009-08-20 15:11:01 -0500907
908 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500909 return session;
910}
911EXPORT_SYMBOL_GPL(iscsi_alloc_session);
912
Mike Christie79706342008-05-21 15:54:12 -0500913static int iscsi_get_next_target_id(struct device *dev, void *data)
914{
915 struct iscsi_cls_session *session;
916 unsigned long flags;
917 int err = 0;
918
919 if (!iscsi_is_session_dev(dev))
920 return 0;
921
922 session = iscsi_dev_to_session(dev);
923 spin_lock_irqsave(&session->lock, flags);
924 if (*((unsigned int *) data) == session->target_id)
925 err = -EEXIST;
926 spin_unlock_irqrestore(&session->lock, flags);
927 return err;
928}
929
Mike Christie6a8a0d32006-06-28 12:00:31 -0500930int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500931{
932 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500933 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600934 unsigned long flags;
Mike Christie79706342008-05-21 15:54:12 -0500935 unsigned int id = target_id;
Mike Christie8434aa82006-06-28 12:00:30 -0500936 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500937
Mike Christie30a6c652006-04-06 21:13:39 -0500938 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600939 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -0500940
941 if (id == ISCSI_MAX_TARGET) {
942 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
943 err = device_for_each_child(&shost->shost_gendev, &id,
944 iscsi_get_next_target_id);
945 if (!err)
946 break;
947 }
948
949 if (id == ISCSI_MAX_TARGET) {
950 iscsi_cls_session_printk(KERN_ERR, session,
951 "Too many iscsi targets. Max "
952 "number of targets is %d.\n",
953 ISCSI_MAX_TARGET - 1);
Jaswinder Singh Rajput24add1c2009-06-20 13:29:21 +0530954 err = -EOVERFLOW;
Mike Christie79706342008-05-21 15:54:12 -0500955 goto release_host;
956 }
957 }
958 session->target_id = id;
Mike Christie30a6c652006-04-06 21:13:39 -0500959
Kay Sievers71610f52008-12-03 22:41:36 +0100960 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500961 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600962 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600963 iscsi_cls_session_printk(KERN_ERR, session,
964 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500965 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600966 }
967 transport_register_device(&session->dev);
968
Mike Christie26974782007-12-13 12:43:29 -0600969 spin_lock_irqsave(&sesslock, flags);
970 list_add(&session->sess_list, &sesslist);
971 spin_unlock_irqrestore(&sesslock, flags);
972
Mike Christie26974782007-12-13 12:43:29 -0600973 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -0500974 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500975 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500976
Mike Christie8434aa82006-06-28 12:00:30 -0500977release_host:
978 scsi_host_put(shost);
979 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600980}
Mike Christie8434aa82006-06-28 12:00:30 -0500981EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600982
983/**
Mike Christie8434aa82006-06-28 12:00:30 -0500984 * iscsi_create_session - create iscsi class session
985 * @shost: scsi host
986 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -0500987 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -0500988 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600989 *
Mike Christie8434aa82006-06-28 12:00:30 -0500990 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500991 */
Mike Christie8434aa82006-06-28 12:00:30 -0500992struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500993iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
994 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500995{
996 struct iscsi_cls_session *session;
997
Mike Christie5d91e202008-05-21 15:54:01 -0500998 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -0500999 if (!session)
1000 return NULL;
1001
Mike Christie6a8a0d32006-06-28 12:00:31 -05001002 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -05001003 iscsi_free_session(session);
1004 return NULL;
1005 }
1006 return session;
1007}
1008EXPORT_SYMBOL_GPL(iscsi_create_session);
1009
Mike Christie26974782007-12-13 12:43:29 -06001010static void iscsi_conn_release(struct device *dev)
1011{
1012 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
1013 struct device *parent = conn->dev.parent;
1014
Mike Christie632248a2009-08-20 15:11:01 -05001015 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
Mike Christie26974782007-12-13 12:43:29 -06001016 kfree(conn);
1017 put_device(parent);
1018}
1019
1020static int iscsi_is_conn_dev(const struct device *dev)
1021{
1022 return dev->release == iscsi_conn_release;
1023}
1024
1025static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
1026{
1027 if (!iscsi_is_conn_dev(dev))
1028 return 0;
1029 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
1030}
1031
Mike Christie8434aa82006-06-28 12:00:30 -05001032void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001033{
Mike Christie30a6c652006-04-06 21:13:39 -05001034 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie26974782007-12-13 12:43:29 -06001035 unsigned long flags;
1036 int err;
Mike Christie30a6c652006-04-06 21:13:39 -05001037
Mike Christie632248a2009-08-20 15:11:01 -05001038 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
1039
Mike Christie26974782007-12-13 12:43:29 -06001040 spin_lock_irqsave(&sesslock, flags);
1041 list_del(&session->sess_list);
1042 spin_unlock_irqrestore(&sesslock, flags);
1043
Mike Christie45ab33b2008-03-04 13:26:55 -06001044 /* make sure there are no blocks/unblocks queued */
1045 flush_workqueue(iscsi_eh_timer_workq);
1046 /* make sure the timedout callout is not running */
1047 if (!cancel_delayed_work(&session->recovery_work))
1048 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -06001049 /*
1050 * If we are blocked let commands flow again. The lld or iscsi
1051 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -06001052 * We assume that LLD will not be calling block/unblock while
1053 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -06001054 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001055 spin_lock_irqsave(&session->lock, flags);
1056 session->state = ISCSI_SESSION_FREE;
1057 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -06001058
Mike Christie45ab33b2008-03-04 13:26:55 -06001059 scsi_target_unblock(&session->dev);
1060 /* flush running scans then delete devices */
Mike Christie06d25af2009-03-05 14:46:02 -06001061 scsi_flush_work(shost);
Mike Christie45ab33b2008-03-04 13:26:55 -06001062 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -05001063
Mike Christie26974782007-12-13 12:43:29 -06001064 /* hw iscsi may not have removed all connections from session */
1065 err = device_for_each_child(&session->dev, NULL,
1066 iscsi_iter_destroy_conn_fn);
1067 if (err)
Mike Christie322d7392008-01-31 13:36:52 -06001068 iscsi_cls_session_printk(KERN_ERR, session,
1069 "Could not delete all connections "
1070 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -05001071
Mike Christie7b8631b2006-01-13 18:05:50 -06001072 transport_unregister_device(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001073
1074 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001075 device_del(&session->dev);
1076}
1077EXPORT_SYMBOL_GPL(iscsi_remove_session);
1078
1079void iscsi_free_session(struct iscsi_cls_session *session)
1080{
Mike Christie632248a2009-08-20 15:11:01 -05001081 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
Mike Christie26974782007-12-13 12:43:29 -06001082 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -05001083 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001084}
Mike Christie8434aa82006-06-28 12:00:30 -05001085EXPORT_SYMBOL_GPL(iscsi_free_session);
1086
1087/**
1088 * iscsi_destroy_session - destroy iscsi session
1089 * @session: iscsi_session
1090 *
1091 * Can be called by a LLD or iscsi_transport. There must not be
1092 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -05001093 */
Mike Christie8434aa82006-06-28 12:00:30 -05001094int iscsi_destroy_session(struct iscsi_cls_session *session)
1095{
1096 iscsi_remove_session(session);
Mike Christie632248a2009-08-20 15:11:01 -05001097 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001098 iscsi_free_session(session);
1099 return 0;
1100}
Mike Christie7b8631b2006-01-13 18:05:50 -06001101EXPORT_SYMBOL_GPL(iscsi_destroy_session);
1102
Mike Christie7b8631b2006-01-13 18:05:50 -06001103/**
1104 * iscsi_create_conn - create iscsi class connection
1105 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -05001106 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -06001107 * @cid: connection id
1108 *
1109 * This can be called from a LLD or iscsi_transport. The connection
1110 * is child of the session so cid must be unique for all connections
1111 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -05001112 *
1113 * Since we do not support MCS, cid will normally be zero. In some cases
1114 * for software iscsi we could be trying to preallocate a connection struct
1115 * in which case there could be two connection structs and cid would be
1116 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -05001117 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001118struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05001119iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -06001120{
1121 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -06001122 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -06001123 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -06001124 int err;
1125
Mike Christie5d91e202008-05-21 15:54:01 -05001126 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001127 if (!conn)
1128 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -05001129 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06001130 conn->dd_data = &conn[1];
1131
Mike Christie22a39fb2011-02-16 15:04:33 -06001132 mutex_init(&conn->ep_mutex);
Mike Christie7b8631b2006-01-13 18:05:50 -06001133 INIT_LIST_HEAD(&conn->conn_list);
1134 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -05001135 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001136
1137 /* this is released in the dev's release function */
1138 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -04001139 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -05001140
Kay Sievers71610f52008-12-03 22:41:36 +01001141 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001142 conn->dev.parent = &session->dev;
1143 conn->dev.release = iscsi_conn_release;
1144 err = device_register(&conn->dev);
1145 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001146 iscsi_cls_session_printk(KERN_ERR, session, "could not "
1147 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001148 goto release_parent_ref;
1149 }
1150 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -06001151
1152 spin_lock_irqsave(&connlock, flags);
1153 list_add(&conn->conn_list, &connlist);
Mike Christie26974782007-12-13 12:43:29 -06001154 spin_unlock_irqrestore(&connlock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05001155
1156 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001157 return conn;
1158
1159release_parent_ref:
1160 put_device(&session->dev);
1161free_conn:
1162 kfree(conn);
1163 return NULL;
1164}
1165
1166EXPORT_SYMBOL_GPL(iscsi_create_conn);
1167
1168/**
1169 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -05001170 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -06001171 *
Mike Christie26974782007-12-13 12:43:29 -06001172 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001173 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001174int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1175{
Mike Christie26974782007-12-13 12:43:29 -06001176 unsigned long flags;
1177
1178 spin_lock_irqsave(&connlock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001179 list_del(&conn->conn_list);
1180 spin_unlock_irqrestore(&connlock, flags);
1181
Mike Christie7b8631b2006-01-13 18:05:50 -06001182 transport_unregister_device(&conn->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001183 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001184 device_unregister(&conn->dev);
1185 return 0;
1186}
Mike Christie7b8631b2006-01-13 18:05:50 -06001187EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1188
1189/*
Mike Christie7b8631b2006-01-13 18:05:50 -06001190 * iscsi interface functions
1191 */
Alex Aizman0896b752005-08-04 19:33:07 -07001192static struct iscsi_internal *
1193iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Alex Aizman0896b752005-08-04 19:33:07 -07001195 struct iscsi_internal *priv;
1196 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Alex Aizman0896b752005-08-04 19:33:07 -07001198 spin_lock_irqsave(&iscsi_transport_lock, flags);
1199 list_for_each_entry(priv, &iscsi_transports, list) {
1200 if (tt == priv->iscsi_transport) {
1201 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1202 return priv;
1203 }
1204 }
1205 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1206 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Alex Aizman0896b752005-08-04 19:33:07 -07001209static int
Michael Chan43514772009-06-08 18:14:41 -07001210iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -05001211{
Michael Chan43514772009-06-08 18:14:41 -07001212 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -07001213}
1214
Mike Christie7b7232f2006-02-01 21:06:49 -06001215int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -07001216 char *data, uint32_t data_size)
1217{
1218 struct nlmsghdr *nlh;
1219 struct sk_buff *skb;
1220 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07001221 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05001222 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001223 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1224 data_size);
1225
Mike Christie790f39a2006-05-18 20:31:39 -05001226 priv = iscsi_if_transport_lookup(conn->transport);
1227 if (!priv)
1228 return -EINVAL;
1229
Mike Christie43a145a2006-10-16 18:09:38 -04001230 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001231 if (!skb) {
Mike Christiee5bd7b542008-09-24 11:46:10 -05001232 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06001233 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1234 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001235 return -ENOMEM;
1236 }
1237
Michael Chan43514772009-06-08 18:14:41 -07001238 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001239 ev = NLMSG_DATA(nlh);
1240 memset(ev, 0, sizeof(*ev));
1241 ev->transport_handle = iscsi_handle(conn->transport);
1242 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05001243 ev->r.recv_req.cid = conn->cid;
1244 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001245 pdu = (char*)ev + sizeof(*ev);
1246 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1247 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1248
Michael Chan43514772009-06-08 18:14:41 -07001249 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001250}
1251EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1252
Michael Chan43514772009-06-08 18:14:41 -07001253int iscsi_offload_mesg(struct Scsi_Host *shost,
1254 struct iscsi_transport *transport, uint32_t type,
1255 char *data, uint16_t data_size)
1256{
1257 struct nlmsghdr *nlh;
1258 struct sk_buff *skb;
1259 struct iscsi_uevent *ev;
1260 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
1261
Michael Chana541f842009-07-29 08:49:52 +00001262 skb = alloc_skb(len, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001263 if (!skb) {
1264 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
1265 return -ENOMEM;
1266 }
1267
1268 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1269 ev = NLMSG_DATA(nlh);
1270 memset(ev, 0, sizeof(*ev));
1271 ev->type = type;
1272 ev->transport_handle = iscsi_handle(transport);
1273 switch (type) {
1274 case ISCSI_KEVENT_PATH_REQ:
1275 ev->r.req_path.host_no = shost->host_no;
1276 break;
1277 case ISCSI_KEVENT_IF_DOWN:
1278 ev->r.notify_if_down.host_no = shost->host_no;
1279 break;
1280 }
1281
1282 memcpy((char *)ev + sizeof(*ev), data, data_size);
1283
Michael Chana541f842009-07-29 08:49:52 +00001284 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001285}
1286EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1287
Mike Christiee5bd7b542008-09-24 11:46:10 -05001288void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07001289{
1290 struct nlmsghdr *nlh;
1291 struct sk_buff *skb;
1292 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05001293 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001294 int len = NLMSG_SPACE(sizeof(*ev));
1295
Mike Christie790f39a2006-05-18 20:31:39 -05001296 priv = iscsi_if_transport_lookup(conn->transport);
1297 if (!priv)
1298 return;
1299
Mike Christie43a145a2006-10-16 18:09:38 -04001300 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001301 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001302 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1303 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07001304 return;
1305 }
1306
Michael Chan43514772009-06-08 18:14:41 -07001307 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001308 ev = NLMSG_DATA(nlh);
1309 ev->transport_handle = iscsi_handle(conn->transport);
1310 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07001311 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05001312 ev->r.connerror.cid = conn->cid;
1313 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001314
Michael Chan43514772009-06-08 18:14:41 -07001315 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001316
Mike Christie322d7392008-01-31 13:36:52 -06001317 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1318 error);
Alex Aizman0896b752005-08-04 19:33:07 -07001319}
Mike Christiee5bd7b542008-09-24 11:46:10 -05001320EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07001321
1322static int
Michael Chan43514772009-06-08 18:14:41 -07001323iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1324 void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07001325{
1326 struct sk_buff *skb;
1327 struct nlmsghdr *nlh;
1328 int len = NLMSG_SPACE(size);
1329 int flags = multi ? NLM_F_MULTI : 0;
1330 int t = done ? NLMSG_DONE : type;
1331
Mike Christie43a145a2006-10-16 18:09:38 -04001332 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05001333 if (!skb) {
1334 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1335 return -ENOMEM;
1336 }
Alex Aizman0896b752005-08-04 19:33:07 -07001337
Michael Chan43514772009-06-08 18:14:41 -07001338 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001339 nlh->nlmsg_flags = flags;
1340 memcpy(NLMSG_DATA(nlh), payload, size);
Michael Chan43514772009-06-08 18:14:41 -07001341 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001342}
1343
1344static int
Mike Christie5b940ad2006-02-01 21:06:56 -06001345iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07001346{
1347 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1348 struct iscsi_stats *stats;
1349 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06001350 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001351 struct nlmsghdr *nlhstat;
1352 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05001353 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001354 int len = NLMSG_SPACE(sizeof(*ev) +
1355 sizeof(struct iscsi_stats) +
1356 sizeof(struct iscsi_stats_custom) *
1357 ISCSI_STATS_CUSTOM_MAX);
1358 int err = 0;
1359
Mike Christie790f39a2006-05-18 20:31:39 -05001360 priv = iscsi_if_transport_lookup(transport);
1361 if (!priv)
1362 return -EINVAL;
1363
Mike Christieb5c7a122006-04-06 21:13:33 -05001364 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07001365 if (!conn)
1366 return -EEXIST;
1367
1368 do {
1369 int actual_size;
1370
Mike Christie43a145a2006-10-16 18:09:38 -04001371 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001372 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06001373 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1374 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001375 return -ENOMEM;
1376 }
1377
Michael Chan43514772009-06-08 18:14:41 -07001378 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07001379 (len - sizeof(*nlhstat)), 0);
1380 evstat = NLMSG_DATA(nlhstat);
1381 memset(evstat, 0, sizeof(*evstat));
1382 evstat->transport_handle = iscsi_handle(conn->transport);
1383 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05001384 evstat->u.get_stats.cid =
1385 ev->u.get_stats.cid;
1386 evstat->u.get_stats.sid =
1387 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07001388 stats = (struct iscsi_stats *)
1389 ((char*)evstat + sizeof(*evstat));
1390 memset(stats, 0, sizeof(*stats));
1391
Mike Christie7b7232f2006-02-01 21:06:49 -06001392 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07001393 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1394 sizeof(struct iscsi_stats) +
1395 sizeof(struct iscsi_stats_custom) *
1396 stats->custom_length);
1397 actual_size -= sizeof(*nlhstat);
1398 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06001399 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07001400 nlhstat->nlmsg_len = actual_size;
1401
Michael Chan43514772009-06-08 18:14:41 -07001402 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1403 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001404 } while (err < 0 && err != -ECONNREFUSED);
1405
1406 return err;
1407}
1408
Mike Christie53cb8a12006-06-28 12:00:32 -05001409/**
Mike Christie26974782007-12-13 12:43:29 -06001410 * iscsi_session_event - send session destr. completion event
1411 * @session: iscsi class session
1412 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05001413 */
Mike Christie26974782007-12-13 12:43:29 -06001414int iscsi_session_event(struct iscsi_cls_session *session,
1415 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05001416{
1417 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05001418 struct Scsi_Host *shost;
1419 struct iscsi_uevent *ev;
1420 struct sk_buff *skb;
1421 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05001422 int rc, len = NLMSG_SPACE(sizeof(*ev));
1423
Mike Christie26974782007-12-13 12:43:29 -06001424 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05001425 if (!priv)
1426 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05001427 shost = iscsi_session_to_shost(session);
1428
Mike Christie43a145a2006-10-16 18:09:38 -04001429 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001430 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001431 iscsi_cls_session_printk(KERN_ERR, session,
1432 "Cannot notify userspace of session "
1433 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001434 return -ENOMEM;
1435 }
1436
Michael Chan43514772009-06-08 18:14:41 -07001437 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Mike Christie53cb8a12006-06-28 12:00:32 -05001438 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06001439 ev->transport_handle = iscsi_handle(session->transport);
1440
1441 ev->type = event;
1442 switch (event) {
1443 case ISCSI_KEVENT_DESTROY_SESSION:
1444 ev->r.d_session.host_no = shost->host_no;
1445 ev->r.d_session.sid = session->sid;
1446 break;
1447 case ISCSI_KEVENT_CREATE_SESSION:
1448 ev->r.c_session_ret.host_no = shost->host_no;
1449 ev->r.c_session_ret.sid = session->sid;
1450 break;
1451 case ISCSI_KEVENT_UNBIND_SESSION:
1452 ev->r.unbind_session.host_no = shost->host_no;
1453 ev->r.unbind_session.sid = session->sid;
1454 break;
1455 default:
Mike Christie322d7392008-01-31 13:36:52 -06001456 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1457 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06001458 kfree_skb(skb);
1459 return -EINVAL;
1460 }
Mike Christie53cb8a12006-06-28 12:00:32 -05001461
1462 /*
1463 * this will occur if the daemon is not up, so we just warn
1464 * the user and when the daemon is restarted it will handle it
1465 */
Michael Chan43514772009-06-08 18:14:41 -07001466 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001467 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06001468 iscsi_cls_session_printk(KERN_ERR, session,
1469 "Cannot notify userspace of session "
1470 "event %u. Check iscsi daemon\n",
1471 event);
Mike Christie632248a2009-08-20 15:11:01 -05001472
1473 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
1474 event, rc);
Mike Christie53cb8a12006-06-28 12:00:32 -05001475 return rc;
1476}
Mike Christie26974782007-12-13 12:43:29 -06001477EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001478
Alex Aizman0896b752005-08-04 19:33:07 -07001479static int
Mike Christied82ff9be2008-05-21 15:54:13 -05001480iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1481 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
Mike Christie40753ca2008-05-21 15:53:56 -05001482 uint16_t cmds_max, uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001483{
1484 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001485 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06001486 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06001487
Mike Christied82ff9be2008-05-21 15:54:13 -05001488 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06001489 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06001490 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001491 return -ENOMEM;
1492
Mike Christie5e7facb2009-03-05 14:46:06 -06001493 shost = iscsi_session_to_shost(session);
1494 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001495 ev->r.c_session_ret.sid = session->sid;
Mike Christie632248a2009-08-20 15:11:01 -05001496 ISCSI_DBG_TRANS_SESSION(session,
1497 "Completed creating transport session\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001498 return 0;
1499}
1500
1501static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001502iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001503{
Mike Christie7b8631b2006-01-13 18:05:50 -06001504 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001505 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001506
Mike Christieb5c7a122006-04-06 21:13:33 -05001507 session = iscsi_session_lookup(ev->u.c_conn.sid);
1508 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001509 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001510 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001511 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001512 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001513
Mike Christie7b7232f2006-02-01 21:06:49 -06001514 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001515 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001516 iscsi_cls_session_printk(KERN_ERR, session,
1517 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001518 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001519 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001520
Mike Christieb5c7a122006-04-06 21:13:33 -05001521 ev->r.c_conn_ret.sid = session->sid;
1522 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie632248a2009-08-20 15:11:01 -05001523
1524 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001525 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001526}
1527
1528static int
1529iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1530{
Mike Christie7b8631b2006-01-13 18:05:50 -06001531 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001532
Mike Christieb5c7a122006-04-06 21:13:33 -05001533 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001534 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001535 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001536
Mike Christie632248a2009-08-20 15:11:01 -05001537 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001538 if (transport->destroy_conn)
1539 transport->destroy_conn(conn);
Mike Christie632248a2009-08-20 15:11:01 -05001540
Mike Christie7b8631b2006-01-13 18:05:50 -06001541 return 0;
1542}
1543
Mike Christiefd7255f2006-04-06 21:13:36 -05001544static int
1545iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1546{
1547 char *data = (char*)ev + sizeof(*ev);
1548 struct iscsi_cls_conn *conn;
1549 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001550 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001551
1552 session = iscsi_session_lookup(ev->u.set_param.sid);
1553 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1554 if (!conn || !session)
1555 return -EINVAL;
1556
1557 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001558 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001559 sscanf(data, "%d", &value);
Mike Christiefdd46dc2009-11-11 16:34:34 -06001560 session->recovery_tmo = value;
Mike Christie30a6c652006-04-06 21:13:39 -05001561 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001562 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001563 err = transport->set_param(conn, ev->u.set_param.param,
1564 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001565 }
1566
1567 return err;
1568}
1569
Mike Christie10eb0f02009-05-13 17:57:38 -05001570static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1571 struct iscsi_uevent *ev, int msg_type)
1572{
1573 struct iscsi_endpoint *ep;
1574 struct sockaddr *dst_addr;
1575 struct Scsi_Host *shost = NULL;
1576 int non_blocking, err = 0;
1577
1578 if (!transport->ep_connect)
1579 return -EINVAL;
1580
1581 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1582 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1583 if (!shost) {
1584 printk(KERN_ERR "ep connect failed. Could not find "
1585 "host no %u\n",
1586 ev->u.ep_connect_through_host.host_no);
1587 return -ENODEV;
1588 }
1589 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1590 } else
1591 non_blocking = ev->u.ep_connect.non_blocking;
1592
1593 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1594 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1595 if (IS_ERR(ep)) {
1596 err = PTR_ERR(ep);
1597 goto release_host;
1598 }
1599
1600 ev->r.ep_connect_ret.handle = ep->id;
1601release_host:
1602 if (shost)
1603 scsi_host_put(shost);
1604 return err;
1605}
1606
Mike Christie22a39fb2011-02-16 15:04:33 -06001607static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
1608 u64 ep_handle)
1609{
1610 struct iscsi_cls_conn *conn;
1611 struct iscsi_endpoint *ep;
1612
1613 if (!transport->ep_disconnect)
1614 return -EINVAL;
1615
1616 ep = iscsi_lookup_endpoint(ep_handle);
1617 if (!ep)
1618 return -EINVAL;
1619 conn = ep->conn;
1620 if (conn) {
1621 mutex_lock(&conn->ep_mutex);
1622 conn->ep = NULL;
1623 mutex_unlock(&conn->ep_mutex);
1624 }
1625
1626 transport->ep_disconnect(ep);
1627 return 0;
1628}
1629
Mike Christie7b8631b2006-01-13 18:05:50 -06001630static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001631iscsi_if_transport_ep(struct iscsi_transport *transport,
1632 struct iscsi_uevent *ev, int msg_type)
1633{
Mike Christied82ff9be2008-05-21 15:54:13 -05001634 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001635 int rc = 0;
1636
1637 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05001638 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001639 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001640 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001641 break;
1642 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1643 if (!transport->ep_poll)
1644 return -EINVAL;
1645
Mike Christied82ff9be2008-05-21 15:54:13 -05001646 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1647 if (!ep)
1648 return -EINVAL;
1649
1650 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001651 ev->u.ep_poll.timeout_ms);
1652 break;
1653 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie22a39fb2011-02-16 15:04:33 -06001654 rc = iscsi_if_ep_disconnect(transport,
1655 ev->u.ep_disconnect.ep_handle);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001656 break;
1657 }
1658 return rc;
1659}
1660
1661static int
Mike Christie01cb2252006-06-28 12:00:22 -05001662iscsi_tgt_dscvr(struct iscsi_transport *transport,
1663 struct iscsi_uevent *ev)
1664{
Mike Christie2174a042007-05-30 12:57:10 -05001665 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001666 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001667 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001668
1669 if (!transport->tgt_dscvr)
1670 return -EINVAL;
1671
Mike Christie2174a042007-05-30 12:57:10 -05001672 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001673 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05001674 printk(KERN_ERR "target discovery could not find host no %u\n",
1675 ev->u.tgt_dscvr.host_no);
1676 return -ENODEV;
1677 }
1678
1679
Mike Christie01cb2252006-06-28 12:00:22 -05001680 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001681 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1682 ev->u.tgt_dscvr.enable, dst_addr);
1683 scsi_host_put(shost);
1684 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001685}
1686
1687static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001688iscsi_set_host_param(struct iscsi_transport *transport,
1689 struct iscsi_uevent *ev)
1690{
1691 char *data = (char*)ev + sizeof(*ev);
1692 struct Scsi_Host *shost;
1693 int err;
1694
1695 if (!transport->set_host_param)
1696 return -ENOSYS;
1697
1698 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001699 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05001700 printk(KERN_ERR "set_host_param could not find host no %u\n",
1701 ev->u.set_host_param.host_no);
1702 return -ENODEV;
1703 }
1704
1705 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1706 data, ev->u.set_host_param.len);
1707 scsi_host_put(shost);
1708 return err;
1709}
1710
1711static int
Michael Chan43514772009-06-08 18:14:41 -07001712iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1713{
1714 struct Scsi_Host *shost;
1715 struct iscsi_path *params;
1716 int err;
1717
1718 if (!transport->set_path)
1719 return -ENOSYS;
1720
1721 shost = scsi_host_lookup(ev->u.set_path.host_no);
1722 if (!shost) {
1723 printk(KERN_ERR "set path could not find host no %u\n",
1724 ev->u.set_path.host_no);
1725 return -ENODEV;
1726 }
1727
1728 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1729 err = transport->set_path(shost, params);
1730
1731 scsi_host_put(shost);
1732 return err;
1733}
1734
1735static int
Mike Christie56c155b2011-07-25 13:48:37 -05001736iscsi_set_iface_params(struct iscsi_transport *transport,
1737 struct iscsi_uevent *ev)
1738{
1739 char *data = (char *)ev + sizeof(*ev);
1740 struct Scsi_Host *shost;
1741 int err;
1742
1743 if (!transport->set_iface_param)
1744 return -ENOSYS;
1745
1746 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
1747 if (!shost) {
1748 printk(KERN_ERR "set_iface_params could not find host no %u\n",
1749 ev->u.set_iface_params.host_no);
1750 return -ENODEV;
1751 }
1752
1753 err = transport->set_iface_param(shost, data,
1754 ev->u.set_iface_params.count);
1755 scsi_host_put(shost);
1756 return err;
1757}
1758
1759static int
Michael Chan43514772009-06-08 18:14:41 -07001760iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07001761{
1762 int err = 0;
1763 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1764 struct iscsi_transport *transport = NULL;
1765 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001766 struct iscsi_cls_session *session;
1767 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05001768 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001769
Michael Chan43514772009-06-08 18:14:41 -07001770 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1771 *group = ISCSI_NL_GRP_UIP;
1772 else
1773 *group = ISCSI_NL_GRP_ISCSID;
1774
Alex Aizman0896b752005-08-04 19:33:07 -07001775 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1776 if (!priv)
1777 return -EINVAL;
1778 transport = priv->iscsi_transport;
1779
Mike Christie7b7232f2006-02-01 21:06:49 -06001780 if (!try_module_get(transport->owner))
1781 return -EINVAL;
1782
Alex Aizman0896b752005-08-04 19:33:07 -07001783 switch (nlh->nlmsg_type) {
1784 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001785 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001786 ev->u.c_session.initial_cmdsn,
1787 ev->u.c_session.cmds_max,
1788 ev->u.c_session.queue_depth);
1789 break;
1790 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001791 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05001792 if (!ep) {
1793 err = -EINVAL;
1794 break;
1795 }
Mike Christied82ff9be2008-05-21 15:54:13 -05001796
1797 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001798 ev->u.c_bound_session.initial_cmdsn,
1799 ev->u.c_bound_session.cmds_max,
1800 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001801 break;
1802 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001803 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001804 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001805 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001806 else
1807 err = -EINVAL;
1808 break;
1809 case ISCSI_UEVENT_UNBIND_SESSION:
1810 session = iscsi_session_lookup(ev->u.d_session.sid);
1811 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06001812 scsi_queue_work(iscsi_session_to_shost(session),
1813 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06001814 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001815 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001816 break;
1817 case ISCSI_UEVENT_CREATE_CONN:
1818 err = iscsi_if_create_conn(transport, ev);
1819 break;
1820 case ISCSI_UEVENT_DESTROY_CONN:
1821 err = iscsi_if_destroy_conn(transport, ev);
1822 break;
1823 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001824 session = iscsi_session_lookup(ev->u.b_conn.sid);
1825 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001826
Mike Christie22a39fb2011-02-16 15:04:33 -06001827 if (conn && conn->ep)
1828 iscsi_if_ep_disconnect(transport, conn->ep->id);
1829
1830 if (!session || !conn) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001831 err = -EINVAL;
Mike Christie22a39fb2011-02-16 15:04:33 -06001832 break;
1833 }
1834
1835 ev->r.retcode = transport->bind_conn(session, conn,
1836 ev->u.b_conn.transport_eph,
1837 ev->u.b_conn.is_leading);
1838 if (ev->r.retcode || !transport->ep_connect)
1839 break;
1840
1841 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
1842 if (ep) {
1843 ep->conn = conn;
1844
1845 mutex_lock(&conn->ep_mutex);
1846 conn->ep = ep;
1847 mutex_unlock(&conn->ep_mutex);
1848 } else
1849 iscsi_cls_conn_printk(KERN_ERR, conn,
1850 "Could not set ep conn "
1851 "binding\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001852 break;
1853 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001854 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001855 break;
1856 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001857 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001858 if (conn)
1859 ev->r.retcode = transport->start_conn(conn);
1860 else
1861 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001862 break;
1863 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001864 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001865 if (conn)
1866 transport->stop_conn(conn, ev->u.stop_conn.flag);
1867 else
1868 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001869 break;
1870 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001871 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001872 if (conn)
1873 ev->r.retcode = transport->send_pdu(conn,
1874 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1875 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1876 ev->u.send_pdu.data_size);
1877 else
1878 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001879 break;
1880 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001881 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001882 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001883 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1884 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1885 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001886 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001887 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1888 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001889 case ISCSI_UEVENT_TGT_DSCVR:
1890 err = iscsi_tgt_dscvr(transport, ev);
1891 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001892 case ISCSI_UEVENT_SET_HOST_PARAM:
1893 err = iscsi_set_host_param(transport, ev);
1894 break;
Michael Chan43514772009-06-08 18:14:41 -07001895 case ISCSI_UEVENT_PATH_UPDATE:
1896 err = iscsi_set_path(transport, ev);
1897 break;
Mike Christie56c155b2011-07-25 13:48:37 -05001898 case ISCSI_UEVENT_SET_IFACE_PARAMS:
1899 err = iscsi_set_iface_params(transport, ev);
1900 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001901 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001902 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001903 break;
1904 }
1905
Mike Christie7b7232f2006-02-01 21:06:49 -06001906 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001907 return err;
1908}
1909
Mike Christieb5c7a122006-04-06 21:13:33 -05001910/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001911 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1912 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001913 */
Alex Aizman0896b752005-08-04 19:33:07 -07001914static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001915iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001916{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001917 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001918 while (skb->len >= NLMSG_SPACE(0)) {
1919 int err;
1920 uint32_t rlen;
1921 struct nlmsghdr *nlh;
1922 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07001923 uint32_t group;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001924
1925 nlh = nlmsg_hdr(skb);
1926 if (nlh->nlmsg_len < sizeof(*nlh) ||
1927 skb->len < nlh->nlmsg_len) {
1928 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001929 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001930
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001931 ev = NLMSG_DATA(nlh);
1932 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1933 if (rlen > skb->len)
1934 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001935
Michael Chan43514772009-06-08 18:14:41 -07001936 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001937 if (err) {
1938 ev->type = ISCSI_KEVENT_IF_ERROR;
1939 ev->iferror = err;
1940 }
1941 do {
1942 /*
1943 * special case for GET_STATS:
1944 * on success - sending reply and stats from
1945 * inside of if_recv_msg(),
1946 * on error - fall through.
1947 */
1948 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001949 break;
Michael Chan43514772009-06-08 18:14:41 -07001950 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001951 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1952 } while (err < 0 && err != -ECONNREFUSED);
1953 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001954 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001955 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001956}
1957
Mike Christiefd7255f2006-04-06 21:13:36 -05001958#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01001959struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05001960 __ATTR(_name,_mode,_show,_store)
1961
Alex Aizman0896b752005-08-04 19:33:07 -07001962/*
1963 * iSCSI connection attrs
1964 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001965#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001967show_conn_param_##param(struct device *dev, \
1968 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001969{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001970 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001971 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001972 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001973}
1974
Mike Christiea54a52c2006-06-28 12:00:23 -05001975#define iscsi_conn_attr(field, param) \
1976 iscsi_conn_attr_show(param) \
1977static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001978 NULL);
1979
Mike Christiea54a52c2006-06-28 12:00:23 -05001980iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1981iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1982iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1983iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1984iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1985iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1986iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
Mike Christiea54a52c2006-06-28 12:00:23 -05001987iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1988iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001989iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1990iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Mike Christie289324b2011-02-16 15:04:37 -06001992#define iscsi_conn_ep_attr_show(param) \
1993static ssize_t show_conn_ep_param_##param(struct device *dev, \
1994 struct device_attribute *attr,\
1995 char *buf) \
1996{ \
1997 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
1998 struct iscsi_transport *t = conn->transport; \
1999 struct iscsi_endpoint *ep; \
2000 ssize_t rc; \
2001 \
2002 /* \
2003 * Need to make sure ep_disconnect does not free the LLD's \
2004 * interconnect resources while we are trying to read them. \
2005 */ \
2006 mutex_lock(&conn->ep_mutex); \
2007 ep = conn->ep; \
2008 if (!ep && t->ep_connect) { \
2009 mutex_unlock(&conn->ep_mutex); \
2010 return -ENOTCONN; \
2011 } \
2012 \
2013 if (ep) \
2014 rc = t->get_ep_param(ep, param, buf); \
2015 else \
2016 rc = t->get_conn_param(conn, param, buf); \
2017 mutex_unlock(&conn->ep_mutex); \
2018 return rc; \
2019}
2020
2021#define iscsi_conn_ep_attr(field, param) \
2022 iscsi_conn_ep_attr_show(param) \
2023static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
2024 show_conn_ep_param_##param, NULL);
2025
2026iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
2027iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
2028
Mike Christie3128c6c2011-07-25 13:48:42 -05002029static struct attribute *iscsi_conn_attrs[] = {
2030 &dev_attr_conn_max_recv_dlength.attr,
2031 &dev_attr_conn_max_xmit_dlength.attr,
2032 &dev_attr_conn_header_digest.attr,
2033 &dev_attr_conn_data_digest.attr,
2034 &dev_attr_conn_ifmarker.attr,
2035 &dev_attr_conn_ofmarker.attr,
2036 &dev_attr_conn_address.attr,
2037 &dev_attr_conn_port.attr,
2038 &dev_attr_conn_exp_statsn.attr,
2039 &dev_attr_conn_persistent_address.attr,
2040 &dev_attr_conn_persistent_port.attr,
2041 &dev_attr_conn_ping_tmo.attr,
2042 &dev_attr_conn_recv_tmo.attr,
2043 NULL,
2044};
2045
2046static mode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
2047 struct attribute *attr, int i)
2048{
2049 struct device *cdev = container_of(kobj, struct device, kobj);
2050 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
2051 struct iscsi_transport *t = conn->transport;
2052 int param;
2053
2054 if (attr == &dev_attr_conn_max_recv_dlength.attr)
2055 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
2056 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
2057 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
2058 else if (attr == &dev_attr_conn_header_digest.attr)
2059 param = ISCSI_PARAM_HDRDGST_EN;
2060 else if (attr == &dev_attr_conn_data_digest.attr)
2061 param = ISCSI_PARAM_DATADGST_EN;
2062 else if (attr == &dev_attr_conn_ifmarker.attr)
2063 param = ISCSI_PARAM_IFMARKER_EN;
2064 else if (attr == &dev_attr_conn_ofmarker.attr)
2065 param = ISCSI_PARAM_OFMARKER_EN;
2066 else if (attr == &dev_attr_conn_address.attr)
2067 param = ISCSI_PARAM_CONN_ADDRESS;
2068 else if (attr == &dev_attr_conn_port.attr)
2069 param = ISCSI_PARAM_CONN_PORT;
2070 else if (attr == &dev_attr_conn_exp_statsn.attr)
2071 param = ISCSI_PARAM_EXP_STATSN;
2072 else if (attr == &dev_attr_conn_persistent_address.attr)
2073 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
2074 else if (attr == &dev_attr_conn_persistent_port.attr)
2075 param = ISCSI_PARAM_PERSISTENT_PORT;
2076 else if (attr == &dev_attr_conn_ping_tmo.attr)
2077 param = ISCSI_PARAM_PING_TMO;
2078 else if (attr == &dev_attr_conn_recv_tmo.attr)
2079 param = ISCSI_PARAM_RECV_TMO;
2080 else {
2081 WARN_ONCE(1, "Invalid conn attr");
2082 return 0;
2083 }
2084
2085 return t->attr_is_visible(ISCSI_PARAM, param);
2086}
2087
2088static struct attribute_group iscsi_conn_group = {
2089 .attrs = iscsi_conn_attrs,
2090 .is_visible = iscsi_conn_attr_is_visible,
2091};
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093/*
Alex Aizman0896b752005-08-04 19:33:07 -07002094 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 */
Mike Christieb2c64162007-05-30 12:57:16 -05002096#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002098show_session_param_##param(struct device *dev, \
2099 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002100{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002101 struct iscsi_cls_session *session = \
2102 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002103 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05002104 \
2105 if (perm && !capable(CAP_SYS_ADMIN)) \
2106 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05002107 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002108}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Mike Christieb2c64162007-05-30 12:57:16 -05002110#define iscsi_session_attr(field, param, perm) \
2111 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05002112static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05002113 NULL);
Mike Christieb2c64162007-05-30 12:57:16 -05002114iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
2115iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
2116iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
2117iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
2118iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
2119iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
2120iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
2121iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
2122iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
2123iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
2124iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
2125iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
2126iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
2127iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06002128iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
2129iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
2130iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002131iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05002132iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05302133iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
2134iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05002135
Mike Christie6eabafb2008-01-31 13:36:43 -06002136static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01002137show_priv_session_state(struct device *dev, struct device_attribute *attr,
2138 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06002139{
Tony Jonesee959b02008-02-22 00:13:36 +01002140 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06002141 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
2142}
2143static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
2144 NULL);
2145
Mike Christiefd7255f2006-04-06 21:13:36 -05002146#define iscsi_priv_session_attr_show(field, format) \
2147static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002148show_priv_session_##field(struct device *dev, \
2149 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002150{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002151 struct iscsi_cls_session *session = \
2152 iscsi_dev_to_session(dev->parent); \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302153 if (session->field == -1) \
2154 return sprintf(buf, "off\n"); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002155 return sprintf(buf, format"\n", session->field); \
2156}
2157
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302158#define iscsi_priv_session_attr_store(field) \
2159static ssize_t \
2160store_priv_session_##field(struct device *dev, \
2161 struct device_attribute *attr, \
2162 const char *buf, size_t count) \
2163{ \
2164 int val; \
2165 char *cp; \
2166 struct iscsi_cls_session *session = \
2167 iscsi_dev_to_session(dev->parent); \
2168 if ((session->state == ISCSI_SESSION_FREE) || \
2169 (session->state == ISCSI_SESSION_FAILED)) \
2170 return -EBUSY; \
2171 if (strncmp(buf, "off", 3) == 0) \
2172 session->field = -1; \
2173 else { \
2174 val = simple_strtoul(buf, &cp, 0); \
2175 if (*cp != '\0' && *cp != '\n') \
2176 return -EINVAL; \
2177 session->field = val; \
2178 } \
2179 return count; \
2180}
2181
2182#define iscsi_priv_session_rw_attr(field, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002183 iscsi_priv_session_attr_show(field, format) \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302184 iscsi_priv_session_attr_store(field) \
Vasiliy Kulikov523f3c82011-02-04 15:24:14 +03002185static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302186 show_priv_session_##field, \
2187 store_priv_session_##field)
2188iscsi_priv_session_rw_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05002189
Mike Christie1d063c12011-07-25 13:48:43 -05002190static struct attribute *iscsi_session_attrs[] = {
2191 &dev_attr_sess_initial_r2t.attr,
2192 &dev_attr_sess_max_outstanding_r2t.attr,
2193 &dev_attr_sess_immediate_data.attr,
2194 &dev_attr_sess_first_burst_len.attr,
2195 &dev_attr_sess_max_burst_len.attr,
2196 &dev_attr_sess_data_pdu_in_order.attr,
2197 &dev_attr_sess_data_seq_in_order.attr,
2198 &dev_attr_sess_erl.attr,
2199 &dev_attr_sess_targetname.attr,
2200 &dev_attr_sess_tpgt.attr,
2201 &dev_attr_sess_password.attr,
2202 &dev_attr_sess_password_in.attr,
2203 &dev_attr_sess_username.attr,
2204 &dev_attr_sess_username_in.attr,
2205 &dev_attr_sess_fast_abort.attr,
2206 &dev_attr_sess_abort_tmo.attr,
2207 &dev_attr_sess_lu_reset_tmo.attr,
2208 &dev_attr_sess_tgt_reset_tmo.attr,
2209 &dev_attr_sess_ifacename.attr,
2210 &dev_attr_sess_initiatorname.attr,
2211 &dev_attr_sess_targetalias.attr,
2212 &dev_attr_priv_sess_recovery_tmo.attr,
2213 &dev_attr_priv_sess_state.attr,
2214 NULL,
2215};
2216
2217static mode_t iscsi_session_attr_is_visible(struct kobject *kobj,
2218 struct attribute *attr, int i)
2219{
2220 struct device *cdev = container_of(kobj, struct device, kobj);
2221 struct iscsi_cls_session *session = transport_class_to_session(cdev);
2222 struct iscsi_transport *t = session->transport;
2223 int param;
2224
2225 if (attr == &dev_attr_sess_initial_r2t.attr)
2226 param = ISCSI_PARAM_INITIAL_R2T_EN;
2227 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
2228 param = ISCSI_PARAM_MAX_R2T;
2229 else if (attr == &dev_attr_sess_immediate_data.attr)
2230 param = ISCSI_PARAM_IMM_DATA_EN;
2231 else if (attr == &dev_attr_sess_first_burst_len.attr)
2232 param = ISCSI_PARAM_FIRST_BURST;
2233 else if (attr == &dev_attr_sess_max_burst_len.attr)
2234 param = ISCSI_PARAM_MAX_BURST;
2235 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
2236 param = ISCSI_PARAM_PDU_INORDER_EN;
2237 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
2238 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
2239 else if (attr == &dev_attr_sess_erl.attr)
2240 param = ISCSI_PARAM_ERL;
2241 else if (attr == &dev_attr_sess_targetname.attr)
2242 param = ISCSI_PARAM_TARGET_NAME;
2243 else if (attr == &dev_attr_sess_tpgt.attr)
2244 param = ISCSI_PARAM_TPGT;
2245 else if (attr == &dev_attr_sess_password.attr)
2246 param = ISCSI_PARAM_USERNAME;
2247 else if (attr == &dev_attr_sess_password_in.attr)
2248 param = ISCSI_PARAM_USERNAME_IN;
2249 else if (attr == &dev_attr_sess_username.attr)
2250 param = ISCSI_PARAM_PASSWORD;
2251 else if (attr == &dev_attr_sess_username_in.attr)
2252 param = ISCSI_PARAM_PASSWORD_IN;
2253 else if (attr == &dev_attr_sess_fast_abort.attr)
2254 param = ISCSI_PARAM_FAST_ABORT;
2255 else if (attr == &dev_attr_sess_abort_tmo.attr)
2256 param = ISCSI_PARAM_ABORT_TMO;
2257 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
2258 param = ISCSI_PARAM_LU_RESET_TMO;
2259 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
2260 param = ISCSI_PARAM_TGT_RESET_TMO;
2261 else if (attr == &dev_attr_sess_ifacename.attr)
2262 param = ISCSI_PARAM_IFACE_NAME;
2263 else if (attr == &dev_attr_sess_initiatorname.attr)
2264 param = ISCSI_PARAM_INITIATOR_NAME;
2265 else if (attr == &dev_attr_sess_targetalias.attr)
2266 param = ISCSI_PARAM_TARGET_ALIAS;
2267 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
2268 return S_IRUGO | S_IWUSR;
2269 else if (attr == &dev_attr_priv_sess_state.attr)
2270 return S_IRUGO;
2271 else {
2272 WARN_ONCE(1, "Invalid session attr");
2273 return 0;
2274 }
2275
2276 return t->attr_is_visible(ISCSI_PARAM, param);
2277}
2278
2279static struct attribute_group iscsi_session_group = {
2280 .attrs = iscsi_session_attrs,
2281 .is_visible = iscsi_session_attr_is_visible,
2282};
2283
Mike Christie1819dc82007-05-30 12:57:08 -05002284/*
2285 * iSCSI host attrs
2286 */
2287#define iscsi_host_attr_show(param) \
2288static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002289show_host_param_##param(struct device *dev, \
2290 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05002291{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002292 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05002293 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
2294 return priv->iscsi_transport->get_host_param(shost, param, buf); \
2295}
2296
2297#define iscsi_host_attr(field, param) \
2298 iscsi_host_attr_show(param) \
2299static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
2300 NULL);
2301
Mike Christied8196ed2007-05-30 12:57:25 -05002302iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002303iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05002304iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05002305iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002306
Mike Christie1819dc82007-05-30 12:57:08 -05002307#define SETUP_HOST_RD_ATTR(field, param_flag) \
2308do { \
2309 if (tt->host_param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01002310 priv->host_attrs[count] = &dev_attr_host_##field; \
Mike Christie1819dc82007-05-30 12:57:08 -05002311 count++; \
2312 } \
2313} while (0)
2314
Alex Aizman0896b752005-08-04 19:33:07 -07002315static int iscsi_session_match(struct attribute_container *cont,
2316 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Mike Christie7b8631b2006-01-13 18:05:50 -06002318 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07002320 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Alex Aizman0896b752005-08-04 19:33:07 -07002322 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return 0;
2324
Mike Christie7b8631b2006-01-13 18:05:50 -06002325 session = iscsi_dev_to_session(dev);
2326 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07002327 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return 0;
2329
Alex Aizman0896b752005-08-04 19:33:07 -07002330 priv = to_iscsi_internal(shost->transportt);
2331 if (priv->session_cont.ac.class != &iscsi_session_class.class)
2332 return 0;
2333
2334 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
Alex Aizman0896b752005-08-04 19:33:07 -07002337static int iscsi_conn_match(struct attribute_container *cont,
2338 struct device *dev)
2339{
Mike Christie7b8631b2006-01-13 18:05:50 -06002340 struct iscsi_cls_session *session;
2341 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07002342 struct Scsi_Host *shost;
2343 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Alex Aizman0896b752005-08-04 19:33:07 -07002345 if (!iscsi_is_conn_dev(dev))
2346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Mike Christie7b8631b2006-01-13 18:05:50 -06002348 conn = iscsi_dev_to_conn(dev);
2349 session = iscsi_dev_to_session(conn->dev.parent);
2350 shost = iscsi_session_to_shost(session);
2351
Alex Aizman0896b752005-08-04 19:33:07 -07002352 if (!shost->transportt)
2353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Alex Aizman0896b752005-08-04 19:33:07 -07002355 priv = to_iscsi_internal(shost->transportt);
2356 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
2357 return 0;
2358
2359 return &priv->conn_cont.ac == cont;
2360}
2361
Mike Christie30a6c652006-04-06 21:13:39 -05002362static int iscsi_host_match(struct attribute_container *cont,
2363 struct device *dev)
2364{
2365 struct Scsi_Host *shost;
2366 struct iscsi_internal *priv;
2367
2368 if (!scsi_is_host_device(dev))
2369 return 0;
2370
2371 shost = dev_to_shost(dev);
2372 if (!shost->transportt ||
2373 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
2374 return 0;
2375
2376 priv = to_iscsi_internal(shost->transportt);
2377 return &priv->t.host_attrs.ac == cont;
2378}
2379
Mike Christie7b8631b2006-01-13 18:05:50 -06002380struct scsi_transport_template *
2381iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07002382{
2383 struct iscsi_internal *priv;
2384 unsigned long flags;
2385 int count = 0, err;
2386
2387 BUG_ON(!tt);
2388
2389 priv = iscsi_if_transport_lookup(tt);
2390 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002391 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002392
Jes Sorensen24669f752006-01-16 10:31:18 -05002393 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07002394 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002395 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002396 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07002397 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05002398 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06002399 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07002400
Tony Jonesee959b02008-02-22 00:13:36 +01002401 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01002402 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01002403 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07002404 if (err)
2405 goto free_priv;
2406
Tony Jonesee959b02008-02-22 00:13:36 +01002407 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07002408 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01002409 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07002410
Mike Christie30a6c652006-04-06 21:13:39 -05002411 /* host parameters */
2412 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
2413 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
2414 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christie32c6e1b2008-05-21 15:53:58 -05002415 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05002416 transport_container_register(&priv->t.host_attrs);
2417
Mike Christied8196ed2007-05-30 12:57:25 -05002418 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
Mike Christie22236962007-05-30 12:57:24 -05002419 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
Mike Christie1819dc82007-05-30 12:57:08 -05002420 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05002421 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002422 BUG_ON(count > ISCSI_HOST_ATTRS);
2423 priv->host_attrs[count] = NULL;
2424 count = 0;
2425
Alex Aizman0896b752005-08-04 19:33:07 -07002426 /* connection parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07002427 priv->conn_cont.ac.class = &iscsi_connection_class.class;
2428 priv->conn_cont.ac.match = iscsi_conn_match;
Mike Christie3128c6c2011-07-25 13:48:42 -05002429 priv->conn_cont.ac.grp = &iscsi_conn_group;
Alex Aizman0896b752005-08-04 19:33:07 -07002430 transport_container_register(&priv->conn_cont);
2431
Alex Aizman0896b752005-08-04 19:33:07 -07002432 /* session parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07002433 priv->session_cont.ac.class = &iscsi_session_class.class;
2434 priv->session_cont.ac.match = iscsi_session_match;
Mike Christie1d063c12011-07-25 13:48:43 -05002435 priv->session_cont.ac.grp = &iscsi_session_group;
Alex Aizman0896b752005-08-04 19:33:07 -07002436 transport_container_register(&priv->session_cont);
2437
Alex Aizman0896b752005-08-04 19:33:07 -07002438 spin_lock_irqsave(&iscsi_transport_lock, flags);
2439 list_add(&priv->list, &iscsi_transports);
2440 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Alex Aizman0896b752005-08-04 19:33:07 -07002442 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06002443 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Tony Jonesee959b02008-02-22 00:13:36 +01002445unregister_dev:
2446 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05002447 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002448free_priv:
2449 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06002450 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
Alex Aizman0896b752005-08-04 19:33:07 -07002452EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Alex Aizman0896b752005-08-04 19:33:07 -07002454int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
Alex Aizman0896b752005-08-04 19:33:07 -07002456 struct iscsi_internal *priv;
2457 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Alex Aizman0896b752005-08-04 19:33:07 -07002459 BUG_ON(!tt);
2460
Arjan van de Ven0b950672006-01-11 13:16:10 +01002461 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002462
2463 priv = iscsi_if_transport_lookup(tt);
2464 BUG_ON (!priv);
2465
Alex Aizman0896b752005-08-04 19:33:07 -07002466 spin_lock_irqsave(&iscsi_transport_lock, flags);
2467 list_del(&priv->list);
2468 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2469
2470 transport_container_unregister(&priv->conn_cont);
2471 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05002472 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07002473
Tony Jonesee959b02008-02-22 00:13:36 +01002474 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
2475 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002476 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002477
2478 return 0;
2479}
2480EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
2481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482static __init int iscsi_transport_init(void)
2483{
Alex Aizman0896b752005-08-04 19:33:07 -07002484 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Meelis Roos09492602006-12-17 12:10:26 -06002486 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05002487 ISCSI_TRANSPORT_VERSION);
2488
Mike Christie41be1442007-02-28 17:32:18 -06002489 atomic_set(&iscsi_session_nr, 0);
2490
Alex Aizman0896b752005-08-04 19:33:07 -07002491 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if (err)
2493 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07002494
Mike Christied82ff9be2008-05-21 15:54:13 -05002495 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002496 if (err)
2497 goto unregister_transport_class;
2498
Mike Christie8d079132011-07-25 13:48:40 -05002499 err = class_register(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002500 if (err)
2501 goto unregister_endpoint_class;
2502
Mike Christie8d079132011-07-25 13:48:40 -05002503 err = transport_class_register(&iscsi_host_class);
2504 if (err)
2505 goto unregister_iface_class;
2506
Mike Christie30a6c652006-04-06 21:13:39 -05002507 err = transport_class_register(&iscsi_connection_class);
2508 if (err)
2509 goto unregister_host_class;
2510
Alex Aizman0896b752005-08-04 19:33:07 -07002511 err = transport_class_register(&iscsi_session_class);
2512 if (err)
2513 goto unregister_conn_class;
2514
Mike Christied82ff9be2008-05-21 15:54:13 -05002515 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2516 NULL, THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07002517 if (!nls) {
2518 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04002519 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07002520 }
2521
Mike Christied8bf5412007-12-13 12:43:27 -06002522 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2523 if (!iscsi_eh_timer_workq)
2524 goto release_nls;
2525
Mike Christie43a145a2006-10-16 18:09:38 -04002526 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07002527
Mike Christied8bf5412007-12-13 12:43:27 -06002528release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002529 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002530unregister_session_class:
2531 transport_class_unregister(&iscsi_session_class);
2532unregister_conn_class:
2533 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002534unregister_host_class:
2535 transport_class_unregister(&iscsi_host_class);
Mike Christie8d079132011-07-25 13:48:40 -05002536unregister_iface_class:
2537 class_unregister(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002538unregister_endpoint_class:
2539 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002540unregister_transport_class:
2541 class_unregister(&iscsi_transport_class);
2542 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
2544
2545static void __exit iscsi_transport_exit(void)
2546{
Mike Christied8bf5412007-12-13 12:43:27 -06002547 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002548 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002549 transport_class_unregister(&iscsi_connection_class);
2550 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002551 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002552 class_unregister(&iscsi_endpoint_class);
Mike Christie8d079132011-07-25 13:48:40 -05002553 class_unregister(&iscsi_iface_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002554 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
2557module_init(iscsi_transport_init);
2558module_exit(iscsi_transport_exit);
2559
Alex Aizman0896b752005-08-04 19:33:07 -07002560MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2561 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2562 "Alex Aizman <itn780@yahoo.com>");
2563MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05002565MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
Stephen Hemminger058548a2010-12-09 09:37:56 -08002566MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);