blob: e9eca98e1102d06583358d248f351b970df8c3a7 [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
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +053035#define ISCSI_SESSION_ATTRS 23
Mike Christiec238c3b2008-01-31 13:36:51 -060036#define ISCSI_CONN_ATTRS 13
Mike Christied8196ed2007-05-30 12:57:25 -050037#define ISCSI_HOST_ATTRS 4
Mike Christied82ff9be2008-05-21 15:54:13 -050038
Mike Christie4c2133c2008-06-16 10:11:34 -050039#define ISCSI_TRANSPORT_VERSION "2.0-870"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Mike Christie632248a2009-08-20 15:11:01 -050041static int dbg_session;
42module_param_named(debug_session, dbg_session, int,
43 S_IRUGO | S_IWUSR);
44MODULE_PARM_DESC(debug_session,
45 "Turn on debugging for sessions in scsi_transport_iscsi "
46 "module. Set to 1 to turn on, and zero to turn off. Default "
47 "is off.");
48
49static int dbg_conn;
50module_param_named(debug_conn, dbg_conn, int,
51 S_IRUGO | S_IWUSR);
52MODULE_PARM_DESC(debug_conn,
53 "Turn on debugging for connections in scsi_transport_iscsi "
54 "module. Set to 1 to turn on, and zero to turn off. Default "
55 "is off.");
56
57#define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
58 do { \
59 if (dbg_session) \
60 iscsi_cls_session_printk(KERN_INFO, _session, \
61 "%s: " dbg_fmt, \
62 __func__, ##arg); \
63 } while (0);
64
65#define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
66 do { \
67 if (dbg_conn) \
68 iscsi_cls_conn_printk(KERN_INFO, _conn, \
69 "%s: " dbg_fmt, \
70 __func__, ##arg); \
71 } while (0);
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073struct iscsi_internal {
74 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070075 struct iscsi_transport *iscsi_transport;
76 struct list_head list;
Tony Jonesee959b02008-02-22 00:13:36 +010077 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050078
Tony Jonesee959b02008-02-22 00:13:36 +010079 struct device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070080 struct transport_container conn_cont;
Alex Aizman0896b752005-08-04 19:33:07 -070081 struct transport_container session_cont;
Tony Jonesee959b02008-02-22 00:13:36 +010082 struct device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Mike Christie41be1442007-02-28 17:32:18 -060085static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060086static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050087
Alex Aizman0896b752005-08-04 19:33:07 -070088/*
89 * list of registered transports and lock that must
90 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010091 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070092 */
93static LIST_HEAD(iscsi_transports);
94static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Alex Aizman0896b752005-08-04 19:33:07 -070096#define to_iscsi_internal(tmpl) \
97 container_of(tmpl, struct iscsi_internal, t)
98
Tony Jonesee959b02008-02-22 00:13:36 +010099#define dev_to_iscsi_internal(_dev) \
100 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700101
Tony Jonesee959b02008-02-22 00:13:36 +0100102static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700103{
Tony Jonesee959b02008-02-22 00:13:36 +0100104 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -0700105 kfree(priv);
106}
107
108/*
109 * iscsi_transport_class represents the iscsi_transports that are
110 * registered.
111 */
112static struct class iscsi_transport_class = {
113 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +0100114 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -0700115};
116
117static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100118show_transport_handle(struct device *dev, struct device_attribute *attr,
119 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -0700120{
Tony Jonesee959b02008-02-22 00:13:36 +0100121 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Mike Christie762e2bf2005-09-12 21:01:46 -0500122 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -0700123}
Tony Jonesee959b02008-02-22 00:13:36 +0100124static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700125
126#define show_transport_attr(name, format) \
127static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100128show_transport_##name(struct device *dev, \
129 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -0700130{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100131 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Alex Aizman0896b752005-08-04 19:33:07 -0700132 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
133} \
Tony Jonesee959b02008-02-22 00:13:36 +0100134static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700135
136show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700137
138static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100139 &dev_attr_handle.attr,
140 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700141 NULL,
142};
143
144static struct attribute_group iscsi_transport_group = {
145 .attrs = iscsi_transport_attrs,
146};
147
Mike Christied82ff9be2008-05-21 15:54:13 -0500148/*
149 * iSCSI endpoint attrs
150 */
151#define iscsi_dev_to_endpoint(_dev) \
152 container_of(_dev, struct iscsi_endpoint, dev)
153
154#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
155struct device_attribute dev_attr_##_prefix##_##_name = \
156 __ATTR(_name,_mode,_show,_store)
157
158static void iscsi_endpoint_release(struct device *dev)
159{
160 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
161 kfree(ep);
162}
163
164static struct class iscsi_endpoint_class = {
165 .name = "iscsi_endpoint",
166 .dev_release = iscsi_endpoint_release,
167};
168
169static ssize_t
170show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
171{
172 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500173 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500174}
175static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
176
177static struct attribute *iscsi_endpoint_attrs[] = {
178 &dev_attr_ep_handle.attr,
179 NULL,
180};
181
182static struct attribute_group iscsi_endpoint_group = {
183 .attrs = iscsi_endpoint_attrs,
184};
185
186#define ISCSI_MAX_EPID -1
187
188static int iscsi_match_epid(struct device *dev, void *data)
189{
190 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500191 uint64_t *epid = (uint64_t *) data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500192
193 return *epid == ep->id;
194}
195
196struct iscsi_endpoint *
197iscsi_create_endpoint(int dd_size)
198{
199 struct device *dev;
200 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500201 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500202 int err;
203
204 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400205 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500206 iscsi_match_epid);
207 if (!dev)
208 break;
209 }
210 if (id == ISCSI_MAX_EPID) {
211 printk(KERN_ERR "Too many connections. Max supported %u\n",
212 ISCSI_MAX_EPID - 1);
213 return NULL;
214 }
215
216 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
217 if (!ep)
218 return NULL;
219
220 ep->id = id;
221 ep->dev.class = &iscsi_endpoint_class;
Kay Sievers71610f52008-12-03 22:41:36 +0100222 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500223 err = device_register(&ep->dev);
224 if (err)
225 goto free_ep;
226
227 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
228 if (err)
229 goto unregister_dev;
230
231 if (dd_size)
232 ep->dd_data = &ep[1];
233 return ep;
234
235unregister_dev:
236 device_unregister(&ep->dev);
237 return NULL;
238
239free_ep:
240 kfree(ep);
241 return NULL;
242}
243EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
244
245void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
246{
247 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
248 device_unregister(&ep->dev);
249}
250EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
251
252struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
253{
Mike Christief80f8682008-06-16 10:11:35 -0500254 struct iscsi_endpoint *ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500255 struct device *dev;
256
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400257 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500258 iscsi_match_epid);
259 if (!dev)
260 return NULL;
261
Mike Christief80f8682008-06-16 10:11:35 -0500262 ep = iscsi_dev_to_endpoint(dev);
263 /*
264 * we can drop this now because the interface will prevent
265 * removals and lookups from racing.
266 */
267 put_device(dev);
268 return ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500269}
270EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
271
Mike Christie8d079132011-07-25 13:48:40 -0500272/*
273 * Interface to display network param to sysfs
274 */
275
276static void iscsi_iface_release(struct device *dev)
277{
278 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
279 struct device *parent = iface->dev.parent;
280
281 kfree(iface);
282 put_device(parent);
283}
284
285
286static struct class iscsi_iface_class = {
287 .name = "iscsi_iface",
288 .dev_release = iscsi_iface_release,
289};
290
291#define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
292struct device_attribute dev_attr_##_prefix##_##_name = \
293 __ATTR(_name, _mode, _show, _store)
294
295/* iface attrs show */
296#define iscsi_iface_attr_show(type, name, param_type, param) \
297static ssize_t \
298show_##type##_##name(struct device *dev, struct device_attribute *attr, \
299 char *buf) \
300{ \
301 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
302 struct iscsi_transport *t = iface->transport; \
303 return t->get_iface_param(iface, param_type, param, buf); \
304} \
305
306#define iscsi_iface_net_attr(type, name, param) \
307 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
308static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
309
310/* generic read only ipvi4 attribute */
311iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
312iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
313iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
314iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
315
316/* generic read only ipv6 attribute */
317iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
318iscsi_iface_net_attr(ipv6_iface, link_local_addr, ISCSI_NET_PARAM_IPV6_LINKLOCAL);
319iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
320iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
321 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
322iscsi_iface_net_attr(ipv6_iface, linklocal_autocfg,
323 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
324
325/* common read only iface attribute */
326iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
327iscsi_iface_net_attr(iface, vlan, ISCSI_NET_PARAM_VLAN_ID);
328
329static mode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
330 struct attribute *attr, int i)
331{
332 struct device *dev = container_of(kobj, struct device, kobj);
333 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
334 struct iscsi_transport *t = iface->transport;
335
336 if (attr == &dev_attr_iface_enabled.attr)
337 return (t->iface_param_mask & ISCSI_NET_IFACE_ENABLE) ?
338 S_IRUGO : 0;
339 else if (attr == &dev_attr_iface_vlan.attr)
340 return (t->iface_param_mask & ISCSI_NET_VLAN_ID) ? S_IRUGO : 0;
341
342 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
343 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
344 return (t->iface_param_mask & ISCSI_NET_IPV4_ADDR) ?
345 S_IRUGO : 0;
346 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
347 return (t->iface_param_mask & ISCSI_NET_IPV4_GW) ?
348 S_IRUGO : 0;
349 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
350 return (t->iface_param_mask & ISCSI_NET_IPV4_SUBNET) ?
351 S_IRUGO : 0;
352 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
353 return (t->iface_param_mask & ISCSI_NET_IPV4_BOOTPROTO) ?
354 S_IRUGO : 0;
355 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
356 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
357 return (t->iface_param_mask & ISCSI_NET_IPV6_ADDR) ?
358 S_IRUGO : 0;
359 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
360 return (t->iface_param_mask & ISCSI_NET_IPV6_LINKLOCAL) ?
361 S_IRUGO : 0;
362 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
363 return (t->iface_param_mask & ISCSI_NET_IPV6_ROUTER) ?
364 S_IRUGO : 0;
365 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
366 return (t->iface_param_mask & ISCSI_NET_IPV6_ADDR_AUTOCFG) ?
367 S_IRUGO : 0;
368 else if (attr == &dev_attr_ipv6_iface_linklocal_autocfg.attr)
369 return (t->iface_param_mask & ISCSI_NET_IPV6_LINKLOCAL_AUTOCFG) ?
370 S_IRUGO : 0;
371 }
372
373 return 0;
374}
375
376static struct attribute *iscsi_iface_attrs[] = {
377 &dev_attr_iface_enabled.attr,
378 &dev_attr_iface_vlan.attr,
379 &dev_attr_ipv4_iface_ipaddress.attr,
380 &dev_attr_ipv4_iface_gateway.attr,
381 &dev_attr_ipv4_iface_subnet.attr,
382 &dev_attr_ipv4_iface_bootproto.attr,
383 &dev_attr_ipv6_iface_ipaddress.attr,
384 &dev_attr_ipv6_iface_link_local_addr.attr,
385 &dev_attr_ipv6_iface_router_addr.attr,
386 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
387 &dev_attr_ipv6_iface_linklocal_autocfg.attr,
388 NULL,
389};
390
391static struct attribute_group iscsi_iface_group = {
392 .attrs = iscsi_iface_attrs,
393 .is_visible = iscsi_iface_attr_is_visible,
394};
395
396struct iscsi_iface *
397iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
398 uint32_t iface_type, uint32_t iface_num, int dd_size)
399{
400 struct iscsi_iface *iface;
401 int err;
402
403 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
404 if (!iface)
405 return NULL;
406
407 iface->transport = transport;
408 iface->iface_type = iface_type;
409 iface->iface_num = iface_num;
410 iface->dev.release = iscsi_iface_release;
411 iface->dev.class = &iscsi_iface_class;
412 /* parent reference released in iscsi_iface_release */
413 iface->dev.parent = get_device(&shost->shost_gendev);
414 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
415 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
416 iface_num);
417 else
418 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
419 iface_num);
420
421 err = device_register(&iface->dev);
422 if (err)
423 goto free_iface;
424
425 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
426 if (err)
427 goto unreg_iface;
428
429 if (dd_size)
430 iface->dd_data = &iface[1];
431 return iface;
432
433unreg_iface:
434 device_unregister(&iface->dev);
435 return NULL;
436
437free_iface:
438 put_device(iface->dev.parent);
439 kfree(iface);
440 return NULL;
441}
442EXPORT_SYMBOL_GPL(iscsi_create_iface);
443
444void iscsi_destroy_iface(struct iscsi_iface *iface)
445{
446 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
447 device_unregister(&iface->dev);
448}
449EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
450
Mike Christie30a6c652006-04-06 21:13:39 -0500451static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100452 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500453{
454 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500455 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500456
457 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -0600458 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -0500459 mutex_init(&ihost->mutex);
Mike Christie30a6c652006-04-06 21:13:39 -0500460 return 0;
461}
462
463static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
464 "iscsi_host",
465 iscsi_setup_host,
Mike Christie06d25af2009-03-05 14:46:02 -0600466 NULL,
Mike Christie30a6c652006-04-06 21:13:39 -0500467 NULL);
468
Alex Aizman0896b752005-08-04 19:33:07 -0700469static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
470 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 NULL,
472 NULL,
473 NULL);
474
Alex Aizman0896b752005-08-04 19:33:07 -0700475static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
476 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 NULL,
478 NULL,
479 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700480
481static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100482static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700483
Mike Christie7b7232f2006-02-01 21:06:49 -0600484static LIST_HEAD(sesslist);
485static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700486static LIST_HEAD(connlist);
487static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Mike Christieb5c7a122006-04-06 21:13:33 -0500489static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
490{
491 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
492 return sess->sid;
493}
494
495/*
496 * Returns the matching session to a given sid
497 */
498static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600499{
500 unsigned long flags;
501 struct iscsi_cls_session *sess;
502
503 spin_lock_irqsave(&sesslock, flags);
504 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500505 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600506 spin_unlock_irqrestore(&sesslock, flags);
507 return sess;
508 }
509 }
510 spin_unlock_irqrestore(&sesslock, flags);
511 return NULL;
512}
513
Mike Christieb5c7a122006-04-06 21:13:33 -0500514/*
515 * Returns the matching connection to a given sid / cid tuple
516 */
517static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600518{
519 unsigned long flags;
520 struct iscsi_cls_conn *conn;
521
522 spin_lock_irqsave(&connlock, flags);
523 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500524 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600525 spin_unlock_irqrestore(&connlock, flags);
526 return conn;
527 }
528 }
529 spin_unlock_irqrestore(&connlock, flags);
530 return NULL;
531}
532
Mike Christie7b8631b2006-01-13 18:05:50 -0600533/*
534 * The following functions can be used by LLDs that allocate
535 * their own scsi_hosts or by software iscsi LLDs
536 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600537static struct {
538 int value;
539 char *name;
540} iscsi_session_state_names[] = {
541 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
542 { ISCSI_SESSION_FAILED, "FAILED" },
543 { ISCSI_SESSION_FREE, "FREE" },
544};
545
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200546static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600547{
548 int i;
549 char *name = NULL;
550
551 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
552 if (iscsi_session_state_names[i].value == state) {
553 name = iscsi_session_state_names[i].name;
554 break;
555 }
556 }
557 return name;
558}
559
560int iscsi_session_chkready(struct iscsi_cls_session *session)
561{
562 unsigned long flags;
563 int err;
564
565 spin_lock_irqsave(&session->lock, flags);
566 switch (session->state) {
567 case ISCSI_SESSION_LOGGED_IN:
568 err = 0;
569 break;
570 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -0500571 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600572 break;
573 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500574 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600575 break;
576 default:
577 err = DID_NO_CONNECT << 16;
578 break;
579 }
580 spin_unlock_irqrestore(&session->lock, flags);
581 return err;
582}
583EXPORT_SYMBOL_GPL(iscsi_session_chkready);
584
Mike Christie7b8631b2006-01-13 18:05:50 -0600585static void iscsi_session_release(struct device *dev)
586{
587 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600588 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Mike Christie7b8631b2006-01-13 18:05:50 -0600590 shost = iscsi_session_to_shost(session);
591 scsi_host_put(shost);
Mike Christie632248a2009-08-20 15:11:01 -0500592 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600593 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600594}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Mike Christie7b8631b2006-01-13 18:05:50 -0600596static int iscsi_is_session_dev(const struct device *dev)
597{
598 return dev->release == iscsi_session_release;
599}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Mike Christiea4804cd2008-05-21 15:54:00 -0500601static int iscsi_iter_session_fn(struct device *dev, void *data)
602{
603 void (* fn) (struct iscsi_cls_session *) = data;
604
605 if (!iscsi_is_session_dev(dev))
606 return 0;
607 fn(iscsi_dev_to_session(dev));
608 return 0;
609}
610
611void iscsi_host_for_each_session(struct Scsi_Host *shost,
612 void (*fn)(struct iscsi_cls_session *))
613{
614 device_for_each_child(&shost->shost_gendev, fn,
615 iscsi_iter_session_fn);
616}
617EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
618
Mike Christie8aae18a2008-01-31 13:36:48 -0600619/**
620 * iscsi_scan_finished - helper to report when running scans are done
621 * @shost: scsi host
622 * @time: scan run time
623 *
624 * This function can be used by drives like qla4xxx to report to the scsi
625 * layer when the scans it kicked off at module load time are done.
626 */
627int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
628{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500629 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600630 /*
631 * qla4xxx will have kicked off some session unblocks before calling
632 * scsi_scan_host, so just wait for them to complete.
633 */
634 return !atomic_read(&ihost->nr_scans);
635}
636EXPORT_SYMBOL_GPL(iscsi_scan_finished);
637
Mike Christie79706342008-05-21 15:54:12 -0500638struct iscsi_scan_data {
639 unsigned int channel;
640 unsigned int id;
641 unsigned int lun;
642};
643
644static int iscsi_user_scan_session(struct device *dev, void *data)
645{
646 struct iscsi_scan_data *scan_data = data;
647 struct iscsi_cls_session *session;
648 struct Scsi_Host *shost;
649 struct iscsi_cls_host *ihost;
650 unsigned long flags;
651 unsigned int id;
652
653 if (!iscsi_is_session_dev(dev))
654 return 0;
655
656 session = iscsi_dev_to_session(dev);
Mike Christie632248a2009-08-20 15:11:01 -0500657
658 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
659
Mike Christie79706342008-05-21 15:54:12 -0500660 shost = iscsi_session_to_shost(session);
661 ihost = shost->shost_data;
662
663 mutex_lock(&ihost->mutex);
664 spin_lock_irqsave(&session->lock, flags);
665 if (session->state != ISCSI_SESSION_LOGGED_IN) {
666 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie632248a2009-08-20 15:11:01 -0500667 goto user_scan_exit;
Mike Christie79706342008-05-21 15:54:12 -0500668 }
669 id = session->target_id;
670 spin_unlock_irqrestore(&session->lock, flags);
671
672 if (id != ISCSI_MAX_TARGET) {
673 if ((scan_data->channel == SCAN_WILD_CARD ||
674 scan_data->channel == 0) &&
675 (scan_data->id == SCAN_WILD_CARD ||
676 scan_data->id == id))
677 scsi_scan_target(&session->dev, 0, id,
678 scan_data->lun, 1);
679 }
Mike Christie632248a2009-08-20 15:11:01 -0500680
681user_scan_exit:
Mike Christie79706342008-05-21 15:54:12 -0500682 mutex_unlock(&ihost->mutex);
Mike Christie632248a2009-08-20 15:11:01 -0500683 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
Mike Christie79706342008-05-21 15:54:12 -0500684 return 0;
685}
686
Mike Christie30a6c652006-04-06 21:13:39 -0500687static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
688 uint id, uint lun)
689{
Mike Christie79706342008-05-21 15:54:12 -0500690 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500691
Mike Christie79706342008-05-21 15:54:12 -0500692 scan_data.channel = channel;
693 scan_data.id = id;
694 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -0500695
Mike Christie79706342008-05-21 15:54:12 -0500696 return device_for_each_child(&shost->shost_gendev, &scan_data,
697 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -0500698}
699
Mike Christiebd976f62008-01-31 13:36:46 -0600700static void iscsi_scan_session(struct work_struct *work)
701{
702 struct iscsi_cls_session *session =
703 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600704 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500705 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500706 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600707
Mike Christie79706342008-05-21 15:54:12 -0500708 scan_data.channel = 0;
709 scan_data.id = SCAN_WILD_CARD;
710 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -0600711
Mike Christie79706342008-05-21 15:54:12 -0500712 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -0600713 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600714}
715
Mike Christiec01be6d2010-07-22 16:59:49 +0530716/**
717 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
Randy Dunlape6d4ef42010-08-14 13:05:41 -0700718 * @cmd: scsi cmd passed to scsi eh handler
Mike Christiec01be6d2010-07-22 16:59:49 +0530719 *
720 * If the session is down this function will wait for the recovery
721 * timer to fire or for the session to be logged back in. If the
722 * recovery timer fires then FAST_IO_FAIL is returned. The caller
723 * should pass this error value to the scsi eh.
724 */
725int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
726{
727 struct iscsi_cls_session *session =
728 starget_to_session(scsi_target(cmd->device));
729 unsigned long flags;
730 int ret = 0;
731
732 spin_lock_irqsave(&session->lock, flags);
733 while (session->state != ISCSI_SESSION_LOGGED_IN) {
734 if (session->state == ISCSI_SESSION_FREE) {
735 ret = FAST_IO_FAIL;
736 break;
737 }
738 spin_unlock_irqrestore(&session->lock, flags);
739 msleep(1000);
740 spin_lock_irqsave(&session->lock, flags);
741 }
742 spin_unlock_irqrestore(&session->lock, flags);
743 return ret;
744}
745EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
746
David Howellsc4028952006-11-22 14:57:56 +0000747static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500748{
David Howellsc4028952006-11-22 14:57:56 +0000749 struct iscsi_cls_session *session =
750 container_of(work, struct iscsi_cls_session,
751 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600752 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500753
Mike Christie322d7392008-01-31 13:36:52 -0600754 iscsi_cls_session_printk(KERN_INFO, session,
755 "session recovery timed out after %d secs\n",
756 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500757
Mike Christie6eabafb2008-01-31 13:36:43 -0600758 spin_lock_irqsave(&session->lock, flags);
759 switch (session->state) {
760 case ISCSI_SESSION_FAILED:
761 session->state = ISCSI_SESSION_FREE;
762 break;
763 case ISCSI_SESSION_LOGGED_IN:
764 case ISCSI_SESSION_FREE:
765 /* we raced with the unblock's flush */
766 spin_unlock_irqrestore(&session->lock, flags);
767 return;
768 }
769 spin_unlock_irqrestore(&session->lock, flags);
770
Mike Christie30a6c652006-04-06 21:13:39 -0500771 if (session->transport->session_recovery_timedout)
772 session->transport->session_recovery_timedout(session);
773
Mike Christie632248a2009-08-20 15:11:01 -0500774 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500775 scsi_target_unblock(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500776 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500777}
778
Mike Christie45ab33b2008-03-04 13:26:55 -0600779static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500780{
Mike Christie45ab33b2008-03-04 13:26:55 -0600781 struct iscsi_cls_session *session =
782 container_of(work, struct iscsi_cls_session,
783 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600784 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500785 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600786 unsigned long flags;
787
Mike Christie632248a2009-08-20 15:11:01 -0500788 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
Mike Christie45ab33b2008-03-04 13:26:55 -0600789 /*
790 * The recovery and unblock work get run from the same workqueue,
791 * so try to cancel it if it was going to run after this unblock.
792 */
793 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600794 spin_lock_irqsave(&session->lock, flags);
795 session->state = ISCSI_SESSION_LOGGED_IN;
796 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600797 /* start IO */
798 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600799 /*
800 * Only do kernel scanning if the driver is properly hooked into
801 * the async scanning code (drivers like iscsi_tcp do login and
802 * scanning from userspace).
803 */
804 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -0600805 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -0600806 atomic_inc(&ihost->nr_scans);
807 }
Mike Christie632248a2009-08-20 15:11:01 -0500808 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600809}
Mike Christie45ab33b2008-03-04 13:26:55 -0600810
811/**
812 * iscsi_unblock_session - set a session as logged in and start IO.
813 * @session: iscsi session
814 *
815 * Mark a session as ready to accept IO.
816 */
817void iscsi_unblock_session(struct iscsi_cls_session *session)
818{
819 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
820 /*
821 * make sure all the events have completed before tell the driver
822 * it is safe
823 */
824 flush_workqueue(iscsi_eh_timer_workq);
825}
Mike Christie30a6c652006-04-06 21:13:39 -0500826EXPORT_SYMBOL_GPL(iscsi_unblock_session);
827
Mike Christie45ab33b2008-03-04 13:26:55 -0600828static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500829{
Mike Christie45ab33b2008-03-04 13:26:55 -0600830 struct iscsi_cls_session *session =
831 container_of(work, struct iscsi_cls_session,
832 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600833 unsigned long flags;
834
Mike Christie632248a2009-08-20 15:11:01 -0500835 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600836 spin_lock_irqsave(&session->lock, flags);
837 session->state = ISCSI_SESSION_FAILED;
838 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500839 scsi_target_block(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500840 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
Mike Christiefdd46dc2009-11-11 16:34:34 -0600841 if (session->recovery_tmo >= 0)
842 queue_delayed_work(iscsi_eh_timer_workq,
843 &session->recovery_work,
844 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500845}
Mike Christie45ab33b2008-03-04 13:26:55 -0600846
847void iscsi_block_session(struct iscsi_cls_session *session)
848{
849 queue_work(iscsi_eh_timer_workq, &session->block_work);
850}
Mike Christie30a6c652006-04-06 21:13:39 -0500851EXPORT_SYMBOL_GPL(iscsi_block_session);
852
Mike Christie26974782007-12-13 12:43:29 -0600853static void __iscsi_unbind_session(struct work_struct *work)
854{
855 struct iscsi_cls_session *session =
856 container_of(work, struct iscsi_cls_session,
857 unbind_work);
858 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500859 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500860 unsigned long flags;
Mike Christie26974782007-12-13 12:43:29 -0600861
Mike Christie632248a2009-08-20 15:11:01 -0500862 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
863
Mike Christie26974782007-12-13 12:43:29 -0600864 /* Prevent new scans and make sure scanning is not in progress */
865 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -0500866 spin_lock_irqsave(&session->lock, flags);
867 if (session->target_id == ISCSI_MAX_TARGET) {
868 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600869 mutex_unlock(&ihost->mutex);
870 return;
871 }
Mike Christie79706342008-05-21 15:54:12 -0500872 session->target_id = ISCSI_MAX_TARGET;
873 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600874 mutex_unlock(&ihost->mutex);
875
876 scsi_remove_target(&session->dev);
877 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -0500878 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
Mike Christie26974782007-12-13 12:43:29 -0600879}
880
Mike Christie7b8631b2006-01-13 18:05:50 -0600881struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500882iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
883 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600884{
885 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Mike Christie5d91e202008-05-21 15:54:01 -0500887 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -0500888 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600889 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500890 return NULL;
891
Mike Christie7b8631b2006-01-13 18:05:50 -0600892 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500893 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -0600894 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +0000895 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500896 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -0600897 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
898 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -0600899 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -0600900 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -0600901 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -0600902
Mike Christie6a8a0d32006-06-28 12:00:31 -0500903 /* this is released in the dev's release function */
904 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500905 session->dev.parent = &shost->shost_gendev;
906 session->dev.release = iscsi_session_release;
907 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -0500908 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -0500909 session->dd_data = &session[1];
Mike Christie632248a2009-08-20 15:11:01 -0500910
911 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500912 return session;
913}
914EXPORT_SYMBOL_GPL(iscsi_alloc_session);
915
Mike Christie79706342008-05-21 15:54:12 -0500916static int iscsi_get_next_target_id(struct device *dev, void *data)
917{
918 struct iscsi_cls_session *session;
919 unsigned long flags;
920 int err = 0;
921
922 if (!iscsi_is_session_dev(dev))
923 return 0;
924
925 session = iscsi_dev_to_session(dev);
926 spin_lock_irqsave(&session->lock, flags);
927 if (*((unsigned int *) data) == session->target_id)
928 err = -EEXIST;
929 spin_unlock_irqrestore(&session->lock, flags);
930 return err;
931}
932
Mike Christie6a8a0d32006-06-28 12:00:31 -0500933int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500934{
935 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500936 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600937 unsigned long flags;
Mike Christie79706342008-05-21 15:54:12 -0500938 unsigned int id = target_id;
Mike Christie8434aa82006-06-28 12:00:30 -0500939 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500940
Mike Christie30a6c652006-04-06 21:13:39 -0500941 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600942 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -0500943
944 if (id == ISCSI_MAX_TARGET) {
945 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
946 err = device_for_each_child(&shost->shost_gendev, &id,
947 iscsi_get_next_target_id);
948 if (!err)
949 break;
950 }
951
952 if (id == ISCSI_MAX_TARGET) {
953 iscsi_cls_session_printk(KERN_ERR, session,
954 "Too many iscsi targets. Max "
955 "number of targets is %d.\n",
956 ISCSI_MAX_TARGET - 1);
Jaswinder Singh Rajput24add1c2009-06-20 13:29:21 +0530957 err = -EOVERFLOW;
Mike Christie79706342008-05-21 15:54:12 -0500958 goto release_host;
959 }
960 }
961 session->target_id = id;
Mike Christie30a6c652006-04-06 21:13:39 -0500962
Kay Sievers71610f52008-12-03 22:41:36 +0100963 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500964 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600965 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600966 iscsi_cls_session_printk(KERN_ERR, session,
967 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500968 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600969 }
970 transport_register_device(&session->dev);
971
Mike Christie26974782007-12-13 12:43:29 -0600972 spin_lock_irqsave(&sesslock, flags);
973 list_add(&session->sess_list, &sesslist);
974 spin_unlock_irqrestore(&sesslock, flags);
975
Mike Christie26974782007-12-13 12:43:29 -0600976 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -0500977 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500978 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500979
Mike Christie8434aa82006-06-28 12:00:30 -0500980release_host:
981 scsi_host_put(shost);
982 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600983}
Mike Christie8434aa82006-06-28 12:00:30 -0500984EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600985
986/**
Mike Christie8434aa82006-06-28 12:00:30 -0500987 * iscsi_create_session - create iscsi class session
988 * @shost: scsi host
989 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -0500990 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -0500991 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600992 *
Mike Christie8434aa82006-06-28 12:00:30 -0500993 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500994 */
Mike Christie8434aa82006-06-28 12:00:30 -0500995struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500996iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
997 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500998{
999 struct iscsi_cls_session *session;
1000
Mike Christie5d91e202008-05-21 15:54:01 -05001001 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -05001002 if (!session)
1003 return NULL;
1004
Mike Christie6a8a0d32006-06-28 12:00:31 -05001005 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -05001006 iscsi_free_session(session);
1007 return NULL;
1008 }
1009 return session;
1010}
1011EXPORT_SYMBOL_GPL(iscsi_create_session);
1012
Mike Christie26974782007-12-13 12:43:29 -06001013static void iscsi_conn_release(struct device *dev)
1014{
1015 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
1016 struct device *parent = conn->dev.parent;
1017
Mike Christie632248a2009-08-20 15:11:01 -05001018 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
Mike Christie26974782007-12-13 12:43:29 -06001019 kfree(conn);
1020 put_device(parent);
1021}
1022
1023static int iscsi_is_conn_dev(const struct device *dev)
1024{
1025 return dev->release == iscsi_conn_release;
1026}
1027
1028static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
1029{
1030 if (!iscsi_is_conn_dev(dev))
1031 return 0;
1032 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
1033}
1034
Mike Christie8434aa82006-06-28 12:00:30 -05001035void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001036{
Mike Christie30a6c652006-04-06 21:13:39 -05001037 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie26974782007-12-13 12:43:29 -06001038 unsigned long flags;
1039 int err;
Mike Christie30a6c652006-04-06 21:13:39 -05001040
Mike Christie632248a2009-08-20 15:11:01 -05001041 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
1042
Mike Christie26974782007-12-13 12:43:29 -06001043 spin_lock_irqsave(&sesslock, flags);
1044 list_del(&session->sess_list);
1045 spin_unlock_irqrestore(&sesslock, flags);
1046
Mike Christie45ab33b2008-03-04 13:26:55 -06001047 /* make sure there are no blocks/unblocks queued */
1048 flush_workqueue(iscsi_eh_timer_workq);
1049 /* make sure the timedout callout is not running */
1050 if (!cancel_delayed_work(&session->recovery_work))
1051 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -06001052 /*
1053 * If we are blocked let commands flow again. The lld or iscsi
1054 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -06001055 * We assume that LLD will not be calling block/unblock while
1056 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -06001057 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001058 spin_lock_irqsave(&session->lock, flags);
1059 session->state = ISCSI_SESSION_FREE;
1060 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -06001061
Mike Christie45ab33b2008-03-04 13:26:55 -06001062 scsi_target_unblock(&session->dev);
1063 /* flush running scans then delete devices */
Mike Christie06d25af2009-03-05 14:46:02 -06001064 scsi_flush_work(shost);
Mike Christie45ab33b2008-03-04 13:26:55 -06001065 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -05001066
Mike Christie26974782007-12-13 12:43:29 -06001067 /* hw iscsi may not have removed all connections from session */
1068 err = device_for_each_child(&session->dev, NULL,
1069 iscsi_iter_destroy_conn_fn);
1070 if (err)
Mike Christie322d7392008-01-31 13:36:52 -06001071 iscsi_cls_session_printk(KERN_ERR, session,
1072 "Could not delete all connections "
1073 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -05001074
Mike Christie7b8631b2006-01-13 18:05:50 -06001075 transport_unregister_device(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001076
1077 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001078 device_del(&session->dev);
1079}
1080EXPORT_SYMBOL_GPL(iscsi_remove_session);
1081
1082void iscsi_free_session(struct iscsi_cls_session *session)
1083{
Mike Christie632248a2009-08-20 15:11:01 -05001084 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
Mike Christie26974782007-12-13 12:43:29 -06001085 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -05001086 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001087}
Mike Christie8434aa82006-06-28 12:00:30 -05001088EXPORT_SYMBOL_GPL(iscsi_free_session);
1089
1090/**
1091 * iscsi_destroy_session - destroy iscsi session
1092 * @session: iscsi_session
1093 *
1094 * Can be called by a LLD or iscsi_transport. There must not be
1095 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -05001096 */
Mike Christie8434aa82006-06-28 12:00:30 -05001097int iscsi_destroy_session(struct iscsi_cls_session *session)
1098{
1099 iscsi_remove_session(session);
Mike Christie632248a2009-08-20 15:11:01 -05001100 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001101 iscsi_free_session(session);
1102 return 0;
1103}
Mike Christie7b8631b2006-01-13 18:05:50 -06001104EXPORT_SYMBOL_GPL(iscsi_destroy_session);
1105
Mike Christie7b8631b2006-01-13 18:05:50 -06001106/**
1107 * iscsi_create_conn - create iscsi class connection
1108 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -05001109 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -06001110 * @cid: connection id
1111 *
1112 * This can be called from a LLD or iscsi_transport. The connection
1113 * is child of the session so cid must be unique for all connections
1114 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -05001115 *
1116 * Since we do not support MCS, cid will normally be zero. In some cases
1117 * for software iscsi we could be trying to preallocate a connection struct
1118 * in which case there could be two connection structs and cid would be
1119 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -05001120 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001121struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05001122iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -06001123{
1124 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -06001125 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -06001126 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -06001127 int err;
1128
Mike Christie5d91e202008-05-21 15:54:01 -05001129 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001130 if (!conn)
1131 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -05001132 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06001133 conn->dd_data = &conn[1];
1134
Mike Christie22a39fb2011-02-16 15:04:33 -06001135 mutex_init(&conn->ep_mutex);
Mike Christie7b8631b2006-01-13 18:05:50 -06001136 INIT_LIST_HEAD(&conn->conn_list);
1137 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -05001138 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001139
1140 /* this is released in the dev's release function */
1141 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -04001142 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -05001143
Kay Sievers71610f52008-12-03 22:41:36 +01001144 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001145 conn->dev.parent = &session->dev;
1146 conn->dev.release = iscsi_conn_release;
1147 err = device_register(&conn->dev);
1148 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001149 iscsi_cls_session_printk(KERN_ERR, session, "could not "
1150 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001151 goto release_parent_ref;
1152 }
1153 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -06001154
1155 spin_lock_irqsave(&connlock, flags);
1156 list_add(&conn->conn_list, &connlist);
Mike Christie26974782007-12-13 12:43:29 -06001157 spin_unlock_irqrestore(&connlock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05001158
1159 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001160 return conn;
1161
1162release_parent_ref:
1163 put_device(&session->dev);
1164free_conn:
1165 kfree(conn);
1166 return NULL;
1167}
1168
1169EXPORT_SYMBOL_GPL(iscsi_create_conn);
1170
1171/**
1172 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -05001173 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -06001174 *
Mike Christie26974782007-12-13 12:43:29 -06001175 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001176 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001177int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1178{
Mike Christie26974782007-12-13 12:43:29 -06001179 unsigned long flags;
1180
1181 spin_lock_irqsave(&connlock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001182 list_del(&conn->conn_list);
1183 spin_unlock_irqrestore(&connlock, flags);
1184
Mike Christie7b8631b2006-01-13 18:05:50 -06001185 transport_unregister_device(&conn->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001186 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001187 device_unregister(&conn->dev);
1188 return 0;
1189}
Mike Christie7b8631b2006-01-13 18:05:50 -06001190EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1191
1192/*
Mike Christie7b8631b2006-01-13 18:05:50 -06001193 * iscsi interface functions
1194 */
Alex Aizman0896b752005-08-04 19:33:07 -07001195static struct iscsi_internal *
1196iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Alex Aizman0896b752005-08-04 19:33:07 -07001198 struct iscsi_internal *priv;
1199 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Alex Aizman0896b752005-08-04 19:33:07 -07001201 spin_lock_irqsave(&iscsi_transport_lock, flags);
1202 list_for_each_entry(priv, &iscsi_transports, list) {
1203 if (tt == priv->iscsi_transport) {
1204 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1205 return priv;
1206 }
1207 }
1208 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1209 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Alex Aizman0896b752005-08-04 19:33:07 -07001212static int
Michael Chan43514772009-06-08 18:14:41 -07001213iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -05001214{
Michael Chan43514772009-06-08 18:14:41 -07001215 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -07001216}
1217
Mike Christie7b7232f2006-02-01 21:06:49 -06001218int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -07001219 char *data, uint32_t data_size)
1220{
1221 struct nlmsghdr *nlh;
1222 struct sk_buff *skb;
1223 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07001224 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05001225 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001226 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1227 data_size);
1228
Mike Christie790f39a2006-05-18 20:31:39 -05001229 priv = iscsi_if_transport_lookup(conn->transport);
1230 if (!priv)
1231 return -EINVAL;
1232
Mike Christie43a145a2006-10-16 18:09:38 -04001233 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001234 if (!skb) {
Mike Christiee5bd7b542008-09-24 11:46:10 -05001235 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06001236 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1237 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001238 return -ENOMEM;
1239 }
1240
Michael Chan43514772009-06-08 18:14:41 -07001241 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001242 ev = NLMSG_DATA(nlh);
1243 memset(ev, 0, sizeof(*ev));
1244 ev->transport_handle = iscsi_handle(conn->transport);
1245 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05001246 ev->r.recv_req.cid = conn->cid;
1247 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001248 pdu = (char*)ev + sizeof(*ev);
1249 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1250 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1251
Michael Chan43514772009-06-08 18:14:41 -07001252 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001253}
1254EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1255
Michael Chan43514772009-06-08 18:14:41 -07001256int iscsi_offload_mesg(struct Scsi_Host *shost,
1257 struct iscsi_transport *transport, uint32_t type,
1258 char *data, uint16_t data_size)
1259{
1260 struct nlmsghdr *nlh;
1261 struct sk_buff *skb;
1262 struct iscsi_uevent *ev;
1263 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
1264
Michael Chana541f842009-07-29 08:49:52 +00001265 skb = alloc_skb(len, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001266 if (!skb) {
1267 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
1268 return -ENOMEM;
1269 }
1270
1271 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1272 ev = NLMSG_DATA(nlh);
1273 memset(ev, 0, sizeof(*ev));
1274 ev->type = type;
1275 ev->transport_handle = iscsi_handle(transport);
1276 switch (type) {
1277 case ISCSI_KEVENT_PATH_REQ:
1278 ev->r.req_path.host_no = shost->host_no;
1279 break;
1280 case ISCSI_KEVENT_IF_DOWN:
1281 ev->r.notify_if_down.host_no = shost->host_no;
1282 break;
1283 }
1284
1285 memcpy((char *)ev + sizeof(*ev), data, data_size);
1286
Michael Chana541f842009-07-29 08:49:52 +00001287 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001288}
1289EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1290
Mike Christiee5bd7b542008-09-24 11:46:10 -05001291void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07001292{
1293 struct nlmsghdr *nlh;
1294 struct sk_buff *skb;
1295 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05001296 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001297 int len = NLMSG_SPACE(sizeof(*ev));
1298
Mike Christie790f39a2006-05-18 20:31:39 -05001299 priv = iscsi_if_transport_lookup(conn->transport);
1300 if (!priv)
1301 return;
1302
Mike Christie43a145a2006-10-16 18:09:38 -04001303 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001304 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001305 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1306 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07001307 return;
1308 }
1309
Michael Chan43514772009-06-08 18:14:41 -07001310 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001311 ev = NLMSG_DATA(nlh);
1312 ev->transport_handle = iscsi_handle(conn->transport);
1313 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07001314 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05001315 ev->r.connerror.cid = conn->cid;
1316 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001317
Michael Chan43514772009-06-08 18:14:41 -07001318 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001319
Mike Christie322d7392008-01-31 13:36:52 -06001320 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1321 error);
Alex Aizman0896b752005-08-04 19:33:07 -07001322}
Mike Christiee5bd7b542008-09-24 11:46:10 -05001323EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07001324
1325static int
Michael Chan43514772009-06-08 18:14:41 -07001326iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1327 void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07001328{
1329 struct sk_buff *skb;
1330 struct nlmsghdr *nlh;
1331 int len = NLMSG_SPACE(size);
1332 int flags = multi ? NLM_F_MULTI : 0;
1333 int t = done ? NLMSG_DONE : type;
1334
Mike Christie43a145a2006-10-16 18:09:38 -04001335 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05001336 if (!skb) {
1337 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1338 return -ENOMEM;
1339 }
Alex Aizman0896b752005-08-04 19:33:07 -07001340
Michael Chan43514772009-06-08 18:14:41 -07001341 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001342 nlh->nlmsg_flags = flags;
1343 memcpy(NLMSG_DATA(nlh), payload, size);
Michael Chan43514772009-06-08 18:14:41 -07001344 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001345}
1346
1347static int
Mike Christie5b940ad2006-02-01 21:06:56 -06001348iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07001349{
1350 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1351 struct iscsi_stats *stats;
1352 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06001353 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001354 struct nlmsghdr *nlhstat;
1355 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05001356 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001357 int len = NLMSG_SPACE(sizeof(*ev) +
1358 sizeof(struct iscsi_stats) +
1359 sizeof(struct iscsi_stats_custom) *
1360 ISCSI_STATS_CUSTOM_MAX);
1361 int err = 0;
1362
Mike Christie790f39a2006-05-18 20:31:39 -05001363 priv = iscsi_if_transport_lookup(transport);
1364 if (!priv)
1365 return -EINVAL;
1366
Mike Christieb5c7a122006-04-06 21:13:33 -05001367 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07001368 if (!conn)
1369 return -EEXIST;
1370
1371 do {
1372 int actual_size;
1373
Mike Christie43a145a2006-10-16 18:09:38 -04001374 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001375 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06001376 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1377 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001378 return -ENOMEM;
1379 }
1380
Michael Chan43514772009-06-08 18:14:41 -07001381 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07001382 (len - sizeof(*nlhstat)), 0);
1383 evstat = NLMSG_DATA(nlhstat);
1384 memset(evstat, 0, sizeof(*evstat));
1385 evstat->transport_handle = iscsi_handle(conn->transport);
1386 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05001387 evstat->u.get_stats.cid =
1388 ev->u.get_stats.cid;
1389 evstat->u.get_stats.sid =
1390 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07001391 stats = (struct iscsi_stats *)
1392 ((char*)evstat + sizeof(*evstat));
1393 memset(stats, 0, sizeof(*stats));
1394
Mike Christie7b7232f2006-02-01 21:06:49 -06001395 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07001396 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1397 sizeof(struct iscsi_stats) +
1398 sizeof(struct iscsi_stats_custom) *
1399 stats->custom_length);
1400 actual_size -= sizeof(*nlhstat);
1401 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06001402 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07001403 nlhstat->nlmsg_len = actual_size;
1404
Michael Chan43514772009-06-08 18:14:41 -07001405 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1406 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001407 } while (err < 0 && err != -ECONNREFUSED);
1408
1409 return err;
1410}
1411
Mike Christie53cb8a12006-06-28 12:00:32 -05001412/**
Mike Christie26974782007-12-13 12:43:29 -06001413 * iscsi_session_event - send session destr. completion event
1414 * @session: iscsi class session
1415 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05001416 */
Mike Christie26974782007-12-13 12:43:29 -06001417int iscsi_session_event(struct iscsi_cls_session *session,
1418 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05001419{
1420 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05001421 struct Scsi_Host *shost;
1422 struct iscsi_uevent *ev;
1423 struct sk_buff *skb;
1424 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05001425 int rc, len = NLMSG_SPACE(sizeof(*ev));
1426
Mike Christie26974782007-12-13 12:43:29 -06001427 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05001428 if (!priv)
1429 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05001430 shost = iscsi_session_to_shost(session);
1431
Mike Christie43a145a2006-10-16 18:09:38 -04001432 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001433 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001434 iscsi_cls_session_printk(KERN_ERR, session,
1435 "Cannot notify userspace of session "
1436 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001437 return -ENOMEM;
1438 }
1439
Michael Chan43514772009-06-08 18:14:41 -07001440 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Mike Christie53cb8a12006-06-28 12:00:32 -05001441 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06001442 ev->transport_handle = iscsi_handle(session->transport);
1443
1444 ev->type = event;
1445 switch (event) {
1446 case ISCSI_KEVENT_DESTROY_SESSION:
1447 ev->r.d_session.host_no = shost->host_no;
1448 ev->r.d_session.sid = session->sid;
1449 break;
1450 case ISCSI_KEVENT_CREATE_SESSION:
1451 ev->r.c_session_ret.host_no = shost->host_no;
1452 ev->r.c_session_ret.sid = session->sid;
1453 break;
1454 case ISCSI_KEVENT_UNBIND_SESSION:
1455 ev->r.unbind_session.host_no = shost->host_no;
1456 ev->r.unbind_session.sid = session->sid;
1457 break;
1458 default:
Mike Christie322d7392008-01-31 13:36:52 -06001459 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1460 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06001461 kfree_skb(skb);
1462 return -EINVAL;
1463 }
Mike Christie53cb8a12006-06-28 12:00:32 -05001464
1465 /*
1466 * this will occur if the daemon is not up, so we just warn
1467 * the user and when the daemon is restarted it will handle it
1468 */
Michael Chan43514772009-06-08 18:14:41 -07001469 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001470 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06001471 iscsi_cls_session_printk(KERN_ERR, session,
1472 "Cannot notify userspace of session "
1473 "event %u. Check iscsi daemon\n",
1474 event);
Mike Christie632248a2009-08-20 15:11:01 -05001475
1476 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
1477 event, rc);
Mike Christie53cb8a12006-06-28 12:00:32 -05001478 return rc;
1479}
Mike Christie26974782007-12-13 12:43:29 -06001480EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001481
Alex Aizman0896b752005-08-04 19:33:07 -07001482static int
Mike Christied82ff9be2008-05-21 15:54:13 -05001483iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1484 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
Mike Christie40753ca2008-05-21 15:53:56 -05001485 uint16_t cmds_max, uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001486{
1487 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001488 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06001489 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06001490
Mike Christied82ff9be2008-05-21 15:54:13 -05001491 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06001492 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06001493 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001494 return -ENOMEM;
1495
Mike Christie5e7facb2009-03-05 14:46:06 -06001496 shost = iscsi_session_to_shost(session);
1497 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001498 ev->r.c_session_ret.sid = session->sid;
Mike Christie632248a2009-08-20 15:11:01 -05001499 ISCSI_DBG_TRANS_SESSION(session,
1500 "Completed creating transport session\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001501 return 0;
1502}
1503
1504static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001505iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001506{
Mike Christie7b8631b2006-01-13 18:05:50 -06001507 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001508 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001509
Mike Christieb5c7a122006-04-06 21:13:33 -05001510 session = iscsi_session_lookup(ev->u.c_conn.sid);
1511 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001512 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001513 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001514 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001515 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001516
Mike Christie7b7232f2006-02-01 21:06:49 -06001517 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001518 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001519 iscsi_cls_session_printk(KERN_ERR, session,
1520 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001521 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001522 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001523
Mike Christieb5c7a122006-04-06 21:13:33 -05001524 ev->r.c_conn_ret.sid = session->sid;
1525 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie632248a2009-08-20 15:11:01 -05001526
1527 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001528 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001529}
1530
1531static int
1532iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1533{
Mike Christie7b8631b2006-01-13 18:05:50 -06001534 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001535
Mike Christieb5c7a122006-04-06 21:13:33 -05001536 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001537 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001538 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001539
Mike Christie632248a2009-08-20 15:11:01 -05001540 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001541 if (transport->destroy_conn)
1542 transport->destroy_conn(conn);
Mike Christie632248a2009-08-20 15:11:01 -05001543
Mike Christie7b8631b2006-01-13 18:05:50 -06001544 return 0;
1545}
1546
Mike Christiefd7255f2006-04-06 21:13:36 -05001547static int
1548iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1549{
1550 char *data = (char*)ev + sizeof(*ev);
1551 struct iscsi_cls_conn *conn;
1552 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001553 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001554
1555 session = iscsi_session_lookup(ev->u.set_param.sid);
1556 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1557 if (!conn || !session)
1558 return -EINVAL;
1559
1560 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001561 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001562 sscanf(data, "%d", &value);
Mike Christiefdd46dc2009-11-11 16:34:34 -06001563 session->recovery_tmo = value;
Mike Christie30a6c652006-04-06 21:13:39 -05001564 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001565 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001566 err = transport->set_param(conn, ev->u.set_param.param,
1567 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001568 }
1569
1570 return err;
1571}
1572
Mike Christie10eb0f02009-05-13 17:57:38 -05001573static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1574 struct iscsi_uevent *ev, int msg_type)
1575{
1576 struct iscsi_endpoint *ep;
1577 struct sockaddr *dst_addr;
1578 struct Scsi_Host *shost = NULL;
1579 int non_blocking, err = 0;
1580
1581 if (!transport->ep_connect)
1582 return -EINVAL;
1583
1584 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1585 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1586 if (!shost) {
1587 printk(KERN_ERR "ep connect failed. Could not find "
1588 "host no %u\n",
1589 ev->u.ep_connect_through_host.host_no);
1590 return -ENODEV;
1591 }
1592 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1593 } else
1594 non_blocking = ev->u.ep_connect.non_blocking;
1595
1596 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1597 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1598 if (IS_ERR(ep)) {
1599 err = PTR_ERR(ep);
1600 goto release_host;
1601 }
1602
1603 ev->r.ep_connect_ret.handle = ep->id;
1604release_host:
1605 if (shost)
1606 scsi_host_put(shost);
1607 return err;
1608}
1609
Mike Christie22a39fb2011-02-16 15:04:33 -06001610static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
1611 u64 ep_handle)
1612{
1613 struct iscsi_cls_conn *conn;
1614 struct iscsi_endpoint *ep;
1615
1616 if (!transport->ep_disconnect)
1617 return -EINVAL;
1618
1619 ep = iscsi_lookup_endpoint(ep_handle);
1620 if (!ep)
1621 return -EINVAL;
1622 conn = ep->conn;
1623 if (conn) {
1624 mutex_lock(&conn->ep_mutex);
1625 conn->ep = NULL;
1626 mutex_unlock(&conn->ep_mutex);
1627 }
1628
1629 transport->ep_disconnect(ep);
1630 return 0;
1631}
1632
Mike Christie7b8631b2006-01-13 18:05:50 -06001633static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001634iscsi_if_transport_ep(struct iscsi_transport *transport,
1635 struct iscsi_uevent *ev, int msg_type)
1636{
Mike Christied82ff9be2008-05-21 15:54:13 -05001637 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001638 int rc = 0;
1639
1640 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05001641 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001642 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001643 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001644 break;
1645 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1646 if (!transport->ep_poll)
1647 return -EINVAL;
1648
Mike Christied82ff9be2008-05-21 15:54:13 -05001649 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1650 if (!ep)
1651 return -EINVAL;
1652
1653 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001654 ev->u.ep_poll.timeout_ms);
1655 break;
1656 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie22a39fb2011-02-16 15:04:33 -06001657 rc = iscsi_if_ep_disconnect(transport,
1658 ev->u.ep_disconnect.ep_handle);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001659 break;
1660 }
1661 return rc;
1662}
1663
1664static int
Mike Christie01cb2252006-06-28 12:00:22 -05001665iscsi_tgt_dscvr(struct iscsi_transport *transport,
1666 struct iscsi_uevent *ev)
1667{
Mike Christie2174a042007-05-30 12:57:10 -05001668 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001669 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001670 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001671
1672 if (!transport->tgt_dscvr)
1673 return -EINVAL;
1674
Mike Christie2174a042007-05-30 12:57:10 -05001675 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001676 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05001677 printk(KERN_ERR "target discovery could not find host no %u\n",
1678 ev->u.tgt_dscvr.host_no);
1679 return -ENODEV;
1680 }
1681
1682
Mike Christie01cb2252006-06-28 12:00:22 -05001683 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001684 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1685 ev->u.tgt_dscvr.enable, dst_addr);
1686 scsi_host_put(shost);
1687 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001688}
1689
1690static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001691iscsi_set_host_param(struct iscsi_transport *transport,
1692 struct iscsi_uevent *ev)
1693{
1694 char *data = (char*)ev + sizeof(*ev);
1695 struct Scsi_Host *shost;
1696 int err;
1697
1698 if (!transport->set_host_param)
1699 return -ENOSYS;
1700
1701 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001702 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05001703 printk(KERN_ERR "set_host_param could not find host no %u\n",
1704 ev->u.set_host_param.host_no);
1705 return -ENODEV;
1706 }
1707
1708 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1709 data, ev->u.set_host_param.len);
1710 scsi_host_put(shost);
1711 return err;
1712}
1713
1714static int
Michael Chan43514772009-06-08 18:14:41 -07001715iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1716{
1717 struct Scsi_Host *shost;
1718 struct iscsi_path *params;
1719 int err;
1720
1721 if (!transport->set_path)
1722 return -ENOSYS;
1723
1724 shost = scsi_host_lookup(ev->u.set_path.host_no);
1725 if (!shost) {
1726 printk(KERN_ERR "set path could not find host no %u\n",
1727 ev->u.set_path.host_no);
1728 return -ENODEV;
1729 }
1730
1731 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1732 err = transport->set_path(shost, params);
1733
1734 scsi_host_put(shost);
1735 return err;
1736}
1737
1738static int
Mike Christie56c155b2011-07-25 13:48:37 -05001739iscsi_set_iface_params(struct iscsi_transport *transport,
1740 struct iscsi_uevent *ev)
1741{
1742 char *data = (char *)ev + sizeof(*ev);
1743 struct Scsi_Host *shost;
1744 int err;
1745
1746 if (!transport->set_iface_param)
1747 return -ENOSYS;
1748
1749 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
1750 if (!shost) {
1751 printk(KERN_ERR "set_iface_params could not find host no %u\n",
1752 ev->u.set_iface_params.host_no);
1753 return -ENODEV;
1754 }
1755
1756 err = transport->set_iface_param(shost, data,
1757 ev->u.set_iface_params.count);
1758 scsi_host_put(shost);
1759 return err;
1760}
1761
1762static int
Michael Chan43514772009-06-08 18:14:41 -07001763iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07001764{
1765 int err = 0;
1766 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1767 struct iscsi_transport *transport = NULL;
1768 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001769 struct iscsi_cls_session *session;
1770 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05001771 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001772
Michael Chan43514772009-06-08 18:14:41 -07001773 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1774 *group = ISCSI_NL_GRP_UIP;
1775 else
1776 *group = ISCSI_NL_GRP_ISCSID;
1777
Alex Aizman0896b752005-08-04 19:33:07 -07001778 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1779 if (!priv)
1780 return -EINVAL;
1781 transport = priv->iscsi_transport;
1782
Mike Christie7b7232f2006-02-01 21:06:49 -06001783 if (!try_module_get(transport->owner))
1784 return -EINVAL;
1785
Alex Aizman0896b752005-08-04 19:33:07 -07001786 switch (nlh->nlmsg_type) {
1787 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001788 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001789 ev->u.c_session.initial_cmdsn,
1790 ev->u.c_session.cmds_max,
1791 ev->u.c_session.queue_depth);
1792 break;
1793 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001794 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05001795 if (!ep) {
1796 err = -EINVAL;
1797 break;
1798 }
Mike Christied82ff9be2008-05-21 15:54:13 -05001799
1800 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001801 ev->u.c_bound_session.initial_cmdsn,
1802 ev->u.c_bound_session.cmds_max,
1803 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001804 break;
1805 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001806 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001807 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001808 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001809 else
1810 err = -EINVAL;
1811 break;
1812 case ISCSI_UEVENT_UNBIND_SESSION:
1813 session = iscsi_session_lookup(ev->u.d_session.sid);
1814 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06001815 scsi_queue_work(iscsi_session_to_shost(session),
1816 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06001817 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001818 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001819 break;
1820 case ISCSI_UEVENT_CREATE_CONN:
1821 err = iscsi_if_create_conn(transport, ev);
1822 break;
1823 case ISCSI_UEVENT_DESTROY_CONN:
1824 err = iscsi_if_destroy_conn(transport, ev);
1825 break;
1826 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001827 session = iscsi_session_lookup(ev->u.b_conn.sid);
1828 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001829
Mike Christie22a39fb2011-02-16 15:04:33 -06001830 if (conn && conn->ep)
1831 iscsi_if_ep_disconnect(transport, conn->ep->id);
1832
1833 if (!session || !conn) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001834 err = -EINVAL;
Mike Christie22a39fb2011-02-16 15:04:33 -06001835 break;
1836 }
1837
1838 ev->r.retcode = transport->bind_conn(session, conn,
1839 ev->u.b_conn.transport_eph,
1840 ev->u.b_conn.is_leading);
1841 if (ev->r.retcode || !transport->ep_connect)
1842 break;
1843
1844 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
1845 if (ep) {
1846 ep->conn = conn;
1847
1848 mutex_lock(&conn->ep_mutex);
1849 conn->ep = ep;
1850 mutex_unlock(&conn->ep_mutex);
1851 } else
1852 iscsi_cls_conn_printk(KERN_ERR, conn,
1853 "Could not set ep conn "
1854 "binding\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001855 break;
1856 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001857 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001858 break;
1859 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001860 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001861 if (conn)
1862 ev->r.retcode = transport->start_conn(conn);
1863 else
1864 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001865 break;
1866 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001867 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001868 if (conn)
1869 transport->stop_conn(conn, ev->u.stop_conn.flag);
1870 else
1871 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001872 break;
1873 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001874 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001875 if (conn)
1876 ev->r.retcode = transport->send_pdu(conn,
1877 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1878 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1879 ev->u.send_pdu.data_size);
1880 else
1881 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001882 break;
1883 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001884 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001885 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001886 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1887 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1888 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001889 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001890 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1891 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001892 case ISCSI_UEVENT_TGT_DSCVR:
1893 err = iscsi_tgt_dscvr(transport, ev);
1894 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001895 case ISCSI_UEVENT_SET_HOST_PARAM:
1896 err = iscsi_set_host_param(transport, ev);
1897 break;
Michael Chan43514772009-06-08 18:14:41 -07001898 case ISCSI_UEVENT_PATH_UPDATE:
1899 err = iscsi_set_path(transport, ev);
1900 break;
Mike Christie56c155b2011-07-25 13:48:37 -05001901 case ISCSI_UEVENT_SET_IFACE_PARAMS:
1902 err = iscsi_set_iface_params(transport, ev);
1903 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001904 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001905 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001906 break;
1907 }
1908
Mike Christie7b7232f2006-02-01 21:06:49 -06001909 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001910 return err;
1911}
1912
Mike Christieb5c7a122006-04-06 21:13:33 -05001913/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001914 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1915 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001916 */
Alex Aizman0896b752005-08-04 19:33:07 -07001917static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001918iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001919{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001920 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001921 while (skb->len >= NLMSG_SPACE(0)) {
1922 int err;
1923 uint32_t rlen;
1924 struct nlmsghdr *nlh;
1925 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07001926 uint32_t group;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001927
1928 nlh = nlmsg_hdr(skb);
1929 if (nlh->nlmsg_len < sizeof(*nlh) ||
1930 skb->len < nlh->nlmsg_len) {
1931 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001932 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001933
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001934 ev = NLMSG_DATA(nlh);
1935 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1936 if (rlen > skb->len)
1937 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001938
Michael Chan43514772009-06-08 18:14:41 -07001939 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001940 if (err) {
1941 ev->type = ISCSI_KEVENT_IF_ERROR;
1942 ev->iferror = err;
1943 }
1944 do {
1945 /*
1946 * special case for GET_STATS:
1947 * on success - sending reply and stats from
1948 * inside of if_recv_msg(),
1949 * on error - fall through.
1950 */
1951 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001952 break;
Michael Chan43514772009-06-08 18:14:41 -07001953 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001954 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1955 } while (err < 0 && err != -ECONNREFUSED);
1956 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001957 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001958 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001959}
1960
Mike Christiefd7255f2006-04-06 21:13:36 -05001961#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01001962struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05001963 __ATTR(_name,_mode,_show,_store)
1964
Alex Aizman0896b752005-08-04 19:33:07 -07001965/*
1966 * iSCSI connection attrs
1967 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001968#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001970show_conn_param_##param(struct device *dev, \
1971 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001972{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001973 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001974 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001975 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001976}
1977
Mike Christiea54a52c2006-06-28 12:00:23 -05001978#define iscsi_conn_attr(field, param) \
1979 iscsi_conn_attr_show(param) \
1980static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001981 NULL);
1982
Mike Christiea54a52c2006-06-28 12:00:23 -05001983iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1984iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1985iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1986iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1987iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1988iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1989iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
Mike Christiea54a52c2006-06-28 12:00:23 -05001990iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1991iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001992iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1993iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Mike Christie289324b2011-02-16 15:04:37 -06001995#define iscsi_conn_ep_attr_show(param) \
1996static ssize_t show_conn_ep_param_##param(struct device *dev, \
1997 struct device_attribute *attr,\
1998 char *buf) \
1999{ \
2000 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
2001 struct iscsi_transport *t = conn->transport; \
2002 struct iscsi_endpoint *ep; \
2003 ssize_t rc; \
2004 \
2005 /* \
2006 * Need to make sure ep_disconnect does not free the LLD's \
2007 * interconnect resources while we are trying to read them. \
2008 */ \
2009 mutex_lock(&conn->ep_mutex); \
2010 ep = conn->ep; \
2011 if (!ep && t->ep_connect) { \
2012 mutex_unlock(&conn->ep_mutex); \
2013 return -ENOTCONN; \
2014 } \
2015 \
2016 if (ep) \
2017 rc = t->get_ep_param(ep, param, buf); \
2018 else \
2019 rc = t->get_conn_param(conn, param, buf); \
2020 mutex_unlock(&conn->ep_mutex); \
2021 return rc; \
2022}
2023
2024#define iscsi_conn_ep_attr(field, param) \
2025 iscsi_conn_ep_attr_show(param) \
2026static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
2027 show_conn_ep_param_##param, NULL);
2028
2029iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
2030iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
2031
Mike Christie3128c6c2011-07-25 13:48:42 -05002032static struct attribute *iscsi_conn_attrs[] = {
2033 &dev_attr_conn_max_recv_dlength.attr,
2034 &dev_attr_conn_max_xmit_dlength.attr,
2035 &dev_attr_conn_header_digest.attr,
2036 &dev_attr_conn_data_digest.attr,
2037 &dev_attr_conn_ifmarker.attr,
2038 &dev_attr_conn_ofmarker.attr,
2039 &dev_attr_conn_address.attr,
2040 &dev_attr_conn_port.attr,
2041 &dev_attr_conn_exp_statsn.attr,
2042 &dev_attr_conn_persistent_address.attr,
2043 &dev_attr_conn_persistent_port.attr,
2044 &dev_attr_conn_ping_tmo.attr,
2045 &dev_attr_conn_recv_tmo.attr,
2046 NULL,
2047};
2048
2049static mode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
2050 struct attribute *attr, int i)
2051{
2052 struct device *cdev = container_of(kobj, struct device, kobj);
2053 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
2054 struct iscsi_transport *t = conn->transport;
2055 int param;
2056
2057 if (attr == &dev_attr_conn_max_recv_dlength.attr)
2058 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
2059 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
2060 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
2061 else if (attr == &dev_attr_conn_header_digest.attr)
2062 param = ISCSI_PARAM_HDRDGST_EN;
2063 else if (attr == &dev_attr_conn_data_digest.attr)
2064 param = ISCSI_PARAM_DATADGST_EN;
2065 else if (attr == &dev_attr_conn_ifmarker.attr)
2066 param = ISCSI_PARAM_IFMARKER_EN;
2067 else if (attr == &dev_attr_conn_ofmarker.attr)
2068 param = ISCSI_PARAM_OFMARKER_EN;
2069 else if (attr == &dev_attr_conn_address.attr)
2070 param = ISCSI_PARAM_CONN_ADDRESS;
2071 else if (attr == &dev_attr_conn_port.attr)
2072 param = ISCSI_PARAM_CONN_PORT;
2073 else if (attr == &dev_attr_conn_exp_statsn.attr)
2074 param = ISCSI_PARAM_EXP_STATSN;
2075 else if (attr == &dev_attr_conn_persistent_address.attr)
2076 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
2077 else if (attr == &dev_attr_conn_persistent_port.attr)
2078 param = ISCSI_PARAM_PERSISTENT_PORT;
2079 else if (attr == &dev_attr_conn_ping_tmo.attr)
2080 param = ISCSI_PARAM_PING_TMO;
2081 else if (attr == &dev_attr_conn_recv_tmo.attr)
2082 param = ISCSI_PARAM_RECV_TMO;
2083 else {
2084 WARN_ONCE(1, "Invalid conn attr");
2085 return 0;
2086 }
2087
2088 return t->attr_is_visible(ISCSI_PARAM, param);
2089}
2090
2091static struct attribute_group iscsi_conn_group = {
2092 .attrs = iscsi_conn_attrs,
2093 .is_visible = iscsi_conn_attr_is_visible,
2094};
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096/*
Alex Aizman0896b752005-08-04 19:33:07 -07002097 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 */
Mike Christieb2c64162007-05-30 12:57:16 -05002099#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002101show_session_param_##param(struct device *dev, \
2102 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002103{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002104 struct iscsi_cls_session *session = \
2105 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002106 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05002107 \
2108 if (perm && !capable(CAP_SYS_ADMIN)) \
2109 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05002110 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002111}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Mike Christieb2c64162007-05-30 12:57:16 -05002113#define iscsi_session_attr(field, param, perm) \
2114 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05002115static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05002116 NULL);
2117
Mike Christieb2c64162007-05-30 12:57:16 -05002118iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
2119iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
2120iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
2121iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
2122iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
2123iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
2124iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
2125iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
2126iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
2127iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
2128iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
2129iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
2130iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
2131iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06002132iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
2133iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
2134iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002135iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05002136iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05302137iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
2138iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05002139
Mike Christie6eabafb2008-01-31 13:36:43 -06002140static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01002141show_priv_session_state(struct device *dev, struct device_attribute *attr,
2142 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06002143{
Tony Jonesee959b02008-02-22 00:13:36 +01002144 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06002145 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
2146}
2147static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
2148 NULL);
2149
Mike Christiefd7255f2006-04-06 21:13:36 -05002150#define iscsi_priv_session_attr_show(field, format) \
2151static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002152show_priv_session_##field(struct device *dev, \
2153 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002154{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002155 struct iscsi_cls_session *session = \
2156 iscsi_dev_to_session(dev->parent); \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302157 if (session->field == -1) \
2158 return sprintf(buf, "off\n"); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002159 return sprintf(buf, format"\n", session->field); \
2160}
2161
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302162#define iscsi_priv_session_attr_store(field) \
2163static ssize_t \
2164store_priv_session_##field(struct device *dev, \
2165 struct device_attribute *attr, \
2166 const char *buf, size_t count) \
2167{ \
2168 int val; \
2169 char *cp; \
2170 struct iscsi_cls_session *session = \
2171 iscsi_dev_to_session(dev->parent); \
2172 if ((session->state == ISCSI_SESSION_FREE) || \
2173 (session->state == ISCSI_SESSION_FAILED)) \
2174 return -EBUSY; \
2175 if (strncmp(buf, "off", 3) == 0) \
2176 session->field = -1; \
2177 else { \
2178 val = simple_strtoul(buf, &cp, 0); \
2179 if (*cp != '\0' && *cp != '\n') \
2180 return -EINVAL; \
2181 session->field = val; \
2182 } \
2183 return count; \
2184}
2185
2186#define iscsi_priv_session_rw_attr(field, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002187 iscsi_priv_session_attr_show(field, format) \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302188 iscsi_priv_session_attr_store(field) \
Vasiliy Kulikov523f3c82011-02-04 15:24:14 +03002189static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302190 show_priv_session_##field, \
2191 store_priv_session_##field)
2192iscsi_priv_session_rw_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05002193
Mike Christie1819dc82007-05-30 12:57:08 -05002194/*
2195 * iSCSI host attrs
2196 */
2197#define iscsi_host_attr_show(param) \
2198static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002199show_host_param_##param(struct device *dev, \
2200 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05002201{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002202 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05002203 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
2204 return priv->iscsi_transport->get_host_param(shost, param, buf); \
2205}
2206
2207#define iscsi_host_attr(field, param) \
2208 iscsi_host_attr_show(param) \
2209static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
2210 NULL);
2211
Mike Christied8196ed2007-05-30 12:57:25 -05002212iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002213iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05002214iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05002215iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002216
Mike Christiefd7255f2006-04-06 21:13:36 -05002217#define SETUP_PRIV_SESSION_RD_ATTR(field) \
2218do { \
Tony Jonesee959b02008-02-22 00:13:36 +01002219 priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05002220 count++; \
2221} while (0)
2222
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302223#define SETUP_PRIV_SESSION_RW_ATTR(field) \
2224do { \
2225 priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
2226 count++; \
2227} while (0)
Mike Christiea54a52c2006-06-28 12:00:23 -05002228
Mike Christiefd7255f2006-04-06 21:13:36 -05002229#define SETUP_SESSION_RD_ATTR(field, param_flag) \
2230do { \
2231 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01002232 priv->session_attrs[count] = &dev_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05002234 } \
2235} while (0)
2236
Mike Christie1819dc82007-05-30 12:57:08 -05002237#define SETUP_HOST_RD_ATTR(field, param_flag) \
2238do { \
2239 if (tt->host_param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01002240 priv->host_attrs[count] = &dev_attr_host_##field; \
Mike Christie1819dc82007-05-30 12:57:08 -05002241 count++; \
2242 } \
2243} while (0)
2244
Alex Aizman0896b752005-08-04 19:33:07 -07002245static int iscsi_session_match(struct attribute_container *cont,
2246 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Mike Christie7b8631b2006-01-13 18:05:50 -06002248 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07002250 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Alex Aizman0896b752005-08-04 19:33:07 -07002252 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return 0;
2254
Mike Christie7b8631b2006-01-13 18:05:50 -06002255 session = iscsi_dev_to_session(dev);
2256 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07002257 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return 0;
2259
Alex Aizman0896b752005-08-04 19:33:07 -07002260 priv = to_iscsi_internal(shost->transportt);
2261 if (priv->session_cont.ac.class != &iscsi_session_class.class)
2262 return 0;
2263
2264 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
Alex Aizman0896b752005-08-04 19:33:07 -07002267static int iscsi_conn_match(struct attribute_container *cont,
2268 struct device *dev)
2269{
Mike Christie7b8631b2006-01-13 18:05:50 -06002270 struct iscsi_cls_session *session;
2271 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07002272 struct Scsi_Host *shost;
2273 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Alex Aizman0896b752005-08-04 19:33:07 -07002275 if (!iscsi_is_conn_dev(dev))
2276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Mike Christie7b8631b2006-01-13 18:05:50 -06002278 conn = iscsi_dev_to_conn(dev);
2279 session = iscsi_dev_to_session(conn->dev.parent);
2280 shost = iscsi_session_to_shost(session);
2281
Alex Aizman0896b752005-08-04 19:33:07 -07002282 if (!shost->transportt)
2283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Alex Aizman0896b752005-08-04 19:33:07 -07002285 priv = to_iscsi_internal(shost->transportt);
2286 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
2287 return 0;
2288
2289 return &priv->conn_cont.ac == cont;
2290}
2291
Mike Christie30a6c652006-04-06 21:13:39 -05002292static int iscsi_host_match(struct attribute_container *cont,
2293 struct device *dev)
2294{
2295 struct Scsi_Host *shost;
2296 struct iscsi_internal *priv;
2297
2298 if (!scsi_is_host_device(dev))
2299 return 0;
2300
2301 shost = dev_to_shost(dev);
2302 if (!shost->transportt ||
2303 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
2304 return 0;
2305
2306 priv = to_iscsi_internal(shost->transportt);
2307 return &priv->t.host_attrs.ac == cont;
2308}
2309
Mike Christie7b8631b2006-01-13 18:05:50 -06002310struct scsi_transport_template *
2311iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07002312{
2313 struct iscsi_internal *priv;
2314 unsigned long flags;
2315 int count = 0, err;
2316
2317 BUG_ON(!tt);
2318
2319 priv = iscsi_if_transport_lookup(tt);
2320 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002321 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002322
Jes Sorensen24669f752006-01-16 10:31:18 -05002323 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07002324 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002325 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002326 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07002327 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05002328 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06002329 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07002330
Tony Jonesee959b02008-02-22 00:13:36 +01002331 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01002332 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01002333 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07002334 if (err)
2335 goto free_priv;
2336
Tony Jonesee959b02008-02-22 00:13:36 +01002337 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07002338 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01002339 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07002340
Mike Christie30a6c652006-04-06 21:13:39 -05002341 /* host parameters */
2342 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
2343 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
2344 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christie32c6e1b2008-05-21 15:53:58 -05002345 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05002346 transport_container_register(&priv->t.host_attrs);
2347
Mike Christied8196ed2007-05-30 12:57:25 -05002348 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
Mike Christie22236962007-05-30 12:57:24 -05002349 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
Mike Christie1819dc82007-05-30 12:57:08 -05002350 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05002351 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002352 BUG_ON(count > ISCSI_HOST_ATTRS);
2353 priv->host_attrs[count] = NULL;
2354 count = 0;
2355
Alex Aizman0896b752005-08-04 19:33:07 -07002356 /* connection parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07002357 priv->conn_cont.ac.class = &iscsi_connection_class.class;
2358 priv->conn_cont.ac.match = iscsi_conn_match;
Mike Christie3128c6c2011-07-25 13:48:42 -05002359 priv->conn_cont.ac.grp = &iscsi_conn_group;
Alex Aizman0896b752005-08-04 19:33:07 -07002360 transport_container_register(&priv->conn_cont);
2361
Alex Aizman0896b752005-08-04 19:33:07 -07002362 /* session parameters */
2363 priv->session_cont.ac.attrs = &priv->session_attrs[0];
2364 priv->session_cont.ac.class = &iscsi_session_class.class;
2365 priv->session_cont.ac.match = iscsi_session_match;
2366 transport_container_register(&priv->session_cont);
2367
Mike Christiefd7255f2006-04-06 21:13:36 -05002368 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
2369 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
2370 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
2371 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
2372 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
2373 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
2374 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
2375 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christiea54a52c2006-06-28 12:00:23 -05002376 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
2377 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
Mike Christieb2c64162007-05-30 12:57:16 -05002378 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
2379 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
2380 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
2381 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
Mike Christie843c0a82007-12-13 12:43:20 -06002382 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
Mike Christie4cd49ea2007-12-13 12:43:38 -06002383 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
2384 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002385 SETUP_SESSION_RD_ATTR(tgt_reset_tmo,ISCSI_TGT_RESET_TMO);
Mike Christie88dfd342008-05-21 15:54:16 -05002386 SETUP_SESSION_RD_ATTR(ifacename, ISCSI_IFACE_NAME);
2387 SETUP_SESSION_RD_ATTR(initiatorname, ISCSI_INITIATOR_NAME);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05302388 SETUP_SESSION_RD_ATTR(targetalias, ISCSI_TARGET_ALIAS);
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302389 SETUP_PRIV_SESSION_RW_ATTR(recovery_tmo);
Mike Christie6eabafb2008-01-31 13:36:43 -06002390 SETUP_PRIV_SESSION_RD_ATTR(state);
Mike Christiefd7255f2006-04-06 21:13:36 -05002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07002393 priv->session_attrs[count] = NULL;
Mike Christie8d079132011-07-25 13:48:40 -05002394 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Alex Aizman0896b752005-08-04 19:33:07 -07002396 spin_lock_irqsave(&iscsi_transport_lock, flags);
2397 list_add(&priv->list, &iscsi_transports);
2398 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Alex Aizman0896b752005-08-04 19:33:07 -07002400 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06002401 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Tony Jonesee959b02008-02-22 00:13:36 +01002403unregister_dev:
2404 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05002405 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002406free_priv:
2407 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06002408 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
Alex Aizman0896b752005-08-04 19:33:07 -07002410EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Alex Aizman0896b752005-08-04 19:33:07 -07002412int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Alex Aizman0896b752005-08-04 19:33:07 -07002414 struct iscsi_internal *priv;
2415 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Alex Aizman0896b752005-08-04 19:33:07 -07002417 BUG_ON(!tt);
2418
Arjan van de Ven0b950672006-01-11 13:16:10 +01002419 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002420
2421 priv = iscsi_if_transport_lookup(tt);
2422 BUG_ON (!priv);
2423
Alex Aizman0896b752005-08-04 19:33:07 -07002424 spin_lock_irqsave(&iscsi_transport_lock, flags);
2425 list_del(&priv->list);
2426 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2427
2428 transport_container_unregister(&priv->conn_cont);
2429 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05002430 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07002431
Tony Jonesee959b02008-02-22 00:13:36 +01002432 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
2433 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002434 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002435
2436 return 0;
2437}
2438EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440static __init int iscsi_transport_init(void)
2441{
Alex Aizman0896b752005-08-04 19:33:07 -07002442 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Meelis Roos09492602006-12-17 12:10:26 -06002444 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05002445 ISCSI_TRANSPORT_VERSION);
2446
Mike Christie41be1442007-02-28 17:32:18 -06002447 atomic_set(&iscsi_session_nr, 0);
2448
Alex Aizman0896b752005-08-04 19:33:07 -07002449 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (err)
2451 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07002452
Mike Christied82ff9be2008-05-21 15:54:13 -05002453 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002454 if (err)
2455 goto unregister_transport_class;
2456
Mike Christie8d079132011-07-25 13:48:40 -05002457 err = class_register(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002458 if (err)
2459 goto unregister_endpoint_class;
2460
Mike Christie8d079132011-07-25 13:48:40 -05002461 err = transport_class_register(&iscsi_host_class);
2462 if (err)
2463 goto unregister_iface_class;
2464
Mike Christie30a6c652006-04-06 21:13:39 -05002465 err = transport_class_register(&iscsi_connection_class);
2466 if (err)
2467 goto unregister_host_class;
2468
Alex Aizman0896b752005-08-04 19:33:07 -07002469 err = transport_class_register(&iscsi_session_class);
2470 if (err)
2471 goto unregister_conn_class;
2472
Mike Christied82ff9be2008-05-21 15:54:13 -05002473 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2474 NULL, THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07002475 if (!nls) {
2476 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04002477 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07002478 }
2479
Mike Christied8bf5412007-12-13 12:43:27 -06002480 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2481 if (!iscsi_eh_timer_workq)
2482 goto release_nls;
2483
Mike Christie43a145a2006-10-16 18:09:38 -04002484 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07002485
Mike Christied8bf5412007-12-13 12:43:27 -06002486release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002487 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002488unregister_session_class:
2489 transport_class_unregister(&iscsi_session_class);
2490unregister_conn_class:
2491 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002492unregister_host_class:
2493 transport_class_unregister(&iscsi_host_class);
Mike Christie8d079132011-07-25 13:48:40 -05002494unregister_iface_class:
2495 class_unregister(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002496unregister_endpoint_class:
2497 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002498unregister_transport_class:
2499 class_unregister(&iscsi_transport_class);
2500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
2503static void __exit iscsi_transport_exit(void)
2504{
Mike Christied8bf5412007-12-13 12:43:27 -06002505 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002506 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002507 transport_class_unregister(&iscsi_connection_class);
2508 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002509 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002510 class_unregister(&iscsi_endpoint_class);
Mike Christie8d079132011-07-25 13:48:40 -05002511 class_unregister(&iscsi_iface_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002512 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
2515module_init(iscsi_transport_init);
2516module_exit(iscsi_transport_exit);
2517
Alex Aizman0896b752005-08-04 19:33:07 -07002518MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2519 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2520 "Alex Aizman <itn780@yahoo.com>");
2521MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05002523MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
Stephen Hemminger058548a2010-12-09 09:37:56 -08002524MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);