blob: b3a2515386f84a16ed69704c5d16e9c9f802b44c [file] [log] [blame]
Alex Aizman0896b752005-08-04 19:33:07 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * iSCSI transport class definitions
3 *
4 * Copyright (C) IBM Corporation, 2004
Alex Aizman0896b752005-08-04 19:33:07 -07005 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23#include <linux/module.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010024#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Mike Christie90eeb012011-07-25 13:48:50 -050026#include <linux/bsg-lib.h>
Alex Aizman0896b752005-08-04 19:33:07 -070027#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <scsi/scsi.h>
29#include <scsi/scsi_host.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_transport.h>
32#include <scsi/scsi_transport_iscsi.h>
Alex Aizman0896b752005-08-04 19:33:07 -070033#include <scsi/iscsi_if.h>
Mike Christiec01be6d2010-07-22 16:59:49 +053034#include <scsi/scsi_cmnd.h>
Mike Christie90eeb012011-07-25 13:48:50 -050035#include <scsi/scsi_bsg_iscsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Mike Christie4c2133c2008-06-16 10:11:34 -050037#define ISCSI_TRANSPORT_VERSION "2.0-870"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Mike Christie632248a2009-08-20 15:11:01 -050039static int dbg_session;
40module_param_named(debug_session, dbg_session, int,
41 S_IRUGO | S_IWUSR);
42MODULE_PARM_DESC(debug_session,
43 "Turn on debugging for sessions in scsi_transport_iscsi "
44 "module. Set to 1 to turn on, and zero to turn off. Default "
45 "is off.");
46
47static int dbg_conn;
48module_param_named(debug_conn, dbg_conn, int,
49 S_IRUGO | S_IWUSR);
50MODULE_PARM_DESC(debug_conn,
51 "Turn on debugging for connections in scsi_transport_iscsi "
52 "module. Set to 1 to turn on, and zero to turn off. Default "
53 "is off.");
54
55#define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
56 do { \
57 if (dbg_session) \
58 iscsi_cls_session_printk(KERN_INFO, _session, \
59 "%s: " dbg_fmt, \
60 __func__, ##arg); \
61 } while (0);
62
63#define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
64 do { \
65 if (dbg_conn) \
66 iscsi_cls_conn_printk(KERN_INFO, _conn, \
67 "%s: " dbg_fmt, \
68 __func__, ##arg); \
69 } while (0);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071struct iscsi_internal {
72 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070073 struct iscsi_transport *iscsi_transport;
74 struct list_head list;
Tony Jonesee959b02008-02-22 00:13:36 +010075 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050076
Alex Aizman0896b752005-08-04 19:33:07 -070077 struct transport_container conn_cont;
Alex Aizman0896b752005-08-04 19:33:07 -070078 struct transport_container session_cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Mike Christie41be1442007-02-28 17:32:18 -060081static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060082static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050083
Alex Aizman0896b752005-08-04 19:33:07 -070084/*
85 * list of registered transports and lock that must
86 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010087 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070088 */
89static LIST_HEAD(iscsi_transports);
90static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Alex Aizman0896b752005-08-04 19:33:07 -070092#define to_iscsi_internal(tmpl) \
93 container_of(tmpl, struct iscsi_internal, t)
94
Tony Jonesee959b02008-02-22 00:13:36 +010095#define dev_to_iscsi_internal(_dev) \
96 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -070097
Tony Jonesee959b02008-02-22 00:13:36 +010098static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -070099{
Tony Jonesee959b02008-02-22 00:13:36 +0100100 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -0700101 kfree(priv);
102}
103
104/*
105 * iscsi_transport_class represents the iscsi_transports that are
106 * registered.
107 */
108static struct class iscsi_transport_class = {
109 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +0100110 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -0700111};
112
113static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100114show_transport_handle(struct device *dev, struct device_attribute *attr,
115 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -0700116{
Tony Jonesee959b02008-02-22 00:13:36 +0100117 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Mike Christie762e2bf2005-09-12 21:01:46 -0500118 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -0700119}
Tony Jonesee959b02008-02-22 00:13:36 +0100120static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700121
122#define show_transport_attr(name, format) \
123static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100124show_transport_##name(struct device *dev, \
125 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -0700126{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100127 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Alex Aizman0896b752005-08-04 19:33:07 -0700128 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
129} \
Tony Jonesee959b02008-02-22 00:13:36 +0100130static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700131
132show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700133
134static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100135 &dev_attr_handle.attr,
136 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700137 NULL,
138};
139
140static struct attribute_group iscsi_transport_group = {
141 .attrs = iscsi_transport_attrs,
142};
143
Mike Christied82ff9be2008-05-21 15:54:13 -0500144/*
145 * iSCSI endpoint attrs
146 */
147#define iscsi_dev_to_endpoint(_dev) \
148 container_of(_dev, struct iscsi_endpoint, dev)
149
150#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
151struct device_attribute dev_attr_##_prefix##_##_name = \
152 __ATTR(_name,_mode,_show,_store)
153
154static void iscsi_endpoint_release(struct device *dev)
155{
156 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
157 kfree(ep);
158}
159
160static struct class iscsi_endpoint_class = {
161 .name = "iscsi_endpoint",
162 .dev_release = iscsi_endpoint_release,
163};
164
165static ssize_t
166show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
167{
168 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500169 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500170}
171static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
172
173static struct attribute *iscsi_endpoint_attrs[] = {
174 &dev_attr_ep_handle.attr,
175 NULL,
176};
177
178static struct attribute_group iscsi_endpoint_group = {
179 .attrs = iscsi_endpoint_attrs,
180};
181
182#define ISCSI_MAX_EPID -1
183
184static int iscsi_match_epid(struct device *dev, void *data)
185{
186 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500187 uint64_t *epid = (uint64_t *) data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500188
189 return *epid == ep->id;
190}
191
192struct iscsi_endpoint *
193iscsi_create_endpoint(int dd_size)
194{
195 struct device *dev;
196 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500197 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500198 int err;
199
200 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400201 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500202 iscsi_match_epid);
203 if (!dev)
204 break;
205 }
206 if (id == ISCSI_MAX_EPID) {
207 printk(KERN_ERR "Too many connections. Max supported %u\n",
208 ISCSI_MAX_EPID - 1);
209 return NULL;
210 }
211
212 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
213 if (!ep)
214 return NULL;
215
216 ep->id = id;
217 ep->dev.class = &iscsi_endpoint_class;
Kay Sievers71610f52008-12-03 22:41:36 +0100218 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500219 err = device_register(&ep->dev);
220 if (err)
221 goto free_ep;
222
223 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
224 if (err)
225 goto unregister_dev;
226
227 if (dd_size)
228 ep->dd_data = &ep[1];
229 return ep;
230
231unregister_dev:
232 device_unregister(&ep->dev);
233 return NULL;
234
235free_ep:
236 kfree(ep);
237 return NULL;
238}
239EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
240
241void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
242{
243 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
244 device_unregister(&ep->dev);
245}
246EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
247
248struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
249{
Mike Christief80f8682008-06-16 10:11:35 -0500250 struct iscsi_endpoint *ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500251 struct device *dev;
252
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400253 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500254 iscsi_match_epid);
255 if (!dev)
256 return NULL;
257
Mike Christief80f8682008-06-16 10:11:35 -0500258 ep = iscsi_dev_to_endpoint(dev);
259 /*
260 * we can drop this now because the interface will prevent
261 * removals and lookups from racing.
262 */
263 put_device(dev);
264 return ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500265}
266EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
267
Mike Christie8d079132011-07-25 13:48:40 -0500268/*
269 * Interface to display network param to sysfs
270 */
271
272static void iscsi_iface_release(struct device *dev)
273{
274 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
275 struct device *parent = iface->dev.parent;
276
277 kfree(iface);
278 put_device(parent);
279}
280
281
282static struct class iscsi_iface_class = {
283 .name = "iscsi_iface",
284 .dev_release = iscsi_iface_release,
285};
286
287#define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
288struct device_attribute dev_attr_##_prefix##_##_name = \
289 __ATTR(_name, _mode, _show, _store)
290
291/* iface attrs show */
292#define iscsi_iface_attr_show(type, name, param_type, param) \
293static ssize_t \
294show_##type##_##name(struct device *dev, struct device_attribute *attr, \
295 char *buf) \
296{ \
297 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
298 struct iscsi_transport *t = iface->transport; \
299 return t->get_iface_param(iface, param_type, param, buf); \
300} \
301
302#define iscsi_iface_net_attr(type, name, param) \
303 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
304static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
305
306/* generic read only ipvi4 attribute */
307iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
308iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
309iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
310iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
311
312/* generic read only ipv6 attribute */
313iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
314iscsi_iface_net_attr(ipv6_iface, link_local_addr, ISCSI_NET_PARAM_IPV6_LINKLOCAL);
315iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
316iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
317 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
318iscsi_iface_net_attr(ipv6_iface, linklocal_autocfg,
319 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
320
321/* common read only iface attribute */
322iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
323iscsi_iface_net_attr(iface, vlan, ISCSI_NET_PARAM_VLAN_ID);
Mike Christie4223b9e2011-07-25 13:48:47 -0500324iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
325iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
Vikas Chaudhary8c7d40f2011-08-01 03:26:12 -0700326iscsi_iface_net_attr(iface, mtu, ISCSI_NET_PARAM_MTU);
Mike Christie8d079132011-07-25 13:48:40 -0500327
328static mode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
329 struct attribute *attr, int i)
330{
331 struct device *dev = container_of(kobj, struct device, kobj);
332 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
333 struct iscsi_transport *t = iface->transport;
Mike Christieb78dbba2011-07-25 13:48:44 -0500334 int param;
Mike Christie8d079132011-07-25 13:48:40 -0500335
336 if (attr == &dev_attr_iface_enabled.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500337 param = ISCSI_NET_PARAM_IFACE_ENABLE;
Mike Christie8d079132011-07-25 13:48:40 -0500338 else if (attr == &dev_attr_iface_vlan.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500339 param = ISCSI_NET_PARAM_VLAN_ID;
Mike Christie4223b9e2011-07-25 13:48:47 -0500340 else if (attr == &dev_attr_iface_vlan_priority.attr)
341 param = ISCSI_NET_PARAM_VLAN_PRIORITY;
342 else if (attr == &dev_attr_iface_vlan_enabled.attr)
343 param = ISCSI_NET_PARAM_VLAN_ENABLED;
Vikas Chaudhary8c7d40f2011-08-01 03:26:12 -0700344 else if (attr == &dev_attr_iface_mtu.attr)
345 param = ISCSI_NET_PARAM_MTU;
Mike Christieb78dbba2011-07-25 13:48:44 -0500346 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
Mike Christie8d079132011-07-25 13:48:40 -0500347 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500348 param = ISCSI_NET_PARAM_IPV4_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500349 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500350 param = ISCSI_NET_PARAM_IPV4_GW;
Mike Christie8d079132011-07-25 13:48:40 -0500351 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500352 param = ISCSI_NET_PARAM_IPV4_SUBNET;
Mike Christie8d079132011-07-25 13:48:40 -0500353 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500354 param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
355 else
356 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500357 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
358 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500359 param = ISCSI_NET_PARAM_IPV6_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500360 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500361 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
Mike Christie8d079132011-07-25 13:48:40 -0500362 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500363 param = ISCSI_NET_PARAM_IPV6_ROUTER;
Mike Christie8d079132011-07-25 13:48:40 -0500364 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500365 param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
Mike Christie8d079132011-07-25 13:48:40 -0500366 else if (attr == &dev_attr_ipv6_iface_linklocal_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500367 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
368 else
369 return 0;
370 } else {
371 WARN_ONCE(1, "Invalid iface attr");
372 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500373 }
374
Mike Christieb78dbba2011-07-25 13:48:44 -0500375 return t->attr_is_visible(ISCSI_NET_PARAM, param);
Mike Christie8d079132011-07-25 13:48:40 -0500376}
377
378static struct attribute *iscsi_iface_attrs[] = {
379 &dev_attr_iface_enabled.attr,
380 &dev_attr_iface_vlan.attr,
Mike Christie4223b9e2011-07-25 13:48:47 -0500381 &dev_attr_iface_vlan_priority.attr,
382 &dev_attr_iface_vlan_enabled.attr,
Mike Christie8d079132011-07-25 13:48:40 -0500383 &dev_attr_ipv4_iface_ipaddress.attr,
384 &dev_attr_ipv4_iface_gateway.attr,
385 &dev_attr_ipv4_iface_subnet.attr,
386 &dev_attr_ipv4_iface_bootproto.attr,
387 &dev_attr_ipv6_iface_ipaddress.attr,
388 &dev_attr_ipv6_iface_link_local_addr.attr,
389 &dev_attr_ipv6_iface_router_addr.attr,
390 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
391 &dev_attr_ipv6_iface_linklocal_autocfg.attr,
Vikas Chaudhary8c7d40f2011-08-01 03:26:12 -0700392 &dev_attr_iface_mtu.attr,
Mike Christie8d079132011-07-25 13:48:40 -0500393 NULL,
394};
395
396static struct attribute_group iscsi_iface_group = {
397 .attrs = iscsi_iface_attrs,
398 .is_visible = iscsi_iface_attr_is_visible,
399};
400
401struct iscsi_iface *
402iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
403 uint32_t iface_type, uint32_t iface_num, int dd_size)
404{
405 struct iscsi_iface *iface;
406 int err;
407
408 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
409 if (!iface)
410 return NULL;
411
412 iface->transport = transport;
413 iface->iface_type = iface_type;
414 iface->iface_num = iface_num;
415 iface->dev.release = iscsi_iface_release;
416 iface->dev.class = &iscsi_iface_class;
417 /* parent reference released in iscsi_iface_release */
418 iface->dev.parent = get_device(&shost->shost_gendev);
419 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
420 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
421 iface_num);
422 else
423 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
424 iface_num);
425
426 err = device_register(&iface->dev);
427 if (err)
428 goto free_iface;
429
430 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
431 if (err)
432 goto unreg_iface;
433
434 if (dd_size)
435 iface->dd_data = &iface[1];
436 return iface;
437
438unreg_iface:
439 device_unregister(&iface->dev);
440 return NULL;
441
442free_iface:
443 put_device(iface->dev.parent);
444 kfree(iface);
445 return NULL;
446}
447EXPORT_SYMBOL_GPL(iscsi_create_iface);
448
449void iscsi_destroy_iface(struct iscsi_iface *iface)
450{
451 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
452 device_unregister(&iface->dev);
453}
454EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
455
Mike Christie90eeb012011-07-25 13:48:50 -0500456/*
457 * BSG support
458 */
459/**
460 * iscsi_bsg_host_dispatch - Dispatch command to LLD.
461 * @job: bsg job to be processed
462 */
463static int iscsi_bsg_host_dispatch(struct bsg_job *job)
464{
465 struct Scsi_Host *shost = iscsi_job_to_shost(job);
466 struct iscsi_bsg_request *req = job->request;
467 struct iscsi_bsg_reply *reply = job->reply;
468 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
469 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
470 int ret;
471
472 /* check if we have the msgcode value at least */
473 if (job->request_len < sizeof(uint32_t)) {
474 ret = -ENOMSG;
475 goto fail_host_msg;
476 }
477
478 /* Validate the host command */
479 switch (req->msgcode) {
480 case ISCSI_BSG_HST_VENDOR:
481 cmdlen += sizeof(struct iscsi_bsg_host_vendor);
482 if ((shost->hostt->vendor_id == 0L) ||
483 (req->rqst_data.h_vendor.vendor_id !=
484 shost->hostt->vendor_id)) {
485 ret = -ESRCH;
486 goto fail_host_msg;
487 }
488 break;
489 default:
490 ret = -EBADR;
491 goto fail_host_msg;
492 }
493
494 /* check if we really have all the request data needed */
495 if (job->request_len < cmdlen) {
496 ret = -ENOMSG;
497 goto fail_host_msg;
498 }
499
500 ret = i->iscsi_transport->bsg_request(job);
501 if (!ret)
502 return 0;
503
504fail_host_msg:
505 /* return the errno failure code as the only status */
506 BUG_ON(job->reply_len < sizeof(uint32_t));
507 reply->reply_payload_rcv_len = 0;
508 reply->result = ret;
509 job->reply_len = sizeof(uint32_t);
510 bsg_job_done(job, ret, 0);
511 return 0;
512}
513
514/**
515 * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
516 * @shost: shost for iscsi_host
517 * @cls_host: iscsi_cls_host adding the structures to
518 */
519static int
520iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
521{
522 struct device *dev = &shost->shost_gendev;
523 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
524 struct request_queue *q;
525 char bsg_name[20];
526 int ret;
527
528 if (!i->iscsi_transport->bsg_request)
529 return -ENOTSUPP;
530
531 snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
532
533 q = __scsi_alloc_queue(shost, bsg_request_fn);
534 if (!q)
535 return -ENOMEM;
536
537 ret = bsg_setup_queue(dev, q, bsg_name, iscsi_bsg_host_dispatch, 0);
538 if (ret) {
539 shost_printk(KERN_ERR, shost, "bsg interface failed to "
540 "initialize - no request queue\n");
541 blk_cleanup_queue(q);
542 return ret;
543 }
544
545 ihost->bsg_q = q;
546 return 0;
547}
548
Mike Christie30a6c652006-04-06 21:13:39 -0500549static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100550 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500551{
552 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500553 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500554
555 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -0600556 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -0500557 mutex_init(&ihost->mutex);
Mike Christie90eeb012011-07-25 13:48:50 -0500558
559 iscsi_bsg_host_add(shost, ihost);
560 /* ignore any bsg add error - we just can't do sgio */
561
562 return 0;
563}
564
565static int iscsi_remove_host(struct transport_container *tc,
566 struct device *dev, struct device *cdev)
567{
568 struct Scsi_Host *shost = dev_to_shost(dev);
569 struct iscsi_cls_host *ihost = shost->shost_data;
570
571 if (ihost->bsg_q) {
572 bsg_remove_queue(ihost->bsg_q);
573 blk_cleanup_queue(ihost->bsg_q);
574 }
Mike Christie30a6c652006-04-06 21:13:39 -0500575 return 0;
576}
577
578static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
579 "iscsi_host",
580 iscsi_setup_host,
Mike Christie90eeb012011-07-25 13:48:50 -0500581 iscsi_remove_host,
Mike Christie30a6c652006-04-06 21:13:39 -0500582 NULL);
583
Alex Aizman0896b752005-08-04 19:33:07 -0700584static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
585 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 NULL,
587 NULL,
588 NULL);
589
Alex Aizman0896b752005-08-04 19:33:07 -0700590static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
591 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 NULL,
593 NULL,
594 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700595
596static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100597static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700598
Mike Christie7b7232f2006-02-01 21:06:49 -0600599static LIST_HEAD(sesslist);
600static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700601static LIST_HEAD(connlist);
602static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Mike Christieb5c7a122006-04-06 21:13:33 -0500604static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
605{
606 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
607 return sess->sid;
608}
609
610/*
611 * Returns the matching session to a given sid
612 */
613static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600614{
615 unsigned long flags;
616 struct iscsi_cls_session *sess;
617
618 spin_lock_irqsave(&sesslock, flags);
619 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500620 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600621 spin_unlock_irqrestore(&sesslock, flags);
622 return sess;
623 }
624 }
625 spin_unlock_irqrestore(&sesslock, flags);
626 return NULL;
627}
628
Mike Christieb5c7a122006-04-06 21:13:33 -0500629/*
630 * Returns the matching connection to a given sid / cid tuple
631 */
632static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600633{
634 unsigned long flags;
635 struct iscsi_cls_conn *conn;
636
637 spin_lock_irqsave(&connlock, flags);
638 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500639 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600640 spin_unlock_irqrestore(&connlock, flags);
641 return conn;
642 }
643 }
644 spin_unlock_irqrestore(&connlock, flags);
645 return NULL;
646}
647
Mike Christie7b8631b2006-01-13 18:05:50 -0600648/*
649 * The following functions can be used by LLDs that allocate
650 * their own scsi_hosts or by software iscsi LLDs
651 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600652static struct {
653 int value;
654 char *name;
655} iscsi_session_state_names[] = {
656 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
657 { ISCSI_SESSION_FAILED, "FAILED" },
658 { ISCSI_SESSION_FREE, "FREE" },
659};
660
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200661static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600662{
663 int i;
664 char *name = NULL;
665
666 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
667 if (iscsi_session_state_names[i].value == state) {
668 name = iscsi_session_state_names[i].name;
669 break;
670 }
671 }
672 return name;
673}
674
675int iscsi_session_chkready(struct iscsi_cls_session *session)
676{
677 unsigned long flags;
678 int err;
679
680 spin_lock_irqsave(&session->lock, flags);
681 switch (session->state) {
682 case ISCSI_SESSION_LOGGED_IN:
683 err = 0;
684 break;
685 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -0500686 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600687 break;
688 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500689 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600690 break;
691 default:
692 err = DID_NO_CONNECT << 16;
693 break;
694 }
695 spin_unlock_irqrestore(&session->lock, flags);
696 return err;
697}
698EXPORT_SYMBOL_GPL(iscsi_session_chkready);
699
Manish Rangankar17fa5752011-07-25 13:48:52 -0500700int iscsi_is_session_online(struct iscsi_cls_session *session)
701{
702 unsigned long flags;
703 int ret = 0;
704
705 spin_lock_irqsave(&session->lock, flags);
706 if (session->state == ISCSI_SESSION_LOGGED_IN)
707 ret = 1;
708 spin_unlock_irqrestore(&session->lock, flags);
709 return ret;
710}
711EXPORT_SYMBOL_GPL(iscsi_is_session_online);
712
Mike Christie7b8631b2006-01-13 18:05:50 -0600713static void iscsi_session_release(struct device *dev)
714{
715 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600716 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Mike Christie7b8631b2006-01-13 18:05:50 -0600718 shost = iscsi_session_to_shost(session);
719 scsi_host_put(shost);
Mike Christie632248a2009-08-20 15:11:01 -0500720 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600721 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600722}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Mike Christie7b8631b2006-01-13 18:05:50 -0600724static int iscsi_is_session_dev(const struct device *dev)
725{
726 return dev->release == iscsi_session_release;
727}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Mike Christiea4804cd2008-05-21 15:54:00 -0500729static int iscsi_iter_session_fn(struct device *dev, void *data)
730{
731 void (* fn) (struct iscsi_cls_session *) = data;
732
733 if (!iscsi_is_session_dev(dev))
734 return 0;
735 fn(iscsi_dev_to_session(dev));
736 return 0;
737}
738
739void iscsi_host_for_each_session(struct Scsi_Host *shost,
740 void (*fn)(struct iscsi_cls_session *))
741{
742 device_for_each_child(&shost->shost_gendev, fn,
743 iscsi_iter_session_fn);
744}
745EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
746
Mike Christie8aae18a2008-01-31 13:36:48 -0600747/**
748 * iscsi_scan_finished - helper to report when running scans are done
749 * @shost: scsi host
750 * @time: scan run time
751 *
752 * This function can be used by drives like qla4xxx to report to the scsi
753 * layer when the scans it kicked off at module load time are done.
754 */
755int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
756{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500757 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600758 /*
759 * qla4xxx will have kicked off some session unblocks before calling
760 * scsi_scan_host, so just wait for them to complete.
761 */
762 return !atomic_read(&ihost->nr_scans);
763}
764EXPORT_SYMBOL_GPL(iscsi_scan_finished);
765
Mike Christie79706342008-05-21 15:54:12 -0500766struct iscsi_scan_data {
767 unsigned int channel;
768 unsigned int id;
769 unsigned int lun;
770};
771
772static int iscsi_user_scan_session(struct device *dev, void *data)
773{
774 struct iscsi_scan_data *scan_data = data;
775 struct iscsi_cls_session *session;
776 struct Scsi_Host *shost;
777 struct iscsi_cls_host *ihost;
778 unsigned long flags;
779 unsigned int id;
780
781 if (!iscsi_is_session_dev(dev))
782 return 0;
783
784 session = iscsi_dev_to_session(dev);
Mike Christie632248a2009-08-20 15:11:01 -0500785
786 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
787
Mike Christie79706342008-05-21 15:54:12 -0500788 shost = iscsi_session_to_shost(session);
789 ihost = shost->shost_data;
790
791 mutex_lock(&ihost->mutex);
792 spin_lock_irqsave(&session->lock, flags);
793 if (session->state != ISCSI_SESSION_LOGGED_IN) {
794 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie632248a2009-08-20 15:11:01 -0500795 goto user_scan_exit;
Mike Christie79706342008-05-21 15:54:12 -0500796 }
797 id = session->target_id;
798 spin_unlock_irqrestore(&session->lock, flags);
799
800 if (id != ISCSI_MAX_TARGET) {
801 if ((scan_data->channel == SCAN_WILD_CARD ||
802 scan_data->channel == 0) &&
803 (scan_data->id == SCAN_WILD_CARD ||
804 scan_data->id == id))
805 scsi_scan_target(&session->dev, 0, id,
806 scan_data->lun, 1);
807 }
Mike Christie632248a2009-08-20 15:11:01 -0500808
809user_scan_exit:
Mike Christie79706342008-05-21 15:54:12 -0500810 mutex_unlock(&ihost->mutex);
Mike Christie632248a2009-08-20 15:11:01 -0500811 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
Mike Christie79706342008-05-21 15:54:12 -0500812 return 0;
813}
814
Mike Christie30a6c652006-04-06 21:13:39 -0500815static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
816 uint id, uint lun)
817{
Mike Christie79706342008-05-21 15:54:12 -0500818 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500819
Mike Christie79706342008-05-21 15:54:12 -0500820 scan_data.channel = channel;
821 scan_data.id = id;
822 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -0500823
Mike Christie79706342008-05-21 15:54:12 -0500824 return device_for_each_child(&shost->shost_gendev, &scan_data,
825 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -0500826}
827
Mike Christiebd976f62008-01-31 13:36:46 -0600828static void iscsi_scan_session(struct work_struct *work)
829{
830 struct iscsi_cls_session *session =
831 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600832 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500833 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500834 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600835
Mike Christie79706342008-05-21 15:54:12 -0500836 scan_data.channel = 0;
837 scan_data.id = SCAN_WILD_CARD;
838 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -0600839
Mike Christie79706342008-05-21 15:54:12 -0500840 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -0600841 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600842}
843
Mike Christiec01be6d2010-07-22 16:59:49 +0530844/**
845 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
Randy Dunlape6d4ef42010-08-14 13:05:41 -0700846 * @cmd: scsi cmd passed to scsi eh handler
Mike Christiec01be6d2010-07-22 16:59:49 +0530847 *
848 * If the session is down this function will wait for the recovery
849 * timer to fire or for the session to be logged back in. If the
850 * recovery timer fires then FAST_IO_FAIL is returned. The caller
851 * should pass this error value to the scsi eh.
852 */
853int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
854{
855 struct iscsi_cls_session *session =
856 starget_to_session(scsi_target(cmd->device));
857 unsigned long flags;
858 int ret = 0;
859
860 spin_lock_irqsave(&session->lock, flags);
861 while (session->state != ISCSI_SESSION_LOGGED_IN) {
862 if (session->state == ISCSI_SESSION_FREE) {
863 ret = FAST_IO_FAIL;
864 break;
865 }
866 spin_unlock_irqrestore(&session->lock, flags);
867 msleep(1000);
868 spin_lock_irqsave(&session->lock, flags);
869 }
870 spin_unlock_irqrestore(&session->lock, flags);
871 return ret;
872}
873EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
874
David Howellsc4028952006-11-22 14:57:56 +0000875static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500876{
David Howellsc4028952006-11-22 14:57:56 +0000877 struct iscsi_cls_session *session =
878 container_of(work, struct iscsi_cls_session,
879 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600880 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500881
Mike Christie322d7392008-01-31 13:36:52 -0600882 iscsi_cls_session_printk(KERN_INFO, session,
883 "session recovery timed out after %d secs\n",
884 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500885
Mike Christie6eabafb2008-01-31 13:36:43 -0600886 spin_lock_irqsave(&session->lock, flags);
887 switch (session->state) {
888 case ISCSI_SESSION_FAILED:
889 session->state = ISCSI_SESSION_FREE;
890 break;
891 case ISCSI_SESSION_LOGGED_IN:
892 case ISCSI_SESSION_FREE:
893 /* we raced with the unblock's flush */
894 spin_unlock_irqrestore(&session->lock, flags);
895 return;
896 }
897 spin_unlock_irqrestore(&session->lock, flags);
898
Mike Christie30a6c652006-04-06 21:13:39 -0500899 if (session->transport->session_recovery_timedout)
900 session->transport->session_recovery_timedout(session);
901
Mike Christie632248a2009-08-20 15:11:01 -0500902 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500903 scsi_target_unblock(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500904 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500905}
906
Mike Christie45ab33b2008-03-04 13:26:55 -0600907static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500908{
Mike Christie45ab33b2008-03-04 13:26:55 -0600909 struct iscsi_cls_session *session =
910 container_of(work, struct iscsi_cls_session,
911 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600912 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500913 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600914 unsigned long flags;
915
Mike Christie632248a2009-08-20 15:11:01 -0500916 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
Mike Christie45ab33b2008-03-04 13:26:55 -0600917 /*
918 * The recovery and unblock work get run from the same workqueue,
919 * so try to cancel it if it was going to run after this unblock.
920 */
921 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600922 spin_lock_irqsave(&session->lock, flags);
923 session->state = ISCSI_SESSION_LOGGED_IN;
924 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600925 /* start IO */
926 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600927 /*
928 * Only do kernel scanning if the driver is properly hooked into
929 * the async scanning code (drivers like iscsi_tcp do login and
930 * scanning from userspace).
931 */
932 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -0600933 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -0600934 atomic_inc(&ihost->nr_scans);
935 }
Mike Christie632248a2009-08-20 15:11:01 -0500936 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600937}
Mike Christie45ab33b2008-03-04 13:26:55 -0600938
939/**
940 * iscsi_unblock_session - set a session as logged in and start IO.
941 * @session: iscsi session
942 *
943 * Mark a session as ready to accept IO.
944 */
945void iscsi_unblock_session(struct iscsi_cls_session *session)
946{
947 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
948 /*
949 * make sure all the events have completed before tell the driver
950 * it is safe
951 */
952 flush_workqueue(iscsi_eh_timer_workq);
953}
Mike Christie30a6c652006-04-06 21:13:39 -0500954EXPORT_SYMBOL_GPL(iscsi_unblock_session);
955
Mike Christie45ab33b2008-03-04 13:26:55 -0600956static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500957{
Mike Christie45ab33b2008-03-04 13:26:55 -0600958 struct iscsi_cls_session *session =
959 container_of(work, struct iscsi_cls_session,
960 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600961 unsigned long flags;
962
Mike Christie632248a2009-08-20 15:11:01 -0500963 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600964 spin_lock_irqsave(&session->lock, flags);
965 session->state = ISCSI_SESSION_FAILED;
966 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500967 scsi_target_block(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500968 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
Mike Christiefdd46dc2009-11-11 16:34:34 -0600969 if (session->recovery_tmo >= 0)
970 queue_delayed_work(iscsi_eh_timer_workq,
971 &session->recovery_work,
972 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500973}
Mike Christie45ab33b2008-03-04 13:26:55 -0600974
975void iscsi_block_session(struct iscsi_cls_session *session)
976{
977 queue_work(iscsi_eh_timer_workq, &session->block_work);
978}
Mike Christie30a6c652006-04-06 21:13:39 -0500979EXPORT_SYMBOL_GPL(iscsi_block_session);
980
Mike Christie26974782007-12-13 12:43:29 -0600981static void __iscsi_unbind_session(struct work_struct *work)
982{
983 struct iscsi_cls_session *session =
984 container_of(work, struct iscsi_cls_session,
985 unbind_work);
986 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500987 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500988 unsigned long flags;
Mike Christie26974782007-12-13 12:43:29 -0600989
Mike Christie632248a2009-08-20 15:11:01 -0500990 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
991
Mike Christie26974782007-12-13 12:43:29 -0600992 /* Prevent new scans and make sure scanning is not in progress */
993 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -0500994 spin_lock_irqsave(&session->lock, flags);
995 if (session->target_id == ISCSI_MAX_TARGET) {
996 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600997 mutex_unlock(&ihost->mutex);
998 return;
999 }
Mike Christie79706342008-05-21 15:54:12 -05001000 session->target_id = ISCSI_MAX_TARGET;
1001 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001002 mutex_unlock(&ihost->mutex);
1003
1004 scsi_remove_target(&session->dev);
1005 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -05001006 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
Mike Christie26974782007-12-13 12:43:29 -06001007}
1008
Mike Christie7b8631b2006-01-13 18:05:50 -06001009struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -05001010iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1011 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06001012{
1013 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Mike Christie5d91e202008-05-21 15:54:01 -05001015 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -05001016 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001017 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -05001018 return NULL;
1019
Mike Christie7b8631b2006-01-13 18:05:50 -06001020 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -05001021 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -06001022 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +00001023 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -05001024 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -06001025 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
1026 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -06001027 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -06001028 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001029 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -06001030
Mike Christie6a8a0d32006-06-28 12:00:31 -05001031 /* this is released in the dev's release function */
1032 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -05001033 session->dev.parent = &shost->shost_gendev;
1034 session->dev.release = iscsi_session_release;
1035 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -05001036 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -05001037 session->dd_data = &session[1];
Mike Christie632248a2009-08-20 15:11:01 -05001038
1039 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001040 return session;
1041}
1042EXPORT_SYMBOL_GPL(iscsi_alloc_session);
1043
Mike Christie79706342008-05-21 15:54:12 -05001044static int iscsi_get_next_target_id(struct device *dev, void *data)
1045{
1046 struct iscsi_cls_session *session;
1047 unsigned long flags;
1048 int err = 0;
1049
1050 if (!iscsi_is_session_dev(dev))
1051 return 0;
1052
1053 session = iscsi_dev_to_session(dev);
1054 spin_lock_irqsave(&session->lock, flags);
1055 if (*((unsigned int *) data) == session->target_id)
1056 err = -EEXIST;
1057 spin_unlock_irqrestore(&session->lock, flags);
1058 return err;
1059}
1060
Mike Christie6a8a0d32006-06-28 12:00:31 -05001061int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05001062{
1063 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001064 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -06001065 unsigned long flags;
Mike Christie79706342008-05-21 15:54:12 -05001066 unsigned int id = target_id;
Mike Christie8434aa82006-06-28 12:00:30 -05001067 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -05001068
Mike Christie30a6c652006-04-06 21:13:39 -05001069 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -06001070 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -05001071
1072 if (id == ISCSI_MAX_TARGET) {
1073 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
1074 err = device_for_each_child(&shost->shost_gendev, &id,
1075 iscsi_get_next_target_id);
1076 if (!err)
1077 break;
1078 }
1079
1080 if (id == ISCSI_MAX_TARGET) {
1081 iscsi_cls_session_printk(KERN_ERR, session,
1082 "Too many iscsi targets. Max "
1083 "number of targets is %d.\n",
1084 ISCSI_MAX_TARGET - 1);
Jaswinder Singh Rajput24add1c2009-06-20 13:29:21 +05301085 err = -EOVERFLOW;
Mike Christie79706342008-05-21 15:54:12 -05001086 goto release_host;
1087 }
1088 }
1089 session->target_id = id;
Mike Christie30a6c652006-04-06 21:13:39 -05001090
Kay Sievers71610f52008-12-03 22:41:36 +01001091 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -05001092 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001093 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001094 iscsi_cls_session_printk(KERN_ERR, session,
1095 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001096 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -06001097 }
1098 transport_register_device(&session->dev);
1099
Mike Christie26974782007-12-13 12:43:29 -06001100 spin_lock_irqsave(&sesslock, flags);
1101 list_add(&session->sess_list, &sesslist);
1102 spin_unlock_irqrestore(&sesslock, flags);
1103
Mike Christie26974782007-12-13 12:43:29 -06001104 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -05001105 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001106 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -05001107
Mike Christie8434aa82006-06-28 12:00:30 -05001108release_host:
1109 scsi_host_put(shost);
1110 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -06001111}
Mike Christie8434aa82006-06-28 12:00:30 -05001112EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -06001113
1114/**
Mike Christie8434aa82006-06-28 12:00:30 -05001115 * iscsi_create_session - create iscsi class session
1116 * @shost: scsi host
1117 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -05001118 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -05001119 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -06001120 *
Mike Christie8434aa82006-06-28 12:00:30 -05001121 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001122 */
Mike Christie8434aa82006-06-28 12:00:30 -05001123struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -05001124iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1125 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05001126{
1127 struct iscsi_cls_session *session;
1128
Mike Christie5d91e202008-05-21 15:54:01 -05001129 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -05001130 if (!session)
1131 return NULL;
1132
Mike Christie6a8a0d32006-06-28 12:00:31 -05001133 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -05001134 iscsi_free_session(session);
1135 return NULL;
1136 }
1137 return session;
1138}
1139EXPORT_SYMBOL_GPL(iscsi_create_session);
1140
Mike Christie26974782007-12-13 12:43:29 -06001141static void iscsi_conn_release(struct device *dev)
1142{
1143 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
1144 struct device *parent = conn->dev.parent;
1145
Mike Christie632248a2009-08-20 15:11:01 -05001146 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
Mike Christie26974782007-12-13 12:43:29 -06001147 kfree(conn);
1148 put_device(parent);
1149}
1150
1151static int iscsi_is_conn_dev(const struct device *dev)
1152{
1153 return dev->release == iscsi_conn_release;
1154}
1155
1156static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
1157{
1158 if (!iscsi_is_conn_dev(dev))
1159 return 0;
1160 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
1161}
1162
Mike Christie8434aa82006-06-28 12:00:30 -05001163void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001164{
Mike Christie30a6c652006-04-06 21:13:39 -05001165 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie26974782007-12-13 12:43:29 -06001166 unsigned long flags;
1167 int err;
Mike Christie30a6c652006-04-06 21:13:39 -05001168
Mike Christie632248a2009-08-20 15:11:01 -05001169 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
1170
Mike Christie26974782007-12-13 12:43:29 -06001171 spin_lock_irqsave(&sesslock, flags);
1172 list_del(&session->sess_list);
1173 spin_unlock_irqrestore(&sesslock, flags);
1174
Mike Christie45ab33b2008-03-04 13:26:55 -06001175 /* make sure there are no blocks/unblocks queued */
1176 flush_workqueue(iscsi_eh_timer_workq);
1177 /* make sure the timedout callout is not running */
1178 if (!cancel_delayed_work(&session->recovery_work))
1179 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -06001180 /*
1181 * If we are blocked let commands flow again. The lld or iscsi
1182 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -06001183 * We assume that LLD will not be calling block/unblock while
1184 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -06001185 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001186 spin_lock_irqsave(&session->lock, flags);
1187 session->state = ISCSI_SESSION_FREE;
1188 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -06001189
Mike Christie45ab33b2008-03-04 13:26:55 -06001190 scsi_target_unblock(&session->dev);
1191 /* flush running scans then delete devices */
Mike Christie06d25af2009-03-05 14:46:02 -06001192 scsi_flush_work(shost);
Mike Christie45ab33b2008-03-04 13:26:55 -06001193 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -05001194
Mike Christie26974782007-12-13 12:43:29 -06001195 /* hw iscsi may not have removed all connections from session */
1196 err = device_for_each_child(&session->dev, NULL,
1197 iscsi_iter_destroy_conn_fn);
1198 if (err)
Mike Christie322d7392008-01-31 13:36:52 -06001199 iscsi_cls_session_printk(KERN_ERR, session,
1200 "Could not delete all connections "
1201 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -05001202
Mike Christie7b8631b2006-01-13 18:05:50 -06001203 transport_unregister_device(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001204
1205 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001206 device_del(&session->dev);
1207}
1208EXPORT_SYMBOL_GPL(iscsi_remove_session);
1209
1210void iscsi_free_session(struct iscsi_cls_session *session)
1211{
Mike Christie632248a2009-08-20 15:11:01 -05001212 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
Mike Christie26974782007-12-13 12:43:29 -06001213 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -05001214 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001215}
Mike Christie8434aa82006-06-28 12:00:30 -05001216EXPORT_SYMBOL_GPL(iscsi_free_session);
1217
1218/**
1219 * iscsi_destroy_session - destroy iscsi session
1220 * @session: iscsi_session
1221 *
1222 * Can be called by a LLD or iscsi_transport. There must not be
1223 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -05001224 */
Mike Christie8434aa82006-06-28 12:00:30 -05001225int iscsi_destroy_session(struct iscsi_cls_session *session)
1226{
1227 iscsi_remove_session(session);
Mike Christie632248a2009-08-20 15:11:01 -05001228 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001229 iscsi_free_session(session);
1230 return 0;
1231}
Mike Christie7b8631b2006-01-13 18:05:50 -06001232EXPORT_SYMBOL_GPL(iscsi_destroy_session);
1233
Mike Christie7b8631b2006-01-13 18:05:50 -06001234/**
1235 * iscsi_create_conn - create iscsi class connection
1236 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -05001237 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -06001238 * @cid: connection id
1239 *
1240 * This can be called from a LLD or iscsi_transport. The connection
1241 * is child of the session so cid must be unique for all connections
1242 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -05001243 *
1244 * Since we do not support MCS, cid will normally be zero. In some cases
1245 * for software iscsi we could be trying to preallocate a connection struct
1246 * in which case there could be two connection structs and cid would be
1247 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -05001248 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001249struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05001250iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -06001251{
1252 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -06001253 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -06001254 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -06001255 int err;
1256
Mike Christie5d91e202008-05-21 15:54:01 -05001257 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001258 if (!conn)
1259 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -05001260 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06001261 conn->dd_data = &conn[1];
1262
Mike Christie22a39fb2011-02-16 15:04:33 -06001263 mutex_init(&conn->ep_mutex);
Mike Christie7b8631b2006-01-13 18:05:50 -06001264 INIT_LIST_HEAD(&conn->conn_list);
1265 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -05001266 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001267
1268 /* this is released in the dev's release function */
1269 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -04001270 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -05001271
Kay Sievers71610f52008-12-03 22:41:36 +01001272 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001273 conn->dev.parent = &session->dev;
1274 conn->dev.release = iscsi_conn_release;
1275 err = device_register(&conn->dev);
1276 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001277 iscsi_cls_session_printk(KERN_ERR, session, "could not "
1278 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001279 goto release_parent_ref;
1280 }
1281 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -06001282
1283 spin_lock_irqsave(&connlock, flags);
1284 list_add(&conn->conn_list, &connlist);
Mike Christie26974782007-12-13 12:43:29 -06001285 spin_unlock_irqrestore(&connlock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05001286
1287 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001288 return conn;
1289
1290release_parent_ref:
1291 put_device(&session->dev);
1292free_conn:
1293 kfree(conn);
1294 return NULL;
1295}
1296
1297EXPORT_SYMBOL_GPL(iscsi_create_conn);
1298
1299/**
1300 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -05001301 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -06001302 *
Mike Christie26974782007-12-13 12:43:29 -06001303 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001304 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001305int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1306{
Mike Christie26974782007-12-13 12:43:29 -06001307 unsigned long flags;
1308
1309 spin_lock_irqsave(&connlock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001310 list_del(&conn->conn_list);
1311 spin_unlock_irqrestore(&connlock, flags);
1312
Mike Christie7b8631b2006-01-13 18:05:50 -06001313 transport_unregister_device(&conn->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001314 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001315 device_unregister(&conn->dev);
1316 return 0;
1317}
Mike Christie7b8631b2006-01-13 18:05:50 -06001318EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1319
1320/*
Mike Christie7b8631b2006-01-13 18:05:50 -06001321 * iscsi interface functions
1322 */
Alex Aizman0896b752005-08-04 19:33:07 -07001323static struct iscsi_internal *
1324iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Alex Aizman0896b752005-08-04 19:33:07 -07001326 struct iscsi_internal *priv;
1327 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Alex Aizman0896b752005-08-04 19:33:07 -07001329 spin_lock_irqsave(&iscsi_transport_lock, flags);
1330 list_for_each_entry(priv, &iscsi_transports, list) {
1331 if (tt == priv->iscsi_transport) {
1332 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1333 return priv;
1334 }
1335 }
1336 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1337 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Alex Aizman0896b752005-08-04 19:33:07 -07001340static int
Michael Chan43514772009-06-08 18:14:41 -07001341iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -05001342{
Michael Chan43514772009-06-08 18:14:41 -07001343 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -07001344}
1345
Mike Christie7b7232f2006-02-01 21:06:49 -06001346int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -07001347 char *data, uint32_t data_size)
1348{
1349 struct nlmsghdr *nlh;
1350 struct sk_buff *skb;
1351 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07001352 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05001353 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001354 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1355 data_size);
1356
Mike Christie790f39a2006-05-18 20:31:39 -05001357 priv = iscsi_if_transport_lookup(conn->transport);
1358 if (!priv)
1359 return -EINVAL;
1360
Mike Christie43a145a2006-10-16 18:09:38 -04001361 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001362 if (!skb) {
Mike Christiee5bd7b542008-09-24 11:46:10 -05001363 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06001364 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1365 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001366 return -ENOMEM;
1367 }
1368
Michael Chan43514772009-06-08 18:14:41 -07001369 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001370 ev = NLMSG_DATA(nlh);
1371 memset(ev, 0, sizeof(*ev));
1372 ev->transport_handle = iscsi_handle(conn->transport);
1373 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05001374 ev->r.recv_req.cid = conn->cid;
1375 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001376 pdu = (char*)ev + sizeof(*ev);
1377 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1378 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1379
Michael Chan43514772009-06-08 18:14:41 -07001380 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001381}
1382EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1383
Michael Chan43514772009-06-08 18:14:41 -07001384int iscsi_offload_mesg(struct Scsi_Host *shost,
1385 struct iscsi_transport *transport, uint32_t type,
1386 char *data, uint16_t data_size)
1387{
1388 struct nlmsghdr *nlh;
1389 struct sk_buff *skb;
1390 struct iscsi_uevent *ev;
1391 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
1392
Michael Chana541f842009-07-29 08:49:52 +00001393 skb = alloc_skb(len, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001394 if (!skb) {
1395 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
1396 return -ENOMEM;
1397 }
1398
1399 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1400 ev = NLMSG_DATA(nlh);
1401 memset(ev, 0, sizeof(*ev));
1402 ev->type = type;
1403 ev->transport_handle = iscsi_handle(transport);
1404 switch (type) {
1405 case ISCSI_KEVENT_PATH_REQ:
1406 ev->r.req_path.host_no = shost->host_no;
1407 break;
1408 case ISCSI_KEVENT_IF_DOWN:
1409 ev->r.notify_if_down.host_no = shost->host_no;
1410 break;
1411 }
1412
1413 memcpy((char *)ev + sizeof(*ev), data, data_size);
1414
Michael Chana541f842009-07-29 08:49:52 +00001415 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001416}
1417EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1418
Mike Christiee5bd7b542008-09-24 11:46:10 -05001419void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07001420{
1421 struct nlmsghdr *nlh;
1422 struct sk_buff *skb;
1423 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05001424 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001425 int len = NLMSG_SPACE(sizeof(*ev));
1426
Mike Christie790f39a2006-05-18 20:31:39 -05001427 priv = iscsi_if_transport_lookup(conn->transport);
1428 if (!priv)
1429 return;
1430
Mike Christie43a145a2006-10-16 18:09:38 -04001431 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001432 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001433 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1434 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07001435 return;
1436 }
1437
Michael Chan43514772009-06-08 18:14:41 -07001438 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001439 ev = NLMSG_DATA(nlh);
1440 ev->transport_handle = iscsi_handle(conn->transport);
1441 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07001442 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05001443 ev->r.connerror.cid = conn->cid;
1444 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001445
Michael Chan43514772009-06-08 18:14:41 -07001446 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001447
Mike Christie322d7392008-01-31 13:36:52 -06001448 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1449 error);
Alex Aizman0896b752005-08-04 19:33:07 -07001450}
Mike Christiee5bd7b542008-09-24 11:46:10 -05001451EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07001452
Manish Rangankar17fa5752011-07-25 13:48:52 -05001453void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
1454 enum iscsi_conn_state state)
1455{
1456 struct nlmsghdr *nlh;
1457 struct sk_buff *skb;
1458 struct iscsi_uevent *ev;
1459 struct iscsi_internal *priv;
1460 int len = NLMSG_SPACE(sizeof(*ev));
1461
1462 priv = iscsi_if_transport_lookup(conn->transport);
1463 if (!priv)
1464 return;
1465
1466 skb = alloc_skb(len, GFP_ATOMIC);
1467 if (!skb) {
1468 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1469 "conn login (%d)\n", state);
1470 return;
1471 }
1472
1473 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1474 ev = NLMSG_DATA(nlh);
1475 ev->transport_handle = iscsi_handle(conn->transport);
1476 ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
1477 ev->r.conn_login.state = state;
1478 ev->r.conn_login.cid = conn->cid;
1479 ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
1480 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
1481
1482 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
1483 state);
1484}
1485EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
1486
Alex Aizman0896b752005-08-04 19:33:07 -07001487static int
Michael Chan43514772009-06-08 18:14:41 -07001488iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1489 void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07001490{
1491 struct sk_buff *skb;
1492 struct nlmsghdr *nlh;
1493 int len = NLMSG_SPACE(size);
1494 int flags = multi ? NLM_F_MULTI : 0;
1495 int t = done ? NLMSG_DONE : type;
1496
Mike Christie43a145a2006-10-16 18:09:38 -04001497 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05001498 if (!skb) {
1499 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1500 return -ENOMEM;
1501 }
Alex Aizman0896b752005-08-04 19:33:07 -07001502
Michael Chan43514772009-06-08 18:14:41 -07001503 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001504 nlh->nlmsg_flags = flags;
1505 memcpy(NLMSG_DATA(nlh), payload, size);
Michael Chan43514772009-06-08 18:14:41 -07001506 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001507}
1508
1509static int
Mike Christie5b940ad2006-02-01 21:06:56 -06001510iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07001511{
1512 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1513 struct iscsi_stats *stats;
1514 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06001515 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001516 struct nlmsghdr *nlhstat;
1517 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05001518 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001519 int len = NLMSG_SPACE(sizeof(*ev) +
1520 sizeof(struct iscsi_stats) +
1521 sizeof(struct iscsi_stats_custom) *
1522 ISCSI_STATS_CUSTOM_MAX);
1523 int err = 0;
1524
Mike Christie790f39a2006-05-18 20:31:39 -05001525 priv = iscsi_if_transport_lookup(transport);
1526 if (!priv)
1527 return -EINVAL;
1528
Mike Christieb5c7a122006-04-06 21:13:33 -05001529 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07001530 if (!conn)
1531 return -EEXIST;
1532
1533 do {
1534 int actual_size;
1535
Mike Christie43a145a2006-10-16 18:09:38 -04001536 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001537 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06001538 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1539 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001540 return -ENOMEM;
1541 }
1542
Michael Chan43514772009-06-08 18:14:41 -07001543 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07001544 (len - sizeof(*nlhstat)), 0);
1545 evstat = NLMSG_DATA(nlhstat);
1546 memset(evstat, 0, sizeof(*evstat));
1547 evstat->transport_handle = iscsi_handle(conn->transport);
1548 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05001549 evstat->u.get_stats.cid =
1550 ev->u.get_stats.cid;
1551 evstat->u.get_stats.sid =
1552 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07001553 stats = (struct iscsi_stats *)
1554 ((char*)evstat + sizeof(*evstat));
1555 memset(stats, 0, sizeof(*stats));
1556
Mike Christie7b7232f2006-02-01 21:06:49 -06001557 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07001558 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1559 sizeof(struct iscsi_stats) +
1560 sizeof(struct iscsi_stats_custom) *
1561 stats->custom_length);
1562 actual_size -= sizeof(*nlhstat);
1563 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06001564 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07001565 nlhstat->nlmsg_len = actual_size;
1566
Michael Chan43514772009-06-08 18:14:41 -07001567 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1568 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001569 } while (err < 0 && err != -ECONNREFUSED);
1570
1571 return err;
1572}
1573
Mike Christie53cb8a12006-06-28 12:00:32 -05001574/**
Mike Christie26974782007-12-13 12:43:29 -06001575 * iscsi_session_event - send session destr. completion event
1576 * @session: iscsi class session
1577 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05001578 */
Mike Christie26974782007-12-13 12:43:29 -06001579int iscsi_session_event(struct iscsi_cls_session *session,
1580 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05001581{
1582 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05001583 struct Scsi_Host *shost;
1584 struct iscsi_uevent *ev;
1585 struct sk_buff *skb;
1586 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05001587 int rc, len = NLMSG_SPACE(sizeof(*ev));
1588
Mike Christie26974782007-12-13 12:43:29 -06001589 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05001590 if (!priv)
1591 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05001592 shost = iscsi_session_to_shost(session);
1593
Mike Christie43a145a2006-10-16 18:09:38 -04001594 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001595 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001596 iscsi_cls_session_printk(KERN_ERR, session,
1597 "Cannot notify userspace of session "
1598 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001599 return -ENOMEM;
1600 }
1601
Michael Chan43514772009-06-08 18:14:41 -07001602 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Mike Christie53cb8a12006-06-28 12:00:32 -05001603 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06001604 ev->transport_handle = iscsi_handle(session->transport);
1605
1606 ev->type = event;
1607 switch (event) {
1608 case ISCSI_KEVENT_DESTROY_SESSION:
1609 ev->r.d_session.host_no = shost->host_no;
1610 ev->r.d_session.sid = session->sid;
1611 break;
1612 case ISCSI_KEVENT_CREATE_SESSION:
1613 ev->r.c_session_ret.host_no = shost->host_no;
1614 ev->r.c_session_ret.sid = session->sid;
1615 break;
1616 case ISCSI_KEVENT_UNBIND_SESSION:
1617 ev->r.unbind_session.host_no = shost->host_no;
1618 ev->r.unbind_session.sid = session->sid;
1619 break;
1620 default:
Mike Christie322d7392008-01-31 13:36:52 -06001621 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1622 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06001623 kfree_skb(skb);
1624 return -EINVAL;
1625 }
Mike Christie53cb8a12006-06-28 12:00:32 -05001626
1627 /*
1628 * this will occur if the daemon is not up, so we just warn
1629 * the user and when the daemon is restarted it will handle it
1630 */
Michael Chan43514772009-06-08 18:14:41 -07001631 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001632 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06001633 iscsi_cls_session_printk(KERN_ERR, session,
1634 "Cannot notify userspace of session "
1635 "event %u. Check iscsi daemon\n",
1636 event);
Mike Christie632248a2009-08-20 15:11:01 -05001637
1638 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
1639 event, rc);
Mike Christie53cb8a12006-06-28 12:00:32 -05001640 return rc;
1641}
Mike Christie26974782007-12-13 12:43:29 -06001642EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001643
Alex Aizman0896b752005-08-04 19:33:07 -07001644static int
Mike Christied82ff9be2008-05-21 15:54:13 -05001645iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1646 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
Mike Christie40753ca2008-05-21 15:53:56 -05001647 uint16_t cmds_max, uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001648{
1649 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001650 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06001651 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06001652
Mike Christied82ff9be2008-05-21 15:54:13 -05001653 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06001654 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06001655 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001656 return -ENOMEM;
1657
Mike Christie5e7facb2009-03-05 14:46:06 -06001658 shost = iscsi_session_to_shost(session);
1659 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001660 ev->r.c_session_ret.sid = session->sid;
Mike Christie632248a2009-08-20 15:11:01 -05001661 ISCSI_DBG_TRANS_SESSION(session,
1662 "Completed creating transport session\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001663 return 0;
1664}
1665
1666static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001667iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001668{
Mike Christie7b8631b2006-01-13 18:05:50 -06001669 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001670 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001671
Mike Christieb5c7a122006-04-06 21:13:33 -05001672 session = iscsi_session_lookup(ev->u.c_conn.sid);
1673 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001674 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001675 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001676 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001677 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001678
Mike Christie7b7232f2006-02-01 21:06:49 -06001679 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001680 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001681 iscsi_cls_session_printk(KERN_ERR, session,
1682 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001683 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001684 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001685
Mike Christieb5c7a122006-04-06 21:13:33 -05001686 ev->r.c_conn_ret.sid = session->sid;
1687 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie632248a2009-08-20 15:11:01 -05001688
1689 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001690 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001691}
1692
1693static int
1694iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1695{
Mike Christie7b8631b2006-01-13 18:05:50 -06001696 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001697
Mike Christieb5c7a122006-04-06 21:13:33 -05001698 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001699 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001700 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001701
Mike Christie632248a2009-08-20 15:11:01 -05001702 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001703 if (transport->destroy_conn)
1704 transport->destroy_conn(conn);
Mike Christie632248a2009-08-20 15:11:01 -05001705
Mike Christie7b8631b2006-01-13 18:05:50 -06001706 return 0;
1707}
1708
Mike Christiefd7255f2006-04-06 21:13:36 -05001709static int
1710iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1711{
1712 char *data = (char*)ev + sizeof(*ev);
1713 struct iscsi_cls_conn *conn;
1714 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001715 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001716
1717 session = iscsi_session_lookup(ev->u.set_param.sid);
1718 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1719 if (!conn || !session)
1720 return -EINVAL;
1721
1722 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001723 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001724 sscanf(data, "%d", &value);
Mike Christiefdd46dc2009-11-11 16:34:34 -06001725 session->recovery_tmo = value;
Mike Christie30a6c652006-04-06 21:13:39 -05001726 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001727 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001728 err = transport->set_param(conn, ev->u.set_param.param,
1729 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001730 }
1731
1732 return err;
1733}
1734
Mike Christie10eb0f02009-05-13 17:57:38 -05001735static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1736 struct iscsi_uevent *ev, int msg_type)
1737{
1738 struct iscsi_endpoint *ep;
1739 struct sockaddr *dst_addr;
1740 struct Scsi_Host *shost = NULL;
1741 int non_blocking, err = 0;
1742
1743 if (!transport->ep_connect)
1744 return -EINVAL;
1745
1746 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1747 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1748 if (!shost) {
1749 printk(KERN_ERR "ep connect failed. Could not find "
1750 "host no %u\n",
1751 ev->u.ep_connect_through_host.host_no);
1752 return -ENODEV;
1753 }
1754 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1755 } else
1756 non_blocking = ev->u.ep_connect.non_blocking;
1757
1758 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1759 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1760 if (IS_ERR(ep)) {
1761 err = PTR_ERR(ep);
1762 goto release_host;
1763 }
1764
1765 ev->r.ep_connect_ret.handle = ep->id;
1766release_host:
1767 if (shost)
1768 scsi_host_put(shost);
1769 return err;
1770}
1771
Mike Christie22a39fb2011-02-16 15:04:33 -06001772static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
1773 u64 ep_handle)
1774{
1775 struct iscsi_cls_conn *conn;
1776 struct iscsi_endpoint *ep;
1777
1778 if (!transport->ep_disconnect)
1779 return -EINVAL;
1780
1781 ep = iscsi_lookup_endpoint(ep_handle);
1782 if (!ep)
1783 return -EINVAL;
1784 conn = ep->conn;
1785 if (conn) {
1786 mutex_lock(&conn->ep_mutex);
1787 conn->ep = NULL;
1788 mutex_unlock(&conn->ep_mutex);
1789 }
1790
1791 transport->ep_disconnect(ep);
1792 return 0;
1793}
1794
Mike Christie7b8631b2006-01-13 18:05:50 -06001795static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001796iscsi_if_transport_ep(struct iscsi_transport *transport,
1797 struct iscsi_uevent *ev, int msg_type)
1798{
Mike Christied82ff9be2008-05-21 15:54:13 -05001799 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001800 int rc = 0;
1801
1802 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05001803 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001804 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001805 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001806 break;
1807 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1808 if (!transport->ep_poll)
1809 return -EINVAL;
1810
Mike Christied82ff9be2008-05-21 15:54:13 -05001811 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1812 if (!ep)
1813 return -EINVAL;
1814
1815 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001816 ev->u.ep_poll.timeout_ms);
1817 break;
1818 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie22a39fb2011-02-16 15:04:33 -06001819 rc = iscsi_if_ep_disconnect(transport,
1820 ev->u.ep_disconnect.ep_handle);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001821 break;
1822 }
1823 return rc;
1824}
1825
1826static int
Mike Christie01cb2252006-06-28 12:00:22 -05001827iscsi_tgt_dscvr(struct iscsi_transport *transport,
1828 struct iscsi_uevent *ev)
1829{
Mike Christie2174a042007-05-30 12:57:10 -05001830 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001831 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001832 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001833
1834 if (!transport->tgt_dscvr)
1835 return -EINVAL;
1836
Mike Christie2174a042007-05-30 12:57:10 -05001837 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001838 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05001839 printk(KERN_ERR "target discovery could not find host no %u\n",
1840 ev->u.tgt_dscvr.host_no);
1841 return -ENODEV;
1842 }
1843
1844
Mike Christie01cb2252006-06-28 12:00:22 -05001845 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001846 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1847 ev->u.tgt_dscvr.enable, dst_addr);
1848 scsi_host_put(shost);
1849 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001850}
1851
1852static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001853iscsi_set_host_param(struct iscsi_transport *transport,
1854 struct iscsi_uevent *ev)
1855{
1856 char *data = (char*)ev + sizeof(*ev);
1857 struct Scsi_Host *shost;
1858 int err;
1859
1860 if (!transport->set_host_param)
1861 return -ENOSYS;
1862
1863 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001864 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05001865 printk(KERN_ERR "set_host_param could not find host no %u\n",
1866 ev->u.set_host_param.host_no);
1867 return -ENODEV;
1868 }
1869
1870 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1871 data, ev->u.set_host_param.len);
1872 scsi_host_put(shost);
1873 return err;
1874}
1875
1876static int
Michael Chan43514772009-06-08 18:14:41 -07001877iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1878{
1879 struct Scsi_Host *shost;
1880 struct iscsi_path *params;
1881 int err;
1882
1883 if (!transport->set_path)
1884 return -ENOSYS;
1885
1886 shost = scsi_host_lookup(ev->u.set_path.host_no);
1887 if (!shost) {
1888 printk(KERN_ERR "set path could not find host no %u\n",
1889 ev->u.set_path.host_no);
1890 return -ENODEV;
1891 }
1892
1893 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1894 err = transport->set_path(shost, params);
1895
1896 scsi_host_put(shost);
1897 return err;
1898}
1899
1900static int
Mike Christie56c155b2011-07-25 13:48:37 -05001901iscsi_set_iface_params(struct iscsi_transport *transport,
1902 struct iscsi_uevent *ev)
1903{
1904 char *data = (char *)ev + sizeof(*ev);
1905 struct Scsi_Host *shost;
1906 int err;
1907
1908 if (!transport->set_iface_param)
1909 return -ENOSYS;
1910
1911 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
1912 if (!shost) {
1913 printk(KERN_ERR "set_iface_params could not find host no %u\n",
1914 ev->u.set_iface_params.host_no);
1915 return -ENODEV;
1916 }
1917
1918 err = transport->set_iface_param(shost, data,
1919 ev->u.set_iface_params.count);
1920 scsi_host_put(shost);
1921 return err;
1922}
1923
1924static int
Michael Chan43514772009-06-08 18:14:41 -07001925iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07001926{
1927 int err = 0;
1928 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1929 struct iscsi_transport *transport = NULL;
1930 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001931 struct iscsi_cls_session *session;
1932 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05001933 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001934
Michael Chan43514772009-06-08 18:14:41 -07001935 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1936 *group = ISCSI_NL_GRP_UIP;
1937 else
1938 *group = ISCSI_NL_GRP_ISCSID;
1939
Alex Aizman0896b752005-08-04 19:33:07 -07001940 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1941 if (!priv)
1942 return -EINVAL;
1943 transport = priv->iscsi_transport;
1944
Mike Christie7b7232f2006-02-01 21:06:49 -06001945 if (!try_module_get(transport->owner))
1946 return -EINVAL;
1947
Alex Aizman0896b752005-08-04 19:33:07 -07001948 switch (nlh->nlmsg_type) {
1949 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001950 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001951 ev->u.c_session.initial_cmdsn,
1952 ev->u.c_session.cmds_max,
1953 ev->u.c_session.queue_depth);
1954 break;
1955 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001956 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05001957 if (!ep) {
1958 err = -EINVAL;
1959 break;
1960 }
Mike Christied82ff9be2008-05-21 15:54:13 -05001961
1962 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001963 ev->u.c_bound_session.initial_cmdsn,
1964 ev->u.c_bound_session.cmds_max,
1965 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001966 break;
1967 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001968 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001969 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001970 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001971 else
1972 err = -EINVAL;
1973 break;
1974 case ISCSI_UEVENT_UNBIND_SESSION:
1975 session = iscsi_session_lookup(ev->u.d_session.sid);
1976 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06001977 scsi_queue_work(iscsi_session_to_shost(session),
1978 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06001979 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001980 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001981 break;
1982 case ISCSI_UEVENT_CREATE_CONN:
1983 err = iscsi_if_create_conn(transport, ev);
1984 break;
1985 case ISCSI_UEVENT_DESTROY_CONN:
1986 err = iscsi_if_destroy_conn(transport, ev);
1987 break;
1988 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001989 session = iscsi_session_lookup(ev->u.b_conn.sid);
1990 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001991
Mike Christie22a39fb2011-02-16 15:04:33 -06001992 if (conn && conn->ep)
1993 iscsi_if_ep_disconnect(transport, conn->ep->id);
1994
1995 if (!session || !conn) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001996 err = -EINVAL;
Mike Christie22a39fb2011-02-16 15:04:33 -06001997 break;
1998 }
1999
2000 ev->r.retcode = transport->bind_conn(session, conn,
2001 ev->u.b_conn.transport_eph,
2002 ev->u.b_conn.is_leading);
2003 if (ev->r.retcode || !transport->ep_connect)
2004 break;
2005
2006 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
2007 if (ep) {
2008 ep->conn = conn;
2009
2010 mutex_lock(&conn->ep_mutex);
2011 conn->ep = ep;
2012 mutex_unlock(&conn->ep_mutex);
2013 } else
2014 iscsi_cls_conn_printk(KERN_ERR, conn,
2015 "Could not set ep conn "
2016 "binding\n");
Alex Aizman0896b752005-08-04 19:33:07 -07002017 break;
2018 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05002019 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07002020 break;
2021 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05002022 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06002023 if (conn)
2024 ev->r.retcode = transport->start_conn(conn);
2025 else
2026 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07002027 break;
2028 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05002029 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06002030 if (conn)
2031 transport->stop_conn(conn, ev->u.stop_conn.flag);
2032 else
2033 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07002034 break;
2035 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05002036 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06002037 if (conn)
2038 ev->r.retcode = transport->send_pdu(conn,
2039 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
2040 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
2041 ev->u.send_pdu.data_size);
2042 else
2043 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07002044 break;
2045 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06002046 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07002047 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05002048 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
2049 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
2050 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05002051 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05002052 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
2053 break;
Mike Christie01cb2252006-06-28 12:00:22 -05002054 case ISCSI_UEVENT_TGT_DSCVR:
2055 err = iscsi_tgt_dscvr(transport, ev);
2056 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05002057 case ISCSI_UEVENT_SET_HOST_PARAM:
2058 err = iscsi_set_host_param(transport, ev);
2059 break;
Michael Chan43514772009-06-08 18:14:41 -07002060 case ISCSI_UEVENT_PATH_UPDATE:
2061 err = iscsi_set_path(transport, ev);
2062 break;
Mike Christie56c155b2011-07-25 13:48:37 -05002063 case ISCSI_UEVENT_SET_IFACE_PARAMS:
2064 err = iscsi_set_iface_params(transport, ev);
2065 break;
Alex Aizman0896b752005-08-04 19:33:07 -07002066 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05002067 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07002068 break;
2069 }
2070
Mike Christie7b7232f2006-02-01 21:06:49 -06002071 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07002072 return err;
2073}
2074
Mike Christieb5c7a122006-04-06 21:13:33 -05002075/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002076 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
2077 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05002078 */
Alex Aizman0896b752005-08-04 19:33:07 -07002079static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002080iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07002081{
Arjan van de Ven0b950672006-01-11 13:16:10 +01002082 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002083 while (skb->len >= NLMSG_SPACE(0)) {
2084 int err;
2085 uint32_t rlen;
2086 struct nlmsghdr *nlh;
2087 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07002088 uint32_t group;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002089
2090 nlh = nlmsg_hdr(skb);
2091 if (nlh->nlmsg_len < sizeof(*nlh) ||
2092 skb->len < nlh->nlmsg_len) {
2093 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06002094 }
Mike Christieee7f8e42006-02-01 21:07:01 -06002095
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002096 ev = NLMSG_DATA(nlh);
2097 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
2098 if (rlen > skb->len)
2099 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07002100
Michael Chan43514772009-06-08 18:14:41 -07002101 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002102 if (err) {
2103 ev->type = ISCSI_KEVENT_IF_ERROR;
2104 ev->iferror = err;
2105 }
2106 do {
2107 /*
2108 * special case for GET_STATS:
2109 * on success - sending reply and stats from
2110 * inside of if_recv_msg(),
2111 * on error - fall through.
2112 */
2113 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07002114 break;
Michael Chan43514772009-06-08 18:14:41 -07002115 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002116 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
2117 } while (err < 0 && err != -ECONNREFUSED);
2118 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07002119 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01002120 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002121}
2122
Mike Christiefd7255f2006-04-06 21:13:36 -05002123#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01002124struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05002125 __ATTR(_name,_mode,_show,_store)
2126
Alex Aizman0896b752005-08-04 19:33:07 -07002127/*
2128 * iSCSI connection attrs
2129 */
Mike Christiea54a52c2006-06-28 12:00:23 -05002130#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002132show_conn_param_##param(struct device *dev, \
2133 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002134{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002135 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002136 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05002137 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002138}
2139
Mike Christiea54a52c2006-06-28 12:00:23 -05002140#define iscsi_conn_attr(field, param) \
2141 iscsi_conn_attr_show(param) \
2142static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05002143 NULL);
2144
Mike Christiea54a52c2006-06-28 12:00:23 -05002145iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
2146iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
2147iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
2148iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
2149iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
2150iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
2151iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
Mike Christiea54a52c2006-06-28 12:00:23 -05002152iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
2153iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06002154iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
2155iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Mike Christie289324b2011-02-16 15:04:37 -06002157#define iscsi_conn_ep_attr_show(param) \
2158static ssize_t show_conn_ep_param_##param(struct device *dev, \
2159 struct device_attribute *attr,\
2160 char *buf) \
2161{ \
2162 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
2163 struct iscsi_transport *t = conn->transport; \
2164 struct iscsi_endpoint *ep; \
2165 ssize_t rc; \
2166 \
2167 /* \
2168 * Need to make sure ep_disconnect does not free the LLD's \
2169 * interconnect resources while we are trying to read them. \
2170 */ \
2171 mutex_lock(&conn->ep_mutex); \
2172 ep = conn->ep; \
2173 if (!ep && t->ep_connect) { \
2174 mutex_unlock(&conn->ep_mutex); \
2175 return -ENOTCONN; \
2176 } \
2177 \
2178 if (ep) \
2179 rc = t->get_ep_param(ep, param, buf); \
2180 else \
2181 rc = t->get_conn_param(conn, param, buf); \
2182 mutex_unlock(&conn->ep_mutex); \
2183 return rc; \
2184}
2185
2186#define iscsi_conn_ep_attr(field, param) \
2187 iscsi_conn_ep_attr_show(param) \
2188static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
2189 show_conn_ep_param_##param, NULL);
2190
2191iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
2192iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
2193
Mike Christie3128c6c2011-07-25 13:48:42 -05002194static struct attribute *iscsi_conn_attrs[] = {
2195 &dev_attr_conn_max_recv_dlength.attr,
2196 &dev_attr_conn_max_xmit_dlength.attr,
2197 &dev_attr_conn_header_digest.attr,
2198 &dev_attr_conn_data_digest.attr,
2199 &dev_attr_conn_ifmarker.attr,
2200 &dev_attr_conn_ofmarker.attr,
2201 &dev_attr_conn_address.attr,
2202 &dev_attr_conn_port.attr,
2203 &dev_attr_conn_exp_statsn.attr,
2204 &dev_attr_conn_persistent_address.attr,
2205 &dev_attr_conn_persistent_port.attr,
2206 &dev_attr_conn_ping_tmo.attr,
2207 &dev_attr_conn_recv_tmo.attr,
2208 NULL,
2209};
2210
2211static mode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
2212 struct attribute *attr, int i)
2213{
2214 struct device *cdev = container_of(kobj, struct device, kobj);
2215 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
2216 struct iscsi_transport *t = conn->transport;
2217 int param;
2218
2219 if (attr == &dev_attr_conn_max_recv_dlength.attr)
2220 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
2221 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
2222 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
2223 else if (attr == &dev_attr_conn_header_digest.attr)
2224 param = ISCSI_PARAM_HDRDGST_EN;
2225 else if (attr == &dev_attr_conn_data_digest.attr)
2226 param = ISCSI_PARAM_DATADGST_EN;
2227 else if (attr == &dev_attr_conn_ifmarker.attr)
2228 param = ISCSI_PARAM_IFMARKER_EN;
2229 else if (attr == &dev_attr_conn_ofmarker.attr)
2230 param = ISCSI_PARAM_OFMARKER_EN;
2231 else if (attr == &dev_attr_conn_address.attr)
2232 param = ISCSI_PARAM_CONN_ADDRESS;
2233 else if (attr == &dev_attr_conn_port.attr)
2234 param = ISCSI_PARAM_CONN_PORT;
2235 else if (attr == &dev_attr_conn_exp_statsn.attr)
2236 param = ISCSI_PARAM_EXP_STATSN;
2237 else if (attr == &dev_attr_conn_persistent_address.attr)
2238 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
2239 else if (attr == &dev_attr_conn_persistent_port.attr)
2240 param = ISCSI_PARAM_PERSISTENT_PORT;
2241 else if (attr == &dev_attr_conn_ping_tmo.attr)
2242 param = ISCSI_PARAM_PING_TMO;
2243 else if (attr == &dev_attr_conn_recv_tmo.attr)
2244 param = ISCSI_PARAM_RECV_TMO;
2245 else {
2246 WARN_ONCE(1, "Invalid conn attr");
2247 return 0;
2248 }
2249
2250 return t->attr_is_visible(ISCSI_PARAM, param);
2251}
2252
2253static struct attribute_group iscsi_conn_group = {
2254 .attrs = iscsi_conn_attrs,
2255 .is_visible = iscsi_conn_attr_is_visible,
2256};
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258/*
Alex Aizman0896b752005-08-04 19:33:07 -07002259 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 */
Mike Christieb2c64162007-05-30 12:57:16 -05002261#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002263show_session_param_##param(struct device *dev, \
2264 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002265{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002266 struct iscsi_cls_session *session = \
2267 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002268 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05002269 \
2270 if (perm && !capable(CAP_SYS_ADMIN)) \
2271 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05002272 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002273}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Mike Christieb2c64162007-05-30 12:57:16 -05002275#define iscsi_session_attr(field, param, perm) \
2276 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05002277static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05002278 NULL);
Mike Christieb2c64162007-05-30 12:57:16 -05002279iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
2280iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
2281iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
2282iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
2283iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
2284iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
2285iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
2286iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
2287iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
2288iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
2289iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
2290iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
2291iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
2292iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06002293iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
2294iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
2295iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002296iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05002297iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05302298iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
2299iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05002300
Mike Christie6eabafb2008-01-31 13:36:43 -06002301static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01002302show_priv_session_state(struct device *dev, struct device_attribute *attr,
2303 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06002304{
Tony Jonesee959b02008-02-22 00:13:36 +01002305 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06002306 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
2307}
2308static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
2309 NULL);
2310
Mike Christiefd7255f2006-04-06 21:13:36 -05002311#define iscsi_priv_session_attr_show(field, format) \
2312static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002313show_priv_session_##field(struct device *dev, \
2314 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002315{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002316 struct iscsi_cls_session *session = \
2317 iscsi_dev_to_session(dev->parent); \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302318 if (session->field == -1) \
2319 return sprintf(buf, "off\n"); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002320 return sprintf(buf, format"\n", session->field); \
2321}
2322
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302323#define iscsi_priv_session_attr_store(field) \
2324static ssize_t \
2325store_priv_session_##field(struct device *dev, \
2326 struct device_attribute *attr, \
2327 const char *buf, size_t count) \
2328{ \
2329 int val; \
2330 char *cp; \
2331 struct iscsi_cls_session *session = \
2332 iscsi_dev_to_session(dev->parent); \
2333 if ((session->state == ISCSI_SESSION_FREE) || \
2334 (session->state == ISCSI_SESSION_FAILED)) \
2335 return -EBUSY; \
2336 if (strncmp(buf, "off", 3) == 0) \
2337 session->field = -1; \
2338 else { \
2339 val = simple_strtoul(buf, &cp, 0); \
2340 if (*cp != '\0' && *cp != '\n') \
2341 return -EINVAL; \
2342 session->field = val; \
2343 } \
2344 return count; \
2345}
2346
2347#define iscsi_priv_session_rw_attr(field, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002348 iscsi_priv_session_attr_show(field, format) \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302349 iscsi_priv_session_attr_store(field) \
Vasiliy Kulikov523f3c82011-02-04 15:24:14 +03002350static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302351 show_priv_session_##field, \
2352 store_priv_session_##field)
2353iscsi_priv_session_rw_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05002354
Mike Christie1d063c12011-07-25 13:48:43 -05002355static struct attribute *iscsi_session_attrs[] = {
2356 &dev_attr_sess_initial_r2t.attr,
2357 &dev_attr_sess_max_outstanding_r2t.attr,
2358 &dev_attr_sess_immediate_data.attr,
2359 &dev_attr_sess_first_burst_len.attr,
2360 &dev_attr_sess_max_burst_len.attr,
2361 &dev_attr_sess_data_pdu_in_order.attr,
2362 &dev_attr_sess_data_seq_in_order.attr,
2363 &dev_attr_sess_erl.attr,
2364 &dev_attr_sess_targetname.attr,
2365 &dev_attr_sess_tpgt.attr,
2366 &dev_attr_sess_password.attr,
2367 &dev_attr_sess_password_in.attr,
2368 &dev_attr_sess_username.attr,
2369 &dev_attr_sess_username_in.attr,
2370 &dev_attr_sess_fast_abort.attr,
2371 &dev_attr_sess_abort_tmo.attr,
2372 &dev_attr_sess_lu_reset_tmo.attr,
2373 &dev_attr_sess_tgt_reset_tmo.attr,
2374 &dev_attr_sess_ifacename.attr,
2375 &dev_attr_sess_initiatorname.attr,
2376 &dev_attr_sess_targetalias.attr,
2377 &dev_attr_priv_sess_recovery_tmo.attr,
2378 &dev_attr_priv_sess_state.attr,
2379 NULL,
2380};
2381
2382static mode_t iscsi_session_attr_is_visible(struct kobject *kobj,
2383 struct attribute *attr, int i)
2384{
2385 struct device *cdev = container_of(kobj, struct device, kobj);
2386 struct iscsi_cls_session *session = transport_class_to_session(cdev);
2387 struct iscsi_transport *t = session->transport;
2388 int param;
2389
2390 if (attr == &dev_attr_sess_initial_r2t.attr)
2391 param = ISCSI_PARAM_INITIAL_R2T_EN;
2392 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
2393 param = ISCSI_PARAM_MAX_R2T;
2394 else if (attr == &dev_attr_sess_immediate_data.attr)
2395 param = ISCSI_PARAM_IMM_DATA_EN;
2396 else if (attr == &dev_attr_sess_first_burst_len.attr)
2397 param = ISCSI_PARAM_FIRST_BURST;
2398 else if (attr == &dev_attr_sess_max_burst_len.attr)
2399 param = ISCSI_PARAM_MAX_BURST;
2400 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
2401 param = ISCSI_PARAM_PDU_INORDER_EN;
2402 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
2403 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
2404 else if (attr == &dev_attr_sess_erl.attr)
2405 param = ISCSI_PARAM_ERL;
2406 else if (attr == &dev_attr_sess_targetname.attr)
2407 param = ISCSI_PARAM_TARGET_NAME;
2408 else if (attr == &dev_attr_sess_tpgt.attr)
2409 param = ISCSI_PARAM_TPGT;
2410 else if (attr == &dev_attr_sess_password.attr)
2411 param = ISCSI_PARAM_USERNAME;
2412 else if (attr == &dev_attr_sess_password_in.attr)
2413 param = ISCSI_PARAM_USERNAME_IN;
2414 else if (attr == &dev_attr_sess_username.attr)
2415 param = ISCSI_PARAM_PASSWORD;
2416 else if (attr == &dev_attr_sess_username_in.attr)
2417 param = ISCSI_PARAM_PASSWORD_IN;
2418 else if (attr == &dev_attr_sess_fast_abort.attr)
2419 param = ISCSI_PARAM_FAST_ABORT;
2420 else if (attr == &dev_attr_sess_abort_tmo.attr)
2421 param = ISCSI_PARAM_ABORT_TMO;
2422 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
2423 param = ISCSI_PARAM_LU_RESET_TMO;
2424 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
2425 param = ISCSI_PARAM_TGT_RESET_TMO;
2426 else if (attr == &dev_attr_sess_ifacename.attr)
2427 param = ISCSI_PARAM_IFACE_NAME;
2428 else if (attr == &dev_attr_sess_initiatorname.attr)
2429 param = ISCSI_PARAM_INITIATOR_NAME;
2430 else if (attr == &dev_attr_sess_targetalias.attr)
2431 param = ISCSI_PARAM_TARGET_ALIAS;
2432 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
2433 return S_IRUGO | S_IWUSR;
2434 else if (attr == &dev_attr_priv_sess_state.attr)
2435 return S_IRUGO;
2436 else {
2437 WARN_ONCE(1, "Invalid session attr");
2438 return 0;
2439 }
2440
2441 return t->attr_is_visible(ISCSI_PARAM, param);
2442}
2443
2444static struct attribute_group iscsi_session_group = {
2445 .attrs = iscsi_session_attrs,
2446 .is_visible = iscsi_session_attr_is_visible,
2447};
2448
Mike Christie1819dc82007-05-30 12:57:08 -05002449/*
2450 * iSCSI host attrs
2451 */
2452#define iscsi_host_attr_show(param) \
2453static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002454show_host_param_##param(struct device *dev, \
2455 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05002456{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002457 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05002458 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
2459 return priv->iscsi_transport->get_host_param(shost, param, buf); \
2460}
2461
2462#define iscsi_host_attr(field, param) \
2463 iscsi_host_attr_show(param) \
2464static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
2465 NULL);
2466
Mike Christied8196ed2007-05-30 12:57:25 -05002467iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002468iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05002469iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05002470iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002471
Mike Christief27fb2e2011-07-25 13:48:45 -05002472static struct attribute *iscsi_host_attrs[] = {
2473 &dev_attr_host_netdev.attr,
2474 &dev_attr_host_hwaddress.attr,
2475 &dev_attr_host_ipaddress.attr,
2476 &dev_attr_host_initiatorname.attr,
2477 NULL,
2478};
2479
2480static mode_t iscsi_host_attr_is_visible(struct kobject *kobj,
2481 struct attribute *attr, int i)
2482{
2483 struct device *cdev = container_of(kobj, struct device, kobj);
2484 struct Scsi_Host *shost = transport_class_to_shost(cdev);
2485 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
2486 int param;
2487
2488 if (attr == &dev_attr_host_netdev.attr)
2489 param = ISCSI_HOST_PARAM_NETDEV_NAME;
2490 else if (attr == &dev_attr_host_hwaddress.attr)
2491 param = ISCSI_HOST_PARAM_HWADDRESS;
2492 else if (attr == &dev_attr_host_ipaddress.attr)
2493 param = ISCSI_HOST_PARAM_IPADDRESS;
2494 else if (attr == &dev_attr_host_initiatorname.attr)
2495 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
2496 else {
2497 WARN_ONCE(1, "Invalid host attr");
2498 return 0;
2499 }
2500
2501 return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
2502}
2503
2504static struct attribute_group iscsi_host_group = {
2505 .attrs = iscsi_host_attrs,
2506 .is_visible = iscsi_host_attr_is_visible,
2507};
Mike Christie1819dc82007-05-30 12:57:08 -05002508
Alex Aizman0896b752005-08-04 19:33:07 -07002509static int iscsi_session_match(struct attribute_container *cont,
2510 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Mike Christie7b8631b2006-01-13 18:05:50 -06002512 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07002514 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Alex Aizman0896b752005-08-04 19:33:07 -07002516 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return 0;
2518
Mike Christie7b8631b2006-01-13 18:05:50 -06002519 session = iscsi_dev_to_session(dev);
2520 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07002521 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 return 0;
2523
Alex Aizman0896b752005-08-04 19:33:07 -07002524 priv = to_iscsi_internal(shost->transportt);
2525 if (priv->session_cont.ac.class != &iscsi_session_class.class)
2526 return 0;
2527
2528 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
Alex Aizman0896b752005-08-04 19:33:07 -07002531static int iscsi_conn_match(struct attribute_container *cont,
2532 struct device *dev)
2533{
Mike Christie7b8631b2006-01-13 18:05:50 -06002534 struct iscsi_cls_session *session;
2535 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07002536 struct Scsi_Host *shost;
2537 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Alex Aizman0896b752005-08-04 19:33:07 -07002539 if (!iscsi_is_conn_dev(dev))
2540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Mike Christie7b8631b2006-01-13 18:05:50 -06002542 conn = iscsi_dev_to_conn(dev);
2543 session = iscsi_dev_to_session(conn->dev.parent);
2544 shost = iscsi_session_to_shost(session);
2545
Alex Aizman0896b752005-08-04 19:33:07 -07002546 if (!shost->transportt)
2547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Alex Aizman0896b752005-08-04 19:33:07 -07002549 priv = to_iscsi_internal(shost->transportt);
2550 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
2551 return 0;
2552
2553 return &priv->conn_cont.ac == cont;
2554}
2555
Mike Christie30a6c652006-04-06 21:13:39 -05002556static int iscsi_host_match(struct attribute_container *cont,
2557 struct device *dev)
2558{
2559 struct Scsi_Host *shost;
2560 struct iscsi_internal *priv;
2561
2562 if (!scsi_is_host_device(dev))
2563 return 0;
2564
2565 shost = dev_to_shost(dev);
2566 if (!shost->transportt ||
2567 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
2568 return 0;
2569
2570 priv = to_iscsi_internal(shost->transportt);
2571 return &priv->t.host_attrs.ac == cont;
2572}
2573
Mike Christie7b8631b2006-01-13 18:05:50 -06002574struct scsi_transport_template *
2575iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07002576{
2577 struct iscsi_internal *priv;
2578 unsigned long flags;
Mike Christief27fb2e2011-07-25 13:48:45 -05002579 int err;
Alex Aizman0896b752005-08-04 19:33:07 -07002580
2581 BUG_ON(!tt);
2582
2583 priv = iscsi_if_transport_lookup(tt);
2584 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002585 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002586
Jes Sorensen24669f752006-01-16 10:31:18 -05002587 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07002588 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002589 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002590 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07002591 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05002592 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06002593 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07002594
Tony Jonesee959b02008-02-22 00:13:36 +01002595 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01002596 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01002597 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07002598 if (err)
2599 goto free_priv;
2600
Tony Jonesee959b02008-02-22 00:13:36 +01002601 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07002602 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01002603 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07002604
Mike Christie30a6c652006-04-06 21:13:39 -05002605 /* host parameters */
Mike Christie30a6c652006-04-06 21:13:39 -05002606 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
2607 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christief27fb2e2011-07-25 13:48:45 -05002608 priv->t.host_attrs.ac.grp = &iscsi_host_group;
Mike Christie32c6e1b2008-05-21 15:53:58 -05002609 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05002610 transport_container_register(&priv->t.host_attrs);
2611
Alex Aizman0896b752005-08-04 19:33:07 -07002612 /* connection parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07002613 priv->conn_cont.ac.class = &iscsi_connection_class.class;
2614 priv->conn_cont.ac.match = iscsi_conn_match;
Mike Christie3128c6c2011-07-25 13:48:42 -05002615 priv->conn_cont.ac.grp = &iscsi_conn_group;
Alex Aizman0896b752005-08-04 19:33:07 -07002616 transport_container_register(&priv->conn_cont);
2617
Alex Aizman0896b752005-08-04 19:33:07 -07002618 /* session parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07002619 priv->session_cont.ac.class = &iscsi_session_class.class;
2620 priv->session_cont.ac.match = iscsi_session_match;
Mike Christie1d063c12011-07-25 13:48:43 -05002621 priv->session_cont.ac.grp = &iscsi_session_group;
Alex Aizman0896b752005-08-04 19:33:07 -07002622 transport_container_register(&priv->session_cont);
2623
Alex Aizman0896b752005-08-04 19:33:07 -07002624 spin_lock_irqsave(&iscsi_transport_lock, flags);
2625 list_add(&priv->list, &iscsi_transports);
2626 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Alex Aizman0896b752005-08-04 19:33:07 -07002628 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06002629 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Tony Jonesee959b02008-02-22 00:13:36 +01002631unregister_dev:
2632 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05002633 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002634free_priv:
2635 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06002636 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637}
Alex Aizman0896b752005-08-04 19:33:07 -07002638EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Alex Aizman0896b752005-08-04 19:33:07 -07002640int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
Alex Aizman0896b752005-08-04 19:33:07 -07002642 struct iscsi_internal *priv;
2643 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Alex Aizman0896b752005-08-04 19:33:07 -07002645 BUG_ON(!tt);
2646
Arjan van de Ven0b950672006-01-11 13:16:10 +01002647 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002648
2649 priv = iscsi_if_transport_lookup(tt);
2650 BUG_ON (!priv);
2651
Alex Aizman0896b752005-08-04 19:33:07 -07002652 spin_lock_irqsave(&iscsi_transport_lock, flags);
2653 list_del(&priv->list);
2654 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2655
2656 transport_container_unregister(&priv->conn_cont);
2657 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05002658 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07002659
Tony Jonesee959b02008-02-22 00:13:36 +01002660 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
2661 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002662 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002663
2664 return 0;
2665}
2666EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668static __init int iscsi_transport_init(void)
2669{
Alex Aizman0896b752005-08-04 19:33:07 -07002670 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Meelis Roos09492602006-12-17 12:10:26 -06002672 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05002673 ISCSI_TRANSPORT_VERSION);
2674
Mike Christie41be1442007-02-28 17:32:18 -06002675 atomic_set(&iscsi_session_nr, 0);
2676
Alex Aizman0896b752005-08-04 19:33:07 -07002677 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 if (err)
2679 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07002680
Mike Christied82ff9be2008-05-21 15:54:13 -05002681 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002682 if (err)
2683 goto unregister_transport_class;
2684
Mike Christie8d079132011-07-25 13:48:40 -05002685 err = class_register(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002686 if (err)
2687 goto unregister_endpoint_class;
2688
Mike Christie8d079132011-07-25 13:48:40 -05002689 err = transport_class_register(&iscsi_host_class);
2690 if (err)
2691 goto unregister_iface_class;
2692
Mike Christie30a6c652006-04-06 21:13:39 -05002693 err = transport_class_register(&iscsi_connection_class);
2694 if (err)
2695 goto unregister_host_class;
2696
Alex Aizman0896b752005-08-04 19:33:07 -07002697 err = transport_class_register(&iscsi_session_class);
2698 if (err)
2699 goto unregister_conn_class;
2700
Mike Christied82ff9be2008-05-21 15:54:13 -05002701 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2702 NULL, THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07002703 if (!nls) {
2704 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04002705 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07002706 }
2707
Mike Christied8bf5412007-12-13 12:43:27 -06002708 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2709 if (!iscsi_eh_timer_workq)
2710 goto release_nls;
2711
Mike Christie43a145a2006-10-16 18:09:38 -04002712 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07002713
Mike Christied8bf5412007-12-13 12:43:27 -06002714release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002715 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002716unregister_session_class:
2717 transport_class_unregister(&iscsi_session_class);
2718unregister_conn_class:
2719 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002720unregister_host_class:
2721 transport_class_unregister(&iscsi_host_class);
Mike Christie8d079132011-07-25 13:48:40 -05002722unregister_iface_class:
2723 class_unregister(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002724unregister_endpoint_class:
2725 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002726unregister_transport_class:
2727 class_unregister(&iscsi_transport_class);
2728 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
2731static void __exit iscsi_transport_exit(void)
2732{
Mike Christied8bf5412007-12-13 12:43:27 -06002733 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002734 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002735 transport_class_unregister(&iscsi_connection_class);
2736 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002737 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002738 class_unregister(&iscsi_endpoint_class);
Mike Christie8d079132011-07-25 13:48:40 -05002739 class_unregister(&iscsi_iface_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002740 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
2743module_init(iscsi_transport_init);
2744module_exit(iscsi_transport_exit);
2745
Alex Aizman0896b752005-08-04 19:33:07 -07002746MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2747 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2748 "Alex Aizman <itn780@yahoo.com>");
2749MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05002751MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
Stephen Hemminger058548a2010-12-09 09:37:56 -08002752MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);