blob: 133926b1bb78bc39231260b5359dff3ff9a16bcb [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
Mike Christie8526cb12013-05-06 12:06:56 -05001022 * @idx: index to match
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001023 *
1024 * Finds the flashnode session object for the passed index
1025 *
1026 * Returns:
1027 * pointer to found flashnode session object on success
1028 * %NULL on failure
1029 */
1030static struct iscsi_bus_flash_session *
Mike Christie8526cb12013-05-06 12:06:56 -05001031iscsi_get_flashnode_by_index(struct Scsi_Host *shost, uint32_t idx)
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001032{
1033 struct iscsi_bus_flash_session *fnode_sess = NULL;
1034 struct device *dev;
1035
Mike Christie8526cb12013-05-06 12:06:56 -05001036 dev = device_find_child(&shost->shost_gendev, &idx,
1037 flashnode_match_index);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001038 if (dev)
1039 fnode_sess = iscsi_dev_to_flash_session(dev);
1040
1041 return fnode_sess;
1042}
1043
1044/**
1045 * iscsi_find_flashnode_sess - finds flashnode session entry
1046 * @shost: pointer to host data
1047 * @data: pointer to data containing value to use for comparison
1048 * @fn: function pointer that does actual comparison
1049 *
1050 * Finds the flashnode session object comparing the data passed using logic
1051 * defined in passed function pointer
1052 *
1053 * Returns:
1054 * pointer to found flashnode session device object on success
1055 * %NULL on failure
1056 */
1057struct device *
1058iscsi_find_flashnode_sess(struct Scsi_Host *shost, void *data,
1059 int (*fn)(struct device *dev, void *data))
1060{
Mike Christie8526cb12013-05-06 12:06:56 -05001061 return device_find_child(&shost->shost_gendev, data, fn);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001062}
1063EXPORT_SYMBOL_GPL(iscsi_find_flashnode_sess);
1064
1065/**
1066 * iscsi_find_flashnode_conn - finds flashnode connection entry
1067 * @fnode_sess: pointer to parent flashnode session entry
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001068 *
1069 * Finds the flashnode connection object comparing the data passed using logic
1070 * defined in passed function pointer
1071 *
1072 * Returns:
1073 * pointer to found flashnode connection device object on success
1074 * %NULL on failure
1075 */
1076struct device *
Mike Christie8526cb12013-05-06 12:06:56 -05001077iscsi_find_flashnode_conn(struct iscsi_bus_flash_session *fnode_sess)
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001078{
Mike Christie8526cb12013-05-06 12:06:56 -05001079 return device_find_child(&fnode_sess->dev, NULL,
1080 iscsi_is_flashnode_conn_dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001081}
1082EXPORT_SYMBOL_GPL(iscsi_find_flashnode_conn);
1083
1084static int iscsi_iter_destroy_flashnode_conn_fn(struct device *dev, void *data)
1085{
1086 if (!iscsi_is_flashnode_conn_dev(dev, NULL))
1087 return 0;
1088
1089 return iscsi_destroy_flashnode_conn(iscsi_dev_to_flash_conn(dev));
1090}
1091
1092/**
1093 * iscsi_destroy_flashnode_sess - destory flashnode session entry
1094 * @fnode_sess: pointer to flashnode session entry to be destroyed
1095 *
1096 * Deletes the flashnode session entry and all children flashnode connection
1097 * entries from sysfs
1098 */
1099void iscsi_destroy_flashnode_sess(struct iscsi_bus_flash_session *fnode_sess)
1100{
1101 int err;
1102
1103 err = device_for_each_child(&fnode_sess->dev, NULL,
1104 iscsi_iter_destroy_flashnode_conn_fn);
1105 if (err)
1106 pr_err("Could not delete all connections for %s. Error %d.\n",
1107 fnode_sess->dev.kobj.name, err);
1108
1109 device_unregister(&fnode_sess->dev);
1110}
1111EXPORT_SYMBOL_GPL(iscsi_destroy_flashnode_sess);
1112
1113static int iscsi_iter_destroy_flashnode_fn(struct device *dev, void *data)
1114{
1115 if (!iscsi_flashnode_bus_match(dev, NULL))
1116 return 0;
1117
1118 iscsi_destroy_flashnode_sess(iscsi_dev_to_flash_session(dev));
1119 return 0;
1120}
1121
1122/**
1123 * iscsi_destroy_all_flashnode - destory all flashnode session entries
1124 * @shost: pointer to host data
1125 *
1126 * Destroys all the flashnode session entries and all corresponding children
1127 * flashnode connection entries from sysfs
1128 */
1129void iscsi_destroy_all_flashnode(struct Scsi_Host *shost)
1130{
1131 device_for_each_child(&shost->shost_gendev, NULL,
1132 iscsi_iter_destroy_flashnode_fn);
1133}
1134EXPORT_SYMBOL_GPL(iscsi_destroy_all_flashnode);
1135
1136/*
Mike Christie90eeb012011-07-25 13:48:50 -05001137 * BSG support
1138 */
1139/**
1140 * iscsi_bsg_host_dispatch - Dispatch command to LLD.
1141 * @job: bsg job to be processed
1142 */
1143static int iscsi_bsg_host_dispatch(struct bsg_job *job)
1144{
1145 struct Scsi_Host *shost = iscsi_job_to_shost(job);
1146 struct iscsi_bsg_request *req = job->request;
1147 struct iscsi_bsg_reply *reply = job->reply;
1148 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1149 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
1150 int ret;
1151
1152 /* check if we have the msgcode value at least */
1153 if (job->request_len < sizeof(uint32_t)) {
1154 ret = -ENOMSG;
1155 goto fail_host_msg;
1156 }
1157
1158 /* Validate the host command */
1159 switch (req->msgcode) {
1160 case ISCSI_BSG_HST_VENDOR:
1161 cmdlen += sizeof(struct iscsi_bsg_host_vendor);
1162 if ((shost->hostt->vendor_id == 0L) ||
1163 (req->rqst_data.h_vendor.vendor_id !=
1164 shost->hostt->vendor_id)) {
1165 ret = -ESRCH;
1166 goto fail_host_msg;
1167 }
1168 break;
1169 default:
1170 ret = -EBADR;
1171 goto fail_host_msg;
1172 }
1173
1174 /* check if we really have all the request data needed */
1175 if (job->request_len < cmdlen) {
1176 ret = -ENOMSG;
1177 goto fail_host_msg;
1178 }
1179
1180 ret = i->iscsi_transport->bsg_request(job);
1181 if (!ret)
1182 return 0;
1183
1184fail_host_msg:
1185 /* return the errno failure code as the only status */
1186 BUG_ON(job->reply_len < sizeof(uint32_t));
1187 reply->reply_payload_rcv_len = 0;
1188 reply->result = ret;
1189 job->reply_len = sizeof(uint32_t);
1190 bsg_job_done(job, ret, 0);
1191 return 0;
1192}
1193
1194/**
1195 * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
1196 * @shost: shost for iscsi_host
Marcos Paulo de Souzaeae627e2011-11-02 11:17:17 -02001197 * @ihost: iscsi_cls_host adding the structures to
Mike Christie90eeb012011-07-25 13:48:50 -05001198 */
1199static int
1200iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
1201{
1202 struct device *dev = &shost->shost_gendev;
1203 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1204 struct request_queue *q;
1205 char bsg_name[20];
1206 int ret;
1207
1208 if (!i->iscsi_transport->bsg_request)
1209 return -ENOTSUPP;
1210
1211 snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
1212
1213 q = __scsi_alloc_queue(shost, bsg_request_fn);
1214 if (!q)
1215 return -ENOMEM;
1216
1217 ret = bsg_setup_queue(dev, q, bsg_name, iscsi_bsg_host_dispatch, 0);
1218 if (ret) {
1219 shost_printk(KERN_ERR, shost, "bsg interface failed to "
1220 "initialize - no request queue\n");
1221 blk_cleanup_queue(q);
1222 return ret;
1223 }
1224
1225 ihost->bsg_q = q;
1226 return 0;
1227}
1228
Mike Christie30a6c652006-04-06 21:13:39 -05001229static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +01001230 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -05001231{
1232 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001233 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -05001234
1235 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -06001236 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -05001237 mutex_init(&ihost->mutex);
Mike Christie90eeb012011-07-25 13:48:50 -05001238
1239 iscsi_bsg_host_add(shost, ihost);
1240 /* ignore any bsg add error - we just can't do sgio */
1241
1242 return 0;
1243}
1244
1245static int iscsi_remove_host(struct transport_container *tc,
1246 struct device *dev, struct device *cdev)
1247{
1248 struct Scsi_Host *shost = dev_to_shost(dev);
1249 struct iscsi_cls_host *ihost = shost->shost_data;
1250
1251 if (ihost->bsg_q) {
Tejun Heo86072d82012-06-04 20:40:54 -07001252 bsg_unregister_queue(ihost->bsg_q);
Mike Christie90eeb012011-07-25 13:48:50 -05001253 blk_cleanup_queue(ihost->bsg_q);
1254 }
Mike Christie30a6c652006-04-06 21:13:39 -05001255 return 0;
1256}
1257
1258static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
1259 "iscsi_host",
1260 iscsi_setup_host,
Mike Christie90eeb012011-07-25 13:48:50 -05001261 iscsi_remove_host,
Mike Christie30a6c652006-04-06 21:13:39 -05001262 NULL);
1263
Alex Aizman0896b752005-08-04 19:33:07 -07001264static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
1265 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 NULL,
1267 NULL,
1268 NULL);
1269
Alex Aizman0896b752005-08-04 19:33:07 -07001270static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
1271 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 NULL,
1273 NULL,
1274 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -07001275
1276static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +01001277static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001278
Mike Christie7b7232f2006-02-01 21:06:49 -06001279static LIST_HEAD(sesslist);
1280static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -07001281static LIST_HEAD(connlist);
1282static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Mike Christieb5c7a122006-04-06 21:13:33 -05001284static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
1285{
1286 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
1287 return sess->sid;
1288}
1289
1290/*
1291 * Returns the matching session to a given sid
1292 */
1293static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -06001294{
1295 unsigned long flags;
1296 struct iscsi_cls_session *sess;
1297
1298 spin_lock_irqsave(&sesslock, flags);
1299 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -05001300 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001301 spin_unlock_irqrestore(&sesslock, flags);
1302 return sess;
1303 }
1304 }
1305 spin_unlock_irqrestore(&sesslock, flags);
1306 return NULL;
1307}
1308
Mike Christieb5c7a122006-04-06 21:13:33 -05001309/*
1310 * Returns the matching connection to a given sid / cid tuple
1311 */
1312static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -06001313{
1314 unsigned long flags;
1315 struct iscsi_cls_conn *conn;
1316
1317 spin_lock_irqsave(&connlock, flags);
1318 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -05001319 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001320 spin_unlock_irqrestore(&connlock, flags);
1321 return conn;
1322 }
1323 }
1324 spin_unlock_irqrestore(&connlock, flags);
1325 return NULL;
1326}
1327
Mike Christie7b8631b2006-01-13 18:05:50 -06001328/*
1329 * The following functions can be used by LLDs that allocate
1330 * their own scsi_hosts or by software iscsi LLDs
1331 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001332static struct {
1333 int value;
1334 char *name;
1335} iscsi_session_state_names[] = {
1336 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
1337 { ISCSI_SESSION_FAILED, "FAILED" },
1338 { ISCSI_SESSION_FREE, "FREE" },
1339};
1340
Adrian Bunk3b0f2082008-02-13 23:30:17 +02001341static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -06001342{
1343 int i;
1344 char *name = NULL;
1345
1346 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
1347 if (iscsi_session_state_names[i].value == state) {
1348 name = iscsi_session_state_names[i].name;
1349 break;
1350 }
1351 }
1352 return name;
1353}
1354
1355int iscsi_session_chkready(struct iscsi_cls_session *session)
1356{
1357 unsigned long flags;
1358 int err;
1359
1360 spin_lock_irqsave(&session->lock, flags);
1361 switch (session->state) {
1362 case ISCSI_SESSION_LOGGED_IN:
1363 err = 0;
1364 break;
1365 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -05001366 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -06001367 break;
1368 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -05001369 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -06001370 break;
1371 default:
1372 err = DID_NO_CONNECT << 16;
1373 break;
1374 }
1375 spin_unlock_irqrestore(&session->lock, flags);
1376 return err;
1377}
1378EXPORT_SYMBOL_GPL(iscsi_session_chkready);
1379
Manish Rangankar17fa5752011-07-25 13:48:52 -05001380int iscsi_is_session_online(struct iscsi_cls_session *session)
1381{
1382 unsigned long flags;
1383 int ret = 0;
1384
1385 spin_lock_irqsave(&session->lock, flags);
1386 if (session->state == ISCSI_SESSION_LOGGED_IN)
1387 ret = 1;
1388 spin_unlock_irqrestore(&session->lock, flags);
1389 return ret;
1390}
1391EXPORT_SYMBOL_GPL(iscsi_is_session_online);
1392
Mike Christie7b8631b2006-01-13 18:05:50 -06001393static void iscsi_session_release(struct device *dev)
1394{
1395 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001396 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Mike Christie7b8631b2006-01-13 18:05:50 -06001398 shost = iscsi_session_to_shost(session);
1399 scsi_host_put(shost);
Mike Christie632248a2009-08-20 15:11:01 -05001400 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001401 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -06001402}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Nilesh Javali6260a5d2012-02-27 03:08:51 -08001404int iscsi_is_session_dev(const struct device *dev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001405{
1406 return dev->release == iscsi_session_release;
1407}
Nilesh Javali6260a5d2012-02-27 03:08:51 -08001408EXPORT_SYMBOL_GPL(iscsi_is_session_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Mike Christiea4804cd2008-05-21 15:54:00 -05001410static int iscsi_iter_session_fn(struct device *dev, void *data)
1411{
1412 void (* fn) (struct iscsi_cls_session *) = data;
1413
1414 if (!iscsi_is_session_dev(dev))
1415 return 0;
1416 fn(iscsi_dev_to_session(dev));
1417 return 0;
1418}
1419
1420void iscsi_host_for_each_session(struct Scsi_Host *shost,
1421 void (*fn)(struct iscsi_cls_session *))
1422{
1423 device_for_each_child(&shost->shost_gendev, fn,
1424 iscsi_iter_session_fn);
1425}
1426EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
1427
Mike Christie8aae18a2008-01-31 13:36:48 -06001428/**
1429 * iscsi_scan_finished - helper to report when running scans are done
1430 * @shost: scsi host
1431 * @time: scan run time
1432 *
1433 * This function can be used by drives like qla4xxx to report to the scsi
1434 * layer when the scans it kicked off at module load time are done.
1435 */
1436int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
1437{
Mike Christie32c6e1b2008-05-21 15:53:58 -05001438 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -06001439 /*
1440 * qla4xxx will have kicked off some session unblocks before calling
1441 * scsi_scan_host, so just wait for them to complete.
1442 */
1443 return !atomic_read(&ihost->nr_scans);
1444}
1445EXPORT_SYMBOL_GPL(iscsi_scan_finished);
1446
Mike Christie79706342008-05-21 15:54:12 -05001447struct iscsi_scan_data {
1448 unsigned int channel;
1449 unsigned int id;
1450 unsigned int lun;
1451};
1452
1453static int iscsi_user_scan_session(struct device *dev, void *data)
1454{
1455 struct iscsi_scan_data *scan_data = data;
1456 struct iscsi_cls_session *session;
1457 struct Scsi_Host *shost;
1458 struct iscsi_cls_host *ihost;
1459 unsigned long flags;
1460 unsigned int id;
1461
1462 if (!iscsi_is_session_dev(dev))
1463 return 0;
1464
1465 session = iscsi_dev_to_session(dev);
Mike Christie632248a2009-08-20 15:11:01 -05001466
1467 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
1468
Mike Christie79706342008-05-21 15:54:12 -05001469 shost = iscsi_session_to_shost(session);
1470 ihost = shost->shost_data;
1471
1472 mutex_lock(&ihost->mutex);
1473 spin_lock_irqsave(&session->lock, flags);
1474 if (session->state != ISCSI_SESSION_LOGGED_IN) {
1475 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05001476 goto user_scan_exit;
Mike Christie79706342008-05-21 15:54:12 -05001477 }
1478 id = session->target_id;
1479 spin_unlock_irqrestore(&session->lock, flags);
1480
1481 if (id != ISCSI_MAX_TARGET) {
1482 if ((scan_data->channel == SCAN_WILD_CARD ||
1483 scan_data->channel == 0) &&
1484 (scan_data->id == SCAN_WILD_CARD ||
1485 scan_data->id == id))
1486 scsi_scan_target(&session->dev, 0, id,
1487 scan_data->lun, 1);
1488 }
Mike Christie632248a2009-08-20 15:11:01 -05001489
1490user_scan_exit:
Mike Christie79706342008-05-21 15:54:12 -05001491 mutex_unlock(&ihost->mutex);
Mike Christie632248a2009-08-20 15:11:01 -05001492 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
Mike Christie79706342008-05-21 15:54:12 -05001493 return 0;
1494}
1495
Mike Christie30a6c652006-04-06 21:13:39 -05001496static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
1497 uint id, uint lun)
1498{
Mike Christie79706342008-05-21 15:54:12 -05001499 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -05001500
Mike Christie79706342008-05-21 15:54:12 -05001501 scan_data.channel = channel;
1502 scan_data.id = id;
1503 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -05001504
Mike Christie79706342008-05-21 15:54:12 -05001505 return device_for_each_child(&shost->shost_gendev, &scan_data,
1506 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -05001507}
1508
Mike Christiebd976f62008-01-31 13:36:46 -06001509static void iscsi_scan_session(struct work_struct *work)
1510{
1511 struct iscsi_cls_session *session =
1512 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -06001513 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001514 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -05001515 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -06001516
Mike Christie79706342008-05-21 15:54:12 -05001517 scan_data.channel = 0;
1518 scan_data.id = SCAN_WILD_CARD;
1519 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -06001520
Mike Christie79706342008-05-21 15:54:12 -05001521 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -06001522 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -06001523}
1524
Mike Christiec01be6d2010-07-22 16:59:49 +05301525/**
1526 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
Randy Dunlape6d4ef42010-08-14 13:05:41 -07001527 * @cmd: scsi cmd passed to scsi eh handler
Mike Christiec01be6d2010-07-22 16:59:49 +05301528 *
1529 * If the session is down this function will wait for the recovery
1530 * timer to fire or for the session to be logged back in. If the
1531 * recovery timer fires then FAST_IO_FAIL is returned. The caller
1532 * should pass this error value to the scsi eh.
1533 */
1534int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
1535{
1536 struct iscsi_cls_session *session =
1537 starget_to_session(scsi_target(cmd->device));
1538 unsigned long flags;
1539 int ret = 0;
1540
1541 spin_lock_irqsave(&session->lock, flags);
1542 while (session->state != ISCSI_SESSION_LOGGED_IN) {
1543 if (session->state == ISCSI_SESSION_FREE) {
1544 ret = FAST_IO_FAIL;
1545 break;
1546 }
1547 spin_unlock_irqrestore(&session->lock, flags);
1548 msleep(1000);
1549 spin_lock_irqsave(&session->lock, flags);
1550 }
1551 spin_unlock_irqrestore(&session->lock, flags);
1552 return ret;
1553}
1554EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
1555
David Howellsc4028952006-11-22 14:57:56 +00001556static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -05001557{
David Howellsc4028952006-11-22 14:57:56 +00001558 struct iscsi_cls_session *session =
1559 container_of(work, struct iscsi_cls_session,
1560 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -06001561 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -05001562
Mike Christie322d7392008-01-31 13:36:52 -06001563 iscsi_cls_session_printk(KERN_INFO, session,
1564 "session recovery timed out after %d secs\n",
1565 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -05001566
Mike Christie6eabafb2008-01-31 13:36:43 -06001567 spin_lock_irqsave(&session->lock, flags);
1568 switch (session->state) {
1569 case ISCSI_SESSION_FAILED:
1570 session->state = ISCSI_SESSION_FREE;
1571 break;
1572 case ISCSI_SESSION_LOGGED_IN:
1573 case ISCSI_SESSION_FREE:
1574 /* we raced with the unblock's flush */
1575 spin_unlock_irqrestore(&session->lock, flags);
1576 return;
1577 }
1578 spin_unlock_irqrestore(&session->lock, flags);
1579
Mike Christie30a6c652006-04-06 21:13:39 -05001580 if (session->transport->session_recovery_timedout)
1581 session->transport->session_recovery_timedout(session);
1582
Mike Christie632248a2009-08-20 15:11:01 -05001583 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
Mike Christie5d9fb5c2012-05-17 23:56:57 -05001584 scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
Mike Christie632248a2009-08-20 15:11:01 -05001585 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
Mike Christie30a6c652006-04-06 21:13:39 -05001586}
1587
Mike Christie45ab33b2008-03-04 13:26:55 -06001588static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -05001589{
Mike Christie45ab33b2008-03-04 13:26:55 -06001590 struct iscsi_cls_session *session =
1591 container_of(work, struct iscsi_cls_session,
1592 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -06001593 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001594 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -06001595 unsigned long flags;
1596
Mike Christie632248a2009-08-20 15:11:01 -05001597 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
Mike Christie45ab33b2008-03-04 13:26:55 -06001598 /*
1599 * The recovery and unblock work get run from the same workqueue,
1600 * so try to cancel it if it was going to run after this unblock.
1601 */
1602 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -06001603 spin_lock_irqsave(&session->lock, flags);
1604 session->state = ISCSI_SESSION_LOGGED_IN;
1605 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -06001606 /* start IO */
Mike Christie5d9fb5c2012-05-17 23:56:57 -05001607 scsi_target_unblock(&session->dev, SDEV_RUNNING);
Mike Christie8aae18a2008-01-31 13:36:48 -06001608 /*
1609 * Only do kernel scanning if the driver is properly hooked into
1610 * the async scanning code (drivers like iscsi_tcp do login and
1611 * scanning from userspace).
1612 */
1613 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -06001614 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -06001615 atomic_inc(&ihost->nr_scans);
1616 }
Mike Christie632248a2009-08-20 15:11:01 -05001617 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -06001618}
Mike Christie45ab33b2008-03-04 13:26:55 -06001619
1620/**
1621 * iscsi_unblock_session - set a session as logged in and start IO.
1622 * @session: iscsi session
1623 *
1624 * Mark a session as ready to accept IO.
1625 */
1626void iscsi_unblock_session(struct iscsi_cls_session *session)
1627{
1628 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
1629 /*
1630 * make sure all the events have completed before tell the driver
1631 * it is safe
1632 */
1633 flush_workqueue(iscsi_eh_timer_workq);
1634}
Mike Christie30a6c652006-04-06 21:13:39 -05001635EXPORT_SYMBOL_GPL(iscsi_unblock_session);
1636
Mike Christie45ab33b2008-03-04 13:26:55 -06001637static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -05001638{
Mike Christie45ab33b2008-03-04 13:26:55 -06001639 struct iscsi_cls_session *session =
1640 container_of(work, struct iscsi_cls_session,
1641 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -06001642 unsigned long flags;
1643
Mike Christie632248a2009-08-20 15:11:01 -05001644 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -06001645 spin_lock_irqsave(&session->lock, flags);
1646 session->state = ISCSI_SESSION_FAILED;
1647 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -05001648 scsi_target_block(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001649 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
Mike Christiefdd46dc2009-11-11 16:34:34 -06001650 if (session->recovery_tmo >= 0)
1651 queue_delayed_work(iscsi_eh_timer_workq,
1652 &session->recovery_work,
1653 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -05001654}
Mike Christie45ab33b2008-03-04 13:26:55 -06001655
1656void iscsi_block_session(struct iscsi_cls_session *session)
1657{
1658 queue_work(iscsi_eh_timer_workq, &session->block_work);
1659}
Mike Christie30a6c652006-04-06 21:13:39 -05001660EXPORT_SYMBOL_GPL(iscsi_block_session);
1661
Mike Christie26974782007-12-13 12:43:29 -06001662static void __iscsi_unbind_session(struct work_struct *work)
1663{
1664 struct iscsi_cls_session *session =
1665 container_of(work, struct iscsi_cls_session,
1666 unbind_work);
1667 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001668 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -05001669 unsigned long flags;
Mike Christie8d4a6902011-10-06 03:56:57 -05001670 unsigned int target_id;
Mike Christie26974782007-12-13 12:43:29 -06001671
Mike Christie632248a2009-08-20 15:11:01 -05001672 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
1673
Mike Christie26974782007-12-13 12:43:29 -06001674 /* Prevent new scans and make sure scanning is not in progress */
1675 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -05001676 spin_lock_irqsave(&session->lock, flags);
1677 if (session->target_id == ISCSI_MAX_TARGET) {
1678 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001679 mutex_unlock(&ihost->mutex);
1680 return;
1681 }
Mike Christie8d4a6902011-10-06 03:56:57 -05001682
1683 target_id = session->target_id;
Mike Christie79706342008-05-21 15:54:12 -05001684 session->target_id = ISCSI_MAX_TARGET;
1685 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001686 mutex_unlock(&ihost->mutex);
1687
Mike Christie8d4a6902011-10-06 03:56:57 -05001688 if (session->ida_used)
1689 ida_simple_remove(&iscsi_sess_ida, target_id);
1690
Mike Christie26974782007-12-13 12:43:29 -06001691 scsi_remove_target(&session->dev);
1692 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -05001693 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
Mike Christie26974782007-12-13 12:43:29 -06001694}
1695
Mike Christie7b8631b2006-01-13 18:05:50 -06001696struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -05001697iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1698 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06001699{
1700 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Mike Christie5d91e202008-05-21 15:54:01 -05001702 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -05001703 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001704 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -05001705 return NULL;
1706
Mike Christie7b8631b2006-01-13 18:05:50 -06001707 session->transport = transport;
Mike Christie0c70d842011-12-05 16:44:01 -06001708 session->creator = -1;
Mike Christie30a6c652006-04-06 21:13:39 -05001709 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -06001710 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +00001711 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -05001712 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -06001713 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
1714 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -06001715 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -06001716 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001717 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -06001718
Mike Christie6a8a0d32006-06-28 12:00:31 -05001719 /* this is released in the dev's release function */
1720 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -05001721 session->dev.parent = &shost->shost_gendev;
1722 session->dev.release = iscsi_session_release;
1723 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -05001724 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -05001725 session->dd_data = &session[1];
Mike Christie632248a2009-08-20 15:11:01 -05001726
1727 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001728 return session;
1729}
1730EXPORT_SYMBOL_GPL(iscsi_alloc_session);
1731
Mike Christie6a8a0d32006-06-28 12:00:31 -05001732int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05001733{
1734 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001735 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -06001736 unsigned long flags;
Mike Christie8d4a6902011-10-06 03:56:57 -05001737 int id = 0;
Mike Christie8434aa82006-06-28 12:00:30 -05001738 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -05001739
Mike Christie30a6c652006-04-06 21:13:39 -05001740 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -06001741 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -05001742
Mike Christie8d4a6902011-10-06 03:56:57 -05001743 if (target_id == ISCSI_MAX_TARGET) {
1744 id = ida_simple_get(&iscsi_sess_ida, 0, 0, GFP_KERNEL);
Mike Christie79706342008-05-21 15:54:12 -05001745
Mike Christie8d4a6902011-10-06 03:56:57 -05001746 if (id < 0) {
Mike Christie79706342008-05-21 15:54:12 -05001747 iscsi_cls_session_printk(KERN_ERR, session,
Mike Christie8d4a6902011-10-06 03:56:57 -05001748 "Failure in Target ID Allocation\n");
1749 return id;
Mike Christie79706342008-05-21 15:54:12 -05001750 }
Mike Christie8d4a6902011-10-06 03:56:57 -05001751 session->target_id = (unsigned int)id;
1752 session->ida_used = true;
1753 } else
1754 session->target_id = target_id;
Mike Christie30a6c652006-04-06 21:13:39 -05001755
Kay Sievers71610f52008-12-03 22:41:36 +01001756 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -05001757 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001758 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001759 iscsi_cls_session_printk(KERN_ERR, session,
1760 "could not register session's dev\n");
Mike Christie8d4a6902011-10-06 03:56:57 -05001761 goto release_ida;
Mike Christie7b8631b2006-01-13 18:05:50 -06001762 }
1763 transport_register_device(&session->dev);
1764
Mike Christie26974782007-12-13 12:43:29 -06001765 spin_lock_irqsave(&sesslock, flags);
1766 list_add(&session->sess_list, &sesslist);
1767 spin_unlock_irqrestore(&sesslock, flags);
1768
Mike Christie26974782007-12-13 12:43:29 -06001769 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -05001770 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001771 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -05001772
Mike Christie8d4a6902011-10-06 03:56:57 -05001773release_ida:
1774 if (session->ida_used)
1775 ida_simple_remove(&iscsi_sess_ida, session->target_id);
1776
Mike Christie8434aa82006-06-28 12:00:30 -05001777 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -06001778}
Mike Christie8434aa82006-06-28 12:00:30 -05001779EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -06001780
1781/**
Mike Christie8434aa82006-06-28 12:00:30 -05001782 * iscsi_create_session - create iscsi class session
1783 * @shost: scsi host
1784 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -05001785 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -05001786 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -06001787 *
Mike Christie8434aa82006-06-28 12:00:30 -05001788 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001789 */
Mike Christie8434aa82006-06-28 12:00:30 -05001790struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -05001791iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1792 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05001793{
1794 struct iscsi_cls_session *session;
1795
Mike Christie5d91e202008-05-21 15:54:01 -05001796 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -05001797 if (!session)
1798 return NULL;
1799
Mike Christie6a8a0d32006-06-28 12:00:31 -05001800 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -05001801 iscsi_free_session(session);
1802 return NULL;
1803 }
1804 return session;
1805}
1806EXPORT_SYMBOL_GPL(iscsi_create_session);
1807
Mike Christie26974782007-12-13 12:43:29 -06001808static void iscsi_conn_release(struct device *dev)
1809{
1810 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
1811 struct device *parent = conn->dev.parent;
1812
Mike Christie632248a2009-08-20 15:11:01 -05001813 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
Mike Christie26974782007-12-13 12:43:29 -06001814 kfree(conn);
1815 put_device(parent);
1816}
1817
1818static int iscsi_is_conn_dev(const struct device *dev)
1819{
1820 return dev->release == iscsi_conn_release;
1821}
1822
1823static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
1824{
1825 if (!iscsi_is_conn_dev(dev))
1826 return 0;
1827 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
1828}
1829
Mike Christie8434aa82006-06-28 12:00:30 -05001830void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001831{
Mike Christie30a6c652006-04-06 21:13:39 -05001832 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie26974782007-12-13 12:43:29 -06001833 unsigned long flags;
1834 int err;
Mike Christie30a6c652006-04-06 21:13:39 -05001835
Mike Christie632248a2009-08-20 15:11:01 -05001836 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
1837
Mike Christie26974782007-12-13 12:43:29 -06001838 spin_lock_irqsave(&sesslock, flags);
1839 list_del(&session->sess_list);
1840 spin_unlock_irqrestore(&sesslock, flags);
1841
Mike Christie45ab33b2008-03-04 13:26:55 -06001842 /* make sure there are no blocks/unblocks queued */
1843 flush_workqueue(iscsi_eh_timer_workq);
1844 /* make sure the timedout callout is not running */
1845 if (!cancel_delayed_work(&session->recovery_work))
1846 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -06001847 /*
1848 * If we are blocked let commands flow again. The lld or iscsi
1849 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -06001850 * We assume that LLD will not be calling block/unblock while
1851 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -06001852 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001853 spin_lock_irqsave(&session->lock, flags);
1854 session->state = ISCSI_SESSION_FREE;
1855 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -06001856
Mike Christie5d9fb5c2012-05-17 23:56:57 -05001857 scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
Mike Christie45ab33b2008-03-04 13:26:55 -06001858 /* flush running scans then delete devices */
Mike Christie06d25af2009-03-05 14:46:02 -06001859 scsi_flush_work(shost);
Mike Christie45ab33b2008-03-04 13:26:55 -06001860 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -05001861
Mike Christie26974782007-12-13 12:43:29 -06001862 /* hw iscsi may not have removed all connections from session */
1863 err = device_for_each_child(&session->dev, NULL,
1864 iscsi_iter_destroy_conn_fn);
1865 if (err)
Mike Christie322d7392008-01-31 13:36:52 -06001866 iscsi_cls_session_printk(KERN_ERR, session,
1867 "Could not delete all connections "
1868 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -05001869
Mike Christie7b8631b2006-01-13 18:05:50 -06001870 transport_unregister_device(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001871
1872 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001873 device_del(&session->dev);
1874}
1875EXPORT_SYMBOL_GPL(iscsi_remove_session);
1876
1877void iscsi_free_session(struct iscsi_cls_session *session)
1878{
Mike Christie632248a2009-08-20 15:11:01 -05001879 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
Mike Christie26974782007-12-13 12:43:29 -06001880 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -05001881 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001882}
Mike Christie8434aa82006-06-28 12:00:30 -05001883EXPORT_SYMBOL_GPL(iscsi_free_session);
1884
1885/**
1886 * iscsi_destroy_session - destroy iscsi session
1887 * @session: iscsi_session
1888 *
1889 * Can be called by a LLD or iscsi_transport. There must not be
1890 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -05001891 */
Mike Christie8434aa82006-06-28 12:00:30 -05001892int iscsi_destroy_session(struct iscsi_cls_session *session)
1893{
1894 iscsi_remove_session(session);
Mike Christie632248a2009-08-20 15:11:01 -05001895 ISCSI_DBG_TRANS_SESSION(session, "Completing session destruction\n");
Mike Christie8434aa82006-06-28 12:00:30 -05001896 iscsi_free_session(session);
1897 return 0;
1898}
Mike Christie7b8631b2006-01-13 18:05:50 -06001899EXPORT_SYMBOL_GPL(iscsi_destroy_session);
1900
Mike Christie7b8631b2006-01-13 18:05:50 -06001901/**
1902 * iscsi_create_conn - create iscsi class connection
1903 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -05001904 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -06001905 * @cid: connection id
1906 *
1907 * This can be called from a LLD or iscsi_transport. The connection
1908 * is child of the session so cid must be unique for all connections
1909 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -05001910 *
1911 * Since we do not support MCS, cid will normally be zero. In some cases
1912 * for software iscsi we could be trying to preallocate a connection struct
1913 * in which case there could be two connection structs and cid would be
1914 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -05001915 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001916struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05001917iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -06001918{
1919 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -06001920 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -06001921 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -06001922 int err;
1923
Mike Christie5d91e202008-05-21 15:54:01 -05001924 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06001925 if (!conn)
1926 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -05001927 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06001928 conn->dd_data = &conn[1];
1929
Mike Christie22a39fb2011-02-16 15:04:33 -06001930 mutex_init(&conn->ep_mutex);
Mike Christie7b8631b2006-01-13 18:05:50 -06001931 INIT_LIST_HEAD(&conn->conn_list);
1932 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -05001933 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001934
1935 /* this is released in the dev's release function */
1936 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -04001937 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -05001938
Kay Sievers71610f52008-12-03 22:41:36 +01001939 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001940 conn->dev.parent = &session->dev;
1941 conn->dev.release = iscsi_conn_release;
1942 err = device_register(&conn->dev);
1943 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06001944 iscsi_cls_session_printk(KERN_ERR, session, "could not "
1945 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001946 goto release_parent_ref;
1947 }
1948 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -06001949
1950 spin_lock_irqsave(&connlock, flags);
1951 list_add(&conn->conn_list, &connlist);
Mike Christie26974782007-12-13 12:43:29 -06001952 spin_unlock_irqrestore(&connlock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05001953
1954 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001955 return conn;
1956
1957release_parent_ref:
1958 put_device(&session->dev);
1959free_conn:
1960 kfree(conn);
1961 return NULL;
1962}
1963
1964EXPORT_SYMBOL_GPL(iscsi_create_conn);
1965
1966/**
1967 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -05001968 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -06001969 *
Mike Christie26974782007-12-13 12:43:29 -06001970 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05001971 */
Mike Christie7b8631b2006-01-13 18:05:50 -06001972int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
1973{
Mike Christie26974782007-12-13 12:43:29 -06001974 unsigned long flags;
1975
1976 spin_lock_irqsave(&connlock, flags);
Mike Christie26974782007-12-13 12:43:29 -06001977 list_del(&conn->conn_list);
1978 spin_unlock_irqrestore(&connlock, flags);
1979
Mike Christie7b8631b2006-01-13 18:05:50 -06001980 transport_unregister_device(&conn->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001981 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001982 device_unregister(&conn->dev);
1983 return 0;
1984}
Mike Christie7b8631b2006-01-13 18:05:50 -06001985EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
1986
1987/*
Mike Christie7b8631b2006-01-13 18:05:50 -06001988 * iscsi interface functions
1989 */
Alex Aizman0896b752005-08-04 19:33:07 -07001990static struct iscsi_internal *
1991iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Alex Aizman0896b752005-08-04 19:33:07 -07001993 struct iscsi_internal *priv;
1994 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Alex Aizman0896b752005-08-04 19:33:07 -07001996 spin_lock_irqsave(&iscsi_transport_lock, flags);
1997 list_for_each_entry(priv, &iscsi_transports, list) {
1998 if (tt == priv->iscsi_transport) {
1999 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2000 return priv;
2001 }
2002 }
2003 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2004 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Alex Aizman0896b752005-08-04 19:33:07 -07002007static int
Michael Chan43514772009-06-08 18:14:41 -07002008iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -05002009{
Michael Chan43514772009-06-08 18:14:41 -07002010 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -07002011}
2012
Mike Christie7b7232f2006-02-01 21:06:49 -06002013int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -07002014 char *data, uint32_t data_size)
2015{
2016 struct nlmsghdr *nlh;
2017 struct sk_buff *skb;
2018 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07002019 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05002020 struct iscsi_internal *priv;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002021 int len = nlmsg_total_size(sizeof(*ev) + sizeof(struct iscsi_hdr) +
2022 data_size);
Alex Aizman0896b752005-08-04 19:33:07 -07002023
Mike Christie790f39a2006-05-18 20:31:39 -05002024 priv = iscsi_if_transport_lookup(conn->transport);
2025 if (!priv)
2026 return -EINVAL;
2027
Mike Christie43a145a2006-10-16 18:09:38 -04002028 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002029 if (!skb) {
Mike Christiee5bd7b52008-09-24 11:46:10 -05002030 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06002031 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
2032 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07002033 return -ENOMEM;
2034 }
2035
Michael Chan43514772009-06-08 18:14:41 -07002036 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002037 ev = nlmsg_data(nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07002038 memset(ev, 0, sizeof(*ev));
2039 ev->transport_handle = iscsi_handle(conn->transport);
2040 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05002041 ev->r.recv_req.cid = conn->cid;
2042 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07002043 pdu = (char*)ev + sizeof(*ev);
2044 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
2045 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
2046
Michael Chan43514772009-06-08 18:14:41 -07002047 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002048}
2049EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
2050
Michael Chan43514772009-06-08 18:14:41 -07002051int iscsi_offload_mesg(struct Scsi_Host *shost,
2052 struct iscsi_transport *transport, uint32_t type,
2053 char *data, uint16_t data_size)
2054{
2055 struct nlmsghdr *nlh;
2056 struct sk_buff *skb;
2057 struct iscsi_uevent *ev;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002058 int len = nlmsg_total_size(sizeof(*ev) + data_size);
Michael Chan43514772009-06-08 18:14:41 -07002059
Michael Chana541f842009-07-29 08:49:52 +00002060 skb = alloc_skb(len, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07002061 if (!skb) {
2062 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
2063 return -ENOMEM;
2064 }
2065
2066 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002067 ev = nlmsg_data(nlh);
Michael Chan43514772009-06-08 18:14:41 -07002068 memset(ev, 0, sizeof(*ev));
2069 ev->type = type;
2070 ev->transport_handle = iscsi_handle(transport);
2071 switch (type) {
2072 case ISCSI_KEVENT_PATH_REQ:
2073 ev->r.req_path.host_no = shost->host_no;
2074 break;
2075 case ISCSI_KEVENT_IF_DOWN:
2076 ev->r.notify_if_down.host_no = shost->host_no;
2077 break;
2078 }
2079
2080 memcpy((char *)ev + sizeof(*ev), data, data_size);
2081
Michael Chana541f842009-07-29 08:49:52 +00002082 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07002083}
2084EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
2085
Mike Christiee5bd7b52008-09-24 11:46:10 -05002086void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07002087{
2088 struct nlmsghdr *nlh;
2089 struct sk_buff *skb;
2090 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05002091 struct iscsi_internal *priv;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002092 int len = nlmsg_total_size(sizeof(*ev));
Alex Aizman0896b752005-08-04 19:33:07 -07002093
Mike Christie790f39a2006-05-18 20:31:39 -05002094 priv = iscsi_if_transport_lookup(conn->transport);
2095 if (!priv)
2096 return;
2097
Mike Christie43a145a2006-10-16 18:09:38 -04002098 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002099 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06002100 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2101 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07002102 return;
2103 }
2104
Michael Chan43514772009-06-08 18:14:41 -07002105 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002106 ev = nlmsg_data(nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07002107 ev->transport_handle = iscsi_handle(conn->transport);
2108 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07002109 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05002110 ev->r.connerror.cid = conn->cid;
2111 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07002112
Michael Chan43514772009-06-08 18:14:41 -07002113 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002114
Mike Christie322d7392008-01-31 13:36:52 -06002115 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
2116 error);
Alex Aizman0896b752005-08-04 19:33:07 -07002117}
Mike Christiee5bd7b52008-09-24 11:46:10 -05002118EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07002119
Manish Rangankar17fa5752011-07-25 13:48:52 -05002120void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
2121 enum iscsi_conn_state state)
2122{
2123 struct nlmsghdr *nlh;
2124 struct sk_buff *skb;
2125 struct iscsi_uevent *ev;
2126 struct iscsi_internal *priv;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002127 int len = nlmsg_total_size(sizeof(*ev));
Manish Rangankar17fa5752011-07-25 13:48:52 -05002128
2129 priv = iscsi_if_transport_lookup(conn->transport);
2130 if (!priv)
2131 return;
2132
2133 skb = alloc_skb(len, GFP_ATOMIC);
2134 if (!skb) {
2135 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2136 "conn login (%d)\n", state);
2137 return;
2138 }
2139
2140 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002141 ev = nlmsg_data(nlh);
Manish Rangankar17fa5752011-07-25 13:48:52 -05002142 ev->transport_handle = iscsi_handle(conn->transport);
2143 ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
2144 ev->r.conn_login.state = state;
2145 ev->r.conn_login.cid = conn->cid;
2146 ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
2147 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2148
2149 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
2150 state);
2151}
2152EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
2153
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302154void iscsi_post_host_event(uint32_t host_no, struct iscsi_transport *transport,
2155 enum iscsi_host_event_code code, uint32_t data_size,
2156 uint8_t *data)
2157{
2158 struct nlmsghdr *nlh;
2159 struct sk_buff *skb;
2160 struct iscsi_uevent *ev;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002161 int len = nlmsg_total_size(sizeof(*ev) + data_size);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302162
Mike Christie49d0e642012-03-06 16:09:01 -06002163 skb = alloc_skb(len, GFP_NOIO);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302164 if (!skb) {
2165 printk(KERN_ERR "gracefully ignored host event (%d):%d OOM\n",
2166 host_no, code);
2167 return;
2168 }
2169
2170 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002171 ev = nlmsg_data(nlh);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302172 ev->transport_handle = iscsi_handle(transport);
2173 ev->type = ISCSI_KEVENT_HOST_EVENT;
2174 ev->r.host_event.host_no = host_no;
2175 ev->r.host_event.code = code;
2176 ev->r.host_event.data_size = data_size;
2177
2178 if (data_size)
2179 memcpy((char *)ev + sizeof(*ev), data, data_size);
2180
Mike Christie49d0e642012-03-06 16:09:01 -06002181 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302182}
2183EXPORT_SYMBOL_GPL(iscsi_post_host_event);
2184
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302185void iscsi_ping_comp_event(uint32_t host_no, struct iscsi_transport *transport,
2186 uint32_t status, uint32_t pid, uint32_t data_size,
2187 uint8_t *data)
2188{
2189 struct nlmsghdr *nlh;
2190 struct sk_buff *skb;
2191 struct iscsi_uevent *ev;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002192 int len = nlmsg_total_size(sizeof(*ev) + data_size);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302193
Mike Christie49d0e642012-03-06 16:09:01 -06002194 skb = alloc_skb(len, GFP_NOIO);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302195 if (!skb) {
2196 printk(KERN_ERR "gracefully ignored ping comp: OOM\n");
2197 return;
2198 }
2199
2200 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002201 ev = nlmsg_data(nlh);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302202 ev->transport_handle = iscsi_handle(transport);
2203 ev->type = ISCSI_KEVENT_PING_COMP;
2204 ev->r.ping_comp.host_no = host_no;
2205 ev->r.ping_comp.status = status;
2206 ev->r.ping_comp.pid = pid;
2207 ev->r.ping_comp.data_size = data_size;
2208 memcpy((char *)ev + sizeof(*ev), data, data_size);
2209
Mike Christie49d0e642012-03-06 16:09:01 -06002210 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302211}
2212EXPORT_SYMBOL_GPL(iscsi_ping_comp_event);
2213
Alex Aizman0896b752005-08-04 19:33:07 -07002214static int
Michael Chan43514772009-06-08 18:14:41 -07002215iscsi_if_send_reply(uint32_t group, int seq, int type, int done, int multi,
2216 void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07002217{
2218 struct sk_buff *skb;
2219 struct nlmsghdr *nlh;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002220 int len = nlmsg_total_size(size);
Alex Aizman0896b752005-08-04 19:33:07 -07002221 int flags = multi ? NLM_F_MULTI : 0;
2222 int t = done ? NLMSG_DONE : type;
2223
Mike Christie43a145a2006-10-16 18:09:38 -04002224 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05002225 if (!skb) {
2226 printk(KERN_ERR "Could not allocate skb to send reply.\n");
2227 return -ENOMEM;
2228 }
Alex Aizman0896b752005-08-04 19:33:07 -07002229
Michael Chan43514772009-06-08 18:14:41 -07002230 nlh = __nlmsg_put(skb, 0, 0, t, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -07002231 nlh->nlmsg_flags = flags;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002232 memcpy(nlmsg_data(nlh), payload, size);
Michael Chan43514772009-06-08 18:14:41 -07002233 return iscsi_multicast_skb(skb, group, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002234}
2235
2236static int
Mike Christie5b940ad2006-02-01 21:06:56 -06002237iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07002238{
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002239 struct iscsi_uevent *ev = nlmsg_data(nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07002240 struct iscsi_stats *stats;
2241 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06002242 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07002243 struct nlmsghdr *nlhstat;
2244 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05002245 struct iscsi_internal *priv;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002246 int len = nlmsg_total_size(sizeof(*ev) +
2247 sizeof(struct iscsi_stats) +
2248 sizeof(struct iscsi_stats_custom) *
2249 ISCSI_STATS_CUSTOM_MAX);
Alex Aizman0896b752005-08-04 19:33:07 -07002250 int err = 0;
2251
Mike Christie790f39a2006-05-18 20:31:39 -05002252 priv = iscsi_if_transport_lookup(transport);
2253 if (!priv)
2254 return -EINVAL;
2255
Mike Christieb5c7a122006-04-06 21:13:33 -05002256 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07002257 if (!conn)
2258 return -EEXIST;
2259
2260 do {
2261 int actual_size;
2262
Mike Christie43a145a2006-10-16 18:09:38 -04002263 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002264 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06002265 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
2266 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07002267 return -ENOMEM;
2268 }
2269
Michael Chan43514772009-06-08 18:14:41 -07002270 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07002271 (len - sizeof(*nlhstat)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002272 evstat = nlmsg_data(nlhstat);
Alex Aizman0896b752005-08-04 19:33:07 -07002273 memset(evstat, 0, sizeof(*evstat));
2274 evstat->transport_handle = iscsi_handle(conn->transport);
2275 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05002276 evstat->u.get_stats.cid =
2277 ev->u.get_stats.cid;
2278 evstat->u.get_stats.sid =
2279 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07002280 stats = (struct iscsi_stats *)
2281 ((char*)evstat + sizeof(*evstat));
2282 memset(stats, 0, sizeof(*stats));
2283
Mike Christie7b7232f2006-02-01 21:06:49 -06002284 transport->get_stats(conn, stats);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002285 actual_size = nlmsg_total_size(sizeof(struct iscsi_uevent) +
2286 sizeof(struct iscsi_stats) +
2287 sizeof(struct iscsi_stats_custom) *
2288 stats->custom_length);
Alex Aizman0896b752005-08-04 19:33:07 -07002289 actual_size -= sizeof(*nlhstat);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002290 actual_size = nlmsg_msg_size(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06002291 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07002292 nlhstat->nlmsg_len = actual_size;
2293
Michael Chan43514772009-06-08 18:14:41 -07002294 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
2295 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002296 } while (err < 0 && err != -ECONNREFUSED);
2297
2298 return err;
2299}
2300
Mike Christie53cb8a12006-06-28 12:00:32 -05002301/**
Mike Christie26974782007-12-13 12:43:29 -06002302 * iscsi_session_event - send session destr. completion event
2303 * @session: iscsi class session
2304 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05002305 */
Mike Christie26974782007-12-13 12:43:29 -06002306int iscsi_session_event(struct iscsi_cls_session *session,
2307 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05002308{
2309 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05002310 struct Scsi_Host *shost;
2311 struct iscsi_uevent *ev;
2312 struct sk_buff *skb;
2313 struct nlmsghdr *nlh;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002314 int rc, len = nlmsg_total_size(sizeof(*ev));
Mike Christie53cb8a12006-06-28 12:00:32 -05002315
Mike Christie26974782007-12-13 12:43:29 -06002316 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05002317 if (!priv)
2318 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05002319 shost = iscsi_session_to_shost(session);
2320
Mike Christie43a145a2006-10-16 18:09:38 -04002321 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05002322 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06002323 iscsi_cls_session_printk(KERN_ERR, session,
2324 "Cannot notify userspace of session "
2325 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05002326 return -ENOMEM;
2327 }
2328
Michael Chan43514772009-06-08 18:14:41 -07002329 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002330 ev = nlmsg_data(nlh);
Mike Christie26974782007-12-13 12:43:29 -06002331 ev->transport_handle = iscsi_handle(session->transport);
2332
2333 ev->type = event;
2334 switch (event) {
2335 case ISCSI_KEVENT_DESTROY_SESSION:
2336 ev->r.d_session.host_no = shost->host_no;
2337 ev->r.d_session.sid = session->sid;
2338 break;
2339 case ISCSI_KEVENT_CREATE_SESSION:
2340 ev->r.c_session_ret.host_no = shost->host_no;
2341 ev->r.c_session_ret.sid = session->sid;
2342 break;
2343 case ISCSI_KEVENT_UNBIND_SESSION:
2344 ev->r.unbind_session.host_no = shost->host_no;
2345 ev->r.unbind_session.sid = session->sid;
2346 break;
2347 default:
Mike Christie322d7392008-01-31 13:36:52 -06002348 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
2349 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06002350 kfree_skb(skb);
2351 return -EINVAL;
2352 }
Mike Christie53cb8a12006-06-28 12:00:32 -05002353
2354 /*
2355 * this will occur if the daemon is not up, so we just warn
2356 * the user and when the daemon is restarted it will handle it
2357 */
Michael Chan43514772009-06-08 18:14:41 -07002358 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08002359 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06002360 iscsi_cls_session_printk(KERN_ERR, session,
2361 "Cannot notify userspace of session "
2362 "event %u. Check iscsi daemon\n",
2363 event);
Mike Christie632248a2009-08-20 15:11:01 -05002364
2365 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
2366 event, rc);
Mike Christie53cb8a12006-06-28 12:00:32 -05002367 return rc;
2368}
Mike Christie26974782007-12-13 12:43:29 -06002369EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05002370
Alex Aizman0896b752005-08-04 19:33:07 -07002371static int
Mike Christied82ff9be2008-05-21 15:54:13 -05002372iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
Mike Christie0c70d842011-12-05 16:44:01 -06002373 struct iscsi_uevent *ev, pid_t pid,
2374 uint32_t initial_cmdsn, uint16_t cmds_max,
2375 uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06002376{
2377 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06002378 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06002379 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06002380
Mike Christied82ff9be2008-05-21 15:54:13 -05002381 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06002382 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06002383 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06002384 return -ENOMEM;
2385
Mike Christie0c70d842011-12-05 16:44:01 -06002386 session->creator = pid;
Mike Christie5e7facb2009-03-05 14:46:06 -06002387 shost = iscsi_session_to_shost(session);
2388 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05002389 ev->r.c_session_ret.sid = session->sid;
Mike Christie632248a2009-08-20 15:11:01 -05002390 ISCSI_DBG_TRANS_SESSION(session,
2391 "Completed creating transport session\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002392 return 0;
2393}
2394
2395static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002396iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06002397{
Mike Christie7b8631b2006-01-13 18:05:50 -06002398 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06002399 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06002400
Mike Christieb5c7a122006-04-06 21:13:33 -05002401 session = iscsi_session_lookup(ev->u.c_conn.sid);
2402 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002403 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05002404 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06002405 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05002406 }
Mike Christie7b8631b2006-01-13 18:05:50 -06002407
Mike Christie7b7232f2006-02-01 21:06:49 -06002408 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05002409 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06002410 iscsi_cls_session_printk(KERN_ERR, session,
2411 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06002412 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05002413 }
Mike Christie7b8631b2006-01-13 18:05:50 -06002414
Mike Christieb5c7a122006-04-06 21:13:33 -05002415 ev->r.c_conn_ret.sid = session->sid;
2416 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie632248a2009-08-20 15:11:01 -05002417
2418 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002419 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06002420}
2421
2422static int
2423iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2424{
Mike Christie7b8631b2006-01-13 18:05:50 -06002425 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06002426
Mike Christieb5c7a122006-04-06 21:13:33 -05002427 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06002428 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06002429 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06002430
Mike Christie632248a2009-08-20 15:11:01 -05002431 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002432 if (transport->destroy_conn)
2433 transport->destroy_conn(conn);
Mike Christie632248a2009-08-20 15:11:01 -05002434
Mike Christie7b8631b2006-01-13 18:05:50 -06002435 return 0;
2436}
2437
Mike Christiefd7255f2006-04-06 21:13:36 -05002438static int
2439iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2440{
2441 char *data = (char*)ev + sizeof(*ev);
2442 struct iscsi_cls_conn *conn;
2443 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05002444 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05002445
2446 session = iscsi_session_lookup(ev->u.set_param.sid);
2447 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
2448 if (!conn || !session)
2449 return -EINVAL;
2450
2451 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05002452 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05002453 sscanf(data, "%d", &value);
Mike Christiefdd46dc2009-11-11 16:34:34 -06002454 session->recovery_tmo = value;
Mike Christie30a6c652006-04-06 21:13:39 -05002455 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002456 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05002457 err = transport->set_param(conn, ev->u.set_param.param,
2458 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05002459 }
2460
2461 return err;
2462}
2463
Mike Christie10eb0f02009-05-13 17:57:38 -05002464static int iscsi_if_ep_connect(struct iscsi_transport *transport,
2465 struct iscsi_uevent *ev, int msg_type)
2466{
2467 struct iscsi_endpoint *ep;
2468 struct sockaddr *dst_addr;
2469 struct Scsi_Host *shost = NULL;
2470 int non_blocking, err = 0;
2471
2472 if (!transport->ep_connect)
2473 return -EINVAL;
2474
2475 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
2476 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
2477 if (!shost) {
2478 printk(KERN_ERR "ep connect failed. Could not find "
2479 "host no %u\n",
2480 ev->u.ep_connect_through_host.host_no);
2481 return -ENODEV;
2482 }
2483 non_blocking = ev->u.ep_connect_through_host.non_blocking;
2484 } else
2485 non_blocking = ev->u.ep_connect.non_blocking;
2486
2487 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
2488 ep = transport->ep_connect(shost, dst_addr, non_blocking);
2489 if (IS_ERR(ep)) {
2490 err = PTR_ERR(ep);
2491 goto release_host;
2492 }
2493
2494 ev->r.ep_connect_ret.handle = ep->id;
2495release_host:
2496 if (shost)
2497 scsi_host_put(shost);
2498 return err;
2499}
2500
Mike Christie22a39fb2011-02-16 15:04:33 -06002501static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
2502 u64 ep_handle)
2503{
2504 struct iscsi_cls_conn *conn;
2505 struct iscsi_endpoint *ep;
2506
2507 if (!transport->ep_disconnect)
2508 return -EINVAL;
2509
2510 ep = iscsi_lookup_endpoint(ep_handle);
2511 if (!ep)
2512 return -EINVAL;
2513 conn = ep->conn;
2514 if (conn) {
2515 mutex_lock(&conn->ep_mutex);
2516 conn->ep = NULL;
2517 mutex_unlock(&conn->ep_mutex);
2518 }
2519
2520 transport->ep_disconnect(ep);
2521 return 0;
2522}
2523
Mike Christie7b8631b2006-01-13 18:05:50 -06002524static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05002525iscsi_if_transport_ep(struct iscsi_transport *transport,
2526 struct iscsi_uevent *ev, int msg_type)
2527{
Mike Christied82ff9be2008-05-21 15:54:13 -05002528 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05002529 int rc = 0;
2530
2531 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05002532 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05002533 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05002534 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05002535 break;
2536 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
2537 if (!transport->ep_poll)
2538 return -EINVAL;
2539
Mike Christied82ff9be2008-05-21 15:54:13 -05002540 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
2541 if (!ep)
2542 return -EINVAL;
2543
2544 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05002545 ev->u.ep_poll.timeout_ms);
2546 break;
2547 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie22a39fb2011-02-16 15:04:33 -06002548 rc = iscsi_if_ep_disconnect(transport,
2549 ev->u.ep_disconnect.ep_handle);
Or Gerlitz264faaa2006-05-02 19:46:36 -05002550 break;
2551 }
2552 return rc;
2553}
2554
2555static int
Mike Christie01cb2252006-06-28 12:00:22 -05002556iscsi_tgt_dscvr(struct iscsi_transport *transport,
2557 struct iscsi_uevent *ev)
2558{
Mike Christie2174a042007-05-30 12:57:10 -05002559 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05002560 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05002561 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05002562
2563 if (!transport->tgt_dscvr)
2564 return -EINVAL;
2565
Mike Christie2174a042007-05-30 12:57:10 -05002566 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04002567 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05002568 printk(KERN_ERR "target discovery could not find host no %u\n",
2569 ev->u.tgt_dscvr.host_no);
2570 return -ENODEV;
2571 }
2572
2573
Mike Christie01cb2252006-06-28 12:00:22 -05002574 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05002575 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
2576 ev->u.tgt_dscvr.enable, dst_addr);
2577 scsi_host_put(shost);
2578 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05002579}
2580
2581static int
Mike Christie1d9bf132007-05-30 12:57:11 -05002582iscsi_set_host_param(struct iscsi_transport *transport,
2583 struct iscsi_uevent *ev)
2584{
2585 char *data = (char*)ev + sizeof(*ev);
2586 struct Scsi_Host *shost;
2587 int err;
2588
2589 if (!transport->set_host_param)
2590 return -ENOSYS;
2591
2592 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04002593 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05002594 printk(KERN_ERR "set_host_param could not find host no %u\n",
2595 ev->u.set_host_param.host_no);
2596 return -ENODEV;
2597 }
2598
2599 err = transport->set_host_param(shost, ev->u.set_host_param.param,
2600 data, ev->u.set_host_param.len);
2601 scsi_host_put(shost);
2602 return err;
2603}
2604
2605static int
Michael Chan43514772009-06-08 18:14:41 -07002606iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2607{
2608 struct Scsi_Host *shost;
2609 struct iscsi_path *params;
2610 int err;
2611
2612 if (!transport->set_path)
2613 return -ENOSYS;
2614
2615 shost = scsi_host_lookup(ev->u.set_path.host_no);
2616 if (!shost) {
2617 printk(KERN_ERR "set path could not find host no %u\n",
2618 ev->u.set_path.host_no);
2619 return -ENODEV;
2620 }
2621
2622 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
2623 err = transport->set_path(shost, params);
2624
2625 scsi_host_put(shost);
2626 return err;
2627}
2628
2629static int
Mike Christie56c155b2011-07-25 13:48:37 -05002630iscsi_set_iface_params(struct iscsi_transport *transport,
Mike Christie00c31882011-10-06 03:56:59 -05002631 struct iscsi_uevent *ev, uint32_t len)
Mike Christie56c155b2011-07-25 13:48:37 -05002632{
2633 char *data = (char *)ev + sizeof(*ev);
2634 struct Scsi_Host *shost;
2635 int err;
2636
2637 if (!transport->set_iface_param)
2638 return -ENOSYS;
2639
2640 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
2641 if (!shost) {
2642 printk(KERN_ERR "set_iface_params could not find host no %u\n",
2643 ev->u.set_iface_params.host_no);
2644 return -ENODEV;
2645 }
2646
Mike Christie00c31882011-10-06 03:56:59 -05002647 err = transport->set_iface_param(shost, data, len);
Mike Christie56c155b2011-07-25 13:48:37 -05002648 scsi_host_put(shost);
2649 return err;
2650}
2651
2652static int
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302653iscsi_send_ping(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2654{
2655 struct Scsi_Host *shost;
2656 struct sockaddr *dst_addr;
2657 int err;
2658
2659 if (!transport->send_ping)
2660 return -ENOSYS;
2661
2662 shost = scsi_host_lookup(ev->u.iscsi_ping.host_no);
2663 if (!shost) {
2664 printk(KERN_ERR "iscsi_ping could not find host no %u\n",
2665 ev->u.iscsi_ping.host_no);
2666 return -ENODEV;
2667 }
2668
2669 dst_addr = (struct sockaddr *)((char *)ev + sizeof(*ev));
2670 err = transport->send_ping(shost, ev->u.iscsi_ping.iface_num,
2671 ev->u.iscsi_ping.iface_type,
2672 ev->u.iscsi_ping.payload_size,
2673 ev->u.iscsi_ping.pid,
2674 dst_addr);
2675 scsi_host_put(shost);
2676 return err;
2677}
2678
2679static int
Nilesh Javali6260a5d2012-02-27 03:08:51 -08002680iscsi_get_chap(struct iscsi_transport *transport, struct nlmsghdr *nlh)
2681{
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002682 struct iscsi_uevent *ev = nlmsg_data(nlh);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08002683 struct Scsi_Host *shost = NULL;
2684 struct iscsi_chap_rec *chap_rec;
2685 struct iscsi_internal *priv;
2686 struct sk_buff *skbchap;
2687 struct nlmsghdr *nlhchap;
2688 struct iscsi_uevent *evchap;
2689 uint32_t chap_buf_size;
2690 int len, err = 0;
2691 char *buf;
2692
2693 if (!transport->get_chap)
2694 return -EINVAL;
2695
2696 priv = iscsi_if_transport_lookup(transport);
2697 if (!priv)
2698 return -EINVAL;
2699
2700 chap_buf_size = (ev->u.get_chap.num_entries * sizeof(*chap_rec));
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002701 len = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08002702
2703 shost = scsi_host_lookup(ev->u.get_chap.host_no);
2704 if (!shost) {
2705 printk(KERN_ERR "%s: failed. Cound not find host no %u\n",
2706 __func__, ev->u.get_chap.host_no);
2707 return -ENODEV;
2708 }
2709
2710 do {
2711 int actual_size;
2712
2713 skbchap = alloc_skb(len, GFP_KERNEL);
2714 if (!skbchap) {
2715 printk(KERN_ERR "can not deliver chap: OOM\n");
2716 err = -ENOMEM;
2717 goto exit_get_chap;
2718 }
2719
2720 nlhchap = __nlmsg_put(skbchap, 0, 0, 0,
2721 (len - sizeof(*nlhchap)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002722 evchap = nlmsg_data(nlhchap);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08002723 memset(evchap, 0, sizeof(*evchap));
2724 evchap->transport_handle = iscsi_handle(transport);
2725 evchap->type = nlh->nlmsg_type;
2726 evchap->u.get_chap.host_no = ev->u.get_chap.host_no;
2727 evchap->u.get_chap.chap_tbl_idx = ev->u.get_chap.chap_tbl_idx;
2728 evchap->u.get_chap.num_entries = ev->u.get_chap.num_entries;
2729 buf = (char *) ((char *)evchap + sizeof(*evchap));
2730 memset(buf, 0, chap_buf_size);
2731
2732 err = transport->get_chap(shost, ev->u.get_chap.chap_tbl_idx,
2733 &evchap->u.get_chap.num_entries, buf);
2734
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002735 actual_size = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08002736 skb_trim(skbchap, NLMSG_ALIGN(actual_size));
2737 nlhchap->nlmsg_len = actual_size;
2738
2739 err = iscsi_multicast_skb(skbchap, ISCSI_NL_GRP_ISCSID,
2740 GFP_KERNEL);
2741 } while (err < 0 && err != -ECONNREFUSED);
2742
2743exit_get_chap:
2744 scsi_host_put(shost);
2745 return err;
2746}
2747
2748static int iscsi_delete_chap(struct iscsi_transport *transport,
2749 struct iscsi_uevent *ev)
2750{
2751 struct Scsi_Host *shost;
2752 int err = 0;
2753
2754 if (!transport->delete_chap)
2755 return -ENOSYS;
2756
2757 shost = scsi_host_lookup(ev->u.delete_chap.host_no);
2758 if (!shost) {
2759 printk(KERN_ERR "%s could not find host no %u\n",
2760 __func__, ev->u.delete_chap.host_no);
2761 return -ENODEV;
2762 }
2763
2764 err = transport->delete_chap(shost, ev->u.delete_chap.chap_tbl_idx);
2765 scsi_host_put(shost);
2766 return err;
2767}
2768
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002769static const struct {
2770 enum iscsi_discovery_parent_type value;
2771 char *name;
2772} iscsi_discovery_parent_names[] = {
2773 {ISCSI_DISC_PARENT_UNKNOWN, "Unknown" },
2774 {ISCSI_DISC_PARENT_SENDTGT, "Sendtarget" },
2775 {ISCSI_DISC_PARENT_ISNS, "isns" },
2776};
2777
2778char *iscsi_get_discovery_parent_name(int parent_type)
2779{
2780 int i;
2781 char *state = "Unknown!";
2782
2783 for (i = 0; i < ARRAY_SIZE(iscsi_discovery_parent_names); i++) {
2784 if (iscsi_discovery_parent_names[i].value & parent_type) {
2785 state = iscsi_discovery_parent_names[i].name;
2786 break;
2787 }
2788 }
2789 return state;
2790}
2791EXPORT_SYMBOL_GPL(iscsi_get_discovery_parent_name);
2792
2793static int iscsi_set_flashnode_param(struct iscsi_transport *transport,
2794 struct iscsi_uevent *ev, uint32_t len)
2795{
2796 char *data = (char *)ev + sizeof(*ev);
2797 struct Scsi_Host *shost;
2798 struct iscsi_bus_flash_session *fnode_sess;
2799 struct iscsi_bus_flash_conn *fnode_conn;
2800 struct device *dev;
Mike Christie8526cb12013-05-06 12:06:56 -05002801 uint32_t idx;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002802 int err = 0;
2803
2804 if (!transport->set_flashnode_param) {
2805 err = -ENOSYS;
2806 goto exit_set_fnode;
2807 }
2808
2809 shost = scsi_host_lookup(ev->u.set_flashnode.host_no);
2810 if (!shost) {
2811 pr_err("%s could not find host no %u\n",
2812 __func__, ev->u.set_flashnode.host_no);
2813 err = -ENODEV;
2814 goto put_host;
2815 }
2816
Mike Christie8526cb12013-05-06 12:06:56 -05002817 idx = ev->u.set_flashnode.flashnode_idx;
2818 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002819 if (!fnode_sess) {
2820 pr_err("%s could not find flashnode %u for host no %u\n",
Mike Christie8526cb12013-05-06 12:06:56 -05002821 __func__, idx, ev->u.set_flashnode.host_no);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002822 err = -ENODEV;
2823 goto put_host;
2824 }
2825
Mike Christie8526cb12013-05-06 12:06:56 -05002826 dev = iscsi_find_flashnode_conn(fnode_sess);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002827 if (!dev) {
2828 err = -ENODEV;
Mike Christie8526cb12013-05-06 12:06:56 -05002829 goto put_sess;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002830 }
2831
2832 fnode_conn = iscsi_dev_to_flash_conn(dev);
2833 err = transport->set_flashnode_param(fnode_sess, fnode_conn, data, len);
Mike Christie8526cb12013-05-06 12:06:56 -05002834 put_device(dev);
2835
2836put_sess:
2837 put_device(&fnode_sess->dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002838
2839put_host:
2840 scsi_host_put(shost);
2841
2842exit_set_fnode:
2843 return err;
2844}
2845
2846static int iscsi_new_flashnode(struct iscsi_transport *transport,
2847 struct iscsi_uevent *ev, uint32_t len)
2848{
2849 char *data = (char *)ev + sizeof(*ev);
2850 struct Scsi_Host *shost;
2851 int index;
2852 int err = 0;
2853
2854 if (!transport->new_flashnode) {
2855 err = -ENOSYS;
2856 goto exit_new_fnode;
2857 }
2858
2859 shost = scsi_host_lookup(ev->u.new_flashnode.host_no);
2860 if (!shost) {
2861 pr_err("%s could not find host no %u\n",
2862 __func__, ev->u.new_flashnode.host_no);
2863 err = -ENODEV;
2864 goto put_host;
2865 }
2866
2867 index = transport->new_flashnode(shost, data, len);
2868
2869 if (index >= 0)
2870 ev->r.new_flashnode_ret.flashnode_idx = index;
2871 else
2872 err = -EIO;
2873
2874put_host:
2875 scsi_host_put(shost);
2876
2877exit_new_fnode:
2878 return err;
2879}
2880
2881static int iscsi_del_flashnode(struct iscsi_transport *transport,
2882 struct iscsi_uevent *ev)
2883{
2884 struct Scsi_Host *shost;
2885 struct iscsi_bus_flash_session *fnode_sess;
Mike Christie8526cb12013-05-06 12:06:56 -05002886 uint32_t idx;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002887 int err = 0;
2888
2889 if (!transport->del_flashnode) {
2890 err = -ENOSYS;
2891 goto exit_del_fnode;
2892 }
2893
2894 shost = scsi_host_lookup(ev->u.del_flashnode.host_no);
2895 if (!shost) {
2896 pr_err("%s could not find host no %u\n",
2897 __func__, ev->u.del_flashnode.host_no);
2898 err = -ENODEV;
2899 goto put_host;
2900 }
2901
Mike Christie8526cb12013-05-06 12:06:56 -05002902 idx = ev->u.del_flashnode.flashnode_idx;
2903 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002904 if (!fnode_sess) {
2905 pr_err("%s could not find flashnode %u for host no %u\n",
Mike Christie8526cb12013-05-06 12:06:56 -05002906 __func__, idx, ev->u.del_flashnode.host_no);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002907 err = -ENODEV;
2908 goto put_host;
2909 }
2910
2911 err = transport->del_flashnode(fnode_sess);
Mike Christie8526cb12013-05-06 12:06:56 -05002912 put_device(&fnode_sess->dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002913
2914put_host:
2915 scsi_host_put(shost);
2916
2917exit_del_fnode:
2918 return err;
2919}
2920
2921static int iscsi_login_flashnode(struct iscsi_transport *transport,
2922 struct iscsi_uevent *ev)
2923{
2924 struct Scsi_Host *shost;
2925 struct iscsi_bus_flash_session *fnode_sess;
2926 struct iscsi_bus_flash_conn *fnode_conn;
2927 struct device *dev;
Mike Christie8526cb12013-05-06 12:06:56 -05002928 uint32_t idx;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002929 int err = 0;
2930
2931 if (!transport->login_flashnode) {
2932 err = -ENOSYS;
2933 goto exit_login_fnode;
2934 }
2935
2936 shost = scsi_host_lookup(ev->u.login_flashnode.host_no);
2937 if (!shost) {
2938 pr_err("%s could not find host no %u\n",
2939 __func__, ev->u.login_flashnode.host_no);
2940 err = -ENODEV;
2941 goto put_host;
2942 }
2943
Mike Christie8526cb12013-05-06 12:06:56 -05002944 idx = ev->u.login_flashnode.flashnode_idx;
2945 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002946 if (!fnode_sess) {
2947 pr_err("%s could not find flashnode %u for host no %u\n",
Mike Christie8526cb12013-05-06 12:06:56 -05002948 __func__, idx, ev->u.login_flashnode.host_no);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002949 err = -ENODEV;
2950 goto put_host;
2951 }
2952
Mike Christie8526cb12013-05-06 12:06:56 -05002953 dev = iscsi_find_flashnode_conn(fnode_sess);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002954 if (!dev) {
2955 err = -ENODEV;
Mike Christie8526cb12013-05-06 12:06:56 -05002956 goto put_sess;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002957 }
2958
2959 fnode_conn = iscsi_dev_to_flash_conn(dev);
2960 err = transport->login_flashnode(fnode_sess, fnode_conn);
Mike Christie8526cb12013-05-06 12:06:56 -05002961 put_device(dev);
2962
2963put_sess:
2964 put_device(&fnode_sess->dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002965
2966put_host:
2967 scsi_host_put(shost);
2968
2969exit_login_fnode:
2970 return err;
2971}
2972
2973static int iscsi_logout_flashnode(struct iscsi_transport *transport,
2974 struct iscsi_uevent *ev)
2975{
2976 struct Scsi_Host *shost;
2977 struct iscsi_bus_flash_session *fnode_sess;
2978 struct iscsi_bus_flash_conn *fnode_conn;
2979 struct device *dev;
Mike Christie8526cb12013-05-06 12:06:56 -05002980 uint32_t idx;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002981 int err = 0;
2982
2983 if (!transport->logout_flashnode) {
2984 err = -ENOSYS;
2985 goto exit_logout_fnode;
2986 }
2987
2988 shost = scsi_host_lookup(ev->u.logout_flashnode.host_no);
2989 if (!shost) {
2990 pr_err("%s could not find host no %u\n",
2991 __func__, ev->u.logout_flashnode.host_no);
2992 err = -ENODEV;
2993 goto put_host;
2994 }
2995
Mike Christie8526cb12013-05-06 12:06:56 -05002996 idx = ev->u.logout_flashnode.flashnode_idx;
2997 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04002998 if (!fnode_sess) {
2999 pr_err("%s could not find flashnode %u for host no %u\n",
Mike Christie8526cb12013-05-06 12:06:56 -05003000 __func__, idx, ev->u.logout_flashnode.host_no);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003001 err = -ENODEV;
3002 goto put_host;
3003 }
3004
Mike Christie8526cb12013-05-06 12:06:56 -05003005 dev = iscsi_find_flashnode_conn(fnode_sess);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003006 if (!dev) {
3007 err = -ENODEV;
Mike Christie8526cb12013-05-06 12:06:56 -05003008 goto put_sess;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003009 }
3010
3011 fnode_conn = iscsi_dev_to_flash_conn(dev);
3012
3013 err = transport->logout_flashnode(fnode_sess, fnode_conn);
Mike Christie8526cb12013-05-06 12:06:56 -05003014 put_device(dev);
3015
3016put_sess:
3017 put_device(&fnode_sess->dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003018
3019put_host:
3020 scsi_host_put(shost);
3021
3022exit_logout_fnode:
3023 return err;
3024}
3025
3026static int iscsi_logout_flashnode_sid(struct iscsi_transport *transport,
3027 struct iscsi_uevent *ev)
3028{
3029 struct Scsi_Host *shost;
3030 struct iscsi_cls_session *session;
3031 int err = 0;
3032
3033 if (!transport->logout_flashnode_sid) {
3034 err = -ENOSYS;
3035 goto exit_logout_sid;
3036 }
3037
3038 shost = scsi_host_lookup(ev->u.logout_flashnode_sid.host_no);
3039 if (!shost) {
3040 pr_err("%s could not find host no %u\n",
3041 __func__, ev->u.logout_flashnode.host_no);
3042 err = -ENODEV;
3043 goto put_host;
3044 }
3045
3046 session = iscsi_session_lookup(ev->u.logout_flashnode_sid.sid);
3047 if (!session) {
3048 pr_err("%s could not find session id %u\n",
3049 __func__, ev->u.logout_flashnode_sid.sid);
3050 err = -EINVAL;
3051 goto put_host;
3052 }
3053
3054 err = transport->logout_flashnode_sid(session);
3055
3056put_host:
3057 scsi_host_put(shost);
3058
3059exit_logout_sid:
3060 return err;
3061}
3062
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003063static int
Michael Chan43514772009-06-08 18:14:41 -07003064iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07003065{
3066 int err = 0;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003067 struct iscsi_uevent *ev = nlmsg_data(nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07003068 struct iscsi_transport *transport = NULL;
3069 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06003070 struct iscsi_cls_session *session;
3071 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05003072 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07003073
Michael Chan43514772009-06-08 18:14:41 -07003074 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
3075 *group = ISCSI_NL_GRP_UIP;
3076 else
3077 *group = ISCSI_NL_GRP_ISCSID;
3078
Alex Aizman0896b752005-08-04 19:33:07 -07003079 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
3080 if (!priv)
3081 return -EINVAL;
3082 transport = priv->iscsi_transport;
3083
Mike Christie7b7232f2006-02-01 21:06:49 -06003084 if (!try_module_get(transport->owner))
3085 return -EINVAL;
3086
Alex Aizman0896b752005-08-04 19:33:07 -07003087 switch (nlh->nlmsg_type) {
3088 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05003089 err = iscsi_if_create_session(priv, ep, ev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003090 NETLINK_CB(skb).portid,
Mike Christie40753ca2008-05-21 15:53:56 -05003091 ev->u.c_session.initial_cmdsn,
3092 ev->u.c_session.cmds_max,
3093 ev->u.c_session.queue_depth);
3094 break;
3095 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05003096 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05003097 if (!ep) {
3098 err = -EINVAL;
3099 break;
3100 }
Mike Christied82ff9be2008-05-21 15:54:13 -05003101
3102 err = iscsi_if_create_session(priv, ep, ev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003103 NETLINK_CB(skb).portid,
Mike Christie40753ca2008-05-21 15:53:56 -05003104 ev->u.c_bound_session.initial_cmdsn,
3105 ev->u.c_bound_session.cmds_max,
3106 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07003107 break;
3108 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05003109 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06003110 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06003111 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06003112 else
3113 err = -EINVAL;
3114 break;
3115 case ISCSI_UEVENT_UNBIND_SESSION:
3116 session = iscsi_session_lookup(ev->u.d_session.sid);
3117 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06003118 scsi_queue_work(iscsi_session_to_shost(session),
3119 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06003120 else
Mike Christie7b7232f2006-02-01 21:06:49 -06003121 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07003122 break;
3123 case ISCSI_UEVENT_CREATE_CONN:
3124 err = iscsi_if_create_conn(transport, ev);
3125 break;
3126 case ISCSI_UEVENT_DESTROY_CONN:
3127 err = iscsi_if_destroy_conn(transport, ev);
3128 break;
3129 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05003130 session = iscsi_session_lookup(ev->u.b_conn.sid);
3131 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06003132
Mike Christie22a39fb2011-02-16 15:04:33 -06003133 if (conn && conn->ep)
3134 iscsi_if_ep_disconnect(transport, conn->ep->id);
3135
3136 if (!session || !conn) {
Mike Christie7b7232f2006-02-01 21:06:49 -06003137 err = -EINVAL;
Mike Christie22a39fb2011-02-16 15:04:33 -06003138 break;
3139 }
3140
3141 ev->r.retcode = transport->bind_conn(session, conn,
3142 ev->u.b_conn.transport_eph,
3143 ev->u.b_conn.is_leading);
3144 if (ev->r.retcode || !transport->ep_connect)
3145 break;
3146
3147 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
3148 if (ep) {
3149 ep->conn = conn;
3150
3151 mutex_lock(&conn->ep_mutex);
3152 conn->ep = ep;
3153 mutex_unlock(&conn->ep_mutex);
3154 } else
3155 iscsi_cls_conn_printk(KERN_ERR, conn,
3156 "Could not set ep conn "
3157 "binding\n");
Alex Aizman0896b752005-08-04 19:33:07 -07003158 break;
3159 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05003160 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07003161 break;
3162 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05003163 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06003164 if (conn)
3165 ev->r.retcode = transport->start_conn(conn);
3166 else
3167 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07003168 break;
3169 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05003170 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06003171 if (conn)
3172 transport->stop_conn(conn, ev->u.stop_conn.flag);
3173 else
3174 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07003175 break;
3176 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05003177 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06003178 if (conn)
3179 ev->r.retcode = transport->send_pdu(conn,
3180 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
3181 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
3182 ev->u.send_pdu.data_size);
3183 else
3184 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07003185 break;
3186 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06003187 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07003188 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05003189 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
3190 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
3191 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05003192 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05003193 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
3194 break;
Mike Christie01cb2252006-06-28 12:00:22 -05003195 case ISCSI_UEVENT_TGT_DSCVR:
3196 err = iscsi_tgt_dscvr(transport, ev);
3197 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05003198 case ISCSI_UEVENT_SET_HOST_PARAM:
3199 err = iscsi_set_host_param(transport, ev);
3200 break;
Michael Chan43514772009-06-08 18:14:41 -07003201 case ISCSI_UEVENT_PATH_UPDATE:
3202 err = iscsi_set_path(transport, ev);
3203 break;
Mike Christie56c155b2011-07-25 13:48:37 -05003204 case ISCSI_UEVENT_SET_IFACE_PARAMS:
Mike Christie00c31882011-10-06 03:56:59 -05003205 err = iscsi_set_iface_params(transport, ev,
3206 nlmsg_attrlen(nlh, sizeof(*ev)));
Mike Christie56c155b2011-07-25 13:48:37 -05003207 break;
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05303208 case ISCSI_UEVENT_PING:
3209 err = iscsi_send_ping(transport, ev);
3210 break;
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003211 case ISCSI_UEVENT_GET_CHAP:
3212 err = iscsi_get_chap(transport, nlh);
3213 break;
3214 case ISCSI_UEVENT_DELETE_CHAP:
3215 err = iscsi_delete_chap(transport, ev);
3216 break;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003217 case ISCSI_UEVENT_SET_FLASHNODE_PARAMS:
3218 err = iscsi_set_flashnode_param(transport, ev,
3219 nlmsg_attrlen(nlh,
3220 sizeof(*ev)));
3221 break;
3222 case ISCSI_UEVENT_NEW_FLASHNODE:
3223 err = iscsi_new_flashnode(transport, ev,
3224 nlmsg_attrlen(nlh, sizeof(*ev)));
3225 break;
3226 case ISCSI_UEVENT_DEL_FLASHNODE:
3227 err = iscsi_del_flashnode(transport, ev);
3228 break;
3229 case ISCSI_UEVENT_LOGIN_FLASHNODE:
3230 err = iscsi_login_flashnode(transport, ev);
3231 break;
3232 case ISCSI_UEVENT_LOGOUT_FLASHNODE:
3233 err = iscsi_logout_flashnode(transport, ev);
3234 break;
3235 case ISCSI_UEVENT_LOGOUT_FLASHNODE_SID:
3236 err = iscsi_logout_flashnode_sid(transport, ev);
3237 break;
Alex Aizman0896b752005-08-04 19:33:07 -07003238 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05003239 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07003240 break;
3241 }
3242
Mike Christie7b7232f2006-02-01 21:06:49 -06003243 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07003244 return err;
3245}
3246
Mike Christieb5c7a122006-04-06 21:13:33 -05003247/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003248 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
3249 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05003250 */
Alex Aizman0896b752005-08-04 19:33:07 -07003251static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003252iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07003253{
Arjan van de Ven0b950672006-01-11 13:16:10 +01003254 mutex_lock(&rx_queue_mutex);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003255 while (skb->len >= NLMSG_HDRLEN) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003256 int err;
3257 uint32_t rlen;
3258 struct nlmsghdr *nlh;
3259 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07003260 uint32_t group;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003261
3262 nlh = nlmsg_hdr(skb);
3263 if (nlh->nlmsg_len < sizeof(*nlh) ||
3264 skb->len < nlh->nlmsg_len) {
3265 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06003266 }
Mike Christieee7f8e42006-02-01 21:07:01 -06003267
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003268 ev = nlmsg_data(nlh);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003269 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
3270 if (rlen > skb->len)
3271 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07003272
Michael Chan43514772009-06-08 18:14:41 -07003273 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003274 if (err) {
3275 ev->type = ISCSI_KEVENT_IF_ERROR;
3276 ev->iferror = err;
3277 }
3278 do {
3279 /*
3280 * special case for GET_STATS:
3281 * on success - sending reply and stats from
3282 * inside of if_recv_msg(),
3283 * on error - fall through.
3284 */
3285 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07003286 break;
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003287 if (ev->type == ISCSI_UEVENT_GET_CHAP && !err)
3288 break;
Michael Chan43514772009-06-08 18:14:41 -07003289 err = iscsi_if_send_reply(group, nlh->nlmsg_seq,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003290 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
Sasha Levin46a7c172012-01-25 22:16:16 -05003291 } while (err < 0 && err != -ECONNREFUSED && err != -ESRCH);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003292 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07003293 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01003294 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07003295}
3296
Mike Christiefd7255f2006-04-06 21:13:36 -05003297#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01003298struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05003299 __ATTR(_name,_mode,_show,_store)
3300
Alex Aizman0896b752005-08-04 19:33:07 -07003301/*
3302 * iSCSI connection attrs
3303 */
Mike Christiea54a52c2006-06-28 12:00:23 -05003304#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01003306show_conn_param_##param(struct device *dev, \
3307 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05003308{ \
Tony Jonesee959b02008-02-22 00:13:36 +01003309 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05003310 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05003311 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05003312}
3313
Mike Christiea54a52c2006-06-28 12:00:23 -05003314#define iscsi_conn_attr(field, param) \
3315 iscsi_conn_attr_show(param) \
3316static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05003317 NULL);
3318
Mike Christiea54a52c2006-06-28 12:00:23 -05003319iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
3320iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
3321iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
3322iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
3323iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
3324iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
3325iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
Mike Christiea54a52c2006-06-28 12:00:23 -05003326iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
3327iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06003328iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
3329iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
Mike Christie289324b2011-02-16 15:04:37 -06003331#define iscsi_conn_ep_attr_show(param) \
3332static ssize_t show_conn_ep_param_##param(struct device *dev, \
3333 struct device_attribute *attr,\
3334 char *buf) \
3335{ \
3336 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
3337 struct iscsi_transport *t = conn->transport; \
3338 struct iscsi_endpoint *ep; \
3339 ssize_t rc; \
3340 \
3341 /* \
3342 * Need to make sure ep_disconnect does not free the LLD's \
3343 * interconnect resources while we are trying to read them. \
3344 */ \
3345 mutex_lock(&conn->ep_mutex); \
3346 ep = conn->ep; \
3347 if (!ep && t->ep_connect) { \
3348 mutex_unlock(&conn->ep_mutex); \
3349 return -ENOTCONN; \
3350 } \
3351 \
3352 if (ep) \
3353 rc = t->get_ep_param(ep, param, buf); \
3354 else \
3355 rc = t->get_conn_param(conn, param, buf); \
3356 mutex_unlock(&conn->ep_mutex); \
3357 return rc; \
3358}
3359
3360#define iscsi_conn_ep_attr(field, param) \
3361 iscsi_conn_ep_attr_show(param) \
3362static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
3363 show_conn_ep_param_##param, NULL);
3364
3365iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
3366iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
3367
Mike Christie3128c6c2011-07-25 13:48:42 -05003368static struct attribute *iscsi_conn_attrs[] = {
3369 &dev_attr_conn_max_recv_dlength.attr,
3370 &dev_attr_conn_max_xmit_dlength.attr,
3371 &dev_attr_conn_header_digest.attr,
3372 &dev_attr_conn_data_digest.attr,
3373 &dev_attr_conn_ifmarker.attr,
3374 &dev_attr_conn_ofmarker.attr,
3375 &dev_attr_conn_address.attr,
3376 &dev_attr_conn_port.attr,
3377 &dev_attr_conn_exp_statsn.attr,
3378 &dev_attr_conn_persistent_address.attr,
3379 &dev_attr_conn_persistent_port.attr,
3380 &dev_attr_conn_ping_tmo.attr,
3381 &dev_attr_conn_recv_tmo.attr,
3382 NULL,
3383};
3384
Al Viro587a1f12011-07-23 23:11:19 -04003385static umode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
Mike Christie3128c6c2011-07-25 13:48:42 -05003386 struct attribute *attr, int i)
3387{
3388 struct device *cdev = container_of(kobj, struct device, kobj);
3389 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
3390 struct iscsi_transport *t = conn->transport;
3391 int param;
3392
3393 if (attr == &dev_attr_conn_max_recv_dlength.attr)
3394 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
3395 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
3396 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
3397 else if (attr == &dev_attr_conn_header_digest.attr)
3398 param = ISCSI_PARAM_HDRDGST_EN;
3399 else if (attr == &dev_attr_conn_data_digest.attr)
3400 param = ISCSI_PARAM_DATADGST_EN;
3401 else if (attr == &dev_attr_conn_ifmarker.attr)
3402 param = ISCSI_PARAM_IFMARKER_EN;
3403 else if (attr == &dev_attr_conn_ofmarker.attr)
3404 param = ISCSI_PARAM_OFMARKER_EN;
3405 else if (attr == &dev_attr_conn_address.attr)
3406 param = ISCSI_PARAM_CONN_ADDRESS;
3407 else if (attr == &dev_attr_conn_port.attr)
3408 param = ISCSI_PARAM_CONN_PORT;
3409 else if (attr == &dev_attr_conn_exp_statsn.attr)
3410 param = ISCSI_PARAM_EXP_STATSN;
3411 else if (attr == &dev_attr_conn_persistent_address.attr)
3412 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
3413 else if (attr == &dev_attr_conn_persistent_port.attr)
3414 param = ISCSI_PARAM_PERSISTENT_PORT;
3415 else if (attr == &dev_attr_conn_ping_tmo.attr)
3416 param = ISCSI_PARAM_PING_TMO;
3417 else if (attr == &dev_attr_conn_recv_tmo.attr)
3418 param = ISCSI_PARAM_RECV_TMO;
3419 else {
3420 WARN_ONCE(1, "Invalid conn attr");
3421 return 0;
3422 }
3423
3424 return t->attr_is_visible(ISCSI_PARAM, param);
3425}
3426
3427static struct attribute_group iscsi_conn_group = {
3428 .attrs = iscsi_conn_attrs,
3429 .is_visible = iscsi_conn_attr_is_visible,
3430};
3431
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432/*
Alex Aizman0896b752005-08-04 19:33:07 -07003433 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 */
Mike Christieb2c64162007-05-30 12:57:16 -05003435#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01003437show_session_param_##param(struct device *dev, \
3438 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05003439{ \
Tony Jonesee959b02008-02-22 00:13:36 +01003440 struct iscsi_cls_session *session = \
3441 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05003442 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05003443 \
3444 if (perm && !capable(CAP_SYS_ADMIN)) \
3445 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05003446 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05003447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Mike Christieb2c64162007-05-30 12:57:16 -05003449#define iscsi_session_attr(field, param, perm) \
3450 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05003451static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05003452 NULL);
Mike Christieb2c64162007-05-30 12:57:16 -05003453iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
3454iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
3455iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
3456iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
3457iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
3458iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
3459iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
3460iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
3461iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
3462iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
3463iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
3464iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
3465iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
3466iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie30534952012-02-27 03:08:53 -08003467iscsi_session_attr(chap_out_idx, ISCSI_PARAM_CHAP_OUT_IDX, 1);
3468iscsi_session_attr(chap_in_idx, ISCSI_PARAM_CHAP_IN_IDX, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06003469iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
3470iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
3471iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie3fe5ae82009-11-11 16:34:33 -06003472iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05003473iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05303474iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
3475iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05003476
Mike Christie6eabafb2008-01-31 13:36:43 -06003477static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01003478show_priv_session_state(struct device *dev, struct device_attribute *attr,
3479 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06003480{
Tony Jonesee959b02008-02-22 00:13:36 +01003481 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06003482 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
3483}
3484static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
3485 NULL);
Mike Christie0c70d842011-12-05 16:44:01 -06003486static ssize_t
3487show_priv_session_creator(struct device *dev, struct device_attribute *attr,
3488 char *buf)
3489{
3490 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
3491 return sprintf(buf, "%d\n", session->creator);
3492}
3493static ISCSI_CLASS_ATTR(priv_sess, creator, S_IRUGO, show_priv_session_creator,
3494 NULL);
Manish Rangankar930a9f02012-11-23 06:58:41 -05003495static ssize_t
3496show_priv_session_target_id(struct device *dev, struct device_attribute *attr,
3497 char *buf)
3498{
3499 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
3500 return sprintf(buf, "%d\n", session->target_id);
3501}
3502static ISCSI_CLASS_ATTR(priv_sess, target_id, S_IRUGO,
3503 show_priv_session_target_id, NULL);
Mike Christie6eabafb2008-01-31 13:36:43 -06003504
Mike Christiefd7255f2006-04-06 21:13:36 -05003505#define iscsi_priv_session_attr_show(field, format) \
3506static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01003507show_priv_session_##field(struct device *dev, \
3508 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05003509{ \
Tony Jonesee959b02008-02-22 00:13:36 +01003510 struct iscsi_cls_session *session = \
3511 iscsi_dev_to_session(dev->parent); \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05303512 if (session->field == -1) \
3513 return sprintf(buf, "off\n"); \
Mike Christiefd7255f2006-04-06 21:13:36 -05003514 return sprintf(buf, format"\n", session->field); \
3515}
3516
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05303517#define iscsi_priv_session_attr_store(field) \
3518static ssize_t \
3519store_priv_session_##field(struct device *dev, \
3520 struct device_attribute *attr, \
3521 const char *buf, size_t count) \
3522{ \
3523 int val; \
3524 char *cp; \
3525 struct iscsi_cls_session *session = \
3526 iscsi_dev_to_session(dev->parent); \
3527 if ((session->state == ISCSI_SESSION_FREE) || \
3528 (session->state == ISCSI_SESSION_FAILED)) \
3529 return -EBUSY; \
3530 if (strncmp(buf, "off", 3) == 0) \
3531 session->field = -1; \
3532 else { \
3533 val = simple_strtoul(buf, &cp, 0); \
3534 if (*cp != '\0' && *cp != '\n') \
3535 return -EINVAL; \
3536 session->field = val; \
3537 } \
3538 return count; \
3539}
3540
3541#define iscsi_priv_session_rw_attr(field, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05003542 iscsi_priv_session_attr_show(field, format) \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05303543 iscsi_priv_session_attr_store(field) \
Vasiliy Kulikov523f3c82011-02-04 15:24:14 +03003544static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05303545 show_priv_session_##field, \
3546 store_priv_session_##field)
3547iscsi_priv_session_rw_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05003548
Mike Christie1d063c12011-07-25 13:48:43 -05003549static struct attribute *iscsi_session_attrs[] = {
3550 &dev_attr_sess_initial_r2t.attr,
3551 &dev_attr_sess_max_outstanding_r2t.attr,
3552 &dev_attr_sess_immediate_data.attr,
3553 &dev_attr_sess_first_burst_len.attr,
3554 &dev_attr_sess_max_burst_len.attr,
3555 &dev_attr_sess_data_pdu_in_order.attr,
3556 &dev_attr_sess_data_seq_in_order.attr,
3557 &dev_attr_sess_erl.attr,
3558 &dev_attr_sess_targetname.attr,
3559 &dev_attr_sess_tpgt.attr,
3560 &dev_attr_sess_password.attr,
3561 &dev_attr_sess_password_in.attr,
3562 &dev_attr_sess_username.attr,
3563 &dev_attr_sess_username_in.attr,
3564 &dev_attr_sess_fast_abort.attr,
3565 &dev_attr_sess_abort_tmo.attr,
3566 &dev_attr_sess_lu_reset_tmo.attr,
3567 &dev_attr_sess_tgt_reset_tmo.attr,
3568 &dev_attr_sess_ifacename.attr,
3569 &dev_attr_sess_initiatorname.attr,
3570 &dev_attr_sess_targetalias.attr,
3571 &dev_attr_priv_sess_recovery_tmo.attr,
3572 &dev_attr_priv_sess_state.attr,
Mike Christie0c70d842011-12-05 16:44:01 -06003573 &dev_attr_priv_sess_creator.attr,
Mike Christie30534952012-02-27 03:08:53 -08003574 &dev_attr_sess_chap_out_idx.attr,
3575 &dev_attr_sess_chap_in_idx.attr,
Manish Rangankar930a9f02012-11-23 06:58:41 -05003576 &dev_attr_priv_sess_target_id.attr,
Mike Christie1d063c12011-07-25 13:48:43 -05003577 NULL,
3578};
3579
Al Viro587a1f12011-07-23 23:11:19 -04003580static umode_t iscsi_session_attr_is_visible(struct kobject *kobj,
Mike Christie1d063c12011-07-25 13:48:43 -05003581 struct attribute *attr, int i)
3582{
3583 struct device *cdev = container_of(kobj, struct device, kobj);
3584 struct iscsi_cls_session *session = transport_class_to_session(cdev);
3585 struct iscsi_transport *t = session->transport;
3586 int param;
3587
3588 if (attr == &dev_attr_sess_initial_r2t.attr)
3589 param = ISCSI_PARAM_INITIAL_R2T_EN;
3590 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
3591 param = ISCSI_PARAM_MAX_R2T;
3592 else if (attr == &dev_attr_sess_immediate_data.attr)
3593 param = ISCSI_PARAM_IMM_DATA_EN;
3594 else if (attr == &dev_attr_sess_first_burst_len.attr)
3595 param = ISCSI_PARAM_FIRST_BURST;
3596 else if (attr == &dev_attr_sess_max_burst_len.attr)
3597 param = ISCSI_PARAM_MAX_BURST;
3598 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
3599 param = ISCSI_PARAM_PDU_INORDER_EN;
3600 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
3601 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
3602 else if (attr == &dev_attr_sess_erl.attr)
3603 param = ISCSI_PARAM_ERL;
3604 else if (attr == &dev_attr_sess_targetname.attr)
3605 param = ISCSI_PARAM_TARGET_NAME;
3606 else if (attr == &dev_attr_sess_tpgt.attr)
3607 param = ISCSI_PARAM_TPGT;
Mike Christie30534952012-02-27 03:08:53 -08003608 else if (attr == &dev_attr_sess_chap_in_idx.attr)
3609 param = ISCSI_PARAM_CHAP_IN_IDX;
3610 else if (attr == &dev_attr_sess_chap_out_idx.attr)
3611 param = ISCSI_PARAM_CHAP_OUT_IDX;
Mike Christie1d063c12011-07-25 13:48:43 -05003612 else if (attr == &dev_attr_sess_password.attr)
3613 param = ISCSI_PARAM_USERNAME;
3614 else if (attr == &dev_attr_sess_password_in.attr)
3615 param = ISCSI_PARAM_USERNAME_IN;
3616 else if (attr == &dev_attr_sess_username.attr)
3617 param = ISCSI_PARAM_PASSWORD;
3618 else if (attr == &dev_attr_sess_username_in.attr)
3619 param = ISCSI_PARAM_PASSWORD_IN;
3620 else if (attr == &dev_attr_sess_fast_abort.attr)
3621 param = ISCSI_PARAM_FAST_ABORT;
3622 else if (attr == &dev_attr_sess_abort_tmo.attr)
3623 param = ISCSI_PARAM_ABORT_TMO;
3624 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
3625 param = ISCSI_PARAM_LU_RESET_TMO;
3626 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
3627 param = ISCSI_PARAM_TGT_RESET_TMO;
3628 else if (attr == &dev_attr_sess_ifacename.attr)
3629 param = ISCSI_PARAM_IFACE_NAME;
3630 else if (attr == &dev_attr_sess_initiatorname.attr)
3631 param = ISCSI_PARAM_INITIATOR_NAME;
3632 else if (attr == &dev_attr_sess_targetalias.attr)
3633 param = ISCSI_PARAM_TARGET_ALIAS;
3634 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
3635 return S_IRUGO | S_IWUSR;
3636 else if (attr == &dev_attr_priv_sess_state.attr)
3637 return S_IRUGO;
Mike Christie0c70d842011-12-05 16:44:01 -06003638 else if (attr == &dev_attr_priv_sess_creator.attr)
3639 return S_IRUGO;
Manish Rangankar930a9f02012-11-23 06:58:41 -05003640 else if (attr == &dev_attr_priv_sess_target_id.attr)
3641 return S_IRUGO;
Mike Christie1d063c12011-07-25 13:48:43 -05003642 else {
3643 WARN_ONCE(1, "Invalid session attr");
3644 return 0;
3645 }
3646
3647 return t->attr_is_visible(ISCSI_PARAM, param);
3648}
3649
3650static struct attribute_group iscsi_session_group = {
3651 .attrs = iscsi_session_attrs,
3652 .is_visible = iscsi_session_attr_is_visible,
3653};
3654
Mike Christie1819dc82007-05-30 12:57:08 -05003655/*
3656 * iSCSI host attrs
3657 */
3658#define iscsi_host_attr_show(param) \
3659static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01003660show_host_param_##param(struct device *dev, \
3661 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05003662{ \
Tony Jonesee959b02008-02-22 00:13:36 +01003663 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05003664 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
3665 return priv->iscsi_transport->get_host_param(shost, param, buf); \
3666}
3667
3668#define iscsi_host_attr(field, param) \
3669 iscsi_host_attr_show(param) \
3670static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
3671 NULL);
3672
Mike Christied8196ed2007-05-30 12:57:25 -05003673iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05003674iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05003675iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05003676iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08003677iscsi_host_attr(port_state, ISCSI_HOST_PARAM_PORT_STATE);
3678iscsi_host_attr(port_speed, ISCSI_HOST_PARAM_PORT_SPEED);
Mike Christie1819dc82007-05-30 12:57:08 -05003679
Mike Christief27fb2e2011-07-25 13:48:45 -05003680static struct attribute *iscsi_host_attrs[] = {
3681 &dev_attr_host_netdev.attr,
3682 &dev_attr_host_hwaddress.attr,
3683 &dev_attr_host_ipaddress.attr,
3684 &dev_attr_host_initiatorname.attr,
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08003685 &dev_attr_host_port_state.attr,
3686 &dev_attr_host_port_speed.attr,
Mike Christief27fb2e2011-07-25 13:48:45 -05003687 NULL,
3688};
3689
Al Viro587a1f12011-07-23 23:11:19 -04003690static umode_t iscsi_host_attr_is_visible(struct kobject *kobj,
Mike Christief27fb2e2011-07-25 13:48:45 -05003691 struct attribute *attr, int i)
3692{
3693 struct device *cdev = container_of(kobj, struct device, kobj);
3694 struct Scsi_Host *shost = transport_class_to_shost(cdev);
3695 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
3696 int param;
3697
3698 if (attr == &dev_attr_host_netdev.attr)
3699 param = ISCSI_HOST_PARAM_NETDEV_NAME;
3700 else if (attr == &dev_attr_host_hwaddress.attr)
3701 param = ISCSI_HOST_PARAM_HWADDRESS;
3702 else if (attr == &dev_attr_host_ipaddress.attr)
3703 param = ISCSI_HOST_PARAM_IPADDRESS;
3704 else if (attr == &dev_attr_host_initiatorname.attr)
3705 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08003706 else if (attr == &dev_attr_host_port_state.attr)
3707 param = ISCSI_HOST_PARAM_PORT_STATE;
3708 else if (attr == &dev_attr_host_port_speed.attr)
3709 param = ISCSI_HOST_PARAM_PORT_SPEED;
Mike Christief27fb2e2011-07-25 13:48:45 -05003710 else {
3711 WARN_ONCE(1, "Invalid host attr");
3712 return 0;
3713 }
3714
3715 return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
3716}
3717
3718static struct attribute_group iscsi_host_group = {
3719 .attrs = iscsi_host_attrs,
3720 .is_visible = iscsi_host_attr_is_visible,
3721};
Mike Christie1819dc82007-05-30 12:57:08 -05003722
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08003723/* convert iscsi_port_speed values to ascii string name */
3724static const struct {
3725 enum iscsi_port_speed value;
3726 char *name;
3727} iscsi_port_speed_names[] = {
3728 {ISCSI_PORT_SPEED_UNKNOWN, "Unknown" },
3729 {ISCSI_PORT_SPEED_10MBPS, "10 Mbps" },
3730 {ISCSI_PORT_SPEED_100MBPS, "100 Mbps" },
3731 {ISCSI_PORT_SPEED_1GBPS, "1 Gbps" },
3732 {ISCSI_PORT_SPEED_10GBPS, "10 Gbps" },
3733};
3734
3735char *iscsi_get_port_speed_name(struct Scsi_Host *shost)
3736{
3737 int i;
3738 char *speed = "Unknown!";
3739 struct iscsi_cls_host *ihost = shost->shost_data;
3740 uint32_t port_speed = ihost->port_speed;
3741
3742 for (i = 0; i < ARRAY_SIZE(iscsi_port_speed_names); i++) {
3743 if (iscsi_port_speed_names[i].value & port_speed) {
3744 speed = iscsi_port_speed_names[i].name;
3745 break;
3746 }
3747 }
3748 return speed;
3749}
3750EXPORT_SYMBOL_GPL(iscsi_get_port_speed_name);
3751
3752/* convert iscsi_port_state values to ascii string name */
3753static const struct {
3754 enum iscsi_port_state value;
3755 char *name;
3756} iscsi_port_state_names[] = {
3757 {ISCSI_PORT_STATE_DOWN, "LINK DOWN" },
3758 {ISCSI_PORT_STATE_UP, "LINK UP" },
3759};
3760
3761char *iscsi_get_port_state_name(struct Scsi_Host *shost)
3762{
3763 int i;
3764 char *state = "Unknown!";
3765 struct iscsi_cls_host *ihost = shost->shost_data;
3766 uint32_t port_state = ihost->port_state;
3767
3768 for (i = 0; i < ARRAY_SIZE(iscsi_port_state_names); i++) {
3769 if (iscsi_port_state_names[i].value & port_state) {
3770 state = iscsi_port_state_names[i].name;
3771 break;
3772 }
3773 }
3774 return state;
3775}
3776EXPORT_SYMBOL_GPL(iscsi_get_port_state_name);
3777
Alex Aizman0896b752005-08-04 19:33:07 -07003778static int iscsi_session_match(struct attribute_container *cont,
3779 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780{
Mike Christie7b8631b2006-01-13 18:05:50 -06003781 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07003783 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Alex Aizman0896b752005-08-04 19:33:07 -07003785 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 return 0;
3787
Mike Christie7b8631b2006-01-13 18:05:50 -06003788 session = iscsi_dev_to_session(dev);
3789 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07003790 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return 0;
3792
Alex Aizman0896b752005-08-04 19:33:07 -07003793 priv = to_iscsi_internal(shost->transportt);
3794 if (priv->session_cont.ac.class != &iscsi_session_class.class)
3795 return 0;
3796
3797 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798}
3799
Alex Aizman0896b752005-08-04 19:33:07 -07003800static int iscsi_conn_match(struct attribute_container *cont,
3801 struct device *dev)
3802{
Mike Christie7b8631b2006-01-13 18:05:50 -06003803 struct iscsi_cls_session *session;
3804 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07003805 struct Scsi_Host *shost;
3806 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
Alex Aizman0896b752005-08-04 19:33:07 -07003808 if (!iscsi_is_conn_dev(dev))
3809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
Mike Christie7b8631b2006-01-13 18:05:50 -06003811 conn = iscsi_dev_to_conn(dev);
3812 session = iscsi_dev_to_session(conn->dev.parent);
3813 shost = iscsi_session_to_shost(session);
3814
Alex Aizman0896b752005-08-04 19:33:07 -07003815 if (!shost->transportt)
3816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Alex Aizman0896b752005-08-04 19:33:07 -07003818 priv = to_iscsi_internal(shost->transportt);
3819 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
3820 return 0;
3821
3822 return &priv->conn_cont.ac == cont;
3823}
3824
Mike Christie30a6c652006-04-06 21:13:39 -05003825static int iscsi_host_match(struct attribute_container *cont,
3826 struct device *dev)
3827{
3828 struct Scsi_Host *shost;
3829 struct iscsi_internal *priv;
3830
3831 if (!scsi_is_host_device(dev))
3832 return 0;
3833
3834 shost = dev_to_shost(dev);
3835 if (!shost->transportt ||
3836 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
3837 return 0;
3838
3839 priv = to_iscsi_internal(shost->transportt);
3840 return &priv->t.host_attrs.ac == cont;
3841}
3842
Mike Christie7b8631b2006-01-13 18:05:50 -06003843struct scsi_transport_template *
3844iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07003845{
3846 struct iscsi_internal *priv;
3847 unsigned long flags;
Mike Christief27fb2e2011-07-25 13:48:45 -05003848 int err;
Alex Aizman0896b752005-08-04 19:33:07 -07003849
3850 BUG_ON(!tt);
3851
3852 priv = iscsi_if_transport_lookup(tt);
3853 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06003854 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07003855
Jes Sorensen24669f752006-01-16 10:31:18 -05003856 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07003857 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06003858 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07003859 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07003860 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05003861 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06003862 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07003863
Tony Jonesee959b02008-02-22 00:13:36 +01003864 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01003865 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01003866 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07003867 if (err)
3868 goto free_priv;
3869
Tony Jonesee959b02008-02-22 00:13:36 +01003870 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07003871 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01003872 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07003873
Mike Christie30a6c652006-04-06 21:13:39 -05003874 /* host parameters */
Mike Christie30a6c652006-04-06 21:13:39 -05003875 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
3876 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christief27fb2e2011-07-25 13:48:45 -05003877 priv->t.host_attrs.ac.grp = &iscsi_host_group;
Mike Christie32c6e1b2008-05-21 15:53:58 -05003878 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05003879 transport_container_register(&priv->t.host_attrs);
3880
Alex Aizman0896b752005-08-04 19:33:07 -07003881 /* connection parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07003882 priv->conn_cont.ac.class = &iscsi_connection_class.class;
3883 priv->conn_cont.ac.match = iscsi_conn_match;
Mike Christie3128c6c2011-07-25 13:48:42 -05003884 priv->conn_cont.ac.grp = &iscsi_conn_group;
Alex Aizman0896b752005-08-04 19:33:07 -07003885 transport_container_register(&priv->conn_cont);
3886
Alex Aizman0896b752005-08-04 19:33:07 -07003887 /* session parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07003888 priv->session_cont.ac.class = &iscsi_session_class.class;
3889 priv->session_cont.ac.match = iscsi_session_match;
Mike Christie1d063c12011-07-25 13:48:43 -05003890 priv->session_cont.ac.grp = &iscsi_session_group;
Alex Aizman0896b752005-08-04 19:33:07 -07003891 transport_container_register(&priv->session_cont);
3892
Alex Aizman0896b752005-08-04 19:33:07 -07003893 spin_lock_irqsave(&iscsi_transport_lock, flags);
3894 list_add(&priv->list, &iscsi_transports);
3895 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Alex Aizman0896b752005-08-04 19:33:07 -07003897 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06003898 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Tony Jonesee959b02008-02-22 00:13:36 +01003900unregister_dev:
3901 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05003902 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07003903free_priv:
3904 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06003905 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906}
Alex Aizman0896b752005-08-04 19:33:07 -07003907EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Alex Aizman0896b752005-08-04 19:33:07 -07003909int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910{
Alex Aizman0896b752005-08-04 19:33:07 -07003911 struct iscsi_internal *priv;
3912 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Alex Aizman0896b752005-08-04 19:33:07 -07003914 BUG_ON(!tt);
3915
Arjan van de Ven0b950672006-01-11 13:16:10 +01003916 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07003917
3918 priv = iscsi_if_transport_lookup(tt);
3919 BUG_ON (!priv);
3920
Alex Aizman0896b752005-08-04 19:33:07 -07003921 spin_lock_irqsave(&iscsi_transport_lock, flags);
3922 list_del(&priv->list);
3923 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
3924
3925 transport_container_unregister(&priv->conn_cont);
3926 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05003927 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07003928
Tony Jonesee959b02008-02-22 00:13:36 +01003929 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
3930 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01003931 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07003932
3933 return 0;
3934}
3935EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
3936
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937static __init int iscsi_transport_init(void)
3938{
Alex Aizman0896b752005-08-04 19:33:07 -07003939 int err;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003940 struct netlink_kernel_cfg cfg = {
3941 .groups = 1,
3942 .input = iscsi_if_rx,
3943 };
Meelis Roos09492602006-12-17 12:10:26 -06003944 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05003945 ISCSI_TRANSPORT_VERSION);
3946
Mike Christie41be1442007-02-28 17:32:18 -06003947 atomic_set(&iscsi_session_nr, 0);
3948
Alex Aizman0896b752005-08-04 19:33:07 -07003949 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 if (err)
3951 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07003952
Mike Christied82ff9be2008-05-21 15:54:13 -05003953 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07003954 if (err)
3955 goto unregister_transport_class;
3956
Mike Christie8d079132011-07-25 13:48:40 -05003957 err = class_register(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05003958 if (err)
3959 goto unregister_endpoint_class;
3960
Mike Christie8d079132011-07-25 13:48:40 -05003961 err = transport_class_register(&iscsi_host_class);
3962 if (err)
3963 goto unregister_iface_class;
3964
Mike Christie30a6c652006-04-06 21:13:39 -05003965 err = transport_class_register(&iscsi_connection_class);
3966 if (err)
3967 goto unregister_host_class;
3968
Alex Aizman0896b752005-08-04 19:33:07 -07003969 err = transport_class_register(&iscsi_session_class);
3970 if (err)
3971 goto unregister_conn_class;
3972
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003973 err = bus_register(&iscsi_flashnode_bus);
3974 if (err)
3975 goto unregister_session_class;
3976
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003977 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, &cfg);
Alex Aizman0896b752005-08-04 19:33:07 -07003978 if (!nls) {
3979 err = -ENOBUFS;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003980 goto unregister_flashnode_bus;
Alex Aizman0896b752005-08-04 19:33:07 -07003981 }
3982
Mike Christied8bf5412007-12-13 12:43:27 -06003983 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
Wei Yongjunbcab2cc2013-04-19 09:23:28 +08003984 if (!iscsi_eh_timer_workq) {
3985 err = -ENOMEM;
Mike Christied8bf5412007-12-13 12:43:27 -06003986 goto release_nls;
Wei Yongjunbcab2cc2013-04-19 09:23:28 +08003987 }
Mike Christied8bf5412007-12-13 12:43:27 -06003988
Mike Christie43a145a2006-10-16 18:09:38 -04003989 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07003990
Mike Christied8bf5412007-12-13 12:43:27 -06003991release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08003992 netlink_kernel_release(nls);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003993unregister_flashnode_bus:
3994 bus_unregister(&iscsi_flashnode_bus);
Alex Aizman0896b752005-08-04 19:33:07 -07003995unregister_session_class:
3996 transport_class_unregister(&iscsi_session_class);
3997unregister_conn_class:
3998 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05003999unregister_host_class:
4000 transport_class_unregister(&iscsi_host_class);
Mike Christie8d079132011-07-25 13:48:40 -05004001unregister_iface_class:
4002 class_unregister(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05004003unregister_endpoint_class:
4004 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07004005unregister_transport_class:
4006 class_unregister(&iscsi_transport_class);
4007 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008}
4009
4010static void __exit iscsi_transport_exit(void)
4011{
Mike Christied8bf5412007-12-13 12:43:27 -06004012 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08004013 netlink_kernel_release(nls);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04004014 bus_unregister(&iscsi_flashnode_bus);
Alex Aizman0896b752005-08-04 19:33:07 -07004015 transport_class_unregister(&iscsi_connection_class);
4016 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05004017 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05004018 class_unregister(&iscsi_endpoint_class);
Mike Christie8d079132011-07-25 13:48:40 -05004019 class_unregister(&iscsi_iface_class);
Alex Aizman0896b752005-08-04 19:33:07 -07004020 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021}
4022
4023module_init(iscsi_transport_init);
4024module_exit(iscsi_transport_exit);
4025
Alex Aizman0896b752005-08-04 19:33:07 -07004026MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
4027 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
4028 "Alex Aizman <itn780@yahoo.com>");
4029MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05004031MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
Stephen Hemminger058548a2010-12-09 09:37:56 -08004032MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);