blob: 13f90515ff6eabfb8916a9267ba514259730e5a4 [file] [log] [blame]
Alex Aizman0896b752005-08-04 19:33:07 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * iSCSI transport class definitions
3 *
4 * Copyright (C) IBM Corporation, 2004
Alex Aizman0896b752005-08-04 19:33:07 -07005 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23#include <linux/module.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010024#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Alex Aizman0896b752005-08-04 19:33:07 -070026#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <scsi/scsi.h>
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_transport.h>
31#include <scsi/scsi_transport_iscsi.h>
Alex Aizman0896b752005-08-04 19:33:07 -070032#include <scsi/iscsi_if.h>
Mike Christiec01be6d2010-07-22 16:59:49 +053033#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Mike Christie4c2133c2008-06-16 10:11:34 -050035#define ISCSI_TRANSPORT_VERSION "2.0-870"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Mike Christie632248a2009-08-20 15:11:01 -050037static int dbg_session;
38module_param_named(debug_session, dbg_session, int,
39 S_IRUGO | S_IWUSR);
40MODULE_PARM_DESC(debug_session,
41 "Turn on debugging for sessions in scsi_transport_iscsi "
42 "module. Set to 1 to turn on, and zero to turn off. Default "
43 "is off.");
44
45static int dbg_conn;
46module_param_named(debug_conn, dbg_conn, int,
47 S_IRUGO | S_IWUSR);
48MODULE_PARM_DESC(debug_conn,
49 "Turn on debugging for connections in scsi_transport_iscsi "
50 "module. Set to 1 to turn on, and zero to turn off. Default "
51 "is off.");
52
53#define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
54 do { \
55 if (dbg_session) \
56 iscsi_cls_session_printk(KERN_INFO, _session, \
57 "%s: " dbg_fmt, \
58 __func__, ##arg); \
59 } while (0);
60
61#define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
62 do { \
63 if (dbg_conn) \
64 iscsi_cls_conn_printk(KERN_INFO, _conn, \
65 "%s: " dbg_fmt, \
66 __func__, ##arg); \
67 } while (0);
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069struct iscsi_internal {
70 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070071 struct iscsi_transport *iscsi_transport;
72 struct list_head list;
Tony Jonesee959b02008-02-22 00:13:36 +010073 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050074
Alex Aizman0896b752005-08-04 19:33:07 -070075 struct transport_container conn_cont;
Alex Aizman0896b752005-08-04 19:33:07 -070076 struct transport_container session_cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Mike Christie41be1442007-02-28 17:32:18 -060079static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060080static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050081
Alex Aizman0896b752005-08-04 19:33:07 -070082/*
83 * list of registered transports and lock that must
84 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010085 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070086 */
87static LIST_HEAD(iscsi_transports);
88static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Alex Aizman0896b752005-08-04 19:33:07 -070090#define to_iscsi_internal(tmpl) \
91 container_of(tmpl, struct iscsi_internal, t)
92
Tony Jonesee959b02008-02-22 00:13:36 +010093#define dev_to_iscsi_internal(_dev) \
94 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -070095
Tony Jonesee959b02008-02-22 00:13:36 +010096static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -070097{
Tony Jonesee959b02008-02-22 00:13:36 +010098 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -070099 kfree(priv);
100}
101
102/*
103 * iscsi_transport_class represents the iscsi_transports that are
104 * registered.
105 */
106static struct class iscsi_transport_class = {
107 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +0100108 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -0700109};
110
111static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100112show_transport_handle(struct device *dev, struct device_attribute *attr,
113 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -0700114{
Tony Jonesee959b02008-02-22 00:13:36 +0100115 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Mike Christie762e2bf2005-09-12 21:01:46 -0500116 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -0700117}
Tony Jonesee959b02008-02-22 00:13:36 +0100118static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700119
120#define show_transport_attr(name, format) \
121static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100122show_transport_##name(struct device *dev, \
123 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -0700124{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100125 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Alex Aizman0896b752005-08-04 19:33:07 -0700126 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
127} \
Tony Jonesee959b02008-02-22 00:13:36 +0100128static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700129
130show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700131
132static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100133 &dev_attr_handle.attr,
134 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700135 NULL,
136};
137
138static struct attribute_group iscsi_transport_group = {
139 .attrs = iscsi_transport_attrs,
140};
141
Mike Christied82ff9be2008-05-21 15:54:13 -0500142/*
143 * iSCSI endpoint attrs
144 */
145#define iscsi_dev_to_endpoint(_dev) \
146 container_of(_dev, struct iscsi_endpoint, dev)
147
148#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
149struct device_attribute dev_attr_##_prefix##_##_name = \
150 __ATTR(_name,_mode,_show,_store)
151
152static void iscsi_endpoint_release(struct device *dev)
153{
154 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
155 kfree(ep);
156}
157
158static struct class iscsi_endpoint_class = {
159 .name = "iscsi_endpoint",
160 .dev_release = iscsi_endpoint_release,
161};
162
163static ssize_t
164show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
165{
166 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500167 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500168}
169static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
170
171static struct attribute *iscsi_endpoint_attrs[] = {
172 &dev_attr_ep_handle.attr,
173 NULL,
174};
175
176static struct attribute_group iscsi_endpoint_group = {
177 .attrs = iscsi_endpoint_attrs,
178};
179
180#define ISCSI_MAX_EPID -1
181
182static int iscsi_match_epid(struct device *dev, void *data)
183{
184 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500185 uint64_t *epid = (uint64_t *) data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500186
187 return *epid == ep->id;
188}
189
190struct iscsi_endpoint *
191iscsi_create_endpoint(int dd_size)
192{
193 struct device *dev;
194 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500195 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500196 int err;
197
198 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400199 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500200 iscsi_match_epid);
201 if (!dev)
202 break;
203 }
204 if (id == ISCSI_MAX_EPID) {
205 printk(KERN_ERR "Too many connections. Max supported %u\n",
206 ISCSI_MAX_EPID - 1);
207 return NULL;
208 }
209
210 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
211 if (!ep)
212 return NULL;
213
214 ep->id = id;
215 ep->dev.class = &iscsi_endpoint_class;
Kay Sievers71610f52008-12-03 22:41:36 +0100216 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500217 err = device_register(&ep->dev);
218 if (err)
219 goto free_ep;
220
221 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
222 if (err)
223 goto unregister_dev;
224
225 if (dd_size)
226 ep->dd_data = &ep[1];
227 return ep;
228
229unregister_dev:
230 device_unregister(&ep->dev);
231 return NULL;
232
233free_ep:
234 kfree(ep);
235 return NULL;
236}
237EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
238
239void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
240{
241 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
242 device_unregister(&ep->dev);
243}
244EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
245
246struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
247{
Mike Christief80f8682008-06-16 10:11:35 -0500248 struct iscsi_endpoint *ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500249 struct device *dev;
250
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400251 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500252 iscsi_match_epid);
253 if (!dev)
254 return NULL;
255
Mike Christief80f8682008-06-16 10:11:35 -0500256 ep = iscsi_dev_to_endpoint(dev);
257 /*
258 * we can drop this now because the interface will prevent
259 * removals and lookups from racing.
260 */
261 put_device(dev);
262 return ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500263}
264EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
265
Mike Christie8d079132011-07-25 13:48:40 -0500266/*
267 * Interface to display network param to sysfs
268 */
269
270static void iscsi_iface_release(struct device *dev)
271{
272 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
273 struct device *parent = iface->dev.parent;
274
275 kfree(iface);
276 put_device(parent);
277}
278
279
280static struct class iscsi_iface_class = {
281 .name = "iscsi_iface",
282 .dev_release = iscsi_iface_release,
283};
284
285#define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
286struct device_attribute dev_attr_##_prefix##_##_name = \
287 __ATTR(_name, _mode, _show, _store)
288
289/* iface attrs show */
290#define iscsi_iface_attr_show(type, name, param_type, param) \
291static ssize_t \
292show_##type##_##name(struct device *dev, struct device_attribute *attr, \
293 char *buf) \
294{ \
295 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
296 struct iscsi_transport *t = iface->transport; \
297 return t->get_iface_param(iface, param_type, param, buf); \
298} \
299
300#define iscsi_iface_net_attr(type, name, param) \
301 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
302static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
303
304/* generic read only ipvi4 attribute */
305iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
306iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
307iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
308iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
309
310/* generic read only ipv6 attribute */
311iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
312iscsi_iface_net_attr(ipv6_iface, link_local_addr, ISCSI_NET_PARAM_IPV6_LINKLOCAL);
313iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
314iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
315 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
316iscsi_iface_net_attr(ipv6_iface, linklocal_autocfg,
317 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
318
319/* common read only iface attribute */
320iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
321iscsi_iface_net_attr(iface, vlan, ISCSI_NET_PARAM_VLAN_ID);
Mike Christie4223b9e2011-07-25 13:48:47 -0500322iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
323iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
Mike Christie8d079132011-07-25 13:48:40 -0500324
325static mode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
326 struct attribute *attr, int i)
327{
328 struct device *dev = container_of(kobj, struct device, kobj);
329 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
330 struct iscsi_transport *t = iface->transport;
Mike Christieb78dbba2011-07-25 13:48:44 -0500331 int param;
Mike Christie8d079132011-07-25 13:48:40 -0500332
333 if (attr == &dev_attr_iface_enabled.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500334 param = ISCSI_NET_PARAM_IFACE_ENABLE;
Mike Christie8d079132011-07-25 13:48:40 -0500335 else if (attr == &dev_attr_iface_vlan.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500336 param = ISCSI_NET_PARAM_VLAN_ID;
Mike Christie4223b9e2011-07-25 13:48:47 -0500337 else if (attr == &dev_attr_iface_vlan_priority.attr)
338 param = ISCSI_NET_PARAM_VLAN_PRIORITY;
339 else if (attr == &dev_attr_iface_vlan_enabled.attr)
340 param = ISCSI_NET_PARAM_VLAN_ENABLED;
Mike Christieb78dbba2011-07-25 13:48:44 -0500341 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
Mike Christie8d079132011-07-25 13:48:40 -0500342 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500343 param = ISCSI_NET_PARAM_IPV4_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500344 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500345 param = ISCSI_NET_PARAM_IPV4_GW;
Mike Christie8d079132011-07-25 13:48:40 -0500346 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500347 param = ISCSI_NET_PARAM_IPV4_SUBNET;
Mike Christie8d079132011-07-25 13:48:40 -0500348 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500349 param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
350 else
351 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500352 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
353 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500354 param = ISCSI_NET_PARAM_IPV6_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500355 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500356 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
Mike Christie8d079132011-07-25 13:48:40 -0500357 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500358 param = ISCSI_NET_PARAM_IPV6_ROUTER;
Mike Christie8d079132011-07-25 13:48:40 -0500359 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500360 param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
Mike Christie8d079132011-07-25 13:48:40 -0500361 else if (attr == &dev_attr_ipv6_iface_linklocal_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500362 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
363 else
364 return 0;
365 } else {
366 WARN_ONCE(1, "Invalid iface attr");
367 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500368 }
369
Mike Christieb78dbba2011-07-25 13:48:44 -0500370 return t->attr_is_visible(ISCSI_NET_PARAM, param);
Mike Christie8d079132011-07-25 13:48:40 -0500371}
372
373static struct attribute *iscsi_iface_attrs[] = {
374 &dev_attr_iface_enabled.attr,
375 &dev_attr_iface_vlan.attr,
Mike Christie4223b9e2011-07-25 13:48:47 -0500376 &dev_attr_iface_vlan_priority.attr,
377 &dev_attr_iface_vlan_enabled.attr,
Mike Christie8d079132011-07-25 13:48:40 -0500378 &dev_attr_ipv4_iface_ipaddress.attr,
379 &dev_attr_ipv4_iface_gateway.attr,
380 &dev_attr_ipv4_iface_subnet.attr,
381 &dev_attr_ipv4_iface_bootproto.attr,
382 &dev_attr_ipv6_iface_ipaddress.attr,
383 &dev_attr_ipv6_iface_link_local_addr.attr,
384 &dev_attr_ipv6_iface_router_addr.attr,
385 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
386 &dev_attr_ipv6_iface_linklocal_autocfg.attr,
387 NULL,
388};
389
390static struct attribute_group iscsi_iface_group = {
391 .attrs = iscsi_iface_attrs,
392 .is_visible = iscsi_iface_attr_is_visible,
393};
394
395struct iscsi_iface *
396iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
397 uint32_t iface_type, uint32_t iface_num, int dd_size)
398{
399 struct iscsi_iface *iface;
400 int err;
401
402 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
403 if (!iface)
404 return NULL;
405
406 iface->transport = transport;
407 iface->iface_type = iface_type;
408 iface->iface_num = iface_num;
409 iface->dev.release = iscsi_iface_release;
410 iface->dev.class = &iscsi_iface_class;
411 /* parent reference released in iscsi_iface_release */
412 iface->dev.parent = get_device(&shost->shost_gendev);
413 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
414 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
415 iface_num);
416 else
417 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
418 iface_num);
419
420 err = device_register(&iface->dev);
421 if (err)
422 goto free_iface;
423
424 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
425 if (err)
426 goto unreg_iface;
427
428 if (dd_size)
429 iface->dd_data = &iface[1];
430 return iface;
431
432unreg_iface:
433 device_unregister(&iface->dev);
434 return NULL;
435
436free_iface:
437 put_device(iface->dev.parent);
438 kfree(iface);
439 return NULL;
440}
441EXPORT_SYMBOL_GPL(iscsi_create_iface);
442
443void iscsi_destroy_iface(struct iscsi_iface *iface)
444{
445 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
446 device_unregister(&iface->dev);
447}
448EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
449
Mike Christie30a6c652006-04-06 21:13:39 -0500450static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100451 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500452{
453 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500454 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500455
456 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -0600457 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -0500458 mutex_init(&ihost->mutex);
Mike Christie30a6c652006-04-06 21:13:39 -0500459 return 0;
460}
461
462static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
463 "iscsi_host",
464 iscsi_setup_host,
Mike Christie06d25af2009-03-05 14:46:02 -0600465 NULL,
Mike Christie30a6c652006-04-06 21:13:39 -0500466 NULL);
467
Alex Aizman0896b752005-08-04 19:33:07 -0700468static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
469 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 NULL,
471 NULL,
472 NULL);
473
Alex Aizman0896b752005-08-04 19:33:07 -0700474static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
475 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 NULL,
477 NULL,
478 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700479
480static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100481static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700482
Mike Christie7b7232f2006-02-01 21:06:49 -0600483static LIST_HEAD(sesslist);
484static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700485static LIST_HEAD(connlist);
486static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Mike Christieb5c7a122006-04-06 21:13:33 -0500488static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
489{
490 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
491 return sess->sid;
492}
493
494/*
495 * Returns the matching session to a given sid
496 */
497static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600498{
499 unsigned long flags;
500 struct iscsi_cls_session *sess;
501
502 spin_lock_irqsave(&sesslock, flags);
503 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500504 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600505 spin_unlock_irqrestore(&sesslock, flags);
506 return sess;
507 }
508 }
509 spin_unlock_irqrestore(&sesslock, flags);
510 return NULL;
511}
512
Mike Christieb5c7a122006-04-06 21:13:33 -0500513/*
514 * Returns the matching connection to a given sid / cid tuple
515 */
516static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600517{
518 unsigned long flags;
519 struct iscsi_cls_conn *conn;
520
521 spin_lock_irqsave(&connlock, flags);
522 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500523 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600524 spin_unlock_irqrestore(&connlock, flags);
525 return conn;
526 }
527 }
528 spin_unlock_irqrestore(&connlock, flags);
529 return NULL;
530}
531
Mike Christie7b8631b2006-01-13 18:05:50 -0600532/*
533 * The following functions can be used by LLDs that allocate
534 * their own scsi_hosts or by software iscsi LLDs
535 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600536static struct {
537 int value;
538 char *name;
539} iscsi_session_state_names[] = {
540 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
541 { ISCSI_SESSION_FAILED, "FAILED" },
542 { ISCSI_SESSION_FREE, "FREE" },
543};
544
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200545static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600546{
547 int i;
548 char *name = NULL;
549
550 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
551 if (iscsi_session_state_names[i].value == state) {
552 name = iscsi_session_state_names[i].name;
553 break;
554 }
555 }
556 return name;
557}
558
559int iscsi_session_chkready(struct iscsi_cls_session *session)
560{
561 unsigned long flags;
562 int err;
563
564 spin_lock_irqsave(&session->lock, flags);
565 switch (session->state) {
566 case ISCSI_SESSION_LOGGED_IN:
567 err = 0;
568 break;
569 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -0500570 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600571 break;
572 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -0500573 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -0600574 break;
575 default:
576 err = DID_NO_CONNECT << 16;
577 break;
578 }
579 spin_unlock_irqrestore(&session->lock, flags);
580 return err;
581}
582EXPORT_SYMBOL_GPL(iscsi_session_chkready);
583
Mike Christie7b8631b2006-01-13 18:05:50 -0600584static void iscsi_session_release(struct device *dev)
585{
586 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600587 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Mike Christie7b8631b2006-01-13 18:05:50 -0600589 shost = iscsi_session_to_shost(session);
590 scsi_host_put(shost);
Mike Christie632248a2009-08-20 15:11:01 -0500591 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600592 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600593}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Mike Christie7b8631b2006-01-13 18:05:50 -0600595static int iscsi_is_session_dev(const struct device *dev)
596{
597 return dev->release == iscsi_session_release;
598}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Mike Christiea4804cd2008-05-21 15:54:00 -0500600static int iscsi_iter_session_fn(struct device *dev, void *data)
601{
602 void (* fn) (struct iscsi_cls_session *) = data;
603
604 if (!iscsi_is_session_dev(dev))
605 return 0;
606 fn(iscsi_dev_to_session(dev));
607 return 0;
608}
609
610void iscsi_host_for_each_session(struct Scsi_Host *shost,
611 void (*fn)(struct iscsi_cls_session *))
612{
613 device_for_each_child(&shost->shost_gendev, fn,
614 iscsi_iter_session_fn);
615}
616EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
617
Mike Christie8aae18a2008-01-31 13:36:48 -0600618/**
619 * iscsi_scan_finished - helper to report when running scans are done
620 * @shost: scsi host
621 * @time: scan run time
622 *
623 * This function can be used by drives like qla4xxx to report to the scsi
624 * layer when the scans it kicked off at module load time are done.
625 */
626int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
627{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500628 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600629 /*
630 * qla4xxx will have kicked off some session unblocks before calling
631 * scsi_scan_host, so just wait for them to complete.
632 */
633 return !atomic_read(&ihost->nr_scans);
634}
635EXPORT_SYMBOL_GPL(iscsi_scan_finished);
636
Mike Christie79706342008-05-21 15:54:12 -0500637struct iscsi_scan_data {
638 unsigned int channel;
639 unsigned int id;
640 unsigned int lun;
641};
642
643static int iscsi_user_scan_session(struct device *dev, void *data)
644{
645 struct iscsi_scan_data *scan_data = data;
646 struct iscsi_cls_session *session;
647 struct Scsi_Host *shost;
648 struct iscsi_cls_host *ihost;
649 unsigned long flags;
650 unsigned int id;
651
652 if (!iscsi_is_session_dev(dev))
653 return 0;
654
655 session = iscsi_dev_to_session(dev);
Mike Christie632248a2009-08-20 15:11:01 -0500656
657 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
658
Mike Christie79706342008-05-21 15:54:12 -0500659 shost = iscsi_session_to_shost(session);
660 ihost = shost->shost_data;
661
662 mutex_lock(&ihost->mutex);
663 spin_lock_irqsave(&session->lock, flags);
664 if (session->state != ISCSI_SESSION_LOGGED_IN) {
665 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie632248a2009-08-20 15:11:01 -0500666 goto user_scan_exit;
Mike Christie79706342008-05-21 15:54:12 -0500667 }
668 id = session->target_id;
669 spin_unlock_irqrestore(&session->lock, flags);
670
671 if (id != ISCSI_MAX_TARGET) {
672 if ((scan_data->channel == SCAN_WILD_CARD ||
673 scan_data->channel == 0) &&
674 (scan_data->id == SCAN_WILD_CARD ||
675 scan_data->id == id))
676 scsi_scan_target(&session->dev, 0, id,
677 scan_data->lun, 1);
678 }
Mike Christie632248a2009-08-20 15:11:01 -0500679
680user_scan_exit:
Mike Christie79706342008-05-21 15:54:12 -0500681 mutex_unlock(&ihost->mutex);
Mike Christie632248a2009-08-20 15:11:01 -0500682 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
Mike Christie79706342008-05-21 15:54:12 -0500683 return 0;
684}
685
Mike Christie30a6c652006-04-06 21:13:39 -0500686static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
687 uint id, uint lun)
688{
Mike Christie79706342008-05-21 15:54:12 -0500689 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500690
Mike Christie79706342008-05-21 15:54:12 -0500691 scan_data.channel = channel;
692 scan_data.id = id;
693 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -0500694
Mike Christie79706342008-05-21 15:54:12 -0500695 return device_for_each_child(&shost->shost_gendev, &scan_data,
696 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -0500697}
698
Mike Christiebd976f62008-01-31 13:36:46 -0600699static void iscsi_scan_session(struct work_struct *work)
700{
701 struct iscsi_cls_session *session =
702 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600703 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500704 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500705 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600706
Mike Christie79706342008-05-21 15:54:12 -0500707 scan_data.channel = 0;
708 scan_data.id = SCAN_WILD_CARD;
709 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -0600710
Mike Christie79706342008-05-21 15:54:12 -0500711 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -0600712 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600713}
714
Mike Christiec01be6d2010-07-22 16:59:49 +0530715/**
716 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
Randy Dunlape6d4ef42010-08-14 13:05:41 -0700717 * @cmd: scsi cmd passed to scsi eh handler
Mike Christiec01be6d2010-07-22 16:59:49 +0530718 *
719 * If the session is down this function will wait for the recovery
720 * timer to fire or for the session to be logged back in. If the
721 * recovery timer fires then FAST_IO_FAIL is returned. The caller
722 * should pass this error value to the scsi eh.
723 */
724int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
725{
726 struct iscsi_cls_session *session =
727 starget_to_session(scsi_target(cmd->device));
728 unsigned long flags;
729 int ret = 0;
730
731 spin_lock_irqsave(&session->lock, flags);
732 while (session->state != ISCSI_SESSION_LOGGED_IN) {
733 if (session->state == ISCSI_SESSION_FREE) {
734 ret = FAST_IO_FAIL;
735 break;
736 }
737 spin_unlock_irqrestore(&session->lock, flags);
738 msleep(1000);
739 spin_lock_irqsave(&session->lock, flags);
740 }
741 spin_unlock_irqrestore(&session->lock, flags);
742 return ret;
743}
744EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
745
David Howellsc4028952006-11-22 14:57:56 +0000746static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500747{
David Howellsc4028952006-11-22 14:57:56 +0000748 struct iscsi_cls_session *session =
749 container_of(work, struct iscsi_cls_session,
750 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600751 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500752
Mike Christie322d7392008-01-31 13:36:52 -0600753 iscsi_cls_session_printk(KERN_INFO, session,
754 "session recovery timed out after %d secs\n",
755 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500756
Mike Christie6eabafb2008-01-31 13:36:43 -0600757 spin_lock_irqsave(&session->lock, flags);
758 switch (session->state) {
759 case ISCSI_SESSION_FAILED:
760 session->state = ISCSI_SESSION_FREE;
761 break;
762 case ISCSI_SESSION_LOGGED_IN:
763 case ISCSI_SESSION_FREE:
764 /* we raced with the unblock's flush */
765 spin_unlock_irqrestore(&session->lock, flags);
766 return;
767 }
768 spin_unlock_irqrestore(&session->lock, flags);
769
Mike Christie30a6c652006-04-06 21:13:39 -0500770 if (session->transport->session_recovery_timedout)
771 session->transport->session_recovery_timedout(session);
772
Mike Christie632248a2009-08-20 15:11:01 -0500773 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500774 scsi_target_unblock(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500775 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -0500776}
777
Mike Christie45ab33b2008-03-04 13:26:55 -0600778static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500779{
Mike Christie45ab33b2008-03-04 13:26:55 -0600780 struct iscsi_cls_session *session =
781 container_of(work, struct iscsi_cls_session,
782 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600783 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500784 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600785 unsigned long flags;
786
Mike Christie632248a2009-08-20 15:11:01 -0500787 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
Mike Christie45ab33b2008-03-04 13:26:55 -0600788 /*
789 * The recovery and unblock work get run from the same workqueue,
790 * so try to cancel it if it was going to run after this unblock.
791 */
792 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600793 spin_lock_irqsave(&session->lock, flags);
794 session->state = ISCSI_SESSION_LOGGED_IN;
795 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600796 /* start IO */
797 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600798 /*
799 * Only do kernel scanning if the driver is properly hooked into
800 * the async scanning code (drivers like iscsi_tcp do login and
801 * scanning from userspace).
802 */
803 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -0600804 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -0600805 atomic_inc(&ihost->nr_scans);
806 }
Mike Christie632248a2009-08-20 15:11:01 -0500807 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600808}
Mike Christie45ab33b2008-03-04 13:26:55 -0600809
810/**
811 * iscsi_unblock_session - set a session as logged in and start IO.
812 * @session: iscsi session
813 *
814 * Mark a session as ready to accept IO.
815 */
816void iscsi_unblock_session(struct iscsi_cls_session *session)
817{
818 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
819 /*
820 * make sure all the events have completed before tell the driver
821 * it is safe
822 */
823 flush_workqueue(iscsi_eh_timer_workq);
824}
Mike Christie30a6c652006-04-06 21:13:39 -0500825EXPORT_SYMBOL_GPL(iscsi_unblock_session);
826
Mike Christie45ab33b2008-03-04 13:26:55 -0600827static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500828{
Mike Christie45ab33b2008-03-04 13:26:55 -0600829 struct iscsi_cls_session *session =
830 container_of(work, struct iscsi_cls_session,
831 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600832 unsigned long flags;
833
Mike Christie632248a2009-08-20 15:11:01 -0500834 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -0600835 spin_lock_irqsave(&session->lock, flags);
836 session->state = ISCSI_SESSION_FAILED;
837 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500838 scsi_target_block(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -0500839 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
Mike Christiefdd46dc2009-11-11 16:34:34 -0600840 if (session->recovery_tmo >= 0)
841 queue_delayed_work(iscsi_eh_timer_workq,
842 &session->recovery_work,
843 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500844}
Mike Christie45ab33b2008-03-04 13:26:55 -0600845
846void iscsi_block_session(struct iscsi_cls_session *session)
847{
848 queue_work(iscsi_eh_timer_workq, &session->block_work);
849}
Mike Christie30a6c652006-04-06 21:13:39 -0500850EXPORT_SYMBOL_GPL(iscsi_block_session);
851
Mike Christie26974782007-12-13 12:43:29 -0600852static void __iscsi_unbind_session(struct work_struct *work)
853{
854 struct iscsi_cls_session *session =
855 container_of(work, struct iscsi_cls_session,
856 unbind_work);
857 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500858 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500859 unsigned long flags;
Mike Christie26974782007-12-13 12:43:29 -0600860
Mike Christie632248a2009-08-20 15:11:01 -0500861 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
862
Mike Christie26974782007-12-13 12:43:29 -0600863 /* Prevent new scans and make sure scanning is not in progress */
864 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -0500865 spin_lock_irqsave(&session->lock, flags);
866 if (session->target_id == ISCSI_MAX_TARGET) {
867 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600868 mutex_unlock(&ihost->mutex);
869 return;
870 }
Mike Christie79706342008-05-21 15:54:12 -0500871 session->target_id = ISCSI_MAX_TARGET;
872 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600873 mutex_unlock(&ihost->mutex);
874
875 scsi_remove_target(&session->dev);
876 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -0500877 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
Mike Christie26974782007-12-13 12:43:29 -0600878}
879
Mike Christie7b8631b2006-01-13 18:05:50 -0600880struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500881iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
882 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600883{
884 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Mike Christie5d91e202008-05-21 15:54:01 -0500886 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -0500887 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600888 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500889 return NULL;
890
Mike Christie7b8631b2006-01-13 18:05:50 -0600891 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500892 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -0600893 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +0000894 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500895 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -0600896 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
897 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -0600898 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -0600899 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -0600900 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -0600901
Mike Christie6a8a0d32006-06-28 12:00:31 -0500902 /* this is released in the dev's release function */
903 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500904 session->dev.parent = &shost->shost_gendev;
905 session->dev.release = iscsi_session_release;
906 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -0500907 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -0500908 session->dd_data = &session[1];
Mike Christie632248a2009-08-20 15:11:01 -0500909
910 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500911 return session;
912}
913EXPORT_SYMBOL_GPL(iscsi_alloc_session);
914
Mike Christie79706342008-05-21 15:54:12 -0500915static int iscsi_get_next_target_id(struct device *dev, void *data)
916{
917 struct iscsi_cls_session *session;
918 unsigned long flags;
919 int err = 0;
920
921 if (!iscsi_is_session_dev(dev))
922 return 0;
923
924 session = iscsi_dev_to_session(dev);
925 spin_lock_irqsave(&session->lock, flags);
926 if (*((unsigned int *) data) == session->target_id)
927 err = -EEXIST;
928 spin_unlock_irqrestore(&session->lock, flags);
929 return err;
930}
931
Mike Christie6a8a0d32006-06-28 12:00:31 -0500932int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500933{
934 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500935 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600936 unsigned long flags;
Mike Christie79706342008-05-21 15:54:12 -0500937 unsigned int id = target_id;
Mike Christie8434aa82006-06-28 12:00:30 -0500938 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500939
Mike Christie30a6c652006-04-06 21:13:39 -0500940 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600941 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -0500942
943 if (id == ISCSI_MAX_TARGET) {
944 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
945 err = device_for_each_child(&shost->shost_gendev, &id,
946 iscsi_get_next_target_id);
947 if (!err)
948 break;
949 }
950
951 if (id == ISCSI_MAX_TARGET) {
952 iscsi_cls_session_printk(KERN_ERR, session,
953 "Too many iscsi targets. Max "
954 "number of targets is %d.\n",
955 ISCSI_MAX_TARGET - 1);
Jaswinder Singh Rajput24add1c2009-06-20 13:29:21 +0530956 err = -EOVERFLOW;
Mike Christie79706342008-05-21 15:54:12 -0500957 goto release_host;
958 }
959 }
960 session->target_id = id;
Mike Christie30a6c652006-04-06 21:13:39 -0500961
Kay Sievers71610f52008-12-03 22:41:36 +0100962 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500963 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600964 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600965 iscsi_cls_session_printk(KERN_ERR, session,
966 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500967 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600968 }
969 transport_register_device(&session->dev);
970
Mike Christie26974782007-12-13 12:43:29 -0600971 spin_lock_irqsave(&sesslock, flags);
972 list_add(&session->sess_list, &sesslist);
973 spin_unlock_irqrestore(&sesslock, flags);
974
Mike Christie26974782007-12-13 12:43:29 -0600975 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -0500976 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500977 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500978
Mike Christie8434aa82006-06-28 12:00:30 -0500979release_host:
980 scsi_host_put(shost);
981 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600982}
Mike Christie8434aa82006-06-28 12:00:30 -0500983EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600984
985/**
Mike Christie8434aa82006-06-28 12:00:30 -0500986 * iscsi_create_session - create iscsi class session
987 * @shost: scsi host
988 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -0500989 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -0500990 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600991 *
Mike Christie8434aa82006-06-28 12:00:30 -0500992 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500993 */
Mike Christie8434aa82006-06-28 12:00:30 -0500994struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500995iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
996 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500997{
998 struct iscsi_cls_session *session;
999
Mike Christie5d91e202008-05-21 15:54:01 -05001000 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -05001001 if (!session)
1002 return NULL;
1003
Mike Christie6a8a0d32006-06-28 12:00:31 -05001004 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -05001005 iscsi_free_session(session);
1006 return NULL;
1007 }
1008 return session;
1009}
1010EXPORT_SYMBOL_GPL(iscsi_create_session);
1011
Mike Christie26974782007-12-13 12:43:29 -06001012static void iscsi_conn_release(struct device *dev)
1013{
1014 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
1015 struct device *parent = conn->dev.parent;
1016
Mike Christie632248a2009-08-20 15:11:01 -05001017 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
Mike Christie26974782007-12-13 12:43:29 -06001018 kfree(conn);
1019 put_device(parent);
1020}
1021
1022static int iscsi_is_conn_dev(const struct device *dev)
1023{
1024 return dev->release == iscsi_conn_release;
1025}
1026
1027static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
1028{
1029 if (!iscsi_is_conn_dev(dev))
1030 return 0;
1031 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
1032}
1033
Mike Christie8434aa82006-06-28 12:00:30 -05001034void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001035{
Mike Christie30a6c652006-04-06 21:13:39 -05001036 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie26974782007-12-13 12:43:29 -06001037 unsigned long flags;
1038 int err;
Mike Christie30a6c652006-04-06 21:13:39 -05001039
Mike Christie632248a2009-08-20 15:11:01 -05001040 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
1041
Mike Christie26974782007-12-13 12:43:29 -06001042 spin_lock_irqsave(&sesslock, flags);
1043 list_del(&session->sess_list);
1044 spin_unlock_irqrestore(&sesslock, flags);
1045
Mike Christie45ab33b2008-03-04 13:26:55 -06001046 /* make sure there are no blocks/unblocks queued */
1047 flush_workqueue(iscsi_eh_timer_workq);
1048 /* make sure the timedout callout is not running */
1049 if (!cancel_delayed_work(&session->recovery_work))
1050 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -06001051 /*
1052 * If we are blocked let commands flow again. The lld or iscsi
1053 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -06001054 * We assume that LLD will not be calling block/unblock while
1055 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -06001056 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001057 spin_lock_irqsave(&session->lock, flags);
1058 session->state = ISCSI_SESSION_FREE;
1059 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -06001060
Mike Christie45ab33b2008-03-04 13:26:55 -06001061 scsi_target_unblock(&session->dev);
1062 /* flush running scans then delete devices */
Mike Christie06d25af2009-03-05 14:46:02 -06001063 scsi_flush_work(shost);
Mike Christie45ab33b2008-03-04 13:26:55 -06001064 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -05001065
Mike Christie26974782007-12-13 12:43:29 -06001066 /* hw iscsi may not have removed all connections from session */
1067 err = device_for_each_child(&session->dev, NULL,
1068 iscsi_iter_destroy_conn_fn);
1069 if (err)
Mike Christie322d7392008-01-31 13:36:52 -06001070 iscsi_cls_session_printk(KERN_ERR, session,
1071 "Could not delete all connections "
1072 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -05001073
Mike Christie7b8631b2006-01-13 18:05:50 -06001074 transport_unregister_device(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001075
1076 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001077 device_del(&session->dev);
1078}
1079EXPORT_SYMBOL_GPL(iscsi_remove_session);
1080
1081void iscsi_free_session(struct iscsi_cls_session *session)
1082{
Mike Christie632248a2009-08-20 15:11:01 -05001083 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
Mike Christie26974782007-12-13 12:43:29 -06001084 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -05001085 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001086}
Mike Christie8434aa82006-06-28 12:00:30 -05001087EXPORT_SYMBOL_GPL(iscsi_free_session);
1088
1089/**
1090 * iscsi_destroy_session - destroy iscsi session
1091 * @session: iscsi_session
1092 *
1093 * Can be called by a LLD or iscsi_transport. There must not be
1094 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -05001095 */
Mike Christie8434aa82006-06-28 12:00:30 -05001096int iscsi_destroy_session(struct iscsi_cls_session *session)
1097{
1098 iscsi_remove_session(session);
Mike Christie632248a2009-08-20 15:11:01 -05001099 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001100 iscsi_free_session(session);
1101 return 0;
1102}
Mike Christie7b8631b2006-01-13 18:05:50 -06001103EXPORT_SYMBOL_GPL(iscsi_destroy_session);
1104
Mike Christie7b8631b2006-01-13 18:05:50 -06001105/**
1106 * iscsi_create_conn - create iscsi class connection
1107 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -05001108 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -06001109 * @cid: connection id
1110 *
1111 * This can be called from a LLD or iscsi_transport. The connection
1112 * is child of the session so cid must be unique for all connections
1113 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -05001114 *
1115 * Since we do not support MCS, cid will normally be zero. In some cases
1116 * for software iscsi we could be trying to preallocate a connection struct
1117 * in which case there could be two connection structs and cid would be
1118 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -05001119 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001120struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05001121iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -06001122{
1123 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -06001124 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -06001125 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -06001126 int err;
1127
Mike Christie5d91e202008-05-21 15:54:01 -05001128 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001129 if (!conn)
1130 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -05001131 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06001132 conn->dd_data = &conn[1];
1133
Mike Christie22a39fb2011-02-16 15:04:33 -06001134 mutex_init(&conn->ep_mutex);
Mike Christie7b8631b2006-01-13 18:05:50 -06001135 INIT_LIST_HEAD(&conn->conn_list);
1136 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -05001137 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001138
1139 /* this is released in the dev's release function */
1140 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -04001141 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -05001142
Kay Sievers71610f52008-12-03 22:41:36 +01001143 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001144 conn->dev.parent = &session->dev;
1145 conn->dev.release = iscsi_conn_release;
1146 err = device_register(&conn->dev);
1147 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001148 iscsi_cls_session_printk(KERN_ERR, session, "could not "
1149 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001150 goto release_parent_ref;
1151 }
1152 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -06001153
1154 spin_lock_irqsave(&connlock, flags);
1155 list_add(&conn->conn_list, &connlist);
Mike Christie26974782007-12-13 12:43:29 -06001156 spin_unlock_irqrestore(&connlock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05001157
1158 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001159 return conn;
1160
1161release_parent_ref:
1162 put_device(&session->dev);
1163free_conn:
1164 kfree(conn);
1165 return NULL;
1166}
1167
1168EXPORT_SYMBOL_GPL(iscsi_create_conn);
1169
1170/**
1171 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -05001172 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -06001173 *
Mike Christie26974782007-12-13 12:43:29 -06001174 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001175 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001176int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1177{
Mike Christie26974782007-12-13 12:43:29 -06001178 unsigned long flags;
1179
1180 spin_lock_irqsave(&connlock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001181 list_del(&conn->conn_list);
1182 spin_unlock_irqrestore(&connlock, flags);
1183
Mike Christie7b8631b2006-01-13 18:05:50 -06001184 transport_unregister_device(&conn->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001185 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001186 device_unregister(&conn->dev);
1187 return 0;
1188}
Mike Christie7b8631b2006-01-13 18:05:50 -06001189EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1190
1191/*
Mike Christie7b8631b2006-01-13 18:05:50 -06001192 * iscsi interface functions
1193 */
Alex Aizman0896b752005-08-04 19:33:07 -07001194static struct iscsi_internal *
1195iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Alex Aizman0896b752005-08-04 19:33:07 -07001197 struct iscsi_internal *priv;
1198 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Alex Aizman0896b752005-08-04 19:33:07 -07001200 spin_lock_irqsave(&iscsi_transport_lock, flags);
1201 list_for_each_entry(priv, &iscsi_transports, list) {
1202 if (tt == priv->iscsi_transport) {
1203 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1204 return priv;
1205 }
1206 }
1207 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1208 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Alex Aizman0896b752005-08-04 19:33:07 -07001211static int
Michael Chan43514772009-06-08 18:14:41 -07001212iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -05001213{
Michael Chan43514772009-06-08 18:14:41 -07001214 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -07001215}
1216
Mike Christie7b7232f2006-02-01 21:06:49 -06001217int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -07001218 char *data, uint32_t data_size)
1219{
1220 struct nlmsghdr *nlh;
1221 struct sk_buff *skb;
1222 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07001223 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05001224 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001225 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
1226 data_size);
1227
Mike Christie790f39a2006-05-18 20:31:39 -05001228 priv = iscsi_if_transport_lookup(conn->transport);
1229 if (!priv)
1230 return -EINVAL;
1231
Mike Christie43a145a2006-10-16 18:09:38 -04001232 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001233 if (!skb) {
Mike Christiee5bd7b52008-09-24 11:46:10 -05001234 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06001235 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
1236 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001237 return -ENOMEM;
1238 }
1239
Michael Chan43514772009-06-08 18:14:41 -07001240 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001241 ev = NLMSG_DATA(nlh);
1242 memset(ev, 0, sizeof(*ev));
1243 ev->transport_handle = iscsi_handle(conn->transport);
1244 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05001245 ev->r.recv_req.cid = conn->cid;
1246 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001247 pdu = (char*)ev + sizeof(*ev);
1248 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
1249 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
1250
Michael Chan43514772009-06-08 18:14:41 -07001251 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001252}
1253EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
1254
Michael Chan43514772009-06-08 18:14:41 -07001255int iscsi_offload_mesg(struct Scsi_Host *shost,
1256 struct iscsi_transport *transport, uint32_t type,
1257 char *data, uint16_t data_size)
1258{
1259 struct nlmsghdr *nlh;
1260 struct sk_buff *skb;
1261 struct iscsi_uevent *ev;
1262 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
1263
Michael Chana541f842009-07-29 08:49:52 +00001264 skb = alloc_skb(len, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001265 if (!skb) {
1266 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
1267 return -ENOMEM;
1268 }
1269
1270 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
1271 ev = NLMSG_DATA(nlh);
1272 memset(ev, 0, sizeof(*ev));
1273 ev->type = type;
1274 ev->transport_handle = iscsi_handle(transport);
1275 switch (type) {
1276 case ISCSI_KEVENT_PATH_REQ:
1277 ev->r.req_path.host_no = shost->host_no;
1278 break;
1279 case ISCSI_KEVENT_IF_DOWN:
1280 ev->r.notify_if_down.host_no = shost->host_no;
1281 break;
1282 }
1283
1284 memcpy((char *)ev + sizeof(*ev), data, data_size);
1285
Michael Chana541f842009-07-29 08:49:52 +00001286 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07001287}
1288EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
1289
Mike Christiee5bd7b52008-09-24 11:46:10 -05001290void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07001291{
1292 struct nlmsghdr *nlh;
1293 struct sk_buff *skb;
1294 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05001295 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001296 int len = NLMSG_SPACE(sizeof(*ev));
1297
Mike Christie790f39a2006-05-18 20:31:39 -05001298 priv = iscsi_if_transport_lookup(conn->transport);
1299 if (!priv)
1300 return;
1301
Mike Christie43a145a2006-10-16 18:09:38 -04001302 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001303 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001304 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
1305 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07001306 return;
1307 }
1308
Michael Chan43514772009-06-08 18:14:41 -07001309 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001310 ev = NLMSG_DATA(nlh);
1311 ev->transport_handle = iscsi_handle(conn->transport);
1312 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07001313 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05001314 ev->r.connerror.cid = conn->cid;
1315 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07001316
Michael Chan43514772009-06-08 18:14:41 -07001317 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001318
Mike Christie322d7392008-01-31 13:36:52 -06001319 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
1320 error);
Alex Aizman0896b752005-08-04 19:33:07 -07001321}
Mike Christiee5bd7b52008-09-24 11:46:10 -05001322EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07001323
1324static int
Michael Chan43514772009-06-08 18:14:41 -07001325iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
1326 void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07001327{
1328 struct sk_buff *skb;
1329 struct nlmsghdr *nlh;
1330 int len = NLMSG_SPACE(size);
1331 int flags = multi ? NLM_F_MULTI : 0;
1332 int t = done ? NLMSG_DONE : type;
1333
Mike Christie43a145a2006-10-16 18:09:38 -04001334 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05001335 if (!skb) {
1336 printk(KERN_ERR "Could not allocate skb to send reply.\n");
1337 return -ENOMEM;
1338 }
Alex Aizman0896b752005-08-04 19:33:07 -07001339
Michael Chan43514772009-06-08 18:14:41 -07001340 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07001341 nlh->nlmsg_flags = flags;
1342 memcpy(NLMSG_DATA(nlh), payload, size);
Michael Chan43514772009-06-08 18:14:41 -07001343 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001344}
1345
1346static int
Mike Christie5b940ad2006-02-01 21:06:56 -06001347iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07001348{
1349 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1350 struct iscsi_stats *stats;
1351 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06001352 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001353 struct nlmsghdr *nlhstat;
1354 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05001355 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07001356 int len = NLMSG_SPACE(sizeof(*ev) +
1357 sizeof(struct iscsi_stats) +
1358 sizeof(struct iscsi_stats_custom) *
1359 ISCSI_STATS_CUSTOM_MAX);
1360 int err = 0;
1361
Mike Christie790f39a2006-05-18 20:31:39 -05001362 priv = iscsi_if_transport_lookup(transport);
1363 if (!priv)
1364 return -EINVAL;
1365
Mike Christieb5c7a122006-04-06 21:13:33 -05001366 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07001367 if (!conn)
1368 return -EEXIST;
1369
1370 do {
1371 int actual_size;
1372
Mike Christie43a145a2006-10-16 18:09:38 -04001373 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001374 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06001375 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
1376 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001377 return -ENOMEM;
1378 }
1379
Michael Chan43514772009-06-08 18:14:41 -07001380 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07001381 (len - sizeof(*nlhstat)), 0);
1382 evstat = NLMSG_DATA(nlhstat);
1383 memset(evstat, 0, sizeof(*evstat));
1384 evstat->transport_handle = iscsi_handle(conn->transport);
1385 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05001386 evstat->u.get_stats.cid =
1387 ev->u.get_stats.cid;
1388 evstat->u.get_stats.sid =
1389 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07001390 stats = (struct iscsi_stats *)
1391 ((char*)evstat + sizeof(*evstat));
1392 memset(stats, 0, sizeof(*stats));
1393
Mike Christie7b7232f2006-02-01 21:06:49 -06001394 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07001395 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1396 sizeof(struct iscsi_stats) +
1397 sizeof(struct iscsi_stats_custom) *
1398 stats->custom_length);
1399 actual_size -= sizeof(*nlhstat);
1400 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06001401 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07001402 nlhstat->nlmsg_len = actual_size;
1403
Michael Chan43514772009-06-08 18:14:41 -07001404 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
1405 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07001406 } while (err < 0 && err != -ECONNREFUSED);
1407
1408 return err;
1409}
1410
Mike Christie53cb8a12006-06-28 12:00:32 -05001411/**
Mike Christie26974782007-12-13 12:43:29 -06001412 * iscsi_session_event - send session destr. completion event
1413 * @session: iscsi class session
1414 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05001415 */
Mike Christie26974782007-12-13 12:43:29 -06001416int iscsi_session_event(struct iscsi_cls_session *session,
1417 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05001418{
1419 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05001420 struct Scsi_Host *shost;
1421 struct iscsi_uevent *ev;
1422 struct sk_buff *skb;
1423 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05001424 int rc, len = NLMSG_SPACE(sizeof(*ev));
1425
Mike Christie26974782007-12-13 12:43:29 -06001426 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05001427 if (!priv)
1428 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05001429 shost = iscsi_session_to_shost(session);
1430
Mike Christie43a145a2006-10-16 18:09:38 -04001431 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001432 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001433 iscsi_cls_session_printk(KERN_ERR, session,
1434 "Cannot notify userspace of session "
1435 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001436 return -ENOMEM;
1437 }
1438
Michael Chan43514772009-06-08 18:14:41 -07001439 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Mike Christie53cb8a12006-06-28 12:00:32 -05001440 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06001441 ev->transport_handle = iscsi_handle(session->transport);
1442
1443 ev->type = event;
1444 switch (event) {
1445 case ISCSI_KEVENT_DESTROY_SESSION:
1446 ev->r.d_session.host_no = shost->host_no;
1447 ev->r.d_session.sid = session->sid;
1448 break;
1449 case ISCSI_KEVENT_CREATE_SESSION:
1450 ev->r.c_session_ret.host_no = shost->host_no;
1451 ev->r.c_session_ret.sid = session->sid;
1452 break;
1453 case ISCSI_KEVENT_UNBIND_SESSION:
1454 ev->r.unbind_session.host_no = shost->host_no;
1455 ev->r.unbind_session.sid = session->sid;
1456 break;
1457 default:
Mike Christie322d7392008-01-31 13:36:52 -06001458 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1459 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06001460 kfree_skb(skb);
1461 return -EINVAL;
1462 }
Mike Christie53cb8a12006-06-28 12:00:32 -05001463
1464 /*
1465 * this will occur if the daemon is not up, so we just warn
1466 * the user and when the daemon is restarted it will handle it
1467 */
Michael Chan43514772009-06-08 18:14:41 -07001468 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001469 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06001470 iscsi_cls_session_printk(KERN_ERR, session,
1471 "Cannot notify userspace of session "
1472 "event %u. Check iscsi daemon\n",
1473 event);
Mike Christie632248a2009-08-20 15:11:01 -05001474
1475 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
1476 event, rc);
Mike Christie53cb8a12006-06-28 12:00:32 -05001477 return rc;
1478}
Mike Christie26974782007-12-13 12:43:29 -06001479EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001480
Alex Aizman0896b752005-08-04 19:33:07 -07001481static int
Mike Christied82ff9be2008-05-21 15:54:13 -05001482iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
1483 struct iscsi_uevent *ev, uint32_t initial_cmdsn,
Mike Christie40753ca2008-05-21 15:53:56 -05001484 uint16_t cmds_max, uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001485{
1486 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001487 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06001488 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06001489
Mike Christied82ff9be2008-05-21 15:54:13 -05001490 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06001491 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06001492 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001493 return -ENOMEM;
1494
Mike Christie5e7facb2009-03-05 14:46:06 -06001495 shost = iscsi_session_to_shost(session);
1496 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001497 ev->r.c_session_ret.sid = session->sid;
Mike Christie632248a2009-08-20 15:11:01 -05001498 ISCSI_DBG_TRANS_SESSION(session,
1499 "Completed creating transport session\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001500 return 0;
1501}
1502
1503static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001504iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001505{
Mike Christie7b8631b2006-01-13 18:05:50 -06001506 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001507 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001508
Mike Christieb5c7a122006-04-06 21:13:33 -05001509 session = iscsi_session_lookup(ev->u.c_conn.sid);
1510 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001511 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001512 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001513 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001514 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001515
Mike Christie7b7232f2006-02-01 21:06:49 -06001516 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001517 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001518 iscsi_cls_session_printk(KERN_ERR, session,
1519 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001520 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001521 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001522
Mike Christieb5c7a122006-04-06 21:13:33 -05001523 ev->r.c_conn_ret.sid = session->sid;
1524 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie632248a2009-08-20 15:11:01 -05001525
1526 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001527 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001528}
1529
1530static int
1531iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1532{
Mike Christie7b8631b2006-01-13 18:05:50 -06001533 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001534
Mike Christieb5c7a122006-04-06 21:13:33 -05001535 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001536 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001537 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001538
Mike Christie632248a2009-08-20 15:11:01 -05001539 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001540 if (transport->destroy_conn)
1541 transport->destroy_conn(conn);
Mike Christie632248a2009-08-20 15:11:01 -05001542
Mike Christie7b8631b2006-01-13 18:05:50 -06001543 return 0;
1544}
1545
Mike Christiefd7255f2006-04-06 21:13:36 -05001546static int
1547iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1548{
1549 char *data = (char*)ev + sizeof(*ev);
1550 struct iscsi_cls_conn *conn;
1551 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001552 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001553
1554 session = iscsi_session_lookup(ev->u.set_param.sid);
1555 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1556 if (!conn || !session)
1557 return -EINVAL;
1558
1559 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001560 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001561 sscanf(data, "%d", &value);
Mike Christiefdd46dc2009-11-11 16:34:34 -06001562 session->recovery_tmo = value;
Mike Christie30a6c652006-04-06 21:13:39 -05001563 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001564 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001565 err = transport->set_param(conn, ev->u.set_param.param,
1566 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001567 }
1568
1569 return err;
1570}
1571
Mike Christie10eb0f02009-05-13 17:57:38 -05001572static int iscsi_if_ep_connect(struct iscsi_transport *transport,
1573 struct iscsi_uevent *ev, int msg_type)
1574{
1575 struct iscsi_endpoint *ep;
1576 struct sockaddr *dst_addr;
1577 struct Scsi_Host *shost = NULL;
1578 int non_blocking, err = 0;
1579
1580 if (!transport->ep_connect)
1581 return -EINVAL;
1582
1583 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
1584 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
1585 if (!shost) {
1586 printk(KERN_ERR "ep connect failed. Could not find "
1587 "host no %u\n",
1588 ev->u.ep_connect_through_host.host_no);
1589 return -ENODEV;
1590 }
1591 non_blocking = ev->u.ep_connect_through_host.non_blocking;
1592 } else
1593 non_blocking = ev->u.ep_connect.non_blocking;
1594
1595 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1596 ep = transport->ep_connect(shost, dst_addr, non_blocking);
1597 if (IS_ERR(ep)) {
1598 err = PTR_ERR(ep);
1599 goto release_host;
1600 }
1601
1602 ev->r.ep_connect_ret.handle = ep->id;
1603release_host:
1604 if (shost)
1605 scsi_host_put(shost);
1606 return err;
1607}
1608
Mike Christie22a39fb2011-02-16 15:04:33 -06001609static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
1610 u64 ep_handle)
1611{
1612 struct iscsi_cls_conn *conn;
1613 struct iscsi_endpoint *ep;
1614
1615 if (!transport->ep_disconnect)
1616 return -EINVAL;
1617
1618 ep = iscsi_lookup_endpoint(ep_handle);
1619 if (!ep)
1620 return -EINVAL;
1621 conn = ep->conn;
1622 if (conn) {
1623 mutex_lock(&conn->ep_mutex);
1624 conn->ep = NULL;
1625 mutex_unlock(&conn->ep_mutex);
1626 }
1627
1628 transport->ep_disconnect(ep);
1629 return 0;
1630}
1631
Mike Christie7b8631b2006-01-13 18:05:50 -06001632static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001633iscsi_if_transport_ep(struct iscsi_transport *transport,
1634 struct iscsi_uevent *ev, int msg_type)
1635{
Mike Christied82ff9be2008-05-21 15:54:13 -05001636 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001637 int rc = 0;
1638
1639 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05001640 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001641 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001642 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001643 break;
1644 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1645 if (!transport->ep_poll)
1646 return -EINVAL;
1647
Mike Christied82ff9be2008-05-21 15:54:13 -05001648 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
1649 if (!ep)
1650 return -EINVAL;
1651
1652 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001653 ev->u.ep_poll.timeout_ms);
1654 break;
1655 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie22a39fb2011-02-16 15:04:33 -06001656 rc = iscsi_if_ep_disconnect(transport,
1657 ev->u.ep_disconnect.ep_handle);
Or Gerlitz264faaa2006-05-02 19:46:36 -05001658 break;
1659 }
1660 return rc;
1661}
1662
1663static int
Mike Christie01cb2252006-06-28 12:00:22 -05001664iscsi_tgt_dscvr(struct iscsi_transport *transport,
1665 struct iscsi_uevent *ev)
1666{
Mike Christie2174a042007-05-30 12:57:10 -05001667 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001668 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001669 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001670
1671 if (!transport->tgt_dscvr)
1672 return -EINVAL;
1673
Mike Christie2174a042007-05-30 12:57:10 -05001674 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001675 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05001676 printk(KERN_ERR "target discovery could not find host no %u\n",
1677 ev->u.tgt_dscvr.host_no);
1678 return -ENODEV;
1679 }
1680
1681
Mike Christie01cb2252006-06-28 12:00:22 -05001682 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001683 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1684 ev->u.tgt_dscvr.enable, dst_addr);
1685 scsi_host_put(shost);
1686 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001687}
1688
1689static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001690iscsi_set_host_param(struct iscsi_transport *transport,
1691 struct iscsi_uevent *ev)
1692{
1693 char *data = (char*)ev + sizeof(*ev);
1694 struct Scsi_Host *shost;
1695 int err;
1696
1697 if (!transport->set_host_param)
1698 return -ENOSYS;
1699
1700 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04001701 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05001702 printk(KERN_ERR "set_host_param could not find host no %u\n",
1703 ev->u.set_host_param.host_no);
1704 return -ENODEV;
1705 }
1706
1707 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1708 data, ev->u.set_host_param.len);
1709 scsi_host_put(shost);
1710 return err;
1711}
1712
1713static int
Michael Chan43514772009-06-08 18:14:41 -07001714iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1715{
1716 struct Scsi_Host *shost;
1717 struct iscsi_path *params;
1718 int err;
1719
1720 if (!transport->set_path)
1721 return -ENOSYS;
1722
1723 shost = scsi_host_lookup(ev->u.set_path.host_no);
1724 if (!shost) {
1725 printk(KERN_ERR "set path could not find host no %u\n",
1726 ev->u.set_path.host_no);
1727 return -ENODEV;
1728 }
1729
1730 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
1731 err = transport->set_path(shost, params);
1732
1733 scsi_host_put(shost);
1734 return err;
1735}
1736
1737static int
Mike Christie56c155b2011-07-25 13:48:37 -05001738iscsi_set_iface_params(struct iscsi_transport *transport,
1739 struct iscsi_uevent *ev)
1740{
1741 char *data = (char *)ev + sizeof(*ev);
1742 struct Scsi_Host *shost;
1743 int err;
1744
1745 if (!transport->set_iface_param)
1746 return -ENOSYS;
1747
1748 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
1749 if (!shost) {
1750 printk(KERN_ERR "set_iface_params could not find host no %u\n",
1751 ev->u.set_iface_params.host_no);
1752 return -ENODEV;
1753 }
1754
1755 err = transport->set_iface_param(shost, data,
1756 ev->u.set_iface_params.count);
1757 scsi_host_put(shost);
1758 return err;
1759}
1760
1761static int
Michael Chan43514772009-06-08 18:14:41 -07001762iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07001763{
1764 int err = 0;
1765 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1766 struct iscsi_transport *transport = NULL;
1767 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001768 struct iscsi_cls_session *session;
1769 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05001770 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001771
Michael Chan43514772009-06-08 18:14:41 -07001772 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
1773 *group = ISCSI_NL_GRP_UIP;
1774 else
1775 *group = ISCSI_NL_GRP_ISCSID;
1776
Alex Aizman0896b752005-08-04 19:33:07 -07001777 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1778 if (!priv)
1779 return -EINVAL;
1780 transport = priv->iscsi_transport;
1781
Mike Christie7b7232f2006-02-01 21:06:49 -06001782 if (!try_module_get(transport->owner))
1783 return -EINVAL;
1784
Alex Aizman0896b752005-08-04 19:33:07 -07001785 switch (nlh->nlmsg_type) {
1786 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001787 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001788 ev->u.c_session.initial_cmdsn,
1789 ev->u.c_session.cmds_max,
1790 ev->u.c_session.queue_depth);
1791 break;
1792 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05001793 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05001794 if (!ep) {
1795 err = -EINVAL;
1796 break;
1797 }
Mike Christied82ff9be2008-05-21 15:54:13 -05001798
1799 err = iscsi_if_create_session(priv, ep, ev,
Mike Christie40753ca2008-05-21 15:53:56 -05001800 ev->u.c_bound_session.initial_cmdsn,
1801 ev->u.c_bound_session.cmds_max,
1802 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001803 break;
1804 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001805 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001806 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001807 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001808 else
1809 err = -EINVAL;
1810 break;
1811 case ISCSI_UEVENT_UNBIND_SESSION:
1812 session = iscsi_session_lookup(ev->u.d_session.sid);
1813 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06001814 scsi_queue_work(iscsi_session_to_shost(session),
1815 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06001816 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001817 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001818 break;
1819 case ISCSI_UEVENT_CREATE_CONN:
1820 err = iscsi_if_create_conn(transport, ev);
1821 break;
1822 case ISCSI_UEVENT_DESTROY_CONN:
1823 err = iscsi_if_destroy_conn(transport, ev);
1824 break;
1825 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001826 session = iscsi_session_lookup(ev->u.b_conn.sid);
1827 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001828
Mike Christie22a39fb2011-02-16 15:04:33 -06001829 if (conn && conn->ep)
1830 iscsi_if_ep_disconnect(transport, conn->ep->id);
1831
1832 if (!session || !conn) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001833 err = -EINVAL;
Mike Christie22a39fb2011-02-16 15:04:33 -06001834 break;
1835 }
1836
1837 ev->r.retcode = transport->bind_conn(session, conn,
1838 ev->u.b_conn.transport_eph,
1839 ev->u.b_conn.is_leading);
1840 if (ev->r.retcode || !transport->ep_connect)
1841 break;
1842
1843 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
1844 if (ep) {
1845 ep->conn = conn;
1846
1847 mutex_lock(&conn->ep_mutex);
1848 conn->ep = ep;
1849 mutex_unlock(&conn->ep_mutex);
1850 } else
1851 iscsi_cls_conn_printk(KERN_ERR, conn,
1852 "Could not set ep conn "
1853 "binding\n");
Alex Aizman0896b752005-08-04 19:33:07 -07001854 break;
1855 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001856 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001857 break;
1858 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001859 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001860 if (conn)
1861 ev->r.retcode = transport->start_conn(conn);
1862 else
1863 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001864 break;
1865 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001866 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001867 if (conn)
1868 transport->stop_conn(conn, ev->u.stop_conn.flag);
1869 else
1870 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001871 break;
1872 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001873 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001874 if (conn)
1875 ev->r.retcode = transport->send_pdu(conn,
1876 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1877 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1878 ev->u.send_pdu.data_size);
1879 else
1880 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001881 break;
1882 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001883 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001884 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001885 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1886 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1887 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05001888 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05001889 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1890 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001891 case ISCSI_UEVENT_TGT_DSCVR:
1892 err = iscsi_tgt_dscvr(transport, ev);
1893 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001894 case ISCSI_UEVENT_SET_HOST_PARAM:
1895 err = iscsi_set_host_param(transport, ev);
1896 break;
Michael Chan43514772009-06-08 18:14:41 -07001897 case ISCSI_UEVENT_PATH_UPDATE:
1898 err = iscsi_set_path(transport, ev);
1899 break;
Mike Christie56c155b2011-07-25 13:48:37 -05001900 case ISCSI_UEVENT_SET_IFACE_PARAMS:
1901 err = iscsi_set_iface_params(transport, ev);
1902 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001903 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001904 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001905 break;
1906 }
1907
Mike Christie7b7232f2006-02-01 21:06:49 -06001908 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001909 return err;
1910}
1911
Mike Christieb5c7a122006-04-06 21:13:33 -05001912/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001913 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1914 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001915 */
Alex Aizman0896b752005-08-04 19:33:07 -07001916static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001917iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001918{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001919 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001920 while (skb->len >= NLMSG_SPACE(0)) {
1921 int err;
1922 uint32_t rlen;
1923 struct nlmsghdr *nlh;
1924 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07001925 uint32_t group;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001926
1927 nlh = nlmsg_hdr(skb);
1928 if (nlh->nlmsg_len < sizeof(*nlh) ||
1929 skb->len < nlh->nlmsg_len) {
1930 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001931 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001932
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001933 ev = NLMSG_DATA(nlh);
1934 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1935 if (rlen > skb->len)
1936 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001937
Michael Chan43514772009-06-08 18:14:41 -07001938 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001939 if (err) {
1940 ev->type = ISCSI_KEVENT_IF_ERROR;
1941 ev->iferror = err;
1942 }
1943 do {
1944 /*
1945 * special case for GET_STATS:
1946 * on success - sending reply and stats from
1947 * inside of if_recv_msg(),
1948 * on error - fall through.
1949 */
1950 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001951 break;
Michael Chan43514772009-06-08 18:14:41 -07001952 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001953 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1954 } while (err < 0 && err != -ECONNREFUSED);
1955 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001956 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001957 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001958}
1959
Mike Christiefd7255f2006-04-06 21:13:36 -05001960#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01001961struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05001962 __ATTR(_name,_mode,_show,_store)
1963
Alex Aizman0896b752005-08-04 19:33:07 -07001964/*
1965 * iSCSI connection attrs
1966 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001967#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001969show_conn_param_##param(struct device *dev, \
1970 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001971{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001972 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001973 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001974 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001975}
1976
Mike Christiea54a52c2006-06-28 12:00:23 -05001977#define iscsi_conn_attr(field, param) \
1978 iscsi_conn_attr_show(param) \
1979static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001980 NULL);
1981
Mike Christiea54a52c2006-06-28 12:00:23 -05001982iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1983iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1984iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1985iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1986iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1987iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1988iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
Mike Christiea54a52c2006-06-28 12:00:23 -05001989iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1990iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001991iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1992iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Mike Christie289324b2011-02-16 15:04:37 -06001994#define iscsi_conn_ep_attr_show(param) \
1995static ssize_t show_conn_ep_param_##param(struct device *dev, \
1996 struct device_attribute *attr,\
1997 char *buf) \
1998{ \
1999 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
2000 struct iscsi_transport *t = conn->transport; \
2001 struct iscsi_endpoint *ep; \
2002 ssize_t rc; \
2003 \
2004 /* \
2005 * Need to make sure ep_disconnect does not free the LLD's \
2006 * interconnect resources while we are trying to read them. \
2007 */ \
2008 mutex_lock(&conn->ep_mutex); \
2009 ep = conn->ep; \
2010 if (!ep && t->ep_connect) { \
2011 mutex_unlock(&conn->ep_mutex); \
2012 return -ENOTCONN; \
2013 } \
2014 \
2015 if (ep) \
2016 rc = t->get_ep_param(ep, param, buf); \
2017 else \
2018 rc = t->get_conn_param(conn, param, buf); \
2019 mutex_unlock(&conn->ep_mutex); \
2020 return rc; \
2021}
2022
2023#define iscsi_conn_ep_attr(field, param) \
2024 iscsi_conn_ep_attr_show(param) \
2025static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
2026 show_conn_ep_param_##param, NULL);
2027
2028iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
2029iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
2030
Mike Christie3128c6c2011-07-25 13:48:42 -05002031static struct attribute *iscsi_conn_attrs[] = {
2032 &dev_attr_conn_max_recv_dlength.attr,
2033 &dev_attr_conn_max_xmit_dlength.attr,
2034 &dev_attr_conn_header_digest.attr,
2035 &dev_attr_conn_data_digest.attr,
2036 &dev_attr_conn_ifmarker.attr,
2037 &dev_attr_conn_ofmarker.attr,
2038 &dev_attr_conn_address.attr,
2039 &dev_attr_conn_port.attr,
2040 &dev_attr_conn_exp_statsn.attr,
2041 &dev_attr_conn_persistent_address.attr,
2042 &dev_attr_conn_persistent_port.attr,
2043 &dev_attr_conn_ping_tmo.attr,
2044 &dev_attr_conn_recv_tmo.attr,
2045 NULL,
2046};
2047
2048static mode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
2049 struct attribute *attr, int i)
2050{
2051 struct device *cdev = container_of(kobj, struct device, kobj);
2052 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
2053 struct iscsi_transport *t = conn->transport;
2054 int param;
2055
2056 if (attr == &dev_attr_conn_max_recv_dlength.attr)
2057 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
2058 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
2059 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
2060 else if (attr == &dev_attr_conn_header_digest.attr)
2061 param = ISCSI_PARAM_HDRDGST_EN;
2062 else if (attr == &dev_attr_conn_data_digest.attr)
2063 param = ISCSI_PARAM_DATADGST_EN;
2064 else if (attr == &dev_attr_conn_ifmarker.attr)
2065 param = ISCSI_PARAM_IFMARKER_EN;
2066 else if (attr == &dev_attr_conn_ofmarker.attr)
2067 param = ISCSI_PARAM_OFMARKER_EN;
2068 else if (attr == &dev_attr_conn_address.attr)
2069 param = ISCSI_PARAM_CONN_ADDRESS;
2070 else if (attr == &dev_attr_conn_port.attr)
2071 param = ISCSI_PARAM_CONN_PORT;
2072 else if (attr == &dev_attr_conn_exp_statsn.attr)
2073 param = ISCSI_PARAM_EXP_STATSN;
2074 else if (attr == &dev_attr_conn_persistent_address.attr)
2075 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
2076 else if (attr == &dev_attr_conn_persistent_port.attr)
2077 param = ISCSI_PARAM_PERSISTENT_PORT;
2078 else if (attr == &dev_attr_conn_ping_tmo.attr)
2079 param = ISCSI_PARAM_PING_TMO;
2080 else if (attr == &dev_attr_conn_recv_tmo.attr)
2081 param = ISCSI_PARAM_RECV_TMO;
2082 else {
2083 WARN_ONCE(1, "Invalid conn attr");
2084 return 0;
2085 }
2086
2087 return t->attr_is_visible(ISCSI_PARAM, param);
2088}
2089
2090static struct attribute_group iscsi_conn_group = {
2091 .attrs = iscsi_conn_attrs,
2092 .is_visible = iscsi_conn_attr_is_visible,
2093};
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095/*
Alex Aizman0896b752005-08-04 19:33:07 -07002096 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 */
Mike Christieb2c64162007-05-30 12:57:16 -05002098#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002100show_session_param_##param(struct device *dev, \
2101 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002102{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002103 struct iscsi_cls_session *session = \
2104 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002105 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05002106 \
2107 if (perm && !capable(CAP_SYS_ADMIN)) \
2108 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05002109 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002110}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Mike Christieb2c64162007-05-30 12:57:16 -05002112#define iscsi_session_attr(field, param, perm) \
2113 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05002114static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05002115 NULL);
Mike Christieb2c64162007-05-30 12:57:16 -05002116iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
2117iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
2118iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
2119iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
2120iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
2121iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
2122iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
2123iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
2124iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
2125iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
2126iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
2127iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
2128iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
2129iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06002130iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
2131iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
2132iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002133iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05002134iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05302135iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
2136iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05002137
Mike Christie6eabafb2008-01-31 13:36:43 -06002138static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01002139show_priv_session_state(struct device *dev, struct device_attribute *attr,
2140 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06002141{
Tony Jonesee959b02008-02-22 00:13:36 +01002142 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06002143 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
2144}
2145static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
2146 NULL);
2147
Mike Christiefd7255f2006-04-06 21:13:36 -05002148#define iscsi_priv_session_attr_show(field, format) \
2149static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002150show_priv_session_##field(struct device *dev, \
2151 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002152{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002153 struct iscsi_cls_session *session = \
2154 iscsi_dev_to_session(dev->parent); \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302155 if (session->field == -1) \
2156 return sprintf(buf, "off\n"); \
Mike Christiefd7255f2006-04-06 21:13:36 -05002157 return sprintf(buf, format"\n", session->field); \
2158}
2159
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302160#define iscsi_priv_session_attr_store(field) \
2161static ssize_t \
2162store_priv_session_##field(struct device *dev, \
2163 struct device_attribute *attr, \
2164 const char *buf, size_t count) \
2165{ \
2166 int val; \
2167 char *cp; \
2168 struct iscsi_cls_session *session = \
2169 iscsi_dev_to_session(dev->parent); \
2170 if ((session->state == ISCSI_SESSION_FREE) || \
2171 (session->state == ISCSI_SESSION_FAILED)) \
2172 return -EBUSY; \
2173 if (strncmp(buf, "off", 3) == 0) \
2174 session->field = -1; \
2175 else { \
2176 val = simple_strtoul(buf, &cp, 0); \
2177 if (*cp != '\0' && *cp != '\n') \
2178 return -EINVAL; \
2179 session->field = val; \
2180 } \
2181 return count; \
2182}
2183
2184#define iscsi_priv_session_rw_attr(field, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05002185 iscsi_priv_session_attr_show(field, format) \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302186 iscsi_priv_session_attr_store(field) \
Vasiliy Kulikov523f3c82011-02-04 15:24:14 +03002187static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05302188 show_priv_session_##field, \
2189 store_priv_session_##field)
2190iscsi_priv_session_rw_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05002191
Mike Christie1d063c12011-07-25 13:48:43 -05002192static struct attribute *iscsi_session_attrs[] = {
2193 &dev_attr_sess_initial_r2t.attr,
2194 &dev_attr_sess_max_outstanding_r2t.attr,
2195 &dev_attr_sess_immediate_data.attr,
2196 &dev_attr_sess_first_burst_len.attr,
2197 &dev_attr_sess_max_burst_len.attr,
2198 &dev_attr_sess_data_pdu_in_order.attr,
2199 &dev_attr_sess_data_seq_in_order.attr,
2200 &dev_attr_sess_erl.attr,
2201 &dev_attr_sess_targetname.attr,
2202 &dev_attr_sess_tpgt.attr,
2203 &dev_attr_sess_password.attr,
2204 &dev_attr_sess_password_in.attr,
2205 &dev_attr_sess_username.attr,
2206 &dev_attr_sess_username_in.attr,
2207 &dev_attr_sess_fast_abort.attr,
2208 &dev_attr_sess_abort_tmo.attr,
2209 &dev_attr_sess_lu_reset_tmo.attr,
2210 &dev_attr_sess_tgt_reset_tmo.attr,
2211 &dev_attr_sess_ifacename.attr,
2212 &dev_attr_sess_initiatorname.attr,
2213 &dev_attr_sess_targetalias.attr,
2214 &dev_attr_priv_sess_recovery_tmo.attr,
2215 &dev_attr_priv_sess_state.attr,
2216 NULL,
2217};
2218
2219static mode_t iscsi_session_attr_is_visible(struct kobject *kobj,
2220 struct attribute *attr, int i)
2221{
2222 struct device *cdev = container_of(kobj, struct device, kobj);
2223 struct iscsi_cls_session *session = transport_class_to_session(cdev);
2224 struct iscsi_transport *t = session->transport;
2225 int param;
2226
2227 if (attr == &dev_attr_sess_initial_r2t.attr)
2228 param = ISCSI_PARAM_INITIAL_R2T_EN;
2229 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
2230 param = ISCSI_PARAM_MAX_R2T;
2231 else if (attr == &dev_attr_sess_immediate_data.attr)
2232 param = ISCSI_PARAM_IMM_DATA_EN;
2233 else if (attr == &dev_attr_sess_first_burst_len.attr)
2234 param = ISCSI_PARAM_FIRST_BURST;
2235 else if (attr == &dev_attr_sess_max_burst_len.attr)
2236 param = ISCSI_PARAM_MAX_BURST;
2237 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
2238 param = ISCSI_PARAM_PDU_INORDER_EN;
2239 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
2240 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
2241 else if (attr == &dev_attr_sess_erl.attr)
2242 param = ISCSI_PARAM_ERL;
2243 else if (attr == &dev_attr_sess_targetname.attr)
2244 param = ISCSI_PARAM_TARGET_NAME;
2245 else if (attr == &dev_attr_sess_tpgt.attr)
2246 param = ISCSI_PARAM_TPGT;
2247 else if (attr == &dev_attr_sess_password.attr)
2248 param = ISCSI_PARAM_USERNAME;
2249 else if (attr == &dev_attr_sess_password_in.attr)
2250 param = ISCSI_PARAM_USERNAME_IN;
2251 else if (attr == &dev_attr_sess_username.attr)
2252 param = ISCSI_PARAM_PASSWORD;
2253 else if (attr == &dev_attr_sess_username_in.attr)
2254 param = ISCSI_PARAM_PASSWORD_IN;
2255 else if (attr == &dev_attr_sess_fast_abort.attr)
2256 param = ISCSI_PARAM_FAST_ABORT;
2257 else if (attr == &dev_attr_sess_abort_tmo.attr)
2258 param = ISCSI_PARAM_ABORT_TMO;
2259 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
2260 param = ISCSI_PARAM_LU_RESET_TMO;
2261 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
2262 param = ISCSI_PARAM_TGT_RESET_TMO;
2263 else if (attr == &dev_attr_sess_ifacename.attr)
2264 param = ISCSI_PARAM_IFACE_NAME;
2265 else if (attr == &dev_attr_sess_initiatorname.attr)
2266 param = ISCSI_PARAM_INITIATOR_NAME;
2267 else if (attr == &dev_attr_sess_targetalias.attr)
2268 param = ISCSI_PARAM_TARGET_ALIAS;
2269 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
2270 return S_IRUGO | S_IWUSR;
2271 else if (attr == &dev_attr_priv_sess_state.attr)
2272 return S_IRUGO;
2273 else {
2274 WARN_ONCE(1, "Invalid session attr");
2275 return 0;
2276 }
2277
2278 return t->attr_is_visible(ISCSI_PARAM, param);
2279}
2280
2281static struct attribute_group iscsi_session_group = {
2282 .attrs = iscsi_session_attrs,
2283 .is_visible = iscsi_session_attr_is_visible,
2284};
2285
Mike Christie1819dc82007-05-30 12:57:08 -05002286/*
2287 * iSCSI host attrs
2288 */
2289#define iscsi_host_attr_show(param) \
2290static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01002291show_host_param_##param(struct device *dev, \
2292 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05002293{ \
Tony Jonesee959b02008-02-22 00:13:36 +01002294 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05002295 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
2296 return priv->iscsi_transport->get_host_param(shost, param, buf); \
2297}
2298
2299#define iscsi_host_attr(field, param) \
2300 iscsi_host_attr_show(param) \
2301static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
2302 NULL);
2303
Mike Christied8196ed2007-05-30 12:57:25 -05002304iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002305iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05002306iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05002307iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05002308
Mike Christief27fb2e2011-07-25 13:48:45 -05002309static struct attribute *iscsi_host_attrs[] = {
2310 &dev_attr_host_netdev.attr,
2311 &dev_attr_host_hwaddress.attr,
2312 &dev_attr_host_ipaddress.attr,
2313 &dev_attr_host_initiatorname.attr,
2314 NULL,
2315};
2316
2317static mode_t iscsi_host_attr_is_visible(struct kobject *kobj,
2318 struct attribute *attr, int i)
2319{
2320 struct device *cdev = container_of(kobj, struct device, kobj);
2321 struct Scsi_Host *shost = transport_class_to_shost(cdev);
2322 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
2323 int param;
2324
2325 if (attr == &dev_attr_host_netdev.attr)
2326 param = ISCSI_HOST_PARAM_NETDEV_NAME;
2327 else if (attr == &dev_attr_host_hwaddress.attr)
2328 param = ISCSI_HOST_PARAM_HWADDRESS;
2329 else if (attr == &dev_attr_host_ipaddress.attr)
2330 param = ISCSI_HOST_PARAM_IPADDRESS;
2331 else if (attr == &dev_attr_host_initiatorname.attr)
2332 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
2333 else {
2334 WARN_ONCE(1, "Invalid host attr");
2335 return 0;
2336 }
2337
2338 return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
2339}
2340
2341static struct attribute_group iscsi_host_group = {
2342 .attrs = iscsi_host_attrs,
2343 .is_visible = iscsi_host_attr_is_visible,
2344};
Mike Christie1819dc82007-05-30 12:57:08 -05002345
Alex Aizman0896b752005-08-04 19:33:07 -07002346static int iscsi_session_match(struct attribute_container *cont,
2347 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Mike Christie7b8631b2006-01-13 18:05:50 -06002349 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07002351 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Alex Aizman0896b752005-08-04 19:33:07 -07002353 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return 0;
2355
Mike Christie7b8631b2006-01-13 18:05:50 -06002356 session = iscsi_dev_to_session(dev);
2357 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07002358 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 return 0;
2360
Alex Aizman0896b752005-08-04 19:33:07 -07002361 priv = to_iscsi_internal(shost->transportt);
2362 if (priv->session_cont.ac.class != &iscsi_session_class.class)
2363 return 0;
2364
2365 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Alex Aizman0896b752005-08-04 19:33:07 -07002368static int iscsi_conn_match(struct attribute_container *cont,
2369 struct device *dev)
2370{
Mike Christie7b8631b2006-01-13 18:05:50 -06002371 struct iscsi_cls_session *session;
2372 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07002373 struct Scsi_Host *shost;
2374 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Alex Aizman0896b752005-08-04 19:33:07 -07002376 if (!iscsi_is_conn_dev(dev))
2377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Mike Christie7b8631b2006-01-13 18:05:50 -06002379 conn = iscsi_dev_to_conn(dev);
2380 session = iscsi_dev_to_session(conn->dev.parent);
2381 shost = iscsi_session_to_shost(session);
2382
Alex Aizman0896b752005-08-04 19:33:07 -07002383 if (!shost->transportt)
2384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Alex Aizman0896b752005-08-04 19:33:07 -07002386 priv = to_iscsi_internal(shost->transportt);
2387 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
2388 return 0;
2389
2390 return &priv->conn_cont.ac == cont;
2391}
2392
Mike Christie30a6c652006-04-06 21:13:39 -05002393static int iscsi_host_match(struct attribute_container *cont,
2394 struct device *dev)
2395{
2396 struct Scsi_Host *shost;
2397 struct iscsi_internal *priv;
2398
2399 if (!scsi_is_host_device(dev))
2400 return 0;
2401
2402 shost = dev_to_shost(dev);
2403 if (!shost->transportt ||
2404 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
2405 return 0;
2406
2407 priv = to_iscsi_internal(shost->transportt);
2408 return &priv->t.host_attrs.ac == cont;
2409}
2410
Mike Christie7b8631b2006-01-13 18:05:50 -06002411struct scsi_transport_template *
2412iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07002413{
2414 struct iscsi_internal *priv;
2415 unsigned long flags;
Mike Christief27fb2e2011-07-25 13:48:45 -05002416 int err;
Alex Aizman0896b752005-08-04 19:33:07 -07002417
2418 BUG_ON(!tt);
2419
2420 priv = iscsi_if_transport_lookup(tt);
2421 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002422 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002423
Jes Sorensen24669f752006-01-16 10:31:18 -05002424 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07002425 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06002426 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002427 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07002428 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05002429 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06002430 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07002431
Tony Jonesee959b02008-02-22 00:13:36 +01002432 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01002433 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01002434 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07002435 if (err)
2436 goto free_priv;
2437
Tony Jonesee959b02008-02-22 00:13:36 +01002438 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07002439 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01002440 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07002441
Mike Christie30a6c652006-04-06 21:13:39 -05002442 /* host parameters */
Mike Christie30a6c652006-04-06 21:13:39 -05002443 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
2444 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christief27fb2e2011-07-25 13:48:45 -05002445 priv->t.host_attrs.ac.grp = &iscsi_host_group;
Mike Christie32c6e1b2008-05-21 15:53:58 -05002446 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05002447 transport_container_register(&priv->t.host_attrs);
2448
Alex Aizman0896b752005-08-04 19:33:07 -07002449 /* connection parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07002450 priv->conn_cont.ac.class = &iscsi_connection_class.class;
2451 priv->conn_cont.ac.match = iscsi_conn_match;
Mike Christie3128c6c2011-07-25 13:48:42 -05002452 priv->conn_cont.ac.grp = &iscsi_conn_group;
Alex Aizman0896b752005-08-04 19:33:07 -07002453 transport_container_register(&priv->conn_cont);
2454
Alex Aizman0896b752005-08-04 19:33:07 -07002455 /* session parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07002456 priv->session_cont.ac.class = &iscsi_session_class.class;
2457 priv->session_cont.ac.match = iscsi_session_match;
Mike Christie1d063c12011-07-25 13:48:43 -05002458 priv->session_cont.ac.grp = &iscsi_session_group;
Alex Aizman0896b752005-08-04 19:33:07 -07002459 transport_container_register(&priv->session_cont);
2460
Alex Aizman0896b752005-08-04 19:33:07 -07002461 spin_lock_irqsave(&iscsi_transport_lock, flags);
2462 list_add(&priv->list, &iscsi_transports);
2463 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Alex Aizman0896b752005-08-04 19:33:07 -07002465 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06002466 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Tony Jonesee959b02008-02-22 00:13:36 +01002468unregister_dev:
2469 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05002470 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07002471free_priv:
2472 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06002473 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
Alex Aizman0896b752005-08-04 19:33:07 -07002475EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Alex Aizman0896b752005-08-04 19:33:07 -07002477int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Alex Aizman0896b752005-08-04 19:33:07 -07002479 struct iscsi_internal *priv;
2480 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Alex Aizman0896b752005-08-04 19:33:07 -07002482 BUG_ON(!tt);
2483
Arjan van de Ven0b950672006-01-11 13:16:10 +01002484 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002485
2486 priv = iscsi_if_transport_lookup(tt);
2487 BUG_ON (!priv);
2488
Alex Aizman0896b752005-08-04 19:33:07 -07002489 spin_lock_irqsave(&iscsi_transport_lock, flags);
2490 list_del(&priv->list);
2491 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2492
2493 transport_container_unregister(&priv->conn_cont);
2494 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05002495 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07002496
Tony Jonesee959b02008-02-22 00:13:36 +01002497 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
2498 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002499 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07002500
2501 return 0;
2502}
2503EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505static __init int iscsi_transport_init(void)
2506{
Alex Aizman0896b752005-08-04 19:33:07 -07002507 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Meelis Roos09492602006-12-17 12:10:26 -06002509 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05002510 ISCSI_TRANSPORT_VERSION);
2511
Mike Christie41be1442007-02-28 17:32:18 -06002512 atomic_set(&iscsi_session_nr, 0);
2513
Alex Aizman0896b752005-08-04 19:33:07 -07002514 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if (err)
2516 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07002517
Mike Christied82ff9be2008-05-21 15:54:13 -05002518 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002519 if (err)
2520 goto unregister_transport_class;
2521
Mike Christie8d079132011-07-25 13:48:40 -05002522 err = class_register(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002523 if (err)
2524 goto unregister_endpoint_class;
2525
Mike Christie8d079132011-07-25 13:48:40 -05002526 err = transport_class_register(&iscsi_host_class);
2527 if (err)
2528 goto unregister_iface_class;
2529
Mike Christie30a6c652006-04-06 21:13:39 -05002530 err = transport_class_register(&iscsi_connection_class);
2531 if (err)
2532 goto unregister_host_class;
2533
Alex Aizman0896b752005-08-04 19:33:07 -07002534 err = transport_class_register(&iscsi_session_class);
2535 if (err)
2536 goto unregister_conn_class;
2537
Mike Christied82ff9be2008-05-21 15:54:13 -05002538 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx,
2539 NULL, THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07002540 if (!nls) {
2541 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04002542 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07002543 }
2544
Mike Christied8bf5412007-12-13 12:43:27 -06002545 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
2546 if (!iscsi_eh_timer_workq)
2547 goto release_nls;
2548
Mike Christie43a145a2006-10-16 18:09:38 -04002549 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07002550
Mike Christied8bf5412007-12-13 12:43:27 -06002551release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002552 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002553unregister_session_class:
2554 transport_class_unregister(&iscsi_session_class);
2555unregister_conn_class:
2556 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002557unregister_host_class:
2558 transport_class_unregister(&iscsi_host_class);
Mike Christie8d079132011-07-25 13:48:40 -05002559unregister_iface_class:
2560 class_unregister(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002561unregister_endpoint_class:
2562 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002563unregister_transport_class:
2564 class_unregister(&iscsi_transport_class);
2565 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
2568static void __exit iscsi_transport_exit(void)
2569{
Mike Christied8bf5412007-12-13 12:43:27 -06002570 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002571 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07002572 transport_class_unregister(&iscsi_connection_class);
2573 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05002574 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05002575 class_unregister(&iscsi_endpoint_class);
Mike Christie8d079132011-07-25 13:48:40 -05002576 class_unregister(&iscsi_iface_class);
Alex Aizman0896b752005-08-04 19:33:07 -07002577 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
2580module_init(iscsi_transport_init);
2581module_exit(iscsi_transport_exit);
2582
Alex Aizman0896b752005-08-04 19:33:07 -07002583MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
2584 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
2585 "Alex Aizman <itn780@yahoo.com>");
2586MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05002588MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
Stephen Hemminger058548a2010-12-09 09:37:56 -08002589MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);