blob: a8dd85dc77b11ed47c54b3dbcb238b4aa05081cc [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;
Mike Christieb78dbba2011-07-25 13:48:44 -0500332 int param;
Mike Christie8d079132011-07-25 13:48:40 -0500333
334 if (attr == &dev_attr_iface_enabled.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500335 param = ISCSI_NET_PARAM_IFACE_ENABLE;
Mike Christie8d079132011-07-25 13:48:40 -0500336 else if (attr == &dev_attr_iface_vlan.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500337 param = ISCSI_NET_PARAM_VLAN_ID;
338 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
Mike Christie8d079132011-07-25 13:48:40 -0500339 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500340 param = ISCSI_NET_PARAM_IPV4_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500341 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500342 param = ISCSI_NET_PARAM_IPV4_GW;
Mike Christie8d079132011-07-25 13:48:40 -0500343 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500344 param = ISCSI_NET_PARAM_IPV4_SUBNET;
Mike Christie8d079132011-07-25 13:48:40 -0500345 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500346 param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
347 else
348 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500349 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
350 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500351 param = ISCSI_NET_PARAM_IPV6_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500352 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500353 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
Mike Christie8d079132011-07-25 13:48:40 -0500354 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500355 param = ISCSI_NET_PARAM_IPV6_ROUTER;
Mike Christie8d079132011-07-25 13:48:40 -0500356 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500357 param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
Mike Christie8d079132011-07-25 13:48:40 -0500358 else if (attr == &dev_attr_ipv6_iface_linklocal_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500359 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
360 else
361 return 0;
362 } else {
363 WARN_ONCE(1, "Invalid iface attr");
364 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500365 }
366
Mike Christieb78dbba2011-07-25 13:48:44 -0500367 return t->attr_is_visible(ISCSI_NET_PARAM, param);
Mike Christie8d079132011-07-25 13:48:40 -0500368}
369
370static struct attribute *iscsi_iface_attrs[] = {
371 &dev_attr_iface_enabled.attr,
372 &dev_attr_iface_vlan.attr,
373 &dev_attr_ipv4_iface_ipaddress.attr,
374 &dev_attr_ipv4_iface_gateway.attr,
375 &dev_attr_ipv4_iface_subnet.attr,
376 &dev_attr_ipv4_iface_bootproto.attr,
377 &dev_attr_ipv6_iface_ipaddress.attr,
378 &dev_attr_ipv6_iface_link_local_addr.attr,
379 &dev_attr_ipv6_iface_router_addr.attr,
380 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
381 &dev_attr_ipv6_iface_linklocal_autocfg.attr,
382 NULL,
383};
384
385static struct attribute_group iscsi_iface_group = {
386 .attrs = iscsi_iface_attrs,
387 .is_visible = iscsi_iface_attr_is_visible,
388};
389
390struct iscsi_iface *
391iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
392 uint32_t iface_type, uint32_t iface_num, int dd_size)
393{
394 struct iscsi_iface *iface;
395 int err;
396
397 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
398 if (!iface)
399 return NULL;
400
401 iface->transport = transport;
402 iface->iface_type = iface_type;
403 iface->iface_num = iface_num;
404 iface->dev.release = iscsi_iface_release;
405 iface->dev.class = &iscsi_iface_class;
406 /* parent reference released in iscsi_iface_release */
407 iface->dev.parent = get_device(&shost->shost_gendev);
408 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
409 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
410 iface_num);
411 else
412 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
413 iface_num);
414
415 err = device_register(&iface->dev);
416 if (err)
417 goto free_iface;
418
419 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
420 if (err)
421 goto unreg_iface;
422
423 if (dd_size)
424 iface->dd_data = &iface[1];
425 return iface;
426
427unreg_iface:
428 device_unregister(&iface->dev);
429 return NULL;
430
431free_iface:
432 put_device(iface->dev.parent);
433 kfree(iface);
434 return NULL;
435}
436EXPORT_SYMBOL_GPL(iscsi_create_iface);
437
438void iscsi_destroy_iface(struct iscsi_iface *iface)
439{
440 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
441 device_unregister(&iface->dev);
442}
443EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
444
Mike Christie30a6c652006-04-06 21:13:39 -0500445static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100446 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500447{
448 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500449 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500450
451 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -0600452 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -0500453 mutex_init(&ihost->mutex);
Mike Christie30a6c652006-04-06 21:13:39 -0500454 return 0;
455}
456
457static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
458 "iscsi_host",
459 iscsi_setup_host,
Mike Christie06d25af2009-03-05 14:46:02 -0600460 NULL,
Mike Christie30a6c652006-04-06 21:13:39 -0500461 NULL);
462
Alex Aizman0896b752005-08-04 19:33:07 -0700463static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
464 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 NULL,
466 NULL,
467 NULL);
468
Alex Aizman0896b752005-08-04 19:33:07 -0700469static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
470 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 NULL,
472 NULL,
473 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700474
475static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100476static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700477
Mike Christie7b7232f2006-02-01 21:06:49 -0600478static LIST_HEAD(sesslist);
479static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700480static LIST_HEAD(connlist);
481static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Mike Christieb5c7a122006-04-06 21:13:33 -0500483static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
484{
485 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
486 return sess->sid;
487}
488
489/*
490 * Returns the matching session to a given sid
491 */
492static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600493{
494 unsigned long flags;
495 struct iscsi_cls_session *sess;
496
497 spin_lock_irqsave(&sesslock, flags);
498 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500499 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600500 spin_unlock_irqrestore(&sesslock, flags);
501 return sess;
502 }
503 }
504 spin_unlock_irqrestore(&sesslock, flags);
505 return NULL;
506}
507
Mike Christieb5c7a122006-04-06 21:13:33 -0500508/*
509 * Returns the matching connection to a given sid / cid tuple
510 */
511static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600512{
513 unsigned long flags;
514 struct iscsi_cls_conn *conn;
515
516 spin_lock_irqsave(&connlock, flags);
517 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500518 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600519 spin_unlock_irqrestore(&connlock, flags);
520 return conn;
521 }
522 }
523 spin_unlock_irqrestore(&connlock, flags);
524 return NULL;
525}
526
Mike Christie7b8631b2006-01-13 18:05:50 -0600527/*
528 * The following functions can be used by LLDs that allocate
529 * their own scsi_hosts or by software iscsi LLDs
530 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600531static struct {
532 int value;
533 char *name;
534} iscsi_session_state_names[] = {
535 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
536 { ISCSI_SESSION_FAILED, "FAILED" },
537 { ISCSI_SESSION_FREE, "FREE" },
538};
539
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200540static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600541{
542 int i;
543 char *name = NULL;
544
545 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
546 if (iscsi_session_state_names[i].value == state) {
547 name = iscsi_session_state_names[i].name;
548 break;
549 }
550 }
551 return name;
552}
553
554int iscsi_session_chkready(struct iscsi_cls_session *session)
555{
556 unsigned long flags;
557 int err;
558
559 spin_lock_irqsave(&session->lock, flags);
560 switch (session->state) {
561 case ISCSI_SESSION_LOGGED_IN:
562 err = 0;
563 break;
564 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -0500565 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600566 break;
567 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500568 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600569 break;
570 default:
571 err = DID_NO_CONNECT << 16;
572 break;
573 }
574 spin_unlock_irqrestore(&session->lock, flags);
575 return err;
576}
577EXPORT_SYMBOL_GPL(iscsi_session_chkready);
578
Mike Christie7b8631b2006-01-13 18:05:50 -0600579static void iscsi_session_release(struct device *dev)
580{
581 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600582 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Mike Christie7b8631b2006-01-13 18:05:50 -0600584 shost = iscsi_session_to_shost(session);
585 scsi_host_put(shost);
Mike Christie632248a2009-08-20 15:11:01 -0500586 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600587 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600588}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Mike Christie7b8631b2006-01-13 18:05:50 -0600590static int iscsi_is_session_dev(const struct device *dev)
591{
592 return dev->release == iscsi_session_release;
593}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Mike Christiea4804cd2008-05-21 15:54:00 -0500595static int iscsi_iter_session_fn(struct device *dev, void *data)
596{
597 void (* fn) (struct iscsi_cls_session *) = data;
598
599 if (!iscsi_is_session_dev(dev))
600 return 0;
601 fn(iscsi_dev_to_session(dev));
602 return 0;
603}
604
605void iscsi_host_for_each_session(struct Scsi_Host *shost,
606 void (*fn)(struct iscsi_cls_session *))
607{
608 device_for_each_child(&shost->shost_gendev, fn,
609 iscsi_iter_session_fn);
610}
611EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
612
Mike Christie8aae18a2008-01-31 13:36:48 -0600613/**
614 * iscsi_scan_finished - helper to report when running scans are done
615 * @shost: scsi host
616 * @time: scan run time
617 *
618 * This function can be used by drives like qla4xxx to report to the scsi
619 * layer when the scans it kicked off at module load time are done.
620 */
621int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
622{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500623 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600624 /*
625 * qla4xxx will have kicked off some session unblocks before calling
626 * scsi_scan_host, so just wait for them to complete.
627 */
628 return !atomic_read(&ihost->nr_scans);
629}
630EXPORT_SYMBOL_GPL(iscsi_scan_finished);
631
Mike Christie79706342008-05-21 15:54:12 -0500632struct iscsi_scan_data {
633 unsigned int channel;
634 unsigned int id;
635 unsigned int lun;
636};
637
638static int iscsi_user_scan_session(struct device *dev, void *data)
639{
640 struct iscsi_scan_data *scan_data = data;
641 struct iscsi_cls_session *session;
642 struct Scsi_Host *shost;
643 struct iscsi_cls_host *ihost;
644 unsigned long flags;
645 unsigned int id;
646
647 if (!iscsi_is_session_dev(dev))
648 return 0;
649
650 session = iscsi_dev_to_session(dev);
Mike Christie632248a2009-08-20 15:11:01 -0500651
652 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
653
Mike Christie79706342008-05-21 15:54:12 -0500654 shost = iscsi_session_to_shost(session);
655 ihost = shost->shost_data;
656
657 mutex_lock(&ihost->mutex);
658 spin_lock_irqsave(&session->lock, flags);
659 if (session->state != ISCSI_SESSION_LOGGED_IN) {
660 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie632248a2009-08-20 15:11:01 -0500661 goto user_scan_exit;
Mike Christie79706342008-05-21 15:54:12 -0500662 }
663 id = session->target_id;
664 spin_unlock_irqrestore(&session->lock, flags);
665
666 if (id != ISCSI_MAX_TARGET) {
667 if ((scan_data->channel == SCAN_WILD_CARD ||
668 scan_data->channel == 0) &&
669 (scan_data->id == SCAN_WILD_CARD ||
670 scan_data->id == id))
671 scsi_scan_target(&session->dev, 0, id,
672 scan_data->lun, 1);
673 }
Mike Christie632248a2009-08-20 15:11:01 -0500674
675user_scan_exit:
Mike Christie79706342008-05-21 15:54:12 -0500676 mutex_unlock(&ihost->mutex);
Mike Christie632248a2009-08-20 15:11:01 -0500677 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
Mike Christie79706342008-05-21 15:54:12 -0500678 return 0;
679}
680
Mike Christie30a6c652006-04-06 21:13:39 -0500681static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
682 uint id, uint lun)
683{
Mike Christie79706342008-05-21 15:54:12 -0500684 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500685
Mike Christie79706342008-05-21 15:54:12 -0500686 scan_data.channel = channel;
687 scan_data.id = id;
688 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -0500689
Mike Christie79706342008-05-21 15:54:12 -0500690 return device_for_each_child(&shost->shost_gendev, &scan_data,
691 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -0500692}
693
Mike Christiebd976f62008-01-31 13:36:46 -0600694static void iscsi_scan_session(struct work_struct *work)
695{
696 struct iscsi_cls_session *session =
697 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600698 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500699 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500700 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600701
Mike Christie79706342008-05-21 15:54:12 -0500702 scan_data.channel = 0;
703 scan_data.id = SCAN_WILD_CARD;
704 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -0600705
Mike Christie79706342008-05-21 15:54:12 -0500706 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -0600707 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600708}
709
Mike Christiec01be6d2010-07-22 16:59:49 +0530710/**
711 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
Randy Dunlape6d4ef42010-08-14 13:05:41 -0700712 * @cmd: scsi cmd passed to scsi eh handler
Mike Christiec01be6d2010-07-22 16:59:49 +0530713 *
714 * If the session is down this function will wait for the recovery
715 * timer to fire or for the session to be logged back in. If the
716 * recovery timer fires then FAST_IO_FAIL is returned. The caller
717 * should pass this error value to the scsi eh.
718 */
719int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
720{
721 struct iscsi_cls_session *session =
722 starget_to_session(scsi_target(cmd->device));
723 unsigned long flags;
724 int ret = 0;
725
726 spin_lock_irqsave(&session->lock, flags);
727 while (session->state != ISCSI_SESSION_LOGGED_IN) {
728 if (session->state == ISCSI_SESSION_FREE) {
729 ret = FAST_IO_FAIL;
730 break;
731 }
732 spin_unlock_irqrestore(&session->lock, flags);
733 msleep(1000);
734 spin_lock_irqsave(&session->lock, flags);
735 }
736 spin_unlock_irqrestore(&session->lock, flags);
737 return ret;
738}
739EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
740
David Howellsc4028952006-11-22 14:57:56 +0000741static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500742{
David Howellsc4028952006-11-22 14:57:56 +0000743 struct iscsi_cls_session *session =
744 container_of(work, struct iscsi_cls_session,
745 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600746 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500747
Mike Christie322d7392008-01-31 13:36:52 -0600748 iscsi_cls_session_printk(KERN_INFO, session,
749 "session recovery timed out after %d secs\n",
750 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500751
Mike Christie6eabafb2008-01-31 13:36:43 -0600752 spin_lock_irqsave(&session->lock, flags);
753 switch (session->state) {
754 case ISCSI_SESSION_FAILED:
755 session->state = ISCSI_SESSION_FREE;
756 break;
757 case ISCSI_SESSION_LOGGED_IN:
758 case ISCSI_SESSION_FREE:
759 /* we raced with the unblock's flush */
760 spin_unlock_irqrestore(&session->lock, flags);
761 return;
762 }
763 spin_unlock_irqrestore(&session->lock, flags);
764
Mike Christie30a6c652006-04-06 21:13:39 -0500765 if (session->transport->session_recovery_timedout)
766 session->transport->session_recovery_timedout(session);
767
Mike Christie632248a2009-08-20 15:11:01 -0500768 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500769 scsi_target_unblock(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500770 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500771}
772
Mike Christie45ab33b2008-03-04 13:26:55 -0600773static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500774{
Mike Christie45ab33b2008-03-04 13:26:55 -0600775 struct iscsi_cls_session *session =
776 container_of(work, struct iscsi_cls_session,
777 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600778 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500779 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600780 unsigned long flags;
781
Mike Christie632248a2009-08-20 15:11:01 -0500782 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
Mike Christie45ab33b2008-03-04 13:26:55 -0600783 /*
784 * The recovery and unblock work get run from the same workqueue,
785 * so try to cancel it if it was going to run after this unblock.
786 */
787 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600788 spin_lock_irqsave(&session->lock, flags);
789 session->state = ISCSI_SESSION_LOGGED_IN;
790 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600791 /* start IO */
792 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600793 /*
794 * Only do kernel scanning if the driver is properly hooked into
795 * the async scanning code (drivers like iscsi_tcp do login and
796 * scanning from userspace).
797 */
798 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -0600799 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -0600800 atomic_inc(&ihost->nr_scans);
801 }
Mike Christie632248a2009-08-20 15:11:01 -0500802 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600803}
Mike Christie45ab33b2008-03-04 13:26:55 -0600804
805/**
806 * iscsi_unblock_session - set a session as logged in and start IO.
807 * @session: iscsi session
808 *
809 * Mark a session as ready to accept IO.
810 */
811void iscsi_unblock_session(struct iscsi_cls_session *session)
812{
813 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
814 /*
815 * make sure all the events have completed before tell the driver
816 * it is safe
817 */
818 flush_workqueue(iscsi_eh_timer_workq);
819}
Mike Christie30a6c652006-04-06 21:13:39 -0500820EXPORT_SYMBOL_GPL(iscsi_unblock_session);
821
Mike Christie45ab33b2008-03-04 13:26:55 -0600822static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500823{
Mike Christie45ab33b2008-03-04 13:26:55 -0600824 struct iscsi_cls_session *session =
825 container_of(work, struct iscsi_cls_session,
826 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600827 unsigned long flags;
828
Mike Christie632248a2009-08-20 15:11:01 -0500829 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600830 spin_lock_irqsave(&session->lock, flags);
831 session->state = ISCSI_SESSION_FAILED;
832 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500833 scsi_target_block(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500834 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
Mike Christiefdd46dc2009-11-11 16:34:34 -0600835 if (session->recovery_tmo >= 0)
836 queue_delayed_work(iscsi_eh_timer_workq,
837 &session->recovery_work,
838 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500839}
Mike Christie45ab33b2008-03-04 13:26:55 -0600840
841void iscsi_block_session(struct iscsi_cls_session *session)
842{
843 queue_work(iscsi_eh_timer_workq, &session->block_work);
844}
Mike Christie30a6c652006-04-06 21:13:39 -0500845EXPORT_SYMBOL_GPL(iscsi_block_session);
846
Mike Christie26974782007-12-13 12:43:29 -0600847static void __iscsi_unbind_session(struct work_struct *work)
848{
849 struct iscsi_cls_session *session =
850 container_of(work, struct iscsi_cls_session,
851 unbind_work);
852 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500853 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500854 unsigned long flags;
Mike Christie26974782007-12-13 12:43:29 -0600855
Mike Christie632248a2009-08-20 15:11:01 -0500856 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
857
Mike Christie26974782007-12-13 12:43:29 -0600858 /* Prevent new scans and make sure scanning is not in progress */
859 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -0500860 spin_lock_irqsave(&session->lock, flags);
861 if (session->target_id == ISCSI_MAX_TARGET) {
862 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600863 mutex_unlock(&ihost->mutex);
864 return;
865 }
Mike Christie79706342008-05-21 15:54:12 -0500866 session->target_id = ISCSI_MAX_TARGET;
867 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600868 mutex_unlock(&ihost->mutex);
869
870 scsi_remove_target(&session->dev);
871 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -0500872 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
Mike Christie26974782007-12-13 12:43:29 -0600873}
874
Mike Christie7b8631b2006-01-13 18:05:50 -0600875struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500876iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
877 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600878{
879 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Mike Christie5d91e202008-05-21 15:54:01 -0500881 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -0500882 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600883 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500884 return NULL;
885
Mike Christie7b8631b2006-01-13 18:05:50 -0600886 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500887 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -0600888 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +0000889 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500890 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -0600891 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
892 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -0600893 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -0600894 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -0600895 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -0600896
Mike Christie6a8a0d32006-06-28 12:00:31 -0500897 /* this is released in the dev's release function */
898 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500899 session->dev.parent = &shost->shost_gendev;
900 session->dev.release = iscsi_session_release;
901 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -0500902 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -0500903 session->dd_data = &session[1];
Mike Christie632248a2009-08-20 15:11:01 -0500904
905 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500906 return session;
907}
908EXPORT_SYMBOL_GPL(iscsi_alloc_session);
909
Mike Christie79706342008-05-21 15:54:12 -0500910static int iscsi_get_next_target_id(struct device *dev, void *data)
911{
912 struct iscsi_cls_session *session;
913 unsigned long flags;
914 int err = 0;
915
916 if (!iscsi_is_session_dev(dev))
917 return 0;
918
919 session = iscsi_dev_to_session(dev);
920 spin_lock_irqsave(&session->lock, flags);
921 if (*((unsigned int *) data) == session->target_id)
922 err = -EEXIST;
923 spin_unlock_irqrestore(&session->lock, flags);
924 return err;
925}
926
Mike Christie6a8a0d32006-06-28 12:00:31 -0500927int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500928{
929 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500930 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600931 unsigned long flags;
Mike Christie79706342008-05-21 15:54:12 -0500932 unsigned int id = target_id;
Mike Christie8434aa82006-06-28 12:00:30 -0500933 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500934
Mike Christie30a6c652006-04-06 21:13:39 -0500935 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600936 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -0500937
938 if (id == ISCSI_MAX_TARGET) {
939 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
940 err = device_for_each_child(&shost->shost_gendev, &id,
941 iscsi_get_next_target_id);
942 if (!err)
943 break;
944 }
945
946 if (id == ISCSI_MAX_TARGET) {
947 iscsi_cls_session_printk(KERN_ERR, session,
948 "Too many iscsi targets. Max "
949 "number of targets is %d.\n",
950 ISCSI_MAX_TARGET - 1);
Jaswinder Singh Rajput24add1c2009-06-20 13:29:21 +0530951 err = -EOVERFLOW;
Mike Christie79706342008-05-21 15:54:12 -0500952 goto release_host;
953 }
954 }
955 session->target_id = id;
Mike Christie30a6c652006-04-06 21:13:39 -0500956
Kay Sievers71610f52008-12-03 22:41:36 +0100957 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500958 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600959 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600960 iscsi_cls_session_printk(KERN_ERR, session,
961 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500962 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600963 }
964 transport_register_device(&session->dev);
965
Mike Christie26974782007-12-13 12:43:29 -0600966 spin_lock_irqsave(&sesslock, flags);
967 list_add(&session->sess_list, &sesslist);
968 spin_unlock_irqrestore(&sesslock, flags);
969
Mike Christie26974782007-12-13 12:43:29 -0600970 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -0500971 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500972 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500973
Mike Christie8434aa82006-06-28 12:00:30 -0500974release_host:
975 scsi_host_put(shost);
976 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600977}
Mike Christie8434aa82006-06-28 12:00:30 -0500978EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600979
980/**
Mike Christie8434aa82006-06-28 12:00:30 -0500981 * iscsi_create_session - create iscsi class session
982 * @shost: scsi host
983 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -0500984 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -0500985 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600986 *
Mike Christie8434aa82006-06-28 12:00:30 -0500987 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500988 */
Mike Christie8434aa82006-06-28 12:00:30 -0500989struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500990iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
991 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500992{
993 struct iscsi_cls_session *session;
994
Mike Christie5d91e202008-05-21 15:54:01 -0500995 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -0500996 if (!session)
997 return NULL;
998
Mike Christie6a8a0d32006-06-28 12:00:31 -0500999 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -05001000 iscsi_free_session(session);
1001 return NULL;
1002 }
1003 return session;
1004}
1005EXPORT_SYMBOL_GPL(iscsi_create_session);
1006
Mike Christie26974782007-12-13 12:43:29 -06001007static void iscsi_conn_release(struct device *dev)
1008{
1009 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
1010 struct device *parent = conn->dev.parent;
1011
Mike Christie632248a2009-08-20 15:11:01 -05001012 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
Mike Christie26974782007-12-13 12:43:29 -06001013 kfree(conn);
1014 put_device(parent);
1015}
1016
1017static int iscsi_is_conn_dev(const struct device *dev)
1018{
1019 return dev->release == iscsi_conn_release;
1020}
1021
1022static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
1023{
1024 if (!iscsi_is_conn_dev(dev))
1025 return 0;
1026 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
1027}
1028
Mike Christie8434aa82006-06-28 12:00:30 -05001029void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001030{
Mike Christie30a6c652006-04-06 21:13:39 -05001031 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie26974782007-12-13 12:43:29 -06001032 unsigned long flags;
1033 int err;
Mike Christie30a6c652006-04-06 21:13:39 -05001034
Mike Christie632248a2009-08-20 15:11:01 -05001035 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
1036
Mike Christie26974782007-12-13 12:43:29 -06001037 spin_lock_irqsave(&sesslock, flags);
1038 list_del(&session->sess_list);
1039 spin_unlock_irqrestore(&sesslock, flags);
1040
Mike Christie45ab33b2008-03-04 13:26:55 -06001041 /* make sure there are no blocks/unblocks queued */
1042 flush_workqueue(iscsi_eh_timer_workq);
1043 /* make sure the timedout callout is not running */
1044 if (!cancel_delayed_work(&session->recovery_work))
1045 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -06001046 /*
1047 * If we are blocked let commands flow again. The lld or iscsi
1048 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -06001049 * We assume that LLD will not be calling block/unblock while
1050 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -06001051 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001052 spin_lock_irqsave(&session->lock, flags);
1053 session->state = ISCSI_SESSION_FREE;
1054 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -06001055
Mike Christie45ab33b2008-03-04 13:26:55 -06001056 scsi_target_unblock(&session->dev);
1057 /* flush running scans then delete devices */
Mike Christie06d25af2009-03-05 14:46:02 -06001058 scsi_flush_work(shost);
Mike Christie45ab33b2008-03-04 13:26:55 -06001059 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -05001060
Mike Christie26974782007-12-13 12:43:29 -06001061 /* hw iscsi may not have removed all connections from session */
1062 err = device_for_each_child(&session->dev, NULL,
1063 iscsi_iter_destroy_conn_fn);
1064 if (err)
Mike Christie322d7392008-01-31 13:36:52 -06001065 iscsi_cls_session_printk(KERN_ERR, session,
1066 "Could not delete all connections "
1067 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -05001068
Mike Christie7b8631b2006-01-13 18:05:50 -06001069 transport_unregister_device(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001070
1071 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001072 device_del(&session->dev);
1073}
1074EXPORT_SYMBOL_GPL(iscsi_remove_session);
1075
1076void iscsi_free_session(struct iscsi_cls_session *session)
1077{
Mike Christie632248a2009-08-20 15:11:01 -05001078 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
Mike Christie26974782007-12-13 12:43:29 -06001079 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -05001080 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001081}
Mike Christie8434aa82006-06-28 12:00:30 -05001082EXPORT_SYMBOL_GPL(iscsi_free_session);
1083
1084/**
1085 * iscsi_destroy_session - destroy iscsi session
1086 * @session: iscsi_session
1087 *
1088 * Can be called by a LLD or iscsi_transport. There must not be
1089 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -05001090 */
Mike Christie8434aa82006-06-28 12:00:30 -05001091int iscsi_destroy_session(struct iscsi_cls_session *session)
1092{
1093 iscsi_remove_session(session);
Mike Christie632248a2009-08-20 15:11:01 -05001094 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001095 iscsi_free_session(session);
1096 return 0;
1097}
Mike Christie7b8631b2006-01-13 18:05:50 -06001098EXPORT_SYMBOL_GPL(iscsi_destroy_session);
1099
Mike Christie7b8631b2006-01-13 18:05:50 -06001100/**
1101 * iscsi_create_conn - create iscsi class connection
1102 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -05001103 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -06001104 * @cid: connection id
1105 *
1106 * This can be called from a LLD or iscsi_transport. The connection
1107 * is child of the session so cid must be unique for all connections
1108 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -05001109 *
1110 * Since we do not support MCS, cid will normally be zero. In some cases
1111 * for software iscsi we could be trying to preallocate a connection struct
1112 * in which case there could be two connection structs and cid would be
1113 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -05001114 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001115struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05001116iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -06001117{
1118 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -06001119 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -06001120 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -06001121 int err;
1122
Mike Christie5d91e202008-05-21 15:54:01 -05001123 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001124 if (!conn)
1125 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -05001126 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06001127 conn->dd_data = &conn[1];
1128
Mike Christie22a39fb2011-02-16 15:04:33 -06001129 mutex_init(&conn->ep_mutex);
Mike Christie7b8631b2006-01-13 18:05:50 -06001130 INIT_LIST_HEAD(&conn->conn_list);
1131 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -05001132 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001133
1134 /* this is released in the dev's release function */
1135 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -04001136 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -05001137
Kay Sievers71610f52008-12-03 22:41:36 +01001138 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001139 conn->dev.parent = &session->dev;
1140 conn->dev.release = iscsi_conn_release;
1141 err = device_register(&conn->dev);
1142 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001143 iscsi_cls_session_printk(KERN_ERR, session, "could not "
1144 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001145 goto release_parent_ref;
1146 }
1147 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -06001148
1149 spin_lock_irqsave(&connlock, flags);
1150 list_add(&conn->conn_list, &connlist);
Mike Christie26974782007-12-13 12:43:29 -06001151 spin_unlock_irqrestore(&connlock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05001152
1153 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001154 return conn;
1155
1156release_parent_ref:
1157 put_device(&session->dev);
1158free_conn:
1159 kfree(conn);
1160 return NULL;
1161}
1162
1163EXPORT_SYMBOL_GPL(iscsi_create_conn);
1164
1165/**
1166 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -05001167 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -06001168 *
Mike Christie26974782007-12-13 12:43:29 -06001169 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001170 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001171int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1172{
Mike Christie26974782007-12-13 12:43:29 -06001173 unsigned long flags;
1174
1175 spin_lock_irqsave(&connlock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001176 list_del(&conn->conn_list);
1177 spin_unlock_irqrestore(&connlock, flags);
1178
Mike Christie7b8631b2006-01-13 18:05:50 -06001179 transport_unregister_device(&conn->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001180 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001181 device_unregister(&conn->dev);
1182 return 0;
1183}
Mike Christie7b8631b2006-01-13 18:05:50 -06001184EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1185
1186/*
Mike Christie7b8631b2006-01-13 18:05:50 -06001187 * iscsi interface functions
1188 */
Alex Aizman0896b752005-08-04 19:33:07 -07001189static struct iscsi_internal *
1190iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Alex Aizman0896b752005-08-04 19:33:07 -07001192 struct iscsi_internal *priv;
1193 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Alex Aizman0896b752005-08-04 19:33:07 -07001195 spin_lock_irqsave(&iscsi_transport_lock, flags);
1196 list_for_each_entry(priv, &iscsi_transports, list) {
1197 if (tt == priv->iscsi_transport) {
1198 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1199 return priv;
1200 }
1201 }
1202 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1203 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Alex Aizman0896b752005-08-04 19:33:07 -07001206static int
Michael Chan43514772009-06-08 18:14:41 -07001207iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -05001208{
Michael Chan43514772009-06-08 18:14:41 -07001209 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -07001210}
1211
Mike Christie7b7232f2006-02-01 21:06:49 -06001212int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -07001213 char *data, uint32_t data_size)
1214{
1215 struct nlmsghdr *nlh;
1216 struct sk_buff *skb;
1217 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07001218 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05001219 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001220 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1221 data_size);
1222
Mike Christie790f39a2006-05-18 20:31:39 -05001223 priv = iscsi_if_transport_lookup(conn->transport);
1224 if (!priv)
1225 return -EINVAL;
1226
Mike Christie43a145a2006-10-16 18:09:38 -04001227 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001228 if (!skb) {
Mike Christiee5bd7b52008-09-24 11:46:10 -05001229 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06001230 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1231 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001232 return -ENOMEM;
1233 }
1234
Michael Chan43514772009-06-08 18:14:41 -07001235 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001236 ev = NLMSG_DATA(nlh);
1237 memset(ev, 0, sizeof(*ev));
1238 ev->transport_handle = iscsi_handle(conn->transport);
1239 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05001240 ev->r.recv_req.cid = conn->cid;
1241 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001242 pdu = (char*)ev + sizeof(*ev);
1243 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1244 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1245
Michael Chan43514772009-06-08 18:14:41 -07001246 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001247}
1248EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1249
Michael Chan43514772009-06-08 18:14:41 -07001250int iscsi_offload_mesg(struct Scsi_Host *shost,
1251 struct iscsi_transport *transport, uint32_t type,
1252 char *data, uint16_t data_size)
1253{
1254 struct nlmsghdr *nlh;
1255 struct sk_buff *skb;
1256 struct iscsi_uevent *ev;
1257 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
1258
Michael Chana541f842009-07-29 08:49:52 +00001259 skb = alloc_skb(len, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001260 if (!skb) {
1261 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
1262 return -ENOMEM;
1263 }
1264
1265 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1266 ev = NLMSG_DATA(nlh);
1267 memset(ev, 0, sizeof(*ev));
1268 ev->type = type;
1269 ev->transport_handle = iscsi_handle(transport);
1270 switch (type) {
1271 case ISCSI_KEVENT_PATH_REQ:
1272 ev->r.req_path.host_no = shost->host_no;
1273 break;
1274 case ISCSI_KEVENT_IF_DOWN:
1275 ev->r.notify_if_down.host_no = shost->host_no;
1276 break;
1277 }
1278
1279 memcpy((char *)ev + sizeof(*ev), data, data_size);
1280
Michael Chana541f842009-07-29 08:49:52 +00001281 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001282}
1283EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1284
Mike Christiee5bd7b52008-09-24 11:46:10 -05001285void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07001286{
1287 struct nlmsghdr *nlh;
1288 struct sk_buff *skb;
1289 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05001290 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001291 int len = NLMSG_SPACE(sizeof(*ev));
1292
Mike Christie790f39a2006-05-18 20:31:39 -05001293 priv = iscsi_if_transport_lookup(conn->transport);
1294 if (!priv)
1295 return;
1296
Mike Christie43a145a2006-10-16 18:09:38 -04001297 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001298 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001299 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1300 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07001301 return;
1302 }
1303
Michael Chan43514772009-06-08 18:14:41 -07001304 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001305 ev = NLMSG_DATA(nlh);
1306 ev->transport_handle = iscsi_handle(conn->transport);
1307 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07001308 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05001309 ev->r.connerror.cid = conn->cid;
1310 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001311
Michael Chan43514772009-06-08 18:14:41 -07001312 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001313
Mike Christie322d7392008-01-31 13:36:52 -06001314 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1315 error);
Alex Aizman0896b752005-08-04 19:33:07 -07001316}
Mike Christiee5bd7b52008-09-24 11:46:10 -05001317EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07001318
1319static int
Michael Chan43514772009-06-08 18:14:41 -07001320iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1321 void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07001322{
1323 struct sk_buff *skb;
1324 struct nlmsghdr *nlh;
1325 int len = NLMSG_SPACE(size);
1326 int flags = multi ? NLM_F_MULTI : 0;
1327 int t = done ? NLMSG_DONE : type;
1328
Mike Christie43a145a2006-10-16 18:09:38 -04001329 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05001330 if (!skb) {
1331 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1332 return -ENOMEM;
1333 }
Alex Aizman0896b752005-08-04 19:33:07 -07001334
Michael Chan43514772009-06-08 18:14:41 -07001335 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001336 nlh->nlmsg_flags = flags;
1337 memcpy(NLMSG_DATA(nlh), payload, size);
Michael Chan43514772009-06-08 18:14:41 -07001338 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001339}
1340
1341static int
Mike Christie5b940ad2006-02-01 21:06:56 -06001342iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07001343{
1344 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1345 struct iscsi_stats *stats;
1346 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06001347 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001348 struct nlmsghdr *nlhstat;
1349 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05001350 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001351 int len = NLMSG_SPACE(sizeof(*ev) +
1352 sizeof(struct iscsi_stats) +
1353 sizeof(struct iscsi_stats_custom) *
1354 ISCSI_STATS_CUSTOM_MAX);
1355 int err = 0;
1356
Mike Christie790f39a2006-05-18 20:31:39 -05001357 priv = iscsi_if_transport_lookup(transport);
1358 if (!priv)
1359 return -EINVAL;
1360
Mike Christieb5c7a122006-04-06 21:13:33 -05001361 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07001362 if (!conn)
1363 return -EEXIST;
1364
1365 do {
1366 int actual_size;
1367
Mike Christie43a145a2006-10-16 18:09:38 -04001368 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001369 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06001370 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1371 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001372 return -ENOMEM;
1373 }
1374
Michael Chan43514772009-06-08 18:14:41 -07001375 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07001376 (len - sizeof(*nlhstat)), 0);
1377 evstat = NLMSG_DATA(nlhstat);
1378 memset(evstat, 0, sizeof(*evstat));
1379 evstat->transport_handle = iscsi_handle(conn->transport);
1380 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05001381 evstat->u.get_stats.cid =
1382 ev->u.get_stats.cid;
1383 evstat->u.get_stats.sid =
1384 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07001385 stats = (struct iscsi_stats *)
1386 ((char*)evstat + sizeof(*evstat));
1387 memset(stats, 0, sizeof(*stats));
1388
Mike Christie7b7232f2006-02-01 21:06:49 -06001389 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07001390 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1391 sizeof(struct iscsi_stats) +
1392 sizeof(struct iscsi_stats_custom) *
1393 stats->custom_length);
1394 actual_size -= sizeof(*nlhstat);
1395 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06001396 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07001397 nlhstat->nlmsg_len = actual_size;
1398
Michael Chan43514772009-06-08 18:14:41 -07001399 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1400 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001401 } while (err < 0 && err != -ECONNREFUSED);
1402
1403 return err;
1404}
1405
Mike Christie53cb8a12006-06-28 12:00:32 -05001406/**
Mike Christie26974782007-12-13 12:43:29 -06001407 * iscsi_session_event - send session destr. completion event
1408 * @session: iscsi class session
1409 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05001410 */
Mike Christie26974782007-12-13 12:43:29 -06001411int iscsi_session_event(struct iscsi_cls_session *session,
1412 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05001413{
1414 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05001415 struct Scsi_Host *shost;
1416 struct iscsi_uevent *ev;
1417 struct sk_buff *skb;
1418 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05001419 int rc, len = NLMSG_SPACE(sizeof(*ev));
1420
Mike Christie26974782007-12-13 12:43:29 -06001421 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05001422 if (!priv)
1423 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05001424 shost = iscsi_session_to_shost(session);
1425
Mike Christie43a145a2006-10-16 18:09:38 -04001426 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001427 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001428 iscsi_cls_session_printk(KERN_ERR, session,
1429 "Cannot notify userspace of session "
1430 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001431 return -ENOMEM;
1432 }
1433
Michael Chan43514772009-06-08 18:14:41 -07001434 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Mike Christie53cb8a12006-06-28 12:00:32 -05001435 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06001436 ev->transport_handle = iscsi_handle(session->transport);
1437
1438 ev->type = event;
1439 switch (event) {
1440 case ISCSI_KEVENT_DESTROY_SESSION:
1441 ev->r.d_session.host_no = shost->host_no;
1442 ev->r.d_session.sid = session->sid;
1443 break;
1444 case ISCSI_KEVENT_CREATE_SESSION:
1445 ev->r.c_session_ret.host_no = shost->host_no;
1446 ev->r.c_session_ret.sid = session->sid;
1447 break;
1448 case ISCSI_KEVENT_UNBIND_SESSION:
1449 ev->r.unbind_session.host_no = shost->host_no;
1450 ev->r.unbind_session.sid = session->sid;
1451 break;
1452 default:
Mike Christie322d7392008-01-31 13:36:52 -06001453 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1454 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06001455 kfree_skb(skb);
1456 return -EINVAL;
1457 }
Mike Christie53cb8a12006-06-28 12:00:32 -05001458
1459 /*
1460 * this will occur if the daemon is not up, so we just warn
1461 * the user and when the daemon is restarted it will handle it
1462 */
Michael Chan43514772009-06-08 18:14:41 -07001463 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001464 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06001465 iscsi_cls_session_printk(KERN_ERR, session,
1466 "Cannot notify userspace of session "
1467 "event %u. Check iscsi daemon\n",
1468 event);
Mike Christie632248a2009-08-20 15:11:01 -05001469
1470 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
1471 event, rc);
Mike Christie53cb8a12006-06-28 12:00:32 -05001472 return rc;
1473}
Mike Christie26974782007-12-13 12:43:29 -06001474EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001475
Alex Aizman0896b752005-08-04 19:33:07 -07001476static int
Mike Christied82ff9be2008-05-21 15:54:13 -05001477iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1478 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
Mike Christie40753ca2008-05-21 15:53:56 -05001479 uint16_t cmds_max, uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001480{
1481 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001482 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06001483 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06001484
Mike Christied82ff9be2008-05-21 15:54:13 -05001485 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06001486 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06001487 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001488 return -ENOMEM;
1489
Mike Christie5e7facb2009-03-05 14:46:06 -06001490 shost = iscsi_session_to_shost(session);
1491 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001492 ev->r.c_session_ret.sid = session->sid;
Mike Christie632248a2009-08-20 15:11:01 -05001493 ISCSI_DBG_TRANS_SESSION(session,
1494 "Completed creating transport session\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001495 return 0;
1496}
1497
1498static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001499iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001500{
Mike Christie7b8631b2006-01-13 18:05:50 -06001501 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001502 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001503
Mike Christieb5c7a122006-04-06 21:13:33 -05001504 session = iscsi_session_lookup(ev->u.c_conn.sid);
1505 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001506 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001507 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001508 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001509 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001510
Mike Christie7b7232f2006-02-01 21:06:49 -06001511 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001512 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001513 iscsi_cls_session_printk(KERN_ERR, session,
1514 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001515 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001516 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001517
Mike Christieb5c7a122006-04-06 21:13:33 -05001518 ev->r.c_conn_ret.sid = session->sid;
1519 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie632248a2009-08-20 15:11:01 -05001520
1521 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001522 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001523}
1524
1525static int
1526iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1527{
Mike Christie7b8631b2006-01-13 18:05:50 -06001528 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001529
Mike Christieb5c7a122006-04-06 21:13:33 -05001530 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001531 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001532 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001533
Mike Christie632248a2009-08-20 15:11:01 -05001534 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001535 if (transport->destroy_conn)
1536 transport->destroy_conn(conn);
Mike Christie632248a2009-08-20 15:11:01 -05001537
Mike Christie7b8631b2006-01-13 18:05:50 -06001538 return 0;
1539}
1540
Mike Christiefd7255f2006-04-06 21:13:36 -05001541static int
1542iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1543{
1544 char *data = (char*)ev + sizeof(*ev);
1545 struct iscsi_cls_conn *conn;
1546 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001547 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001548
1549 session = iscsi_session_lookup(ev->u.set_param.sid);
1550 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1551 if (!conn || !session)
1552 return -EINVAL;
1553
1554 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001555 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001556 sscanf(data, "%d", &value);
Mike Christiefdd46dc2009-11-11 16:34:34 -06001557 session->recovery_tmo = value;
Mike Christie30a6c652006-04-06 21:13:39 -05001558 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001559 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001560 err = transport->set_param(conn, ev->u.set_param.param,
1561 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001562 }
1563
1564 return err;
1565}
1566
Mike Christie10eb0f02009-05-13 17:57:38 -05001567static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1568 struct iscsi_uevent *ev, int msg_type)
1569{
1570 struct iscsi_endpoint *ep;
1571 struct sockaddr *dst_addr;
1572 struct Scsi_Host *shost = NULL;
1573 int non_blocking, err = 0;
1574
1575 if (!transport->ep_connect)
1576 return -EINVAL;
1577
1578 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1579 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1580 if (!shost) {
1581 printk(KERN_ERR "ep connect failed. Could not find "
1582 "host no %u\n",
1583 ev->u.ep_connect_through_host.host_no);
1584 return -ENODEV;
1585 }
1586 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1587 } else
1588 non_blocking = ev->u.ep_connect.non_blocking;
1589
1590 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1591 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1592 if (IS_ERR(ep)) {
1593 err = PTR_ERR(ep);
1594 goto release_host;
1595 }
1596
1597 ev->r.ep_connect_ret.handle = ep->id;
1598release_host:
1599 if (shost)
1600 scsi_host_put(shost);
1601 return err;
1602}
1603
Mike Christie22a39fb2011-02-16 15:04:33 -06001604static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
1605 u64 ep_handle)
1606{
1607 struct iscsi_cls_conn *conn;
1608 struct iscsi_endpoint *ep;
1609
1610 if (!transport->ep_disconnect)
1611 return -EINVAL;
1612
1613 ep = iscsi_lookup_endpoint(ep_handle);
1614 if (!ep)
1615 return -EINVAL;
1616 conn = ep->conn;
1617 if (conn) {
1618 mutex_lock(&conn->ep_mutex);
1619 conn->ep = NULL;
1620 mutex_unlock(&conn->ep_mutex);
1621 }
1622
1623 transport->ep_disconnect(ep);
1624 return 0;
1625}
1626
Mike Christie7b8631b2006-01-13 18:05:50 -06001627static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001628iscsi_if_transport_ep(struct iscsi_transport *transport,
1629 struct iscsi_uevent *ev, int msg_type)
1630{
Mike Christied82ff9be2008-05-21 15:54:13 -05001631 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001632 int rc = 0;
1633
1634 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05001635 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001636 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001637 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001638 break;
1639 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1640 if (!transport->ep_poll)
1641 return -EINVAL;
1642
Mike Christied82ff9be2008-05-21 15:54:13 -05001643 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1644 if (!ep)
1645 return -EINVAL;
1646
1647 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001648 ev->u.ep_poll.timeout_ms);
1649 break;
1650 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie22a39fb2011-02-16 15:04:33 -06001651 rc = iscsi_if_ep_disconnect(transport,
1652 ev->u.ep_disconnect.ep_handle);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001653 break;
1654 }
1655 return rc;
1656}
1657
1658static int
Mike Christie01cb2252006-06-28 12:00:22 -05001659iscsi_tgt_dscvr(struct iscsi_transport *transport,
1660 struct iscsi_uevent *ev)
1661{
Mike Christie2174a042007-05-30 12:57:10 -05001662 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001663 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001664 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001665
1666 if (!transport->tgt_dscvr)
1667 return -EINVAL;
1668
Mike Christie2174a042007-05-30 12:57:10 -05001669 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001670 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05001671 printk(KERN_ERR "target discovery could not find host no %u\n",
1672 ev->u.tgt_dscvr.host_no);
1673 return -ENODEV;
1674 }
1675
1676
Mike Christie01cb2252006-06-28 12:00:22 -05001677 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001678 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1679 ev->u.tgt_dscvr.enable, dst_addr);
1680 scsi_host_put(shost);
1681 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001682}
1683
1684static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001685iscsi_set_host_param(struct iscsi_transport *transport,
1686 struct iscsi_uevent *ev)
1687{
1688 char *data = (char*)ev + sizeof(*ev);
1689 struct Scsi_Host *shost;
1690 int err;
1691
1692 if (!transport->set_host_param)
1693 return -ENOSYS;
1694
1695 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001696 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05001697 printk(KERN_ERR "set_host_param could not find host no %u\n",
1698 ev->u.set_host_param.host_no);
1699 return -ENODEV;
1700 }
1701
1702 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1703 data, ev->u.set_host_param.len);
1704 scsi_host_put(shost);
1705 return err;
1706}
1707
1708static int
Michael Chan43514772009-06-08 18:14:41 -07001709iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1710{
1711 struct Scsi_Host *shost;
1712 struct iscsi_path *params;
1713 int err;
1714
1715 if (!transport->set_path)
1716 return -ENOSYS;
1717
1718 shost = scsi_host_lookup(ev->u.set_path.host_no);
1719 if (!shost) {
1720 printk(KERN_ERR "set path could not find host no %u\n",
1721 ev->u.set_path.host_no);
1722 return -ENODEV;
1723 }
1724
1725 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1726 err = transport->set_path(shost, params);
1727
1728 scsi_host_put(shost);
1729 return err;
1730}
1731
1732static int
Mike Christie56c155b2011-07-25 13:48:37 -05001733iscsi_set_iface_params(struct iscsi_transport *transport,
1734 struct iscsi_uevent *ev)
1735{
1736 char *data = (char *)ev + sizeof(*ev);
1737 struct Scsi_Host *shost;
1738 int err;
1739
1740 if (!transport->set_iface_param)
1741 return -ENOSYS;
1742
1743 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
1744 if (!shost) {
1745 printk(KERN_ERR "set_iface_params could not find host no %u\n",
1746 ev->u.set_iface_params.host_no);
1747 return -ENODEV;
1748 }
1749
1750 err = transport->set_iface_param(shost, data,
1751 ev->u.set_iface_params.count);
1752 scsi_host_put(shost);
1753 return err;
1754}
1755
1756static int
Michael Chan43514772009-06-08 18:14:41 -07001757iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07001758{
1759 int err = 0;
1760 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1761 struct iscsi_transport *transport = NULL;
1762 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001763 struct iscsi_cls_session *session;
1764 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05001765 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001766
Michael Chan43514772009-06-08 18:14:41 -07001767 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1768 *group = ISCSI_NL_GRP_UIP;
1769 else
1770 *group = ISCSI_NL_GRP_ISCSID;
1771
Alex Aizman0896b752005-08-04 19:33:07 -07001772 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1773 if (!priv)
1774 return -EINVAL;
1775 transport = priv->iscsi_transport;
1776
Mike Christie7b7232f2006-02-01 21:06:49 -06001777 if (!try_module_get(transport->owner))
1778 return -EINVAL;
1779
Alex Aizman0896b752005-08-04 19:33:07 -07001780 switch (nlh->nlmsg_type) {
1781 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001782 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001783 ev->u.c_session.initial_cmdsn,
1784 ev->u.c_session.cmds_max,
1785 ev->u.c_session.queue_depth);
1786 break;
1787 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001788 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05001789 if (!ep) {
1790 err = -EINVAL;
1791 break;
1792 }
Mike Christied82ff9be2008-05-21 15:54:13 -05001793
1794 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001795 ev->u.c_bound_session.initial_cmdsn,
1796 ev->u.c_bound_session.cmds_max,
1797 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001798 break;
1799 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001800 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001801 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001802 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001803 else
1804 err = -EINVAL;
1805 break;
1806 case ISCSI_UEVENT_UNBIND_SESSION:
1807 session = iscsi_session_lookup(ev->u.d_session.sid);
1808 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06001809 scsi_queue_work(iscsi_session_to_shost(session),
1810 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06001811 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001812 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001813 break;
1814 case ISCSI_UEVENT_CREATE_CONN:
1815 err = iscsi_if_create_conn(transport, ev);
1816 break;
1817 case ISCSI_UEVENT_DESTROY_CONN:
1818 err = iscsi_if_destroy_conn(transport, ev);
1819 break;
1820 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001821 session = iscsi_session_lookup(ev->u.b_conn.sid);
1822 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001823
Mike Christie22a39fb2011-02-16 15:04:33 -06001824 if (conn && conn->ep)
1825 iscsi_if_ep_disconnect(transport, conn->ep->id);
1826
1827 if (!session || !conn) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001828 err = -EINVAL;
Mike Christie22a39fb2011-02-16 15:04:33 -06001829 break;
1830 }
1831
1832 ev->r.retcode = transport->bind_conn(session, conn,
1833 ev->u.b_conn.transport_eph,
1834 ev->u.b_conn.is_leading);
1835 if (ev->r.retcode || !transport->ep_connect)
1836 break;
1837
1838 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
1839 if (ep) {
1840 ep->conn = conn;
1841
1842 mutex_lock(&conn->ep_mutex);
1843 conn->ep = ep;
1844 mutex_unlock(&conn->ep_mutex);
1845 } else
1846 iscsi_cls_conn_printk(KERN_ERR, conn,
1847 "Could not set ep conn "
1848 "binding\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001849 break;
1850 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001851 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001852 break;
1853 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001854 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001855 if (conn)
1856 ev->r.retcode = transport->start_conn(conn);
1857 else
1858 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001859 break;
1860 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001861 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001862 if (conn)
1863 transport->stop_conn(conn, ev->u.stop_conn.flag);
1864 else
1865 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001866 break;
1867 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001868 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001869 if (conn)
1870 ev->r.retcode = transport->send_pdu(conn,
1871 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1872 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1873 ev->u.send_pdu.data_size);
1874 else
1875 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001876 break;
1877 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001878 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001879 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001880 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1881 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1882 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001883 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001884 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1885 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001886 case ISCSI_UEVENT_TGT_DSCVR:
1887 err = iscsi_tgt_dscvr(transport, ev);
1888 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001889 case ISCSI_UEVENT_SET_HOST_PARAM:
1890 err = iscsi_set_host_param(transport, ev);
1891 break;
Michael Chan43514772009-06-08 18:14:41 -07001892 case ISCSI_UEVENT_PATH_UPDATE:
1893 err = iscsi_set_path(transport, ev);
1894 break;
Mike Christie56c155b2011-07-25 13:48:37 -05001895 case ISCSI_UEVENT_SET_IFACE_PARAMS:
1896 err = iscsi_set_iface_params(transport, ev);
1897 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001898 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001899 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001900 break;
1901 }
1902
Mike Christie7b7232f2006-02-01 21:06:49 -06001903 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001904 return err;
1905}
1906
Mike Christieb5c7a122006-04-06 21:13:33 -05001907/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001908 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1909 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001910 */
Alex Aizman0896b752005-08-04 19:33:07 -07001911static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001912iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001913{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001914 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001915 while (skb->len >= NLMSG_SPACE(0)) {
1916 int err;
1917 uint32_t rlen;
1918 struct nlmsghdr *nlh;
1919 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07001920 uint32_t group;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001921
1922 nlh = nlmsg_hdr(skb);
1923 if (nlh->nlmsg_len < sizeof(*nlh) ||
1924 skb->len < nlh->nlmsg_len) {
1925 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001926 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001927
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001928 ev = NLMSG_DATA(nlh);
1929 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1930 if (rlen > skb->len)
1931 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001932
Michael Chan43514772009-06-08 18:14:41 -07001933 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001934 if (err) {
1935 ev->type = ISCSI_KEVENT_IF_ERROR;
1936 ev->iferror = err;
1937 }
1938 do {
1939 /*
1940 * special case for GET_STATS:
1941 * on success - sending reply and stats from
1942 * inside of if_recv_msg(),
1943 * on error - fall through.
1944 */
1945 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001946 break;
Michael Chan43514772009-06-08 18:14:41 -07001947 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001948 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1949 } while (err < 0 && err != -ECONNREFUSED);
1950 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001951 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001952 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001953}
1954
Mike Christiefd7255f2006-04-06 21:13:36 -05001955#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01001956struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05001957 __ATTR(_name,_mode,_show,_store)
1958
Alex Aizman0896b752005-08-04 19:33:07 -07001959/*
1960 * iSCSI connection attrs
1961 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001962#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001964show_conn_param_##param(struct device *dev, \
1965 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001966{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001967 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001968 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001969 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001970}
1971
Mike Christiea54a52c2006-06-28 12:00:23 -05001972#define iscsi_conn_attr(field, param) \
1973 iscsi_conn_attr_show(param) \
1974static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001975 NULL);
1976
Mike Christiea54a52c2006-06-28 12:00:23 -05001977iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1978iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1979iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1980iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1981iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1982iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1983iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
Mike Christiea54a52c2006-06-28 12:00:23 -05001984iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1985iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001986iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1987iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Mike Christie289324b2011-02-16 15:04:37 -06001989#define iscsi_conn_ep_attr_show(param) \
1990static ssize_t show_conn_ep_param_##param(struct device *dev, \
1991 struct device_attribute *attr,\
1992 char *buf) \
1993{ \
1994 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
1995 struct iscsi_transport *t = conn->transport; \
1996 struct iscsi_endpoint *ep; \
1997 ssize_t rc; \
1998 \
1999 /* \
2000 * Need to make sure ep_disconnect does not free the LLD's \
2001 * interconnect resources while we are trying to read them. \
2002 */ \
2003 mutex_lock(&conn->ep_mutex); \
2004 ep = conn->ep; \
2005 if (!ep && t->ep_connect) { \
2006 mutex_unlock(&conn->ep_mutex); \
2007 return -ENOTCONN; \
2008 } \
2009 \
2010 if (ep) \
2011 rc = t->get_ep_param(ep, param, buf); \
2012 else \
2013 rc = t->get_conn_param(conn, param, buf); \
2014 mutex_unlock(&conn->ep_mutex); \
2015 return rc; \
2016}
2017
2018#define iscsi_conn_ep_attr(field, param) \
2019 iscsi_conn_ep_attr_show(param) \
2020static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
2021 show_conn_ep_param_##param, NULL);
2022
2023iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
2024iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
2025
Mike Christie3128c6c2011-07-25 13:48:42 -05002026static struct attribute *iscsi_conn_attrs[] = {
2027 &dev_attr_conn_max_recv_dlength.attr,
2028 &dev_attr_conn_max_xmit_dlength.attr,
2029 &dev_attr_conn_header_digest.attr,
2030 &dev_attr_conn_data_digest.attr,
2031 &dev_attr_conn_ifmarker.attr,
2032 &dev_attr_conn_ofmarker.attr,
2033 &dev_attr_conn_address.attr,
2034 &dev_attr_conn_port.attr,
2035 &dev_attr_conn_exp_statsn.attr,
2036 &dev_attr_conn_persistent_address.attr,
2037 &dev_attr_conn_persistent_port.attr,
2038 &dev_attr_conn_ping_tmo.attr,
2039 &dev_attr_conn_recv_tmo.attr,
2040 NULL,
2041};
2042
2043static mode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
2044 struct attribute *attr, int i)
2045{
2046 struct device *cdev = container_of(kobj, struct device, kobj);
2047 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
2048 struct iscsi_transport *t = conn->transport;
2049 int param;
2050
2051 if (attr == &dev_attr_conn_max_recv_dlength.attr)
2052 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
2053 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
2054 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
2055 else if (attr == &dev_attr_conn_header_digest.attr)
2056 param = ISCSI_PARAM_HDRDGST_EN;
2057 else if (attr == &dev_attr_conn_data_digest.attr)
2058 param = ISCSI_PARAM_DATADGST_EN;
2059 else if (attr == &dev_attr_conn_ifmarker.attr)
2060 param = ISCSI_PARAM_IFMARKER_EN;
2061 else if (attr == &dev_attr_conn_ofmarker.attr)
2062 param = ISCSI_PARAM_OFMARKER_EN;
2063 else if (attr == &dev_attr_conn_address.attr)
2064 param = ISCSI_PARAM_CONN_ADDRESS;
2065 else if (attr == &dev_attr_conn_port.attr)
2066 param = ISCSI_PARAM_CONN_PORT;
2067 else if (attr == &dev_attr_conn_exp_statsn.attr)
2068 param = ISCSI_PARAM_EXP_STATSN;
2069 else if (attr == &dev_attr_conn_persistent_address.attr)
2070 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
2071 else if (attr == &dev_attr_conn_persistent_port.attr)
2072 param = ISCSI_PARAM_PERSISTENT_PORT;
2073 else if (attr == &dev_attr_conn_ping_tmo.attr)
2074 param = ISCSI_PARAM_PING_TMO;
2075 else if (attr == &dev_attr_conn_recv_tmo.attr)
2076 param = ISCSI_PARAM_RECV_TMO;
2077 else {
2078 WARN_ONCE(1, "Invalid conn attr");
2079 return 0;
2080 }
2081
2082 return t->attr_is_visible(ISCSI_PARAM, param);
2083}
2084
2085static struct attribute_group iscsi_conn_group = {
2086 .attrs = iscsi_conn_attrs,
2087 .is_visible = iscsi_conn_attr_is_visible,
2088};
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090/*
Alex Aizman0896b752005-08-04 19:33:07 -07002091 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 */
Mike Christieb2c64162007-05-30 12:57:16 -05002093#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002095show_session_param_##param(struct device *dev, \
2096 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002097{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002098 struct iscsi_cls_session *session = \
2099 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002100 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05002101 \
2102 if (perm && !capable(CAP_SYS_ADMIN)) \
2103 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05002104 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002105}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Mike Christieb2c64162007-05-30 12:57:16 -05002107#define iscsi_session_attr(field, param, perm) \
2108 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05002109static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05002110 NULL);
Mike Christieb2c64162007-05-30 12:57:16 -05002111iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
2112iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
2113iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
2114iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
2115iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
2116iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
2117iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
2118iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
2119iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
2120iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
2121iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
2122iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
2123iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
2124iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06002125iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
2126iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
2127iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002128iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05002129iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05302130iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
2131iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05002132
Mike Christie6eabafb2008-01-31 13:36:43 -06002133static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01002134show_priv_session_state(struct device *dev, struct device_attribute *attr,
2135 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06002136{
Tony Jonesee959b02008-02-22 00:13:36 +01002137 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06002138 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
2139}
2140static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
2141 NULL);
2142
Mike Christiefd7255f2006-04-06 21:13:36 -05002143#define iscsi_priv_session_attr_show(field, format) \
2144static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002145show_priv_session_##field(struct device *dev, \
2146 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002147{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002148 struct iscsi_cls_session *session = \
2149 iscsi_dev_to_session(dev->parent); \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302150 if (session->field == -1) \
2151 return sprintf(buf, "off\n"); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002152 return sprintf(buf, format"\n", session->field); \
2153}
2154
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302155#define iscsi_priv_session_attr_store(field) \
2156static ssize_t \
2157store_priv_session_##field(struct device *dev, \
2158 struct device_attribute *attr, \
2159 const char *buf, size_t count) \
2160{ \
2161 int val; \
2162 char *cp; \
2163 struct iscsi_cls_session *session = \
2164 iscsi_dev_to_session(dev->parent); \
2165 if ((session->state == ISCSI_SESSION_FREE) || \
2166 (session->state == ISCSI_SESSION_FAILED)) \
2167 return -EBUSY; \
2168 if (strncmp(buf, "off", 3) == 0) \
2169 session->field = -1; \
2170 else { \
2171 val = simple_strtoul(buf, &cp, 0); \
2172 if (*cp != '\0' && *cp != '\n') \
2173 return -EINVAL; \
2174 session->field = val; \
2175 } \
2176 return count; \
2177}
2178
2179#define iscsi_priv_session_rw_attr(field, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002180 iscsi_priv_session_attr_show(field, format) \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302181 iscsi_priv_session_attr_store(field) \
Vasiliy Kulikov523f3c82011-02-04 15:24:14 +03002182static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302183 show_priv_session_##field, \
2184 store_priv_session_##field)
2185iscsi_priv_session_rw_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05002186
Mike Christie1d063c12011-07-25 13:48:43 -05002187static struct attribute *iscsi_session_attrs[] = {
2188 &dev_attr_sess_initial_r2t.attr,
2189 &dev_attr_sess_max_outstanding_r2t.attr,
2190 &dev_attr_sess_immediate_data.attr,
2191 &dev_attr_sess_first_burst_len.attr,
2192 &dev_attr_sess_max_burst_len.attr,
2193 &dev_attr_sess_data_pdu_in_order.attr,
2194 &dev_attr_sess_data_seq_in_order.attr,
2195 &dev_attr_sess_erl.attr,
2196 &dev_attr_sess_targetname.attr,
2197 &dev_attr_sess_tpgt.attr,
2198 &dev_attr_sess_password.attr,
2199 &dev_attr_sess_password_in.attr,
2200 &dev_attr_sess_username.attr,
2201 &dev_attr_sess_username_in.attr,
2202 &dev_attr_sess_fast_abort.attr,
2203 &dev_attr_sess_abort_tmo.attr,
2204 &dev_attr_sess_lu_reset_tmo.attr,
2205 &dev_attr_sess_tgt_reset_tmo.attr,
2206 &dev_attr_sess_ifacename.attr,
2207 &dev_attr_sess_initiatorname.attr,
2208 &dev_attr_sess_targetalias.attr,
2209 &dev_attr_priv_sess_recovery_tmo.attr,
2210 &dev_attr_priv_sess_state.attr,
2211 NULL,
2212};
2213
2214static mode_t iscsi_session_attr_is_visible(struct kobject *kobj,
2215 struct attribute *attr, int i)
2216{
2217 struct device *cdev = container_of(kobj, struct device, kobj);
2218 struct iscsi_cls_session *session = transport_class_to_session(cdev);
2219 struct iscsi_transport *t = session->transport;
2220 int param;
2221
2222 if (attr == &dev_attr_sess_initial_r2t.attr)
2223 param = ISCSI_PARAM_INITIAL_R2T_EN;
2224 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
2225 param = ISCSI_PARAM_MAX_R2T;
2226 else if (attr == &dev_attr_sess_immediate_data.attr)
2227 param = ISCSI_PARAM_IMM_DATA_EN;
2228 else if (attr == &dev_attr_sess_first_burst_len.attr)
2229 param = ISCSI_PARAM_FIRST_BURST;
2230 else if (attr == &dev_attr_sess_max_burst_len.attr)
2231 param = ISCSI_PARAM_MAX_BURST;
2232 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
2233 param = ISCSI_PARAM_PDU_INORDER_EN;
2234 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
2235 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
2236 else if (attr == &dev_attr_sess_erl.attr)
2237 param = ISCSI_PARAM_ERL;
2238 else if (attr == &dev_attr_sess_targetname.attr)
2239 param = ISCSI_PARAM_TARGET_NAME;
2240 else if (attr == &dev_attr_sess_tpgt.attr)
2241 param = ISCSI_PARAM_TPGT;
2242 else if (attr == &dev_attr_sess_password.attr)
2243 param = ISCSI_PARAM_USERNAME;
2244 else if (attr == &dev_attr_sess_password_in.attr)
2245 param = ISCSI_PARAM_USERNAME_IN;
2246 else if (attr == &dev_attr_sess_username.attr)
2247 param = ISCSI_PARAM_PASSWORD;
2248 else if (attr == &dev_attr_sess_username_in.attr)
2249 param = ISCSI_PARAM_PASSWORD_IN;
2250 else if (attr == &dev_attr_sess_fast_abort.attr)
2251 param = ISCSI_PARAM_FAST_ABORT;
2252 else if (attr == &dev_attr_sess_abort_tmo.attr)
2253 param = ISCSI_PARAM_ABORT_TMO;
2254 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
2255 param = ISCSI_PARAM_LU_RESET_TMO;
2256 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
2257 param = ISCSI_PARAM_TGT_RESET_TMO;
2258 else if (attr == &dev_attr_sess_ifacename.attr)
2259 param = ISCSI_PARAM_IFACE_NAME;
2260 else if (attr == &dev_attr_sess_initiatorname.attr)
2261 param = ISCSI_PARAM_INITIATOR_NAME;
2262 else if (attr == &dev_attr_sess_targetalias.attr)
2263 param = ISCSI_PARAM_TARGET_ALIAS;
2264 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
2265 return S_IRUGO | S_IWUSR;
2266 else if (attr == &dev_attr_priv_sess_state.attr)
2267 return S_IRUGO;
2268 else {
2269 WARN_ONCE(1, "Invalid session attr");
2270 return 0;
2271 }
2272
2273 return t->attr_is_visible(ISCSI_PARAM, param);
2274}
2275
2276static struct attribute_group iscsi_session_group = {
2277 .attrs = iscsi_session_attrs,
2278 .is_visible = iscsi_session_attr_is_visible,
2279};
2280
Mike Christie1819dc82007-05-30 12:57:08 -05002281/*
2282 * iSCSI host attrs
2283 */
2284#define iscsi_host_attr_show(param) \
2285static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002286show_host_param_##param(struct device *dev, \
2287 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05002288{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002289 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05002290 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
2291 return priv->iscsi_transport->get_host_param(shost, param, buf); \
2292}
2293
2294#define iscsi_host_attr(field, param) \
2295 iscsi_host_attr_show(param) \
2296static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
2297 NULL);
2298
Mike Christied8196ed2007-05-30 12:57:25 -05002299iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002300iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05002301iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05002302iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002303
Mike Christie1819dc82007-05-30 12:57:08 -05002304#define SETUP_HOST_RD_ATTR(field, param_flag) \
2305do { \
2306 if (tt->host_param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01002307 priv->host_attrs[count] = &dev_attr_host_##field; \
Mike Christie1819dc82007-05-30 12:57:08 -05002308 count++; \
2309 } \
2310} while (0)
2311
Alex Aizman0896b752005-08-04 19:33:07 -07002312static int iscsi_session_match(struct attribute_container *cont,
2313 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Mike Christie7b8631b2006-01-13 18:05:50 -06002315 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07002317 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Alex Aizman0896b752005-08-04 19:33:07 -07002319 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return 0;
2321
Mike Christie7b8631b2006-01-13 18:05:50 -06002322 session = iscsi_dev_to_session(dev);
2323 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07002324 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return 0;
2326
Alex Aizman0896b752005-08-04 19:33:07 -07002327 priv = to_iscsi_internal(shost->transportt);
2328 if (priv->session_cont.ac.class != &iscsi_session_class.class)
2329 return 0;
2330
2331 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
Alex Aizman0896b752005-08-04 19:33:07 -07002334static int iscsi_conn_match(struct attribute_container *cont,
2335 struct device *dev)
2336{
Mike Christie7b8631b2006-01-13 18:05:50 -06002337 struct iscsi_cls_session *session;
2338 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07002339 struct Scsi_Host *shost;
2340 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Alex Aizman0896b752005-08-04 19:33:07 -07002342 if (!iscsi_is_conn_dev(dev))
2343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Mike Christie7b8631b2006-01-13 18:05:50 -06002345 conn = iscsi_dev_to_conn(dev);
2346 session = iscsi_dev_to_session(conn->dev.parent);
2347 shost = iscsi_session_to_shost(session);
2348
Alex Aizman0896b752005-08-04 19:33:07 -07002349 if (!shost->transportt)
2350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Alex Aizman0896b752005-08-04 19:33:07 -07002352 priv = to_iscsi_internal(shost->transportt);
2353 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
2354 return 0;
2355
2356 return &priv->conn_cont.ac == cont;
2357}
2358
Mike Christie30a6c652006-04-06 21:13:39 -05002359static int iscsi_host_match(struct attribute_container *cont,
2360 struct device *dev)
2361{
2362 struct Scsi_Host *shost;
2363 struct iscsi_internal *priv;
2364
2365 if (!scsi_is_host_device(dev))
2366 return 0;
2367
2368 shost = dev_to_shost(dev);
2369 if (!shost->transportt ||
2370 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
2371 return 0;
2372
2373 priv = to_iscsi_internal(shost->transportt);
2374 return &priv->t.host_attrs.ac == cont;
2375}
2376
Mike Christie7b8631b2006-01-13 18:05:50 -06002377struct scsi_transport_template *
2378iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07002379{
2380 struct iscsi_internal *priv;
2381 unsigned long flags;
2382 int count = 0, err;
2383
2384 BUG_ON(!tt);
2385
2386 priv = iscsi_if_transport_lookup(tt);
2387 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002388 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002389
Jes Sorensen24669f752006-01-16 10:31:18 -05002390 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07002391 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002392 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002393 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07002394 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05002395 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06002396 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07002397
Tony Jonesee959b02008-02-22 00:13:36 +01002398 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01002399 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01002400 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07002401 if (err)
2402 goto free_priv;
2403
Tony Jonesee959b02008-02-22 00:13:36 +01002404 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07002405 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01002406 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07002407
Mike Christie30a6c652006-04-06 21:13:39 -05002408 /* host parameters */
2409 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
2410 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
2411 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christie32c6e1b2008-05-21 15:53:58 -05002412 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05002413 transport_container_register(&priv->t.host_attrs);
2414
Mike Christied8196ed2007-05-30 12:57:25 -05002415 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
Mike Christie22236962007-05-30 12:57:24 -05002416 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
Mike Christie1819dc82007-05-30 12:57:08 -05002417 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05002418 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002419 BUG_ON(count > ISCSI_HOST_ATTRS);
2420 priv->host_attrs[count] = NULL;
2421 count = 0;
2422
Alex Aizman0896b752005-08-04 19:33:07 -07002423 /* connection parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07002424 priv->conn_cont.ac.class = &iscsi_connection_class.class;
2425 priv->conn_cont.ac.match = iscsi_conn_match;
Mike Christie3128c6c2011-07-25 13:48:42 -05002426 priv->conn_cont.ac.grp = &iscsi_conn_group;
Alex Aizman0896b752005-08-04 19:33:07 -07002427 transport_container_register(&priv->conn_cont);
2428
Alex Aizman0896b752005-08-04 19:33:07 -07002429 /* session parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07002430 priv->session_cont.ac.class = &iscsi_session_class.class;
2431 priv->session_cont.ac.match = iscsi_session_match;
Mike Christie1d063c12011-07-25 13:48:43 -05002432 priv->session_cont.ac.grp = &iscsi_session_group;
Alex Aizman0896b752005-08-04 19:33:07 -07002433 transport_container_register(&priv->session_cont);
2434
Alex Aizman0896b752005-08-04 19:33:07 -07002435 spin_lock_irqsave(&iscsi_transport_lock, flags);
2436 list_add(&priv->list, &iscsi_transports);
2437 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Alex Aizman0896b752005-08-04 19:33:07 -07002439 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06002440 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Tony Jonesee959b02008-02-22 00:13:36 +01002442unregister_dev:
2443 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05002444 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002445free_priv:
2446 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06002447 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
Alex Aizman0896b752005-08-04 19:33:07 -07002449EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Alex Aizman0896b752005-08-04 19:33:07 -07002451int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
Alex Aizman0896b752005-08-04 19:33:07 -07002453 struct iscsi_internal *priv;
2454 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Alex Aizman0896b752005-08-04 19:33:07 -07002456 BUG_ON(!tt);
2457
Arjan van de Ven0b950672006-01-11 13:16:10 +01002458 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002459
2460 priv = iscsi_if_transport_lookup(tt);
2461 BUG_ON (!priv);
2462
Alex Aizman0896b752005-08-04 19:33:07 -07002463 spin_lock_irqsave(&iscsi_transport_lock, flags);
2464 list_del(&priv->list);
2465 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2466
2467 transport_container_unregister(&priv->conn_cont);
2468 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05002469 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07002470
Tony Jonesee959b02008-02-22 00:13:36 +01002471 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
2472 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002473 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002474
2475 return 0;
2476}
2477EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
2478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479static __init int iscsi_transport_init(void)
2480{
Alex Aizman0896b752005-08-04 19:33:07 -07002481 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Meelis Roos09492602006-12-17 12:10:26 -06002483 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05002484 ISCSI_TRANSPORT_VERSION);
2485
Mike Christie41be1442007-02-28 17:32:18 -06002486 atomic_set(&iscsi_session_nr, 0);
2487
Alex Aizman0896b752005-08-04 19:33:07 -07002488 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 if (err)
2490 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07002491
Mike Christied82ff9be2008-05-21 15:54:13 -05002492 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002493 if (err)
2494 goto unregister_transport_class;
2495
Mike Christie8d079132011-07-25 13:48:40 -05002496 err = class_register(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002497 if (err)
2498 goto unregister_endpoint_class;
2499
Mike Christie8d079132011-07-25 13:48:40 -05002500 err = transport_class_register(&iscsi_host_class);
2501 if (err)
2502 goto unregister_iface_class;
2503
Mike Christie30a6c652006-04-06 21:13:39 -05002504 err = transport_class_register(&iscsi_connection_class);
2505 if (err)
2506 goto unregister_host_class;
2507
Alex Aizman0896b752005-08-04 19:33:07 -07002508 err = transport_class_register(&iscsi_session_class);
2509 if (err)
2510 goto unregister_conn_class;
2511
Mike Christied82ff9be2008-05-21 15:54:13 -05002512 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2513 NULL, THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07002514 if (!nls) {
2515 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04002516 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07002517 }
2518
Mike Christied8bf5412007-12-13 12:43:27 -06002519 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2520 if (!iscsi_eh_timer_workq)
2521 goto release_nls;
2522
Mike Christie43a145a2006-10-16 18:09:38 -04002523 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07002524
Mike Christied8bf5412007-12-13 12:43:27 -06002525release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002526 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002527unregister_session_class:
2528 transport_class_unregister(&iscsi_session_class);
2529unregister_conn_class:
2530 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002531unregister_host_class:
2532 transport_class_unregister(&iscsi_host_class);
Mike Christie8d079132011-07-25 13:48:40 -05002533unregister_iface_class:
2534 class_unregister(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002535unregister_endpoint_class:
2536 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002537unregister_transport_class:
2538 class_unregister(&iscsi_transport_class);
2539 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
2542static void __exit iscsi_transport_exit(void)
2543{
Mike Christied8bf5412007-12-13 12:43:27 -06002544 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002545 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002546 transport_class_unregister(&iscsi_connection_class);
2547 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002548 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002549 class_unregister(&iscsi_endpoint_class);
Mike Christie8d079132011-07-25 13:48:40 -05002550 class_unregister(&iscsi_iface_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002551 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
2554module_init(iscsi_transport_init);
2555module_exit(iscsi_transport_exit);
2556
Alex Aizman0896b752005-08-04 19:33:07 -07002557MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2558 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2559 "Alex Aizman <itn780@yahoo.com>");
2560MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05002562MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
Stephen Hemminger058548a2010-12-09 09:37:56 -08002563MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);