blob: cfd491437239750937b7a3d16f5235f6503f158b [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>
Mike Christie8d4a6902011-10-06 03:56:57 -050027#include <linux/idr.h>
Alex Aizman0896b752005-08-04 19:33:07 -070028#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <scsi/scsi.h>
30#include <scsi/scsi_host.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_transport.h>
33#include <scsi/scsi_transport_iscsi.h>
Alex Aizman0896b752005-08-04 19:33:07 -070034#include <scsi/iscsi_if.h>
Mike Christiec01be6d2010-07-22 16:59:49 +053035#include <scsi/scsi_cmnd.h>
Mike Christie90eeb012011-07-25 13:48:50 -050036#include <scsi/scsi_bsg_iscsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Mike Christie4c2133c2008-06-16 10:11:34 -050038#define ISCSI_TRANSPORT_VERSION "2.0-870"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Mike Christie632248a2009-08-20 15:11:01 -050040static int dbg_session;
41module_param_named(debug_session, dbg_session, int,
42 S_IRUGO | S_IWUSR);
43MODULE_PARM_DESC(debug_session,
44 "Turn on debugging for sessions in scsi_transport_iscsi "
45 "module. Set to 1 to turn on, and zero to turn off. Default "
46 "is off.");
47
48static int dbg_conn;
49module_param_named(debug_conn, dbg_conn, int,
50 S_IRUGO | S_IWUSR);
51MODULE_PARM_DESC(debug_conn,
52 "Turn on debugging for connections in scsi_transport_iscsi "
53 "module. Set to 1 to turn on, and zero to turn off. Default "
54 "is off.");
55
56#define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
57 do { \
58 if (dbg_session) \
59 iscsi_cls_session_printk(KERN_INFO, _session, \
60 "%s: " dbg_fmt, \
61 __func__, ##arg); \
62 } while (0);
63
64#define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
65 do { \
66 if (dbg_conn) \
67 iscsi_cls_conn_printk(KERN_INFO, _conn, \
68 "%s: " dbg_fmt, \
69 __func__, ##arg); \
70 } while (0);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072struct iscsi_internal {
73 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070074 struct iscsi_transport *iscsi_transport;
75 struct list_head list;
Tony Jonesee959b02008-02-22 00:13:36 +010076 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050077
Alex Aizman0896b752005-08-04 19:33:07 -070078 struct transport_container conn_cont;
Alex Aizman0896b752005-08-04 19:33:07 -070079 struct transport_container session_cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
Mike Christie41be1442007-02-28 17:32:18 -060082static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060083static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050084
Mike Christie8d4a6902011-10-06 03:56:57 -050085static DEFINE_IDA(iscsi_sess_ida);
Alex Aizman0896b752005-08-04 19:33:07 -070086/*
87 * list of registered transports and lock that must
88 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010089 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070090 */
91static LIST_HEAD(iscsi_transports);
92static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Alex Aizman0896b752005-08-04 19:33:07 -070094#define to_iscsi_internal(tmpl) \
95 container_of(tmpl, struct iscsi_internal, t)
96
Tony Jonesee959b02008-02-22 00:13:36 +010097#define dev_to_iscsi_internal(_dev) \
98 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -070099
Tony Jonesee959b02008-02-22 00:13:36 +0100100static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700101{
Tony Jonesee959b02008-02-22 00:13:36 +0100102 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -0700103 kfree(priv);
104}
105
106/*
107 * iscsi_transport_class represents the iscsi_transports that are
108 * registered.
109 */
110static struct class iscsi_transport_class = {
111 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +0100112 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -0700113};
114
115static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100116show_transport_handle(struct device *dev, struct device_attribute *attr,
117 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -0700118{
Tony Jonesee959b02008-02-22 00:13:36 +0100119 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Mike Christie762e2bf2005-09-12 21:01:46 -0500120 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -0700121}
Tony Jonesee959b02008-02-22 00:13:36 +0100122static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700123
124#define show_transport_attr(name, format) \
125static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100126show_transport_##name(struct device *dev, \
127 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -0700128{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100129 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Alex Aizman0896b752005-08-04 19:33:07 -0700130 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
131} \
Tony Jonesee959b02008-02-22 00:13:36 +0100132static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700133
134show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700135
136static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100137 &dev_attr_handle.attr,
138 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700139 NULL,
140};
141
142static struct attribute_group iscsi_transport_group = {
143 .attrs = iscsi_transport_attrs,
144};
145
Mike Christied82ff9be2008-05-21 15:54:13 -0500146/*
147 * iSCSI endpoint attrs
148 */
149#define iscsi_dev_to_endpoint(_dev) \
150 container_of(_dev, struct iscsi_endpoint, dev)
151
152#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
153struct device_attribute dev_attr_##_prefix##_##_name = \
154 __ATTR(_name,_mode,_show,_store)
155
156static void iscsi_endpoint_release(struct device *dev)
157{
158 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
159 kfree(ep);
160}
161
162static struct class iscsi_endpoint_class = {
163 .name = "iscsi_endpoint",
164 .dev_release = iscsi_endpoint_release,
165};
166
167static ssize_t
168show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
169{
170 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500171 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500172}
173static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
174
175static struct attribute *iscsi_endpoint_attrs[] = {
176 &dev_attr_ep_handle.attr,
177 NULL,
178};
179
180static struct attribute_group iscsi_endpoint_group = {
181 .attrs = iscsi_endpoint_attrs,
182};
183
184#define ISCSI_MAX_EPID -1
185
186static int iscsi_match_epid(struct device *dev, void *data)
187{
188 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500189 uint64_t *epid = (uint64_t *) data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500190
191 return *epid == ep->id;
192}
193
194struct iscsi_endpoint *
195iscsi_create_endpoint(int dd_size)
196{
197 struct device *dev;
198 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500199 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500200 int err;
201
202 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400203 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500204 iscsi_match_epid);
205 if (!dev)
206 break;
207 }
208 if (id == ISCSI_MAX_EPID) {
209 printk(KERN_ERR "Too many connections. Max supported %u\n",
210 ISCSI_MAX_EPID - 1);
211 return NULL;
212 }
213
214 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
215 if (!ep)
216 return NULL;
217
218 ep->id = id;
219 ep->dev.class = &iscsi_endpoint_class;
Kay Sievers71610f52008-12-03 22:41:36 +0100220 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500221 err = device_register(&ep->dev);
222 if (err)
223 goto free_ep;
224
225 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
226 if (err)
227 goto unregister_dev;
228
229 if (dd_size)
230 ep->dd_data = &ep[1];
231 return ep;
232
233unregister_dev:
234 device_unregister(&ep->dev);
235 return NULL;
236
237free_ep:
238 kfree(ep);
239 return NULL;
240}
241EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
242
243void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
244{
245 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
246 device_unregister(&ep->dev);
247}
248EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
249
250struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
251{
Mike Christief80f8682008-06-16 10:11:35 -0500252 struct iscsi_endpoint *ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500253 struct device *dev;
254
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400255 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500256 iscsi_match_epid);
257 if (!dev)
258 return NULL;
259
Mike Christief80f8682008-06-16 10:11:35 -0500260 ep = iscsi_dev_to_endpoint(dev);
261 /*
262 * we can drop this now because the interface will prevent
263 * removals and lookups from racing.
264 */
265 put_device(dev);
266 return ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500267}
268EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
269
Mike Christie8d079132011-07-25 13:48:40 -0500270/*
271 * Interface to display network param to sysfs
272 */
273
274static void iscsi_iface_release(struct device *dev)
275{
276 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
277 struct device *parent = iface->dev.parent;
278
279 kfree(iface);
280 put_device(parent);
281}
282
283
284static struct class iscsi_iface_class = {
285 .name = "iscsi_iface",
286 .dev_release = iscsi_iface_release,
287};
288
289#define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
290struct device_attribute dev_attr_##_prefix##_##_name = \
291 __ATTR(_name, _mode, _show, _store)
292
293/* iface attrs show */
294#define iscsi_iface_attr_show(type, name, param_type, param) \
295static ssize_t \
296show_##type##_##name(struct device *dev, struct device_attribute *attr, \
297 char *buf) \
298{ \
299 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
300 struct iscsi_transport *t = iface->transport; \
301 return t->get_iface_param(iface, param_type, param, buf); \
302} \
303
304#define iscsi_iface_net_attr(type, name, param) \
305 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
306static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
307
308/* generic read only ipvi4 attribute */
309iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
310iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
311iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
312iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
313
314/* generic read only ipv6 attribute */
315iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
316iscsi_iface_net_attr(ipv6_iface, link_local_addr, ISCSI_NET_PARAM_IPV6_LINKLOCAL);
317iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
318iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
319 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
Mike Christie88f4f512011-10-06 03:56:58 -0500320iscsi_iface_net_attr(ipv6_iface, link_local_autocfg,
Mike Christie8d079132011-07-25 13:48:40 -0500321 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
322
323/* common read only iface attribute */
324iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
Mike Christie2d636732011-10-11 17:55:11 -0500325iscsi_iface_net_attr(iface, vlan_id, ISCSI_NET_PARAM_VLAN_ID);
Mike Christie4223b9e2011-07-25 13:48:47 -0500326iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
327iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
Vikas Chaudhary8c7d40f2011-08-01 03:26:12 -0700328iscsi_iface_net_attr(iface, mtu, ISCSI_NET_PARAM_MTU);
Vikas Chaudharyfcb51242011-08-01 03:26:18 -0700329iscsi_iface_net_attr(iface, port, ISCSI_NET_PARAM_PORT);
Mike Christie8d079132011-07-25 13:48:40 -0500330
Al Viro587a1f12011-07-23 23:11:19 -0400331static umode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
Mike Christie8d079132011-07-25 13:48:40 -0500332 struct attribute *attr, int i)
333{
334 struct device *dev = container_of(kobj, struct device, kobj);
335 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
336 struct iscsi_transport *t = iface->transport;
Mike Christieb78dbba2011-07-25 13:48:44 -0500337 int param;
Mike Christie8d079132011-07-25 13:48:40 -0500338
339 if (attr == &dev_attr_iface_enabled.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500340 param = ISCSI_NET_PARAM_IFACE_ENABLE;
Mike Christie2d636732011-10-11 17:55:11 -0500341 else if (attr == &dev_attr_iface_vlan_id.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500342 param = ISCSI_NET_PARAM_VLAN_ID;
Mike Christie4223b9e2011-07-25 13:48:47 -0500343 else if (attr == &dev_attr_iface_vlan_priority.attr)
344 param = ISCSI_NET_PARAM_VLAN_PRIORITY;
345 else if (attr == &dev_attr_iface_vlan_enabled.attr)
346 param = ISCSI_NET_PARAM_VLAN_ENABLED;
Vikas Chaudhary8c7d40f2011-08-01 03:26:12 -0700347 else if (attr == &dev_attr_iface_mtu.attr)
348 param = ISCSI_NET_PARAM_MTU;
Vikas Chaudharyfcb51242011-08-01 03:26:18 -0700349 else if (attr == &dev_attr_iface_port.attr)
350 param = ISCSI_NET_PARAM_PORT;
Mike Christieb78dbba2011-07-25 13:48:44 -0500351 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
Mike Christie8d079132011-07-25 13:48:40 -0500352 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500353 param = ISCSI_NET_PARAM_IPV4_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500354 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500355 param = ISCSI_NET_PARAM_IPV4_GW;
Mike Christie8d079132011-07-25 13:48:40 -0500356 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500357 param = ISCSI_NET_PARAM_IPV4_SUBNET;
Mike Christie8d079132011-07-25 13:48:40 -0500358 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500359 param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
360 else
361 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500362 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
363 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500364 param = ISCSI_NET_PARAM_IPV6_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500365 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500366 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
Mike Christie8d079132011-07-25 13:48:40 -0500367 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500368 param = ISCSI_NET_PARAM_IPV6_ROUTER;
Mike Christie8d079132011-07-25 13:48:40 -0500369 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500370 param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
Mike Christie88f4f512011-10-06 03:56:58 -0500371 else if (attr == &dev_attr_ipv6_iface_link_local_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500372 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
373 else
374 return 0;
375 } else {
376 WARN_ONCE(1, "Invalid iface attr");
377 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500378 }
379
Mike Christieb78dbba2011-07-25 13:48:44 -0500380 return t->attr_is_visible(ISCSI_NET_PARAM, param);
Mike Christie8d079132011-07-25 13:48:40 -0500381}
382
383static struct attribute *iscsi_iface_attrs[] = {
384 &dev_attr_iface_enabled.attr,
Mike Christie2d636732011-10-11 17:55:11 -0500385 &dev_attr_iface_vlan_id.attr,
Mike Christie4223b9e2011-07-25 13:48:47 -0500386 &dev_attr_iface_vlan_priority.attr,
387 &dev_attr_iface_vlan_enabled.attr,
Mike Christie8d079132011-07-25 13:48:40 -0500388 &dev_attr_ipv4_iface_ipaddress.attr,
389 &dev_attr_ipv4_iface_gateway.attr,
390 &dev_attr_ipv4_iface_subnet.attr,
391 &dev_attr_ipv4_iface_bootproto.attr,
392 &dev_attr_ipv6_iface_ipaddress.attr,
393 &dev_attr_ipv6_iface_link_local_addr.attr,
394 &dev_attr_ipv6_iface_router_addr.attr,
395 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
Mike Christie88f4f512011-10-06 03:56:58 -0500396 &dev_attr_ipv6_iface_link_local_autocfg.attr,
Vikas Chaudhary8c7d40f2011-08-01 03:26:12 -0700397 &dev_attr_iface_mtu.attr,
Vikas Chaudharyfcb51242011-08-01 03:26:18 -0700398 &dev_attr_iface_port.attr,
Mike Christie8d079132011-07-25 13:48:40 -0500399 NULL,
400};
401
402static struct attribute_group iscsi_iface_group = {
403 .attrs = iscsi_iface_attrs,
404 .is_visible = iscsi_iface_attr_is_visible,
405};
406
407struct iscsi_iface *
408iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
409 uint32_t iface_type, uint32_t iface_num, int dd_size)
410{
411 struct iscsi_iface *iface;
412 int err;
413
414 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
415 if (!iface)
416 return NULL;
417
418 iface->transport = transport;
419 iface->iface_type = iface_type;
420 iface->iface_num = iface_num;
421 iface->dev.release = iscsi_iface_release;
422 iface->dev.class = &iscsi_iface_class;
423 /* parent reference released in iscsi_iface_release */
424 iface->dev.parent = get_device(&shost->shost_gendev);
425 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
426 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
427 iface_num);
428 else
429 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
430 iface_num);
431
432 err = device_register(&iface->dev);
433 if (err)
434 goto free_iface;
435
436 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
437 if (err)
438 goto unreg_iface;
439
440 if (dd_size)
441 iface->dd_data = &iface[1];
442 return iface;
443
444unreg_iface:
445 device_unregister(&iface->dev);
446 return NULL;
447
448free_iface:
449 put_device(iface->dev.parent);
450 kfree(iface);
451 return NULL;
452}
453EXPORT_SYMBOL_GPL(iscsi_create_iface);
454
455void iscsi_destroy_iface(struct iscsi_iface *iface)
456{
457 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
458 device_unregister(&iface->dev);
459}
460EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
461
Mike Christie90eeb012011-07-25 13:48:50 -0500462/*
463 * BSG support
464 */
465/**
466 * iscsi_bsg_host_dispatch - Dispatch command to LLD.
467 * @job: bsg job to be processed
468 */
469static int iscsi_bsg_host_dispatch(struct bsg_job *job)
470{
471 struct Scsi_Host *shost = iscsi_job_to_shost(job);
472 struct iscsi_bsg_request *req = job->request;
473 struct iscsi_bsg_reply *reply = job->reply;
474 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
475 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
476 int ret;
477
478 /* check if we have the msgcode value at least */
479 if (job->request_len < sizeof(uint32_t)) {
480 ret = -ENOMSG;
481 goto fail_host_msg;
482 }
483
484 /* Validate the host command */
485 switch (req->msgcode) {
486 case ISCSI_BSG_HST_VENDOR:
487 cmdlen += sizeof(struct iscsi_bsg_host_vendor);
488 if ((shost->hostt->vendor_id == 0L) ||
489 (req->rqst_data.h_vendor.vendor_id !=
490 shost->hostt->vendor_id)) {
491 ret = -ESRCH;
492 goto fail_host_msg;
493 }
494 break;
495 default:
496 ret = -EBADR;
497 goto fail_host_msg;
498 }
499
500 /* check if we really have all the request data needed */
501 if (job->request_len < cmdlen) {
502 ret = -ENOMSG;
503 goto fail_host_msg;
504 }
505
506 ret = i->iscsi_transport->bsg_request(job);
507 if (!ret)
508 return 0;
509
510fail_host_msg:
511 /* return the errno failure code as the only status */
512 BUG_ON(job->reply_len < sizeof(uint32_t));
513 reply->reply_payload_rcv_len = 0;
514 reply->result = ret;
515 job->reply_len = sizeof(uint32_t);
516 bsg_job_done(job, ret, 0);
517 return 0;
518}
519
520/**
521 * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
522 * @shost: shost for iscsi_host
Marcos Paulo de Souzaeae627e2011-11-02 11:17:17 -0200523 * @ihost: iscsi_cls_host adding the structures to
Mike Christie90eeb012011-07-25 13:48:50 -0500524 */
525static int
526iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
527{
528 struct device *dev = &shost->shost_gendev;
529 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
530 struct request_queue *q;
531 char bsg_name[20];
532 int ret;
533
534 if (!i->iscsi_transport->bsg_request)
535 return -ENOTSUPP;
536
537 snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
538
539 q = __scsi_alloc_queue(shost, bsg_request_fn);
540 if (!q)
541 return -ENOMEM;
542
543 ret = bsg_setup_queue(dev, q, bsg_name, iscsi_bsg_host_dispatch, 0);
544 if (ret) {
545 shost_printk(KERN_ERR, shost, "bsg interface failed to "
546 "initialize - no request queue\n");
547 blk_cleanup_queue(q);
548 return ret;
549 }
550
551 ihost->bsg_q = q;
552 return 0;
553}
554
Mike Christie30a6c652006-04-06 21:13:39 -0500555static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100556 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500557{
558 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500559 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500560
561 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -0600562 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -0500563 mutex_init(&ihost->mutex);
Mike Christie90eeb012011-07-25 13:48:50 -0500564
565 iscsi_bsg_host_add(shost, ihost);
566 /* ignore any bsg add error - we just can't do sgio */
567
568 return 0;
569}
570
571static int iscsi_remove_host(struct transport_container *tc,
572 struct device *dev, struct device *cdev)
573{
574 struct Scsi_Host *shost = dev_to_shost(dev);
575 struct iscsi_cls_host *ihost = shost->shost_data;
576
577 if (ihost->bsg_q) {
578 bsg_remove_queue(ihost->bsg_q);
579 blk_cleanup_queue(ihost->bsg_q);
580 }
Mike Christie30a6c652006-04-06 21:13:39 -0500581 return 0;
582}
583
584static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
585 "iscsi_host",
586 iscsi_setup_host,
Mike Christie90eeb012011-07-25 13:48:50 -0500587 iscsi_remove_host,
Mike Christie30a6c652006-04-06 21:13:39 -0500588 NULL);
589
Alex Aizman0896b752005-08-04 19:33:07 -0700590static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
591 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 NULL,
593 NULL,
594 NULL);
595
Alex Aizman0896b752005-08-04 19:33:07 -0700596static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
597 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 NULL,
599 NULL,
600 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700601
602static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100603static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700604
Mike Christie7b7232f2006-02-01 21:06:49 -0600605static LIST_HEAD(sesslist);
606static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700607static LIST_HEAD(connlist);
608static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Mike Christieb5c7a122006-04-06 21:13:33 -0500610static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
611{
612 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
613 return sess->sid;
614}
615
616/*
617 * Returns the matching session to a given sid
618 */
619static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600620{
621 unsigned long flags;
622 struct iscsi_cls_session *sess;
623
624 spin_lock_irqsave(&sesslock, flags);
625 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500626 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600627 spin_unlock_irqrestore(&sesslock, flags);
628 return sess;
629 }
630 }
631 spin_unlock_irqrestore(&sesslock, flags);
632 return NULL;
633}
634
Mike Christieb5c7a122006-04-06 21:13:33 -0500635/*
636 * Returns the matching connection to a given sid / cid tuple
637 */
638static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600639{
640 unsigned long flags;
641 struct iscsi_cls_conn *conn;
642
643 spin_lock_irqsave(&connlock, flags);
644 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500645 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600646 spin_unlock_irqrestore(&connlock, flags);
647 return conn;
648 }
649 }
650 spin_unlock_irqrestore(&connlock, flags);
651 return NULL;
652}
653
Mike Christie7b8631b2006-01-13 18:05:50 -0600654/*
655 * The following functions can be used by LLDs that allocate
656 * their own scsi_hosts or by software iscsi LLDs
657 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600658static struct {
659 int value;
660 char *name;
661} iscsi_session_state_names[] = {
662 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
663 { ISCSI_SESSION_FAILED, "FAILED" },
664 { ISCSI_SESSION_FREE, "FREE" },
665};
666
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200667static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600668{
669 int i;
670 char *name = NULL;
671
672 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
673 if (iscsi_session_state_names[i].value == state) {
674 name = iscsi_session_state_names[i].name;
675 break;
676 }
677 }
678 return name;
679}
680
681int iscsi_session_chkready(struct iscsi_cls_session *session)
682{
683 unsigned long flags;
684 int err;
685
686 spin_lock_irqsave(&session->lock, flags);
687 switch (session->state) {
688 case ISCSI_SESSION_LOGGED_IN:
689 err = 0;
690 break;
691 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -0500692 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600693 break;
694 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500695 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600696 break;
697 default:
698 err = DID_NO_CONNECT << 16;
699 break;
700 }
701 spin_unlock_irqrestore(&session->lock, flags);
702 return err;
703}
704EXPORT_SYMBOL_GPL(iscsi_session_chkready);
705
Manish Rangankar17fa5752011-07-25 13:48:52 -0500706int iscsi_is_session_online(struct iscsi_cls_session *session)
707{
708 unsigned long flags;
709 int ret = 0;
710
711 spin_lock_irqsave(&session->lock, flags);
712 if (session->state == ISCSI_SESSION_LOGGED_IN)
713 ret = 1;
714 spin_unlock_irqrestore(&session->lock, flags);
715 return ret;
716}
717EXPORT_SYMBOL_GPL(iscsi_is_session_online);
718
Mike Christie7b8631b2006-01-13 18:05:50 -0600719static void iscsi_session_release(struct device *dev)
720{
721 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600722 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Mike Christie7b8631b2006-01-13 18:05:50 -0600724 shost = iscsi_session_to_shost(session);
725 scsi_host_put(shost);
Mike Christie632248a2009-08-20 15:11:01 -0500726 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600727 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600728}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Mike Christie7b8631b2006-01-13 18:05:50 -0600730static int iscsi_is_session_dev(const struct device *dev)
731{
732 return dev->release == iscsi_session_release;
733}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Mike Christiea4804cd2008-05-21 15:54:00 -0500735static int iscsi_iter_session_fn(struct device *dev, void *data)
736{
737 void (* fn) (struct iscsi_cls_session *) = data;
738
739 if (!iscsi_is_session_dev(dev))
740 return 0;
741 fn(iscsi_dev_to_session(dev));
742 return 0;
743}
744
745void iscsi_host_for_each_session(struct Scsi_Host *shost,
746 void (*fn)(struct iscsi_cls_session *))
747{
748 device_for_each_child(&shost->shost_gendev, fn,
749 iscsi_iter_session_fn);
750}
751EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
752
Mike Christie8aae18a2008-01-31 13:36:48 -0600753/**
754 * iscsi_scan_finished - helper to report when running scans are done
755 * @shost: scsi host
756 * @time: scan run time
757 *
758 * This function can be used by drives like qla4xxx to report to the scsi
759 * layer when the scans it kicked off at module load time are done.
760 */
761int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
762{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500763 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600764 /*
765 * qla4xxx will have kicked off some session unblocks before calling
766 * scsi_scan_host, so just wait for them to complete.
767 */
768 return !atomic_read(&ihost->nr_scans);
769}
770EXPORT_SYMBOL_GPL(iscsi_scan_finished);
771
Mike Christie79706342008-05-21 15:54:12 -0500772struct iscsi_scan_data {
773 unsigned int channel;
774 unsigned int id;
775 unsigned int lun;
776};
777
778static int iscsi_user_scan_session(struct device *dev, void *data)
779{
780 struct iscsi_scan_data *scan_data = data;
781 struct iscsi_cls_session *session;
782 struct Scsi_Host *shost;
783 struct iscsi_cls_host *ihost;
784 unsigned long flags;
785 unsigned int id;
786
787 if (!iscsi_is_session_dev(dev))
788 return 0;
789
790 session = iscsi_dev_to_session(dev);
Mike Christie632248a2009-08-20 15:11:01 -0500791
792 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
793
Mike Christie79706342008-05-21 15:54:12 -0500794 shost = iscsi_session_to_shost(session);
795 ihost = shost->shost_data;
796
797 mutex_lock(&ihost->mutex);
798 spin_lock_irqsave(&session->lock, flags);
799 if (session->state != ISCSI_SESSION_LOGGED_IN) {
800 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie632248a2009-08-20 15:11:01 -0500801 goto user_scan_exit;
Mike Christie79706342008-05-21 15:54:12 -0500802 }
803 id = session->target_id;
804 spin_unlock_irqrestore(&session->lock, flags);
805
806 if (id != ISCSI_MAX_TARGET) {
807 if ((scan_data->channel == SCAN_WILD_CARD ||
808 scan_data->channel == 0) &&
809 (scan_data->id == SCAN_WILD_CARD ||
810 scan_data->id == id))
811 scsi_scan_target(&session->dev, 0, id,
812 scan_data->lun, 1);
813 }
Mike Christie632248a2009-08-20 15:11:01 -0500814
815user_scan_exit:
Mike Christie79706342008-05-21 15:54:12 -0500816 mutex_unlock(&ihost->mutex);
Mike Christie632248a2009-08-20 15:11:01 -0500817 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
Mike Christie79706342008-05-21 15:54:12 -0500818 return 0;
819}
820
Mike Christie30a6c652006-04-06 21:13:39 -0500821static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
822 uint id, uint lun)
823{
Mike Christie79706342008-05-21 15:54:12 -0500824 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500825
Mike Christie79706342008-05-21 15:54:12 -0500826 scan_data.channel = channel;
827 scan_data.id = id;
828 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -0500829
Mike Christie79706342008-05-21 15:54:12 -0500830 return device_for_each_child(&shost->shost_gendev, &scan_data,
831 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -0500832}
833
Mike Christiebd976f62008-01-31 13:36:46 -0600834static void iscsi_scan_session(struct work_struct *work)
835{
836 struct iscsi_cls_session *session =
837 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600838 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500839 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500840 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600841
Mike Christie79706342008-05-21 15:54:12 -0500842 scan_data.channel = 0;
843 scan_data.id = SCAN_WILD_CARD;
844 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -0600845
Mike Christie79706342008-05-21 15:54:12 -0500846 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -0600847 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600848}
849
Mike Christiec01be6d2010-07-22 16:59:49 +0530850/**
851 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
Randy Dunlape6d4ef42010-08-14 13:05:41 -0700852 * @cmd: scsi cmd passed to scsi eh handler
Mike Christiec01be6d2010-07-22 16:59:49 +0530853 *
854 * If the session is down this function will wait for the recovery
855 * timer to fire or for the session to be logged back in. If the
856 * recovery timer fires then FAST_IO_FAIL is returned. The caller
857 * should pass this error value to the scsi eh.
858 */
859int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
860{
861 struct iscsi_cls_session *session =
862 starget_to_session(scsi_target(cmd->device));
863 unsigned long flags;
864 int ret = 0;
865
866 spin_lock_irqsave(&session->lock, flags);
867 while (session->state != ISCSI_SESSION_LOGGED_IN) {
868 if (session->state == ISCSI_SESSION_FREE) {
869 ret = FAST_IO_FAIL;
870 break;
871 }
872 spin_unlock_irqrestore(&session->lock, flags);
873 msleep(1000);
874 spin_lock_irqsave(&session->lock, flags);
875 }
876 spin_unlock_irqrestore(&session->lock, flags);
877 return ret;
878}
879EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
880
David Howellsc4028952006-11-22 14:57:56 +0000881static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500882{
David Howellsc4028952006-11-22 14:57:56 +0000883 struct iscsi_cls_session *session =
884 container_of(work, struct iscsi_cls_session,
885 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600886 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500887
Mike Christie322d7392008-01-31 13:36:52 -0600888 iscsi_cls_session_printk(KERN_INFO, session,
889 "session recovery timed out after %d secs\n",
890 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500891
Mike Christie6eabafb2008-01-31 13:36:43 -0600892 spin_lock_irqsave(&session->lock, flags);
893 switch (session->state) {
894 case ISCSI_SESSION_FAILED:
895 session->state = ISCSI_SESSION_FREE;
896 break;
897 case ISCSI_SESSION_LOGGED_IN:
898 case ISCSI_SESSION_FREE:
899 /* we raced with the unblock's flush */
900 spin_unlock_irqrestore(&session->lock, flags);
901 return;
902 }
903 spin_unlock_irqrestore(&session->lock, flags);
904
Mike Christie30a6c652006-04-06 21:13:39 -0500905 if (session->transport->session_recovery_timedout)
906 session->transport->session_recovery_timedout(session);
907
Mike Christie632248a2009-08-20 15:11:01 -0500908 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500909 scsi_target_unblock(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500910 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500911}
912
Mike Christie45ab33b2008-03-04 13:26:55 -0600913static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500914{
Mike Christie45ab33b2008-03-04 13:26:55 -0600915 struct iscsi_cls_session *session =
916 container_of(work, struct iscsi_cls_session,
917 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600918 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500919 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600920 unsigned long flags;
921
Mike Christie632248a2009-08-20 15:11:01 -0500922 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
Mike Christie45ab33b2008-03-04 13:26:55 -0600923 /*
924 * The recovery and unblock work get run from the same workqueue,
925 * so try to cancel it if it was going to run after this unblock.
926 */
927 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600928 spin_lock_irqsave(&session->lock, flags);
929 session->state = ISCSI_SESSION_LOGGED_IN;
930 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600931 /* start IO */
932 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600933 /*
934 * Only do kernel scanning if the driver is properly hooked into
935 * the async scanning code (drivers like iscsi_tcp do login and
936 * scanning from userspace).
937 */
938 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -0600939 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -0600940 atomic_inc(&ihost->nr_scans);
941 }
Mike Christie632248a2009-08-20 15:11:01 -0500942 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600943}
Mike Christie45ab33b2008-03-04 13:26:55 -0600944
945/**
946 * iscsi_unblock_session - set a session as logged in and start IO.
947 * @session: iscsi session
948 *
949 * Mark a session as ready to accept IO.
950 */
951void iscsi_unblock_session(struct iscsi_cls_session *session)
952{
953 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
954 /*
955 * make sure all the events have completed before tell the driver
956 * it is safe
957 */
958 flush_workqueue(iscsi_eh_timer_workq);
959}
Mike Christie30a6c652006-04-06 21:13:39 -0500960EXPORT_SYMBOL_GPL(iscsi_unblock_session);
961
Mike Christie45ab33b2008-03-04 13:26:55 -0600962static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500963{
Mike Christie45ab33b2008-03-04 13:26:55 -0600964 struct iscsi_cls_session *session =
965 container_of(work, struct iscsi_cls_session,
966 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600967 unsigned long flags;
968
Mike Christie632248a2009-08-20 15:11:01 -0500969 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600970 spin_lock_irqsave(&session->lock, flags);
971 session->state = ISCSI_SESSION_FAILED;
972 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500973 scsi_target_block(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500974 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
Mike Christiefdd46dc2009-11-11 16:34:34 -0600975 if (session->recovery_tmo >= 0)
976 queue_delayed_work(iscsi_eh_timer_workq,
977 &session->recovery_work,
978 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500979}
Mike Christie45ab33b2008-03-04 13:26:55 -0600980
981void iscsi_block_session(struct iscsi_cls_session *session)
982{
983 queue_work(iscsi_eh_timer_workq, &session->block_work);
984}
Mike Christie30a6c652006-04-06 21:13:39 -0500985EXPORT_SYMBOL_GPL(iscsi_block_session);
986
Mike Christie26974782007-12-13 12:43:29 -0600987static void __iscsi_unbind_session(struct work_struct *work)
988{
989 struct iscsi_cls_session *session =
990 container_of(work, struct iscsi_cls_session,
991 unbind_work);
992 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500993 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500994 unsigned long flags;
Mike Christie8d4a6902011-10-06 03:56:57 -0500995 unsigned int target_id;
Mike Christie26974782007-12-13 12:43:29 -0600996
Mike Christie632248a2009-08-20 15:11:01 -0500997 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
998
Mike Christie26974782007-12-13 12:43:29 -0600999 /* Prevent new scans and make sure scanning is not in progress */
1000 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -05001001 spin_lock_irqsave(&session->lock, flags);
1002 if (session->target_id == ISCSI_MAX_TARGET) {
1003 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001004 mutex_unlock(&ihost->mutex);
1005 return;
1006 }
Mike Christie8d4a6902011-10-06 03:56:57 -05001007
1008 target_id = session->target_id;
Mike Christie79706342008-05-21 15:54:12 -05001009 session->target_id = ISCSI_MAX_TARGET;
1010 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001011 mutex_unlock(&ihost->mutex);
1012
Mike Christie8d4a6902011-10-06 03:56:57 -05001013 if (session->ida_used)
1014 ida_simple_remove(&iscsi_sess_ida, target_id);
1015
Mike Christie26974782007-12-13 12:43:29 -06001016 scsi_remove_target(&session->dev);
1017 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -05001018 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
Mike Christie26974782007-12-13 12:43:29 -06001019}
1020
Mike Christie7b8631b2006-01-13 18:05:50 -06001021struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -05001022iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1023 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06001024{
1025 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Mike Christie5d91e202008-05-21 15:54:01 -05001027 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -05001028 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001029 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -05001030 return NULL;
1031
Mike Christie7b8631b2006-01-13 18:05:50 -06001032 session->transport = transport;
Mike Christie0c70d842011-12-05 16:44:01 -06001033 session->creator = -1;
Mike Christie30a6c652006-04-06 21:13:39 -05001034 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -06001035 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +00001036 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -05001037 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -06001038 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
1039 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -06001040 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -06001041 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001042 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -06001043
Mike Christie6a8a0d32006-06-28 12:00:31 -05001044 /* this is released in the dev's release function */
1045 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -05001046 session->dev.parent = &shost->shost_gendev;
1047 session->dev.release = iscsi_session_release;
1048 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -05001049 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -05001050 session->dd_data = &session[1];
Mike Christie632248a2009-08-20 15:11:01 -05001051
1052 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001053 return session;
1054}
1055EXPORT_SYMBOL_GPL(iscsi_alloc_session);
1056
Mike Christie6a8a0d32006-06-28 12:00:31 -05001057int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05001058{
1059 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001060 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -06001061 unsigned long flags;
Mike Christie8d4a6902011-10-06 03:56:57 -05001062 int id = 0;
Mike Christie8434aa82006-06-28 12:00:30 -05001063 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -05001064
Mike Christie30a6c652006-04-06 21:13:39 -05001065 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -06001066 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -05001067
Mike Christie8d4a6902011-10-06 03:56:57 -05001068 if (target_id == ISCSI_MAX_TARGET) {
1069 id = ida_simple_get(&iscsi_sess_ida, 0, 0, GFP_KERNEL);
Mike Christie79706342008-05-21 15:54:12 -05001070
Mike Christie8d4a6902011-10-06 03:56:57 -05001071 if (id < 0) {
Mike Christie79706342008-05-21 15:54:12 -05001072 iscsi_cls_session_printk(KERN_ERR, session,
Mike Christie8d4a6902011-10-06 03:56:57 -05001073 "Failure in Target ID Allocation\n");
1074 return id;
Mike Christie79706342008-05-21 15:54:12 -05001075 }
Mike Christie8d4a6902011-10-06 03:56:57 -05001076 session->target_id = (unsigned int)id;
1077 session->ida_used = true;
1078 } else
1079 session->target_id = target_id;
Mike Christie30a6c652006-04-06 21:13:39 -05001080
Kay Sievers71610f52008-12-03 22:41:36 +01001081 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -05001082 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001083 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001084 iscsi_cls_session_printk(KERN_ERR, session,
1085 "could not register session's dev\n");
Mike Christie8d4a6902011-10-06 03:56:57 -05001086 goto release_ida;
Mike Christie7b8631b2006-01-13 18:05:50 -06001087 }
1088 transport_register_device(&session->dev);
1089
Mike Christie26974782007-12-13 12:43:29 -06001090 spin_lock_irqsave(&sesslock, flags);
1091 list_add(&session->sess_list, &sesslist);
1092 spin_unlock_irqrestore(&sesslock, flags);
1093
Mike Christie26974782007-12-13 12:43:29 -06001094 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -05001095 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001096 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -05001097
Mike Christie8d4a6902011-10-06 03:56:57 -05001098release_ida:
1099 if (session->ida_used)
1100 ida_simple_remove(&iscsi_sess_ida, session->target_id);
1101
Mike Christie8434aa82006-06-28 12:00:30 -05001102 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -06001103}
Mike Christie8434aa82006-06-28 12:00:30 -05001104EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -06001105
1106/**
Mike Christie8434aa82006-06-28 12:00:30 -05001107 * iscsi_create_session - create iscsi class session
1108 * @shost: scsi host
1109 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -05001110 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -05001111 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -06001112 *
Mike Christie8434aa82006-06-28 12:00:30 -05001113 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001114 */
Mike Christie8434aa82006-06-28 12:00:30 -05001115struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -05001116iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1117 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05001118{
1119 struct iscsi_cls_session *session;
1120
Mike Christie5d91e202008-05-21 15:54:01 -05001121 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -05001122 if (!session)
1123 return NULL;
1124
Mike Christie6a8a0d32006-06-28 12:00:31 -05001125 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -05001126 iscsi_free_session(session);
1127 return NULL;
1128 }
1129 return session;
1130}
1131EXPORT_SYMBOL_GPL(iscsi_create_session);
1132
Mike Christie26974782007-12-13 12:43:29 -06001133static void iscsi_conn_release(struct device *dev)
1134{
1135 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
1136 struct device *parent = conn->dev.parent;
1137
Mike Christie632248a2009-08-20 15:11:01 -05001138 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
Mike Christie26974782007-12-13 12:43:29 -06001139 kfree(conn);
1140 put_device(parent);
1141}
1142
1143static int iscsi_is_conn_dev(const struct device *dev)
1144{
1145 return dev->release == iscsi_conn_release;
1146}
1147
1148static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
1149{
1150 if (!iscsi_is_conn_dev(dev))
1151 return 0;
1152 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
1153}
1154
Mike Christie8434aa82006-06-28 12:00:30 -05001155void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001156{
Mike Christie30a6c652006-04-06 21:13:39 -05001157 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie26974782007-12-13 12:43:29 -06001158 unsigned long flags;
1159 int err;
Mike Christie30a6c652006-04-06 21:13:39 -05001160
Mike Christie632248a2009-08-20 15:11:01 -05001161 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
1162
Mike Christie26974782007-12-13 12:43:29 -06001163 spin_lock_irqsave(&sesslock, flags);
1164 list_del(&session->sess_list);
1165 spin_unlock_irqrestore(&sesslock, flags);
1166
Mike Christie45ab33b2008-03-04 13:26:55 -06001167 /* make sure there are no blocks/unblocks queued */
1168 flush_workqueue(iscsi_eh_timer_workq);
1169 /* make sure the timedout callout is not running */
1170 if (!cancel_delayed_work(&session->recovery_work))
1171 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -06001172 /*
1173 * If we are blocked let commands flow again. The lld or iscsi
1174 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -06001175 * We assume that LLD will not be calling block/unblock while
1176 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -06001177 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001178 spin_lock_irqsave(&session->lock, flags);
1179 session->state = ISCSI_SESSION_FREE;
1180 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -06001181
Mike Christie45ab33b2008-03-04 13:26:55 -06001182 scsi_target_unblock(&session->dev);
1183 /* flush running scans then delete devices */
Mike Christie06d25af2009-03-05 14:46:02 -06001184 scsi_flush_work(shost);
Mike Christie45ab33b2008-03-04 13:26:55 -06001185 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -05001186
Mike Christie26974782007-12-13 12:43:29 -06001187 /* hw iscsi may not have removed all connections from session */
1188 err = device_for_each_child(&session->dev, NULL,
1189 iscsi_iter_destroy_conn_fn);
1190 if (err)
Mike Christie322d7392008-01-31 13:36:52 -06001191 iscsi_cls_session_printk(KERN_ERR, session,
1192 "Could not delete all connections "
1193 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -05001194
Mike Christie7b8631b2006-01-13 18:05:50 -06001195 transport_unregister_device(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001196
1197 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001198 device_del(&session->dev);
1199}
1200EXPORT_SYMBOL_GPL(iscsi_remove_session);
1201
1202void iscsi_free_session(struct iscsi_cls_session *session)
1203{
Mike Christie632248a2009-08-20 15:11:01 -05001204 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
Mike Christie26974782007-12-13 12:43:29 -06001205 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -05001206 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001207}
Mike Christie8434aa82006-06-28 12:00:30 -05001208EXPORT_SYMBOL_GPL(iscsi_free_session);
1209
1210/**
1211 * iscsi_destroy_session - destroy iscsi session
1212 * @session: iscsi_session
1213 *
1214 * Can be called by a LLD or iscsi_transport. There must not be
1215 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -05001216 */
Mike Christie8434aa82006-06-28 12:00:30 -05001217int iscsi_destroy_session(struct iscsi_cls_session *session)
1218{
1219 iscsi_remove_session(session);
Mike Christie632248a2009-08-20 15:11:01 -05001220 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001221 iscsi_free_session(session);
1222 return 0;
1223}
Mike Christie7b8631b2006-01-13 18:05:50 -06001224EXPORT_SYMBOL_GPL(iscsi_destroy_session);
1225
Mike Christie7b8631b2006-01-13 18:05:50 -06001226/**
1227 * iscsi_create_conn - create iscsi class connection
1228 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -05001229 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -06001230 * @cid: connection id
1231 *
1232 * This can be called from a LLD or iscsi_transport. The connection
1233 * is child of the session so cid must be unique for all connections
1234 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -05001235 *
1236 * Since we do not support MCS, cid will normally be zero. In some cases
1237 * for software iscsi we could be trying to preallocate a connection struct
1238 * in which case there could be two connection structs and cid would be
1239 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -05001240 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001241struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05001242iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -06001243{
1244 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -06001245 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -06001246 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -06001247 int err;
1248
Mike Christie5d91e202008-05-21 15:54:01 -05001249 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001250 if (!conn)
1251 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -05001252 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06001253 conn->dd_data = &conn[1];
1254
Mike Christie22a39fb2011-02-16 15:04:33 -06001255 mutex_init(&conn->ep_mutex);
Mike Christie7b8631b2006-01-13 18:05:50 -06001256 INIT_LIST_HEAD(&conn->conn_list);
1257 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -05001258 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001259
1260 /* this is released in the dev's release function */
1261 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -04001262 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -05001263
Kay Sievers71610f52008-12-03 22:41:36 +01001264 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001265 conn->dev.parent = &session->dev;
1266 conn->dev.release = iscsi_conn_release;
1267 err = device_register(&conn->dev);
1268 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001269 iscsi_cls_session_printk(KERN_ERR, session, "could not "
1270 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001271 goto release_parent_ref;
1272 }
1273 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -06001274
1275 spin_lock_irqsave(&connlock, flags);
1276 list_add(&conn->conn_list, &connlist);
Mike Christie26974782007-12-13 12:43:29 -06001277 spin_unlock_irqrestore(&connlock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05001278
1279 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001280 return conn;
1281
1282release_parent_ref:
1283 put_device(&session->dev);
1284free_conn:
1285 kfree(conn);
1286 return NULL;
1287}
1288
1289EXPORT_SYMBOL_GPL(iscsi_create_conn);
1290
1291/**
1292 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -05001293 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -06001294 *
Mike Christie26974782007-12-13 12:43:29 -06001295 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001296 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001297int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1298{
Mike Christie26974782007-12-13 12:43:29 -06001299 unsigned long flags;
1300
1301 spin_lock_irqsave(&connlock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001302 list_del(&conn->conn_list);
1303 spin_unlock_irqrestore(&connlock, flags);
1304
Mike Christie7b8631b2006-01-13 18:05:50 -06001305 transport_unregister_device(&conn->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001306 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001307 device_unregister(&conn->dev);
1308 return 0;
1309}
Mike Christie7b8631b2006-01-13 18:05:50 -06001310EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1311
1312/*
Mike Christie7b8631b2006-01-13 18:05:50 -06001313 * iscsi interface functions
1314 */
Alex Aizman0896b752005-08-04 19:33:07 -07001315static struct iscsi_internal *
1316iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Alex Aizman0896b752005-08-04 19:33:07 -07001318 struct iscsi_internal *priv;
1319 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Alex Aizman0896b752005-08-04 19:33:07 -07001321 spin_lock_irqsave(&iscsi_transport_lock, flags);
1322 list_for_each_entry(priv, &iscsi_transports, list) {
1323 if (tt == priv->iscsi_transport) {
1324 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1325 return priv;
1326 }
1327 }
1328 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1329 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Alex Aizman0896b752005-08-04 19:33:07 -07001332static int
Michael Chan43514772009-06-08 18:14:41 -07001333iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -05001334{
Michael Chan43514772009-06-08 18:14:41 -07001335 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -07001336}
1337
Mike Christie7b7232f2006-02-01 21:06:49 -06001338int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -07001339 char *data, uint32_t data_size)
1340{
1341 struct nlmsghdr *nlh;
1342 struct sk_buff *skb;
1343 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07001344 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05001345 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001346 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1347 data_size);
1348
Mike Christie790f39a2006-05-18 20:31:39 -05001349 priv = iscsi_if_transport_lookup(conn->transport);
1350 if (!priv)
1351 return -EINVAL;
1352
Mike Christie43a145a2006-10-16 18:09:38 -04001353 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001354 if (!skb) {
Mike Christiee5bd7b52008-09-24 11:46:10 -05001355 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06001356 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1357 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001358 return -ENOMEM;
1359 }
1360
Michael Chan43514772009-06-08 18:14:41 -07001361 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001362 ev = NLMSG_DATA(nlh);
1363 memset(ev, 0, sizeof(*ev));
1364 ev->transport_handle = iscsi_handle(conn->transport);
1365 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05001366 ev->r.recv_req.cid = conn->cid;
1367 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001368 pdu = (char*)ev + sizeof(*ev);
1369 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1370 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1371
Michael Chan43514772009-06-08 18:14:41 -07001372 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001373}
1374EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1375
Michael Chan43514772009-06-08 18:14:41 -07001376int iscsi_offload_mesg(struct Scsi_Host *shost,
1377 struct iscsi_transport *transport, uint32_t type,
1378 char *data, uint16_t data_size)
1379{
1380 struct nlmsghdr *nlh;
1381 struct sk_buff *skb;
1382 struct iscsi_uevent *ev;
1383 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
1384
Michael Chana541f842009-07-29 08:49:52 +00001385 skb = alloc_skb(len, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001386 if (!skb) {
1387 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
1388 return -ENOMEM;
1389 }
1390
1391 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1392 ev = NLMSG_DATA(nlh);
1393 memset(ev, 0, sizeof(*ev));
1394 ev->type = type;
1395 ev->transport_handle = iscsi_handle(transport);
1396 switch (type) {
1397 case ISCSI_KEVENT_PATH_REQ:
1398 ev->r.req_path.host_no = shost->host_no;
1399 break;
1400 case ISCSI_KEVENT_IF_DOWN:
1401 ev->r.notify_if_down.host_no = shost->host_no;
1402 break;
1403 }
1404
1405 memcpy((char *)ev + sizeof(*ev), data, data_size);
1406
Michael Chana541f842009-07-29 08:49:52 +00001407 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001408}
1409EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1410
Mike Christiee5bd7b52008-09-24 11:46:10 -05001411void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07001412{
1413 struct nlmsghdr *nlh;
1414 struct sk_buff *skb;
1415 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05001416 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001417 int len = NLMSG_SPACE(sizeof(*ev));
1418
Mike Christie790f39a2006-05-18 20:31:39 -05001419 priv = iscsi_if_transport_lookup(conn->transport);
1420 if (!priv)
1421 return;
1422
Mike Christie43a145a2006-10-16 18:09:38 -04001423 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001424 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001425 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1426 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07001427 return;
1428 }
1429
Michael Chan43514772009-06-08 18:14:41 -07001430 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001431 ev = NLMSG_DATA(nlh);
1432 ev->transport_handle = iscsi_handle(conn->transport);
1433 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07001434 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05001435 ev->r.connerror.cid = conn->cid;
1436 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001437
Michael Chan43514772009-06-08 18:14:41 -07001438 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001439
Mike Christie322d7392008-01-31 13:36:52 -06001440 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1441 error);
Alex Aizman0896b752005-08-04 19:33:07 -07001442}
Mike Christiee5bd7b52008-09-24 11:46:10 -05001443EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07001444
Manish Rangankar17fa5752011-07-25 13:48:52 -05001445void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
1446 enum iscsi_conn_state state)
1447{
1448 struct nlmsghdr *nlh;
1449 struct sk_buff *skb;
1450 struct iscsi_uevent *ev;
1451 struct iscsi_internal *priv;
1452 int len = NLMSG_SPACE(sizeof(*ev));
1453
1454 priv = iscsi_if_transport_lookup(conn->transport);
1455 if (!priv)
1456 return;
1457
1458 skb = alloc_skb(len, GFP_ATOMIC);
1459 if (!skb) {
1460 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1461 "conn login (%d)\n", state);
1462 return;
1463 }
1464
1465 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1466 ev = NLMSG_DATA(nlh);
1467 ev->transport_handle = iscsi_handle(conn->transport);
1468 ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
1469 ev->r.conn_login.state = state;
1470 ev->r.conn_login.cid = conn->cid;
1471 ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
1472 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
1473
1474 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
1475 state);
1476}
1477EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
1478
Alex Aizman0896b752005-08-04 19:33:07 -07001479static int
Michael Chan43514772009-06-08 18:14:41 -07001480iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1481 void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07001482{
1483 struct sk_buff *skb;
1484 struct nlmsghdr *nlh;
1485 int len = NLMSG_SPACE(size);
1486 int flags = multi ? NLM_F_MULTI : 0;
1487 int t = done ? NLMSG_DONE : type;
1488
Mike Christie43a145a2006-10-16 18:09:38 -04001489 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05001490 if (!skb) {
1491 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1492 return -ENOMEM;
1493 }
Alex Aizman0896b752005-08-04 19:33:07 -07001494
Michael Chan43514772009-06-08 18:14:41 -07001495 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001496 nlh->nlmsg_flags = flags;
1497 memcpy(NLMSG_DATA(nlh), payload, size);
Michael Chan43514772009-06-08 18:14:41 -07001498 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001499}
1500
1501static int
Mike Christie5b940ad2006-02-01 21:06:56 -06001502iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07001503{
1504 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1505 struct iscsi_stats *stats;
1506 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06001507 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001508 struct nlmsghdr *nlhstat;
1509 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05001510 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001511 int len = NLMSG_SPACE(sizeof(*ev) +
1512 sizeof(struct iscsi_stats) +
1513 sizeof(struct iscsi_stats_custom) *
1514 ISCSI_STATS_CUSTOM_MAX);
1515 int err = 0;
1516
Mike Christie790f39a2006-05-18 20:31:39 -05001517 priv = iscsi_if_transport_lookup(transport);
1518 if (!priv)
1519 return -EINVAL;
1520
Mike Christieb5c7a122006-04-06 21:13:33 -05001521 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07001522 if (!conn)
1523 return -EEXIST;
1524
1525 do {
1526 int actual_size;
1527
Mike Christie43a145a2006-10-16 18:09:38 -04001528 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001529 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06001530 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1531 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001532 return -ENOMEM;
1533 }
1534
Michael Chan43514772009-06-08 18:14:41 -07001535 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07001536 (len - sizeof(*nlhstat)), 0);
1537 evstat = NLMSG_DATA(nlhstat);
1538 memset(evstat, 0, sizeof(*evstat));
1539 evstat->transport_handle = iscsi_handle(conn->transport);
1540 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05001541 evstat->u.get_stats.cid =
1542 ev->u.get_stats.cid;
1543 evstat->u.get_stats.sid =
1544 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07001545 stats = (struct iscsi_stats *)
1546 ((char*)evstat + sizeof(*evstat));
1547 memset(stats, 0, sizeof(*stats));
1548
Mike Christie7b7232f2006-02-01 21:06:49 -06001549 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07001550 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1551 sizeof(struct iscsi_stats) +
1552 sizeof(struct iscsi_stats_custom) *
1553 stats->custom_length);
1554 actual_size -= sizeof(*nlhstat);
1555 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06001556 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07001557 nlhstat->nlmsg_len = actual_size;
1558
Michael Chan43514772009-06-08 18:14:41 -07001559 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1560 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001561 } while (err < 0 && err != -ECONNREFUSED);
1562
1563 return err;
1564}
1565
Mike Christie53cb8a12006-06-28 12:00:32 -05001566/**
Mike Christie26974782007-12-13 12:43:29 -06001567 * iscsi_session_event - send session destr. completion event
1568 * @session: iscsi class session
1569 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05001570 */
Mike Christie26974782007-12-13 12:43:29 -06001571int iscsi_session_event(struct iscsi_cls_session *session,
1572 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05001573{
1574 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05001575 struct Scsi_Host *shost;
1576 struct iscsi_uevent *ev;
1577 struct sk_buff *skb;
1578 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05001579 int rc, len = NLMSG_SPACE(sizeof(*ev));
1580
Mike Christie26974782007-12-13 12:43:29 -06001581 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05001582 if (!priv)
1583 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05001584 shost = iscsi_session_to_shost(session);
1585
Mike Christie43a145a2006-10-16 18:09:38 -04001586 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001587 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001588 iscsi_cls_session_printk(KERN_ERR, session,
1589 "Cannot notify userspace of session "
1590 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001591 return -ENOMEM;
1592 }
1593
Michael Chan43514772009-06-08 18:14:41 -07001594 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Mike Christie53cb8a12006-06-28 12:00:32 -05001595 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06001596 ev->transport_handle = iscsi_handle(session->transport);
1597
1598 ev->type = event;
1599 switch (event) {
1600 case ISCSI_KEVENT_DESTROY_SESSION:
1601 ev->r.d_session.host_no = shost->host_no;
1602 ev->r.d_session.sid = session->sid;
1603 break;
1604 case ISCSI_KEVENT_CREATE_SESSION:
1605 ev->r.c_session_ret.host_no = shost->host_no;
1606 ev->r.c_session_ret.sid = session->sid;
1607 break;
1608 case ISCSI_KEVENT_UNBIND_SESSION:
1609 ev->r.unbind_session.host_no = shost->host_no;
1610 ev->r.unbind_session.sid = session->sid;
1611 break;
1612 default:
Mike Christie322d7392008-01-31 13:36:52 -06001613 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1614 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06001615 kfree_skb(skb);
1616 return -EINVAL;
1617 }
Mike Christie53cb8a12006-06-28 12:00:32 -05001618
1619 /*
1620 * this will occur if the daemon is not up, so we just warn
1621 * the user and when the daemon is restarted it will handle it
1622 */
Michael Chan43514772009-06-08 18:14:41 -07001623 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001624 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06001625 iscsi_cls_session_printk(KERN_ERR, session,
1626 "Cannot notify userspace of session "
1627 "event %u. Check iscsi daemon\n",
1628 event);
Mike Christie632248a2009-08-20 15:11:01 -05001629
1630 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
1631 event, rc);
Mike Christie53cb8a12006-06-28 12:00:32 -05001632 return rc;
1633}
Mike Christie26974782007-12-13 12:43:29 -06001634EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001635
Alex Aizman0896b752005-08-04 19:33:07 -07001636static int
Mike Christied82ff9be2008-05-21 15:54:13 -05001637iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
Mike Christie0c70d842011-12-05 16:44:01 -06001638 struct iscsi_uevent *ev, pid_t pid,
1639 uint32_t initial_cmdsn, uint16_t cmds_max,
1640 uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001641{
1642 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001643 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06001644 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06001645
Mike Christied82ff9be2008-05-21 15:54:13 -05001646 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06001647 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06001648 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001649 return -ENOMEM;
1650
Mike Christie0c70d842011-12-05 16:44:01 -06001651 session->creator = pid;
Mike Christie5e7facb2009-03-05 14:46:06 -06001652 shost = iscsi_session_to_shost(session);
1653 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001654 ev->r.c_session_ret.sid = session->sid;
Mike Christie632248a2009-08-20 15:11:01 -05001655 ISCSI_DBG_TRANS_SESSION(session,
1656 "Completed creating transport session\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001657 return 0;
1658}
1659
1660static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001661iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001662{
Mike Christie7b8631b2006-01-13 18:05:50 -06001663 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001664 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001665
Mike Christieb5c7a122006-04-06 21:13:33 -05001666 session = iscsi_session_lookup(ev->u.c_conn.sid);
1667 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001668 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001669 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001670 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001671 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001672
Mike Christie7b7232f2006-02-01 21:06:49 -06001673 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001674 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001675 iscsi_cls_session_printk(KERN_ERR, session,
1676 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001677 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001678 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001679
Mike Christieb5c7a122006-04-06 21:13:33 -05001680 ev->r.c_conn_ret.sid = session->sid;
1681 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie632248a2009-08-20 15:11:01 -05001682
1683 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001684 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001685}
1686
1687static int
1688iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1689{
Mike Christie7b8631b2006-01-13 18:05:50 -06001690 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001691
Mike Christieb5c7a122006-04-06 21:13:33 -05001692 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001693 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001694 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001695
Mike Christie632248a2009-08-20 15:11:01 -05001696 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001697 if (transport->destroy_conn)
1698 transport->destroy_conn(conn);
Mike Christie632248a2009-08-20 15:11:01 -05001699
Mike Christie7b8631b2006-01-13 18:05:50 -06001700 return 0;
1701}
1702
Mike Christiefd7255f2006-04-06 21:13:36 -05001703static int
1704iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1705{
1706 char *data = (char*)ev + sizeof(*ev);
1707 struct iscsi_cls_conn *conn;
1708 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001709 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001710
1711 session = iscsi_session_lookup(ev->u.set_param.sid);
1712 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1713 if (!conn || !session)
1714 return -EINVAL;
1715
1716 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001717 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001718 sscanf(data, "%d", &value);
Mike Christiefdd46dc2009-11-11 16:34:34 -06001719 session->recovery_tmo = value;
Mike Christie30a6c652006-04-06 21:13:39 -05001720 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001721 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001722 err = transport->set_param(conn, ev->u.set_param.param,
1723 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001724 }
1725
1726 return err;
1727}
1728
Mike Christie10eb0f02009-05-13 17:57:38 -05001729static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1730 struct iscsi_uevent *ev, int msg_type)
1731{
1732 struct iscsi_endpoint *ep;
1733 struct sockaddr *dst_addr;
1734 struct Scsi_Host *shost = NULL;
1735 int non_blocking, err = 0;
1736
1737 if (!transport->ep_connect)
1738 return -EINVAL;
1739
1740 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1741 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1742 if (!shost) {
1743 printk(KERN_ERR "ep connect failed. Could not find "
1744 "host no %u\n",
1745 ev->u.ep_connect_through_host.host_no);
1746 return -ENODEV;
1747 }
1748 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1749 } else
1750 non_blocking = ev->u.ep_connect.non_blocking;
1751
1752 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1753 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1754 if (IS_ERR(ep)) {
1755 err = PTR_ERR(ep);
1756 goto release_host;
1757 }
1758
1759 ev->r.ep_connect_ret.handle = ep->id;
1760release_host:
1761 if (shost)
1762 scsi_host_put(shost);
1763 return err;
1764}
1765
Mike Christie22a39fb2011-02-16 15:04:33 -06001766static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
1767 u64 ep_handle)
1768{
1769 struct iscsi_cls_conn *conn;
1770 struct iscsi_endpoint *ep;
1771
1772 if (!transport->ep_disconnect)
1773 return -EINVAL;
1774
1775 ep = iscsi_lookup_endpoint(ep_handle);
1776 if (!ep)
1777 return -EINVAL;
1778 conn = ep->conn;
1779 if (conn) {
1780 mutex_lock(&conn->ep_mutex);
1781 conn->ep = NULL;
1782 mutex_unlock(&conn->ep_mutex);
1783 }
1784
1785 transport->ep_disconnect(ep);
1786 return 0;
1787}
1788
Mike Christie7b8631b2006-01-13 18:05:50 -06001789static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001790iscsi_if_transport_ep(struct iscsi_transport *transport,
1791 struct iscsi_uevent *ev, int msg_type)
1792{
Mike Christied82ff9be2008-05-21 15:54:13 -05001793 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001794 int rc = 0;
1795
1796 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05001797 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001798 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001799 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001800 break;
1801 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1802 if (!transport->ep_poll)
1803 return -EINVAL;
1804
Mike Christied82ff9be2008-05-21 15:54:13 -05001805 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1806 if (!ep)
1807 return -EINVAL;
1808
1809 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001810 ev->u.ep_poll.timeout_ms);
1811 break;
1812 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie22a39fb2011-02-16 15:04:33 -06001813 rc = iscsi_if_ep_disconnect(transport,
1814 ev->u.ep_disconnect.ep_handle);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001815 break;
1816 }
1817 return rc;
1818}
1819
1820static int
Mike Christie01cb2252006-06-28 12:00:22 -05001821iscsi_tgt_dscvr(struct iscsi_transport *transport,
1822 struct iscsi_uevent *ev)
1823{
Mike Christie2174a042007-05-30 12:57:10 -05001824 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001825 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001826 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001827
1828 if (!transport->tgt_dscvr)
1829 return -EINVAL;
1830
Mike Christie2174a042007-05-30 12:57:10 -05001831 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001832 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05001833 printk(KERN_ERR "target discovery could not find host no %u\n",
1834 ev->u.tgt_dscvr.host_no);
1835 return -ENODEV;
1836 }
1837
1838
Mike Christie01cb2252006-06-28 12:00:22 -05001839 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001840 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1841 ev->u.tgt_dscvr.enable, dst_addr);
1842 scsi_host_put(shost);
1843 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001844}
1845
1846static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001847iscsi_set_host_param(struct iscsi_transport *transport,
1848 struct iscsi_uevent *ev)
1849{
1850 char *data = (char*)ev + sizeof(*ev);
1851 struct Scsi_Host *shost;
1852 int err;
1853
1854 if (!transport->set_host_param)
1855 return -ENOSYS;
1856
1857 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001858 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05001859 printk(KERN_ERR "set_host_param could not find host no %u\n",
1860 ev->u.set_host_param.host_no);
1861 return -ENODEV;
1862 }
1863
1864 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1865 data, ev->u.set_host_param.len);
1866 scsi_host_put(shost);
1867 return err;
1868}
1869
1870static int
Michael Chan43514772009-06-08 18:14:41 -07001871iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1872{
1873 struct Scsi_Host *shost;
1874 struct iscsi_path *params;
1875 int err;
1876
1877 if (!transport->set_path)
1878 return -ENOSYS;
1879
1880 shost = scsi_host_lookup(ev->u.set_path.host_no);
1881 if (!shost) {
1882 printk(KERN_ERR "set path could not find host no %u\n",
1883 ev->u.set_path.host_no);
1884 return -ENODEV;
1885 }
1886
1887 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1888 err = transport->set_path(shost, params);
1889
1890 scsi_host_put(shost);
1891 return err;
1892}
1893
1894static int
Mike Christie56c155b2011-07-25 13:48:37 -05001895iscsi_set_iface_params(struct iscsi_transport *transport,
Mike Christie00c31882011-10-06 03:56:59 -05001896 struct iscsi_uevent *ev, uint32_t len)
Mike Christie56c155b2011-07-25 13:48:37 -05001897{
1898 char *data = (char *)ev + sizeof(*ev);
1899 struct Scsi_Host *shost;
1900 int err;
1901
1902 if (!transport->set_iface_param)
1903 return -ENOSYS;
1904
1905 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
1906 if (!shost) {
1907 printk(KERN_ERR "set_iface_params could not find host no %u\n",
1908 ev->u.set_iface_params.host_no);
1909 return -ENODEV;
1910 }
1911
Mike Christie00c31882011-10-06 03:56:59 -05001912 err = transport->set_iface_param(shost, data, len);
Mike Christie56c155b2011-07-25 13:48:37 -05001913 scsi_host_put(shost);
1914 return err;
1915}
1916
1917static int
Michael Chan43514772009-06-08 18:14:41 -07001918iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07001919{
1920 int err = 0;
1921 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1922 struct iscsi_transport *transport = NULL;
1923 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001924 struct iscsi_cls_session *session;
1925 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05001926 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001927
Michael Chan43514772009-06-08 18:14:41 -07001928 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1929 *group = ISCSI_NL_GRP_UIP;
1930 else
1931 *group = ISCSI_NL_GRP_ISCSID;
1932
Alex Aizman0896b752005-08-04 19:33:07 -07001933 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1934 if (!priv)
1935 return -EINVAL;
1936 transport = priv->iscsi_transport;
1937
Mike Christie7b7232f2006-02-01 21:06:49 -06001938 if (!try_module_get(transport->owner))
1939 return -EINVAL;
1940
Alex Aizman0896b752005-08-04 19:33:07 -07001941 switch (nlh->nlmsg_type) {
1942 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001943 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie0c70d842011-12-05 16:44:01 -06001944 NETLINK_CREDS(skb)->pid,
Mike Christie40753ca2008-05-21 15:53:56 -05001945 ev->u.c_session.initial_cmdsn,
1946 ev->u.c_session.cmds_max,
1947 ev->u.c_session.queue_depth);
1948 break;
1949 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001950 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05001951 if (!ep) {
1952 err = -EINVAL;
1953 break;
1954 }
Mike Christied82ff9be2008-05-21 15:54:13 -05001955
1956 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie0c70d842011-12-05 16:44:01 -06001957 NETLINK_CREDS(skb)->pid,
Mike Christie40753ca2008-05-21 15:53:56 -05001958 ev->u.c_bound_session.initial_cmdsn,
1959 ev->u.c_bound_session.cmds_max,
1960 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001961 break;
1962 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001963 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001964 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001965 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001966 else
1967 err = -EINVAL;
1968 break;
1969 case ISCSI_UEVENT_UNBIND_SESSION:
1970 session = iscsi_session_lookup(ev->u.d_session.sid);
1971 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06001972 scsi_queue_work(iscsi_session_to_shost(session),
1973 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06001974 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001975 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001976 break;
1977 case ISCSI_UEVENT_CREATE_CONN:
1978 err = iscsi_if_create_conn(transport, ev);
1979 break;
1980 case ISCSI_UEVENT_DESTROY_CONN:
1981 err = iscsi_if_destroy_conn(transport, ev);
1982 break;
1983 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001984 session = iscsi_session_lookup(ev->u.b_conn.sid);
1985 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001986
Mike Christie22a39fb2011-02-16 15:04:33 -06001987 if (conn && conn->ep)
1988 iscsi_if_ep_disconnect(transport, conn->ep->id);
1989
1990 if (!session || !conn) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001991 err = -EINVAL;
Mike Christie22a39fb2011-02-16 15:04:33 -06001992 break;
1993 }
1994
1995 ev->r.retcode = transport->bind_conn(session, conn,
1996 ev->u.b_conn.transport_eph,
1997 ev->u.b_conn.is_leading);
1998 if (ev->r.retcode || !transport->ep_connect)
1999 break;
2000
2001 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
2002 if (ep) {
2003 ep->conn = conn;
2004
2005 mutex_lock(&conn->ep_mutex);
2006 conn->ep = ep;
2007 mutex_unlock(&conn->ep_mutex);
2008 } else
2009 iscsi_cls_conn_printk(KERN_ERR, conn,
2010 "Could not set ep conn "
2011 "binding\n");
Alex Aizman0896b752005-08-04 19:33:07 -07002012 break;
2013 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05002014 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07002015 break;
2016 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05002017 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06002018 if (conn)
2019 ev->r.retcode = transport->start_conn(conn);
2020 else
2021 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07002022 break;
2023 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05002024 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06002025 if (conn)
2026 transport->stop_conn(conn, ev->u.stop_conn.flag);
2027 else
2028 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07002029 break;
2030 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05002031 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06002032 if (conn)
2033 ev->r.retcode = transport->send_pdu(conn,
2034 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
2035 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
2036 ev->u.send_pdu.data_size);
2037 else
2038 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07002039 break;
2040 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06002041 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07002042 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05002043 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
2044 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
2045 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05002046 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05002047 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
2048 break;
Mike Christie01cb2252006-06-28 12:00:22 -05002049 case ISCSI_UEVENT_TGT_DSCVR:
2050 err = iscsi_tgt_dscvr(transport, ev);
2051 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05002052 case ISCSI_UEVENT_SET_HOST_PARAM:
2053 err = iscsi_set_host_param(transport, ev);
2054 break;
Michael Chan43514772009-06-08 18:14:41 -07002055 case ISCSI_UEVENT_PATH_UPDATE:
2056 err = iscsi_set_path(transport, ev);
2057 break;
Mike Christie56c155b2011-07-25 13:48:37 -05002058 case ISCSI_UEVENT_SET_IFACE_PARAMS:
Mike Christie00c31882011-10-06 03:56:59 -05002059 err = iscsi_set_iface_params(transport, ev,
2060 nlmsg_attrlen(nlh, sizeof(*ev)));
Mike Christie56c155b2011-07-25 13:48:37 -05002061 break;
Alex Aizman0896b752005-08-04 19:33:07 -07002062 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05002063 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07002064 break;
2065 }
2066
Mike Christie7b7232f2006-02-01 21:06:49 -06002067 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07002068 return err;
2069}
2070
Mike Christieb5c7a122006-04-06 21:13:33 -05002071/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002072 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
2073 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05002074 */
Alex Aizman0896b752005-08-04 19:33:07 -07002075static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002076iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07002077{
Arjan van de Ven0b950672006-01-11 13:16:10 +01002078 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002079 while (skb->len >= NLMSG_SPACE(0)) {
2080 int err;
2081 uint32_t rlen;
2082 struct nlmsghdr *nlh;
2083 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07002084 uint32_t group;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002085
2086 nlh = nlmsg_hdr(skb);
2087 if (nlh->nlmsg_len < sizeof(*nlh) ||
2088 skb->len < nlh->nlmsg_len) {
2089 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06002090 }
Mike Christieee7f8e42006-02-01 21:07:01 -06002091
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002092 ev = NLMSG_DATA(nlh);
2093 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
2094 if (rlen > skb->len)
2095 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07002096
Michael Chan43514772009-06-08 18:14:41 -07002097 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002098 if (err) {
2099 ev->type = ISCSI_KEVENT_IF_ERROR;
2100 ev->iferror = err;
2101 }
2102 do {
2103 /*
2104 * special case for GET_STATS:
2105 * on success - sending reply and stats from
2106 * inside of if_recv_msg(),
2107 * on error - fall through.
2108 */
2109 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07002110 break;
Michael Chan43514772009-06-08 18:14:41 -07002111 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002112 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
2113 } while (err < 0 && err != -ECONNREFUSED);
2114 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07002115 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01002116 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002117}
2118
Mike Christiefd7255f2006-04-06 21:13:36 -05002119#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01002120struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05002121 __ATTR(_name,_mode,_show,_store)
2122
Alex Aizman0896b752005-08-04 19:33:07 -07002123/*
2124 * iSCSI connection attrs
2125 */
Mike Christiea54a52c2006-06-28 12:00:23 -05002126#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002128show_conn_param_##param(struct device *dev, \
2129 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002130{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002131 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002132 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05002133 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002134}
2135
Mike Christiea54a52c2006-06-28 12:00:23 -05002136#define iscsi_conn_attr(field, param) \
2137 iscsi_conn_attr_show(param) \
2138static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05002139 NULL);
2140
Mike Christiea54a52c2006-06-28 12:00:23 -05002141iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
2142iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
2143iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
2144iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
2145iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
2146iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
2147iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
Mike Christiea54a52c2006-06-28 12:00:23 -05002148iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
2149iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06002150iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
2151iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Mike Christie289324b2011-02-16 15:04:37 -06002153#define iscsi_conn_ep_attr_show(param) \
2154static ssize_t show_conn_ep_param_##param(struct device *dev, \
2155 struct device_attribute *attr,\
2156 char *buf) \
2157{ \
2158 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
2159 struct iscsi_transport *t = conn->transport; \
2160 struct iscsi_endpoint *ep; \
2161 ssize_t rc; \
2162 \
2163 /* \
2164 * Need to make sure ep_disconnect does not free the LLD's \
2165 * interconnect resources while we are trying to read them. \
2166 */ \
2167 mutex_lock(&conn->ep_mutex); \
2168 ep = conn->ep; \
2169 if (!ep && t->ep_connect) { \
2170 mutex_unlock(&conn->ep_mutex); \
2171 return -ENOTCONN; \
2172 } \
2173 \
2174 if (ep) \
2175 rc = t->get_ep_param(ep, param, buf); \
2176 else \
2177 rc = t->get_conn_param(conn, param, buf); \
2178 mutex_unlock(&conn->ep_mutex); \
2179 return rc; \
2180}
2181
2182#define iscsi_conn_ep_attr(field, param) \
2183 iscsi_conn_ep_attr_show(param) \
2184static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
2185 show_conn_ep_param_##param, NULL);
2186
2187iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
2188iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
2189
Mike Christie3128c6c2011-07-25 13:48:42 -05002190static struct attribute *iscsi_conn_attrs[] = {
2191 &dev_attr_conn_max_recv_dlength.attr,
2192 &dev_attr_conn_max_xmit_dlength.attr,
2193 &dev_attr_conn_header_digest.attr,
2194 &dev_attr_conn_data_digest.attr,
2195 &dev_attr_conn_ifmarker.attr,
2196 &dev_attr_conn_ofmarker.attr,
2197 &dev_attr_conn_address.attr,
2198 &dev_attr_conn_port.attr,
2199 &dev_attr_conn_exp_statsn.attr,
2200 &dev_attr_conn_persistent_address.attr,
2201 &dev_attr_conn_persistent_port.attr,
2202 &dev_attr_conn_ping_tmo.attr,
2203 &dev_attr_conn_recv_tmo.attr,
2204 NULL,
2205};
2206
Al Viro587a1f12011-07-23 23:11:19 -04002207static umode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
Mike Christie3128c6c2011-07-25 13:48:42 -05002208 struct attribute *attr, int i)
2209{
2210 struct device *cdev = container_of(kobj, struct device, kobj);
2211 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
2212 struct iscsi_transport *t = conn->transport;
2213 int param;
2214
2215 if (attr == &dev_attr_conn_max_recv_dlength.attr)
2216 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
2217 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
2218 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
2219 else if (attr == &dev_attr_conn_header_digest.attr)
2220 param = ISCSI_PARAM_HDRDGST_EN;
2221 else if (attr == &dev_attr_conn_data_digest.attr)
2222 param = ISCSI_PARAM_DATADGST_EN;
2223 else if (attr == &dev_attr_conn_ifmarker.attr)
2224 param = ISCSI_PARAM_IFMARKER_EN;
2225 else if (attr == &dev_attr_conn_ofmarker.attr)
2226 param = ISCSI_PARAM_OFMARKER_EN;
2227 else if (attr == &dev_attr_conn_address.attr)
2228 param = ISCSI_PARAM_CONN_ADDRESS;
2229 else if (attr == &dev_attr_conn_port.attr)
2230 param = ISCSI_PARAM_CONN_PORT;
2231 else if (attr == &dev_attr_conn_exp_statsn.attr)
2232 param = ISCSI_PARAM_EXP_STATSN;
2233 else if (attr == &dev_attr_conn_persistent_address.attr)
2234 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
2235 else if (attr == &dev_attr_conn_persistent_port.attr)
2236 param = ISCSI_PARAM_PERSISTENT_PORT;
2237 else if (attr == &dev_attr_conn_ping_tmo.attr)
2238 param = ISCSI_PARAM_PING_TMO;
2239 else if (attr == &dev_attr_conn_recv_tmo.attr)
2240 param = ISCSI_PARAM_RECV_TMO;
2241 else {
2242 WARN_ONCE(1, "Invalid conn attr");
2243 return 0;
2244 }
2245
2246 return t->attr_is_visible(ISCSI_PARAM, param);
2247}
2248
2249static struct attribute_group iscsi_conn_group = {
2250 .attrs = iscsi_conn_attrs,
2251 .is_visible = iscsi_conn_attr_is_visible,
2252};
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254/*
Alex Aizman0896b752005-08-04 19:33:07 -07002255 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 */
Mike Christieb2c64162007-05-30 12:57:16 -05002257#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002259show_session_param_##param(struct device *dev, \
2260 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002261{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002262 struct iscsi_cls_session *session = \
2263 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002264 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05002265 \
2266 if (perm && !capable(CAP_SYS_ADMIN)) \
2267 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05002268 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002269}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Mike Christieb2c64162007-05-30 12:57:16 -05002271#define iscsi_session_attr(field, param, perm) \
2272 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05002273static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05002274 NULL);
Mike Christieb2c64162007-05-30 12:57:16 -05002275iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
2276iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
2277iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
2278iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
2279iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
2280iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
2281iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
2282iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
2283iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
2284iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
2285iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
2286iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
2287iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
2288iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06002289iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
2290iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
2291iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002292iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05002293iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05302294iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
2295iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05002296
Mike Christie6eabafb2008-01-31 13:36:43 -06002297static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01002298show_priv_session_state(struct device *dev, struct device_attribute *attr,
2299 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06002300{
Tony Jonesee959b02008-02-22 00:13:36 +01002301 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06002302 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
2303}
2304static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
2305 NULL);
Mike Christie0c70d842011-12-05 16:44:01 -06002306static ssize_t
2307show_priv_session_creator(struct device *dev, struct device_attribute *attr,
2308 char *buf)
2309{
2310 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
2311 return sprintf(buf, "%d\n", session->creator);
2312}
2313static ISCSI_CLASS_ATTR(priv_sess, creator, S_IRUGO, show_priv_session_creator,
2314 NULL);
Mike Christie6eabafb2008-01-31 13:36:43 -06002315
Mike Christiefd7255f2006-04-06 21:13:36 -05002316#define iscsi_priv_session_attr_show(field, format) \
2317static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002318show_priv_session_##field(struct device *dev, \
2319 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002320{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002321 struct iscsi_cls_session *session = \
2322 iscsi_dev_to_session(dev->parent); \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302323 if (session->field == -1) \
2324 return sprintf(buf, "off\n"); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002325 return sprintf(buf, format"\n", session->field); \
2326}
2327
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302328#define iscsi_priv_session_attr_store(field) \
2329static ssize_t \
2330store_priv_session_##field(struct device *dev, \
2331 struct device_attribute *attr, \
2332 const char *buf, size_t count) \
2333{ \
2334 int val; \
2335 char *cp; \
2336 struct iscsi_cls_session *session = \
2337 iscsi_dev_to_session(dev->parent); \
2338 if ((session->state == ISCSI_SESSION_FREE) || \
2339 (session->state == ISCSI_SESSION_FAILED)) \
2340 return -EBUSY; \
2341 if (strncmp(buf, "off", 3) == 0) \
2342 session->field = -1; \
2343 else { \
2344 val = simple_strtoul(buf, &cp, 0); \
2345 if (*cp != '\0' && *cp != '\n') \
2346 return -EINVAL; \
2347 session->field = val; \
2348 } \
2349 return count; \
2350}
2351
2352#define iscsi_priv_session_rw_attr(field, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002353 iscsi_priv_session_attr_show(field, format) \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302354 iscsi_priv_session_attr_store(field) \
Vasiliy Kulikov523f3c82011-02-04 15:24:14 +03002355static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302356 show_priv_session_##field, \
2357 store_priv_session_##field)
2358iscsi_priv_session_rw_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05002359
Mike Christie1d063c12011-07-25 13:48:43 -05002360static struct attribute *iscsi_session_attrs[] = {
2361 &dev_attr_sess_initial_r2t.attr,
2362 &dev_attr_sess_max_outstanding_r2t.attr,
2363 &dev_attr_sess_immediate_data.attr,
2364 &dev_attr_sess_first_burst_len.attr,
2365 &dev_attr_sess_max_burst_len.attr,
2366 &dev_attr_sess_data_pdu_in_order.attr,
2367 &dev_attr_sess_data_seq_in_order.attr,
2368 &dev_attr_sess_erl.attr,
2369 &dev_attr_sess_targetname.attr,
2370 &dev_attr_sess_tpgt.attr,
2371 &dev_attr_sess_password.attr,
2372 &dev_attr_sess_password_in.attr,
2373 &dev_attr_sess_username.attr,
2374 &dev_attr_sess_username_in.attr,
2375 &dev_attr_sess_fast_abort.attr,
2376 &dev_attr_sess_abort_tmo.attr,
2377 &dev_attr_sess_lu_reset_tmo.attr,
2378 &dev_attr_sess_tgt_reset_tmo.attr,
2379 &dev_attr_sess_ifacename.attr,
2380 &dev_attr_sess_initiatorname.attr,
2381 &dev_attr_sess_targetalias.attr,
2382 &dev_attr_priv_sess_recovery_tmo.attr,
2383 &dev_attr_priv_sess_state.attr,
Mike Christie0c70d842011-12-05 16:44:01 -06002384 &dev_attr_priv_sess_creator.attr,
Mike Christie1d063c12011-07-25 13:48:43 -05002385 NULL,
2386};
2387
Al Viro587a1f12011-07-23 23:11:19 -04002388static umode_t iscsi_session_attr_is_visible(struct kobject *kobj,
Mike Christie1d063c12011-07-25 13:48:43 -05002389 struct attribute *attr, int i)
2390{
2391 struct device *cdev = container_of(kobj, struct device, kobj);
2392 struct iscsi_cls_session *session = transport_class_to_session(cdev);
2393 struct iscsi_transport *t = session->transport;
2394 int param;
2395
2396 if (attr == &dev_attr_sess_initial_r2t.attr)
2397 param = ISCSI_PARAM_INITIAL_R2T_EN;
2398 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
2399 param = ISCSI_PARAM_MAX_R2T;
2400 else if (attr == &dev_attr_sess_immediate_data.attr)
2401 param = ISCSI_PARAM_IMM_DATA_EN;
2402 else if (attr == &dev_attr_sess_first_burst_len.attr)
2403 param = ISCSI_PARAM_FIRST_BURST;
2404 else if (attr == &dev_attr_sess_max_burst_len.attr)
2405 param = ISCSI_PARAM_MAX_BURST;
2406 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
2407 param = ISCSI_PARAM_PDU_INORDER_EN;
2408 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
2409 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
2410 else if (attr == &dev_attr_sess_erl.attr)
2411 param = ISCSI_PARAM_ERL;
2412 else if (attr == &dev_attr_sess_targetname.attr)
2413 param = ISCSI_PARAM_TARGET_NAME;
2414 else if (attr == &dev_attr_sess_tpgt.attr)
2415 param = ISCSI_PARAM_TPGT;
2416 else if (attr == &dev_attr_sess_password.attr)
2417 param = ISCSI_PARAM_USERNAME;
2418 else if (attr == &dev_attr_sess_password_in.attr)
2419 param = ISCSI_PARAM_USERNAME_IN;
2420 else if (attr == &dev_attr_sess_username.attr)
2421 param = ISCSI_PARAM_PASSWORD;
2422 else if (attr == &dev_attr_sess_username_in.attr)
2423 param = ISCSI_PARAM_PASSWORD_IN;
2424 else if (attr == &dev_attr_sess_fast_abort.attr)
2425 param = ISCSI_PARAM_FAST_ABORT;
2426 else if (attr == &dev_attr_sess_abort_tmo.attr)
2427 param = ISCSI_PARAM_ABORT_TMO;
2428 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
2429 param = ISCSI_PARAM_LU_RESET_TMO;
2430 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
2431 param = ISCSI_PARAM_TGT_RESET_TMO;
2432 else if (attr == &dev_attr_sess_ifacename.attr)
2433 param = ISCSI_PARAM_IFACE_NAME;
2434 else if (attr == &dev_attr_sess_initiatorname.attr)
2435 param = ISCSI_PARAM_INITIATOR_NAME;
2436 else if (attr == &dev_attr_sess_targetalias.attr)
2437 param = ISCSI_PARAM_TARGET_ALIAS;
2438 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
2439 return S_IRUGO | S_IWUSR;
2440 else if (attr == &dev_attr_priv_sess_state.attr)
2441 return S_IRUGO;
Mike Christie0c70d842011-12-05 16:44:01 -06002442 else if (attr == &dev_attr_priv_sess_creator.attr)
2443 return S_IRUGO;
Mike Christie1d063c12011-07-25 13:48:43 -05002444 else {
2445 WARN_ONCE(1, "Invalid session attr");
2446 return 0;
2447 }
2448
2449 return t->attr_is_visible(ISCSI_PARAM, param);
2450}
2451
2452static struct attribute_group iscsi_session_group = {
2453 .attrs = iscsi_session_attrs,
2454 .is_visible = iscsi_session_attr_is_visible,
2455};
2456
Mike Christie1819dc82007-05-30 12:57:08 -05002457/*
2458 * iSCSI host attrs
2459 */
2460#define iscsi_host_attr_show(param) \
2461static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002462show_host_param_##param(struct device *dev, \
2463 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05002464{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002465 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05002466 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
2467 return priv->iscsi_transport->get_host_param(shost, param, buf); \
2468}
2469
2470#define iscsi_host_attr(field, param) \
2471 iscsi_host_attr_show(param) \
2472static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
2473 NULL);
2474
Mike Christied8196ed2007-05-30 12:57:25 -05002475iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002476iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05002477iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05002478iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002479
Mike Christief27fb2e2011-07-25 13:48:45 -05002480static struct attribute *iscsi_host_attrs[] = {
2481 &dev_attr_host_netdev.attr,
2482 &dev_attr_host_hwaddress.attr,
2483 &dev_attr_host_ipaddress.attr,
2484 &dev_attr_host_initiatorname.attr,
2485 NULL,
2486};
2487
Al Viro587a1f12011-07-23 23:11:19 -04002488static umode_t iscsi_host_attr_is_visible(struct kobject *kobj,
Mike Christief27fb2e2011-07-25 13:48:45 -05002489 struct attribute *attr, int i)
2490{
2491 struct device *cdev = container_of(kobj, struct device, kobj);
2492 struct Scsi_Host *shost = transport_class_to_shost(cdev);
2493 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
2494 int param;
2495
2496 if (attr == &dev_attr_host_netdev.attr)
2497 param = ISCSI_HOST_PARAM_NETDEV_NAME;
2498 else if (attr == &dev_attr_host_hwaddress.attr)
2499 param = ISCSI_HOST_PARAM_HWADDRESS;
2500 else if (attr == &dev_attr_host_ipaddress.attr)
2501 param = ISCSI_HOST_PARAM_IPADDRESS;
2502 else if (attr == &dev_attr_host_initiatorname.attr)
2503 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
2504 else {
2505 WARN_ONCE(1, "Invalid host attr");
2506 return 0;
2507 }
2508
2509 return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
2510}
2511
2512static struct attribute_group iscsi_host_group = {
2513 .attrs = iscsi_host_attrs,
2514 .is_visible = iscsi_host_attr_is_visible,
2515};
Mike Christie1819dc82007-05-30 12:57:08 -05002516
Alex Aizman0896b752005-08-04 19:33:07 -07002517static int iscsi_session_match(struct attribute_container *cont,
2518 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Mike Christie7b8631b2006-01-13 18:05:50 -06002520 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07002522 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Alex Aizman0896b752005-08-04 19:33:07 -07002524 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 return 0;
2526
Mike Christie7b8631b2006-01-13 18:05:50 -06002527 session = iscsi_dev_to_session(dev);
2528 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07002529 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return 0;
2531
Alex Aizman0896b752005-08-04 19:33:07 -07002532 priv = to_iscsi_internal(shost->transportt);
2533 if (priv->session_cont.ac.class != &iscsi_session_class.class)
2534 return 0;
2535
2536 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
Alex Aizman0896b752005-08-04 19:33:07 -07002539static int iscsi_conn_match(struct attribute_container *cont,
2540 struct device *dev)
2541{
Mike Christie7b8631b2006-01-13 18:05:50 -06002542 struct iscsi_cls_session *session;
2543 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07002544 struct Scsi_Host *shost;
2545 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Alex Aizman0896b752005-08-04 19:33:07 -07002547 if (!iscsi_is_conn_dev(dev))
2548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Mike Christie7b8631b2006-01-13 18:05:50 -06002550 conn = iscsi_dev_to_conn(dev);
2551 session = iscsi_dev_to_session(conn->dev.parent);
2552 shost = iscsi_session_to_shost(session);
2553
Alex Aizman0896b752005-08-04 19:33:07 -07002554 if (!shost->transportt)
2555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Alex Aizman0896b752005-08-04 19:33:07 -07002557 priv = to_iscsi_internal(shost->transportt);
2558 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
2559 return 0;
2560
2561 return &priv->conn_cont.ac == cont;
2562}
2563
Mike Christie30a6c652006-04-06 21:13:39 -05002564static int iscsi_host_match(struct attribute_container *cont,
2565 struct device *dev)
2566{
2567 struct Scsi_Host *shost;
2568 struct iscsi_internal *priv;
2569
2570 if (!scsi_is_host_device(dev))
2571 return 0;
2572
2573 shost = dev_to_shost(dev);
2574 if (!shost->transportt ||
2575 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
2576 return 0;
2577
2578 priv = to_iscsi_internal(shost->transportt);
2579 return &priv->t.host_attrs.ac == cont;
2580}
2581
Mike Christie7b8631b2006-01-13 18:05:50 -06002582struct scsi_transport_template *
2583iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07002584{
2585 struct iscsi_internal *priv;
2586 unsigned long flags;
Mike Christief27fb2e2011-07-25 13:48:45 -05002587 int err;
Alex Aizman0896b752005-08-04 19:33:07 -07002588
2589 BUG_ON(!tt);
2590
2591 priv = iscsi_if_transport_lookup(tt);
2592 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002593 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002594
Jes Sorensen24669f752006-01-16 10:31:18 -05002595 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07002596 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002597 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002598 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07002599 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05002600 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06002601 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07002602
Tony Jonesee959b02008-02-22 00:13:36 +01002603 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01002604 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01002605 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07002606 if (err)
2607 goto free_priv;
2608
Tony Jonesee959b02008-02-22 00:13:36 +01002609 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07002610 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01002611 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07002612
Mike Christie30a6c652006-04-06 21:13:39 -05002613 /* host parameters */
Mike Christie30a6c652006-04-06 21:13:39 -05002614 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
2615 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christief27fb2e2011-07-25 13:48:45 -05002616 priv->t.host_attrs.ac.grp = &iscsi_host_group;
Mike Christie32c6e1b2008-05-21 15:53:58 -05002617 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05002618 transport_container_register(&priv->t.host_attrs);
2619
Alex Aizman0896b752005-08-04 19:33:07 -07002620 /* connection parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07002621 priv->conn_cont.ac.class = &iscsi_connection_class.class;
2622 priv->conn_cont.ac.match = iscsi_conn_match;
Mike Christie3128c6c2011-07-25 13:48:42 -05002623 priv->conn_cont.ac.grp = &iscsi_conn_group;
Alex Aizman0896b752005-08-04 19:33:07 -07002624 transport_container_register(&priv->conn_cont);
2625
Alex Aizman0896b752005-08-04 19:33:07 -07002626 /* session parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07002627 priv->session_cont.ac.class = &iscsi_session_class.class;
2628 priv->session_cont.ac.match = iscsi_session_match;
Mike Christie1d063c12011-07-25 13:48:43 -05002629 priv->session_cont.ac.grp = &iscsi_session_group;
Alex Aizman0896b752005-08-04 19:33:07 -07002630 transport_container_register(&priv->session_cont);
2631
Alex Aizman0896b752005-08-04 19:33:07 -07002632 spin_lock_irqsave(&iscsi_transport_lock, flags);
2633 list_add(&priv->list, &iscsi_transports);
2634 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Alex Aizman0896b752005-08-04 19:33:07 -07002636 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06002637 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Tony Jonesee959b02008-02-22 00:13:36 +01002639unregister_dev:
2640 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05002641 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002642free_priv:
2643 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06002644 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645}
Alex Aizman0896b752005-08-04 19:33:07 -07002646EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Alex Aizman0896b752005-08-04 19:33:07 -07002648int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
Alex Aizman0896b752005-08-04 19:33:07 -07002650 struct iscsi_internal *priv;
2651 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Alex Aizman0896b752005-08-04 19:33:07 -07002653 BUG_ON(!tt);
2654
Arjan van de Ven0b950672006-01-11 13:16:10 +01002655 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002656
2657 priv = iscsi_if_transport_lookup(tt);
2658 BUG_ON (!priv);
2659
Alex Aizman0896b752005-08-04 19:33:07 -07002660 spin_lock_irqsave(&iscsi_transport_lock, flags);
2661 list_del(&priv->list);
2662 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2663
2664 transport_container_unregister(&priv->conn_cont);
2665 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05002666 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07002667
Tony Jonesee959b02008-02-22 00:13:36 +01002668 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
2669 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002670 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002671
2672 return 0;
2673}
2674EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676static __init int iscsi_transport_init(void)
2677{
Alex Aizman0896b752005-08-04 19:33:07 -07002678 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Meelis Roos09492602006-12-17 12:10:26 -06002680 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05002681 ISCSI_TRANSPORT_VERSION);
2682
Mike Christie41be1442007-02-28 17:32:18 -06002683 atomic_set(&iscsi_session_nr, 0);
2684
Alex Aizman0896b752005-08-04 19:33:07 -07002685 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 if (err)
2687 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07002688
Mike Christied82ff9be2008-05-21 15:54:13 -05002689 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002690 if (err)
2691 goto unregister_transport_class;
2692
Mike Christie8d079132011-07-25 13:48:40 -05002693 err = class_register(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002694 if (err)
2695 goto unregister_endpoint_class;
2696
Mike Christie8d079132011-07-25 13:48:40 -05002697 err = transport_class_register(&iscsi_host_class);
2698 if (err)
2699 goto unregister_iface_class;
2700
Mike Christie30a6c652006-04-06 21:13:39 -05002701 err = transport_class_register(&iscsi_connection_class);
2702 if (err)
2703 goto unregister_host_class;
2704
Alex Aizman0896b752005-08-04 19:33:07 -07002705 err = transport_class_register(&iscsi_session_class);
2706 if (err)
2707 goto unregister_conn_class;
2708
Mike Christied82ff9be2008-05-21 15:54:13 -05002709 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2710 NULL, THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07002711 if (!nls) {
2712 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04002713 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07002714 }
2715
Mike Christied8bf5412007-12-13 12:43:27 -06002716 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2717 if (!iscsi_eh_timer_workq)
2718 goto release_nls;
2719
Mike Christie43a145a2006-10-16 18:09:38 -04002720 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07002721
Mike Christied8bf5412007-12-13 12:43:27 -06002722release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002723 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002724unregister_session_class:
2725 transport_class_unregister(&iscsi_session_class);
2726unregister_conn_class:
2727 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002728unregister_host_class:
2729 transport_class_unregister(&iscsi_host_class);
Mike Christie8d079132011-07-25 13:48:40 -05002730unregister_iface_class:
2731 class_unregister(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002732unregister_endpoint_class:
2733 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002734unregister_transport_class:
2735 class_unregister(&iscsi_transport_class);
2736 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737}
2738
2739static void __exit iscsi_transport_exit(void)
2740{
Mike Christied8bf5412007-12-13 12:43:27 -06002741 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002742 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002743 transport_class_unregister(&iscsi_connection_class);
2744 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002745 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002746 class_unregister(&iscsi_endpoint_class);
Mike Christie8d079132011-07-25 13:48:40 -05002747 class_unregister(&iscsi_iface_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002748 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
2750
2751module_init(iscsi_transport_init);
2752module_exit(iscsi_transport_exit);
2753
Alex Aizman0896b752005-08-04 19:33:07 -07002754MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2755 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2756 "Alex Aizman <itn780@yahoo.com>");
2757MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05002759MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
Stephen Hemminger058548a2010-12-09 09:37:56 -08002760MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);