blob: cde679f1b01d14778fb342d5539b96a003436a25 [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>
Mike Christie90eeb012011-07-25 13:48:50 -050026#include <linux/bsg-lib.h>
Alex Aizman0896b752005-08-04 19:33:07 -070027#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <scsi/scsi.h>
29#include <scsi/scsi_host.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_transport.h>
32#include <scsi/scsi_transport_iscsi.h>
Alex Aizman0896b752005-08-04 19:33:07 -070033#include <scsi/iscsi_if.h>
Mike Christiec01be6d2010-07-22 16:59:49 +053034#include <scsi/scsi_cmnd.h>
Mike Christie90eeb012011-07-25 13:48:50 -050035#include <scsi/scsi_bsg_iscsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Mike Christie4c2133c2008-06-16 10:11:34 -050037#define ISCSI_TRANSPORT_VERSION "2.0-870"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Mike Christie632248a2009-08-20 15:11:01 -050039static int dbg_session;
40module_param_named(debug_session, dbg_session, int,
41 S_IRUGO | S_IWUSR);
42MODULE_PARM_DESC(debug_session,
43 "Turn on debugging for sessions in scsi_transport_iscsi "
44 "module. Set to 1 to turn on, and zero to turn off. Default "
45 "is off.");
46
47static int dbg_conn;
48module_param_named(debug_conn, dbg_conn, int,
49 S_IRUGO | S_IWUSR);
50MODULE_PARM_DESC(debug_conn,
51 "Turn on debugging for connections in scsi_transport_iscsi "
52 "module. Set to 1 to turn on, and zero to turn off. Default "
53 "is off.");
54
55#define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
56 do { \
57 if (dbg_session) \
58 iscsi_cls_session_printk(KERN_INFO, _session, \
59 "%s: " dbg_fmt, \
60 __func__, ##arg); \
61 } while (0);
62
63#define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
64 do { \
65 if (dbg_conn) \
66 iscsi_cls_conn_printk(KERN_INFO, _conn, \
67 "%s: " dbg_fmt, \
68 __func__, ##arg); \
69 } while (0);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071struct iscsi_internal {
72 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070073 struct iscsi_transport *iscsi_transport;
74 struct list_head list;
Tony Jonesee959b02008-02-22 00:13:36 +010075 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050076
Alex Aizman0896b752005-08-04 19:33:07 -070077 struct transport_container conn_cont;
Alex Aizman0896b752005-08-04 19:33:07 -070078 struct transport_container session_cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Mike Christie41be1442007-02-28 17:32:18 -060081static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060082static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050083
Alex Aizman0896b752005-08-04 19:33:07 -070084/*
85 * list of registered transports and lock that must
86 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010087 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070088 */
89static LIST_HEAD(iscsi_transports);
90static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Alex Aizman0896b752005-08-04 19:33:07 -070092#define to_iscsi_internal(tmpl) \
93 container_of(tmpl, struct iscsi_internal, t)
94
Tony Jonesee959b02008-02-22 00:13:36 +010095#define dev_to_iscsi_internal(_dev) \
96 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -070097
Tony Jonesee959b02008-02-22 00:13:36 +010098static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -070099{
Tony Jonesee959b02008-02-22 00:13:36 +0100100 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -0700101 kfree(priv);
102}
103
104/*
105 * iscsi_transport_class represents the iscsi_transports that are
106 * registered.
107 */
108static struct class iscsi_transport_class = {
109 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +0100110 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -0700111};
112
113static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100114show_transport_handle(struct device *dev, struct device_attribute *attr,
115 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -0700116{
Tony Jonesee959b02008-02-22 00:13:36 +0100117 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Mike Christie762e2bf2005-09-12 21:01:46 -0500118 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -0700119}
Tony Jonesee959b02008-02-22 00:13:36 +0100120static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700121
122#define show_transport_attr(name, format) \
123static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100124show_transport_##name(struct device *dev, \
125 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -0700126{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100127 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Alex Aizman0896b752005-08-04 19:33:07 -0700128 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
129} \
Tony Jonesee959b02008-02-22 00:13:36 +0100130static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700131
132show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700133
134static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100135 &dev_attr_handle.attr,
136 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700137 NULL,
138};
139
140static struct attribute_group iscsi_transport_group = {
141 .attrs = iscsi_transport_attrs,
142};
143
Mike Christied82ff9be2008-05-21 15:54:13 -0500144/*
145 * iSCSI endpoint attrs
146 */
147#define iscsi_dev_to_endpoint(_dev) \
148 container_of(_dev, struct iscsi_endpoint, dev)
149
150#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
151struct device_attribute dev_attr_##_prefix##_##_name = \
152 __ATTR(_name,_mode,_show,_store)
153
154static void iscsi_endpoint_release(struct device *dev)
155{
156 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
157 kfree(ep);
158}
159
160static struct class iscsi_endpoint_class = {
161 .name = "iscsi_endpoint",
162 .dev_release = iscsi_endpoint_release,
163};
164
165static ssize_t
166show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
167{
168 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500169 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500170}
171static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
172
173static struct attribute *iscsi_endpoint_attrs[] = {
174 &dev_attr_ep_handle.attr,
175 NULL,
176};
177
178static struct attribute_group iscsi_endpoint_group = {
179 .attrs = iscsi_endpoint_attrs,
180};
181
182#define ISCSI_MAX_EPID -1
183
184static int iscsi_match_epid(struct device *dev, void *data)
185{
186 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500187 uint64_t *epid = (uint64_t *) data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500188
189 return *epid == ep->id;
190}
191
192struct iscsi_endpoint *
193iscsi_create_endpoint(int dd_size)
194{
195 struct device *dev;
196 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500197 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500198 int err;
199
200 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400201 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500202 iscsi_match_epid);
203 if (!dev)
204 break;
205 }
206 if (id == ISCSI_MAX_EPID) {
207 printk(KERN_ERR "Too many connections. Max supported %u\n",
208 ISCSI_MAX_EPID - 1);
209 return NULL;
210 }
211
212 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
213 if (!ep)
214 return NULL;
215
216 ep->id = id;
217 ep->dev.class = &iscsi_endpoint_class;
Kay Sievers71610f52008-12-03 22:41:36 +0100218 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500219 err = device_register(&ep->dev);
220 if (err)
221 goto free_ep;
222
223 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
224 if (err)
225 goto unregister_dev;
226
227 if (dd_size)
228 ep->dd_data = &ep[1];
229 return ep;
230
231unregister_dev:
232 device_unregister(&ep->dev);
233 return NULL;
234
235free_ep:
236 kfree(ep);
237 return NULL;
238}
239EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
240
241void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
242{
243 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
244 device_unregister(&ep->dev);
245}
246EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
247
248struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
249{
Mike Christief80f8682008-06-16 10:11:35 -0500250 struct iscsi_endpoint *ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500251 struct device *dev;
252
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400253 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500254 iscsi_match_epid);
255 if (!dev)
256 return NULL;
257
Mike Christief80f8682008-06-16 10:11:35 -0500258 ep = iscsi_dev_to_endpoint(dev);
259 /*
260 * we can drop this now because the interface will prevent
261 * removals and lookups from racing.
262 */
263 put_device(dev);
264 return ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500265}
266EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
267
Mike Christie8d079132011-07-25 13:48:40 -0500268/*
269 * Interface to display network param to sysfs
270 */
271
272static void iscsi_iface_release(struct device *dev)
273{
274 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
275 struct device *parent = iface->dev.parent;
276
277 kfree(iface);
278 put_device(parent);
279}
280
281
282static struct class iscsi_iface_class = {
283 .name = "iscsi_iface",
284 .dev_release = iscsi_iface_release,
285};
286
287#define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
288struct device_attribute dev_attr_##_prefix##_##_name = \
289 __ATTR(_name, _mode, _show, _store)
290
291/* iface attrs show */
292#define iscsi_iface_attr_show(type, name, param_type, param) \
293static ssize_t \
294show_##type##_##name(struct device *dev, struct device_attribute *attr, \
295 char *buf) \
296{ \
297 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
298 struct iscsi_transport *t = iface->transport; \
299 return t->get_iface_param(iface, param_type, param, buf); \
300} \
301
302#define iscsi_iface_net_attr(type, name, param) \
303 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
304static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
305
306/* generic read only ipvi4 attribute */
307iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
308iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
309iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
310iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
311
312/* generic read only ipv6 attribute */
313iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
314iscsi_iface_net_attr(ipv6_iface, link_local_addr, ISCSI_NET_PARAM_IPV6_LINKLOCAL);
315iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
316iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
317 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
318iscsi_iface_net_attr(ipv6_iface, linklocal_autocfg,
319 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
320
321/* common read only iface attribute */
322iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
323iscsi_iface_net_attr(iface, vlan, ISCSI_NET_PARAM_VLAN_ID);
Mike Christie4223b9e2011-07-25 13:48:47 -0500324iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
325iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
Mike Christie8d079132011-07-25 13:48:40 -0500326
327static mode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
328 struct attribute *attr, int i)
329{
330 struct device *dev = container_of(kobj, struct device, kobj);
331 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
332 struct iscsi_transport *t = iface->transport;
Mike Christieb78dbba2011-07-25 13:48:44 -0500333 int param;
Mike Christie8d079132011-07-25 13:48:40 -0500334
335 if (attr == &dev_attr_iface_enabled.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500336 param = ISCSI_NET_PARAM_IFACE_ENABLE;
Mike Christie8d079132011-07-25 13:48:40 -0500337 else if (attr == &dev_attr_iface_vlan.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500338 param = ISCSI_NET_PARAM_VLAN_ID;
Mike Christie4223b9e2011-07-25 13:48:47 -0500339 else if (attr == &dev_attr_iface_vlan_priority.attr)
340 param = ISCSI_NET_PARAM_VLAN_PRIORITY;
341 else if (attr == &dev_attr_iface_vlan_enabled.attr)
342 param = ISCSI_NET_PARAM_VLAN_ENABLED;
Mike Christieb78dbba2011-07-25 13:48:44 -0500343 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
Mike Christie8d079132011-07-25 13:48:40 -0500344 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500345 param = ISCSI_NET_PARAM_IPV4_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500346 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500347 param = ISCSI_NET_PARAM_IPV4_GW;
Mike Christie8d079132011-07-25 13:48:40 -0500348 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500349 param = ISCSI_NET_PARAM_IPV4_SUBNET;
Mike Christie8d079132011-07-25 13:48:40 -0500350 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500351 param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
352 else
353 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500354 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
355 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500356 param = ISCSI_NET_PARAM_IPV6_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500357 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500358 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
Mike Christie8d079132011-07-25 13:48:40 -0500359 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500360 param = ISCSI_NET_PARAM_IPV6_ROUTER;
Mike Christie8d079132011-07-25 13:48:40 -0500361 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500362 param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
Mike Christie8d079132011-07-25 13:48:40 -0500363 else if (attr == &dev_attr_ipv6_iface_linklocal_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500364 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
365 else
366 return 0;
367 } else {
368 WARN_ONCE(1, "Invalid iface attr");
369 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500370 }
371
Mike Christieb78dbba2011-07-25 13:48:44 -0500372 return t->attr_is_visible(ISCSI_NET_PARAM, param);
Mike Christie8d079132011-07-25 13:48:40 -0500373}
374
375static struct attribute *iscsi_iface_attrs[] = {
376 &dev_attr_iface_enabled.attr,
377 &dev_attr_iface_vlan.attr,
Mike Christie4223b9e2011-07-25 13:48:47 -0500378 &dev_attr_iface_vlan_priority.attr,
379 &dev_attr_iface_vlan_enabled.attr,
Mike Christie8d079132011-07-25 13:48:40 -0500380 &dev_attr_ipv4_iface_ipaddress.attr,
381 &dev_attr_ipv4_iface_gateway.attr,
382 &dev_attr_ipv4_iface_subnet.attr,
383 &dev_attr_ipv4_iface_bootproto.attr,
384 &dev_attr_ipv6_iface_ipaddress.attr,
385 &dev_attr_ipv6_iface_link_local_addr.attr,
386 &dev_attr_ipv6_iface_router_addr.attr,
387 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
388 &dev_attr_ipv6_iface_linklocal_autocfg.attr,
389 NULL,
390};
391
392static struct attribute_group iscsi_iface_group = {
393 .attrs = iscsi_iface_attrs,
394 .is_visible = iscsi_iface_attr_is_visible,
395};
396
397struct iscsi_iface *
398iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
399 uint32_t iface_type, uint32_t iface_num, int dd_size)
400{
401 struct iscsi_iface *iface;
402 int err;
403
404 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
405 if (!iface)
406 return NULL;
407
408 iface->transport = transport;
409 iface->iface_type = iface_type;
410 iface->iface_num = iface_num;
411 iface->dev.release = iscsi_iface_release;
412 iface->dev.class = &iscsi_iface_class;
413 /* parent reference released in iscsi_iface_release */
414 iface->dev.parent = get_device(&shost->shost_gendev);
415 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
416 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
417 iface_num);
418 else
419 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
420 iface_num);
421
422 err = device_register(&iface->dev);
423 if (err)
424 goto free_iface;
425
426 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
427 if (err)
428 goto unreg_iface;
429
430 if (dd_size)
431 iface->dd_data = &iface[1];
432 return iface;
433
434unreg_iface:
435 device_unregister(&iface->dev);
436 return NULL;
437
438free_iface:
439 put_device(iface->dev.parent);
440 kfree(iface);
441 return NULL;
442}
443EXPORT_SYMBOL_GPL(iscsi_create_iface);
444
445void iscsi_destroy_iface(struct iscsi_iface *iface)
446{
447 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
448 device_unregister(&iface->dev);
449}
450EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
451
Mike Christie90eeb012011-07-25 13:48:50 -0500452/*
453 * BSG support
454 */
455/**
456 * iscsi_bsg_host_dispatch - Dispatch command to LLD.
457 * @job: bsg job to be processed
458 */
459static int iscsi_bsg_host_dispatch(struct bsg_job *job)
460{
461 struct Scsi_Host *shost = iscsi_job_to_shost(job);
462 struct iscsi_bsg_request *req = job->request;
463 struct iscsi_bsg_reply *reply = job->reply;
464 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
465 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
466 int ret;
467
468 /* check if we have the msgcode value at least */
469 if (job->request_len < sizeof(uint32_t)) {
470 ret = -ENOMSG;
471 goto fail_host_msg;
472 }
473
474 /* Validate the host command */
475 switch (req->msgcode) {
476 case ISCSI_BSG_HST_VENDOR:
477 cmdlen += sizeof(struct iscsi_bsg_host_vendor);
478 if ((shost->hostt->vendor_id == 0L) ||
479 (req->rqst_data.h_vendor.vendor_id !=
480 shost->hostt->vendor_id)) {
481 ret = -ESRCH;
482 goto fail_host_msg;
483 }
484 break;
485 default:
486 ret = -EBADR;
487 goto fail_host_msg;
488 }
489
490 /* check if we really have all the request data needed */
491 if (job->request_len < cmdlen) {
492 ret = -ENOMSG;
493 goto fail_host_msg;
494 }
495
496 ret = i->iscsi_transport->bsg_request(job);
497 if (!ret)
498 return 0;
499
500fail_host_msg:
501 /* return the errno failure code as the only status */
502 BUG_ON(job->reply_len < sizeof(uint32_t));
503 reply->reply_payload_rcv_len = 0;
504 reply->result = ret;
505 job->reply_len = sizeof(uint32_t);
506 bsg_job_done(job, ret, 0);
507 return 0;
508}
509
510/**
511 * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
512 * @shost: shost for iscsi_host
513 * @cls_host: iscsi_cls_host adding the structures to
514 */
515static int
516iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
517{
518 struct device *dev = &shost->shost_gendev;
519 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
520 struct request_queue *q;
521 char bsg_name[20];
522 int ret;
523
524 if (!i->iscsi_transport->bsg_request)
525 return -ENOTSUPP;
526
527 snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
528
529 q = __scsi_alloc_queue(shost, bsg_request_fn);
530 if (!q)
531 return -ENOMEM;
532
533 ret = bsg_setup_queue(dev, q, bsg_name, iscsi_bsg_host_dispatch, 0);
534 if (ret) {
535 shost_printk(KERN_ERR, shost, "bsg interface failed to "
536 "initialize - no request queue\n");
537 blk_cleanup_queue(q);
538 return ret;
539 }
540
541 ihost->bsg_q = q;
542 return 0;
543}
544
Mike Christie30a6c652006-04-06 21:13:39 -0500545static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100546 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500547{
548 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500549 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500550
551 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -0600552 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -0500553 mutex_init(&ihost->mutex);
Mike Christie90eeb012011-07-25 13:48:50 -0500554
555 iscsi_bsg_host_add(shost, ihost);
556 /* ignore any bsg add error - we just can't do sgio */
557
558 return 0;
559}
560
561static int iscsi_remove_host(struct transport_container *tc,
562 struct device *dev, struct device *cdev)
563{
564 struct Scsi_Host *shost = dev_to_shost(dev);
565 struct iscsi_cls_host *ihost = shost->shost_data;
566
567 if (ihost->bsg_q) {
568 bsg_remove_queue(ihost->bsg_q);
569 blk_cleanup_queue(ihost->bsg_q);
570 }
Mike Christie30a6c652006-04-06 21:13:39 -0500571 return 0;
572}
573
574static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
575 "iscsi_host",
576 iscsi_setup_host,
Mike Christie90eeb012011-07-25 13:48:50 -0500577 iscsi_remove_host,
Mike Christie30a6c652006-04-06 21:13:39 -0500578 NULL);
579
Alex Aizman0896b752005-08-04 19:33:07 -0700580static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
581 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 NULL,
583 NULL,
584 NULL);
585
Alex Aizman0896b752005-08-04 19:33:07 -0700586static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
587 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 NULL,
589 NULL,
590 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700591
592static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100593static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700594
Mike Christie7b7232f2006-02-01 21:06:49 -0600595static LIST_HEAD(sesslist);
596static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700597static LIST_HEAD(connlist);
598static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Mike Christieb5c7a122006-04-06 21:13:33 -0500600static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
601{
602 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
603 return sess->sid;
604}
605
606/*
607 * Returns the matching session to a given sid
608 */
609static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600610{
611 unsigned long flags;
612 struct iscsi_cls_session *sess;
613
614 spin_lock_irqsave(&sesslock, flags);
615 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500616 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600617 spin_unlock_irqrestore(&sesslock, flags);
618 return sess;
619 }
620 }
621 spin_unlock_irqrestore(&sesslock, flags);
622 return NULL;
623}
624
Mike Christieb5c7a122006-04-06 21:13:33 -0500625/*
626 * Returns the matching connection to a given sid / cid tuple
627 */
628static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600629{
630 unsigned long flags;
631 struct iscsi_cls_conn *conn;
632
633 spin_lock_irqsave(&connlock, flags);
634 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500635 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600636 spin_unlock_irqrestore(&connlock, flags);
637 return conn;
638 }
639 }
640 spin_unlock_irqrestore(&connlock, flags);
641 return NULL;
642}
643
Mike Christie7b8631b2006-01-13 18:05:50 -0600644/*
645 * The following functions can be used by LLDs that allocate
646 * their own scsi_hosts or by software iscsi LLDs
647 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600648static struct {
649 int value;
650 char *name;
651} iscsi_session_state_names[] = {
652 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
653 { ISCSI_SESSION_FAILED, "FAILED" },
654 { ISCSI_SESSION_FREE, "FREE" },
655};
656
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200657static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600658{
659 int i;
660 char *name = NULL;
661
662 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
663 if (iscsi_session_state_names[i].value == state) {
664 name = iscsi_session_state_names[i].name;
665 break;
666 }
667 }
668 return name;
669}
670
671int iscsi_session_chkready(struct iscsi_cls_session *session)
672{
673 unsigned long flags;
674 int err;
675
676 spin_lock_irqsave(&session->lock, flags);
677 switch (session->state) {
678 case ISCSI_SESSION_LOGGED_IN:
679 err = 0;
680 break;
681 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -0500682 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600683 break;
684 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500685 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600686 break;
687 default:
688 err = DID_NO_CONNECT << 16;
689 break;
690 }
691 spin_unlock_irqrestore(&session->lock, flags);
692 return err;
693}
694EXPORT_SYMBOL_GPL(iscsi_session_chkready);
695
Mike Christie7b8631b2006-01-13 18:05:50 -0600696static void iscsi_session_release(struct device *dev)
697{
698 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600699 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Mike Christie7b8631b2006-01-13 18:05:50 -0600701 shost = iscsi_session_to_shost(session);
702 scsi_host_put(shost);
Mike Christie632248a2009-08-20 15:11:01 -0500703 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600704 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600705}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Mike Christie7b8631b2006-01-13 18:05:50 -0600707static int iscsi_is_session_dev(const struct device *dev)
708{
709 return dev->release == iscsi_session_release;
710}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Mike Christiea4804cd2008-05-21 15:54:00 -0500712static int iscsi_iter_session_fn(struct device *dev, void *data)
713{
714 void (* fn) (struct iscsi_cls_session *) = data;
715
716 if (!iscsi_is_session_dev(dev))
717 return 0;
718 fn(iscsi_dev_to_session(dev));
719 return 0;
720}
721
722void iscsi_host_for_each_session(struct Scsi_Host *shost,
723 void (*fn)(struct iscsi_cls_session *))
724{
725 device_for_each_child(&shost->shost_gendev, fn,
726 iscsi_iter_session_fn);
727}
728EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
729
Mike Christie8aae18a2008-01-31 13:36:48 -0600730/**
731 * iscsi_scan_finished - helper to report when running scans are done
732 * @shost: scsi host
733 * @time: scan run time
734 *
735 * This function can be used by drives like qla4xxx to report to the scsi
736 * layer when the scans it kicked off at module load time are done.
737 */
738int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
739{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500740 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600741 /*
742 * qla4xxx will have kicked off some session unblocks before calling
743 * scsi_scan_host, so just wait for them to complete.
744 */
745 return !atomic_read(&ihost->nr_scans);
746}
747EXPORT_SYMBOL_GPL(iscsi_scan_finished);
748
Mike Christie79706342008-05-21 15:54:12 -0500749struct iscsi_scan_data {
750 unsigned int channel;
751 unsigned int id;
752 unsigned int lun;
753};
754
755static int iscsi_user_scan_session(struct device *dev, void *data)
756{
757 struct iscsi_scan_data *scan_data = data;
758 struct iscsi_cls_session *session;
759 struct Scsi_Host *shost;
760 struct iscsi_cls_host *ihost;
761 unsigned long flags;
762 unsigned int id;
763
764 if (!iscsi_is_session_dev(dev))
765 return 0;
766
767 session = iscsi_dev_to_session(dev);
Mike Christie632248a2009-08-20 15:11:01 -0500768
769 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
770
Mike Christie79706342008-05-21 15:54:12 -0500771 shost = iscsi_session_to_shost(session);
772 ihost = shost->shost_data;
773
774 mutex_lock(&ihost->mutex);
775 spin_lock_irqsave(&session->lock, flags);
776 if (session->state != ISCSI_SESSION_LOGGED_IN) {
777 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie632248a2009-08-20 15:11:01 -0500778 goto user_scan_exit;
Mike Christie79706342008-05-21 15:54:12 -0500779 }
780 id = session->target_id;
781 spin_unlock_irqrestore(&session->lock, flags);
782
783 if (id != ISCSI_MAX_TARGET) {
784 if ((scan_data->channel == SCAN_WILD_CARD ||
785 scan_data->channel == 0) &&
786 (scan_data->id == SCAN_WILD_CARD ||
787 scan_data->id == id))
788 scsi_scan_target(&session->dev, 0, id,
789 scan_data->lun, 1);
790 }
Mike Christie632248a2009-08-20 15:11:01 -0500791
792user_scan_exit:
Mike Christie79706342008-05-21 15:54:12 -0500793 mutex_unlock(&ihost->mutex);
Mike Christie632248a2009-08-20 15:11:01 -0500794 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
Mike Christie79706342008-05-21 15:54:12 -0500795 return 0;
796}
797
Mike Christie30a6c652006-04-06 21:13:39 -0500798static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
799 uint id, uint lun)
800{
Mike Christie79706342008-05-21 15:54:12 -0500801 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500802
Mike Christie79706342008-05-21 15:54:12 -0500803 scan_data.channel = channel;
804 scan_data.id = id;
805 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -0500806
Mike Christie79706342008-05-21 15:54:12 -0500807 return device_for_each_child(&shost->shost_gendev, &scan_data,
808 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -0500809}
810
Mike Christiebd976f62008-01-31 13:36:46 -0600811static void iscsi_scan_session(struct work_struct *work)
812{
813 struct iscsi_cls_session *session =
814 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600815 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500816 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500817 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600818
Mike Christie79706342008-05-21 15:54:12 -0500819 scan_data.channel = 0;
820 scan_data.id = SCAN_WILD_CARD;
821 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -0600822
Mike Christie79706342008-05-21 15:54:12 -0500823 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -0600824 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600825}
826
Mike Christiec01be6d2010-07-22 16:59:49 +0530827/**
828 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
Randy Dunlape6d4ef42010-08-14 13:05:41 -0700829 * @cmd: scsi cmd passed to scsi eh handler
Mike Christiec01be6d2010-07-22 16:59:49 +0530830 *
831 * If the session is down this function will wait for the recovery
832 * timer to fire or for the session to be logged back in. If the
833 * recovery timer fires then FAST_IO_FAIL is returned. The caller
834 * should pass this error value to the scsi eh.
835 */
836int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
837{
838 struct iscsi_cls_session *session =
839 starget_to_session(scsi_target(cmd->device));
840 unsigned long flags;
841 int ret = 0;
842
843 spin_lock_irqsave(&session->lock, flags);
844 while (session->state != ISCSI_SESSION_LOGGED_IN) {
845 if (session->state == ISCSI_SESSION_FREE) {
846 ret = FAST_IO_FAIL;
847 break;
848 }
849 spin_unlock_irqrestore(&session->lock, flags);
850 msleep(1000);
851 spin_lock_irqsave(&session->lock, flags);
852 }
853 spin_unlock_irqrestore(&session->lock, flags);
854 return ret;
855}
856EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
857
David Howellsc4028952006-11-22 14:57:56 +0000858static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500859{
David Howellsc4028952006-11-22 14:57:56 +0000860 struct iscsi_cls_session *session =
861 container_of(work, struct iscsi_cls_session,
862 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600863 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500864
Mike Christie322d7392008-01-31 13:36:52 -0600865 iscsi_cls_session_printk(KERN_INFO, session,
866 "session recovery timed out after %d secs\n",
867 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500868
Mike Christie6eabafb2008-01-31 13:36:43 -0600869 spin_lock_irqsave(&session->lock, flags);
870 switch (session->state) {
871 case ISCSI_SESSION_FAILED:
872 session->state = ISCSI_SESSION_FREE;
873 break;
874 case ISCSI_SESSION_LOGGED_IN:
875 case ISCSI_SESSION_FREE:
876 /* we raced with the unblock's flush */
877 spin_unlock_irqrestore(&session->lock, flags);
878 return;
879 }
880 spin_unlock_irqrestore(&session->lock, flags);
881
Mike Christie30a6c652006-04-06 21:13:39 -0500882 if (session->transport->session_recovery_timedout)
883 session->transport->session_recovery_timedout(session);
884
Mike Christie632248a2009-08-20 15:11:01 -0500885 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500886 scsi_target_unblock(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500887 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500888}
889
Mike Christie45ab33b2008-03-04 13:26:55 -0600890static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500891{
Mike Christie45ab33b2008-03-04 13:26:55 -0600892 struct iscsi_cls_session *session =
893 container_of(work, struct iscsi_cls_session,
894 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600895 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500896 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600897 unsigned long flags;
898
Mike Christie632248a2009-08-20 15:11:01 -0500899 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
Mike Christie45ab33b2008-03-04 13:26:55 -0600900 /*
901 * The recovery and unblock work get run from the same workqueue,
902 * so try to cancel it if it was going to run after this unblock.
903 */
904 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600905 spin_lock_irqsave(&session->lock, flags);
906 session->state = ISCSI_SESSION_LOGGED_IN;
907 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600908 /* start IO */
909 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600910 /*
911 * Only do kernel scanning if the driver is properly hooked into
912 * the async scanning code (drivers like iscsi_tcp do login and
913 * scanning from userspace).
914 */
915 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -0600916 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -0600917 atomic_inc(&ihost->nr_scans);
918 }
Mike Christie632248a2009-08-20 15:11:01 -0500919 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600920}
Mike Christie45ab33b2008-03-04 13:26:55 -0600921
922/**
923 * iscsi_unblock_session - set a session as logged in and start IO.
924 * @session: iscsi session
925 *
926 * Mark a session as ready to accept IO.
927 */
928void iscsi_unblock_session(struct iscsi_cls_session *session)
929{
930 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
931 /*
932 * make sure all the events have completed before tell the driver
933 * it is safe
934 */
935 flush_workqueue(iscsi_eh_timer_workq);
936}
Mike Christie30a6c652006-04-06 21:13:39 -0500937EXPORT_SYMBOL_GPL(iscsi_unblock_session);
938
Mike Christie45ab33b2008-03-04 13:26:55 -0600939static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500940{
Mike Christie45ab33b2008-03-04 13:26:55 -0600941 struct iscsi_cls_session *session =
942 container_of(work, struct iscsi_cls_session,
943 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600944 unsigned long flags;
945
Mike Christie632248a2009-08-20 15:11:01 -0500946 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600947 spin_lock_irqsave(&session->lock, flags);
948 session->state = ISCSI_SESSION_FAILED;
949 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500950 scsi_target_block(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500951 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
Mike Christiefdd46dc2009-11-11 16:34:34 -0600952 if (session->recovery_tmo >= 0)
953 queue_delayed_work(iscsi_eh_timer_workq,
954 &session->recovery_work,
955 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500956}
Mike Christie45ab33b2008-03-04 13:26:55 -0600957
958void iscsi_block_session(struct iscsi_cls_session *session)
959{
960 queue_work(iscsi_eh_timer_workq, &session->block_work);
961}
Mike Christie30a6c652006-04-06 21:13:39 -0500962EXPORT_SYMBOL_GPL(iscsi_block_session);
963
Mike Christie26974782007-12-13 12:43:29 -0600964static void __iscsi_unbind_session(struct work_struct *work)
965{
966 struct iscsi_cls_session *session =
967 container_of(work, struct iscsi_cls_session,
968 unbind_work);
969 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500970 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500971 unsigned long flags;
Mike Christie26974782007-12-13 12:43:29 -0600972
Mike Christie632248a2009-08-20 15:11:01 -0500973 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
974
Mike Christie26974782007-12-13 12:43:29 -0600975 /* Prevent new scans and make sure scanning is not in progress */
976 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -0500977 spin_lock_irqsave(&session->lock, flags);
978 if (session->target_id == ISCSI_MAX_TARGET) {
979 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600980 mutex_unlock(&ihost->mutex);
981 return;
982 }
Mike Christie79706342008-05-21 15:54:12 -0500983 session->target_id = ISCSI_MAX_TARGET;
984 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600985 mutex_unlock(&ihost->mutex);
986
987 scsi_remove_target(&session->dev);
988 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -0500989 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
Mike Christie26974782007-12-13 12:43:29 -0600990}
991
Mike Christie7b8631b2006-01-13 18:05:50 -0600992struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500993iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
994 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600995{
996 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Mike Christie5d91e202008-05-21 15:54:01 -0500998 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -0500999 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001000 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -05001001 return NULL;
1002
Mike Christie7b8631b2006-01-13 18:05:50 -06001003 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -05001004 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -06001005 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +00001006 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -05001007 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -06001008 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
1009 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -06001010 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -06001011 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001012 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -06001013
Mike Christie6a8a0d32006-06-28 12:00:31 -05001014 /* this is released in the dev's release function */
1015 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -05001016 session->dev.parent = &shost->shost_gendev;
1017 session->dev.release = iscsi_session_release;
1018 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -05001019 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -05001020 session->dd_data = &session[1];
Mike Christie632248a2009-08-20 15:11:01 -05001021
1022 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001023 return session;
1024}
1025EXPORT_SYMBOL_GPL(iscsi_alloc_session);
1026
Mike Christie79706342008-05-21 15:54:12 -05001027static int iscsi_get_next_target_id(struct device *dev, void *data)
1028{
1029 struct iscsi_cls_session *session;
1030 unsigned long flags;
1031 int err = 0;
1032
1033 if (!iscsi_is_session_dev(dev))
1034 return 0;
1035
1036 session = iscsi_dev_to_session(dev);
1037 spin_lock_irqsave(&session->lock, flags);
1038 if (*((unsigned int *) data) == session->target_id)
1039 err = -EEXIST;
1040 spin_unlock_irqrestore(&session->lock, flags);
1041 return err;
1042}
1043
Mike Christie6a8a0d32006-06-28 12:00:31 -05001044int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05001045{
1046 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001047 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -06001048 unsigned long flags;
Mike Christie79706342008-05-21 15:54:12 -05001049 unsigned int id = target_id;
Mike Christie8434aa82006-06-28 12:00:30 -05001050 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -05001051
Mike Christie30a6c652006-04-06 21:13:39 -05001052 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -06001053 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -05001054
1055 if (id == ISCSI_MAX_TARGET) {
1056 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
1057 err = device_for_each_child(&shost->shost_gendev, &id,
1058 iscsi_get_next_target_id);
1059 if (!err)
1060 break;
1061 }
1062
1063 if (id == ISCSI_MAX_TARGET) {
1064 iscsi_cls_session_printk(KERN_ERR, session,
1065 "Too many iscsi targets. Max "
1066 "number of targets is %d.\n",
1067 ISCSI_MAX_TARGET - 1);
Jaswinder Singh Rajput24add1c2009-06-20 13:29:21 +05301068 err = -EOVERFLOW;
Mike Christie79706342008-05-21 15:54:12 -05001069 goto release_host;
1070 }
1071 }
1072 session->target_id = id;
Mike Christie30a6c652006-04-06 21:13:39 -05001073
Kay Sievers71610f52008-12-03 22:41:36 +01001074 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -05001075 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001076 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001077 iscsi_cls_session_printk(KERN_ERR, session,
1078 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001079 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -06001080 }
1081 transport_register_device(&session->dev);
1082
Mike Christie26974782007-12-13 12:43:29 -06001083 spin_lock_irqsave(&sesslock, flags);
1084 list_add(&session->sess_list, &sesslist);
1085 spin_unlock_irqrestore(&sesslock, flags);
1086
Mike Christie26974782007-12-13 12:43:29 -06001087 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -05001088 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001089 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -05001090
Mike Christie8434aa82006-06-28 12:00:30 -05001091release_host:
1092 scsi_host_put(shost);
1093 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -06001094}
Mike Christie8434aa82006-06-28 12:00:30 -05001095EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -06001096
1097/**
Mike Christie8434aa82006-06-28 12:00:30 -05001098 * iscsi_create_session - create iscsi class session
1099 * @shost: scsi host
1100 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -05001101 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -05001102 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -06001103 *
Mike Christie8434aa82006-06-28 12:00:30 -05001104 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001105 */
Mike Christie8434aa82006-06-28 12:00:30 -05001106struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -05001107iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1108 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05001109{
1110 struct iscsi_cls_session *session;
1111
Mike Christie5d91e202008-05-21 15:54:01 -05001112 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -05001113 if (!session)
1114 return NULL;
1115
Mike Christie6a8a0d32006-06-28 12:00:31 -05001116 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -05001117 iscsi_free_session(session);
1118 return NULL;
1119 }
1120 return session;
1121}
1122EXPORT_SYMBOL_GPL(iscsi_create_session);
1123
Mike Christie26974782007-12-13 12:43:29 -06001124static void iscsi_conn_release(struct device *dev)
1125{
1126 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
1127 struct device *parent = conn->dev.parent;
1128
Mike Christie632248a2009-08-20 15:11:01 -05001129 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
Mike Christie26974782007-12-13 12:43:29 -06001130 kfree(conn);
1131 put_device(parent);
1132}
1133
1134static int iscsi_is_conn_dev(const struct device *dev)
1135{
1136 return dev->release == iscsi_conn_release;
1137}
1138
1139static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
1140{
1141 if (!iscsi_is_conn_dev(dev))
1142 return 0;
1143 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
1144}
1145
Mike Christie8434aa82006-06-28 12:00:30 -05001146void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001147{
Mike Christie30a6c652006-04-06 21:13:39 -05001148 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie26974782007-12-13 12:43:29 -06001149 unsigned long flags;
1150 int err;
Mike Christie30a6c652006-04-06 21:13:39 -05001151
Mike Christie632248a2009-08-20 15:11:01 -05001152 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
1153
Mike Christie26974782007-12-13 12:43:29 -06001154 spin_lock_irqsave(&sesslock, flags);
1155 list_del(&session->sess_list);
1156 spin_unlock_irqrestore(&sesslock, flags);
1157
Mike Christie45ab33b2008-03-04 13:26:55 -06001158 /* make sure there are no blocks/unblocks queued */
1159 flush_workqueue(iscsi_eh_timer_workq);
1160 /* make sure the timedout callout is not running */
1161 if (!cancel_delayed_work(&session->recovery_work))
1162 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -06001163 /*
1164 * If we are blocked let commands flow again. The lld or iscsi
1165 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -06001166 * We assume that LLD will not be calling block/unblock while
1167 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -06001168 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001169 spin_lock_irqsave(&session->lock, flags);
1170 session->state = ISCSI_SESSION_FREE;
1171 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -06001172
Mike Christie45ab33b2008-03-04 13:26:55 -06001173 scsi_target_unblock(&session->dev);
1174 /* flush running scans then delete devices */
Mike Christie06d25af2009-03-05 14:46:02 -06001175 scsi_flush_work(shost);
Mike Christie45ab33b2008-03-04 13:26:55 -06001176 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -05001177
Mike Christie26974782007-12-13 12:43:29 -06001178 /* hw iscsi may not have removed all connections from session */
1179 err = device_for_each_child(&session->dev, NULL,
1180 iscsi_iter_destroy_conn_fn);
1181 if (err)
Mike Christie322d7392008-01-31 13:36:52 -06001182 iscsi_cls_session_printk(KERN_ERR, session,
1183 "Could not delete all connections "
1184 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -05001185
Mike Christie7b8631b2006-01-13 18:05:50 -06001186 transport_unregister_device(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001187
1188 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001189 device_del(&session->dev);
1190}
1191EXPORT_SYMBOL_GPL(iscsi_remove_session);
1192
1193void iscsi_free_session(struct iscsi_cls_session *session)
1194{
Mike Christie632248a2009-08-20 15:11:01 -05001195 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
Mike Christie26974782007-12-13 12:43:29 -06001196 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -05001197 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001198}
Mike Christie8434aa82006-06-28 12:00:30 -05001199EXPORT_SYMBOL_GPL(iscsi_free_session);
1200
1201/**
1202 * iscsi_destroy_session - destroy iscsi session
1203 * @session: iscsi_session
1204 *
1205 * Can be called by a LLD or iscsi_transport. There must not be
1206 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -05001207 */
Mike Christie8434aa82006-06-28 12:00:30 -05001208int iscsi_destroy_session(struct iscsi_cls_session *session)
1209{
1210 iscsi_remove_session(session);
Mike Christie632248a2009-08-20 15:11:01 -05001211 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001212 iscsi_free_session(session);
1213 return 0;
1214}
Mike Christie7b8631b2006-01-13 18:05:50 -06001215EXPORT_SYMBOL_GPL(iscsi_destroy_session);
1216
Mike Christie7b8631b2006-01-13 18:05:50 -06001217/**
1218 * iscsi_create_conn - create iscsi class connection
1219 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -05001220 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -06001221 * @cid: connection id
1222 *
1223 * This can be called from a LLD or iscsi_transport. The connection
1224 * is child of the session so cid must be unique for all connections
1225 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -05001226 *
1227 * Since we do not support MCS, cid will normally be zero. In some cases
1228 * for software iscsi we could be trying to preallocate a connection struct
1229 * in which case there could be two connection structs and cid would be
1230 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -05001231 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001232struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05001233iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -06001234{
1235 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -06001236 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -06001237 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -06001238 int err;
1239
Mike Christie5d91e202008-05-21 15:54:01 -05001240 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001241 if (!conn)
1242 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -05001243 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06001244 conn->dd_data = &conn[1];
1245
Mike Christie22a39fb2011-02-16 15:04:33 -06001246 mutex_init(&conn->ep_mutex);
Mike Christie7b8631b2006-01-13 18:05:50 -06001247 INIT_LIST_HEAD(&conn->conn_list);
1248 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -05001249 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001250
1251 /* this is released in the dev's release function */
1252 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -04001253 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -05001254
Kay Sievers71610f52008-12-03 22:41:36 +01001255 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001256 conn->dev.parent = &session->dev;
1257 conn->dev.release = iscsi_conn_release;
1258 err = device_register(&conn->dev);
1259 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001260 iscsi_cls_session_printk(KERN_ERR, session, "could not "
1261 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001262 goto release_parent_ref;
1263 }
1264 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -06001265
1266 spin_lock_irqsave(&connlock, flags);
1267 list_add(&conn->conn_list, &connlist);
Mike Christie26974782007-12-13 12:43:29 -06001268 spin_unlock_irqrestore(&connlock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05001269
1270 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001271 return conn;
1272
1273release_parent_ref:
1274 put_device(&session->dev);
1275free_conn:
1276 kfree(conn);
1277 return NULL;
1278}
1279
1280EXPORT_SYMBOL_GPL(iscsi_create_conn);
1281
1282/**
1283 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -05001284 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -06001285 *
Mike Christie26974782007-12-13 12:43:29 -06001286 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001287 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001288int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1289{
Mike Christie26974782007-12-13 12:43:29 -06001290 unsigned long flags;
1291
1292 spin_lock_irqsave(&connlock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001293 list_del(&conn->conn_list);
1294 spin_unlock_irqrestore(&connlock, flags);
1295
Mike Christie7b8631b2006-01-13 18:05:50 -06001296 transport_unregister_device(&conn->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001297 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001298 device_unregister(&conn->dev);
1299 return 0;
1300}
Mike Christie7b8631b2006-01-13 18:05:50 -06001301EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1302
1303/*
Mike Christie7b8631b2006-01-13 18:05:50 -06001304 * iscsi interface functions
1305 */
Alex Aizman0896b752005-08-04 19:33:07 -07001306static struct iscsi_internal *
1307iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Alex Aizman0896b752005-08-04 19:33:07 -07001309 struct iscsi_internal *priv;
1310 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Alex Aizman0896b752005-08-04 19:33:07 -07001312 spin_lock_irqsave(&iscsi_transport_lock, flags);
1313 list_for_each_entry(priv, &iscsi_transports, list) {
1314 if (tt == priv->iscsi_transport) {
1315 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1316 return priv;
1317 }
1318 }
1319 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1320 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Alex Aizman0896b752005-08-04 19:33:07 -07001323static int
Michael Chan43514772009-06-08 18:14:41 -07001324iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -05001325{
Michael Chan43514772009-06-08 18:14:41 -07001326 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -07001327}
1328
Mike Christie7b7232f2006-02-01 21:06:49 -06001329int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -07001330 char *data, uint32_t data_size)
1331{
1332 struct nlmsghdr *nlh;
1333 struct sk_buff *skb;
1334 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07001335 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05001336 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001337 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1338 data_size);
1339
Mike Christie790f39a2006-05-18 20:31:39 -05001340 priv = iscsi_if_transport_lookup(conn->transport);
1341 if (!priv)
1342 return -EINVAL;
1343
Mike Christie43a145a2006-10-16 18:09:38 -04001344 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001345 if (!skb) {
Mike Christiee5bd7b52008-09-24 11:46:10 -05001346 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06001347 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1348 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001349 return -ENOMEM;
1350 }
1351
Michael Chan43514772009-06-08 18:14:41 -07001352 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001353 ev = NLMSG_DATA(nlh);
1354 memset(ev, 0, sizeof(*ev));
1355 ev->transport_handle = iscsi_handle(conn->transport);
1356 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05001357 ev->r.recv_req.cid = conn->cid;
1358 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001359 pdu = (char*)ev + sizeof(*ev);
1360 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1361 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1362
Michael Chan43514772009-06-08 18:14:41 -07001363 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001364}
1365EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1366
Michael Chan43514772009-06-08 18:14:41 -07001367int iscsi_offload_mesg(struct Scsi_Host *shost,
1368 struct iscsi_transport *transport, uint32_t type,
1369 char *data, uint16_t data_size)
1370{
1371 struct nlmsghdr *nlh;
1372 struct sk_buff *skb;
1373 struct iscsi_uevent *ev;
1374 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
1375
Michael Chana541f842009-07-29 08:49:52 +00001376 skb = alloc_skb(len, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001377 if (!skb) {
1378 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
1379 return -ENOMEM;
1380 }
1381
1382 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1383 ev = NLMSG_DATA(nlh);
1384 memset(ev, 0, sizeof(*ev));
1385 ev->type = type;
1386 ev->transport_handle = iscsi_handle(transport);
1387 switch (type) {
1388 case ISCSI_KEVENT_PATH_REQ:
1389 ev->r.req_path.host_no = shost->host_no;
1390 break;
1391 case ISCSI_KEVENT_IF_DOWN:
1392 ev->r.notify_if_down.host_no = shost->host_no;
1393 break;
1394 }
1395
1396 memcpy((char *)ev + sizeof(*ev), data, data_size);
1397
Michael Chana541f842009-07-29 08:49:52 +00001398 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001399}
1400EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1401
Mike Christiee5bd7b52008-09-24 11:46:10 -05001402void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07001403{
1404 struct nlmsghdr *nlh;
1405 struct sk_buff *skb;
1406 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05001407 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001408 int len = NLMSG_SPACE(sizeof(*ev));
1409
Mike Christie790f39a2006-05-18 20:31:39 -05001410 priv = iscsi_if_transport_lookup(conn->transport);
1411 if (!priv)
1412 return;
1413
Mike Christie43a145a2006-10-16 18:09:38 -04001414 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001415 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001416 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1417 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07001418 return;
1419 }
1420
Michael Chan43514772009-06-08 18:14:41 -07001421 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001422 ev = NLMSG_DATA(nlh);
1423 ev->transport_handle = iscsi_handle(conn->transport);
1424 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07001425 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05001426 ev->r.connerror.cid = conn->cid;
1427 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001428
Michael Chan43514772009-06-08 18:14:41 -07001429 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001430
Mike Christie322d7392008-01-31 13:36:52 -06001431 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1432 error);
Alex Aizman0896b752005-08-04 19:33:07 -07001433}
Mike Christiee5bd7b52008-09-24 11:46:10 -05001434EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07001435
1436static int
Michael Chan43514772009-06-08 18:14:41 -07001437iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1438 void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07001439{
1440 struct sk_buff *skb;
1441 struct nlmsghdr *nlh;
1442 int len = NLMSG_SPACE(size);
1443 int flags = multi ? NLM_F_MULTI : 0;
1444 int t = done ? NLMSG_DONE : type;
1445
Mike Christie43a145a2006-10-16 18:09:38 -04001446 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05001447 if (!skb) {
1448 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1449 return -ENOMEM;
1450 }
Alex Aizman0896b752005-08-04 19:33:07 -07001451
Michael Chan43514772009-06-08 18:14:41 -07001452 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001453 nlh->nlmsg_flags = flags;
1454 memcpy(NLMSG_DATA(nlh), payload, size);
Michael Chan43514772009-06-08 18:14:41 -07001455 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001456}
1457
1458static int
Mike Christie5b940ad2006-02-01 21:06:56 -06001459iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07001460{
1461 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1462 struct iscsi_stats *stats;
1463 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06001464 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001465 struct nlmsghdr *nlhstat;
1466 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05001467 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001468 int len = NLMSG_SPACE(sizeof(*ev) +
1469 sizeof(struct iscsi_stats) +
1470 sizeof(struct iscsi_stats_custom) *
1471 ISCSI_STATS_CUSTOM_MAX);
1472 int err = 0;
1473
Mike Christie790f39a2006-05-18 20:31:39 -05001474 priv = iscsi_if_transport_lookup(transport);
1475 if (!priv)
1476 return -EINVAL;
1477
Mike Christieb5c7a122006-04-06 21:13:33 -05001478 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07001479 if (!conn)
1480 return -EEXIST;
1481
1482 do {
1483 int actual_size;
1484
Mike Christie43a145a2006-10-16 18:09:38 -04001485 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001486 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06001487 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1488 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001489 return -ENOMEM;
1490 }
1491
Michael Chan43514772009-06-08 18:14:41 -07001492 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07001493 (len - sizeof(*nlhstat)), 0);
1494 evstat = NLMSG_DATA(nlhstat);
1495 memset(evstat, 0, sizeof(*evstat));
1496 evstat->transport_handle = iscsi_handle(conn->transport);
1497 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05001498 evstat->u.get_stats.cid =
1499 ev->u.get_stats.cid;
1500 evstat->u.get_stats.sid =
1501 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07001502 stats = (struct iscsi_stats *)
1503 ((char*)evstat + sizeof(*evstat));
1504 memset(stats, 0, sizeof(*stats));
1505
Mike Christie7b7232f2006-02-01 21:06:49 -06001506 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07001507 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1508 sizeof(struct iscsi_stats) +
1509 sizeof(struct iscsi_stats_custom) *
1510 stats->custom_length);
1511 actual_size -= sizeof(*nlhstat);
1512 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06001513 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07001514 nlhstat->nlmsg_len = actual_size;
1515
Michael Chan43514772009-06-08 18:14:41 -07001516 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1517 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001518 } while (err < 0 && err != -ECONNREFUSED);
1519
1520 return err;
1521}
1522
Mike Christie53cb8a12006-06-28 12:00:32 -05001523/**
Mike Christie26974782007-12-13 12:43:29 -06001524 * iscsi_session_event - send session destr. completion event
1525 * @session: iscsi class session
1526 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05001527 */
Mike Christie26974782007-12-13 12:43:29 -06001528int iscsi_session_event(struct iscsi_cls_session *session,
1529 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05001530{
1531 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05001532 struct Scsi_Host *shost;
1533 struct iscsi_uevent *ev;
1534 struct sk_buff *skb;
1535 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05001536 int rc, len = NLMSG_SPACE(sizeof(*ev));
1537
Mike Christie26974782007-12-13 12:43:29 -06001538 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05001539 if (!priv)
1540 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05001541 shost = iscsi_session_to_shost(session);
1542
Mike Christie43a145a2006-10-16 18:09:38 -04001543 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001544 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001545 iscsi_cls_session_printk(KERN_ERR, session,
1546 "Cannot notify userspace of session "
1547 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001548 return -ENOMEM;
1549 }
1550
Michael Chan43514772009-06-08 18:14:41 -07001551 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Mike Christie53cb8a12006-06-28 12:00:32 -05001552 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06001553 ev->transport_handle = iscsi_handle(session->transport);
1554
1555 ev->type = event;
1556 switch (event) {
1557 case ISCSI_KEVENT_DESTROY_SESSION:
1558 ev->r.d_session.host_no = shost->host_no;
1559 ev->r.d_session.sid = session->sid;
1560 break;
1561 case ISCSI_KEVENT_CREATE_SESSION:
1562 ev->r.c_session_ret.host_no = shost->host_no;
1563 ev->r.c_session_ret.sid = session->sid;
1564 break;
1565 case ISCSI_KEVENT_UNBIND_SESSION:
1566 ev->r.unbind_session.host_no = shost->host_no;
1567 ev->r.unbind_session.sid = session->sid;
1568 break;
1569 default:
Mike Christie322d7392008-01-31 13:36:52 -06001570 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1571 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06001572 kfree_skb(skb);
1573 return -EINVAL;
1574 }
Mike Christie53cb8a12006-06-28 12:00:32 -05001575
1576 /*
1577 * this will occur if the daemon is not up, so we just warn
1578 * the user and when the daemon is restarted it will handle it
1579 */
Michael Chan43514772009-06-08 18:14:41 -07001580 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001581 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06001582 iscsi_cls_session_printk(KERN_ERR, session,
1583 "Cannot notify userspace of session "
1584 "event %u. Check iscsi daemon\n",
1585 event);
Mike Christie632248a2009-08-20 15:11:01 -05001586
1587 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
1588 event, rc);
Mike Christie53cb8a12006-06-28 12:00:32 -05001589 return rc;
1590}
Mike Christie26974782007-12-13 12:43:29 -06001591EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001592
Alex Aizman0896b752005-08-04 19:33:07 -07001593static int
Mike Christied82ff9be2008-05-21 15:54:13 -05001594iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1595 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
Mike Christie40753ca2008-05-21 15:53:56 -05001596 uint16_t cmds_max, uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001597{
1598 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001599 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06001600 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06001601
Mike Christied82ff9be2008-05-21 15:54:13 -05001602 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06001603 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06001604 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001605 return -ENOMEM;
1606
Mike Christie5e7facb2009-03-05 14:46:06 -06001607 shost = iscsi_session_to_shost(session);
1608 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001609 ev->r.c_session_ret.sid = session->sid;
Mike Christie632248a2009-08-20 15:11:01 -05001610 ISCSI_DBG_TRANS_SESSION(session,
1611 "Completed creating transport session\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001612 return 0;
1613}
1614
1615static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001616iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001617{
Mike Christie7b8631b2006-01-13 18:05:50 -06001618 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001619 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001620
Mike Christieb5c7a122006-04-06 21:13:33 -05001621 session = iscsi_session_lookup(ev->u.c_conn.sid);
1622 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001623 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001624 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001625 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001626 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001627
Mike Christie7b7232f2006-02-01 21:06:49 -06001628 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001629 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001630 iscsi_cls_session_printk(KERN_ERR, session,
1631 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001632 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001633 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001634
Mike Christieb5c7a122006-04-06 21:13:33 -05001635 ev->r.c_conn_ret.sid = session->sid;
1636 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie632248a2009-08-20 15:11:01 -05001637
1638 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001639 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001640}
1641
1642static int
1643iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1644{
Mike Christie7b8631b2006-01-13 18:05:50 -06001645 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001646
Mike Christieb5c7a122006-04-06 21:13:33 -05001647 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001648 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001649 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001650
Mike Christie632248a2009-08-20 15:11:01 -05001651 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001652 if (transport->destroy_conn)
1653 transport->destroy_conn(conn);
Mike Christie632248a2009-08-20 15:11:01 -05001654
Mike Christie7b8631b2006-01-13 18:05:50 -06001655 return 0;
1656}
1657
Mike Christiefd7255f2006-04-06 21:13:36 -05001658static int
1659iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1660{
1661 char *data = (char*)ev + sizeof(*ev);
1662 struct iscsi_cls_conn *conn;
1663 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001664 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001665
1666 session = iscsi_session_lookup(ev->u.set_param.sid);
1667 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1668 if (!conn || !session)
1669 return -EINVAL;
1670
1671 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001672 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001673 sscanf(data, "%d", &value);
Mike Christiefdd46dc2009-11-11 16:34:34 -06001674 session->recovery_tmo = value;
Mike Christie30a6c652006-04-06 21:13:39 -05001675 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001676 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001677 err = transport->set_param(conn, ev->u.set_param.param,
1678 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001679 }
1680
1681 return err;
1682}
1683
Mike Christie10eb0f02009-05-13 17:57:38 -05001684static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1685 struct iscsi_uevent *ev, int msg_type)
1686{
1687 struct iscsi_endpoint *ep;
1688 struct sockaddr *dst_addr;
1689 struct Scsi_Host *shost = NULL;
1690 int non_blocking, err = 0;
1691
1692 if (!transport->ep_connect)
1693 return -EINVAL;
1694
1695 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1696 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1697 if (!shost) {
1698 printk(KERN_ERR "ep connect failed. Could not find "
1699 "host no %u\n",
1700 ev->u.ep_connect_through_host.host_no);
1701 return -ENODEV;
1702 }
1703 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1704 } else
1705 non_blocking = ev->u.ep_connect.non_blocking;
1706
1707 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1708 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1709 if (IS_ERR(ep)) {
1710 err = PTR_ERR(ep);
1711 goto release_host;
1712 }
1713
1714 ev->r.ep_connect_ret.handle = ep->id;
1715release_host:
1716 if (shost)
1717 scsi_host_put(shost);
1718 return err;
1719}
1720
Mike Christie22a39fb2011-02-16 15:04:33 -06001721static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
1722 u64 ep_handle)
1723{
1724 struct iscsi_cls_conn *conn;
1725 struct iscsi_endpoint *ep;
1726
1727 if (!transport->ep_disconnect)
1728 return -EINVAL;
1729
1730 ep = iscsi_lookup_endpoint(ep_handle);
1731 if (!ep)
1732 return -EINVAL;
1733 conn = ep->conn;
1734 if (conn) {
1735 mutex_lock(&conn->ep_mutex);
1736 conn->ep = NULL;
1737 mutex_unlock(&conn->ep_mutex);
1738 }
1739
1740 transport->ep_disconnect(ep);
1741 return 0;
1742}
1743
Mike Christie7b8631b2006-01-13 18:05:50 -06001744static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001745iscsi_if_transport_ep(struct iscsi_transport *transport,
1746 struct iscsi_uevent *ev, int msg_type)
1747{
Mike Christied82ff9be2008-05-21 15:54:13 -05001748 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001749 int rc = 0;
1750
1751 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05001752 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001753 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001754 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001755 break;
1756 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1757 if (!transport->ep_poll)
1758 return -EINVAL;
1759
Mike Christied82ff9be2008-05-21 15:54:13 -05001760 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1761 if (!ep)
1762 return -EINVAL;
1763
1764 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001765 ev->u.ep_poll.timeout_ms);
1766 break;
1767 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie22a39fb2011-02-16 15:04:33 -06001768 rc = iscsi_if_ep_disconnect(transport,
1769 ev->u.ep_disconnect.ep_handle);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001770 break;
1771 }
1772 return rc;
1773}
1774
1775static int
Mike Christie01cb2252006-06-28 12:00:22 -05001776iscsi_tgt_dscvr(struct iscsi_transport *transport,
1777 struct iscsi_uevent *ev)
1778{
Mike Christie2174a042007-05-30 12:57:10 -05001779 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001780 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001781 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001782
1783 if (!transport->tgt_dscvr)
1784 return -EINVAL;
1785
Mike Christie2174a042007-05-30 12:57:10 -05001786 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001787 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05001788 printk(KERN_ERR "target discovery could not find host no %u\n",
1789 ev->u.tgt_dscvr.host_no);
1790 return -ENODEV;
1791 }
1792
1793
Mike Christie01cb2252006-06-28 12:00:22 -05001794 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001795 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1796 ev->u.tgt_dscvr.enable, dst_addr);
1797 scsi_host_put(shost);
1798 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001799}
1800
1801static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001802iscsi_set_host_param(struct iscsi_transport *transport,
1803 struct iscsi_uevent *ev)
1804{
1805 char *data = (char*)ev + sizeof(*ev);
1806 struct Scsi_Host *shost;
1807 int err;
1808
1809 if (!transport->set_host_param)
1810 return -ENOSYS;
1811
1812 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001813 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05001814 printk(KERN_ERR "set_host_param could not find host no %u\n",
1815 ev->u.set_host_param.host_no);
1816 return -ENODEV;
1817 }
1818
1819 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1820 data, ev->u.set_host_param.len);
1821 scsi_host_put(shost);
1822 return err;
1823}
1824
1825static int
Michael Chan43514772009-06-08 18:14:41 -07001826iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1827{
1828 struct Scsi_Host *shost;
1829 struct iscsi_path *params;
1830 int err;
1831
1832 if (!transport->set_path)
1833 return -ENOSYS;
1834
1835 shost = scsi_host_lookup(ev->u.set_path.host_no);
1836 if (!shost) {
1837 printk(KERN_ERR "set path could not find host no %u\n",
1838 ev->u.set_path.host_no);
1839 return -ENODEV;
1840 }
1841
1842 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1843 err = transport->set_path(shost, params);
1844
1845 scsi_host_put(shost);
1846 return err;
1847}
1848
1849static int
Mike Christie56c155b2011-07-25 13:48:37 -05001850iscsi_set_iface_params(struct iscsi_transport *transport,
1851 struct iscsi_uevent *ev)
1852{
1853 char *data = (char *)ev + sizeof(*ev);
1854 struct Scsi_Host *shost;
1855 int err;
1856
1857 if (!transport->set_iface_param)
1858 return -ENOSYS;
1859
1860 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
1861 if (!shost) {
1862 printk(KERN_ERR "set_iface_params could not find host no %u\n",
1863 ev->u.set_iface_params.host_no);
1864 return -ENODEV;
1865 }
1866
1867 err = transport->set_iface_param(shost, data,
1868 ev->u.set_iface_params.count);
1869 scsi_host_put(shost);
1870 return err;
1871}
1872
1873static int
Michael Chan43514772009-06-08 18:14:41 -07001874iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07001875{
1876 int err = 0;
1877 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1878 struct iscsi_transport *transport = NULL;
1879 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001880 struct iscsi_cls_session *session;
1881 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05001882 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001883
Michael Chan43514772009-06-08 18:14:41 -07001884 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1885 *group = ISCSI_NL_GRP_UIP;
1886 else
1887 *group = ISCSI_NL_GRP_ISCSID;
1888
Alex Aizman0896b752005-08-04 19:33:07 -07001889 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1890 if (!priv)
1891 return -EINVAL;
1892 transport = priv->iscsi_transport;
1893
Mike Christie7b7232f2006-02-01 21:06:49 -06001894 if (!try_module_get(transport->owner))
1895 return -EINVAL;
1896
Alex Aizman0896b752005-08-04 19:33:07 -07001897 switch (nlh->nlmsg_type) {
1898 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001899 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001900 ev->u.c_session.initial_cmdsn,
1901 ev->u.c_session.cmds_max,
1902 ev->u.c_session.queue_depth);
1903 break;
1904 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001905 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05001906 if (!ep) {
1907 err = -EINVAL;
1908 break;
1909 }
Mike Christied82ff9be2008-05-21 15:54:13 -05001910
1911 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001912 ev->u.c_bound_session.initial_cmdsn,
1913 ev->u.c_bound_session.cmds_max,
1914 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001915 break;
1916 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001917 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001918 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001919 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001920 else
1921 err = -EINVAL;
1922 break;
1923 case ISCSI_UEVENT_UNBIND_SESSION:
1924 session = iscsi_session_lookup(ev->u.d_session.sid);
1925 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06001926 scsi_queue_work(iscsi_session_to_shost(session),
1927 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06001928 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001929 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001930 break;
1931 case ISCSI_UEVENT_CREATE_CONN:
1932 err = iscsi_if_create_conn(transport, ev);
1933 break;
1934 case ISCSI_UEVENT_DESTROY_CONN:
1935 err = iscsi_if_destroy_conn(transport, ev);
1936 break;
1937 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001938 session = iscsi_session_lookup(ev->u.b_conn.sid);
1939 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001940
Mike Christie22a39fb2011-02-16 15:04:33 -06001941 if (conn && conn->ep)
1942 iscsi_if_ep_disconnect(transport, conn->ep->id);
1943
1944 if (!session || !conn) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001945 err = -EINVAL;
Mike Christie22a39fb2011-02-16 15:04:33 -06001946 break;
1947 }
1948
1949 ev->r.retcode = transport->bind_conn(session, conn,
1950 ev->u.b_conn.transport_eph,
1951 ev->u.b_conn.is_leading);
1952 if (ev->r.retcode || !transport->ep_connect)
1953 break;
1954
1955 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
1956 if (ep) {
1957 ep->conn = conn;
1958
1959 mutex_lock(&conn->ep_mutex);
1960 conn->ep = ep;
1961 mutex_unlock(&conn->ep_mutex);
1962 } else
1963 iscsi_cls_conn_printk(KERN_ERR, conn,
1964 "Could not set ep conn "
1965 "binding\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001966 break;
1967 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001968 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001969 break;
1970 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001971 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001972 if (conn)
1973 ev->r.retcode = transport->start_conn(conn);
1974 else
1975 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001976 break;
1977 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001978 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001979 if (conn)
1980 transport->stop_conn(conn, ev->u.stop_conn.flag);
1981 else
1982 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001983 break;
1984 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001985 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001986 if (conn)
1987 ev->r.retcode = transport->send_pdu(conn,
1988 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1989 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1990 ev->u.send_pdu.data_size);
1991 else
1992 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001993 break;
1994 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001995 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001996 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001997 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1998 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1999 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05002000 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05002001 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
2002 break;
Mike Christie01cb2252006-06-28 12:00:22 -05002003 case ISCSI_UEVENT_TGT_DSCVR:
2004 err = iscsi_tgt_dscvr(transport, ev);
2005 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05002006 case ISCSI_UEVENT_SET_HOST_PARAM:
2007 err = iscsi_set_host_param(transport, ev);
2008 break;
Michael Chan43514772009-06-08 18:14:41 -07002009 case ISCSI_UEVENT_PATH_UPDATE:
2010 err = iscsi_set_path(transport, ev);
2011 break;
Mike Christie56c155b2011-07-25 13:48:37 -05002012 case ISCSI_UEVENT_SET_IFACE_PARAMS:
2013 err = iscsi_set_iface_params(transport, ev);
2014 break;
Alex Aizman0896b752005-08-04 19:33:07 -07002015 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05002016 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07002017 break;
2018 }
2019
Mike Christie7b7232f2006-02-01 21:06:49 -06002020 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07002021 return err;
2022}
2023
Mike Christieb5c7a122006-04-06 21:13:33 -05002024/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002025 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
2026 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05002027 */
Alex Aizman0896b752005-08-04 19:33:07 -07002028static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002029iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07002030{
Arjan van de Ven0b950672006-01-11 13:16:10 +01002031 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002032 while (skb->len >= NLMSG_SPACE(0)) {
2033 int err;
2034 uint32_t rlen;
2035 struct nlmsghdr *nlh;
2036 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07002037 uint32_t group;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002038
2039 nlh = nlmsg_hdr(skb);
2040 if (nlh->nlmsg_len < sizeof(*nlh) ||
2041 skb->len < nlh->nlmsg_len) {
2042 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06002043 }
Mike Christieee7f8e42006-02-01 21:07:01 -06002044
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002045 ev = NLMSG_DATA(nlh);
2046 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
2047 if (rlen > skb->len)
2048 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07002049
Michael Chan43514772009-06-08 18:14:41 -07002050 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002051 if (err) {
2052 ev->type = ISCSI_KEVENT_IF_ERROR;
2053 ev->iferror = err;
2054 }
2055 do {
2056 /*
2057 * special case for GET_STATS:
2058 * on success - sending reply and stats from
2059 * inside of if_recv_msg(),
2060 * on error - fall through.
2061 */
2062 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07002063 break;
Michael Chan43514772009-06-08 18:14:41 -07002064 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002065 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
2066 } while (err < 0 && err != -ECONNREFUSED);
2067 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07002068 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01002069 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002070}
2071
Mike Christiefd7255f2006-04-06 21:13:36 -05002072#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01002073struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05002074 __ATTR(_name,_mode,_show,_store)
2075
Alex Aizman0896b752005-08-04 19:33:07 -07002076/*
2077 * iSCSI connection attrs
2078 */
Mike Christiea54a52c2006-06-28 12:00:23 -05002079#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002081show_conn_param_##param(struct device *dev, \
2082 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002083{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002084 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002085 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05002086 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002087}
2088
Mike Christiea54a52c2006-06-28 12:00:23 -05002089#define iscsi_conn_attr(field, param) \
2090 iscsi_conn_attr_show(param) \
2091static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05002092 NULL);
2093
Mike Christiea54a52c2006-06-28 12:00:23 -05002094iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
2095iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
2096iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
2097iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
2098iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
2099iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
2100iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
Mike Christiea54a52c2006-06-28 12:00:23 -05002101iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
2102iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06002103iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
2104iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Mike Christie289324b2011-02-16 15:04:37 -06002106#define iscsi_conn_ep_attr_show(param) \
2107static ssize_t show_conn_ep_param_##param(struct device *dev, \
2108 struct device_attribute *attr,\
2109 char *buf) \
2110{ \
2111 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
2112 struct iscsi_transport *t = conn->transport; \
2113 struct iscsi_endpoint *ep; \
2114 ssize_t rc; \
2115 \
2116 /* \
2117 * Need to make sure ep_disconnect does not free the LLD's \
2118 * interconnect resources while we are trying to read them. \
2119 */ \
2120 mutex_lock(&conn->ep_mutex); \
2121 ep = conn->ep; \
2122 if (!ep && t->ep_connect) { \
2123 mutex_unlock(&conn->ep_mutex); \
2124 return -ENOTCONN; \
2125 } \
2126 \
2127 if (ep) \
2128 rc = t->get_ep_param(ep, param, buf); \
2129 else \
2130 rc = t->get_conn_param(conn, param, buf); \
2131 mutex_unlock(&conn->ep_mutex); \
2132 return rc; \
2133}
2134
2135#define iscsi_conn_ep_attr(field, param) \
2136 iscsi_conn_ep_attr_show(param) \
2137static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
2138 show_conn_ep_param_##param, NULL);
2139
2140iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
2141iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
2142
Mike Christie3128c6c2011-07-25 13:48:42 -05002143static struct attribute *iscsi_conn_attrs[] = {
2144 &dev_attr_conn_max_recv_dlength.attr,
2145 &dev_attr_conn_max_xmit_dlength.attr,
2146 &dev_attr_conn_header_digest.attr,
2147 &dev_attr_conn_data_digest.attr,
2148 &dev_attr_conn_ifmarker.attr,
2149 &dev_attr_conn_ofmarker.attr,
2150 &dev_attr_conn_address.attr,
2151 &dev_attr_conn_port.attr,
2152 &dev_attr_conn_exp_statsn.attr,
2153 &dev_attr_conn_persistent_address.attr,
2154 &dev_attr_conn_persistent_port.attr,
2155 &dev_attr_conn_ping_tmo.attr,
2156 &dev_attr_conn_recv_tmo.attr,
2157 NULL,
2158};
2159
2160static mode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
2161 struct attribute *attr, int i)
2162{
2163 struct device *cdev = container_of(kobj, struct device, kobj);
2164 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
2165 struct iscsi_transport *t = conn->transport;
2166 int param;
2167
2168 if (attr == &dev_attr_conn_max_recv_dlength.attr)
2169 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
2170 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
2171 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
2172 else if (attr == &dev_attr_conn_header_digest.attr)
2173 param = ISCSI_PARAM_HDRDGST_EN;
2174 else if (attr == &dev_attr_conn_data_digest.attr)
2175 param = ISCSI_PARAM_DATADGST_EN;
2176 else if (attr == &dev_attr_conn_ifmarker.attr)
2177 param = ISCSI_PARAM_IFMARKER_EN;
2178 else if (attr == &dev_attr_conn_ofmarker.attr)
2179 param = ISCSI_PARAM_OFMARKER_EN;
2180 else if (attr == &dev_attr_conn_address.attr)
2181 param = ISCSI_PARAM_CONN_ADDRESS;
2182 else if (attr == &dev_attr_conn_port.attr)
2183 param = ISCSI_PARAM_CONN_PORT;
2184 else if (attr == &dev_attr_conn_exp_statsn.attr)
2185 param = ISCSI_PARAM_EXP_STATSN;
2186 else if (attr == &dev_attr_conn_persistent_address.attr)
2187 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
2188 else if (attr == &dev_attr_conn_persistent_port.attr)
2189 param = ISCSI_PARAM_PERSISTENT_PORT;
2190 else if (attr == &dev_attr_conn_ping_tmo.attr)
2191 param = ISCSI_PARAM_PING_TMO;
2192 else if (attr == &dev_attr_conn_recv_tmo.attr)
2193 param = ISCSI_PARAM_RECV_TMO;
2194 else {
2195 WARN_ONCE(1, "Invalid conn attr");
2196 return 0;
2197 }
2198
2199 return t->attr_is_visible(ISCSI_PARAM, param);
2200}
2201
2202static struct attribute_group iscsi_conn_group = {
2203 .attrs = iscsi_conn_attrs,
2204 .is_visible = iscsi_conn_attr_is_visible,
2205};
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207/*
Alex Aizman0896b752005-08-04 19:33:07 -07002208 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 */
Mike Christieb2c64162007-05-30 12:57:16 -05002210#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002212show_session_param_##param(struct device *dev, \
2213 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002214{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002215 struct iscsi_cls_session *session = \
2216 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002217 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05002218 \
2219 if (perm && !capable(CAP_SYS_ADMIN)) \
2220 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05002221 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002222}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Mike Christieb2c64162007-05-30 12:57:16 -05002224#define iscsi_session_attr(field, param, perm) \
2225 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05002226static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05002227 NULL);
Mike Christieb2c64162007-05-30 12:57:16 -05002228iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
2229iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
2230iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
2231iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
2232iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
2233iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
2234iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
2235iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
2236iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
2237iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
2238iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
2239iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
2240iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
2241iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06002242iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
2243iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
2244iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002245iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05002246iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05302247iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
2248iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05002249
Mike Christie6eabafb2008-01-31 13:36:43 -06002250static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01002251show_priv_session_state(struct device *dev, struct device_attribute *attr,
2252 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06002253{
Tony Jonesee959b02008-02-22 00:13:36 +01002254 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06002255 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
2256}
2257static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
2258 NULL);
2259
Mike Christiefd7255f2006-04-06 21:13:36 -05002260#define iscsi_priv_session_attr_show(field, format) \
2261static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002262show_priv_session_##field(struct device *dev, \
2263 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002264{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002265 struct iscsi_cls_session *session = \
2266 iscsi_dev_to_session(dev->parent); \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302267 if (session->field == -1) \
2268 return sprintf(buf, "off\n"); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002269 return sprintf(buf, format"\n", session->field); \
2270}
2271
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302272#define iscsi_priv_session_attr_store(field) \
2273static ssize_t \
2274store_priv_session_##field(struct device *dev, \
2275 struct device_attribute *attr, \
2276 const char *buf, size_t count) \
2277{ \
2278 int val; \
2279 char *cp; \
2280 struct iscsi_cls_session *session = \
2281 iscsi_dev_to_session(dev->parent); \
2282 if ((session->state == ISCSI_SESSION_FREE) || \
2283 (session->state == ISCSI_SESSION_FAILED)) \
2284 return -EBUSY; \
2285 if (strncmp(buf, "off", 3) == 0) \
2286 session->field = -1; \
2287 else { \
2288 val = simple_strtoul(buf, &cp, 0); \
2289 if (*cp != '\0' && *cp != '\n') \
2290 return -EINVAL; \
2291 session->field = val; \
2292 } \
2293 return count; \
2294}
2295
2296#define iscsi_priv_session_rw_attr(field, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002297 iscsi_priv_session_attr_show(field, format) \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302298 iscsi_priv_session_attr_store(field) \
Vasiliy Kulikov523f3c82011-02-04 15:24:14 +03002299static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302300 show_priv_session_##field, \
2301 store_priv_session_##field)
2302iscsi_priv_session_rw_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05002303
Mike Christie1d063c12011-07-25 13:48:43 -05002304static struct attribute *iscsi_session_attrs[] = {
2305 &dev_attr_sess_initial_r2t.attr,
2306 &dev_attr_sess_max_outstanding_r2t.attr,
2307 &dev_attr_sess_immediate_data.attr,
2308 &dev_attr_sess_first_burst_len.attr,
2309 &dev_attr_sess_max_burst_len.attr,
2310 &dev_attr_sess_data_pdu_in_order.attr,
2311 &dev_attr_sess_data_seq_in_order.attr,
2312 &dev_attr_sess_erl.attr,
2313 &dev_attr_sess_targetname.attr,
2314 &dev_attr_sess_tpgt.attr,
2315 &dev_attr_sess_password.attr,
2316 &dev_attr_sess_password_in.attr,
2317 &dev_attr_sess_username.attr,
2318 &dev_attr_sess_username_in.attr,
2319 &dev_attr_sess_fast_abort.attr,
2320 &dev_attr_sess_abort_tmo.attr,
2321 &dev_attr_sess_lu_reset_tmo.attr,
2322 &dev_attr_sess_tgt_reset_tmo.attr,
2323 &dev_attr_sess_ifacename.attr,
2324 &dev_attr_sess_initiatorname.attr,
2325 &dev_attr_sess_targetalias.attr,
2326 &dev_attr_priv_sess_recovery_tmo.attr,
2327 &dev_attr_priv_sess_state.attr,
2328 NULL,
2329};
2330
2331static mode_t iscsi_session_attr_is_visible(struct kobject *kobj,
2332 struct attribute *attr, int i)
2333{
2334 struct device *cdev = container_of(kobj, struct device, kobj);
2335 struct iscsi_cls_session *session = transport_class_to_session(cdev);
2336 struct iscsi_transport *t = session->transport;
2337 int param;
2338
2339 if (attr == &dev_attr_sess_initial_r2t.attr)
2340 param = ISCSI_PARAM_INITIAL_R2T_EN;
2341 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
2342 param = ISCSI_PARAM_MAX_R2T;
2343 else if (attr == &dev_attr_sess_immediate_data.attr)
2344 param = ISCSI_PARAM_IMM_DATA_EN;
2345 else if (attr == &dev_attr_sess_first_burst_len.attr)
2346 param = ISCSI_PARAM_FIRST_BURST;
2347 else if (attr == &dev_attr_sess_max_burst_len.attr)
2348 param = ISCSI_PARAM_MAX_BURST;
2349 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
2350 param = ISCSI_PARAM_PDU_INORDER_EN;
2351 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
2352 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
2353 else if (attr == &dev_attr_sess_erl.attr)
2354 param = ISCSI_PARAM_ERL;
2355 else if (attr == &dev_attr_sess_targetname.attr)
2356 param = ISCSI_PARAM_TARGET_NAME;
2357 else if (attr == &dev_attr_sess_tpgt.attr)
2358 param = ISCSI_PARAM_TPGT;
2359 else if (attr == &dev_attr_sess_password.attr)
2360 param = ISCSI_PARAM_USERNAME;
2361 else if (attr == &dev_attr_sess_password_in.attr)
2362 param = ISCSI_PARAM_USERNAME_IN;
2363 else if (attr == &dev_attr_sess_username.attr)
2364 param = ISCSI_PARAM_PASSWORD;
2365 else if (attr == &dev_attr_sess_username_in.attr)
2366 param = ISCSI_PARAM_PASSWORD_IN;
2367 else if (attr == &dev_attr_sess_fast_abort.attr)
2368 param = ISCSI_PARAM_FAST_ABORT;
2369 else if (attr == &dev_attr_sess_abort_tmo.attr)
2370 param = ISCSI_PARAM_ABORT_TMO;
2371 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
2372 param = ISCSI_PARAM_LU_RESET_TMO;
2373 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
2374 param = ISCSI_PARAM_TGT_RESET_TMO;
2375 else if (attr == &dev_attr_sess_ifacename.attr)
2376 param = ISCSI_PARAM_IFACE_NAME;
2377 else if (attr == &dev_attr_sess_initiatorname.attr)
2378 param = ISCSI_PARAM_INITIATOR_NAME;
2379 else if (attr == &dev_attr_sess_targetalias.attr)
2380 param = ISCSI_PARAM_TARGET_ALIAS;
2381 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
2382 return S_IRUGO | S_IWUSR;
2383 else if (attr == &dev_attr_priv_sess_state.attr)
2384 return S_IRUGO;
2385 else {
2386 WARN_ONCE(1, "Invalid session attr");
2387 return 0;
2388 }
2389
2390 return t->attr_is_visible(ISCSI_PARAM, param);
2391}
2392
2393static struct attribute_group iscsi_session_group = {
2394 .attrs = iscsi_session_attrs,
2395 .is_visible = iscsi_session_attr_is_visible,
2396};
2397
Mike Christie1819dc82007-05-30 12:57:08 -05002398/*
2399 * iSCSI host attrs
2400 */
2401#define iscsi_host_attr_show(param) \
2402static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002403show_host_param_##param(struct device *dev, \
2404 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05002405{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002406 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05002407 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
2408 return priv->iscsi_transport->get_host_param(shost, param, buf); \
2409}
2410
2411#define iscsi_host_attr(field, param) \
2412 iscsi_host_attr_show(param) \
2413static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
2414 NULL);
2415
Mike Christied8196ed2007-05-30 12:57:25 -05002416iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002417iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05002418iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05002419iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002420
Mike Christief27fb2e2011-07-25 13:48:45 -05002421static struct attribute *iscsi_host_attrs[] = {
2422 &dev_attr_host_netdev.attr,
2423 &dev_attr_host_hwaddress.attr,
2424 &dev_attr_host_ipaddress.attr,
2425 &dev_attr_host_initiatorname.attr,
2426 NULL,
2427};
2428
2429static mode_t iscsi_host_attr_is_visible(struct kobject *kobj,
2430 struct attribute *attr, int i)
2431{
2432 struct device *cdev = container_of(kobj, struct device, kobj);
2433 struct Scsi_Host *shost = transport_class_to_shost(cdev);
2434 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
2435 int param;
2436
2437 if (attr == &dev_attr_host_netdev.attr)
2438 param = ISCSI_HOST_PARAM_NETDEV_NAME;
2439 else if (attr == &dev_attr_host_hwaddress.attr)
2440 param = ISCSI_HOST_PARAM_HWADDRESS;
2441 else if (attr == &dev_attr_host_ipaddress.attr)
2442 param = ISCSI_HOST_PARAM_IPADDRESS;
2443 else if (attr == &dev_attr_host_initiatorname.attr)
2444 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
2445 else {
2446 WARN_ONCE(1, "Invalid host attr");
2447 return 0;
2448 }
2449
2450 return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
2451}
2452
2453static struct attribute_group iscsi_host_group = {
2454 .attrs = iscsi_host_attrs,
2455 .is_visible = iscsi_host_attr_is_visible,
2456};
Mike Christie1819dc82007-05-30 12:57:08 -05002457
Alex Aizman0896b752005-08-04 19:33:07 -07002458static int iscsi_session_match(struct attribute_container *cont,
2459 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Mike Christie7b8631b2006-01-13 18:05:50 -06002461 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07002463 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Alex Aizman0896b752005-08-04 19:33:07 -07002465 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return 0;
2467
Mike Christie7b8631b2006-01-13 18:05:50 -06002468 session = iscsi_dev_to_session(dev);
2469 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07002470 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 return 0;
2472
Alex Aizman0896b752005-08-04 19:33:07 -07002473 priv = to_iscsi_internal(shost->transportt);
2474 if (priv->session_cont.ac.class != &iscsi_session_class.class)
2475 return 0;
2476
2477 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478}
2479
Alex Aizman0896b752005-08-04 19:33:07 -07002480static int iscsi_conn_match(struct attribute_container *cont,
2481 struct device *dev)
2482{
Mike Christie7b8631b2006-01-13 18:05:50 -06002483 struct iscsi_cls_session *session;
2484 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07002485 struct Scsi_Host *shost;
2486 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Alex Aizman0896b752005-08-04 19:33:07 -07002488 if (!iscsi_is_conn_dev(dev))
2489 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Mike Christie7b8631b2006-01-13 18:05:50 -06002491 conn = iscsi_dev_to_conn(dev);
2492 session = iscsi_dev_to_session(conn->dev.parent);
2493 shost = iscsi_session_to_shost(session);
2494
Alex Aizman0896b752005-08-04 19:33:07 -07002495 if (!shost->transportt)
2496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Alex Aizman0896b752005-08-04 19:33:07 -07002498 priv = to_iscsi_internal(shost->transportt);
2499 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
2500 return 0;
2501
2502 return &priv->conn_cont.ac == cont;
2503}
2504
Mike Christie30a6c652006-04-06 21:13:39 -05002505static int iscsi_host_match(struct attribute_container *cont,
2506 struct device *dev)
2507{
2508 struct Scsi_Host *shost;
2509 struct iscsi_internal *priv;
2510
2511 if (!scsi_is_host_device(dev))
2512 return 0;
2513
2514 shost = dev_to_shost(dev);
2515 if (!shost->transportt ||
2516 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
2517 return 0;
2518
2519 priv = to_iscsi_internal(shost->transportt);
2520 return &priv->t.host_attrs.ac == cont;
2521}
2522
Mike Christie7b8631b2006-01-13 18:05:50 -06002523struct scsi_transport_template *
2524iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07002525{
2526 struct iscsi_internal *priv;
2527 unsigned long flags;
Mike Christief27fb2e2011-07-25 13:48:45 -05002528 int err;
Alex Aizman0896b752005-08-04 19:33:07 -07002529
2530 BUG_ON(!tt);
2531
2532 priv = iscsi_if_transport_lookup(tt);
2533 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002534 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002535
Jes Sorensen24669f752006-01-16 10:31:18 -05002536 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07002537 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002538 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002539 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07002540 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05002541 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06002542 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07002543
Tony Jonesee959b02008-02-22 00:13:36 +01002544 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01002545 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01002546 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07002547 if (err)
2548 goto free_priv;
2549
Tony Jonesee959b02008-02-22 00:13:36 +01002550 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07002551 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01002552 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07002553
Mike Christie30a6c652006-04-06 21:13:39 -05002554 /* host parameters */
Mike Christie30a6c652006-04-06 21:13:39 -05002555 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
2556 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christief27fb2e2011-07-25 13:48:45 -05002557 priv->t.host_attrs.ac.grp = &iscsi_host_group;
Mike Christie32c6e1b2008-05-21 15:53:58 -05002558 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05002559 transport_container_register(&priv->t.host_attrs);
2560
Alex Aizman0896b752005-08-04 19:33:07 -07002561 /* connection parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07002562 priv->conn_cont.ac.class = &iscsi_connection_class.class;
2563 priv->conn_cont.ac.match = iscsi_conn_match;
Mike Christie3128c6c2011-07-25 13:48:42 -05002564 priv->conn_cont.ac.grp = &iscsi_conn_group;
Alex Aizman0896b752005-08-04 19:33:07 -07002565 transport_container_register(&priv->conn_cont);
2566
Alex Aizman0896b752005-08-04 19:33:07 -07002567 /* session parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07002568 priv->session_cont.ac.class = &iscsi_session_class.class;
2569 priv->session_cont.ac.match = iscsi_session_match;
Mike Christie1d063c12011-07-25 13:48:43 -05002570 priv->session_cont.ac.grp = &iscsi_session_group;
Alex Aizman0896b752005-08-04 19:33:07 -07002571 transport_container_register(&priv->session_cont);
2572
Alex Aizman0896b752005-08-04 19:33:07 -07002573 spin_lock_irqsave(&iscsi_transport_lock, flags);
2574 list_add(&priv->list, &iscsi_transports);
2575 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Alex Aizman0896b752005-08-04 19:33:07 -07002577 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06002578 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Tony Jonesee959b02008-02-22 00:13:36 +01002580unregister_dev:
2581 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05002582 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002583free_priv:
2584 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06002585 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
Alex Aizman0896b752005-08-04 19:33:07 -07002587EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Alex Aizman0896b752005-08-04 19:33:07 -07002589int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
Alex Aizman0896b752005-08-04 19:33:07 -07002591 struct iscsi_internal *priv;
2592 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Alex Aizman0896b752005-08-04 19:33:07 -07002594 BUG_ON(!tt);
2595
Arjan van de Ven0b950672006-01-11 13:16:10 +01002596 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002597
2598 priv = iscsi_if_transport_lookup(tt);
2599 BUG_ON (!priv);
2600
Alex Aizman0896b752005-08-04 19:33:07 -07002601 spin_lock_irqsave(&iscsi_transport_lock, flags);
2602 list_del(&priv->list);
2603 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2604
2605 transport_container_unregister(&priv->conn_cont);
2606 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05002607 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07002608
Tony Jonesee959b02008-02-22 00:13:36 +01002609 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
2610 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002611 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002612
2613 return 0;
2614}
2615EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617static __init int iscsi_transport_init(void)
2618{
Alex Aizman0896b752005-08-04 19:33:07 -07002619 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Meelis Roos09492602006-12-17 12:10:26 -06002621 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05002622 ISCSI_TRANSPORT_VERSION);
2623
Mike Christie41be1442007-02-28 17:32:18 -06002624 atomic_set(&iscsi_session_nr, 0);
2625
Alex Aizman0896b752005-08-04 19:33:07 -07002626 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if (err)
2628 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07002629
Mike Christied82ff9be2008-05-21 15:54:13 -05002630 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002631 if (err)
2632 goto unregister_transport_class;
2633
Mike Christie8d079132011-07-25 13:48:40 -05002634 err = class_register(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002635 if (err)
2636 goto unregister_endpoint_class;
2637
Mike Christie8d079132011-07-25 13:48:40 -05002638 err = transport_class_register(&iscsi_host_class);
2639 if (err)
2640 goto unregister_iface_class;
2641
Mike Christie30a6c652006-04-06 21:13:39 -05002642 err = transport_class_register(&iscsi_connection_class);
2643 if (err)
2644 goto unregister_host_class;
2645
Alex Aizman0896b752005-08-04 19:33:07 -07002646 err = transport_class_register(&iscsi_session_class);
2647 if (err)
2648 goto unregister_conn_class;
2649
Mike Christied82ff9be2008-05-21 15:54:13 -05002650 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2651 NULL, THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07002652 if (!nls) {
2653 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04002654 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07002655 }
2656
Mike Christied8bf5412007-12-13 12:43:27 -06002657 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2658 if (!iscsi_eh_timer_workq)
2659 goto release_nls;
2660
Mike Christie43a145a2006-10-16 18:09:38 -04002661 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07002662
Mike Christied8bf5412007-12-13 12:43:27 -06002663release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002664 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002665unregister_session_class:
2666 transport_class_unregister(&iscsi_session_class);
2667unregister_conn_class:
2668 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002669unregister_host_class:
2670 transport_class_unregister(&iscsi_host_class);
Mike Christie8d079132011-07-25 13:48:40 -05002671unregister_iface_class:
2672 class_unregister(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002673unregister_endpoint_class:
2674 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002675unregister_transport_class:
2676 class_unregister(&iscsi_transport_class);
2677 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678}
2679
2680static void __exit iscsi_transport_exit(void)
2681{
Mike Christied8bf5412007-12-13 12:43:27 -06002682 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002683 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002684 transport_class_unregister(&iscsi_connection_class);
2685 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002686 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002687 class_unregister(&iscsi_endpoint_class);
Mike Christie8d079132011-07-25 13:48:40 -05002688 class_unregister(&iscsi_iface_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002689 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690}
2691
2692module_init(iscsi_transport_init);
2693module_exit(iscsi_transport_exit);
2694
Alex Aizman0896b752005-08-04 19:33:07 -07002695MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2696 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2697 "Alex Aizman <itn780@yahoo.com>");
2698MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05002700MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
Stephen Hemminger058548a2010-12-09 09:37:56 -08002701MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);