blob: ce06e8772f3a8b48a5740dc28a2b056f4a0d003e [file] [log] [blame]
Alex Aizman0896b752005-08-04 19:33:07 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * iSCSI transport class definitions
3 *
4 * Copyright (C) IBM Corporation, 2004
Alex Aizman0896b752005-08-04 19:33:07 -07005 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23#include <linux/module.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010024#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Mike Christie90eeb012011-07-25 13:48:50 -050026#include <linux/bsg-lib.h>
Mike Christie8d4a6902011-10-06 03:56:57 -050027#include <linux/idr.h>
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -040028#include <linux/list.h>
Alex Aizman0896b752005-08-04 19:33:07 -070029#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <scsi/scsi.h>
31#include <scsi/scsi_host.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_transport.h>
34#include <scsi/scsi_transport_iscsi.h>
Alex Aizman0896b752005-08-04 19:33:07 -070035#include <scsi/iscsi_if.h>
Mike Christiec01be6d2010-07-22 16:59:49 +053036#include <scsi/scsi_cmnd.h>
Mike Christie90eeb012011-07-25 13:48:50 -050037#include <scsi/scsi_bsg_iscsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Mike Christie4c2133c2008-06-16 10:11:34 -050039#define ISCSI_TRANSPORT_VERSION "2.0-870"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Mike Christie632248a2009-08-20 15:11:01 -050041static int dbg_session;
42module_param_named(debug_session, dbg_session, int,
43 S_IRUGO | S_IWUSR);
44MODULE_PARM_DESC(debug_session,
45 "Turn on debugging for sessions in scsi_transport_iscsi "
46 "module. Set to 1 to turn on, and zero to turn off. Default "
47 "is off.");
48
49static int dbg_conn;
50module_param_named(debug_conn, dbg_conn, int,
51 S_IRUGO | S_IWUSR);
52MODULE_PARM_DESC(debug_conn,
53 "Turn on debugging for connections in scsi_transport_iscsi "
54 "module. Set to 1 to turn on, and zero to turn off. Default "
55 "is off.");
56
57#define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
58 do { \
59 if (dbg_session) \
60 iscsi_cls_session_printk(KERN_INFO, _session, \
61 "%s: " dbg_fmt, \
62 __func__, ##arg); \
63 } while (0);
64
65#define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
66 do { \
67 if (dbg_conn) \
68 iscsi_cls_conn_printk(KERN_INFO, _conn, \
69 "%s: " dbg_fmt, \
70 __func__, ##arg); \
71 } while (0);
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073struct iscsi_internal {
74 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070075 struct iscsi_transport *iscsi_transport;
76 struct list_head list;
Tony Jonesee959b02008-02-22 00:13:36 +010077 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050078
Alex Aizman0896b752005-08-04 19:33:07 -070079 struct transport_container conn_cont;
Alex Aizman0896b752005-08-04 19:33:07 -070080 struct transport_container session_cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
Mike Christie41be1442007-02-28 17:32:18 -060083static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060084static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050085
Mike Christie8d4a6902011-10-06 03:56:57 -050086static DEFINE_IDA(iscsi_sess_ida);
Alex Aizman0896b752005-08-04 19:33:07 -070087/*
88 * list of registered transports and lock that must
89 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010090 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070091 */
92static LIST_HEAD(iscsi_transports);
93static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Alex Aizman0896b752005-08-04 19:33:07 -070095#define to_iscsi_internal(tmpl) \
96 container_of(tmpl, struct iscsi_internal, t)
97
Tony Jonesee959b02008-02-22 00:13:36 +010098#define dev_to_iscsi_internal(_dev) \
99 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700100
Tony Jonesee959b02008-02-22 00:13:36 +0100101static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700102{
Tony Jonesee959b02008-02-22 00:13:36 +0100103 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -0700104 kfree(priv);
105}
106
107/*
108 * iscsi_transport_class represents the iscsi_transports that are
109 * registered.
110 */
111static struct class iscsi_transport_class = {
112 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +0100113 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -0700114};
115
116static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100117show_transport_handle(struct device *dev, struct device_attribute *attr,
118 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -0700119{
Tony Jonesee959b02008-02-22 00:13:36 +0100120 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Mike Christie762e2bf2005-09-12 21:01:46 -0500121 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -0700122}
Tony Jonesee959b02008-02-22 00:13:36 +0100123static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700124
125#define show_transport_attr(name, format) \
126static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100127show_transport_##name(struct device *dev, \
128 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -0700129{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100130 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Alex Aizman0896b752005-08-04 19:33:07 -0700131 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
132} \
Tony Jonesee959b02008-02-22 00:13:36 +0100133static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700134
135show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700136
137static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100138 &dev_attr_handle.attr,
139 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700140 NULL,
141};
142
143static struct attribute_group iscsi_transport_group = {
144 .attrs = iscsi_transport_attrs,
145};
146
Mike Christied82ff9be2008-05-21 15:54:13 -0500147/*
148 * iSCSI endpoint attrs
149 */
150#define iscsi_dev_to_endpoint(_dev) \
151 container_of(_dev, struct iscsi_endpoint, dev)
152
153#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
154struct device_attribute dev_attr_##_prefix##_##_name = \
155 __ATTR(_name,_mode,_show,_store)
156
157static void iscsi_endpoint_release(struct device *dev)
158{
159 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
160 kfree(ep);
161}
162
163static struct class iscsi_endpoint_class = {
164 .name = "iscsi_endpoint",
165 .dev_release = iscsi_endpoint_release,
166};
167
168static ssize_t
169show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
170{
171 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Mike Christie21536062008-09-24 11:46:11 -0500172 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500173}
174static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
175
176static struct attribute *iscsi_endpoint_attrs[] = {
177 &dev_attr_ep_handle.attr,
178 NULL,
179};
180
181static struct attribute_group iscsi_endpoint_group = {
182 .attrs = iscsi_endpoint_attrs,
183};
184
185#define ISCSI_MAX_EPID -1
186
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100187static int iscsi_match_epid(struct device *dev, const void *data)
Mike Christied82ff9be2008-05-21 15:54:13 -0500188{
189 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100190 const uint64_t *epid = data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500191
192 return *epid == ep->id;
193}
194
195struct iscsi_endpoint *
196iscsi_create_endpoint(int dd_size)
197{
198 struct device *dev;
199 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500200 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500201 int err;
202
203 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400204 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500205 iscsi_match_epid);
206 if (!dev)
207 break;
208 }
209 if (id == ISCSI_MAX_EPID) {
210 printk(KERN_ERR "Too many connections. Max supported %u\n",
211 ISCSI_MAX_EPID - 1);
212 return NULL;
213 }
214
215 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
216 if (!ep)
217 return NULL;
218
219 ep->id = id;
220 ep->dev.class = &iscsi_endpoint_class;
Kay Sievers71610f52008-12-03 22:41:36 +0100221 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500222 err = device_register(&ep->dev);
223 if (err)
224 goto free_ep;
225
226 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
227 if (err)
228 goto unregister_dev;
229
230 if (dd_size)
231 ep->dd_data = &ep[1];
232 return ep;
233
234unregister_dev:
235 device_unregister(&ep->dev);
236 return NULL;
237
238free_ep:
239 kfree(ep);
240 return NULL;
241}
242EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
243
244void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
245{
246 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
247 device_unregister(&ep->dev);
248}
249EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
250
251struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
252{
Mike Christief80f8682008-06-16 10:11:35 -0500253 struct iscsi_endpoint *ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500254 struct device *dev;
255
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400256 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500257 iscsi_match_epid);
258 if (!dev)
259 return NULL;
260
Mike Christief80f8682008-06-16 10:11:35 -0500261 ep = iscsi_dev_to_endpoint(dev);
262 /*
263 * we can drop this now because the interface will prevent
264 * removals and lookups from racing.
265 */
266 put_device(dev);
267 return ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500268}
269EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
270
Mike Christie8d079132011-07-25 13:48:40 -0500271/*
272 * Interface to display network param to sysfs
273 */
274
275static void iscsi_iface_release(struct device *dev)
276{
277 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
278 struct device *parent = iface->dev.parent;
279
280 kfree(iface);
281 put_device(parent);
282}
283
284
285static struct class iscsi_iface_class = {
286 .name = "iscsi_iface",
287 .dev_release = iscsi_iface_release,
288};
289
290#define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
291struct device_attribute dev_attr_##_prefix##_##_name = \
292 __ATTR(_name, _mode, _show, _store)
293
294/* iface attrs show */
295#define iscsi_iface_attr_show(type, name, param_type, param) \
296static ssize_t \
297show_##type##_##name(struct device *dev, struct device_attribute *attr, \
298 char *buf) \
299{ \
300 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
301 struct iscsi_transport *t = iface->transport; \
302 return t->get_iface_param(iface, param_type, param, buf); \
303} \
304
305#define iscsi_iface_net_attr(type, name, param) \
306 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
307static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
308
309/* generic read only ipvi4 attribute */
310iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
311iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
312iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
313iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
314
315/* generic read only ipv6 attribute */
316iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
317iscsi_iface_net_attr(ipv6_iface, link_local_addr, ISCSI_NET_PARAM_IPV6_LINKLOCAL);
318iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
319iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
320 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
Mike Christie88f4f512011-10-06 03:56:58 -0500321iscsi_iface_net_attr(ipv6_iface, link_local_autocfg,
Mike Christie8d079132011-07-25 13:48:40 -0500322 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
323
324/* common read only iface attribute */
325iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
Mike Christie2d636732011-10-11 17:55:11 -0500326iscsi_iface_net_attr(iface, vlan_id, ISCSI_NET_PARAM_VLAN_ID);
Mike Christie4223b9e2011-07-25 13:48:47 -0500327iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
328iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
Vikas Chaudhary8c7d40f2011-08-01 03:26:12 -0700329iscsi_iface_net_attr(iface, mtu, ISCSI_NET_PARAM_MTU);
Vikas Chaudharyfcb51242011-08-01 03:26:18 -0700330iscsi_iface_net_attr(iface, port, ISCSI_NET_PARAM_PORT);
Mike Christie8d079132011-07-25 13:48:40 -0500331
Al Viro587a1f12011-07-23 23:11:19 -0400332static umode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
Mike Christie8d079132011-07-25 13:48:40 -0500333 struct attribute *attr, int i)
334{
335 struct device *dev = container_of(kobj, struct device, kobj);
336 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
337 struct iscsi_transport *t = iface->transport;
Mike Christieb78dbba2011-07-25 13:48:44 -0500338 int param;
Mike Christie8d079132011-07-25 13:48:40 -0500339
340 if (attr == &dev_attr_iface_enabled.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500341 param = ISCSI_NET_PARAM_IFACE_ENABLE;
Mike Christie2d636732011-10-11 17:55:11 -0500342 else if (attr == &dev_attr_iface_vlan_id.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500343 param = ISCSI_NET_PARAM_VLAN_ID;
Mike Christie4223b9e2011-07-25 13:48:47 -0500344 else if (attr == &dev_attr_iface_vlan_priority.attr)
345 param = ISCSI_NET_PARAM_VLAN_PRIORITY;
346 else if (attr == &dev_attr_iface_vlan_enabled.attr)
347 param = ISCSI_NET_PARAM_VLAN_ENABLED;
Vikas Chaudhary8c7d40f2011-08-01 03:26:12 -0700348 else if (attr == &dev_attr_iface_mtu.attr)
349 param = ISCSI_NET_PARAM_MTU;
Vikas Chaudharyfcb51242011-08-01 03:26:18 -0700350 else if (attr == &dev_attr_iface_port.attr)
351 param = ISCSI_NET_PARAM_PORT;
Mike Christieb78dbba2011-07-25 13:48:44 -0500352 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
Mike Christie8d079132011-07-25 13:48:40 -0500353 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500354 param = ISCSI_NET_PARAM_IPV4_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500355 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500356 param = ISCSI_NET_PARAM_IPV4_GW;
Mike Christie8d079132011-07-25 13:48:40 -0500357 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500358 param = ISCSI_NET_PARAM_IPV4_SUBNET;
Mike Christie8d079132011-07-25 13:48:40 -0500359 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500360 param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
361 else
362 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500363 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
364 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500365 param = ISCSI_NET_PARAM_IPV6_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500366 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500367 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
Mike Christie8d079132011-07-25 13:48:40 -0500368 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500369 param = ISCSI_NET_PARAM_IPV6_ROUTER;
Mike Christie8d079132011-07-25 13:48:40 -0500370 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500371 param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
Mike Christie88f4f512011-10-06 03:56:58 -0500372 else if (attr == &dev_attr_ipv6_iface_link_local_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500373 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
374 else
375 return 0;
376 } else {
377 WARN_ONCE(1, "Invalid iface attr");
378 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500379 }
380
Mike Christieb78dbba2011-07-25 13:48:44 -0500381 return t->attr_is_visible(ISCSI_NET_PARAM, param);
Mike Christie8d079132011-07-25 13:48:40 -0500382}
383
384static struct attribute *iscsi_iface_attrs[] = {
385 &dev_attr_iface_enabled.attr,
Mike Christie2d636732011-10-11 17:55:11 -0500386 &dev_attr_iface_vlan_id.attr,
Mike Christie4223b9e2011-07-25 13:48:47 -0500387 &dev_attr_iface_vlan_priority.attr,
388 &dev_attr_iface_vlan_enabled.attr,
Mike Christie8d079132011-07-25 13:48:40 -0500389 &dev_attr_ipv4_iface_ipaddress.attr,
390 &dev_attr_ipv4_iface_gateway.attr,
391 &dev_attr_ipv4_iface_subnet.attr,
392 &dev_attr_ipv4_iface_bootproto.attr,
393 &dev_attr_ipv6_iface_ipaddress.attr,
394 &dev_attr_ipv6_iface_link_local_addr.attr,
395 &dev_attr_ipv6_iface_router_addr.attr,
396 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
Mike Christie88f4f512011-10-06 03:56:58 -0500397 &dev_attr_ipv6_iface_link_local_autocfg.attr,
Vikas Chaudhary8c7d40f2011-08-01 03:26:12 -0700398 &dev_attr_iface_mtu.attr,
Vikas Chaudharyfcb51242011-08-01 03:26:18 -0700399 &dev_attr_iface_port.attr,
Mike Christie8d079132011-07-25 13:48:40 -0500400 NULL,
401};
402
403static struct attribute_group iscsi_iface_group = {
404 .attrs = iscsi_iface_attrs,
405 .is_visible = iscsi_iface_attr_is_visible,
406};
407
408struct iscsi_iface *
409iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
410 uint32_t iface_type, uint32_t iface_num, int dd_size)
411{
412 struct iscsi_iface *iface;
413 int err;
414
415 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
416 if (!iface)
417 return NULL;
418
419 iface->transport = transport;
420 iface->iface_type = iface_type;
421 iface->iface_num = iface_num;
422 iface->dev.release = iscsi_iface_release;
423 iface->dev.class = &iscsi_iface_class;
424 /* parent reference released in iscsi_iface_release */
425 iface->dev.parent = get_device(&shost->shost_gendev);
426 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
427 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
428 iface_num);
429 else
430 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
431 iface_num);
432
433 err = device_register(&iface->dev);
434 if (err)
435 goto free_iface;
436
437 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
438 if (err)
439 goto unreg_iface;
440
441 if (dd_size)
442 iface->dd_data = &iface[1];
443 return iface;
444
445unreg_iface:
446 device_unregister(&iface->dev);
447 return NULL;
448
449free_iface:
450 put_device(iface->dev.parent);
451 kfree(iface);
452 return NULL;
453}
454EXPORT_SYMBOL_GPL(iscsi_create_iface);
455
456void iscsi_destroy_iface(struct iscsi_iface *iface)
457{
458 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
459 device_unregister(&iface->dev);
460}
461EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
462
Mike Christie90eeb012011-07-25 13:48:50 -0500463/*
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -0400464 * Interface to display flash node params to sysfs
465 */
466
467#define ISCSI_FLASHNODE_ATTR(_prefix, _name, _mode, _show, _store) \
468struct device_attribute dev_attr_##_prefix##_##_name = \
469 __ATTR(_name, _mode, _show, _store)
470
471/* flash node session attrs show */
472#define iscsi_flashnode_sess_attr_show(type, name, param) \
473static ssize_t \
474show_##type##_##name(struct device *dev, struct device_attribute *attr, \
475 char *buf) \
476{ \
477 struct iscsi_bus_flash_session *fnode_sess = \
478 iscsi_dev_to_flash_session(dev);\
479 struct iscsi_transport *t = fnode_sess->transport; \
480 return t->get_flashnode_param(fnode_sess, param, buf); \
481} \
482
483
484#define iscsi_flashnode_sess_attr(type, name, param) \
485 iscsi_flashnode_sess_attr_show(type, name, param) \
486static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
487 show_##type##_##name, NULL);
488
489/* Flash node session attributes */
490
491iscsi_flashnode_sess_attr(fnode, auto_snd_tgt_disable,
492 ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE);
493iscsi_flashnode_sess_attr(fnode, discovery_session,
494 ISCSI_FLASHNODE_DISCOVERY_SESS);
495iscsi_flashnode_sess_attr(fnode, portal_type, ISCSI_FLASHNODE_PORTAL_TYPE);
496iscsi_flashnode_sess_attr(fnode, entry_enable, ISCSI_FLASHNODE_ENTRY_EN);
497iscsi_flashnode_sess_attr(fnode, immediate_data, ISCSI_FLASHNODE_IMM_DATA_EN);
498iscsi_flashnode_sess_attr(fnode, initial_r2t, ISCSI_FLASHNODE_INITIAL_R2T_EN);
499iscsi_flashnode_sess_attr(fnode, data_seq_in_order,
500 ISCSI_FLASHNODE_DATASEQ_INORDER);
501iscsi_flashnode_sess_attr(fnode, data_pdu_in_order,
502 ISCSI_FLASHNODE_PDU_INORDER);
503iscsi_flashnode_sess_attr(fnode, chap_auth, ISCSI_FLASHNODE_CHAP_AUTH_EN);
504iscsi_flashnode_sess_attr(fnode, discovery_logout,
505 ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN);
506iscsi_flashnode_sess_attr(fnode, bidi_chap, ISCSI_FLASHNODE_BIDI_CHAP_EN);
507iscsi_flashnode_sess_attr(fnode, discovery_auth_optional,
508 ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL);
509iscsi_flashnode_sess_attr(fnode, erl, ISCSI_FLASHNODE_ERL);
510iscsi_flashnode_sess_attr(fnode, first_burst_len, ISCSI_FLASHNODE_FIRST_BURST);
511iscsi_flashnode_sess_attr(fnode, def_time2wait, ISCSI_FLASHNODE_DEF_TIME2WAIT);
512iscsi_flashnode_sess_attr(fnode, def_time2retain,
513 ISCSI_FLASHNODE_DEF_TIME2RETAIN);
514iscsi_flashnode_sess_attr(fnode, max_outstanding_r2t, ISCSI_FLASHNODE_MAX_R2T);
515iscsi_flashnode_sess_attr(fnode, isid, ISCSI_FLASHNODE_ISID);
516iscsi_flashnode_sess_attr(fnode, tsid, ISCSI_FLASHNODE_TSID);
517iscsi_flashnode_sess_attr(fnode, max_burst_len, ISCSI_FLASHNODE_MAX_BURST);
518iscsi_flashnode_sess_attr(fnode, def_taskmgmt_tmo,
519 ISCSI_FLASHNODE_DEF_TASKMGMT_TMO);
520iscsi_flashnode_sess_attr(fnode, targetalias, ISCSI_FLASHNODE_ALIAS);
521iscsi_flashnode_sess_attr(fnode, targetname, ISCSI_FLASHNODE_NAME);
522iscsi_flashnode_sess_attr(fnode, tpgt, ISCSI_FLASHNODE_TPGT);
523iscsi_flashnode_sess_attr(fnode, discovery_parent_idx,
524 ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX);
525iscsi_flashnode_sess_attr(fnode, discovery_parent_type,
526 ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE);
527iscsi_flashnode_sess_attr(fnode, chap_in_idx, ISCSI_FLASHNODE_CHAP_IN_IDX);
528iscsi_flashnode_sess_attr(fnode, chap_out_idx, ISCSI_FLASHNODE_CHAP_OUT_IDX);
529iscsi_flashnode_sess_attr(fnode, username, ISCSI_FLASHNODE_USERNAME);
530iscsi_flashnode_sess_attr(fnode, username_in, ISCSI_FLASHNODE_USERNAME_IN);
531iscsi_flashnode_sess_attr(fnode, password, ISCSI_FLASHNODE_PASSWORD);
532iscsi_flashnode_sess_attr(fnode, password_in, ISCSI_FLASHNODE_PASSWORD_IN);
533iscsi_flashnode_sess_attr(fnode, is_boot_target, ISCSI_FLASHNODE_IS_BOOT_TGT);
534
535static struct attribute *iscsi_flashnode_sess_attrs[] = {
536 &dev_attr_fnode_auto_snd_tgt_disable.attr,
537 &dev_attr_fnode_discovery_session.attr,
538 &dev_attr_fnode_portal_type.attr,
539 &dev_attr_fnode_entry_enable.attr,
540 &dev_attr_fnode_immediate_data.attr,
541 &dev_attr_fnode_initial_r2t.attr,
542 &dev_attr_fnode_data_seq_in_order.attr,
543 &dev_attr_fnode_data_pdu_in_order.attr,
544 &dev_attr_fnode_chap_auth.attr,
545 &dev_attr_fnode_discovery_logout.attr,
546 &dev_attr_fnode_bidi_chap.attr,
547 &dev_attr_fnode_discovery_auth_optional.attr,
548 &dev_attr_fnode_erl.attr,
549 &dev_attr_fnode_first_burst_len.attr,
550 &dev_attr_fnode_def_time2wait.attr,
551 &dev_attr_fnode_def_time2retain.attr,
552 &dev_attr_fnode_max_outstanding_r2t.attr,
553 &dev_attr_fnode_isid.attr,
554 &dev_attr_fnode_tsid.attr,
555 &dev_attr_fnode_max_burst_len.attr,
556 &dev_attr_fnode_def_taskmgmt_tmo.attr,
557 &dev_attr_fnode_targetalias.attr,
558 &dev_attr_fnode_targetname.attr,
559 &dev_attr_fnode_tpgt.attr,
560 &dev_attr_fnode_discovery_parent_idx.attr,
561 &dev_attr_fnode_discovery_parent_type.attr,
562 &dev_attr_fnode_chap_in_idx.attr,
563 &dev_attr_fnode_chap_out_idx.attr,
564 &dev_attr_fnode_username.attr,
565 &dev_attr_fnode_username_in.attr,
566 &dev_attr_fnode_password.attr,
567 &dev_attr_fnode_password_in.attr,
568 &dev_attr_fnode_is_boot_target.attr,
569 NULL,
570};
571
572static umode_t iscsi_flashnode_sess_attr_is_visible(struct kobject *kobj,
573 struct attribute *attr,
574 int i)
575{
576 struct device *dev = container_of(kobj, struct device, kobj);
577 struct iscsi_bus_flash_session *fnode_sess =
578 iscsi_dev_to_flash_session(dev);
579 struct iscsi_transport *t = fnode_sess->transport;
580 int param;
581
582 if (attr == &dev_attr_fnode_auto_snd_tgt_disable.attr) {
583 param = ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE;
584 } else if (attr == &dev_attr_fnode_discovery_session.attr) {
585 param = ISCSI_FLASHNODE_DISCOVERY_SESS;
586 } else if (attr == &dev_attr_fnode_portal_type.attr) {
587 param = ISCSI_FLASHNODE_PORTAL_TYPE;
588 } else if (attr == &dev_attr_fnode_entry_enable.attr) {
589 param = ISCSI_FLASHNODE_ENTRY_EN;
590 } else if (attr == &dev_attr_fnode_immediate_data.attr) {
591 param = ISCSI_FLASHNODE_IMM_DATA_EN;
592 } else if (attr == &dev_attr_fnode_initial_r2t.attr) {
593 param = ISCSI_FLASHNODE_INITIAL_R2T_EN;
594 } else if (attr == &dev_attr_fnode_data_seq_in_order.attr) {
595 param = ISCSI_FLASHNODE_DATASEQ_INORDER;
596 } else if (attr == &dev_attr_fnode_data_pdu_in_order.attr) {
597 param = ISCSI_FLASHNODE_PDU_INORDER;
598 } else if (attr == &dev_attr_fnode_chap_auth.attr) {
599 param = ISCSI_FLASHNODE_CHAP_AUTH_EN;
600 } else if (attr == &dev_attr_fnode_discovery_logout.attr) {
601 param = ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN;
602 } else if (attr == &dev_attr_fnode_bidi_chap.attr) {
603 param = ISCSI_FLASHNODE_BIDI_CHAP_EN;
604 } else if (attr == &dev_attr_fnode_discovery_auth_optional.attr) {
605 param = ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL;
606 } else if (attr == &dev_attr_fnode_erl.attr) {
607 param = ISCSI_FLASHNODE_ERL;
608 } else if (attr == &dev_attr_fnode_first_burst_len.attr) {
609 param = ISCSI_FLASHNODE_FIRST_BURST;
610 } else if (attr == &dev_attr_fnode_def_time2wait.attr) {
611 param = ISCSI_FLASHNODE_DEF_TIME2WAIT;
612 } else if (attr == &dev_attr_fnode_def_time2retain.attr) {
613 param = ISCSI_FLASHNODE_DEF_TIME2RETAIN;
614 } else if (attr == &dev_attr_fnode_max_outstanding_r2t.attr) {
615 param = ISCSI_FLASHNODE_MAX_R2T;
616 } else if (attr == &dev_attr_fnode_isid.attr) {
617 param = ISCSI_FLASHNODE_ISID;
618 } else if (attr == &dev_attr_fnode_tsid.attr) {
619 param = ISCSI_FLASHNODE_TSID;
620 } else if (attr == &dev_attr_fnode_max_burst_len.attr) {
621 param = ISCSI_FLASHNODE_MAX_BURST;
622 } else if (attr == &dev_attr_fnode_def_taskmgmt_tmo.attr) {
623 param = ISCSI_FLASHNODE_DEF_TASKMGMT_TMO;
624 } else if (attr == &dev_attr_fnode_targetalias.attr) {
625 param = ISCSI_FLASHNODE_ALIAS;
626 } else if (attr == &dev_attr_fnode_targetname.attr) {
627 param = ISCSI_FLASHNODE_NAME;
628 } else if (attr == &dev_attr_fnode_tpgt.attr) {
629 param = ISCSI_FLASHNODE_TPGT;
630 } else if (attr == &dev_attr_fnode_discovery_parent_idx.attr) {
631 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX;
632 } else if (attr == &dev_attr_fnode_discovery_parent_type.attr) {
633 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE;
634 } else if (attr == &dev_attr_fnode_chap_in_idx.attr) {
635 param = ISCSI_FLASHNODE_CHAP_IN_IDX;
636 } else if (attr == &dev_attr_fnode_chap_out_idx.attr) {
637 param = ISCSI_FLASHNODE_CHAP_OUT_IDX;
638 } else if (attr == &dev_attr_fnode_username.attr) {
639 param = ISCSI_FLASHNODE_USERNAME;
640 } else if (attr == &dev_attr_fnode_username_in.attr) {
641 param = ISCSI_FLASHNODE_USERNAME_IN;
642 } else if (attr == &dev_attr_fnode_password.attr) {
643 param = ISCSI_FLASHNODE_PASSWORD;
644 } else if (attr == &dev_attr_fnode_password_in.attr) {
645 param = ISCSI_FLASHNODE_PASSWORD_IN;
646 } else if (attr == &dev_attr_fnode_is_boot_target.attr) {
647 param = ISCSI_FLASHNODE_IS_BOOT_TGT;
648 } else {
649 WARN_ONCE(1, "Invalid flashnode session attr");
650 return 0;
651 }
652
653 return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
654}
655
656static struct attribute_group iscsi_flashnode_sess_attr_group = {
657 .attrs = iscsi_flashnode_sess_attrs,
658 .is_visible = iscsi_flashnode_sess_attr_is_visible,
659};
660
661static const struct attribute_group *iscsi_flashnode_sess_attr_groups[] = {
662 &iscsi_flashnode_sess_attr_group,
663 NULL,
664};
665
666static void iscsi_flashnode_sess_release(struct device *dev)
667{
668 struct iscsi_bus_flash_session *fnode_sess =
669 iscsi_dev_to_flash_session(dev);
670
671 kfree(fnode_sess->targetname);
672 kfree(fnode_sess->targetalias);
673 kfree(fnode_sess->portal_type);
674 kfree(fnode_sess);
675}
676
677struct device_type iscsi_flashnode_sess_dev_type = {
678 .name = "iscsi_flashnode_sess_dev_type",
679 .groups = iscsi_flashnode_sess_attr_groups,
680 .release = iscsi_flashnode_sess_release,
681};
682
683/* flash node connection attrs show */
684#define iscsi_flashnode_conn_attr_show(type, name, param) \
685static ssize_t \
686show_##type##_##name(struct device *dev, struct device_attribute *attr, \
687 char *buf) \
688{ \
689 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);\
690 struct iscsi_bus_flash_session *fnode_sess = \
691 iscsi_flash_conn_to_flash_session(fnode_conn);\
692 struct iscsi_transport *t = fnode_conn->transport; \
693 return t->get_flashnode_param(fnode_sess, param, buf); \
694} \
695
696
697#define iscsi_flashnode_conn_attr(type, name, param) \
698 iscsi_flashnode_conn_attr_show(type, name, param) \
699static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
700 show_##type##_##name, NULL);
701
702/* Flash node connection attributes */
703
704iscsi_flashnode_conn_attr(fnode, is_fw_assigned_ipv6,
705 ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6);
706iscsi_flashnode_conn_attr(fnode, header_digest, ISCSI_FLASHNODE_HDR_DGST_EN);
707iscsi_flashnode_conn_attr(fnode, data_digest, ISCSI_FLASHNODE_DATA_DGST_EN);
708iscsi_flashnode_conn_attr(fnode, snack_req, ISCSI_FLASHNODE_SNACK_REQ_EN);
709iscsi_flashnode_conn_attr(fnode, tcp_timestamp_stat,
710 ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT);
711iscsi_flashnode_conn_attr(fnode, tcp_nagle_disable,
712 ISCSI_FLASHNODE_TCP_NAGLE_DISABLE);
713iscsi_flashnode_conn_attr(fnode, tcp_wsf_disable,
714 ISCSI_FLASHNODE_TCP_WSF_DISABLE);
715iscsi_flashnode_conn_attr(fnode, tcp_timer_scale,
716 ISCSI_FLASHNODE_TCP_TIMER_SCALE);
717iscsi_flashnode_conn_attr(fnode, tcp_timestamp_enable,
718 ISCSI_FLASHNODE_TCP_TIMESTAMP_EN);
719iscsi_flashnode_conn_attr(fnode, fragment_disable,
720 ISCSI_FLASHNODE_IP_FRAG_DISABLE);
721iscsi_flashnode_conn_attr(fnode, keepalive_tmo, ISCSI_FLASHNODE_KEEPALIVE_TMO);
722iscsi_flashnode_conn_attr(fnode, port, ISCSI_FLASHNODE_PORT);
723iscsi_flashnode_conn_attr(fnode, ipaddress, ISCSI_FLASHNODE_IPADDR);
724iscsi_flashnode_conn_attr(fnode, max_recv_dlength,
725 ISCSI_FLASHNODE_MAX_RECV_DLENGTH);
726iscsi_flashnode_conn_attr(fnode, max_xmit_dlength,
727 ISCSI_FLASHNODE_MAX_XMIT_DLENGTH);
728iscsi_flashnode_conn_attr(fnode, local_port, ISCSI_FLASHNODE_LOCAL_PORT);
729iscsi_flashnode_conn_attr(fnode, ipv4_tos, ISCSI_FLASHNODE_IPV4_TOS);
730iscsi_flashnode_conn_attr(fnode, ipv6_traffic_class, ISCSI_FLASHNODE_IPV6_TC);
731iscsi_flashnode_conn_attr(fnode, ipv6_flow_label,
732 ISCSI_FLASHNODE_IPV6_FLOW_LABEL);
733iscsi_flashnode_conn_attr(fnode, redirect_ipaddr,
734 ISCSI_FLASHNODE_REDIRECT_IPADDR);
735iscsi_flashnode_conn_attr(fnode, max_segment_size,
736 ISCSI_FLASHNODE_MAX_SEGMENT_SIZE);
737iscsi_flashnode_conn_attr(fnode, link_local_ipv6,
738 ISCSI_FLASHNODE_LINK_LOCAL_IPV6);
739iscsi_flashnode_conn_attr(fnode, tcp_xmit_wsf, ISCSI_FLASHNODE_TCP_XMIT_WSF);
740iscsi_flashnode_conn_attr(fnode, tcp_recv_wsf, ISCSI_FLASHNODE_TCP_RECV_WSF);
741iscsi_flashnode_conn_attr(fnode, statsn, ISCSI_FLASHNODE_STATSN);
742iscsi_flashnode_conn_attr(fnode, exp_statsn, ISCSI_FLASHNODE_EXP_STATSN);
743
744static struct attribute *iscsi_flashnode_conn_attrs[] = {
745 &dev_attr_fnode_is_fw_assigned_ipv6.attr,
746 &dev_attr_fnode_header_digest.attr,
747 &dev_attr_fnode_data_digest.attr,
748 &dev_attr_fnode_snack_req.attr,
749 &dev_attr_fnode_tcp_timestamp_stat.attr,
750 &dev_attr_fnode_tcp_nagle_disable.attr,
751 &dev_attr_fnode_tcp_wsf_disable.attr,
752 &dev_attr_fnode_tcp_timer_scale.attr,
753 &dev_attr_fnode_tcp_timestamp_enable.attr,
754 &dev_attr_fnode_fragment_disable.attr,
755 &dev_attr_fnode_max_recv_dlength.attr,
756 &dev_attr_fnode_max_xmit_dlength.attr,
757 &dev_attr_fnode_keepalive_tmo.attr,
758 &dev_attr_fnode_port.attr,
759 &dev_attr_fnode_ipaddress.attr,
760 &dev_attr_fnode_redirect_ipaddr.attr,
761 &dev_attr_fnode_max_segment_size.attr,
762 &dev_attr_fnode_local_port.attr,
763 &dev_attr_fnode_ipv4_tos.attr,
764 &dev_attr_fnode_ipv6_traffic_class.attr,
765 &dev_attr_fnode_ipv6_flow_label.attr,
766 &dev_attr_fnode_link_local_ipv6.attr,
767 &dev_attr_fnode_tcp_xmit_wsf.attr,
768 &dev_attr_fnode_tcp_recv_wsf.attr,
769 &dev_attr_fnode_statsn.attr,
770 &dev_attr_fnode_exp_statsn.attr,
771 NULL,
772};
773
774static umode_t iscsi_flashnode_conn_attr_is_visible(struct kobject *kobj,
775 struct attribute *attr,
776 int i)
777{
778 struct device *dev = container_of(kobj, struct device, kobj);
779 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
780 struct iscsi_transport *t = fnode_conn->transport;
781 int param;
782
783 if (attr == &dev_attr_fnode_is_fw_assigned_ipv6.attr) {
784 param = ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6;
785 } else if (attr == &dev_attr_fnode_header_digest.attr) {
786 param = ISCSI_FLASHNODE_HDR_DGST_EN;
787 } else if (attr == &dev_attr_fnode_data_digest.attr) {
788 param = ISCSI_FLASHNODE_DATA_DGST_EN;
789 } else if (attr == &dev_attr_fnode_snack_req.attr) {
790 param = ISCSI_FLASHNODE_SNACK_REQ_EN;
791 } else if (attr == &dev_attr_fnode_tcp_timestamp_stat.attr) {
792 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT;
793 } else if (attr == &dev_attr_fnode_tcp_nagle_disable.attr) {
794 param = ISCSI_FLASHNODE_TCP_NAGLE_DISABLE;
795 } else if (attr == &dev_attr_fnode_tcp_wsf_disable.attr) {
796 param = ISCSI_FLASHNODE_TCP_WSF_DISABLE;
797 } else if (attr == &dev_attr_fnode_tcp_timer_scale.attr) {
798 param = ISCSI_FLASHNODE_TCP_TIMER_SCALE;
799 } else if (attr == &dev_attr_fnode_tcp_timestamp_enable.attr) {
800 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_EN;
801 } else if (attr == &dev_attr_fnode_fragment_disable.attr) {
802 param = ISCSI_FLASHNODE_IP_FRAG_DISABLE;
803 } else if (attr == &dev_attr_fnode_max_recv_dlength.attr) {
804 param = ISCSI_FLASHNODE_MAX_RECV_DLENGTH;
805 } else if (attr == &dev_attr_fnode_max_xmit_dlength.attr) {
806 param = ISCSI_FLASHNODE_MAX_XMIT_DLENGTH;
807 } else if (attr == &dev_attr_fnode_keepalive_tmo.attr) {
808 param = ISCSI_FLASHNODE_KEEPALIVE_TMO;
809 } else if (attr == &dev_attr_fnode_port.attr) {
810 param = ISCSI_FLASHNODE_PORT;
811 } else if (attr == &dev_attr_fnode_ipaddress.attr) {
812 param = ISCSI_FLASHNODE_IPADDR;
813 } else if (attr == &dev_attr_fnode_redirect_ipaddr.attr) {
814 param = ISCSI_FLASHNODE_REDIRECT_IPADDR;
815 } else if (attr == &dev_attr_fnode_max_segment_size.attr) {
816 param = ISCSI_FLASHNODE_MAX_SEGMENT_SIZE;
817 } else if (attr == &dev_attr_fnode_local_port.attr) {
818 param = ISCSI_FLASHNODE_LOCAL_PORT;
819 } else if (attr == &dev_attr_fnode_ipv4_tos.attr) {
820 param = ISCSI_FLASHNODE_IPV4_TOS;
821 } else if (attr == &dev_attr_fnode_ipv6_traffic_class.attr) {
822 param = ISCSI_FLASHNODE_IPV6_TC;
823 } else if (attr == &dev_attr_fnode_ipv6_flow_label.attr) {
824 param = ISCSI_FLASHNODE_IPV6_FLOW_LABEL;
825 } else if (attr == &dev_attr_fnode_link_local_ipv6.attr) {
826 param = ISCSI_FLASHNODE_LINK_LOCAL_IPV6;
827 } else if (attr == &dev_attr_fnode_tcp_xmit_wsf.attr) {
828 param = ISCSI_FLASHNODE_TCP_XMIT_WSF;
829 } else if (attr == &dev_attr_fnode_tcp_recv_wsf.attr) {
830 param = ISCSI_FLASHNODE_TCP_RECV_WSF;
831 } else if (attr == &dev_attr_fnode_statsn.attr) {
832 param = ISCSI_FLASHNODE_STATSN;
833 } else if (attr == &dev_attr_fnode_exp_statsn.attr) {
834 param = ISCSI_FLASHNODE_EXP_STATSN;
835 } else {
836 WARN_ONCE(1, "Invalid flashnode connection attr");
837 return 0;
838 }
839
840 return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
841}
842
843static struct attribute_group iscsi_flashnode_conn_attr_group = {
844 .attrs = iscsi_flashnode_conn_attrs,
845 .is_visible = iscsi_flashnode_conn_attr_is_visible,
846};
847
848static const struct attribute_group *iscsi_flashnode_conn_attr_groups[] = {
849 &iscsi_flashnode_conn_attr_group,
850 NULL,
851};
852
853static void iscsi_flashnode_conn_release(struct device *dev)
854{
855 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
856
857 kfree(fnode_conn->ipaddress);
858 kfree(fnode_conn->redirect_ipaddr);
859 kfree(fnode_conn->link_local_ipv6_addr);
860 kfree(fnode_conn);
861}
862
863struct device_type iscsi_flashnode_conn_dev_type = {
864 .name = "iscsi_flashnode_conn_dev_type",
865 .groups = iscsi_flashnode_conn_attr_groups,
866 .release = iscsi_flashnode_conn_release,
867};
868
869struct bus_type iscsi_flashnode_bus;
870
871int iscsi_flashnode_bus_match(struct device *dev,
872 struct device_driver *drv)
873{
874 if (dev->bus == &iscsi_flashnode_bus)
875 return 1;
876 return 0;
877}
878EXPORT_SYMBOL_GPL(iscsi_flashnode_bus_match);
879
880struct bus_type iscsi_flashnode_bus = {
881 .name = "iscsi_flashnode",
882 .match = &iscsi_flashnode_bus_match,
883};
884
885/**
886 * iscsi_create_flashnode_sess - Add flashnode session entry in sysfs
887 * @shost: pointer to host data
888 * @index: index of flashnode to add in sysfs
889 * @transport: pointer to transport data
890 * @dd_size: total size to allocate
891 *
892 * Adds a sysfs entry for the flashnode session attributes
893 *
894 * Returns:
895 * pointer to allocated flashnode sess on sucess
896 * %NULL on failure
897 */
898struct iscsi_bus_flash_session *
899iscsi_create_flashnode_sess(struct Scsi_Host *shost, int index,
900 struct iscsi_transport *transport,
901 int dd_size)
902{
903 struct iscsi_bus_flash_session *fnode_sess;
904 int err;
905
906 fnode_sess = kzalloc(sizeof(*fnode_sess) + dd_size, GFP_KERNEL);
907 if (!fnode_sess)
908 return NULL;
909
910 fnode_sess->transport = transport;
911 fnode_sess->target_id = index;
912 fnode_sess->dev.type = &iscsi_flashnode_sess_dev_type;
913 fnode_sess->dev.bus = &iscsi_flashnode_bus;
914 fnode_sess->dev.parent = &shost->shost_gendev;
915 dev_set_name(&fnode_sess->dev, "flashnode_sess-%u:%u",
916 shost->host_no, index);
917
918 err = device_register(&fnode_sess->dev);
919 if (err)
920 goto free_fnode_sess;
921
922 if (dd_size)
923 fnode_sess->dd_data = &fnode_sess[1];
924
925 return fnode_sess;
926
927free_fnode_sess:
928 kfree(fnode_sess);
929 return NULL;
930}
931EXPORT_SYMBOL_GPL(iscsi_create_flashnode_sess);
932
933/**
934 * iscsi_create_flashnode_conn - Add flashnode conn entry in sysfs
935 * @shost: pointer to host data
936 * @fnode_sess: pointer to the parent flashnode session entry
937 * @transport: pointer to transport data
938 * @dd_size: total size to allocate
939 *
940 * Adds a sysfs entry for the flashnode connection attributes
941 *
942 * Returns:
943 * pointer to allocated flashnode conn on success
944 * %NULL on failure
945 */
946struct iscsi_bus_flash_conn *
947iscsi_create_flashnode_conn(struct Scsi_Host *shost,
948 struct iscsi_bus_flash_session *fnode_sess,
949 struct iscsi_transport *transport,
950 int dd_size)
951{
952 struct iscsi_bus_flash_conn *fnode_conn;
953 int err;
954
955 fnode_conn = kzalloc(sizeof(*fnode_conn) + dd_size, GFP_KERNEL);
956 if (!fnode_conn)
957 return NULL;
958
959 fnode_conn->transport = transport;
960 fnode_conn->dev.type = &iscsi_flashnode_conn_dev_type;
961 fnode_conn->dev.bus = &iscsi_flashnode_bus;
962 fnode_conn->dev.parent = &fnode_sess->dev;
963 dev_set_name(&fnode_conn->dev, "flashnode_conn-%u:%u:0",
964 shost->host_no, fnode_sess->target_id);
965
966 err = device_register(&fnode_conn->dev);
967 if (err)
968 goto free_fnode_conn;
969
970 if (dd_size)
971 fnode_conn->dd_data = &fnode_conn[1];
972
973 return fnode_conn;
974
975free_fnode_conn:
976 kfree(fnode_conn);
977 return NULL;
978}
979EXPORT_SYMBOL_GPL(iscsi_create_flashnode_conn);
980
981/**
982 * iscsi_is_flashnode_conn_dev - verify passed device is to be flashnode conn
983 * @dev: device to verify
984 * @data: pointer to data containing value to use for verification
985 *
986 * Verifies if the passed device is flashnode conn device
987 *
988 * Returns:
989 * 1 on success
990 * 0 on failure
991 */
992int iscsi_is_flashnode_conn_dev(struct device *dev, void *data)
993{
994 return dev->bus == &iscsi_flashnode_bus;
995}
996EXPORT_SYMBOL_GPL(iscsi_is_flashnode_conn_dev);
997
998static int iscsi_destroy_flashnode_conn(struct iscsi_bus_flash_conn *fnode_conn)
999{
1000 device_unregister(&fnode_conn->dev);
1001 return 0;
1002}
1003
1004static int flashnode_match_index(struct device *dev, void *data)
1005{
1006 struct iscsi_bus_flash_session *fnode_sess = NULL;
1007 int ret = 0;
1008
1009 if (!iscsi_flashnode_bus_match(dev, NULL))
1010 goto exit_match_index;
1011
1012 fnode_sess = iscsi_dev_to_flash_session(dev);
1013 ret = (fnode_sess->target_id == *((int *)data)) ? 1 : 0;
1014
1015exit_match_index:
1016 return ret;
1017}
1018
1019/**
1020 * iscsi_get_flashnode_by_index -finds flashnode session entry by index
1021 * @shost: pointer to host data
1022 * @data: pointer to data containing value to use for comparison
1023 * @fn: function pointer that does actual comparison
1024 *
1025 * Finds the flashnode session object for the passed index
1026 *
1027 * Returns:
1028 * pointer to found flashnode session object on success
1029 * %NULL on failure
1030 */
1031static struct iscsi_bus_flash_session *
1032iscsi_get_flashnode_by_index(struct Scsi_Host *shost, void *data,
1033 int (*fn)(struct device *dev, void *data))
1034{
1035 struct iscsi_bus_flash_session *fnode_sess = NULL;
1036 struct device *dev;
1037
1038 dev = device_find_child(&shost->shost_gendev, data, fn);
1039 if (dev)
1040 fnode_sess = iscsi_dev_to_flash_session(dev);
1041
1042 return fnode_sess;
1043}
1044
1045/**
1046 * iscsi_find_flashnode_sess - finds flashnode session entry
1047 * @shost: pointer to host data
1048 * @data: pointer to data containing value to use for comparison
1049 * @fn: function pointer that does actual comparison
1050 *
1051 * Finds the flashnode session object comparing the data passed using logic
1052 * defined in passed function pointer
1053 *
1054 * Returns:
1055 * pointer to found flashnode session device object on success
1056 * %NULL on failure
1057 */
1058struct device *
1059iscsi_find_flashnode_sess(struct Scsi_Host *shost, void *data,
1060 int (*fn)(struct device *dev, void *data))
1061{
1062 struct device *dev;
1063
1064 dev = device_find_child(&shost->shost_gendev, data, fn);
1065 return dev;
1066}
1067EXPORT_SYMBOL_GPL(iscsi_find_flashnode_sess);
1068
1069/**
1070 * iscsi_find_flashnode_conn - finds flashnode connection entry
1071 * @fnode_sess: pointer to parent flashnode session entry
1072 * @data: pointer to data containing value to use for comparison
1073 * @fn: function pointer that does actual comparison
1074 *
1075 * Finds the flashnode connection object comparing the data passed using logic
1076 * defined in passed function pointer
1077 *
1078 * Returns:
1079 * pointer to found flashnode connection device object on success
1080 * %NULL on failure
1081 */
1082struct device *
1083iscsi_find_flashnode_conn(struct iscsi_bus_flash_session *fnode_sess,
1084 void *data,
1085 int (*fn)(struct device *dev, void *data))
1086{
1087 struct device *dev;
1088
1089 dev = device_find_child(&fnode_sess->dev, data, fn);
1090 return dev;
1091}
1092EXPORT_SYMBOL_GPL(iscsi_find_flashnode_conn);
1093
1094static int iscsi_iter_destroy_flashnode_conn_fn(struct device *dev, void *data)
1095{
1096 if (!iscsi_is_flashnode_conn_dev(dev, NULL))
1097 return 0;
1098
1099 return iscsi_destroy_flashnode_conn(iscsi_dev_to_flash_conn(dev));
1100}
1101
1102/**
1103 * iscsi_destroy_flashnode_sess - destory flashnode session entry
1104 * @fnode_sess: pointer to flashnode session entry to be destroyed
1105 *
1106 * Deletes the flashnode session entry and all children flashnode connection
1107 * entries from sysfs
1108 */
1109void iscsi_destroy_flashnode_sess(struct iscsi_bus_flash_session *fnode_sess)
1110{
1111 int err;
1112
1113 err = device_for_each_child(&fnode_sess->dev, NULL,
1114 iscsi_iter_destroy_flashnode_conn_fn);
1115 if (err)
1116 pr_err("Could not delete all connections for %s. Error %d.\n",
1117 fnode_sess->dev.kobj.name, err);
1118
1119 device_unregister(&fnode_sess->dev);
1120}
1121EXPORT_SYMBOL_GPL(iscsi_destroy_flashnode_sess);
1122
1123static int iscsi_iter_destroy_flashnode_fn(struct device *dev, void *data)
1124{
1125 if (!iscsi_flashnode_bus_match(dev, NULL))
1126 return 0;
1127
1128 iscsi_destroy_flashnode_sess(iscsi_dev_to_flash_session(dev));
1129 return 0;
1130}
1131
1132/**
1133 * iscsi_destroy_all_flashnode - destory all flashnode session entries
1134 * @shost: pointer to host data
1135 *
1136 * Destroys all the flashnode session entries and all corresponding children
1137 * flashnode connection entries from sysfs
1138 */
1139void iscsi_destroy_all_flashnode(struct Scsi_Host *shost)
1140{
1141 device_for_each_child(&shost->shost_gendev, NULL,
1142 iscsi_iter_destroy_flashnode_fn);
1143}
1144EXPORT_SYMBOL_GPL(iscsi_destroy_all_flashnode);
1145
1146/*
Mike Christie90eeb012011-07-25 13:48:50 -05001147 * BSG support
1148 */
1149/**
1150 * iscsi_bsg_host_dispatch - Dispatch command to LLD.
1151 * @job: bsg job to be processed
1152 */
1153static int iscsi_bsg_host_dispatch(struct bsg_job *job)
1154{
1155 struct Scsi_Host *shost = iscsi_job_to_shost(job);
1156 struct iscsi_bsg_request *req = job->request;
1157 struct iscsi_bsg_reply *reply = job->reply;
1158 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1159 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
1160 int ret;
1161
1162 /* check if we have the msgcode value at least */
1163 if (job->request_len < sizeof(uint32_t)) {
1164 ret = -ENOMSG;
1165 goto fail_host_msg;
1166 }
1167
1168 /* Validate the host command */
1169 switch (req->msgcode) {
1170 case ISCSI_BSG_HST_VENDOR:
1171 cmdlen += sizeof(struct iscsi_bsg_host_vendor);
1172 if ((shost->hostt->vendor_id == 0L) ||
1173 (req->rqst_data.h_vendor.vendor_id !=
1174 shost->hostt->vendor_id)) {
1175 ret = -ESRCH;
1176 goto fail_host_msg;
1177 }
1178 break;
1179 default:
1180 ret = -EBADR;
1181 goto fail_host_msg;
1182 }
1183
1184 /* check if we really have all the request data needed */
1185 if (job->request_len < cmdlen) {
1186 ret = -ENOMSG;
1187 goto fail_host_msg;
1188 }
1189
1190 ret = i->iscsi_transport->bsg_request(job);
1191 if (!ret)
1192 return 0;
1193
1194fail_host_msg:
1195 /* return the errno failure code as the only status */
1196 BUG_ON(job->reply_len < sizeof(uint32_t));
1197 reply->reply_payload_rcv_len = 0;
1198 reply->result = ret;
1199 job->reply_len = sizeof(uint32_t);
1200 bsg_job_done(job, ret, 0);
1201 return 0;
1202}
1203
1204/**
1205 * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
1206 * @shost: shost for iscsi_host
Marcos Paulo de Souzaeae627e2011-11-02 11:17:17 -02001207 * @ihost: iscsi_cls_host adding the structures to
Mike Christie90eeb012011-07-25 13:48:50 -05001208 */
1209static int
1210iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
1211{
1212 struct device *dev = &shost->shost_gendev;
1213 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1214 struct request_queue *q;
1215 char bsg_name[20];
1216 int ret;
1217
1218 if (!i->iscsi_transport->bsg_request)
1219 return -ENOTSUPP;
1220
1221 snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
1222
1223 q = __scsi_alloc_queue(shost, bsg_request_fn);
1224 if (!q)
1225 return -ENOMEM;
1226
1227 ret = bsg_setup_queue(dev, q, bsg_name, iscsi_bsg_host_dispatch, 0);
1228 if (ret) {
1229 shost_printk(KERN_ERR, shost, "bsg interface failed to "
1230 "initialize - no request queue\n");
1231 blk_cleanup_queue(q);
1232 return ret;
1233 }
1234
1235 ihost->bsg_q = q;
1236 return 0;
1237}
1238
Mike Christie30a6c652006-04-06 21:13:39 -05001239static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +01001240 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -05001241{
1242 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001243 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -05001244
1245 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -06001246 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -05001247 mutex_init(&ihost->mutex);
Mike Christie90eeb012011-07-25 13:48:50 -05001248
1249 iscsi_bsg_host_add(shost, ihost);
1250 /* ignore any bsg add error - we just can't do sgio */
1251
1252 return 0;
1253}
1254
1255static int iscsi_remove_host(struct transport_container *tc,
1256 struct device *dev, struct device *cdev)
1257{
1258 struct Scsi_Host *shost = dev_to_shost(dev);
1259 struct iscsi_cls_host *ihost = shost->shost_data;
1260
1261 if (ihost->bsg_q) {
Tejun Heo86072d82012-06-04 20:40:54 -07001262 bsg_unregister_queue(ihost->bsg_q);
Mike Christie90eeb012011-07-25 13:48:50 -05001263 blk_cleanup_queue(ihost->bsg_q);
1264 }
Mike Christie30a6c652006-04-06 21:13:39 -05001265 return 0;
1266}
1267
1268static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
1269 "iscsi_host",
1270 iscsi_setup_host,
Mike Christie90eeb012011-07-25 13:48:50 -05001271 iscsi_remove_host,
Mike Christie30a6c652006-04-06 21:13:39 -05001272 NULL);
1273
Alex Aizman0896b752005-08-04 19:33:07 -07001274static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
1275 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 NULL,
1277 NULL,
1278 NULL);
1279
Alex Aizman0896b752005-08-04 19:33:07 -07001280static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
1281 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 NULL,
1283 NULL,
1284 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -07001285
1286static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +01001287static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001288
Mike Christie7b7232f2006-02-01 21:06:49 -06001289static LIST_HEAD(sesslist);
1290static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -07001291static LIST_HEAD(connlist);
1292static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Mike Christieb5c7a122006-04-06 21:13:33 -05001294static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
1295{
1296 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
1297 return sess->sid;
1298}
1299
1300/*
1301 * Returns the matching session to a given sid
1302 */
1303static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -06001304{
1305 unsigned long flags;
1306 struct iscsi_cls_session *sess;
1307
1308 spin_lock_irqsave(&sesslock, flags);
1309 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -05001310 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001311 spin_unlock_irqrestore(&sesslock, flags);
1312 return sess;
1313 }
1314 }
1315 spin_unlock_irqrestore(&sesslock, flags);
1316 return NULL;
1317}
1318
Mike Christieb5c7a122006-04-06 21:13:33 -05001319/*
1320 * Returns the matching connection to a given sid / cid tuple
1321 */
1322static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -06001323{
1324 unsigned long flags;
1325 struct iscsi_cls_conn *conn;
1326
1327 spin_lock_irqsave(&connlock, flags);
1328 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -05001329 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001330 spin_unlock_irqrestore(&connlock, flags);
1331 return conn;
1332 }
1333 }
1334 spin_unlock_irqrestore(&connlock, flags);
1335 return NULL;
1336}
1337
Mike Christie7b8631b2006-01-13 18:05:50 -06001338/*
1339 * The following functions can be used by LLDs that allocate
1340 * their own scsi_hosts or by software iscsi LLDs
1341 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001342static struct {
1343 int value;
1344 char *name;
1345} iscsi_session_state_names[] = {
1346 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
1347 { ISCSI_SESSION_FAILED, "FAILED" },
1348 { ISCSI_SESSION_FREE, "FREE" },
1349};
1350
Adrian Bunk3b0f2082008-02-13 23:30:17 +02001351static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -06001352{
1353 int i;
1354 char *name = NULL;
1355
1356 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
1357 if (iscsi_session_state_names[i].value == state) {
1358 name = iscsi_session_state_names[i].name;
1359 break;
1360 }
1361 }
1362 return name;
1363}
1364
1365int iscsi_session_chkready(struct iscsi_cls_session *session)
1366{
1367 unsigned long flags;
1368 int err;
1369
1370 spin_lock_irqsave(&session->lock, flags);
1371 switch (session->state) {
1372 case ISCSI_SESSION_LOGGED_IN:
1373 err = 0;
1374 break;
1375 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -05001376 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -06001377 break;
1378 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -05001379 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -06001380 break;
1381 default:
1382 err = DID_NO_CONNECT << 16;
1383 break;
1384 }
1385 spin_unlock_irqrestore(&session->lock, flags);
1386 return err;
1387}
1388EXPORT_SYMBOL_GPL(iscsi_session_chkready);
1389
Manish Rangankar17fa5752011-07-25 13:48:52 -05001390int iscsi_is_session_online(struct iscsi_cls_session *session)
1391{
1392 unsigned long flags;
1393 int ret = 0;
1394
1395 spin_lock_irqsave(&session->lock, flags);
1396 if (session->state == ISCSI_SESSION_LOGGED_IN)
1397 ret = 1;
1398 spin_unlock_irqrestore(&session->lock, flags);
1399 return ret;
1400}
1401EXPORT_SYMBOL_GPL(iscsi_is_session_online);
1402
Mike Christie7b8631b2006-01-13 18:05:50 -06001403static void iscsi_session_release(struct device *dev)
1404{
1405 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001406 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Mike Christie7b8631b2006-01-13 18:05:50 -06001408 shost = iscsi_session_to_shost(session);
1409 scsi_host_put(shost);
Mike Christie632248a2009-08-20 15:11:01 -05001410 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001411 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -06001412}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Nilesh Javali6260a5d2012-02-27 03:08:51 -08001414int iscsi_is_session_dev(const struct device *dev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001415{
1416 return dev->release == iscsi_session_release;
1417}
Nilesh Javali6260a5d2012-02-27 03:08:51 -08001418EXPORT_SYMBOL_GPL(iscsi_is_session_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Mike Christiea4804cd2008-05-21 15:54:00 -05001420static int iscsi_iter_session_fn(struct device *dev, void *data)
1421{
1422 void (* fn) (struct iscsi_cls_session *) = data;
1423
1424 if (!iscsi_is_session_dev(dev))
1425 return 0;
1426 fn(iscsi_dev_to_session(dev));
1427 return 0;
1428}
1429
1430void iscsi_host_for_each_session(struct Scsi_Host *shost,
1431 void (*fn)(struct iscsi_cls_session *))
1432{
1433 device_for_each_child(&shost->shost_gendev, fn,
1434 iscsi_iter_session_fn);
1435}
1436EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
1437
Mike Christie8aae18a2008-01-31 13:36:48 -06001438/**
1439 * iscsi_scan_finished - helper to report when running scans are done
1440 * @shost: scsi host
1441 * @time: scan run time
1442 *
1443 * This function can be used by drives like qla4xxx to report to the scsi
1444 * layer when the scans it kicked off at module load time are done.
1445 */
1446int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
1447{
Mike Christie32c6e1b2008-05-21 15:53:58 -05001448 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -06001449 /*
1450 * qla4xxx will have kicked off some session unblocks before calling
1451 * scsi_scan_host, so just wait for them to complete.
1452 */
1453 return !atomic_read(&ihost->nr_scans);
1454}
1455EXPORT_SYMBOL_GPL(iscsi_scan_finished);
1456
Mike Christie79706342008-05-21 15:54:12 -05001457struct iscsi_scan_data {
1458 unsigned int channel;
1459 unsigned int id;
1460 unsigned int lun;
1461};
1462
1463static int iscsi_user_scan_session(struct device *dev, void *data)
1464{
1465 struct iscsi_scan_data *scan_data = data;
1466 struct iscsi_cls_session *session;
1467 struct Scsi_Host *shost;
1468 struct iscsi_cls_host *ihost;
1469 unsigned long flags;
1470 unsigned int id;
1471
1472 if (!iscsi_is_session_dev(dev))
1473 return 0;
1474
1475 session = iscsi_dev_to_session(dev);
Mike Christie632248a2009-08-20 15:11:01 -05001476
1477 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
1478
Mike Christie79706342008-05-21 15:54:12 -05001479 shost = iscsi_session_to_shost(session);
1480 ihost = shost->shost_data;
1481
1482 mutex_lock(&ihost->mutex);
1483 spin_lock_irqsave(&session->lock, flags);
1484 if (session->state != ISCSI_SESSION_LOGGED_IN) {
1485 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05001486 goto user_scan_exit;
Mike Christie79706342008-05-21 15:54:12 -05001487 }
1488 id = session->target_id;
1489 spin_unlock_irqrestore(&session->lock, flags);
1490
1491 if (id != ISCSI_MAX_TARGET) {
1492 if ((scan_data->channel == SCAN_WILD_CARD ||
1493 scan_data->channel == 0) &&
1494 (scan_data->id == SCAN_WILD_CARD ||
1495 scan_data->id == id))
1496 scsi_scan_target(&session->dev, 0, id,
1497 scan_data->lun, 1);
1498 }
Mike Christie632248a2009-08-20 15:11:01 -05001499
1500user_scan_exit:
Mike Christie79706342008-05-21 15:54:12 -05001501 mutex_unlock(&ihost->mutex);
Mike Christie632248a2009-08-20 15:11:01 -05001502 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
Mike Christie79706342008-05-21 15:54:12 -05001503 return 0;
1504}
1505
Mike Christie30a6c652006-04-06 21:13:39 -05001506static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
1507 uint id, uint lun)
1508{
Mike Christie79706342008-05-21 15:54:12 -05001509 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -05001510
Mike Christie79706342008-05-21 15:54:12 -05001511 scan_data.channel = channel;
1512 scan_data.id = id;
1513 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -05001514
Mike Christie79706342008-05-21 15:54:12 -05001515 return device_for_each_child(&shost->shost_gendev, &scan_data,
1516 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -05001517}
1518
Mike Christiebd976f62008-01-31 13:36:46 -06001519static void iscsi_scan_session(struct work_struct *work)
1520{
1521 struct iscsi_cls_session *session =
1522 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -06001523 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001524 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -05001525 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -06001526
Mike Christie79706342008-05-21 15:54:12 -05001527 scan_data.channel = 0;
1528 scan_data.id = SCAN_WILD_CARD;
1529 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -06001530
Mike Christie79706342008-05-21 15:54:12 -05001531 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -06001532 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -06001533}
1534
Mike Christiec01be6d2010-07-22 16:59:49 +05301535/**
1536 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
Randy Dunlape6d4ef42010-08-14 13:05:41 -07001537 * @cmd: scsi cmd passed to scsi eh handler
Mike Christiec01be6d2010-07-22 16:59:49 +05301538 *
1539 * If the session is down this function will wait for the recovery
1540 * timer to fire or for the session to be logged back in. If the
1541 * recovery timer fires then FAST_IO_FAIL is returned. The caller
1542 * should pass this error value to the scsi eh.
1543 */
1544int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
1545{
1546 struct iscsi_cls_session *session =
1547 starget_to_session(scsi_target(cmd->device));
1548 unsigned long flags;
1549 int ret = 0;
1550
1551 spin_lock_irqsave(&session->lock, flags);
1552 while (session->state != ISCSI_SESSION_LOGGED_IN) {
1553 if (session->state == ISCSI_SESSION_FREE) {
1554 ret = FAST_IO_FAIL;
1555 break;
1556 }
1557 spin_unlock_irqrestore(&session->lock, flags);
1558 msleep(1000);
1559 spin_lock_irqsave(&session->lock, flags);
1560 }
1561 spin_unlock_irqrestore(&session->lock, flags);
1562 return ret;
1563}
1564EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
1565
David Howellsc4028952006-11-22 14:57:56 +00001566static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -05001567{
David Howellsc4028952006-11-22 14:57:56 +00001568 struct iscsi_cls_session *session =
1569 container_of(work, struct iscsi_cls_session,
1570 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -06001571 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -05001572
Mike Christie322d7392008-01-31 13:36:52 -06001573 iscsi_cls_session_printk(KERN_INFO, session,
1574 "session recovery timed out after %d secs\n",
1575 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -05001576
Mike Christie6eabafb2008-01-31 13:36:43 -06001577 spin_lock_irqsave(&session->lock, flags);
1578 switch (session->state) {
1579 case ISCSI_SESSION_FAILED:
1580 session->state = ISCSI_SESSION_FREE;
1581 break;
1582 case ISCSI_SESSION_LOGGED_IN:
1583 case ISCSI_SESSION_FREE:
1584 /* we raced with the unblock's flush */
1585 spin_unlock_irqrestore(&session->lock, flags);
1586 return;
1587 }
1588 spin_unlock_irqrestore(&session->lock, flags);
1589
Mike Christie30a6c652006-04-06 21:13:39 -05001590 if (session->transport->session_recovery_timedout)
1591 session->transport->session_recovery_timedout(session);
1592
Mike Christie632248a2009-08-20 15:11:01 -05001593 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
Mike Christie5d9fb5c2012-05-17 23:56:57 -05001594 scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
Mike Christie632248a2009-08-20 15:11:01 -05001595 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -05001596}
1597
Mike Christie45ab33b2008-03-04 13:26:55 -06001598static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -05001599{
Mike Christie45ab33b2008-03-04 13:26:55 -06001600 struct iscsi_cls_session *session =
1601 container_of(work, struct iscsi_cls_session,
1602 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -06001603 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001604 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -06001605 unsigned long flags;
1606
Mike Christie632248a2009-08-20 15:11:01 -05001607 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
Mike Christie45ab33b2008-03-04 13:26:55 -06001608 /*
1609 * The recovery and unblock work get run from the same workqueue,
1610 * so try to cancel it if it was going to run after this unblock.
1611 */
1612 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -06001613 spin_lock_irqsave(&session->lock, flags);
1614 session->state = ISCSI_SESSION_LOGGED_IN;
1615 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -06001616 /* start IO */
Mike Christie5d9fb5c2012-05-17 23:56:57 -05001617 scsi_target_unblock(&session->dev, SDEV_RUNNING);
Mike Christie8aae18a2008-01-31 13:36:48 -06001618 /*
1619 * Only do kernel scanning if the driver is properly hooked into
1620 * the async scanning code (drivers like iscsi_tcp do login and
1621 * scanning from userspace).
1622 */
1623 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -06001624 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -06001625 atomic_inc(&ihost->nr_scans);
1626 }
Mike Christie632248a2009-08-20 15:11:01 -05001627 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -06001628}
Mike Christie45ab33b2008-03-04 13:26:55 -06001629
1630/**
1631 * iscsi_unblock_session - set a session as logged in and start IO.
1632 * @session: iscsi session
1633 *
1634 * Mark a session as ready to accept IO.
1635 */
1636void iscsi_unblock_session(struct iscsi_cls_session *session)
1637{
1638 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
1639 /*
1640 * make sure all the events have completed before tell the driver
1641 * it is safe
1642 */
1643 flush_workqueue(iscsi_eh_timer_workq);
1644}
Mike Christie30a6c652006-04-06 21:13:39 -05001645EXPORT_SYMBOL_GPL(iscsi_unblock_session);
1646
Mike Christie45ab33b2008-03-04 13:26:55 -06001647static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -05001648{
Mike Christie45ab33b2008-03-04 13:26:55 -06001649 struct iscsi_cls_session *session =
1650 container_of(work, struct iscsi_cls_session,
1651 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -06001652 unsigned long flags;
1653
Mike Christie632248a2009-08-20 15:11:01 -05001654 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -06001655 spin_lock_irqsave(&session->lock, flags);
1656 session->state = ISCSI_SESSION_FAILED;
1657 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -05001658 scsi_target_block(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001659 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
Mike Christiefdd46dc2009-11-11 16:34:34 -06001660 if (session->recovery_tmo >= 0)
1661 queue_delayed_work(iscsi_eh_timer_workq,
1662 &session->recovery_work,
1663 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -05001664}
Mike Christie45ab33b2008-03-04 13:26:55 -06001665
1666void iscsi_block_session(struct iscsi_cls_session *session)
1667{
1668 queue_work(iscsi_eh_timer_workq, &session->block_work);
1669}
Mike Christie30a6c652006-04-06 21:13:39 -05001670EXPORT_SYMBOL_GPL(iscsi_block_session);
1671
Mike Christie26974782007-12-13 12:43:29 -06001672static void __iscsi_unbind_session(struct work_struct *work)
1673{
1674 struct iscsi_cls_session *session =
1675 container_of(work, struct iscsi_cls_session,
1676 unbind_work);
1677 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001678 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -05001679 unsigned long flags;
Mike Christie8d4a6902011-10-06 03:56:57 -05001680 unsigned int target_id;
Mike Christie26974782007-12-13 12:43:29 -06001681
Mike Christie632248a2009-08-20 15:11:01 -05001682 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
1683
Mike Christie26974782007-12-13 12:43:29 -06001684 /* Prevent new scans and make sure scanning is not in progress */
1685 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -05001686 spin_lock_irqsave(&session->lock, flags);
1687 if (session->target_id == ISCSI_MAX_TARGET) {
1688 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001689 mutex_unlock(&ihost->mutex);
1690 return;
1691 }
Mike Christie8d4a6902011-10-06 03:56:57 -05001692
1693 target_id = session->target_id;
Mike Christie79706342008-05-21 15:54:12 -05001694 session->target_id = ISCSI_MAX_TARGET;
1695 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001696 mutex_unlock(&ihost->mutex);
1697
Mike Christie8d4a6902011-10-06 03:56:57 -05001698 if (session->ida_used)
1699 ida_simple_remove(&iscsi_sess_ida, target_id);
1700
Mike Christie26974782007-12-13 12:43:29 -06001701 scsi_remove_target(&session->dev);
1702 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -05001703 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
Mike Christie26974782007-12-13 12:43:29 -06001704}
1705
Mike Christie7b8631b2006-01-13 18:05:50 -06001706struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -05001707iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1708 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06001709{
1710 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Mike Christie5d91e202008-05-21 15:54:01 -05001712 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -05001713 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001714 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -05001715 return NULL;
1716
Mike Christie7b8631b2006-01-13 18:05:50 -06001717 session->transport = transport;
Mike Christie0c70d842011-12-05 16:44:01 -06001718 session->creator = -1;
Mike Christie30a6c652006-04-06 21:13:39 -05001719 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -06001720 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +00001721 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -05001722 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -06001723 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
1724 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -06001725 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -06001726 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001727 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -06001728
Mike Christie6a8a0d32006-06-28 12:00:31 -05001729 /* this is released in the dev's release function */
1730 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -05001731 session->dev.parent = &shost->shost_gendev;
1732 session->dev.release = iscsi_session_release;
1733 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -05001734 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -05001735 session->dd_data = &session[1];
Mike Christie632248a2009-08-20 15:11:01 -05001736
1737 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001738 return session;
1739}
1740EXPORT_SYMBOL_GPL(iscsi_alloc_session);
1741
Mike Christie6a8a0d32006-06-28 12:00:31 -05001742int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05001743{
1744 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001745 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -06001746 unsigned long flags;
Mike Christie8d4a6902011-10-06 03:56:57 -05001747 int id = 0;
Mike Christie8434aa82006-06-28 12:00:30 -05001748 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -05001749
Mike Christie30a6c652006-04-06 21:13:39 -05001750 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -06001751 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -05001752
Mike Christie8d4a6902011-10-06 03:56:57 -05001753 if (target_id == ISCSI_MAX_TARGET) {
1754 id = ida_simple_get(&iscsi_sess_ida, 0, 0, GFP_KERNEL);
Mike Christie79706342008-05-21 15:54:12 -05001755
Mike Christie8d4a6902011-10-06 03:56:57 -05001756 if (id < 0) {
Mike Christie79706342008-05-21 15:54:12 -05001757 iscsi_cls_session_printk(KERN_ERR, session,
Mike Christie8d4a6902011-10-06 03:56:57 -05001758 "Failure in Target ID Allocation\n");
1759 return id;
Mike Christie79706342008-05-21 15:54:12 -05001760 }
Mike Christie8d4a6902011-10-06 03:56:57 -05001761 session->target_id = (unsigned int)id;
1762 session->ida_used = true;
1763 } else
1764 session->target_id = target_id;
Mike Christie30a6c652006-04-06 21:13:39 -05001765
Kay Sievers71610f52008-12-03 22:41:36 +01001766 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -05001767 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001768 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001769 iscsi_cls_session_printk(KERN_ERR, session,
1770 "could not register session's dev\n");
Mike Christie8d4a6902011-10-06 03:56:57 -05001771 goto release_ida;
Mike Christie7b8631b2006-01-13 18:05:50 -06001772 }
1773 transport_register_device(&session->dev);
1774
Mike Christie26974782007-12-13 12:43:29 -06001775 spin_lock_irqsave(&sesslock, flags);
1776 list_add(&session->sess_list, &sesslist);
1777 spin_unlock_irqrestore(&sesslock, flags);
1778
Mike Christie26974782007-12-13 12:43:29 -06001779 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -05001780 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001781 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -05001782
Mike Christie8d4a6902011-10-06 03:56:57 -05001783release_ida:
1784 if (session->ida_used)
1785 ida_simple_remove(&iscsi_sess_ida, session->target_id);
1786
Mike Christie8434aa82006-06-28 12:00:30 -05001787 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -06001788}
Mike Christie8434aa82006-06-28 12:00:30 -05001789EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -06001790
1791/**
Mike Christie8434aa82006-06-28 12:00:30 -05001792 * iscsi_create_session - create iscsi class session
1793 * @shost: scsi host
1794 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -05001795 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -05001796 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -06001797 *
Mike Christie8434aa82006-06-28 12:00:30 -05001798 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001799 */
Mike Christie8434aa82006-06-28 12:00:30 -05001800struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -05001801iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1802 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05001803{
1804 struct iscsi_cls_session *session;
1805
Mike Christie5d91e202008-05-21 15:54:01 -05001806 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -05001807 if (!session)
1808 return NULL;
1809
Mike Christie6a8a0d32006-06-28 12:00:31 -05001810 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -05001811 iscsi_free_session(session);
1812 return NULL;
1813 }
1814 return session;
1815}
1816EXPORT_SYMBOL_GPL(iscsi_create_session);
1817
Mike Christie26974782007-12-13 12:43:29 -06001818static void iscsi_conn_release(struct device *dev)
1819{
1820 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
1821 struct device *parent = conn->dev.parent;
1822
Mike Christie632248a2009-08-20 15:11:01 -05001823 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
Mike Christie26974782007-12-13 12:43:29 -06001824 kfree(conn);
1825 put_device(parent);
1826}
1827
1828static int iscsi_is_conn_dev(const struct device *dev)
1829{
1830 return dev->release == iscsi_conn_release;
1831}
1832
1833static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
1834{
1835 if (!iscsi_is_conn_dev(dev))
1836 return 0;
1837 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
1838}
1839
Mike Christie8434aa82006-06-28 12:00:30 -05001840void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001841{
Mike Christie30a6c652006-04-06 21:13:39 -05001842 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie26974782007-12-13 12:43:29 -06001843 unsigned long flags;
1844 int err;
Mike Christie30a6c652006-04-06 21:13:39 -05001845
Mike Christie632248a2009-08-20 15:11:01 -05001846 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
1847
Mike Christie26974782007-12-13 12:43:29 -06001848 spin_lock_irqsave(&sesslock, flags);
1849 list_del(&session->sess_list);
1850 spin_unlock_irqrestore(&sesslock, flags);
1851
Mike Christie45ab33b2008-03-04 13:26:55 -06001852 /* make sure there are no blocks/unblocks queued */
1853 flush_workqueue(iscsi_eh_timer_workq);
1854 /* make sure the timedout callout is not running */
1855 if (!cancel_delayed_work(&session->recovery_work))
1856 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -06001857 /*
1858 * If we are blocked let commands flow again. The lld or iscsi
1859 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -06001860 * We assume that LLD will not be calling block/unblock while
1861 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -06001862 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001863 spin_lock_irqsave(&session->lock, flags);
1864 session->state = ISCSI_SESSION_FREE;
1865 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -06001866
Mike Christie5d9fb5c2012-05-17 23:56:57 -05001867 scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
Mike Christie45ab33b2008-03-04 13:26:55 -06001868 /* flush running scans then delete devices */
Mike Christie06d25af2009-03-05 14:46:02 -06001869 scsi_flush_work(shost);
Mike Christie45ab33b2008-03-04 13:26:55 -06001870 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -05001871
Mike Christie26974782007-12-13 12:43:29 -06001872 /* hw iscsi may not have removed all connections from session */
1873 err = device_for_each_child(&session->dev, NULL,
1874 iscsi_iter_destroy_conn_fn);
1875 if (err)
Mike Christie322d7392008-01-31 13:36:52 -06001876 iscsi_cls_session_printk(KERN_ERR, session,
1877 "Could not delete all connections "
1878 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -05001879
Mike Christie7b8631b2006-01-13 18:05:50 -06001880 transport_unregister_device(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001881
1882 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001883 device_del(&session->dev);
1884}
1885EXPORT_SYMBOL_GPL(iscsi_remove_session);
1886
1887void iscsi_free_session(struct iscsi_cls_session *session)
1888{
Mike Christie632248a2009-08-20 15:11:01 -05001889 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
Mike Christie26974782007-12-13 12:43:29 -06001890 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -05001891 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001892}
Mike Christie8434aa82006-06-28 12:00:30 -05001893EXPORT_SYMBOL_GPL(iscsi_free_session);
1894
1895/**
1896 * iscsi_destroy_session - destroy iscsi session
1897 * @session: iscsi_session
1898 *
1899 * Can be called by a LLD or iscsi_transport. There must not be
1900 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -05001901 */
Mike Christie8434aa82006-06-28 12:00:30 -05001902int iscsi_destroy_session(struct iscsi_cls_session *session)
1903{
1904 iscsi_remove_session(session);
Mike Christie632248a2009-08-20 15:11:01 -05001905 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001906 iscsi_free_session(session);
1907 return 0;
1908}
Mike Christie7b8631b2006-01-13 18:05:50 -06001909EXPORT_SYMBOL_GPL(iscsi_destroy_session);
1910
Mike Christie7b8631b2006-01-13 18:05:50 -06001911/**
1912 * iscsi_create_conn - create iscsi class connection
1913 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -05001914 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -06001915 * @cid: connection id
1916 *
1917 * This can be called from a LLD or iscsi_transport. The connection
1918 * is child of the session so cid must be unique for all connections
1919 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -05001920 *
1921 * Since we do not support MCS, cid will normally be zero. In some cases
1922 * for software iscsi we could be trying to preallocate a connection struct
1923 * in which case there could be two connection structs and cid would be
1924 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -05001925 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001926struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05001927iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -06001928{
1929 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -06001930 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -06001931 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -06001932 int err;
1933
Mike Christie5d91e202008-05-21 15:54:01 -05001934 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001935 if (!conn)
1936 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -05001937 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06001938 conn->dd_data = &conn[1];
1939
Mike Christie22a39fb2011-02-16 15:04:33 -06001940 mutex_init(&conn->ep_mutex);
Mike Christie7b8631b2006-01-13 18:05:50 -06001941 INIT_LIST_HEAD(&conn->conn_list);
1942 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -05001943 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001944
1945 /* this is released in the dev's release function */
1946 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -04001947 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -05001948
Kay Sievers71610f52008-12-03 22:41:36 +01001949 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001950 conn->dev.parent = &session->dev;
1951 conn->dev.release = iscsi_conn_release;
1952 err = device_register(&conn->dev);
1953 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001954 iscsi_cls_session_printk(KERN_ERR, session, "could not "
1955 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001956 goto release_parent_ref;
1957 }
1958 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -06001959
1960 spin_lock_irqsave(&connlock, flags);
1961 list_add(&conn->conn_list, &connlist);
Mike Christie26974782007-12-13 12:43:29 -06001962 spin_unlock_irqrestore(&connlock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05001963
1964 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001965 return conn;
1966
1967release_parent_ref:
1968 put_device(&session->dev);
1969free_conn:
1970 kfree(conn);
1971 return NULL;
1972}
1973
1974EXPORT_SYMBOL_GPL(iscsi_create_conn);
1975
1976/**
1977 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -05001978 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -06001979 *
Mike Christie26974782007-12-13 12:43:29 -06001980 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001981 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001982int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1983{
Mike Christie26974782007-12-13 12:43:29 -06001984 unsigned long flags;
1985
1986 spin_lock_irqsave(&connlock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001987 list_del(&conn->conn_list);
1988 spin_unlock_irqrestore(&connlock, flags);
1989
Mike Christie7b8631b2006-01-13 18:05:50 -06001990 transport_unregister_device(&conn->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001991 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001992 device_unregister(&conn->dev);
1993 return 0;
1994}
Mike Christie7b8631b2006-01-13 18:05:50 -06001995EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1996
1997/*
Mike Christie7b8631b2006-01-13 18:05:50 -06001998 * iscsi interface functions
1999 */
Alex Aizman0896b752005-08-04 19:33:07 -07002000static struct iscsi_internal *
2001iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Alex Aizman0896b752005-08-04 19:33:07 -07002003 struct iscsi_internal *priv;
2004 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Alex Aizman0896b752005-08-04 19:33:07 -07002006 spin_lock_irqsave(&iscsi_transport_lock, flags);
2007 list_for_each_entry(priv, &iscsi_transports, list) {
2008 if (tt == priv->iscsi_transport) {
2009 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2010 return priv;
2011 }
2012 }
2013 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2014 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Alex Aizman0896b752005-08-04 19:33:07 -07002017static int
Michael Chan43514772009-06-08 18:14:41 -07002018iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -05002019{
Michael Chan43514772009-06-08 18:14:41 -07002020 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -07002021}
2022
Mike Christie7b7232f2006-02-01 21:06:49 -06002023int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -07002024 char *data, uint32_t data_size)
2025{
2026 struct nlmsghdr *nlh;
2027 struct sk_buff *skb;
2028 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07002029 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05002030 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07002031 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
2032 data_size);
2033
Mike Christie790f39a2006-05-18 20:31:39 -05002034 priv = iscsi_if_transport_lookup(conn->transport);
2035 if (!priv)
2036 return -EINVAL;
2037
Mike Christie43a145a2006-10-16 18:09:38 -04002038 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002039 if (!skb) {
Mike Christiee5bd7b52008-09-24 11:46:10 -05002040 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06002041 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
2042 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07002043 return -ENOMEM;
2044 }
2045
Michael Chan43514772009-06-08 18:14:41 -07002046 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07002047 ev = NLMSG_DATA(nlh);
2048 memset(ev, 0, sizeof(*ev));
2049 ev->transport_handle = iscsi_handle(conn->transport);
2050 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05002051 ev->r.recv_req.cid = conn->cid;
2052 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07002053 pdu = (char*)ev + sizeof(*ev);
2054 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
2055 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
2056
Michael Chan43514772009-06-08 18:14:41 -07002057 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002058}
2059EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
2060
Michael Chan43514772009-06-08 18:14:41 -07002061int iscsi_offload_mesg(struct Scsi_Host *shost,
2062 struct iscsi_transport *transport, uint32_t type,
2063 char *data, uint16_t data_size)
2064{
2065 struct nlmsghdr *nlh;
2066 struct sk_buff *skb;
2067 struct iscsi_uevent *ev;
2068 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
2069
Michael Chana541f842009-07-29 08:49:52 +00002070 skb = alloc_skb(len, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07002071 if (!skb) {
2072 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
2073 return -ENOMEM;
2074 }
2075
2076 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2077 ev = NLMSG_DATA(nlh);
2078 memset(ev, 0, sizeof(*ev));
2079 ev->type = type;
2080 ev->transport_handle = iscsi_handle(transport);
2081 switch (type) {
2082 case ISCSI_KEVENT_PATH_REQ:
2083 ev->r.req_path.host_no = shost->host_no;
2084 break;
2085 case ISCSI_KEVENT_IF_DOWN:
2086 ev->r.notify_if_down.host_no = shost->host_no;
2087 break;
2088 }
2089
2090 memcpy((char *)ev + sizeof(*ev), data, data_size);
2091
Michael Chana541f842009-07-29 08:49:52 +00002092 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07002093}
2094EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
2095
Mike Christiee5bd7b52008-09-24 11:46:10 -05002096void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07002097{
2098 struct nlmsghdr *nlh;
2099 struct sk_buff *skb;
2100 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05002101 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07002102 int len = NLMSG_SPACE(sizeof(*ev));
2103
Mike Christie790f39a2006-05-18 20:31:39 -05002104 priv = iscsi_if_transport_lookup(conn->transport);
2105 if (!priv)
2106 return;
2107
Mike Christie43a145a2006-10-16 18:09:38 -04002108 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002109 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06002110 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2111 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07002112 return;
2113 }
2114
Michael Chan43514772009-06-08 18:14:41 -07002115 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07002116 ev = NLMSG_DATA(nlh);
2117 ev->transport_handle = iscsi_handle(conn->transport);
2118 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07002119 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05002120 ev->r.connerror.cid = conn->cid;
2121 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07002122
Michael Chan43514772009-06-08 18:14:41 -07002123 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002124
Mike Christie322d7392008-01-31 13:36:52 -06002125 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
2126 error);
Alex Aizman0896b752005-08-04 19:33:07 -07002127}
Mike Christiee5bd7b52008-09-24 11:46:10 -05002128EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07002129
Manish Rangankar17fa5752011-07-25 13:48:52 -05002130void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
2131 enum iscsi_conn_state state)
2132{
2133 struct nlmsghdr *nlh;
2134 struct sk_buff *skb;
2135 struct iscsi_uevent *ev;
2136 struct iscsi_internal *priv;
2137 int len = NLMSG_SPACE(sizeof(*ev));
2138
2139 priv = iscsi_if_transport_lookup(conn->transport);
2140 if (!priv)
2141 return;
2142
2143 skb = alloc_skb(len, GFP_ATOMIC);
2144 if (!skb) {
2145 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2146 "conn login (%d)\n", state);
2147 return;
2148 }
2149
2150 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2151 ev = NLMSG_DATA(nlh);
2152 ev->transport_handle = iscsi_handle(conn->transport);
2153 ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
2154 ev->r.conn_login.state = state;
2155 ev->r.conn_login.cid = conn->cid;
2156 ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
2157 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2158
2159 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
2160 state);
2161}
2162EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
2163
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302164void iscsi_post_host_event(uint32_t host_no, struct iscsi_transport *transport,
2165 enum iscsi_host_event_code code, uint32_t data_size,
2166 uint8_t *data)
2167{
2168 struct nlmsghdr *nlh;
2169 struct sk_buff *skb;
2170 struct iscsi_uevent *ev;
2171 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
2172
Mike Christie49d0e642012-03-06 16:09:01 -06002173 skb = alloc_skb(len, GFP_NOIO);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302174 if (!skb) {
2175 printk(KERN_ERR "gracefully ignored host event (%d):%d OOM\n",
2176 host_no, code);
2177 return;
2178 }
2179
2180 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2181 ev = NLMSG_DATA(nlh);
2182 ev->transport_handle = iscsi_handle(transport);
2183 ev->type = ISCSI_KEVENT_HOST_EVENT;
2184 ev->r.host_event.host_no = host_no;
2185 ev->r.host_event.code = code;
2186 ev->r.host_event.data_size = data_size;
2187
2188 if (data_size)
2189 memcpy((char *)ev + sizeof(*ev), data, data_size);
2190
Mike Christie49d0e642012-03-06 16:09:01 -06002191 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302192}
2193EXPORT_SYMBOL_GPL(iscsi_post_host_event);
2194
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302195void iscsi_ping_comp_event(uint32_t host_no, struct iscsi_transport *transport,
2196 uint32_t status, uint32_t pid, uint32_t data_size,
2197 uint8_t *data)
2198{
2199 struct nlmsghdr *nlh;
2200 struct sk_buff *skb;
2201 struct iscsi_uevent *ev;
2202 int len = NLMSG_SPACE(sizeof(*ev) + data_size);
2203
Mike Christie49d0e642012-03-06 16:09:01 -06002204 skb = alloc_skb(len, GFP_NOIO);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302205 if (!skb) {
2206 printk(KERN_ERR "gracefully ignored ping comp: OOM\n");
2207 return;
2208 }
2209
2210 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2211 ev = NLMSG_DATA(nlh);
2212 ev->transport_handle = iscsi_handle(transport);
2213 ev->type = ISCSI_KEVENT_PING_COMP;
2214 ev->r.ping_comp.host_no = host_no;
2215 ev->r.ping_comp.status = status;
2216 ev->r.ping_comp.pid = pid;
2217 ev->r.ping_comp.data_size = data_size;
2218 memcpy((char *)ev + sizeof(*ev), data, data_size);
2219
Mike Christie49d0e642012-03-06 16:09:01 -06002220 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302221}
2222EXPORT_SYMBOL_GPL(iscsi_ping_comp_event);
2223
Alex Aizman0896b752005-08-04 19:33:07 -07002224static int
Michael Chan43514772009-06-08 18:14:41 -07002225iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
2226 void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07002227{
2228 struct sk_buff *skb;
2229 struct nlmsghdr *nlh;
2230 int len = NLMSG_SPACE(size);
2231 int flags = multi ? NLM_F_MULTI : 0;
2232 int t = done ? NLMSG_DONE : type;
2233
Mike Christie43a145a2006-10-16 18:09:38 -04002234 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05002235 if (!skb) {
2236 printk(KERN_ERR "Could not allocate skb to send reply.\n");
2237 return -ENOMEM;
2238 }
Alex Aizman0896b752005-08-04 19:33:07 -07002239
Michael Chan43514772009-06-08 18:14:41 -07002240 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07002241 nlh->nlmsg_flags = flags;
2242 memcpy(NLMSG_DATA(nlh), payload, size);
Michael Chan43514772009-06-08 18:14:41 -07002243 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002244}
2245
2246static int
Mike Christie5b940ad2006-02-01 21:06:56 -06002247iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07002248{
2249 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
2250 struct iscsi_stats *stats;
2251 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06002252 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07002253 struct nlmsghdr *nlhstat;
2254 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05002255 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -07002256 int len = NLMSG_SPACE(sizeof(*ev) +
2257 sizeof(struct iscsi_stats) +
2258 sizeof(struct iscsi_stats_custom) *
2259 ISCSI_STATS_CUSTOM_MAX);
2260 int err = 0;
2261
Mike Christie790f39a2006-05-18 20:31:39 -05002262 priv = iscsi_if_transport_lookup(transport);
2263 if (!priv)
2264 return -EINVAL;
2265
Mike Christieb5c7a122006-04-06 21:13:33 -05002266 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07002267 if (!conn)
2268 return -EEXIST;
2269
2270 do {
2271 int actual_size;
2272
Mike Christie43a145a2006-10-16 18:09:38 -04002273 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002274 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06002275 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
2276 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07002277 return -ENOMEM;
2278 }
2279
Michael Chan43514772009-06-08 18:14:41 -07002280 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07002281 (len - sizeof(*nlhstat)), 0);
2282 evstat = NLMSG_DATA(nlhstat);
2283 memset(evstat, 0, sizeof(*evstat));
2284 evstat->transport_handle = iscsi_handle(conn->transport);
2285 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05002286 evstat->u.get_stats.cid =
2287 ev->u.get_stats.cid;
2288 evstat->u.get_stats.sid =
2289 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07002290 stats = (struct iscsi_stats *)
2291 ((char*)evstat + sizeof(*evstat));
2292 memset(stats, 0, sizeof(*stats));
2293
Mike Christie7b7232f2006-02-01 21:06:49 -06002294 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07002295 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
2296 sizeof(struct iscsi_stats) +
2297 sizeof(struct iscsi_stats_custom) *
2298 stats->custom_length);
2299 actual_size -= sizeof(*nlhstat);
2300 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06002301 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07002302 nlhstat->nlmsg_len = actual_size;
2303
Michael Chan43514772009-06-08 18:14:41 -07002304 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
2305 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002306 } while (err < 0 && err != -ECONNREFUSED);
2307
2308 return err;
2309}
2310
Mike Christie53cb8a12006-06-28 12:00:32 -05002311/**
Mike Christie26974782007-12-13 12:43:29 -06002312 * iscsi_session_event - send session destr. completion event
2313 * @session: iscsi class session
2314 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05002315 */
Mike Christie26974782007-12-13 12:43:29 -06002316int iscsi_session_event(struct iscsi_cls_session *session,
2317 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05002318{
2319 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05002320 struct Scsi_Host *shost;
2321 struct iscsi_uevent *ev;
2322 struct sk_buff *skb;
2323 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05002324 int rc, len = NLMSG_SPACE(sizeof(*ev));
2325
Mike Christie26974782007-12-13 12:43:29 -06002326 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05002327 if (!priv)
2328 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05002329 shost = iscsi_session_to_shost(session);
2330
Mike Christie43a145a2006-10-16 18:09:38 -04002331 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05002332 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06002333 iscsi_cls_session_printk(KERN_ERR, session,
2334 "Cannot notify userspace of session "
2335 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05002336 return -ENOMEM;
2337 }
2338
Michael Chan43514772009-06-08 18:14:41 -07002339 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Mike Christie53cb8a12006-06-28 12:00:32 -05002340 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06002341 ev->transport_handle = iscsi_handle(session->transport);
2342
2343 ev->type = event;
2344 switch (event) {
2345 case ISCSI_KEVENT_DESTROY_SESSION:
2346 ev->r.d_session.host_no = shost->host_no;
2347 ev->r.d_session.sid = session->sid;
2348 break;
2349 case ISCSI_KEVENT_CREATE_SESSION:
2350 ev->r.c_session_ret.host_no = shost->host_no;
2351 ev->r.c_session_ret.sid = session->sid;
2352 break;
2353 case ISCSI_KEVENT_UNBIND_SESSION:
2354 ev->r.unbind_session.host_no = shost->host_no;
2355 ev->r.unbind_session.sid = session->sid;
2356 break;
2357 default:
Mike Christie322d7392008-01-31 13:36:52 -06002358 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
2359 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06002360 kfree_skb(skb);
2361 return -EINVAL;
2362 }
Mike Christie53cb8a12006-06-28 12:00:32 -05002363
2364 /*
2365 * this will occur if the daemon is not up, so we just warn
2366 * the user and when the daemon is restarted it will handle it
2367 */
Michael Chan43514772009-06-08 18:14:41 -07002368 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08002369 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06002370 iscsi_cls_session_printk(KERN_ERR, session,
2371 "Cannot notify userspace of session "
2372 "event %u. Check iscsi daemon\n",
2373 event);
Mike Christie632248a2009-08-20 15:11:01 -05002374
2375 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
2376 event, rc);
Mike Christie53cb8a12006-06-28 12:00:32 -05002377 return rc;
2378}
Mike Christie26974782007-12-13 12:43:29 -06002379EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05002380
Alex Aizman0896b752005-08-04 19:33:07 -07002381static int
Mike Christied82ff9be2008-05-21 15:54:13 -05002382iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
Mike Christie0c70d842011-12-05 16:44:01 -06002383 struct iscsi_uevent *ev, pid_t pid,
2384 uint32_t initial_cmdsn, uint16_t cmds_max,
2385 uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06002386{
2387 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06002388 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06002389 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06002390
Mike Christied82ff9be2008-05-21 15:54:13 -05002391 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06002392 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06002393 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06002394 return -ENOMEM;
2395
Mike Christie0c70d842011-12-05 16:44:01 -06002396 session->creator = pid;
Mike Christie5e7facb2009-03-05 14:46:06 -06002397 shost = iscsi_session_to_shost(session);
2398 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05002399 ev->r.c_session_ret.sid = session->sid;
Mike Christie632248a2009-08-20 15:11:01 -05002400 ISCSI_DBG_TRANS_SESSION(session,
2401 "Completed creating transport session\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002402 return 0;
2403}
2404
2405static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002406iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06002407{
Mike Christie7b8631b2006-01-13 18:05:50 -06002408 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06002409 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06002410
Mike Christieb5c7a122006-04-06 21:13:33 -05002411 session = iscsi_session_lookup(ev->u.c_conn.sid);
2412 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002413 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05002414 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06002415 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05002416 }
Mike Christie7b8631b2006-01-13 18:05:50 -06002417
Mike Christie7b7232f2006-02-01 21:06:49 -06002418 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05002419 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06002420 iscsi_cls_session_printk(KERN_ERR, session,
2421 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06002422 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05002423 }
Mike Christie7b8631b2006-01-13 18:05:50 -06002424
Mike Christieb5c7a122006-04-06 21:13:33 -05002425 ev->r.c_conn_ret.sid = session->sid;
2426 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie632248a2009-08-20 15:11:01 -05002427
2428 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002429 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06002430}
2431
2432static int
2433iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2434{
Mike Christie7b8631b2006-01-13 18:05:50 -06002435 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06002436
Mike Christieb5c7a122006-04-06 21:13:33 -05002437 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06002438 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06002439 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06002440
Mike Christie632248a2009-08-20 15:11:01 -05002441 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002442 if (transport->destroy_conn)
2443 transport->destroy_conn(conn);
Mike Christie632248a2009-08-20 15:11:01 -05002444
Mike Christie7b8631b2006-01-13 18:05:50 -06002445 return 0;
2446}
2447
Mike Christiefd7255f2006-04-06 21:13:36 -05002448static int
2449iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2450{
2451 char *data = (char*)ev + sizeof(*ev);
2452 struct iscsi_cls_conn *conn;
2453 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05002454 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05002455
2456 session = iscsi_session_lookup(ev->u.set_param.sid);
2457 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
2458 if (!conn || !session)
2459 return -EINVAL;
2460
2461 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05002462 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05002463 sscanf(data, "%d", &value);
Mike Christiefdd46dc2009-11-11 16:34:34 -06002464 session->recovery_tmo = value;
Mike Christie30a6c652006-04-06 21:13:39 -05002465 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002466 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05002467 err = transport->set_param(conn, ev->u.set_param.param,
2468 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05002469 }
2470
2471 return err;
2472}
2473
Mike Christie10eb0f02009-05-13 17:57:38 -05002474static int iscsi_if_ep_connect(struct iscsi_transport *transport,
2475 struct iscsi_uevent *ev, int msg_type)
2476{
2477 struct iscsi_endpoint *ep;
2478 struct sockaddr *dst_addr;
2479 struct Scsi_Host *shost = NULL;
2480 int non_blocking, err = 0;
2481
2482 if (!transport->ep_connect)
2483 return -EINVAL;
2484
2485 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
2486 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
2487 if (!shost) {
2488 printk(KERN_ERR "ep connect failed. Could not find "
2489 "host no %u\n",
2490 ev->u.ep_connect_through_host.host_no);
2491 return -ENODEV;
2492 }
2493 non_blocking = ev->u.ep_connect_through_host.non_blocking;
2494 } else
2495 non_blocking = ev->u.ep_connect.non_blocking;
2496
2497 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
2498 ep = transport->ep_connect(shost, dst_addr, non_blocking);
2499 if (IS_ERR(ep)) {
2500 err = PTR_ERR(ep);
2501 goto release_host;
2502 }
2503
2504 ev->r.ep_connect_ret.handle = ep->id;
2505release_host:
2506 if (shost)
2507 scsi_host_put(shost);
2508 return err;
2509}
2510
Mike Christie22a39fb2011-02-16 15:04:33 -06002511static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
2512 u64 ep_handle)
2513{
2514 struct iscsi_cls_conn *conn;
2515 struct iscsi_endpoint *ep;
2516
2517 if (!transport->ep_disconnect)
2518 return -EINVAL;
2519
2520 ep = iscsi_lookup_endpoint(ep_handle);
2521 if (!ep)
2522 return -EINVAL;
2523 conn = ep->conn;
2524 if (conn) {
2525 mutex_lock(&conn->ep_mutex);
2526 conn->ep = NULL;
2527 mutex_unlock(&conn->ep_mutex);
2528 }
2529
2530 transport->ep_disconnect(ep);
2531 return 0;
2532}
2533
Mike Christie7b8631b2006-01-13 18:05:50 -06002534static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05002535iscsi_if_transport_ep(struct iscsi_transport *transport,
2536 struct iscsi_uevent *ev, int msg_type)
2537{
Mike Christied82ff9be2008-05-21 15:54:13 -05002538 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05002539 int rc = 0;
2540
2541 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05002542 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05002543 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05002544 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05002545 break;
2546 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
2547 if (!transport->ep_poll)
2548 return -EINVAL;
2549
Mike Christied82ff9be2008-05-21 15:54:13 -05002550 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
2551 if (!ep)
2552 return -EINVAL;
2553
2554 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05002555 ev->u.ep_poll.timeout_ms);
2556 break;
2557 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie22a39fb2011-02-16 15:04:33 -06002558 rc = iscsi_if_ep_disconnect(transport,
2559 ev->u.ep_disconnect.ep_handle);
Or Gerlitz264faaa2006-05-02 19:46:36 -05002560 break;
2561 }
2562 return rc;
2563}
2564
2565static int
Mike Christie01cb2252006-06-28 12:00:22 -05002566iscsi_tgt_dscvr(struct iscsi_transport *transport,
2567 struct iscsi_uevent *ev)
2568{
Mike Christie2174a042007-05-30 12:57:10 -05002569 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05002570 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05002571 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05002572
2573 if (!transport->tgt_dscvr)
2574 return -EINVAL;
2575
Mike Christie2174a042007-05-30 12:57:10 -05002576 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04002577 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05002578 printk(KERN_ERR "target discovery could not find host no %u\n",
2579 ev->u.tgt_dscvr.host_no);
2580 return -ENODEV;
2581 }
2582
2583
Mike Christie01cb2252006-06-28 12:00:22 -05002584 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05002585 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
2586 ev->u.tgt_dscvr.enable, dst_addr);
2587 scsi_host_put(shost);
2588 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05002589}
2590
2591static int
Mike Christie1d9bf132007-05-30 12:57:11 -05002592iscsi_set_host_param(struct iscsi_transport *transport,
2593 struct iscsi_uevent *ev)
2594{
2595 char *data = (char*)ev + sizeof(*ev);
2596 struct Scsi_Host *shost;
2597 int err;
2598
2599 if (!transport->set_host_param)
2600 return -ENOSYS;
2601
2602 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04002603 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05002604 printk(KERN_ERR "set_host_param could not find host no %u\n",
2605 ev->u.set_host_param.host_no);
2606 return -ENODEV;
2607 }
2608
2609 err = transport->set_host_param(shost, ev->u.set_host_param.param,
2610 data, ev->u.set_host_param.len);
2611 scsi_host_put(shost);
2612 return err;
2613}
2614
2615static int
Michael Chan43514772009-06-08 18:14:41 -07002616iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2617{
2618 struct Scsi_Host *shost;
2619 struct iscsi_path *params;
2620 int err;
2621
2622 if (!transport->set_path)
2623 return -ENOSYS;
2624
2625 shost = scsi_host_lookup(ev->u.set_path.host_no);
2626 if (!shost) {
2627 printk(KERN_ERR "set path could not find host no %u\n",
2628 ev->u.set_path.host_no);
2629 return -ENODEV;
2630 }
2631
2632 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
2633 err = transport->set_path(shost, params);
2634
2635 scsi_host_put(shost);
2636 return err;
2637}
2638
2639static int
Mike Christie56c155b2011-07-25 13:48:37 -05002640iscsi_set_iface_params(struct iscsi_transport *transport,
Mike Christie00c31882011-10-06 03:56:59 -05002641 struct iscsi_uevent *ev, uint32_t len)
Mike Christie56c155b2011-07-25 13:48:37 -05002642{
2643 char *data = (char *)ev + sizeof(*ev);
2644 struct Scsi_Host *shost;
2645 int err;
2646
2647 if (!transport->set_iface_param)
2648 return -ENOSYS;
2649
2650 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
2651 if (!shost) {
2652 printk(KERN_ERR "set_iface_params could not find host no %u\n",
2653 ev->u.set_iface_params.host_no);
2654 return -ENODEV;
2655 }
2656
Mike Christie00c31882011-10-06 03:56:59 -05002657 err = transport->set_iface_param(shost, data, len);
Mike Christie56c155b2011-07-25 13:48:37 -05002658 scsi_host_put(shost);
2659 return err;
2660}
2661
2662static int
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302663iscsi_send_ping(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2664{
2665 struct Scsi_Host *shost;
2666 struct sockaddr *dst_addr;
2667 int err;
2668
2669 if (!transport->send_ping)
2670 return -ENOSYS;
2671
2672 shost = scsi_host_lookup(ev->u.iscsi_ping.host_no);
2673 if (!shost) {
2674 printk(KERN_ERR "iscsi_ping could not find host no %u\n",
2675 ev->u.iscsi_ping.host_no);
2676 return -ENODEV;
2677 }
2678
2679 dst_addr = (struct sockaddr *)((char *)ev + sizeof(*ev));
2680 err = transport->send_ping(shost, ev->u.iscsi_ping.iface_num,
2681 ev->u.iscsi_ping.iface_type,
2682 ev->u.iscsi_ping.payload_size,
2683 ev->u.iscsi_ping.pid,
2684 dst_addr);
2685 scsi_host_put(shost);
2686 return err;
2687}
2688
2689static int
Nilesh Javali6260a5d2012-02-27 03:08:51 -08002690iscsi_get_chap(struct iscsi_transport *transport, struct nlmsghdr *nlh)
2691{
2692 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
2693 struct Scsi_Host *shost = NULL;
2694 struct iscsi_chap_rec *chap_rec;
2695 struct iscsi_internal *priv;
2696 struct sk_buff *skbchap;
2697 struct nlmsghdr *nlhchap;
2698 struct iscsi_uevent *evchap;
2699 uint32_t chap_buf_size;
2700 int len, err = 0;
2701 char *buf;
2702
2703 if (!transport->get_chap)
2704 return -EINVAL;
2705
2706 priv = iscsi_if_transport_lookup(transport);
2707 if (!priv)
2708 return -EINVAL;
2709
2710 chap_buf_size = (ev->u.get_chap.num_entries * sizeof(*chap_rec));
2711 len = NLMSG_SPACE(sizeof(*ev) + chap_buf_size);
2712
2713 shost = scsi_host_lookup(ev->u.get_chap.host_no);
2714 if (!shost) {
2715 printk(KERN_ERR "%s: failed. Cound not find host no %u\n",
2716 __func__, ev->u.get_chap.host_no);
2717 return -ENODEV;
2718 }
2719
2720 do {
2721 int actual_size;
2722
2723 skbchap = alloc_skb(len, GFP_KERNEL);
2724 if (!skbchap) {
2725 printk(KERN_ERR "can not deliver chap: OOM\n");
2726 err = -ENOMEM;
2727 goto exit_get_chap;
2728 }
2729
2730 nlhchap = __nlmsg_put(skbchap, 0, 0, 0,
2731 (len - sizeof(*nlhchap)), 0);
2732 evchap = NLMSG_DATA(nlhchap);
2733 memset(evchap, 0, sizeof(*evchap));
2734 evchap->transport_handle = iscsi_handle(transport);
2735 evchap->type = nlh->nlmsg_type;
2736 evchap->u.get_chap.host_no = ev->u.get_chap.host_no;
2737 evchap->u.get_chap.chap_tbl_idx = ev->u.get_chap.chap_tbl_idx;
2738 evchap->u.get_chap.num_entries = ev->u.get_chap.num_entries;
2739 buf = (char *) ((char *)evchap + sizeof(*evchap));
2740 memset(buf, 0, chap_buf_size);
2741
2742 err = transport->get_chap(shost, ev->u.get_chap.chap_tbl_idx,
2743 &evchap->u.get_chap.num_entries, buf);
2744
2745 actual_size = NLMSG_SPACE(sizeof(*ev) + chap_buf_size);
2746 skb_trim(skbchap, NLMSG_ALIGN(actual_size));
2747 nlhchap->nlmsg_len = actual_size;
2748
2749 err = iscsi_multicast_skb(skbchap, ISCSI_NL_GRP_ISCSID,
2750 GFP_KERNEL);
2751 } while (err < 0 && err != -ECONNREFUSED);
2752
2753exit_get_chap:
2754 scsi_host_put(shost);
2755 return err;
2756}
2757
2758static int iscsi_delete_chap(struct iscsi_transport *transport,
2759 struct iscsi_uevent *ev)
2760{
2761 struct Scsi_Host *shost;
2762 int err = 0;
2763
2764 if (!transport->delete_chap)
2765 return -ENOSYS;
2766
2767 shost = scsi_host_lookup(ev->u.delete_chap.host_no);
2768 if (!shost) {
2769 printk(KERN_ERR "%s could not find host no %u\n",
2770 __func__, ev->u.delete_chap.host_no);
2771 return -ENODEV;
2772 }
2773
2774 err = transport->delete_chap(shost, ev->u.delete_chap.chap_tbl_idx);
2775 scsi_host_put(shost);
2776 return err;
2777}
2778
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002779static const struct {
2780 enum iscsi_discovery_parent_type value;
2781 char *name;
2782} iscsi_discovery_parent_names[] = {
2783 {ISCSI_DISC_PARENT_UNKNOWN, "Unknown" },
2784 {ISCSI_DISC_PARENT_SENDTGT, "Sendtarget" },
2785 {ISCSI_DISC_PARENT_ISNS, "isns" },
2786};
2787
2788char *iscsi_get_discovery_parent_name(int parent_type)
2789{
2790 int i;
2791 char *state = "Unknown!";
2792
2793 for (i = 0; i < ARRAY_SIZE(iscsi_discovery_parent_names); i++) {
2794 if (iscsi_discovery_parent_names[i].value & parent_type) {
2795 state = iscsi_discovery_parent_names[i].name;
2796 break;
2797 }
2798 }
2799 return state;
2800}
2801EXPORT_SYMBOL_GPL(iscsi_get_discovery_parent_name);
2802
2803static int iscsi_set_flashnode_param(struct iscsi_transport *transport,
2804 struct iscsi_uevent *ev, uint32_t len)
2805{
2806 char *data = (char *)ev + sizeof(*ev);
2807 struct Scsi_Host *shost;
2808 struct iscsi_bus_flash_session *fnode_sess;
2809 struct iscsi_bus_flash_conn *fnode_conn;
2810 struct device *dev;
2811 uint32_t *idx;
2812 int err = 0;
2813
2814 if (!transport->set_flashnode_param) {
2815 err = -ENOSYS;
2816 goto exit_set_fnode;
2817 }
2818
2819 shost = scsi_host_lookup(ev->u.set_flashnode.host_no);
2820 if (!shost) {
2821 pr_err("%s could not find host no %u\n",
2822 __func__, ev->u.set_flashnode.host_no);
2823 err = -ENODEV;
2824 goto put_host;
2825 }
2826
2827 idx = &ev->u.set_flashnode.flashnode_idx;
2828 fnode_sess = iscsi_get_flashnode_by_index(shost, idx,
2829 flashnode_match_index);
2830 if (!fnode_sess) {
2831 pr_err("%s could not find flashnode %u for host no %u\n",
2832 __func__, *idx, ev->u.set_flashnode.host_no);
2833 err = -ENODEV;
2834 goto put_host;
2835 }
2836
2837 dev = iscsi_find_flashnode_conn(fnode_sess, NULL,
2838 iscsi_is_flashnode_conn_dev);
2839 if (!dev) {
2840 err = -ENODEV;
2841 goto put_host;
2842 }
2843
2844 fnode_conn = iscsi_dev_to_flash_conn(dev);
2845 err = transport->set_flashnode_param(fnode_sess, fnode_conn, data, len);
2846
2847put_host:
2848 scsi_host_put(shost);
2849
2850exit_set_fnode:
2851 return err;
2852}
2853
2854static int iscsi_new_flashnode(struct iscsi_transport *transport,
2855 struct iscsi_uevent *ev, uint32_t len)
2856{
2857 char *data = (char *)ev + sizeof(*ev);
2858 struct Scsi_Host *shost;
2859 int index;
2860 int err = 0;
2861
2862 if (!transport->new_flashnode) {
2863 err = -ENOSYS;
2864 goto exit_new_fnode;
2865 }
2866
2867 shost = scsi_host_lookup(ev->u.new_flashnode.host_no);
2868 if (!shost) {
2869 pr_err("%s could not find host no %u\n",
2870 __func__, ev->u.new_flashnode.host_no);
2871 err = -ENODEV;
2872 goto put_host;
2873 }
2874
2875 index = transport->new_flashnode(shost, data, len);
2876
2877 if (index >= 0)
2878 ev->r.new_flashnode_ret.flashnode_idx = index;
2879 else
2880 err = -EIO;
2881
2882put_host:
2883 scsi_host_put(shost);
2884
2885exit_new_fnode:
2886 return err;
2887}
2888
2889static int iscsi_del_flashnode(struct iscsi_transport *transport,
2890 struct iscsi_uevent *ev)
2891{
2892 struct Scsi_Host *shost;
2893 struct iscsi_bus_flash_session *fnode_sess;
2894 uint32_t *idx;
2895 int err = 0;
2896
2897 if (!transport->del_flashnode) {
2898 err = -ENOSYS;
2899 goto exit_del_fnode;
2900 }
2901
2902 shost = scsi_host_lookup(ev->u.del_flashnode.host_no);
2903 if (!shost) {
2904 pr_err("%s could not find host no %u\n",
2905 __func__, ev->u.del_flashnode.host_no);
2906 err = -ENODEV;
2907 goto put_host;
2908 }
2909
2910 idx = &ev->u.del_flashnode.flashnode_idx;
2911 fnode_sess = iscsi_get_flashnode_by_index(shost, idx,
2912 flashnode_match_index);
2913 if (!fnode_sess) {
2914 pr_err("%s could not find flashnode %u for host no %u\n",
2915 __func__, *idx, ev->u.del_flashnode.host_no);
2916 err = -ENODEV;
2917 goto put_host;
2918 }
2919
2920 err = transport->del_flashnode(fnode_sess);
2921
2922put_host:
2923 scsi_host_put(shost);
2924
2925exit_del_fnode:
2926 return err;
2927}
2928
2929static int iscsi_login_flashnode(struct iscsi_transport *transport,
2930 struct iscsi_uevent *ev)
2931{
2932 struct Scsi_Host *shost;
2933 struct iscsi_bus_flash_session *fnode_sess;
2934 struct iscsi_bus_flash_conn *fnode_conn;
2935 struct device *dev;
2936 uint32_t *idx;
2937 int err = 0;
2938
2939 if (!transport->login_flashnode) {
2940 err = -ENOSYS;
2941 goto exit_login_fnode;
2942 }
2943
2944 shost = scsi_host_lookup(ev->u.login_flashnode.host_no);
2945 if (!shost) {
2946 pr_err("%s could not find host no %u\n",
2947 __func__, ev->u.login_flashnode.host_no);
2948 err = -ENODEV;
2949 goto put_host;
2950 }
2951
2952 idx = &ev->u.login_flashnode.flashnode_idx;
2953 fnode_sess = iscsi_get_flashnode_by_index(shost, idx,
2954 flashnode_match_index);
2955 if (!fnode_sess) {
2956 pr_err("%s could not find flashnode %u for host no %u\n",
2957 __func__, *idx, ev->u.login_flashnode.host_no);
2958 err = -ENODEV;
2959 goto put_host;
2960 }
2961
2962 dev = iscsi_find_flashnode_conn(fnode_sess, NULL,
2963 iscsi_is_flashnode_conn_dev);
2964 if (!dev) {
2965 err = -ENODEV;
2966 goto put_host;
2967 }
2968
2969 fnode_conn = iscsi_dev_to_flash_conn(dev);
2970 err = transport->login_flashnode(fnode_sess, fnode_conn);
2971
2972put_host:
2973 scsi_host_put(shost);
2974
2975exit_login_fnode:
2976 return err;
2977}
2978
2979static int iscsi_logout_flashnode(struct iscsi_transport *transport,
2980 struct iscsi_uevent *ev)
2981{
2982 struct Scsi_Host *shost;
2983 struct iscsi_bus_flash_session *fnode_sess;
2984 struct iscsi_bus_flash_conn *fnode_conn;
2985 struct device *dev;
2986 uint32_t *idx;
2987 int err = 0;
2988
2989 if (!transport->logout_flashnode) {
2990 err = -ENOSYS;
2991 goto exit_logout_fnode;
2992 }
2993
2994 shost = scsi_host_lookup(ev->u.logout_flashnode.host_no);
2995 if (!shost) {
2996 pr_err("%s could not find host no %u\n",
2997 __func__, ev->u.logout_flashnode.host_no);
2998 err = -ENODEV;
2999 goto put_host;
3000 }
3001
3002 idx = &ev->u.logout_flashnode.flashnode_idx;
3003 fnode_sess = iscsi_get_flashnode_by_index(shost, idx,
3004 flashnode_match_index);
3005 if (!fnode_sess) {
3006 pr_err("%s could not find flashnode %u for host no %u\n",
3007 __func__, *idx, ev->u.logout_flashnode.host_no);
3008 err = -ENODEV;
3009 goto put_host;
3010 }
3011
3012 dev = iscsi_find_flashnode_conn(fnode_sess, NULL,
3013 iscsi_is_flashnode_conn_dev);
3014 if (!dev) {
3015 err = -ENODEV;
3016 goto put_host;
3017 }
3018
3019 fnode_conn = iscsi_dev_to_flash_conn(dev);
3020
3021 err = transport->logout_flashnode(fnode_sess, fnode_conn);
3022
3023put_host:
3024 scsi_host_put(shost);
3025
3026exit_logout_fnode:
3027 return err;
3028}
3029
3030static int iscsi_logout_flashnode_sid(struct iscsi_transport *transport,
3031 struct iscsi_uevent *ev)
3032{
3033 struct Scsi_Host *shost;
3034 struct iscsi_cls_session *session;
3035 int err = 0;
3036
3037 if (!transport->logout_flashnode_sid) {
3038 err = -ENOSYS;
3039 goto exit_logout_sid;
3040 }
3041
3042 shost = scsi_host_lookup(ev->u.logout_flashnode_sid.host_no);
3043 if (!shost) {
3044 pr_err("%s could not find host no %u\n",
3045 __func__, ev->u.logout_flashnode.host_no);
3046 err = -ENODEV;
3047 goto put_host;
3048 }
3049
3050 session = iscsi_session_lookup(ev->u.logout_flashnode_sid.sid);
3051 if (!session) {
3052 pr_err("%s could not find session id %u\n",
3053 __func__, ev->u.logout_flashnode_sid.sid);
3054 err = -EINVAL;
3055 goto put_host;
3056 }
3057
3058 err = transport->logout_flashnode_sid(session);
3059
3060put_host:
3061 scsi_host_put(shost);
3062
3063exit_logout_sid:
3064 return err;
3065}
3066
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003067static int
Michael Chan43514772009-06-08 18:14:41 -07003068iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07003069{
3070 int err = 0;
3071 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
3072 struct iscsi_transport *transport = NULL;
3073 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06003074 struct iscsi_cls_session *session;
3075 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05003076 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07003077
Michael Chan43514772009-06-08 18:14:41 -07003078 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
3079 *group = ISCSI_NL_GRP_UIP;
3080 else
3081 *group = ISCSI_NL_GRP_ISCSID;
3082
Alex Aizman0896b752005-08-04 19:33:07 -07003083 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
3084 if (!priv)
3085 return -EINVAL;
3086 transport = priv->iscsi_transport;
3087
Mike Christie7b7232f2006-02-01 21:06:49 -06003088 if (!try_module_get(transport->owner))
3089 return -EINVAL;
3090
Alex Aizman0896b752005-08-04 19:33:07 -07003091 switch (nlh->nlmsg_type) {
3092 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05003093 err = iscsi_if_create_session(priv, ep, ev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003094 NETLINK_CB(skb).portid,
Mike Christie40753ca2008-05-21 15:53:56 -05003095 ev->u.c_session.initial_cmdsn,
3096 ev->u.c_session.cmds_max,
3097 ev->u.c_session.queue_depth);
3098 break;
3099 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05003100 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05003101 if (!ep) {
3102 err = -EINVAL;
3103 break;
3104 }
Mike Christied82ff9be2008-05-21 15:54:13 -05003105
3106 err = iscsi_if_create_session(priv, ep, ev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003107 NETLINK_CB(skb).portid,
Mike Christie40753ca2008-05-21 15:53:56 -05003108 ev->u.c_bound_session.initial_cmdsn,
3109 ev->u.c_bound_session.cmds_max,
3110 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07003111 break;
3112 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05003113 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06003114 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06003115 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06003116 else
3117 err = -EINVAL;
3118 break;
3119 case ISCSI_UEVENT_UNBIND_SESSION:
3120 session = iscsi_session_lookup(ev->u.d_session.sid);
3121 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06003122 scsi_queue_work(iscsi_session_to_shost(session),
3123 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06003124 else
Mike Christie7b7232f2006-02-01 21:06:49 -06003125 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07003126 break;
3127 case ISCSI_UEVENT_CREATE_CONN:
3128 err = iscsi_if_create_conn(transport, ev);
3129 break;
3130 case ISCSI_UEVENT_DESTROY_CONN:
3131 err = iscsi_if_destroy_conn(transport, ev);
3132 break;
3133 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05003134 session = iscsi_session_lookup(ev->u.b_conn.sid);
3135 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06003136
Mike Christie22a39fb2011-02-16 15:04:33 -06003137 if (conn && conn->ep)
3138 iscsi_if_ep_disconnect(transport, conn->ep->id);
3139
3140 if (!session || !conn) {
Mike Christie7b7232f2006-02-01 21:06:49 -06003141 err = -EINVAL;
Mike Christie22a39fb2011-02-16 15:04:33 -06003142 break;
3143 }
3144
3145 ev->r.retcode = transport->bind_conn(session, conn,
3146 ev->u.b_conn.transport_eph,
3147 ev->u.b_conn.is_leading);
3148 if (ev->r.retcode || !transport->ep_connect)
3149 break;
3150
3151 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
3152 if (ep) {
3153 ep->conn = conn;
3154
3155 mutex_lock(&conn->ep_mutex);
3156 conn->ep = ep;
3157 mutex_unlock(&conn->ep_mutex);
3158 } else
3159 iscsi_cls_conn_printk(KERN_ERR, conn,
3160 "Could not set ep conn "
3161 "binding\n");
Alex Aizman0896b752005-08-04 19:33:07 -07003162 break;
3163 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05003164 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07003165 break;
3166 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05003167 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06003168 if (conn)
3169 ev->r.retcode = transport->start_conn(conn);
3170 else
3171 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07003172 break;
3173 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05003174 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06003175 if (conn)
3176 transport->stop_conn(conn, ev->u.stop_conn.flag);
3177 else
3178 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07003179 break;
3180 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05003181 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06003182 if (conn)
3183 ev->r.retcode = transport->send_pdu(conn,
3184 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
3185 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
3186 ev->u.send_pdu.data_size);
3187 else
3188 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07003189 break;
3190 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06003191 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07003192 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05003193 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
3194 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
3195 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05003196 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05003197 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
3198 break;
Mike Christie01cb2252006-06-28 12:00:22 -05003199 case ISCSI_UEVENT_TGT_DSCVR:
3200 err = iscsi_tgt_dscvr(transport, ev);
3201 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05003202 case ISCSI_UEVENT_SET_HOST_PARAM:
3203 err = iscsi_set_host_param(transport, ev);
3204 break;
Michael Chan43514772009-06-08 18:14:41 -07003205 case ISCSI_UEVENT_PATH_UPDATE:
3206 err = iscsi_set_path(transport, ev);
3207 break;
Mike Christie56c155b2011-07-25 13:48:37 -05003208 case ISCSI_UEVENT_SET_IFACE_PARAMS:
Mike Christie00c31882011-10-06 03:56:59 -05003209 err = iscsi_set_iface_params(transport, ev,
3210 nlmsg_attrlen(nlh, sizeof(*ev)));
Mike Christie56c155b2011-07-25 13:48:37 -05003211 break;
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05303212 case ISCSI_UEVENT_PING:
3213 err = iscsi_send_ping(transport, ev);
3214 break;
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003215 case ISCSI_UEVENT_GET_CHAP:
3216 err = iscsi_get_chap(transport, nlh);
3217 break;
3218 case ISCSI_UEVENT_DELETE_CHAP:
3219 err = iscsi_delete_chap(transport, ev);
3220 break;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003221 case ISCSI_UEVENT_SET_FLASHNODE_PARAMS:
3222 err = iscsi_set_flashnode_param(transport, ev,
3223 nlmsg_attrlen(nlh,
3224 sizeof(*ev)));
3225 break;
3226 case ISCSI_UEVENT_NEW_FLASHNODE:
3227 err = iscsi_new_flashnode(transport, ev,
3228 nlmsg_attrlen(nlh, sizeof(*ev)));
3229 break;
3230 case ISCSI_UEVENT_DEL_FLASHNODE:
3231 err = iscsi_del_flashnode(transport, ev);
3232 break;
3233 case ISCSI_UEVENT_LOGIN_FLASHNODE:
3234 err = iscsi_login_flashnode(transport, ev);
3235 break;
3236 case ISCSI_UEVENT_LOGOUT_FLASHNODE:
3237 err = iscsi_logout_flashnode(transport, ev);
3238 break;
3239 case ISCSI_UEVENT_LOGOUT_FLASHNODE_SID:
3240 err = iscsi_logout_flashnode_sid(transport, ev);
3241 break;
Alex Aizman0896b752005-08-04 19:33:07 -07003242 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05003243 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07003244 break;
3245 }
3246
Mike Christie7b7232f2006-02-01 21:06:49 -06003247 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07003248 return err;
3249}
3250
Mike Christieb5c7a122006-04-06 21:13:33 -05003251/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003252 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
3253 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05003254 */
Alex Aizman0896b752005-08-04 19:33:07 -07003255static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003256iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07003257{
Arjan van de Ven0b950672006-01-11 13:16:10 +01003258 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003259 while (skb->len >= NLMSG_SPACE(0)) {
3260 int err;
3261 uint32_t rlen;
3262 struct nlmsghdr *nlh;
3263 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07003264 uint32_t group;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003265
3266 nlh = nlmsg_hdr(skb);
3267 if (nlh->nlmsg_len < sizeof(*nlh) ||
3268 skb->len < nlh->nlmsg_len) {
3269 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06003270 }
Mike Christieee7f8e42006-02-01 21:07:01 -06003271
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003272 ev = NLMSG_DATA(nlh);
3273 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
3274 if (rlen > skb->len)
3275 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07003276
Michael Chan43514772009-06-08 18:14:41 -07003277 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003278 if (err) {
3279 ev->type = ISCSI_KEVENT_IF_ERROR;
3280 ev->iferror = err;
3281 }
3282 do {
3283 /*
3284 * special case for GET_STATS:
3285 * on success - sending reply and stats from
3286 * inside of if_recv_msg(),
3287 * on error - fall through.
3288 */
3289 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07003290 break;
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003291 if (ev->type == ISCSI_UEVENT_GET_CHAP && !err)
3292 break;
Michael Chan43514772009-06-08 18:14:41 -07003293 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003294 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
Sasha Levin46a7c172012-01-25 22:16:16 -05003295 } while (err < 0 && err != -ECONNREFUSED && err != -ESRCH);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003296 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07003297 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01003298 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07003299}
3300
Mike Christiefd7255f2006-04-06 21:13:36 -05003301#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01003302struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05003303 __ATTR(_name,_mode,_show,_store)
3304
Alex Aizman0896b752005-08-04 19:33:07 -07003305/*
3306 * iSCSI connection attrs
3307 */
Mike Christiea54a52c2006-06-28 12:00:23 -05003308#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01003310show_conn_param_##param(struct device *dev, \
3311 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05003312{ \
Tony Jonesee959b02008-02-22 00:13:36 +01003313 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05003314 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05003315 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05003316}
3317
Mike Christiea54a52c2006-06-28 12:00:23 -05003318#define iscsi_conn_attr(field, param) \
3319 iscsi_conn_attr_show(param) \
3320static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05003321 NULL);
3322
Mike Christiea54a52c2006-06-28 12:00:23 -05003323iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
3324iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
3325iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
3326iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
3327iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
3328iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
3329iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
Mike Christiea54a52c2006-06-28 12:00:23 -05003330iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
3331iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06003332iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
3333iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Mike Christie289324b2011-02-16 15:04:37 -06003335#define iscsi_conn_ep_attr_show(param) \
3336static ssize_t show_conn_ep_param_##param(struct device *dev, \
3337 struct device_attribute *attr,\
3338 char *buf) \
3339{ \
3340 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
3341 struct iscsi_transport *t = conn->transport; \
3342 struct iscsi_endpoint *ep; \
3343 ssize_t rc; \
3344 \
3345 /* \
3346 * Need to make sure ep_disconnect does not free the LLD's \
3347 * interconnect resources while we are trying to read them. \
3348 */ \
3349 mutex_lock(&conn->ep_mutex); \
3350 ep = conn->ep; \
3351 if (!ep && t->ep_connect) { \
3352 mutex_unlock(&conn->ep_mutex); \
3353 return -ENOTCONN; \
3354 } \
3355 \
3356 if (ep) \
3357 rc = t->get_ep_param(ep, param, buf); \
3358 else \
3359 rc = t->get_conn_param(conn, param, buf); \
3360 mutex_unlock(&conn->ep_mutex); \
3361 return rc; \
3362}
3363
3364#define iscsi_conn_ep_attr(field, param) \
3365 iscsi_conn_ep_attr_show(param) \
3366static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
3367 show_conn_ep_param_##param, NULL);
3368
3369iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
3370iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
3371
Mike Christie3128c6c2011-07-25 13:48:42 -05003372static struct attribute *iscsi_conn_attrs[] = {
3373 &dev_attr_conn_max_recv_dlength.attr,
3374 &dev_attr_conn_max_xmit_dlength.attr,
3375 &dev_attr_conn_header_digest.attr,
3376 &dev_attr_conn_data_digest.attr,
3377 &dev_attr_conn_ifmarker.attr,
3378 &dev_attr_conn_ofmarker.attr,
3379 &dev_attr_conn_address.attr,
3380 &dev_attr_conn_port.attr,
3381 &dev_attr_conn_exp_statsn.attr,
3382 &dev_attr_conn_persistent_address.attr,
3383 &dev_attr_conn_persistent_port.attr,
3384 &dev_attr_conn_ping_tmo.attr,
3385 &dev_attr_conn_recv_tmo.attr,
3386 NULL,
3387};
3388
Al Viro587a1f12011-07-23 23:11:19 -04003389static umode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
Mike Christie3128c6c2011-07-25 13:48:42 -05003390 struct attribute *attr, int i)
3391{
3392 struct device *cdev = container_of(kobj, struct device, kobj);
3393 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
3394 struct iscsi_transport *t = conn->transport;
3395 int param;
3396
3397 if (attr == &dev_attr_conn_max_recv_dlength.attr)
3398 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
3399 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
3400 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
3401 else if (attr == &dev_attr_conn_header_digest.attr)
3402 param = ISCSI_PARAM_HDRDGST_EN;
3403 else if (attr == &dev_attr_conn_data_digest.attr)
3404 param = ISCSI_PARAM_DATADGST_EN;
3405 else if (attr == &dev_attr_conn_ifmarker.attr)
3406 param = ISCSI_PARAM_IFMARKER_EN;
3407 else if (attr == &dev_attr_conn_ofmarker.attr)
3408 param = ISCSI_PARAM_OFMARKER_EN;
3409 else if (attr == &dev_attr_conn_address.attr)
3410 param = ISCSI_PARAM_CONN_ADDRESS;
3411 else if (attr == &dev_attr_conn_port.attr)
3412 param = ISCSI_PARAM_CONN_PORT;
3413 else if (attr == &dev_attr_conn_exp_statsn.attr)
3414 param = ISCSI_PARAM_EXP_STATSN;
3415 else if (attr == &dev_attr_conn_persistent_address.attr)
3416 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
3417 else if (attr == &dev_attr_conn_persistent_port.attr)
3418 param = ISCSI_PARAM_PERSISTENT_PORT;
3419 else if (attr == &dev_attr_conn_ping_tmo.attr)
3420 param = ISCSI_PARAM_PING_TMO;
3421 else if (attr == &dev_attr_conn_recv_tmo.attr)
3422 param = ISCSI_PARAM_RECV_TMO;
3423 else {
3424 WARN_ONCE(1, "Invalid conn attr");
3425 return 0;
3426 }
3427
3428 return t->attr_is_visible(ISCSI_PARAM, param);
3429}
3430
3431static struct attribute_group iscsi_conn_group = {
3432 .attrs = iscsi_conn_attrs,
3433 .is_visible = iscsi_conn_attr_is_visible,
3434};
3435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436/*
Alex Aizman0896b752005-08-04 19:33:07 -07003437 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 */
Mike Christieb2c64162007-05-30 12:57:16 -05003439#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01003441show_session_param_##param(struct device *dev, \
3442 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05003443{ \
Tony Jonesee959b02008-02-22 00:13:36 +01003444 struct iscsi_cls_session *session = \
3445 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05003446 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05003447 \
3448 if (perm && !capable(CAP_SYS_ADMIN)) \
3449 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05003450 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05003451}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Mike Christieb2c64162007-05-30 12:57:16 -05003453#define iscsi_session_attr(field, param, perm) \
3454 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05003455static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05003456 NULL);
Mike Christieb2c64162007-05-30 12:57:16 -05003457iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
3458iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
3459iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
3460iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
3461iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
3462iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
3463iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
3464iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
3465iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
3466iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
3467iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
3468iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
3469iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
3470iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie30534952012-02-27 03:08:53 -08003471iscsi_session_attr(chap_out_idx, ISCSI_PARAM_CHAP_OUT_IDX, 1);
3472iscsi_session_attr(chap_in_idx, ISCSI_PARAM_CHAP_IN_IDX, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06003473iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
3474iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
3475iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie3fe5ae82009-11-11 16:34:33 -06003476iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05003477iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05303478iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
3479iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05003480
Mike Christie6eabafb2008-01-31 13:36:43 -06003481static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01003482show_priv_session_state(struct device *dev, struct device_attribute *attr,
3483 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06003484{
Tony Jonesee959b02008-02-22 00:13:36 +01003485 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06003486 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
3487}
3488static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
3489 NULL);
Mike Christie0c70d842011-12-05 16:44:01 -06003490static ssize_t
3491show_priv_session_creator(struct device *dev, struct device_attribute *attr,
3492 char *buf)
3493{
3494 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
3495 return sprintf(buf, "%d\n", session->creator);
3496}
3497static ISCSI_CLASS_ATTR(priv_sess, creator, S_IRUGO, show_priv_session_creator,
3498 NULL);
Manish Rangankar930a9f02012-11-23 06:58:41 -05003499static ssize_t
3500show_priv_session_target_id(struct device *dev, struct device_attribute *attr,
3501 char *buf)
3502{
3503 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
3504 return sprintf(buf, "%d\n", session->target_id);
3505}
3506static ISCSI_CLASS_ATTR(priv_sess, target_id, S_IRUGO,
3507 show_priv_session_target_id, NULL);
Mike Christie6eabafb2008-01-31 13:36:43 -06003508
Mike Christiefd7255f2006-04-06 21:13:36 -05003509#define iscsi_priv_session_attr_show(field, format) \
3510static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01003511show_priv_session_##field(struct device *dev, \
3512 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05003513{ \
Tony Jonesee959b02008-02-22 00:13:36 +01003514 struct iscsi_cls_session *session = \
3515 iscsi_dev_to_session(dev->parent); \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05303516 if (session->field == -1) \
3517 return sprintf(buf, "off\n"); \
Mike Christiefd7255f2006-04-06 21:13:36 -05003518 return sprintf(buf, format"\n", session->field); \
3519}
3520
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05303521#define iscsi_priv_session_attr_store(field) \
3522static ssize_t \
3523store_priv_session_##field(struct device *dev, \
3524 struct device_attribute *attr, \
3525 const char *buf, size_t count) \
3526{ \
3527 int val; \
3528 char *cp; \
3529 struct iscsi_cls_session *session = \
3530 iscsi_dev_to_session(dev->parent); \
3531 if ((session->state == ISCSI_SESSION_FREE) || \
3532 (session->state == ISCSI_SESSION_FAILED)) \
3533 return -EBUSY; \
3534 if (strncmp(buf, "off", 3) == 0) \
3535 session->field = -1; \
3536 else { \
3537 val = simple_strtoul(buf, &cp, 0); \
3538 if (*cp != '\0' && *cp != '\n') \
3539 return -EINVAL; \
3540 session->field = val; \
3541 } \
3542 return count; \
3543}
3544
3545#define iscsi_priv_session_rw_attr(field, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05003546 iscsi_priv_session_attr_show(field, format) \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05303547 iscsi_priv_session_attr_store(field) \
Vasiliy Kulikov523f3c82011-02-04 15:24:14 +03003548static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05303549 show_priv_session_##field, \
3550 store_priv_session_##field)
3551iscsi_priv_session_rw_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05003552
Mike Christie1d063c12011-07-25 13:48:43 -05003553static struct attribute *iscsi_session_attrs[] = {
3554 &dev_attr_sess_initial_r2t.attr,
3555 &dev_attr_sess_max_outstanding_r2t.attr,
3556 &dev_attr_sess_immediate_data.attr,
3557 &dev_attr_sess_first_burst_len.attr,
3558 &dev_attr_sess_max_burst_len.attr,
3559 &dev_attr_sess_data_pdu_in_order.attr,
3560 &dev_attr_sess_data_seq_in_order.attr,
3561 &dev_attr_sess_erl.attr,
3562 &dev_attr_sess_targetname.attr,
3563 &dev_attr_sess_tpgt.attr,
3564 &dev_attr_sess_password.attr,
3565 &dev_attr_sess_password_in.attr,
3566 &dev_attr_sess_username.attr,
3567 &dev_attr_sess_username_in.attr,
3568 &dev_attr_sess_fast_abort.attr,
3569 &dev_attr_sess_abort_tmo.attr,
3570 &dev_attr_sess_lu_reset_tmo.attr,
3571 &dev_attr_sess_tgt_reset_tmo.attr,
3572 &dev_attr_sess_ifacename.attr,
3573 &dev_attr_sess_initiatorname.attr,
3574 &dev_attr_sess_targetalias.attr,
3575 &dev_attr_priv_sess_recovery_tmo.attr,
3576 &dev_attr_priv_sess_state.attr,
Mike Christie0c70d842011-12-05 16:44:01 -06003577 &dev_attr_priv_sess_creator.attr,
Mike Christie30534952012-02-27 03:08:53 -08003578 &dev_attr_sess_chap_out_idx.attr,
3579 &dev_attr_sess_chap_in_idx.attr,
Manish Rangankar930a9f02012-11-23 06:58:41 -05003580 &dev_attr_priv_sess_target_id.attr,
Mike Christie1d063c12011-07-25 13:48:43 -05003581 NULL,
3582};
3583
Al Viro587a1f12011-07-23 23:11:19 -04003584static umode_t iscsi_session_attr_is_visible(struct kobject *kobj,
Mike Christie1d063c12011-07-25 13:48:43 -05003585 struct attribute *attr, int i)
3586{
3587 struct device *cdev = container_of(kobj, struct device, kobj);
3588 struct iscsi_cls_session *session = transport_class_to_session(cdev);
3589 struct iscsi_transport *t = session->transport;
3590 int param;
3591
3592 if (attr == &dev_attr_sess_initial_r2t.attr)
3593 param = ISCSI_PARAM_INITIAL_R2T_EN;
3594 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
3595 param = ISCSI_PARAM_MAX_R2T;
3596 else if (attr == &dev_attr_sess_immediate_data.attr)
3597 param = ISCSI_PARAM_IMM_DATA_EN;
3598 else if (attr == &dev_attr_sess_first_burst_len.attr)
3599 param = ISCSI_PARAM_FIRST_BURST;
3600 else if (attr == &dev_attr_sess_max_burst_len.attr)
3601 param = ISCSI_PARAM_MAX_BURST;
3602 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
3603 param = ISCSI_PARAM_PDU_INORDER_EN;
3604 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
3605 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
3606 else if (attr == &dev_attr_sess_erl.attr)
3607 param = ISCSI_PARAM_ERL;
3608 else if (attr == &dev_attr_sess_targetname.attr)
3609 param = ISCSI_PARAM_TARGET_NAME;
3610 else if (attr == &dev_attr_sess_tpgt.attr)
3611 param = ISCSI_PARAM_TPGT;
Mike Christie30534952012-02-27 03:08:53 -08003612 else if (attr == &dev_attr_sess_chap_in_idx.attr)
3613 param = ISCSI_PARAM_CHAP_IN_IDX;
3614 else if (attr == &dev_attr_sess_chap_out_idx.attr)
3615 param = ISCSI_PARAM_CHAP_OUT_IDX;
Mike Christie1d063c12011-07-25 13:48:43 -05003616 else if (attr == &dev_attr_sess_password.attr)
3617 param = ISCSI_PARAM_USERNAME;
3618 else if (attr == &dev_attr_sess_password_in.attr)
3619 param = ISCSI_PARAM_USERNAME_IN;
3620 else if (attr == &dev_attr_sess_username.attr)
3621 param = ISCSI_PARAM_PASSWORD;
3622 else if (attr == &dev_attr_sess_username_in.attr)
3623 param = ISCSI_PARAM_PASSWORD_IN;
3624 else if (attr == &dev_attr_sess_fast_abort.attr)
3625 param = ISCSI_PARAM_FAST_ABORT;
3626 else if (attr == &dev_attr_sess_abort_tmo.attr)
3627 param = ISCSI_PARAM_ABORT_TMO;
3628 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
3629 param = ISCSI_PARAM_LU_RESET_TMO;
3630 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
3631 param = ISCSI_PARAM_TGT_RESET_TMO;
3632 else if (attr == &dev_attr_sess_ifacename.attr)
3633 param = ISCSI_PARAM_IFACE_NAME;
3634 else if (attr == &dev_attr_sess_initiatorname.attr)
3635 param = ISCSI_PARAM_INITIATOR_NAME;
3636 else if (attr == &dev_attr_sess_targetalias.attr)
3637 param = ISCSI_PARAM_TARGET_ALIAS;
3638 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
3639 return S_IRUGO | S_IWUSR;
3640 else if (attr == &dev_attr_priv_sess_state.attr)
3641 return S_IRUGO;
Mike Christie0c70d842011-12-05 16:44:01 -06003642 else if (attr == &dev_attr_priv_sess_creator.attr)
3643 return S_IRUGO;
Manish Rangankar930a9f02012-11-23 06:58:41 -05003644 else if (attr == &dev_attr_priv_sess_target_id.attr)
3645 return S_IRUGO;
Mike Christie1d063c12011-07-25 13:48:43 -05003646 else {
3647 WARN_ONCE(1, "Invalid session attr");
3648 return 0;
3649 }
3650
3651 return t->attr_is_visible(ISCSI_PARAM, param);
3652}
3653
3654static struct attribute_group iscsi_session_group = {
3655 .attrs = iscsi_session_attrs,
3656 .is_visible = iscsi_session_attr_is_visible,
3657};
3658
Mike Christie1819dc82007-05-30 12:57:08 -05003659/*
3660 * iSCSI host attrs
3661 */
3662#define iscsi_host_attr_show(param) \
3663static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01003664show_host_param_##param(struct device *dev, \
3665 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05003666{ \
Tony Jonesee959b02008-02-22 00:13:36 +01003667 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05003668 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
3669 return priv->iscsi_transport->get_host_param(shost, param, buf); \
3670}
3671
3672#define iscsi_host_attr(field, param) \
3673 iscsi_host_attr_show(param) \
3674static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
3675 NULL);
3676
Mike Christied8196ed2007-05-30 12:57:25 -05003677iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05003678iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05003679iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05003680iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08003681iscsi_host_attr(port_state, ISCSI_HOST_PARAM_PORT_STATE);
3682iscsi_host_attr(port_speed, ISCSI_HOST_PARAM_PORT_SPEED);
Mike Christie1819dc82007-05-30 12:57:08 -05003683
Mike Christief27fb2e2011-07-25 13:48:45 -05003684static struct attribute *iscsi_host_attrs[] = {
3685 &dev_attr_host_netdev.attr,
3686 &dev_attr_host_hwaddress.attr,
3687 &dev_attr_host_ipaddress.attr,
3688 &dev_attr_host_initiatorname.attr,
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08003689 &dev_attr_host_port_state.attr,
3690 &dev_attr_host_port_speed.attr,
Mike Christief27fb2e2011-07-25 13:48:45 -05003691 NULL,
3692};
3693
Al Viro587a1f12011-07-23 23:11:19 -04003694static umode_t iscsi_host_attr_is_visible(struct kobject *kobj,
Mike Christief27fb2e2011-07-25 13:48:45 -05003695 struct attribute *attr, int i)
3696{
3697 struct device *cdev = container_of(kobj, struct device, kobj);
3698 struct Scsi_Host *shost = transport_class_to_shost(cdev);
3699 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
3700 int param;
3701
3702 if (attr == &dev_attr_host_netdev.attr)
3703 param = ISCSI_HOST_PARAM_NETDEV_NAME;
3704 else if (attr == &dev_attr_host_hwaddress.attr)
3705 param = ISCSI_HOST_PARAM_HWADDRESS;
3706 else if (attr == &dev_attr_host_ipaddress.attr)
3707 param = ISCSI_HOST_PARAM_IPADDRESS;
3708 else if (attr == &dev_attr_host_initiatorname.attr)
3709 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08003710 else if (attr == &dev_attr_host_port_state.attr)
3711 param = ISCSI_HOST_PARAM_PORT_STATE;
3712 else if (attr == &dev_attr_host_port_speed.attr)
3713 param = ISCSI_HOST_PARAM_PORT_SPEED;
Mike Christief27fb2e2011-07-25 13:48:45 -05003714 else {
3715 WARN_ONCE(1, "Invalid host attr");
3716 return 0;
3717 }
3718
3719 return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
3720}
3721
3722static struct attribute_group iscsi_host_group = {
3723 .attrs = iscsi_host_attrs,
3724 .is_visible = iscsi_host_attr_is_visible,
3725};
Mike Christie1819dc82007-05-30 12:57:08 -05003726
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08003727/* convert iscsi_port_speed values to ascii string name */
3728static const struct {
3729 enum iscsi_port_speed value;
3730 char *name;
3731} iscsi_port_speed_names[] = {
3732 {ISCSI_PORT_SPEED_UNKNOWN, "Unknown" },
3733 {ISCSI_PORT_SPEED_10MBPS, "10 Mbps" },
3734 {ISCSI_PORT_SPEED_100MBPS, "100 Mbps" },
3735 {ISCSI_PORT_SPEED_1GBPS, "1 Gbps" },
3736 {ISCSI_PORT_SPEED_10GBPS, "10 Gbps" },
3737};
3738
3739char *iscsi_get_port_speed_name(struct Scsi_Host *shost)
3740{
3741 int i;
3742 char *speed = "Unknown!";
3743 struct iscsi_cls_host *ihost = shost->shost_data;
3744 uint32_t port_speed = ihost->port_speed;
3745
3746 for (i = 0; i < ARRAY_SIZE(iscsi_port_speed_names); i++) {
3747 if (iscsi_port_speed_names[i].value & port_speed) {
3748 speed = iscsi_port_speed_names[i].name;
3749 break;
3750 }
3751 }
3752 return speed;
3753}
3754EXPORT_SYMBOL_GPL(iscsi_get_port_speed_name);
3755
3756/* convert iscsi_port_state values to ascii string name */
3757static const struct {
3758 enum iscsi_port_state value;
3759 char *name;
3760} iscsi_port_state_names[] = {
3761 {ISCSI_PORT_STATE_DOWN, "LINK DOWN" },
3762 {ISCSI_PORT_STATE_UP, "LINK UP" },
3763};
3764
3765char *iscsi_get_port_state_name(struct Scsi_Host *shost)
3766{
3767 int i;
3768 char *state = "Unknown!";
3769 struct iscsi_cls_host *ihost = shost->shost_data;
3770 uint32_t port_state = ihost->port_state;
3771
3772 for (i = 0; i < ARRAY_SIZE(iscsi_port_state_names); i++) {
3773 if (iscsi_port_state_names[i].value & port_state) {
3774 state = iscsi_port_state_names[i].name;
3775 break;
3776 }
3777 }
3778 return state;
3779}
3780EXPORT_SYMBOL_GPL(iscsi_get_port_state_name);
3781
Alex Aizman0896b752005-08-04 19:33:07 -07003782static int iscsi_session_match(struct attribute_container *cont,
3783 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
Mike Christie7b8631b2006-01-13 18:05:50 -06003785 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07003787 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
Alex Aizman0896b752005-08-04 19:33:07 -07003789 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return 0;
3791
Mike Christie7b8631b2006-01-13 18:05:50 -06003792 session = iscsi_dev_to_session(dev);
3793 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07003794 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 return 0;
3796
Alex Aizman0896b752005-08-04 19:33:07 -07003797 priv = to_iscsi_internal(shost->transportt);
3798 if (priv->session_cont.ac.class != &iscsi_session_class.class)
3799 return 0;
3800
3801 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802}
3803
Alex Aizman0896b752005-08-04 19:33:07 -07003804static int iscsi_conn_match(struct attribute_container *cont,
3805 struct device *dev)
3806{
Mike Christie7b8631b2006-01-13 18:05:50 -06003807 struct iscsi_cls_session *session;
3808 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07003809 struct Scsi_Host *shost;
3810 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
Alex Aizman0896b752005-08-04 19:33:07 -07003812 if (!iscsi_is_conn_dev(dev))
3813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Mike Christie7b8631b2006-01-13 18:05:50 -06003815 conn = iscsi_dev_to_conn(dev);
3816 session = iscsi_dev_to_session(conn->dev.parent);
3817 shost = iscsi_session_to_shost(session);
3818
Alex Aizman0896b752005-08-04 19:33:07 -07003819 if (!shost->transportt)
3820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
Alex Aizman0896b752005-08-04 19:33:07 -07003822 priv = to_iscsi_internal(shost->transportt);
3823 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
3824 return 0;
3825
3826 return &priv->conn_cont.ac == cont;
3827}
3828
Mike Christie30a6c652006-04-06 21:13:39 -05003829static int iscsi_host_match(struct attribute_container *cont,
3830 struct device *dev)
3831{
3832 struct Scsi_Host *shost;
3833 struct iscsi_internal *priv;
3834
3835 if (!scsi_is_host_device(dev))
3836 return 0;
3837
3838 shost = dev_to_shost(dev);
3839 if (!shost->transportt ||
3840 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
3841 return 0;
3842
3843 priv = to_iscsi_internal(shost->transportt);
3844 return &priv->t.host_attrs.ac == cont;
3845}
3846
Mike Christie7b8631b2006-01-13 18:05:50 -06003847struct scsi_transport_template *
3848iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07003849{
3850 struct iscsi_internal *priv;
3851 unsigned long flags;
Mike Christief27fb2e2011-07-25 13:48:45 -05003852 int err;
Alex Aizman0896b752005-08-04 19:33:07 -07003853
3854 BUG_ON(!tt);
3855
3856 priv = iscsi_if_transport_lookup(tt);
3857 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06003858 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07003859
Jes Sorensen24669f752006-01-16 10:31:18 -05003860 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07003861 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06003862 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07003863 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07003864 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05003865 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06003866 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07003867
Tony Jonesee959b02008-02-22 00:13:36 +01003868 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01003869 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01003870 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07003871 if (err)
3872 goto free_priv;
3873
Tony Jonesee959b02008-02-22 00:13:36 +01003874 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07003875 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01003876 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07003877
Mike Christie30a6c652006-04-06 21:13:39 -05003878 /* host parameters */
Mike Christie30a6c652006-04-06 21:13:39 -05003879 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
3880 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christief27fb2e2011-07-25 13:48:45 -05003881 priv->t.host_attrs.ac.grp = &iscsi_host_group;
Mike Christie32c6e1b2008-05-21 15:53:58 -05003882 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05003883 transport_container_register(&priv->t.host_attrs);
3884
Alex Aizman0896b752005-08-04 19:33:07 -07003885 /* connection parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07003886 priv->conn_cont.ac.class = &iscsi_connection_class.class;
3887 priv->conn_cont.ac.match = iscsi_conn_match;
Mike Christie3128c6c2011-07-25 13:48:42 -05003888 priv->conn_cont.ac.grp = &iscsi_conn_group;
Alex Aizman0896b752005-08-04 19:33:07 -07003889 transport_container_register(&priv->conn_cont);
3890
Alex Aizman0896b752005-08-04 19:33:07 -07003891 /* session parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07003892 priv->session_cont.ac.class = &iscsi_session_class.class;
3893 priv->session_cont.ac.match = iscsi_session_match;
Mike Christie1d063c12011-07-25 13:48:43 -05003894 priv->session_cont.ac.grp = &iscsi_session_group;
Alex Aizman0896b752005-08-04 19:33:07 -07003895 transport_container_register(&priv->session_cont);
3896
Alex Aizman0896b752005-08-04 19:33:07 -07003897 spin_lock_irqsave(&iscsi_transport_lock, flags);
3898 list_add(&priv->list, &iscsi_transports);
3899 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Alex Aizman0896b752005-08-04 19:33:07 -07003901 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06003902 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
Tony Jonesee959b02008-02-22 00:13:36 +01003904unregister_dev:
3905 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05003906 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07003907free_priv:
3908 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06003909 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910}
Alex Aizman0896b752005-08-04 19:33:07 -07003911EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Alex Aizman0896b752005-08-04 19:33:07 -07003913int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914{
Alex Aizman0896b752005-08-04 19:33:07 -07003915 struct iscsi_internal *priv;
3916 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Alex Aizman0896b752005-08-04 19:33:07 -07003918 BUG_ON(!tt);
3919
Arjan van de Ven0b950672006-01-11 13:16:10 +01003920 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07003921
3922 priv = iscsi_if_transport_lookup(tt);
3923 BUG_ON (!priv);
3924
Alex Aizman0896b752005-08-04 19:33:07 -07003925 spin_lock_irqsave(&iscsi_transport_lock, flags);
3926 list_del(&priv->list);
3927 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
3928
3929 transport_container_unregister(&priv->conn_cont);
3930 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05003931 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07003932
Tony Jonesee959b02008-02-22 00:13:36 +01003933 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
3934 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01003935 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07003936
3937 return 0;
3938}
3939EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
3940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941static __init int iscsi_transport_init(void)
3942{
Alex Aizman0896b752005-08-04 19:33:07 -07003943 int err;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003944 struct netlink_kernel_cfg cfg = {
3945 .groups = 1,
3946 .input = iscsi_if_rx,
3947 };
Meelis Roos09492602006-12-17 12:10:26 -06003948 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05003949 ISCSI_TRANSPORT_VERSION);
3950
Mike Christie41be1442007-02-28 17:32:18 -06003951 atomic_set(&iscsi_session_nr, 0);
3952
Alex Aizman0896b752005-08-04 19:33:07 -07003953 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 if (err)
3955 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07003956
Mike Christied82ff9be2008-05-21 15:54:13 -05003957 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07003958 if (err)
3959 goto unregister_transport_class;
3960
Mike Christie8d079132011-07-25 13:48:40 -05003961 err = class_register(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05003962 if (err)
3963 goto unregister_endpoint_class;
3964
Mike Christie8d079132011-07-25 13:48:40 -05003965 err = transport_class_register(&iscsi_host_class);
3966 if (err)
3967 goto unregister_iface_class;
3968
Mike Christie30a6c652006-04-06 21:13:39 -05003969 err = transport_class_register(&iscsi_connection_class);
3970 if (err)
3971 goto unregister_host_class;
3972
Alex Aizman0896b752005-08-04 19:33:07 -07003973 err = transport_class_register(&iscsi_session_class);
3974 if (err)
3975 goto unregister_conn_class;
3976
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003977 err = bus_register(&iscsi_flashnode_bus);
3978 if (err)
3979 goto unregister_session_class;
3980
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003981 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, &cfg);
Alex Aizman0896b752005-08-04 19:33:07 -07003982 if (!nls) {
3983 err = -ENOBUFS;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003984 goto unregister_flashnode_bus;
Alex Aizman0896b752005-08-04 19:33:07 -07003985 }
3986
Mike Christied8bf5412007-12-13 12:43:27 -06003987 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
3988 if (!iscsi_eh_timer_workq)
3989 goto release_nls;
3990
Mike Christie43a145a2006-10-16 18:09:38 -04003991 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07003992
Mike Christied8bf5412007-12-13 12:43:27 -06003993release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08003994 netlink_kernel_release(nls);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003995unregister_flashnode_bus:
3996 bus_unregister(&iscsi_flashnode_bus);
Alex Aizman0896b752005-08-04 19:33:07 -07003997unregister_session_class:
3998 transport_class_unregister(&iscsi_session_class);
3999unregister_conn_class:
4000 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05004001unregister_host_class:
4002 transport_class_unregister(&iscsi_host_class);
Mike Christie8d079132011-07-25 13:48:40 -05004003unregister_iface_class:
4004 class_unregister(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05004005unregister_endpoint_class:
4006 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07004007unregister_transport_class:
4008 class_unregister(&iscsi_transport_class);
4009 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010}
4011
4012static void __exit iscsi_transport_exit(void)
4013{
Mike Christied8bf5412007-12-13 12:43:27 -06004014 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08004015 netlink_kernel_release(nls);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04004016 bus_unregister(&iscsi_flashnode_bus);
Alex Aizman0896b752005-08-04 19:33:07 -07004017 transport_class_unregister(&iscsi_connection_class);
4018 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05004019 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05004020 class_unregister(&iscsi_endpoint_class);
Mike Christie8d079132011-07-25 13:48:40 -05004021 class_unregister(&iscsi_iface_class);
Alex Aizman0896b752005-08-04 19:33:07 -07004022 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023}
4024
4025module_init(iscsi_transport_init);
4026module_exit(iscsi_transport_exit);
4027
Alex Aizman0896b752005-08-04 19:33:07 -07004028MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
4029 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
4030 "Alex Aizman <itn780@yahoo.com>");
4031MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05004033MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
Stephen Hemminger058548a2010-12-09 09:37:56 -08004034MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);