blob: 554b6f7842236c64d694d0233450431ca6aed2ab [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Alex Aizman0896b752005-08-04 19:33:07 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * iSCSI transport class definitions
4 *
5 * Copyright (C) IBM Corporation, 2004
Alex Aizman0896b752005-08-04 19:33:07 -07006 * Copyright (C) Mike Christie, 2004 - 2005
7 * Copyright (C) Dmitry Yusupov, 2004 - 2005
8 * Copyright (C) Alex Aizman, 2004 - 2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10#include <linux/module.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010011#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Mike Christie90eeb012011-07-25 13:48:50 -050013#include <linux/bsg-lib.h>
Mike Christie8d4a6902011-10-06 03:56:57 -050014#include <linux/idr.h>
Alex Aizman0896b752005-08-04 19:33:07 -070015#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <scsi/scsi.h>
17#include <scsi/scsi_host.h>
18#include <scsi/scsi_device.h>
19#include <scsi/scsi_transport.h>
20#include <scsi/scsi_transport_iscsi.h>
Alex Aizman0896b752005-08-04 19:33:07 -070021#include <scsi/iscsi_if.h>
Mike Christiec01be6d2010-07-22 16:59:49 +053022#include <scsi/scsi_cmnd.h>
Mike Christie90eeb012011-07-25 13:48:50 -050023#include <scsi/scsi_bsg_iscsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Mike Christie4c2133c2008-06-16 10:11:34 -050025#define ISCSI_TRANSPORT_VERSION "2.0-870"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Bo Wubba340c2019-11-20 13:26:17 +000027#define ISCSI_SEND_MAX_ALLOWED 10
28
Fred Herardc2332b02018-11-21 12:04:43 -050029#define CREATE_TRACE_POINTS
30#include <trace/events/iscsi.h>
31
32/*
33 * Export tracepoint symbols to be used by other modules.
34 */
35EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_conn);
36EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_eh);
37EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_session);
38EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_tcp);
39EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_sw_tcp);
40
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); \
Fred Herardc2332b02018-11-21 12:04:43 -050063 iscsi_dbg_trace(trace_iscsi_dbg_trans_session, \
64 &(_session)->dev, \
65 "%s " dbg_fmt, __func__, ##arg); \
Mike Christie632248a2009-08-20 15:11:01 -050066 } while (0);
67
68#define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
69 do { \
70 if (dbg_conn) \
71 iscsi_cls_conn_printk(KERN_INFO, _conn, \
72 "%s: " dbg_fmt, \
Fred Herardc2332b02018-11-21 12:04:43 -050073 __func__, ##arg); \
74 iscsi_dbg_trace(trace_iscsi_dbg_trans_conn, \
75 &(_conn)->dev, \
76 "%s " dbg_fmt, __func__, ##arg); \
Mike Christie632248a2009-08-20 15:11:01 -050077 } while (0);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079struct iscsi_internal {
80 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070081 struct iscsi_transport *iscsi_transport;
82 struct list_head list;
Tony Jonesee959b02008-02-22 00:13:36 +010083 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050084
Alex Aizman0896b752005-08-04 19:33:07 -070085 struct transport_container conn_cont;
Alex Aizman0896b752005-08-04 19:33:07 -070086 struct transport_container session_cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087};
88
Mike Christie41be1442007-02-28 17:32:18 -060089static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060090static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050091
Mike Christie23d6fef2021-05-25 13:18:00 -050092static struct workqueue_struct *iscsi_conn_cleanup_workq;
93
Mike Christie8d4a6902011-10-06 03:56:57 -050094static DEFINE_IDA(iscsi_sess_ida);
Alex Aizman0896b752005-08-04 19:33:07 -070095/*
96 * list of registered transports and lock that must
97 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010098 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070099 */
100static LIST_HEAD(iscsi_transports);
101static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Alex Aizman0896b752005-08-04 19:33:07 -0700103#define to_iscsi_internal(tmpl) \
104 container_of(tmpl, struct iscsi_internal, t)
105
Tony Jonesee959b02008-02-22 00:13:36 +0100106#define dev_to_iscsi_internal(_dev) \
107 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700108
Tony Jonesee959b02008-02-22 00:13:36 +0100109static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700110{
Tony Jonesee959b02008-02-22 00:13:36 +0100111 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -0700112 kfree(priv);
113}
114
115/*
116 * iscsi_transport_class represents the iscsi_transports that are
117 * registered.
118 */
119static struct class iscsi_transport_class = {
120 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +0100121 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -0700122};
123
124static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100125show_transport_handle(struct device *dev, struct device_attribute *attr,
126 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -0700127{
Tony Jonesee959b02008-02-22 00:13:36 +0100128 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Lee Duncan688e8122021-02-23 13:06:24 -0800129
130 if (!capable(CAP_SYS_ADMIN))
131 return -EACCES;
Chris Leechec98ea72021-02-23 18:00:17 -0800132 return sysfs_emit(buf, "%llu\n",
133 (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -0700134}
Tony Jonesee959b02008-02-22 00:13:36 +0100135static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700136
137#define show_transport_attr(name, format) \
138static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100139show_transport_##name(struct device *dev, \
140 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -0700141{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100142 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Chris Leechec98ea72021-02-23 18:00:17 -0800143 return sysfs_emit(buf, format"\n", priv->iscsi_transport->name);\
Alex Aizman0896b752005-08-04 19:33:07 -0700144} \
Tony Jonesee959b02008-02-22 00:13:36 +0100145static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700146
147show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700148
149static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100150 &dev_attr_handle.attr,
151 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700152 NULL,
153};
154
155static struct attribute_group iscsi_transport_group = {
156 .attrs = iscsi_transport_attrs,
157};
158
Mike Christied82ff9be2008-05-21 15:54:13 -0500159/*
160 * iSCSI endpoint attrs
161 */
162#define iscsi_dev_to_endpoint(_dev) \
163 container_of(_dev, struct iscsi_endpoint, dev)
164
165#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
166struct device_attribute dev_attr_##_prefix##_##_name = \
167 __ATTR(_name,_mode,_show,_store)
168
169static void iscsi_endpoint_release(struct device *dev)
170{
171 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
172 kfree(ep);
173}
174
175static struct class iscsi_endpoint_class = {
176 .name = "iscsi_endpoint",
177 .dev_release = iscsi_endpoint_release,
178};
179
180static ssize_t
181show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
182{
183 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Chris Leechec98ea72021-02-23 18:00:17 -0800184 return sysfs_emit(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500185}
186static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
187
188static struct attribute *iscsi_endpoint_attrs[] = {
189 &dev_attr_ep_handle.attr,
190 NULL,
191};
192
193static struct attribute_group iscsi_endpoint_group = {
194 .attrs = iscsi_endpoint_attrs,
195};
196
197#define ISCSI_MAX_EPID -1
198
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100199static int iscsi_match_epid(struct device *dev, const void *data)
Mike Christied82ff9be2008-05-21 15:54:13 -0500200{
201 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100202 const uint64_t *epid = data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500203
204 return *epid == ep->id;
205}
206
207struct iscsi_endpoint *
208iscsi_create_endpoint(int dd_size)
209{
210 struct device *dev;
211 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500212 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500213 int err;
214
215 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400216 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500217 iscsi_match_epid);
218 if (!dev)
219 break;
Sagi Grimberg4ce30872015-05-20 12:38:58 +0300220 else
221 put_device(dev);
Mike Christied82ff9be2008-05-21 15:54:13 -0500222 }
223 if (id == ISCSI_MAX_EPID) {
224 printk(KERN_ERR "Too many connections. Max supported %u\n",
225 ISCSI_MAX_EPID - 1);
226 return NULL;
227 }
228
229 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
230 if (!ep)
231 return NULL;
232
233 ep->id = id;
234 ep->dev.class = &iscsi_endpoint_class;
Kay Sievers71610f52008-12-03 22:41:36 +0100235 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500236 err = device_register(&ep->dev);
237 if (err)
238 goto free_ep;
239
240 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
241 if (err)
242 goto unregister_dev;
243
244 if (dd_size)
245 ep->dd_data = &ep[1];
246 return ep;
247
248unregister_dev:
249 device_unregister(&ep->dev);
250 return NULL;
251
252free_ep:
253 kfree(ep);
254 return NULL;
255}
256EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
257
258void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
259{
260 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
261 device_unregister(&ep->dev);
262}
263EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
264
Mike Christie9e5fe1702021-05-25 13:17:59 -0500265void iscsi_put_endpoint(struct iscsi_endpoint *ep)
266{
267 put_device(&ep->dev);
268}
269EXPORT_SYMBOL_GPL(iscsi_put_endpoint);
270
271/**
272 * iscsi_lookup_endpoint - get ep from handle
273 * @handle: endpoint handle
274 *
275 * Caller must do a iscsi_put_endpoint.
276 */
Mike Christied82ff9be2008-05-21 15:54:13 -0500277struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
278{
279 struct device *dev;
280
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400281 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500282 iscsi_match_epid);
283 if (!dev)
284 return NULL;
285
Mike Christie9e5fe1702021-05-25 13:17:59 -0500286 return iscsi_dev_to_endpoint(dev);
Mike Christied82ff9be2008-05-21 15:54:13 -0500287}
288EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
289
Mike Christie8d079132011-07-25 13:48:40 -0500290/*
291 * Interface to display network param to sysfs
292 */
293
294static void iscsi_iface_release(struct device *dev)
295{
296 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
297 struct device *parent = iface->dev.parent;
298
299 kfree(iface);
300 put_device(parent);
301}
302
303
304static struct class iscsi_iface_class = {
305 .name = "iscsi_iface",
306 .dev_release = iscsi_iface_release,
307};
308
309#define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
310struct device_attribute dev_attr_##_prefix##_##_name = \
311 __ATTR(_name, _mode, _show, _store)
312
313/* iface attrs show */
314#define iscsi_iface_attr_show(type, name, param_type, param) \
315static ssize_t \
316show_##type##_##name(struct device *dev, struct device_attribute *attr, \
317 char *buf) \
318{ \
319 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
320 struct iscsi_transport *t = iface->transport; \
321 return t->get_iface_param(iface, param_type, param, buf); \
322} \
323
324#define iscsi_iface_net_attr(type, name, param) \
325 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
326static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
327
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400328#define iscsi_iface_attr(type, name, param) \
329 iscsi_iface_attr_show(type, name, ISCSI_IFACE_PARAM, param) \
330static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
331
332/* generic read only ipv4 attribute */
Mike Christie8d079132011-07-25 13:48:40 -0500333iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
334iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
335iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
336iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400337iscsi_iface_net_attr(ipv4_iface, dhcp_dns_address_en,
338 ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN);
339iscsi_iface_net_attr(ipv4_iface, dhcp_slp_da_info_en,
340 ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN);
341iscsi_iface_net_attr(ipv4_iface, tos_en, ISCSI_NET_PARAM_IPV4_TOS_EN);
342iscsi_iface_net_attr(ipv4_iface, tos, ISCSI_NET_PARAM_IPV4_TOS);
343iscsi_iface_net_attr(ipv4_iface, grat_arp_en,
344 ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN);
345iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id_en,
346 ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN);
347iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id,
348 ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID);
349iscsi_iface_net_attr(ipv4_iface, dhcp_req_vendor_id_en,
350 ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN);
351iscsi_iface_net_attr(ipv4_iface, dhcp_use_vendor_id_en,
352 ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN);
353iscsi_iface_net_attr(ipv4_iface, dhcp_vendor_id,
354 ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID);
355iscsi_iface_net_attr(ipv4_iface, dhcp_learn_iqn_en,
356 ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN);
357iscsi_iface_net_attr(ipv4_iface, fragment_disable,
358 ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE);
359iscsi_iface_net_attr(ipv4_iface, incoming_forwarding_en,
360 ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN);
361iscsi_iface_net_attr(ipv4_iface, ttl, ISCSI_NET_PARAM_IPV4_TTL);
Mike Christie8d079132011-07-25 13:48:40 -0500362
363/* generic read only ipv6 attribute */
364iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400365iscsi_iface_net_attr(ipv6_iface, link_local_addr,
366 ISCSI_NET_PARAM_IPV6_LINKLOCAL);
Mike Christie8d079132011-07-25 13:48:40 -0500367iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
368iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
369 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
Mike Christie88f4f512011-10-06 03:56:58 -0500370iscsi_iface_net_attr(ipv6_iface, link_local_autocfg,
Mike Christie8d079132011-07-25 13:48:40 -0500371 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400372iscsi_iface_net_attr(ipv6_iface, link_local_state,
373 ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE);
374iscsi_iface_net_attr(ipv6_iface, router_state,
375 ISCSI_NET_PARAM_IPV6_ROUTER_STATE);
376iscsi_iface_net_attr(ipv6_iface, grat_neighbor_adv_en,
377 ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN);
378iscsi_iface_net_attr(ipv6_iface, mld_en, ISCSI_NET_PARAM_IPV6_MLD_EN);
379iscsi_iface_net_attr(ipv6_iface, flow_label, ISCSI_NET_PARAM_IPV6_FLOW_LABEL);
380iscsi_iface_net_attr(ipv6_iface, traffic_class,
381 ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS);
382iscsi_iface_net_attr(ipv6_iface, hop_limit, ISCSI_NET_PARAM_IPV6_HOP_LIMIT);
383iscsi_iface_net_attr(ipv6_iface, nd_reachable_tmo,
384 ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO);
385iscsi_iface_net_attr(ipv6_iface, nd_rexmit_time,
386 ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME);
387iscsi_iface_net_attr(ipv6_iface, nd_stale_tmo,
388 ISCSI_NET_PARAM_IPV6_ND_STALE_TMO);
389iscsi_iface_net_attr(ipv6_iface, dup_addr_detect_cnt,
390 ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT);
391iscsi_iface_net_attr(ipv6_iface, router_adv_link_mtu,
392 ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU);
Mike Christie8d079132011-07-25 13:48:40 -0500393
394/* common read only iface attribute */
395iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
Mike Christie2d636732011-10-11 17:55:11 -0500396iscsi_iface_net_attr(iface, vlan_id, ISCSI_NET_PARAM_VLAN_ID);
Mike Christie4223b9e2011-07-25 13:48:47 -0500397iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
398iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
Vikas Chaudhary8c7d40f2011-08-01 03:26:12 -0700399iscsi_iface_net_attr(iface, mtu, ISCSI_NET_PARAM_MTU);
Vikas Chaudharyfcb51242011-08-01 03:26:18 -0700400iscsi_iface_net_attr(iface, port, ISCSI_NET_PARAM_PORT);
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400401iscsi_iface_net_attr(iface, ipaddress_state, ISCSI_NET_PARAM_IPADDR_STATE);
402iscsi_iface_net_attr(iface, delayed_ack_en, ISCSI_NET_PARAM_DELAYED_ACK_EN);
403iscsi_iface_net_attr(iface, tcp_nagle_disable,
404 ISCSI_NET_PARAM_TCP_NAGLE_DISABLE);
405iscsi_iface_net_attr(iface, tcp_wsf_disable, ISCSI_NET_PARAM_TCP_WSF_DISABLE);
406iscsi_iface_net_attr(iface, tcp_wsf, ISCSI_NET_PARAM_TCP_WSF);
407iscsi_iface_net_attr(iface, tcp_timer_scale, ISCSI_NET_PARAM_TCP_TIMER_SCALE);
408iscsi_iface_net_attr(iface, tcp_timestamp_en, ISCSI_NET_PARAM_TCP_TIMESTAMP_EN);
409iscsi_iface_net_attr(iface, cache_id, ISCSI_NET_PARAM_CACHE_ID);
410iscsi_iface_net_attr(iface, redirect_en, ISCSI_NET_PARAM_REDIRECT_EN);
411
412/* common iscsi specific settings attributes */
413iscsi_iface_attr(iface, def_taskmgmt_tmo, ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO);
414iscsi_iface_attr(iface, header_digest, ISCSI_IFACE_PARAM_HDRDGST_EN);
415iscsi_iface_attr(iface, data_digest, ISCSI_IFACE_PARAM_DATADGST_EN);
416iscsi_iface_attr(iface, immediate_data, ISCSI_IFACE_PARAM_IMM_DATA_EN);
417iscsi_iface_attr(iface, initial_r2t, ISCSI_IFACE_PARAM_INITIAL_R2T_EN);
418iscsi_iface_attr(iface, data_seq_in_order,
419 ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN);
420iscsi_iface_attr(iface, data_pdu_in_order, ISCSI_IFACE_PARAM_PDU_INORDER_EN);
421iscsi_iface_attr(iface, erl, ISCSI_IFACE_PARAM_ERL);
422iscsi_iface_attr(iface, max_recv_dlength, ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH);
423iscsi_iface_attr(iface, first_burst_len, ISCSI_IFACE_PARAM_FIRST_BURST);
424iscsi_iface_attr(iface, max_outstanding_r2t, ISCSI_IFACE_PARAM_MAX_R2T);
425iscsi_iface_attr(iface, max_burst_len, ISCSI_IFACE_PARAM_MAX_BURST);
426iscsi_iface_attr(iface, chap_auth, ISCSI_IFACE_PARAM_CHAP_AUTH_EN);
427iscsi_iface_attr(iface, bidi_chap, ISCSI_IFACE_PARAM_BIDI_CHAP_EN);
428iscsi_iface_attr(iface, discovery_auth_optional,
429 ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL);
430iscsi_iface_attr(iface, discovery_logout,
431 ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN);
432iscsi_iface_attr(iface, strict_login_comp_en,
433 ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN);
434iscsi_iface_attr(iface, initiator_name, ISCSI_IFACE_PARAM_INITIATOR_NAME);
Mike Christie8d079132011-07-25 13:48:40 -0500435
Al Viro587a1f12011-07-23 23:11:19 -0400436static umode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
Mike Christie8d079132011-07-25 13:48:40 -0500437 struct attribute *attr, int i)
438{
439 struct device *dev = container_of(kobj, struct device, kobj);
440 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
441 struct iscsi_transport *t = iface->transport;
Mike Christiee746f342021-06-30 19:25:59 -0500442 int param = -1;
Mike Christie8d079132011-07-25 13:48:40 -0500443
Baokun Li4e285502021-09-01 16:53:36 +0800444 if (attr == &dev_attr_iface_def_taskmgmt_tmo.attr)
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400445 param = ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO;
446 else if (attr == &dev_attr_iface_header_digest.attr)
447 param = ISCSI_IFACE_PARAM_HDRDGST_EN;
448 else if (attr == &dev_attr_iface_data_digest.attr)
449 param = ISCSI_IFACE_PARAM_DATADGST_EN;
450 else if (attr == &dev_attr_iface_immediate_data.attr)
451 param = ISCSI_IFACE_PARAM_IMM_DATA_EN;
452 else if (attr == &dev_attr_iface_initial_r2t.attr)
453 param = ISCSI_IFACE_PARAM_INITIAL_R2T_EN;
454 else if (attr == &dev_attr_iface_data_seq_in_order.attr)
455 param = ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN;
456 else if (attr == &dev_attr_iface_data_pdu_in_order.attr)
457 param = ISCSI_IFACE_PARAM_PDU_INORDER_EN;
458 else if (attr == &dev_attr_iface_erl.attr)
459 param = ISCSI_IFACE_PARAM_ERL;
460 else if (attr == &dev_attr_iface_max_recv_dlength.attr)
461 param = ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH;
462 else if (attr == &dev_attr_iface_first_burst_len.attr)
463 param = ISCSI_IFACE_PARAM_FIRST_BURST;
464 else if (attr == &dev_attr_iface_max_outstanding_r2t.attr)
465 param = ISCSI_IFACE_PARAM_MAX_R2T;
466 else if (attr == &dev_attr_iface_max_burst_len.attr)
467 param = ISCSI_IFACE_PARAM_MAX_BURST;
468 else if (attr == &dev_attr_iface_chap_auth.attr)
469 param = ISCSI_IFACE_PARAM_CHAP_AUTH_EN;
470 else if (attr == &dev_attr_iface_bidi_chap.attr)
471 param = ISCSI_IFACE_PARAM_BIDI_CHAP_EN;
472 else if (attr == &dev_attr_iface_discovery_auth_optional.attr)
473 param = ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL;
474 else if (attr == &dev_attr_iface_discovery_logout.attr)
475 param = ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN;
476 else if (attr == &dev_attr_iface_strict_login_comp_en.attr)
477 param = ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN;
478 else if (attr == &dev_attr_iface_initiator_name.attr)
479 param = ISCSI_IFACE_PARAM_INITIATOR_NAME;
Mike Christiee746f342021-06-30 19:25:59 -0500480
481 if (param != -1)
482 return t->attr_is_visible(ISCSI_IFACE_PARAM, param);
483
Baokun Li4e285502021-09-01 16:53:36 +0800484 if (attr == &dev_attr_iface_enabled.attr)
485 param = ISCSI_NET_PARAM_IFACE_ENABLE;
486 else if (attr == &dev_attr_iface_vlan_id.attr)
Mike Christiee746f342021-06-30 19:25:59 -0500487 param = ISCSI_NET_PARAM_VLAN_ID;
488 else if (attr == &dev_attr_iface_vlan_priority.attr)
489 param = ISCSI_NET_PARAM_VLAN_PRIORITY;
490 else if (attr == &dev_attr_iface_vlan_enabled.attr)
491 param = ISCSI_NET_PARAM_VLAN_ENABLED;
492 else if (attr == &dev_attr_iface_mtu.attr)
493 param = ISCSI_NET_PARAM_MTU;
494 else if (attr == &dev_attr_iface_port.attr)
495 param = ISCSI_NET_PARAM_PORT;
496 else if (attr == &dev_attr_iface_ipaddress_state.attr)
497 param = ISCSI_NET_PARAM_IPADDR_STATE;
498 else if (attr == &dev_attr_iface_delayed_ack_en.attr)
499 param = ISCSI_NET_PARAM_DELAYED_ACK_EN;
500 else if (attr == &dev_attr_iface_tcp_nagle_disable.attr)
501 param = ISCSI_NET_PARAM_TCP_NAGLE_DISABLE;
502 else if (attr == &dev_attr_iface_tcp_wsf_disable.attr)
503 param = ISCSI_NET_PARAM_TCP_WSF_DISABLE;
504 else if (attr == &dev_attr_iface_tcp_wsf.attr)
505 param = ISCSI_NET_PARAM_TCP_WSF;
506 else if (attr == &dev_attr_iface_tcp_timer_scale.attr)
507 param = ISCSI_NET_PARAM_TCP_TIMER_SCALE;
508 else if (attr == &dev_attr_iface_tcp_timestamp_en.attr)
509 param = ISCSI_NET_PARAM_TCP_TIMESTAMP_EN;
510 else if (attr == &dev_attr_iface_cache_id.attr)
511 param = ISCSI_NET_PARAM_CACHE_ID;
512 else if (attr == &dev_attr_iface_redirect_en.attr)
513 param = ISCSI_NET_PARAM_REDIRECT_EN;
Mike Christieb78dbba2011-07-25 13:48:44 -0500514 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
Mike Christie8d079132011-07-25 13:48:40 -0500515 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500516 param = ISCSI_NET_PARAM_IPV4_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500517 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500518 param = ISCSI_NET_PARAM_IPV4_GW;
Mike Christie8d079132011-07-25 13:48:40 -0500519 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500520 param = ISCSI_NET_PARAM_IPV4_SUBNET;
Mike Christie8d079132011-07-25 13:48:40 -0500521 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500522 param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400523 else if (attr ==
524 &dev_attr_ipv4_iface_dhcp_dns_address_en.attr)
525 param = ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN;
526 else if (attr ==
527 &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr)
528 param = ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN;
529 else if (attr == &dev_attr_ipv4_iface_tos_en.attr)
530 param = ISCSI_NET_PARAM_IPV4_TOS_EN;
531 else if (attr == &dev_attr_ipv4_iface_tos.attr)
532 param = ISCSI_NET_PARAM_IPV4_TOS;
533 else if (attr == &dev_attr_ipv4_iface_grat_arp_en.attr)
534 param = ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN;
535 else if (attr ==
536 &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr)
537 param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN;
538 else if (attr == &dev_attr_ipv4_iface_dhcp_alt_client_id.attr)
539 param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID;
540 else if (attr ==
541 &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr)
542 param = ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN;
543 else if (attr ==
544 &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr)
545 param = ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN;
546 else if (attr == &dev_attr_ipv4_iface_dhcp_vendor_id.attr)
547 param = ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID;
548 else if (attr ==
549 &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr)
550 param = ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN;
551 else if (attr ==
552 &dev_attr_ipv4_iface_fragment_disable.attr)
553 param = ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE;
554 else if (attr ==
555 &dev_attr_ipv4_iface_incoming_forwarding_en.attr)
556 param = ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN;
557 else if (attr == &dev_attr_ipv4_iface_ttl.attr)
558 param = ISCSI_NET_PARAM_IPV4_TTL;
Mike Christieb78dbba2011-07-25 13:48:44 -0500559 else
560 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500561 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
562 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500563 param = ISCSI_NET_PARAM_IPV6_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500564 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500565 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
Mike Christie8d079132011-07-25 13:48:40 -0500566 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500567 param = ISCSI_NET_PARAM_IPV6_ROUTER;
Mike Christie8d079132011-07-25 13:48:40 -0500568 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500569 param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
Mike Christie88f4f512011-10-06 03:56:58 -0500570 else if (attr == &dev_attr_ipv6_iface_link_local_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500571 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400572 else if (attr == &dev_attr_ipv6_iface_link_local_state.attr)
573 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE;
574 else if (attr == &dev_attr_ipv6_iface_router_state.attr)
575 param = ISCSI_NET_PARAM_IPV6_ROUTER_STATE;
576 else if (attr ==
577 &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr)
578 param = ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN;
579 else if (attr == &dev_attr_ipv6_iface_mld_en.attr)
580 param = ISCSI_NET_PARAM_IPV6_MLD_EN;
581 else if (attr == &dev_attr_ipv6_iface_flow_label.attr)
582 param = ISCSI_NET_PARAM_IPV6_FLOW_LABEL;
583 else if (attr == &dev_attr_ipv6_iface_traffic_class.attr)
584 param = ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS;
585 else if (attr == &dev_attr_ipv6_iface_hop_limit.attr)
586 param = ISCSI_NET_PARAM_IPV6_HOP_LIMIT;
587 else if (attr == &dev_attr_ipv6_iface_nd_reachable_tmo.attr)
588 param = ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO;
589 else if (attr == &dev_attr_ipv6_iface_nd_rexmit_time.attr)
590 param = ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME;
591 else if (attr == &dev_attr_ipv6_iface_nd_stale_tmo.attr)
592 param = ISCSI_NET_PARAM_IPV6_ND_STALE_TMO;
593 else if (attr == &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr)
594 param = ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT;
595 else if (attr == &dev_attr_ipv6_iface_router_adv_link_mtu.attr)
596 param = ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU;
Mike Christieb78dbba2011-07-25 13:48:44 -0500597 else
598 return 0;
599 } else {
600 WARN_ONCE(1, "Invalid iface attr");
601 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500602 }
603
Mike Christiee746f342021-06-30 19:25:59 -0500604 return t->attr_is_visible(ISCSI_NET_PARAM, param);
Mike Christie8d079132011-07-25 13:48:40 -0500605}
606
607static struct attribute *iscsi_iface_attrs[] = {
608 &dev_attr_iface_enabled.attr,
Mike Christie2d636732011-10-11 17:55:11 -0500609 &dev_attr_iface_vlan_id.attr,
Mike Christie4223b9e2011-07-25 13:48:47 -0500610 &dev_attr_iface_vlan_priority.attr,
611 &dev_attr_iface_vlan_enabled.attr,
Mike Christie8d079132011-07-25 13:48:40 -0500612 &dev_attr_ipv4_iface_ipaddress.attr,
613 &dev_attr_ipv4_iface_gateway.attr,
614 &dev_attr_ipv4_iface_subnet.attr,
615 &dev_attr_ipv4_iface_bootproto.attr,
616 &dev_attr_ipv6_iface_ipaddress.attr,
617 &dev_attr_ipv6_iface_link_local_addr.attr,
618 &dev_attr_ipv6_iface_router_addr.attr,
619 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
Mike Christie88f4f512011-10-06 03:56:58 -0500620 &dev_attr_ipv6_iface_link_local_autocfg.attr,
Vikas Chaudhary8c7d40f2011-08-01 03:26:12 -0700621 &dev_attr_iface_mtu.attr,
Vikas Chaudharyfcb51242011-08-01 03:26:18 -0700622 &dev_attr_iface_port.attr,
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400623 &dev_attr_iface_ipaddress_state.attr,
624 &dev_attr_iface_delayed_ack_en.attr,
625 &dev_attr_iface_tcp_nagle_disable.attr,
626 &dev_attr_iface_tcp_wsf_disable.attr,
627 &dev_attr_iface_tcp_wsf.attr,
628 &dev_attr_iface_tcp_timer_scale.attr,
629 &dev_attr_iface_tcp_timestamp_en.attr,
630 &dev_attr_iface_cache_id.attr,
631 &dev_attr_iface_redirect_en.attr,
632 &dev_attr_iface_def_taskmgmt_tmo.attr,
633 &dev_attr_iface_header_digest.attr,
634 &dev_attr_iface_data_digest.attr,
635 &dev_attr_iface_immediate_data.attr,
636 &dev_attr_iface_initial_r2t.attr,
637 &dev_attr_iface_data_seq_in_order.attr,
638 &dev_attr_iface_data_pdu_in_order.attr,
639 &dev_attr_iface_erl.attr,
640 &dev_attr_iface_max_recv_dlength.attr,
641 &dev_attr_iface_first_burst_len.attr,
642 &dev_attr_iface_max_outstanding_r2t.attr,
643 &dev_attr_iface_max_burst_len.attr,
644 &dev_attr_iface_chap_auth.attr,
645 &dev_attr_iface_bidi_chap.attr,
646 &dev_attr_iface_discovery_auth_optional.attr,
647 &dev_attr_iface_discovery_logout.attr,
648 &dev_attr_iface_strict_login_comp_en.attr,
649 &dev_attr_iface_initiator_name.attr,
650 &dev_attr_ipv4_iface_dhcp_dns_address_en.attr,
651 &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr,
652 &dev_attr_ipv4_iface_tos_en.attr,
653 &dev_attr_ipv4_iface_tos.attr,
654 &dev_attr_ipv4_iface_grat_arp_en.attr,
655 &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr,
656 &dev_attr_ipv4_iface_dhcp_alt_client_id.attr,
657 &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr,
658 &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr,
659 &dev_attr_ipv4_iface_dhcp_vendor_id.attr,
660 &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr,
661 &dev_attr_ipv4_iface_fragment_disable.attr,
662 &dev_attr_ipv4_iface_incoming_forwarding_en.attr,
663 &dev_attr_ipv4_iface_ttl.attr,
664 &dev_attr_ipv6_iface_link_local_state.attr,
665 &dev_attr_ipv6_iface_router_state.attr,
666 &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr,
667 &dev_attr_ipv6_iface_mld_en.attr,
668 &dev_attr_ipv6_iface_flow_label.attr,
669 &dev_attr_ipv6_iface_traffic_class.attr,
670 &dev_attr_ipv6_iface_hop_limit.attr,
671 &dev_attr_ipv6_iface_nd_reachable_tmo.attr,
672 &dev_attr_ipv6_iface_nd_rexmit_time.attr,
673 &dev_attr_ipv6_iface_nd_stale_tmo.attr,
674 &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr,
675 &dev_attr_ipv6_iface_router_adv_link_mtu.attr,
Mike Christie8d079132011-07-25 13:48:40 -0500676 NULL,
677};
678
679static struct attribute_group iscsi_iface_group = {
680 .attrs = iscsi_iface_attrs,
681 .is_visible = iscsi_iface_attr_is_visible,
682};
683
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400684/* convert iscsi_ipaddress_state values to ascii string name */
685static const struct {
686 enum iscsi_ipaddress_state value;
687 char *name;
688} iscsi_ipaddress_state_names[] = {
689 {ISCSI_IPDDRESS_STATE_UNCONFIGURED, "Unconfigured" },
690 {ISCSI_IPDDRESS_STATE_ACQUIRING, "Acquiring" },
691 {ISCSI_IPDDRESS_STATE_TENTATIVE, "Tentative" },
692 {ISCSI_IPDDRESS_STATE_VALID, "Valid" },
693 {ISCSI_IPDDRESS_STATE_DISABLING, "Disabling" },
694 {ISCSI_IPDDRESS_STATE_INVALID, "Invalid" },
695 {ISCSI_IPDDRESS_STATE_DEPRECATED, "Deprecated" },
696};
697
698char *iscsi_get_ipaddress_state_name(enum iscsi_ipaddress_state port_state)
699{
700 int i;
701 char *state = NULL;
702
703 for (i = 0; i < ARRAY_SIZE(iscsi_ipaddress_state_names); i++) {
704 if (iscsi_ipaddress_state_names[i].value == port_state) {
705 state = iscsi_ipaddress_state_names[i].name;
706 break;
707 }
708 }
709 return state;
710}
711EXPORT_SYMBOL_GPL(iscsi_get_ipaddress_state_name);
712
713/* convert iscsi_router_state values to ascii string name */
714static const struct {
715 enum iscsi_router_state value;
716 char *name;
717} iscsi_router_state_names[] = {
718 {ISCSI_ROUTER_STATE_UNKNOWN, "Unknown" },
719 {ISCSI_ROUTER_STATE_ADVERTISED, "Advertised" },
720 {ISCSI_ROUTER_STATE_MANUAL, "Manual" },
721 {ISCSI_ROUTER_STATE_STALE, "Stale" },
722};
723
724char *iscsi_get_router_state_name(enum iscsi_router_state router_state)
725{
726 int i;
727 char *state = NULL;
728
729 for (i = 0; i < ARRAY_SIZE(iscsi_router_state_names); i++) {
730 if (iscsi_router_state_names[i].value == router_state) {
731 state = iscsi_router_state_names[i].name;
732 break;
733 }
734 }
735 return state;
736}
737EXPORT_SYMBOL_GPL(iscsi_get_router_state_name);
738
Mike Christie8d079132011-07-25 13:48:40 -0500739struct iscsi_iface *
740iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
741 uint32_t iface_type, uint32_t iface_num, int dd_size)
742{
743 struct iscsi_iface *iface;
744 int err;
745
746 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
747 if (!iface)
748 return NULL;
749
750 iface->transport = transport;
751 iface->iface_type = iface_type;
752 iface->iface_num = iface_num;
753 iface->dev.release = iscsi_iface_release;
754 iface->dev.class = &iscsi_iface_class;
755 /* parent reference released in iscsi_iface_release */
756 iface->dev.parent = get_device(&shost->shost_gendev);
757 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
758 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
759 iface_num);
760 else
761 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
762 iface_num);
763
764 err = device_register(&iface->dev);
765 if (err)
766 goto free_iface;
767
768 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
769 if (err)
770 goto unreg_iface;
771
772 if (dd_size)
773 iface->dd_data = &iface[1];
774 return iface;
775
776unreg_iface:
777 device_unregister(&iface->dev);
778 return NULL;
779
780free_iface:
781 put_device(iface->dev.parent);
782 kfree(iface);
783 return NULL;
784}
785EXPORT_SYMBOL_GPL(iscsi_create_iface);
786
787void iscsi_destroy_iface(struct iscsi_iface *iface)
788{
789 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
790 device_unregister(&iface->dev);
791}
792EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
793
Mike Christie90eeb012011-07-25 13:48:50 -0500794/*
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -0400795 * Interface to display flash node params to sysfs
796 */
797
798#define ISCSI_FLASHNODE_ATTR(_prefix, _name, _mode, _show, _store) \
799struct device_attribute dev_attr_##_prefix##_##_name = \
800 __ATTR(_name, _mode, _show, _store)
801
802/* flash node session attrs show */
803#define iscsi_flashnode_sess_attr_show(type, name, param) \
804static ssize_t \
805show_##type##_##name(struct device *dev, struct device_attribute *attr, \
806 char *buf) \
807{ \
808 struct iscsi_bus_flash_session *fnode_sess = \
809 iscsi_dev_to_flash_session(dev);\
810 struct iscsi_transport *t = fnode_sess->transport; \
811 return t->get_flashnode_param(fnode_sess, param, buf); \
812} \
813
814
815#define iscsi_flashnode_sess_attr(type, name, param) \
816 iscsi_flashnode_sess_attr_show(type, name, param) \
817static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
818 show_##type##_##name, NULL);
819
820/* Flash node session attributes */
821
822iscsi_flashnode_sess_attr(fnode, auto_snd_tgt_disable,
823 ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE);
824iscsi_flashnode_sess_attr(fnode, discovery_session,
825 ISCSI_FLASHNODE_DISCOVERY_SESS);
826iscsi_flashnode_sess_attr(fnode, portal_type, ISCSI_FLASHNODE_PORTAL_TYPE);
827iscsi_flashnode_sess_attr(fnode, entry_enable, ISCSI_FLASHNODE_ENTRY_EN);
828iscsi_flashnode_sess_attr(fnode, immediate_data, ISCSI_FLASHNODE_IMM_DATA_EN);
829iscsi_flashnode_sess_attr(fnode, initial_r2t, ISCSI_FLASHNODE_INITIAL_R2T_EN);
830iscsi_flashnode_sess_attr(fnode, data_seq_in_order,
831 ISCSI_FLASHNODE_DATASEQ_INORDER);
832iscsi_flashnode_sess_attr(fnode, data_pdu_in_order,
833 ISCSI_FLASHNODE_PDU_INORDER);
834iscsi_flashnode_sess_attr(fnode, chap_auth, ISCSI_FLASHNODE_CHAP_AUTH_EN);
835iscsi_flashnode_sess_attr(fnode, discovery_logout,
836 ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN);
837iscsi_flashnode_sess_attr(fnode, bidi_chap, ISCSI_FLASHNODE_BIDI_CHAP_EN);
838iscsi_flashnode_sess_attr(fnode, discovery_auth_optional,
839 ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL);
840iscsi_flashnode_sess_attr(fnode, erl, ISCSI_FLASHNODE_ERL);
841iscsi_flashnode_sess_attr(fnode, first_burst_len, ISCSI_FLASHNODE_FIRST_BURST);
842iscsi_flashnode_sess_attr(fnode, def_time2wait, ISCSI_FLASHNODE_DEF_TIME2WAIT);
843iscsi_flashnode_sess_attr(fnode, def_time2retain,
844 ISCSI_FLASHNODE_DEF_TIME2RETAIN);
845iscsi_flashnode_sess_attr(fnode, max_outstanding_r2t, ISCSI_FLASHNODE_MAX_R2T);
846iscsi_flashnode_sess_attr(fnode, isid, ISCSI_FLASHNODE_ISID);
847iscsi_flashnode_sess_attr(fnode, tsid, ISCSI_FLASHNODE_TSID);
848iscsi_flashnode_sess_attr(fnode, max_burst_len, ISCSI_FLASHNODE_MAX_BURST);
849iscsi_flashnode_sess_attr(fnode, def_taskmgmt_tmo,
850 ISCSI_FLASHNODE_DEF_TASKMGMT_TMO);
851iscsi_flashnode_sess_attr(fnode, targetalias, ISCSI_FLASHNODE_ALIAS);
852iscsi_flashnode_sess_attr(fnode, targetname, ISCSI_FLASHNODE_NAME);
853iscsi_flashnode_sess_attr(fnode, tpgt, ISCSI_FLASHNODE_TPGT);
854iscsi_flashnode_sess_attr(fnode, discovery_parent_idx,
855 ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX);
856iscsi_flashnode_sess_attr(fnode, discovery_parent_type,
857 ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE);
858iscsi_flashnode_sess_attr(fnode, chap_in_idx, ISCSI_FLASHNODE_CHAP_IN_IDX);
859iscsi_flashnode_sess_attr(fnode, chap_out_idx, ISCSI_FLASHNODE_CHAP_OUT_IDX);
860iscsi_flashnode_sess_attr(fnode, username, ISCSI_FLASHNODE_USERNAME);
861iscsi_flashnode_sess_attr(fnode, username_in, ISCSI_FLASHNODE_USERNAME_IN);
862iscsi_flashnode_sess_attr(fnode, password, ISCSI_FLASHNODE_PASSWORD);
863iscsi_flashnode_sess_attr(fnode, password_in, ISCSI_FLASHNODE_PASSWORD_IN);
864iscsi_flashnode_sess_attr(fnode, is_boot_target, ISCSI_FLASHNODE_IS_BOOT_TGT);
865
866static struct attribute *iscsi_flashnode_sess_attrs[] = {
867 &dev_attr_fnode_auto_snd_tgt_disable.attr,
868 &dev_attr_fnode_discovery_session.attr,
869 &dev_attr_fnode_portal_type.attr,
870 &dev_attr_fnode_entry_enable.attr,
871 &dev_attr_fnode_immediate_data.attr,
872 &dev_attr_fnode_initial_r2t.attr,
873 &dev_attr_fnode_data_seq_in_order.attr,
874 &dev_attr_fnode_data_pdu_in_order.attr,
875 &dev_attr_fnode_chap_auth.attr,
876 &dev_attr_fnode_discovery_logout.attr,
877 &dev_attr_fnode_bidi_chap.attr,
878 &dev_attr_fnode_discovery_auth_optional.attr,
879 &dev_attr_fnode_erl.attr,
880 &dev_attr_fnode_first_burst_len.attr,
881 &dev_attr_fnode_def_time2wait.attr,
882 &dev_attr_fnode_def_time2retain.attr,
883 &dev_attr_fnode_max_outstanding_r2t.attr,
884 &dev_attr_fnode_isid.attr,
885 &dev_attr_fnode_tsid.attr,
886 &dev_attr_fnode_max_burst_len.attr,
887 &dev_attr_fnode_def_taskmgmt_tmo.attr,
888 &dev_attr_fnode_targetalias.attr,
889 &dev_attr_fnode_targetname.attr,
890 &dev_attr_fnode_tpgt.attr,
891 &dev_attr_fnode_discovery_parent_idx.attr,
892 &dev_attr_fnode_discovery_parent_type.attr,
893 &dev_attr_fnode_chap_in_idx.attr,
894 &dev_attr_fnode_chap_out_idx.attr,
895 &dev_attr_fnode_username.attr,
896 &dev_attr_fnode_username_in.attr,
897 &dev_attr_fnode_password.attr,
898 &dev_attr_fnode_password_in.attr,
899 &dev_attr_fnode_is_boot_target.attr,
900 NULL,
901};
902
903static umode_t iscsi_flashnode_sess_attr_is_visible(struct kobject *kobj,
904 struct attribute *attr,
905 int i)
906{
907 struct device *dev = container_of(kobj, struct device, kobj);
908 struct iscsi_bus_flash_session *fnode_sess =
909 iscsi_dev_to_flash_session(dev);
910 struct iscsi_transport *t = fnode_sess->transport;
911 int param;
912
913 if (attr == &dev_attr_fnode_auto_snd_tgt_disable.attr) {
914 param = ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE;
915 } else if (attr == &dev_attr_fnode_discovery_session.attr) {
916 param = ISCSI_FLASHNODE_DISCOVERY_SESS;
917 } else if (attr == &dev_attr_fnode_portal_type.attr) {
918 param = ISCSI_FLASHNODE_PORTAL_TYPE;
919 } else if (attr == &dev_attr_fnode_entry_enable.attr) {
920 param = ISCSI_FLASHNODE_ENTRY_EN;
921 } else if (attr == &dev_attr_fnode_immediate_data.attr) {
922 param = ISCSI_FLASHNODE_IMM_DATA_EN;
923 } else if (attr == &dev_attr_fnode_initial_r2t.attr) {
924 param = ISCSI_FLASHNODE_INITIAL_R2T_EN;
925 } else if (attr == &dev_attr_fnode_data_seq_in_order.attr) {
926 param = ISCSI_FLASHNODE_DATASEQ_INORDER;
927 } else if (attr == &dev_attr_fnode_data_pdu_in_order.attr) {
928 param = ISCSI_FLASHNODE_PDU_INORDER;
929 } else if (attr == &dev_attr_fnode_chap_auth.attr) {
930 param = ISCSI_FLASHNODE_CHAP_AUTH_EN;
931 } else if (attr == &dev_attr_fnode_discovery_logout.attr) {
932 param = ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN;
933 } else if (attr == &dev_attr_fnode_bidi_chap.attr) {
934 param = ISCSI_FLASHNODE_BIDI_CHAP_EN;
935 } else if (attr == &dev_attr_fnode_discovery_auth_optional.attr) {
936 param = ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL;
937 } else if (attr == &dev_attr_fnode_erl.attr) {
938 param = ISCSI_FLASHNODE_ERL;
939 } else if (attr == &dev_attr_fnode_first_burst_len.attr) {
940 param = ISCSI_FLASHNODE_FIRST_BURST;
941 } else if (attr == &dev_attr_fnode_def_time2wait.attr) {
942 param = ISCSI_FLASHNODE_DEF_TIME2WAIT;
943 } else if (attr == &dev_attr_fnode_def_time2retain.attr) {
944 param = ISCSI_FLASHNODE_DEF_TIME2RETAIN;
945 } else if (attr == &dev_attr_fnode_max_outstanding_r2t.attr) {
946 param = ISCSI_FLASHNODE_MAX_R2T;
947 } else if (attr == &dev_attr_fnode_isid.attr) {
948 param = ISCSI_FLASHNODE_ISID;
949 } else if (attr == &dev_attr_fnode_tsid.attr) {
950 param = ISCSI_FLASHNODE_TSID;
951 } else if (attr == &dev_attr_fnode_max_burst_len.attr) {
952 param = ISCSI_FLASHNODE_MAX_BURST;
953 } else if (attr == &dev_attr_fnode_def_taskmgmt_tmo.attr) {
954 param = ISCSI_FLASHNODE_DEF_TASKMGMT_TMO;
955 } else if (attr == &dev_attr_fnode_targetalias.attr) {
956 param = ISCSI_FLASHNODE_ALIAS;
957 } else if (attr == &dev_attr_fnode_targetname.attr) {
958 param = ISCSI_FLASHNODE_NAME;
959 } else if (attr == &dev_attr_fnode_tpgt.attr) {
960 param = ISCSI_FLASHNODE_TPGT;
961 } else if (attr == &dev_attr_fnode_discovery_parent_idx.attr) {
962 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX;
963 } else if (attr == &dev_attr_fnode_discovery_parent_type.attr) {
964 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE;
965 } else if (attr == &dev_attr_fnode_chap_in_idx.attr) {
966 param = ISCSI_FLASHNODE_CHAP_IN_IDX;
967 } else if (attr == &dev_attr_fnode_chap_out_idx.attr) {
968 param = ISCSI_FLASHNODE_CHAP_OUT_IDX;
969 } else if (attr == &dev_attr_fnode_username.attr) {
970 param = ISCSI_FLASHNODE_USERNAME;
971 } else if (attr == &dev_attr_fnode_username_in.attr) {
972 param = ISCSI_FLASHNODE_USERNAME_IN;
973 } else if (attr == &dev_attr_fnode_password.attr) {
974 param = ISCSI_FLASHNODE_PASSWORD;
975 } else if (attr == &dev_attr_fnode_password_in.attr) {
976 param = ISCSI_FLASHNODE_PASSWORD_IN;
977 } else if (attr == &dev_attr_fnode_is_boot_target.attr) {
978 param = ISCSI_FLASHNODE_IS_BOOT_TGT;
979 } else {
980 WARN_ONCE(1, "Invalid flashnode session attr");
981 return 0;
982 }
983
984 return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
985}
986
987static struct attribute_group iscsi_flashnode_sess_attr_group = {
988 .attrs = iscsi_flashnode_sess_attrs,
989 .is_visible = iscsi_flashnode_sess_attr_is_visible,
990};
991
992static const struct attribute_group *iscsi_flashnode_sess_attr_groups[] = {
993 &iscsi_flashnode_sess_attr_group,
994 NULL,
995};
996
997static void iscsi_flashnode_sess_release(struct device *dev)
998{
999 struct iscsi_bus_flash_session *fnode_sess =
1000 iscsi_dev_to_flash_session(dev);
1001
1002 kfree(fnode_sess->targetname);
1003 kfree(fnode_sess->targetalias);
1004 kfree(fnode_sess->portal_type);
1005 kfree(fnode_sess);
1006}
1007
Bhumika Goyal7988faf2017-08-19 13:52:24 +05301008static const struct device_type iscsi_flashnode_sess_dev_type = {
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001009 .name = "iscsi_flashnode_sess_dev_type",
1010 .groups = iscsi_flashnode_sess_attr_groups,
1011 .release = iscsi_flashnode_sess_release,
1012};
1013
1014/* flash node connection attrs show */
1015#define iscsi_flashnode_conn_attr_show(type, name, param) \
1016static ssize_t \
1017show_##type##_##name(struct device *dev, struct device_attribute *attr, \
1018 char *buf) \
1019{ \
1020 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);\
1021 struct iscsi_bus_flash_session *fnode_sess = \
1022 iscsi_flash_conn_to_flash_session(fnode_conn);\
1023 struct iscsi_transport *t = fnode_conn->transport; \
1024 return t->get_flashnode_param(fnode_sess, param, buf); \
1025} \
1026
1027
1028#define iscsi_flashnode_conn_attr(type, name, param) \
1029 iscsi_flashnode_conn_attr_show(type, name, param) \
1030static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
1031 show_##type##_##name, NULL);
1032
1033/* Flash node connection attributes */
1034
1035iscsi_flashnode_conn_attr(fnode, is_fw_assigned_ipv6,
1036 ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6);
1037iscsi_flashnode_conn_attr(fnode, header_digest, ISCSI_FLASHNODE_HDR_DGST_EN);
1038iscsi_flashnode_conn_attr(fnode, data_digest, ISCSI_FLASHNODE_DATA_DGST_EN);
1039iscsi_flashnode_conn_attr(fnode, snack_req, ISCSI_FLASHNODE_SNACK_REQ_EN);
1040iscsi_flashnode_conn_attr(fnode, tcp_timestamp_stat,
1041 ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT);
1042iscsi_flashnode_conn_attr(fnode, tcp_nagle_disable,
1043 ISCSI_FLASHNODE_TCP_NAGLE_DISABLE);
1044iscsi_flashnode_conn_attr(fnode, tcp_wsf_disable,
1045 ISCSI_FLASHNODE_TCP_WSF_DISABLE);
1046iscsi_flashnode_conn_attr(fnode, tcp_timer_scale,
1047 ISCSI_FLASHNODE_TCP_TIMER_SCALE);
1048iscsi_flashnode_conn_attr(fnode, tcp_timestamp_enable,
1049 ISCSI_FLASHNODE_TCP_TIMESTAMP_EN);
1050iscsi_flashnode_conn_attr(fnode, fragment_disable,
1051 ISCSI_FLASHNODE_IP_FRAG_DISABLE);
1052iscsi_flashnode_conn_attr(fnode, keepalive_tmo, ISCSI_FLASHNODE_KEEPALIVE_TMO);
1053iscsi_flashnode_conn_attr(fnode, port, ISCSI_FLASHNODE_PORT);
1054iscsi_flashnode_conn_attr(fnode, ipaddress, ISCSI_FLASHNODE_IPADDR);
1055iscsi_flashnode_conn_attr(fnode, max_recv_dlength,
1056 ISCSI_FLASHNODE_MAX_RECV_DLENGTH);
1057iscsi_flashnode_conn_attr(fnode, max_xmit_dlength,
1058 ISCSI_FLASHNODE_MAX_XMIT_DLENGTH);
1059iscsi_flashnode_conn_attr(fnode, local_port, ISCSI_FLASHNODE_LOCAL_PORT);
1060iscsi_flashnode_conn_attr(fnode, ipv4_tos, ISCSI_FLASHNODE_IPV4_TOS);
1061iscsi_flashnode_conn_attr(fnode, ipv6_traffic_class, ISCSI_FLASHNODE_IPV6_TC);
1062iscsi_flashnode_conn_attr(fnode, ipv6_flow_label,
1063 ISCSI_FLASHNODE_IPV6_FLOW_LABEL);
1064iscsi_flashnode_conn_attr(fnode, redirect_ipaddr,
1065 ISCSI_FLASHNODE_REDIRECT_IPADDR);
1066iscsi_flashnode_conn_attr(fnode, max_segment_size,
1067 ISCSI_FLASHNODE_MAX_SEGMENT_SIZE);
1068iscsi_flashnode_conn_attr(fnode, link_local_ipv6,
1069 ISCSI_FLASHNODE_LINK_LOCAL_IPV6);
1070iscsi_flashnode_conn_attr(fnode, tcp_xmit_wsf, ISCSI_FLASHNODE_TCP_XMIT_WSF);
1071iscsi_flashnode_conn_attr(fnode, tcp_recv_wsf, ISCSI_FLASHNODE_TCP_RECV_WSF);
1072iscsi_flashnode_conn_attr(fnode, statsn, ISCSI_FLASHNODE_STATSN);
1073iscsi_flashnode_conn_attr(fnode, exp_statsn, ISCSI_FLASHNODE_EXP_STATSN);
1074
1075static struct attribute *iscsi_flashnode_conn_attrs[] = {
1076 &dev_attr_fnode_is_fw_assigned_ipv6.attr,
1077 &dev_attr_fnode_header_digest.attr,
1078 &dev_attr_fnode_data_digest.attr,
1079 &dev_attr_fnode_snack_req.attr,
1080 &dev_attr_fnode_tcp_timestamp_stat.attr,
1081 &dev_attr_fnode_tcp_nagle_disable.attr,
1082 &dev_attr_fnode_tcp_wsf_disable.attr,
1083 &dev_attr_fnode_tcp_timer_scale.attr,
1084 &dev_attr_fnode_tcp_timestamp_enable.attr,
1085 &dev_attr_fnode_fragment_disable.attr,
1086 &dev_attr_fnode_max_recv_dlength.attr,
1087 &dev_attr_fnode_max_xmit_dlength.attr,
1088 &dev_attr_fnode_keepalive_tmo.attr,
1089 &dev_attr_fnode_port.attr,
1090 &dev_attr_fnode_ipaddress.attr,
1091 &dev_attr_fnode_redirect_ipaddr.attr,
1092 &dev_attr_fnode_max_segment_size.attr,
1093 &dev_attr_fnode_local_port.attr,
1094 &dev_attr_fnode_ipv4_tos.attr,
1095 &dev_attr_fnode_ipv6_traffic_class.attr,
1096 &dev_attr_fnode_ipv6_flow_label.attr,
1097 &dev_attr_fnode_link_local_ipv6.attr,
1098 &dev_attr_fnode_tcp_xmit_wsf.attr,
1099 &dev_attr_fnode_tcp_recv_wsf.attr,
1100 &dev_attr_fnode_statsn.attr,
1101 &dev_attr_fnode_exp_statsn.attr,
1102 NULL,
1103};
1104
1105static umode_t iscsi_flashnode_conn_attr_is_visible(struct kobject *kobj,
1106 struct attribute *attr,
1107 int i)
1108{
1109 struct device *dev = container_of(kobj, struct device, kobj);
1110 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
1111 struct iscsi_transport *t = fnode_conn->transport;
1112 int param;
1113
1114 if (attr == &dev_attr_fnode_is_fw_assigned_ipv6.attr) {
1115 param = ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6;
1116 } else if (attr == &dev_attr_fnode_header_digest.attr) {
1117 param = ISCSI_FLASHNODE_HDR_DGST_EN;
1118 } else if (attr == &dev_attr_fnode_data_digest.attr) {
1119 param = ISCSI_FLASHNODE_DATA_DGST_EN;
1120 } else if (attr == &dev_attr_fnode_snack_req.attr) {
1121 param = ISCSI_FLASHNODE_SNACK_REQ_EN;
1122 } else if (attr == &dev_attr_fnode_tcp_timestamp_stat.attr) {
1123 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT;
1124 } else if (attr == &dev_attr_fnode_tcp_nagle_disable.attr) {
1125 param = ISCSI_FLASHNODE_TCP_NAGLE_DISABLE;
1126 } else if (attr == &dev_attr_fnode_tcp_wsf_disable.attr) {
1127 param = ISCSI_FLASHNODE_TCP_WSF_DISABLE;
1128 } else if (attr == &dev_attr_fnode_tcp_timer_scale.attr) {
1129 param = ISCSI_FLASHNODE_TCP_TIMER_SCALE;
1130 } else if (attr == &dev_attr_fnode_tcp_timestamp_enable.attr) {
1131 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_EN;
1132 } else if (attr == &dev_attr_fnode_fragment_disable.attr) {
1133 param = ISCSI_FLASHNODE_IP_FRAG_DISABLE;
1134 } else if (attr == &dev_attr_fnode_max_recv_dlength.attr) {
1135 param = ISCSI_FLASHNODE_MAX_RECV_DLENGTH;
1136 } else if (attr == &dev_attr_fnode_max_xmit_dlength.attr) {
1137 param = ISCSI_FLASHNODE_MAX_XMIT_DLENGTH;
1138 } else if (attr == &dev_attr_fnode_keepalive_tmo.attr) {
1139 param = ISCSI_FLASHNODE_KEEPALIVE_TMO;
1140 } else if (attr == &dev_attr_fnode_port.attr) {
1141 param = ISCSI_FLASHNODE_PORT;
1142 } else if (attr == &dev_attr_fnode_ipaddress.attr) {
1143 param = ISCSI_FLASHNODE_IPADDR;
1144 } else if (attr == &dev_attr_fnode_redirect_ipaddr.attr) {
1145 param = ISCSI_FLASHNODE_REDIRECT_IPADDR;
1146 } else if (attr == &dev_attr_fnode_max_segment_size.attr) {
1147 param = ISCSI_FLASHNODE_MAX_SEGMENT_SIZE;
1148 } else if (attr == &dev_attr_fnode_local_port.attr) {
1149 param = ISCSI_FLASHNODE_LOCAL_PORT;
1150 } else if (attr == &dev_attr_fnode_ipv4_tos.attr) {
1151 param = ISCSI_FLASHNODE_IPV4_TOS;
1152 } else if (attr == &dev_attr_fnode_ipv6_traffic_class.attr) {
1153 param = ISCSI_FLASHNODE_IPV6_TC;
1154 } else if (attr == &dev_attr_fnode_ipv6_flow_label.attr) {
1155 param = ISCSI_FLASHNODE_IPV6_FLOW_LABEL;
1156 } else if (attr == &dev_attr_fnode_link_local_ipv6.attr) {
1157 param = ISCSI_FLASHNODE_LINK_LOCAL_IPV6;
1158 } else if (attr == &dev_attr_fnode_tcp_xmit_wsf.attr) {
1159 param = ISCSI_FLASHNODE_TCP_XMIT_WSF;
1160 } else if (attr == &dev_attr_fnode_tcp_recv_wsf.attr) {
1161 param = ISCSI_FLASHNODE_TCP_RECV_WSF;
1162 } else if (attr == &dev_attr_fnode_statsn.attr) {
1163 param = ISCSI_FLASHNODE_STATSN;
1164 } else if (attr == &dev_attr_fnode_exp_statsn.attr) {
1165 param = ISCSI_FLASHNODE_EXP_STATSN;
1166 } else {
1167 WARN_ONCE(1, "Invalid flashnode connection attr");
1168 return 0;
1169 }
1170
1171 return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
1172}
1173
1174static struct attribute_group iscsi_flashnode_conn_attr_group = {
1175 .attrs = iscsi_flashnode_conn_attrs,
1176 .is_visible = iscsi_flashnode_conn_attr_is_visible,
1177};
1178
1179static const struct attribute_group *iscsi_flashnode_conn_attr_groups[] = {
1180 &iscsi_flashnode_conn_attr_group,
1181 NULL,
1182};
1183
1184static void iscsi_flashnode_conn_release(struct device *dev)
1185{
1186 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
1187
1188 kfree(fnode_conn->ipaddress);
1189 kfree(fnode_conn->redirect_ipaddr);
1190 kfree(fnode_conn->link_local_ipv6_addr);
1191 kfree(fnode_conn);
1192}
1193
Bhumika Goyal7988faf2017-08-19 13:52:24 +05301194static const struct device_type iscsi_flashnode_conn_dev_type = {
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001195 .name = "iscsi_flashnode_conn_dev_type",
1196 .groups = iscsi_flashnode_conn_attr_groups,
1197 .release = iscsi_flashnode_conn_release,
1198};
1199
Bart Van Assche4c1340a2016-03-30 11:28:11 -07001200static struct bus_type iscsi_flashnode_bus;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001201
1202int iscsi_flashnode_bus_match(struct device *dev,
1203 struct device_driver *drv)
1204{
1205 if (dev->bus == &iscsi_flashnode_bus)
1206 return 1;
1207 return 0;
1208}
1209EXPORT_SYMBOL_GPL(iscsi_flashnode_bus_match);
1210
Bart Van Assche4c1340a2016-03-30 11:28:11 -07001211static struct bus_type iscsi_flashnode_bus = {
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001212 .name = "iscsi_flashnode",
1213 .match = &iscsi_flashnode_bus_match,
1214};
1215
1216/**
1217 * iscsi_create_flashnode_sess - Add flashnode session entry in sysfs
1218 * @shost: pointer to host data
1219 * @index: index of flashnode to add in sysfs
1220 * @transport: pointer to transport data
1221 * @dd_size: total size to allocate
1222 *
1223 * Adds a sysfs entry for the flashnode session attributes
1224 *
1225 * Returns:
Masanari Iidae2278672014-02-18 22:54:36 +09001226 * pointer to allocated flashnode sess on success
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001227 * %NULL on failure
1228 */
1229struct iscsi_bus_flash_session *
1230iscsi_create_flashnode_sess(struct Scsi_Host *shost, int index,
1231 struct iscsi_transport *transport,
1232 int dd_size)
1233{
1234 struct iscsi_bus_flash_session *fnode_sess;
1235 int err;
1236
1237 fnode_sess = kzalloc(sizeof(*fnode_sess) + dd_size, GFP_KERNEL);
1238 if (!fnode_sess)
1239 return NULL;
1240
1241 fnode_sess->transport = transport;
1242 fnode_sess->target_id = index;
1243 fnode_sess->dev.type = &iscsi_flashnode_sess_dev_type;
1244 fnode_sess->dev.bus = &iscsi_flashnode_bus;
1245 fnode_sess->dev.parent = &shost->shost_gendev;
1246 dev_set_name(&fnode_sess->dev, "flashnode_sess-%u:%u",
1247 shost->host_no, index);
1248
1249 err = device_register(&fnode_sess->dev);
1250 if (err)
1251 goto free_fnode_sess;
1252
1253 if (dd_size)
1254 fnode_sess->dd_data = &fnode_sess[1];
1255
1256 return fnode_sess;
1257
1258free_fnode_sess:
1259 kfree(fnode_sess);
1260 return NULL;
1261}
1262EXPORT_SYMBOL_GPL(iscsi_create_flashnode_sess);
1263
1264/**
1265 * iscsi_create_flashnode_conn - Add flashnode conn entry in sysfs
1266 * @shost: pointer to host data
1267 * @fnode_sess: pointer to the parent flashnode session entry
1268 * @transport: pointer to transport data
1269 * @dd_size: total size to allocate
1270 *
1271 * Adds a sysfs entry for the flashnode connection attributes
1272 *
1273 * Returns:
1274 * pointer to allocated flashnode conn on success
1275 * %NULL on failure
1276 */
1277struct iscsi_bus_flash_conn *
1278iscsi_create_flashnode_conn(struct Scsi_Host *shost,
1279 struct iscsi_bus_flash_session *fnode_sess,
1280 struct iscsi_transport *transport,
1281 int dd_size)
1282{
1283 struct iscsi_bus_flash_conn *fnode_conn;
1284 int err;
1285
1286 fnode_conn = kzalloc(sizeof(*fnode_conn) + dd_size, GFP_KERNEL);
1287 if (!fnode_conn)
1288 return NULL;
1289
1290 fnode_conn->transport = transport;
1291 fnode_conn->dev.type = &iscsi_flashnode_conn_dev_type;
1292 fnode_conn->dev.bus = &iscsi_flashnode_bus;
1293 fnode_conn->dev.parent = &fnode_sess->dev;
1294 dev_set_name(&fnode_conn->dev, "flashnode_conn-%u:%u:0",
1295 shost->host_no, fnode_sess->target_id);
1296
1297 err = device_register(&fnode_conn->dev);
1298 if (err)
1299 goto free_fnode_conn;
1300
1301 if (dd_size)
1302 fnode_conn->dd_data = &fnode_conn[1];
1303
1304 return fnode_conn;
1305
1306free_fnode_conn:
1307 kfree(fnode_conn);
1308 return NULL;
1309}
1310EXPORT_SYMBOL_GPL(iscsi_create_flashnode_conn);
1311
1312/**
1313 * iscsi_is_flashnode_conn_dev - verify passed device is to be flashnode conn
1314 * @dev: device to verify
1315 * @data: pointer to data containing value to use for verification
1316 *
1317 * Verifies if the passed device is flashnode conn device
1318 *
1319 * Returns:
1320 * 1 on success
1321 * 0 on failure
1322 */
Bart Van Assche7a15fdf2016-03-30 11:27:47 -07001323static int iscsi_is_flashnode_conn_dev(struct device *dev, void *data)
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001324{
1325 return dev->bus == &iscsi_flashnode_bus;
1326}
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001327
1328static int iscsi_destroy_flashnode_conn(struct iscsi_bus_flash_conn *fnode_conn)
1329{
1330 device_unregister(&fnode_conn->dev);
1331 return 0;
1332}
1333
1334static int flashnode_match_index(struct device *dev, void *data)
1335{
1336 struct iscsi_bus_flash_session *fnode_sess = NULL;
1337 int ret = 0;
1338
1339 if (!iscsi_flashnode_bus_match(dev, NULL))
1340 goto exit_match_index;
1341
1342 fnode_sess = iscsi_dev_to_flash_session(dev);
1343 ret = (fnode_sess->target_id == *((int *)data)) ? 1 : 0;
1344
1345exit_match_index:
1346 return ret;
1347}
1348
1349/**
1350 * iscsi_get_flashnode_by_index -finds flashnode session entry by index
1351 * @shost: pointer to host data
Mike Christie8526cb12013-05-06 12:06:56 -05001352 * @idx: index to match
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001353 *
1354 * Finds the flashnode session object for the passed index
1355 *
1356 * Returns:
1357 * pointer to found flashnode session object on success
1358 * %NULL on failure
1359 */
1360static struct iscsi_bus_flash_session *
Mike Christie8526cb12013-05-06 12:06:56 -05001361iscsi_get_flashnode_by_index(struct Scsi_Host *shost, uint32_t idx)
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001362{
1363 struct iscsi_bus_flash_session *fnode_sess = NULL;
1364 struct device *dev;
1365
Mike Christie8526cb12013-05-06 12:06:56 -05001366 dev = device_find_child(&shost->shost_gendev, &idx,
1367 flashnode_match_index);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001368 if (dev)
1369 fnode_sess = iscsi_dev_to_flash_session(dev);
1370
1371 return fnode_sess;
1372}
1373
1374/**
1375 * iscsi_find_flashnode_sess - finds flashnode session entry
1376 * @shost: pointer to host data
1377 * @data: pointer to data containing value to use for comparison
1378 * @fn: function pointer that does actual comparison
1379 *
1380 * Finds the flashnode session object comparing the data passed using logic
1381 * defined in passed function pointer
1382 *
1383 * Returns:
1384 * pointer to found flashnode session device object on success
1385 * %NULL on failure
1386 */
1387struct device *
1388iscsi_find_flashnode_sess(struct Scsi_Host *shost, void *data,
1389 int (*fn)(struct device *dev, void *data))
1390{
Mike Christie8526cb12013-05-06 12:06:56 -05001391 return device_find_child(&shost->shost_gendev, data, fn);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001392}
1393EXPORT_SYMBOL_GPL(iscsi_find_flashnode_sess);
1394
1395/**
1396 * iscsi_find_flashnode_conn - finds flashnode connection entry
1397 * @fnode_sess: pointer to parent flashnode session entry
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001398 *
1399 * Finds the flashnode connection object comparing the data passed using logic
1400 * defined in passed function pointer
1401 *
1402 * Returns:
1403 * pointer to found flashnode connection device object on success
1404 * %NULL on failure
1405 */
1406struct device *
Mike Christie8526cb12013-05-06 12:06:56 -05001407iscsi_find_flashnode_conn(struct iscsi_bus_flash_session *fnode_sess)
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001408{
Mike Christie8526cb12013-05-06 12:06:56 -05001409 return device_find_child(&fnode_sess->dev, NULL,
1410 iscsi_is_flashnode_conn_dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001411}
1412EXPORT_SYMBOL_GPL(iscsi_find_flashnode_conn);
1413
1414static int iscsi_iter_destroy_flashnode_conn_fn(struct device *dev, void *data)
1415{
1416 if (!iscsi_is_flashnode_conn_dev(dev, NULL))
1417 return 0;
1418
1419 return iscsi_destroy_flashnode_conn(iscsi_dev_to_flash_conn(dev));
1420}
1421
1422/**
Masanari Iidae2278672014-02-18 22:54:36 +09001423 * iscsi_destroy_flashnode_sess - destroy flashnode session entry
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001424 * @fnode_sess: pointer to flashnode session entry to be destroyed
1425 *
1426 * Deletes the flashnode session entry and all children flashnode connection
1427 * entries from sysfs
1428 */
1429void iscsi_destroy_flashnode_sess(struct iscsi_bus_flash_session *fnode_sess)
1430{
1431 int err;
1432
1433 err = device_for_each_child(&fnode_sess->dev, NULL,
1434 iscsi_iter_destroy_flashnode_conn_fn);
1435 if (err)
1436 pr_err("Could not delete all connections for %s. Error %d.\n",
1437 fnode_sess->dev.kobj.name, err);
1438
1439 device_unregister(&fnode_sess->dev);
1440}
1441EXPORT_SYMBOL_GPL(iscsi_destroy_flashnode_sess);
1442
1443static int iscsi_iter_destroy_flashnode_fn(struct device *dev, void *data)
1444{
1445 if (!iscsi_flashnode_bus_match(dev, NULL))
1446 return 0;
1447
1448 iscsi_destroy_flashnode_sess(iscsi_dev_to_flash_session(dev));
1449 return 0;
1450}
1451
1452/**
Masanari Iidae2278672014-02-18 22:54:36 +09001453 * iscsi_destroy_all_flashnode - destroy all flashnode session entries
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001454 * @shost: pointer to host data
1455 *
1456 * Destroys all the flashnode session entries and all corresponding children
1457 * flashnode connection entries from sysfs
1458 */
1459void iscsi_destroy_all_flashnode(struct Scsi_Host *shost)
1460{
1461 device_for_each_child(&shost->shost_gendev, NULL,
1462 iscsi_iter_destroy_flashnode_fn);
1463}
1464EXPORT_SYMBOL_GPL(iscsi_destroy_all_flashnode);
1465
1466/*
Mike Christie90eeb012011-07-25 13:48:50 -05001467 * BSG support
1468 */
1469/**
1470 * iscsi_bsg_host_dispatch - Dispatch command to LLD.
1471 * @job: bsg job to be processed
1472 */
1473static int iscsi_bsg_host_dispatch(struct bsg_job *job)
1474{
1475 struct Scsi_Host *shost = iscsi_job_to_shost(job);
1476 struct iscsi_bsg_request *req = job->request;
1477 struct iscsi_bsg_reply *reply = job->reply;
1478 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1479 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
1480 int ret;
1481
1482 /* check if we have the msgcode value at least */
1483 if (job->request_len < sizeof(uint32_t)) {
1484 ret = -ENOMSG;
1485 goto fail_host_msg;
1486 }
1487
1488 /* Validate the host command */
1489 switch (req->msgcode) {
1490 case ISCSI_BSG_HST_VENDOR:
1491 cmdlen += sizeof(struct iscsi_bsg_host_vendor);
1492 if ((shost->hostt->vendor_id == 0L) ||
1493 (req->rqst_data.h_vendor.vendor_id !=
1494 shost->hostt->vendor_id)) {
1495 ret = -ESRCH;
1496 goto fail_host_msg;
1497 }
1498 break;
1499 default:
1500 ret = -EBADR;
1501 goto fail_host_msg;
1502 }
1503
1504 /* check if we really have all the request data needed */
1505 if (job->request_len < cmdlen) {
1506 ret = -ENOMSG;
1507 goto fail_host_msg;
1508 }
1509
1510 ret = i->iscsi_transport->bsg_request(job);
1511 if (!ret)
1512 return 0;
1513
1514fail_host_msg:
1515 /* return the errno failure code as the only status */
1516 BUG_ON(job->reply_len < sizeof(uint32_t));
1517 reply->reply_payload_rcv_len = 0;
1518 reply->result = ret;
1519 job->reply_len = sizeof(uint32_t);
1520 bsg_job_done(job, ret, 0);
1521 return 0;
1522}
1523
1524/**
1525 * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
1526 * @shost: shost for iscsi_host
Marcos Paulo de Souzaeae627e2011-11-02 11:17:17 -02001527 * @ihost: iscsi_cls_host adding the structures to
Mike Christie90eeb012011-07-25 13:48:50 -05001528 */
1529static int
1530iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
1531{
1532 struct device *dev = &shost->shost_gendev;
1533 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1534 struct request_queue *q;
1535 char bsg_name[20];
Mike Christie90eeb012011-07-25 13:48:50 -05001536
1537 if (!i->iscsi_transport->bsg_request)
1538 return -ENOTSUPP;
1539
1540 snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
Jens Axboeaae3b062018-10-26 11:26:25 -06001541 q = bsg_setup_queue(dev, bsg_name, iscsi_bsg_host_dispatch, NULL, 0);
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +03001542 if (IS_ERR(q)) {
Mike Christie90eeb012011-07-25 13:48:50 -05001543 shost_printk(KERN_ERR, shost, "bsg interface failed to "
1544 "initialize - no request queue\n");
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +03001545 return PTR_ERR(q);
Mike Christie90eeb012011-07-25 13:48:50 -05001546 }
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +03001547 __scsi_init_queue(shost, q);
Mike Christie90eeb012011-07-25 13:48:50 -05001548
1549 ihost->bsg_q = q;
1550 return 0;
1551}
1552
Mike Christie30a6c652006-04-06 21:13:39 -05001553static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +01001554 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -05001555{
1556 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001557 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -05001558
1559 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -06001560 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -05001561 mutex_init(&ihost->mutex);
Mike Christie90eeb012011-07-25 13:48:50 -05001562
1563 iscsi_bsg_host_add(shost, ihost);
1564 /* ignore any bsg add error - we just can't do sgio */
1565
1566 return 0;
1567}
1568
1569static int iscsi_remove_host(struct transport_container *tc,
1570 struct device *dev, struct device *cdev)
1571{
1572 struct Scsi_Host *shost = dev_to_shost(dev);
1573 struct iscsi_cls_host *ihost = shost->shost_data;
1574
Jens Axboe5e28b8d2018-10-26 11:27:02 -06001575 bsg_remove_queue(ihost->bsg_q);
Mike Christie30a6c652006-04-06 21:13:39 -05001576 return 0;
1577}
1578
1579static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
1580 "iscsi_host",
1581 iscsi_setup_host,
Mike Christie90eeb012011-07-25 13:48:50 -05001582 iscsi_remove_host,
Mike Christie30a6c652006-04-06 21:13:39 -05001583 NULL);
1584
Alex Aizman0896b752005-08-04 19:33:07 -07001585static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
1586 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 NULL,
1588 NULL,
1589 NULL);
1590
Alex Aizman0896b752005-08-04 19:33:07 -07001591static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
1592 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 NULL,
1594 NULL,
1595 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -07001596
1597static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +01001598static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001599
Mike Christie7b7232f2006-02-01 21:06:49 -06001600static LIST_HEAD(sesslist);
1601static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -07001602static LIST_HEAD(connlist);
Bharath Ravi0ab71042020-01-25 01:19:25 -05001603static LIST_HEAD(connlist_err);
Alex Aizman0896b752005-08-04 19:33:07 -07001604static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Mike Christieb5c7a122006-04-06 21:13:33 -05001606static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
1607{
1608 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
1609 return sess->sid;
1610}
1611
1612/*
1613 * Returns the matching session to a given sid
1614 */
1615static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -06001616{
1617 unsigned long flags;
1618 struct iscsi_cls_session *sess;
1619
1620 spin_lock_irqsave(&sesslock, flags);
1621 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -05001622 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001623 spin_unlock_irqrestore(&sesslock, flags);
1624 return sess;
1625 }
1626 }
1627 spin_unlock_irqrestore(&sesslock, flags);
1628 return NULL;
1629}
1630
Mike Christieb5c7a122006-04-06 21:13:33 -05001631/*
1632 * Returns the matching connection to a given sid / cid tuple
1633 */
1634static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -06001635{
1636 unsigned long flags;
1637 struct iscsi_cls_conn *conn;
1638
1639 spin_lock_irqsave(&connlock, flags);
1640 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -05001641 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001642 spin_unlock_irqrestore(&connlock, flags);
1643 return conn;
1644 }
1645 }
1646 spin_unlock_irqrestore(&connlock, flags);
1647 return NULL;
1648}
1649
Mike Christie7b8631b2006-01-13 18:05:50 -06001650/*
1651 * The following functions can be used by LLDs that allocate
1652 * their own scsi_hosts or by software iscsi LLDs
1653 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001654static struct {
1655 int value;
1656 char *name;
1657} iscsi_session_state_names[] = {
1658 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
1659 { ISCSI_SESSION_FAILED, "FAILED" },
1660 { ISCSI_SESSION_FREE, "FREE" },
1661};
1662
Adrian Bunk3b0f2082008-02-13 23:30:17 +02001663static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -06001664{
1665 int i;
1666 char *name = NULL;
1667
1668 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
1669 if (iscsi_session_state_names[i].value == state) {
1670 name = iscsi_session_state_names[i].name;
1671 break;
1672 }
1673 }
1674 return name;
1675}
1676
1677int iscsi_session_chkready(struct iscsi_cls_session *session)
1678{
Mike Christie6eabafb2008-01-31 13:36:43 -06001679 int err;
1680
Mike Christie6eabafb2008-01-31 13:36:43 -06001681 switch (session->state) {
1682 case ISCSI_SESSION_LOGGED_IN:
1683 err = 0;
1684 break;
1685 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -05001686 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -06001687 break;
1688 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -05001689 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -06001690 break;
1691 default:
1692 err = DID_NO_CONNECT << 16;
1693 break;
1694 }
Mike Christie6eabafb2008-01-31 13:36:43 -06001695 return err;
1696}
1697EXPORT_SYMBOL_GPL(iscsi_session_chkready);
1698
Manish Rangankar17fa5752011-07-25 13:48:52 -05001699int iscsi_is_session_online(struct iscsi_cls_session *session)
1700{
1701 unsigned long flags;
1702 int ret = 0;
1703
1704 spin_lock_irqsave(&session->lock, flags);
1705 if (session->state == ISCSI_SESSION_LOGGED_IN)
1706 ret = 1;
1707 spin_unlock_irqrestore(&session->lock, flags);
1708 return ret;
1709}
1710EXPORT_SYMBOL_GPL(iscsi_is_session_online);
1711
Mike Christie7b8631b2006-01-13 18:05:50 -06001712static void iscsi_session_release(struct device *dev)
1713{
1714 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001715 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Mike Christie7b8631b2006-01-13 18:05:50 -06001717 shost = iscsi_session_to_shost(session);
1718 scsi_host_put(shost);
Mike Christie632248a2009-08-20 15:11:01 -05001719 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001720 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -06001721}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Nilesh Javali6260a5d2012-02-27 03:08:51 -08001723int iscsi_is_session_dev(const struct device *dev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001724{
1725 return dev->release == iscsi_session_release;
1726}
Nilesh Javali6260a5d2012-02-27 03:08:51 -08001727EXPORT_SYMBOL_GPL(iscsi_is_session_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Mike Christiea4804cd2008-05-21 15:54:00 -05001729static int iscsi_iter_session_fn(struct device *dev, void *data)
1730{
1731 void (* fn) (struct iscsi_cls_session *) = data;
1732
1733 if (!iscsi_is_session_dev(dev))
1734 return 0;
1735 fn(iscsi_dev_to_session(dev));
1736 return 0;
1737}
1738
1739void iscsi_host_for_each_session(struct Scsi_Host *shost,
1740 void (*fn)(struct iscsi_cls_session *))
1741{
1742 device_for_each_child(&shost->shost_gendev, fn,
1743 iscsi_iter_session_fn);
1744}
1745EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
1746
Mike Christie8aae18a2008-01-31 13:36:48 -06001747/**
1748 * iscsi_scan_finished - helper to report when running scans are done
1749 * @shost: scsi host
1750 * @time: scan run time
1751 *
1752 * This function can be used by drives like qla4xxx to report to the scsi
1753 * layer when the scans it kicked off at module load time are done.
1754 */
1755int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
1756{
Mike Christie32c6e1b2008-05-21 15:53:58 -05001757 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -06001758 /*
1759 * qla4xxx will have kicked off some session unblocks before calling
1760 * scsi_scan_host, so just wait for them to complete.
1761 */
1762 return !atomic_read(&ihost->nr_scans);
1763}
1764EXPORT_SYMBOL_GPL(iscsi_scan_finished);
1765
Mike Christie79706342008-05-21 15:54:12 -05001766struct iscsi_scan_data {
1767 unsigned int channel;
1768 unsigned int id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001769 u64 lun;
Hannes Reinecke1d645082016-03-17 08:39:45 +01001770 enum scsi_scan_mode rescan;
Mike Christie79706342008-05-21 15:54:12 -05001771};
1772
1773static int iscsi_user_scan_session(struct device *dev, void *data)
1774{
1775 struct iscsi_scan_data *scan_data = data;
1776 struct iscsi_cls_session *session;
1777 struct Scsi_Host *shost;
1778 struct iscsi_cls_host *ihost;
1779 unsigned long flags;
1780 unsigned int id;
1781
1782 if (!iscsi_is_session_dev(dev))
1783 return 0;
1784
1785 session = iscsi_dev_to_session(dev);
Mike Christie632248a2009-08-20 15:11:01 -05001786
1787 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
1788
Mike Christie79706342008-05-21 15:54:12 -05001789 shost = iscsi_session_to_shost(session);
1790 ihost = shost->shost_data;
1791
1792 mutex_lock(&ihost->mutex);
1793 spin_lock_irqsave(&session->lock, flags);
1794 if (session->state != ISCSI_SESSION_LOGGED_IN) {
1795 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05001796 goto user_scan_exit;
Mike Christie79706342008-05-21 15:54:12 -05001797 }
1798 id = session->target_id;
1799 spin_unlock_irqrestore(&session->lock, flags);
1800
1801 if (id != ISCSI_MAX_TARGET) {
1802 if ((scan_data->channel == SCAN_WILD_CARD ||
1803 scan_data->channel == 0) &&
1804 (scan_data->id == SCAN_WILD_CARD ||
1805 scan_data->id == id))
1806 scsi_scan_target(&session->dev, 0, id,
Hannes Reinecke1d645082016-03-17 08:39:45 +01001807 scan_data->lun, scan_data->rescan);
Mike Christie79706342008-05-21 15:54:12 -05001808 }
Mike Christie632248a2009-08-20 15:11:01 -05001809
1810user_scan_exit:
Mike Christie79706342008-05-21 15:54:12 -05001811 mutex_unlock(&ihost->mutex);
Mike Christie632248a2009-08-20 15:11:01 -05001812 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
Mike Christie79706342008-05-21 15:54:12 -05001813 return 0;
1814}
1815
Mike Christie30a6c652006-04-06 21:13:39 -05001816static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001817 uint id, u64 lun)
Mike Christie30a6c652006-04-06 21:13:39 -05001818{
Mike Christie79706342008-05-21 15:54:12 -05001819 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -05001820
Mike Christie79706342008-05-21 15:54:12 -05001821 scan_data.channel = channel;
1822 scan_data.id = id;
1823 scan_data.lun = lun;
Hannes Reinecke1d645082016-03-17 08:39:45 +01001824 scan_data.rescan = SCSI_SCAN_MANUAL;
Mike Christie30a6c652006-04-06 21:13:39 -05001825
Mike Christie79706342008-05-21 15:54:12 -05001826 return device_for_each_child(&shost->shost_gendev, &scan_data,
1827 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -05001828}
1829
Mike Christiebd976f62008-01-31 13:36:46 -06001830static void iscsi_scan_session(struct work_struct *work)
1831{
1832 struct iscsi_cls_session *session =
1833 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -06001834 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001835 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -05001836 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -06001837
Mike Christie79706342008-05-21 15:54:12 -05001838 scan_data.channel = 0;
1839 scan_data.id = SCAN_WILD_CARD;
1840 scan_data.lun = SCAN_WILD_CARD;
Hannes Reinecke1d645082016-03-17 08:39:45 +01001841 scan_data.rescan = SCSI_SCAN_RESCAN;
Mike Christiebd976f62008-01-31 13:36:46 -06001842
Mike Christie79706342008-05-21 15:54:12 -05001843 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -06001844 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -06001845}
1846
Mike Christiec01be6d2010-07-22 16:59:49 +05301847/**
1848 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
Randy Dunlape6d4ef42010-08-14 13:05:41 -07001849 * @cmd: scsi cmd passed to scsi eh handler
Mike Christiec01be6d2010-07-22 16:59:49 +05301850 *
1851 * If the session is down this function will wait for the recovery
1852 * timer to fire or for the session to be logged back in. If the
1853 * recovery timer fires then FAST_IO_FAIL is returned. The caller
1854 * should pass this error value to the scsi eh.
1855 */
1856int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
1857{
1858 struct iscsi_cls_session *session =
1859 starget_to_session(scsi_target(cmd->device));
1860 unsigned long flags;
1861 int ret = 0;
1862
1863 spin_lock_irqsave(&session->lock, flags);
1864 while (session->state != ISCSI_SESSION_LOGGED_IN) {
1865 if (session->state == ISCSI_SESSION_FREE) {
1866 ret = FAST_IO_FAIL;
1867 break;
1868 }
1869 spin_unlock_irqrestore(&session->lock, flags);
1870 msleep(1000);
1871 spin_lock_irqsave(&session->lock, flags);
1872 }
1873 spin_unlock_irqrestore(&session->lock, flags);
1874 return ret;
1875}
1876EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
1877
David Howellsc4028952006-11-22 14:57:56 +00001878static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -05001879{
David Howellsc4028952006-11-22 14:57:56 +00001880 struct iscsi_cls_session *session =
1881 container_of(work, struct iscsi_cls_session,
1882 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -06001883 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -05001884
Mike Christie322d7392008-01-31 13:36:52 -06001885 iscsi_cls_session_printk(KERN_INFO, session,
1886 "session recovery timed out after %d secs\n",
1887 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -05001888
Mike Christie6eabafb2008-01-31 13:36:43 -06001889 spin_lock_irqsave(&session->lock, flags);
1890 switch (session->state) {
1891 case ISCSI_SESSION_FAILED:
1892 session->state = ISCSI_SESSION_FREE;
1893 break;
1894 case ISCSI_SESSION_LOGGED_IN:
1895 case ISCSI_SESSION_FREE:
1896 /* we raced with the unblock's flush */
1897 spin_unlock_irqrestore(&session->lock, flags);
1898 return;
1899 }
1900 spin_unlock_irqrestore(&session->lock, flags);
1901
Mike Christie632248a2009-08-20 15:11:01 -05001902 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
Mike Christie5d9fb5c2012-05-17 23:56:57 -05001903 scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
Mike Christie632248a2009-08-20 15:11:01 -05001904 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
Mike Christiea0c2f8b2021-11-05 17:10:47 -05001905
1906 if (session->transport->session_recovery_timedout)
1907 session->transport->session_recovery_timedout(session);
Mike Christie30a6c652006-04-06 21:13:39 -05001908}
1909
Mike Christie45ab33b2008-03-04 13:26:55 -06001910static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -05001911{
Mike Christie45ab33b2008-03-04 13:26:55 -06001912 struct iscsi_cls_session *session =
1913 container_of(work, struct iscsi_cls_session,
1914 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -06001915 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001916 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -06001917 unsigned long flags;
1918
Mike Christie632248a2009-08-20 15:11:01 -05001919 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
Mike Christie45ab33b2008-03-04 13:26:55 -06001920 /*
1921 * The recovery and unblock work get run from the same workqueue,
1922 * so try to cancel it if it was going to run after this unblock.
1923 */
1924 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -06001925 spin_lock_irqsave(&session->lock, flags);
1926 session->state = ISCSI_SESSION_LOGGED_IN;
1927 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -06001928 /* start IO */
Mike Christie5d9fb5c2012-05-17 23:56:57 -05001929 scsi_target_unblock(&session->dev, SDEV_RUNNING);
Mike Christie8aae18a2008-01-31 13:36:48 -06001930 /*
1931 * Only do kernel scanning if the driver is properly hooked into
1932 * the async scanning code (drivers like iscsi_tcp do login and
1933 * scanning from userspace).
1934 */
1935 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -06001936 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -06001937 atomic_inc(&ihost->nr_scans);
1938 }
Mike Christie632248a2009-08-20 15:11:01 -05001939 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -06001940}
Mike Christie45ab33b2008-03-04 13:26:55 -06001941
1942/**
1943 * iscsi_unblock_session - set a session as logged in and start IO.
1944 * @session: iscsi session
1945 *
1946 * Mark a session as ready to accept IO.
1947 */
1948void iscsi_unblock_session(struct iscsi_cls_session *session)
1949{
Mike Christie7ce9fc52021-05-25 13:18:09 -05001950 flush_work(&session->block_work);
1951
Mike Christie45ab33b2008-03-04 13:26:55 -06001952 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
1953 /*
Mike Christie1d726aa2020-07-01 14:47:47 -05001954 * Blocking the session can be done from any context so we only
1955 * queue the block work. Make sure the unblock work has completed
1956 * because it flushes/cancels the other works and updates the state.
Mike Christie45ab33b2008-03-04 13:26:55 -06001957 */
Mike Christie1d726aa2020-07-01 14:47:47 -05001958 flush_work(&session->unblock_work);
Mike Christie45ab33b2008-03-04 13:26:55 -06001959}
Mike Christie30a6c652006-04-06 21:13:39 -05001960EXPORT_SYMBOL_GPL(iscsi_unblock_session);
1961
Mike Christie45ab33b2008-03-04 13:26:55 -06001962static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -05001963{
Mike Christie45ab33b2008-03-04 13:26:55 -06001964 struct iscsi_cls_session *session =
1965 container_of(work, struct iscsi_cls_session,
1966 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -06001967 unsigned long flags;
1968
Mike Christie632248a2009-08-20 15:11:01 -05001969 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -06001970 spin_lock_irqsave(&session->lock, flags);
1971 session->state = ISCSI_SESSION_FAILED;
1972 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -05001973 scsi_target_block(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001974 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
Mike Christiefdd46dc2009-11-11 16:34:34 -06001975 if (session->recovery_tmo >= 0)
1976 queue_delayed_work(iscsi_eh_timer_workq,
1977 &session->recovery_work,
1978 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -05001979}
Mike Christie45ab33b2008-03-04 13:26:55 -06001980
1981void iscsi_block_session(struct iscsi_cls_session *session)
1982{
1983 queue_work(iscsi_eh_timer_workq, &session->block_work);
1984}
Mike Christie30a6c652006-04-06 21:13:39 -05001985EXPORT_SYMBOL_GPL(iscsi_block_session);
1986
Mike Christie26974782007-12-13 12:43:29 -06001987static void __iscsi_unbind_session(struct work_struct *work)
1988{
1989 struct iscsi_cls_session *session =
1990 container_of(work, struct iscsi_cls_session,
1991 unbind_work);
1992 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001993 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -05001994 unsigned long flags;
Mike Christie8d4a6902011-10-06 03:56:57 -05001995 unsigned int target_id;
Mike Christie26974782007-12-13 12:43:29 -06001996
Mike Christie632248a2009-08-20 15:11:01 -05001997 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
1998
Mike Christie26974782007-12-13 12:43:29 -06001999 /* Prevent new scans and make sure scanning is not in progress */
2000 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -05002001 spin_lock_irqsave(&session->lock, flags);
2002 if (session->target_id == ISCSI_MAX_TARGET) {
2003 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -06002004 mutex_unlock(&ihost->mutex);
Wu Bo13e60d32020-03-24 15:58:50 +08002005 goto unbind_session_exit;
Mike Christie26974782007-12-13 12:43:29 -06002006 }
Mike Christie8d4a6902011-10-06 03:56:57 -05002007
2008 target_id = session->target_id;
Mike Christie79706342008-05-21 15:54:12 -05002009 session->target_id = ISCSI_MAX_TARGET;
2010 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -06002011 mutex_unlock(&ihost->mutex);
2012
Mike Christiee463f962020-07-01 14:47:46 -05002013 scsi_remove_target(&session->dev);
2014
Mike Christie8d4a6902011-10-06 03:56:57 -05002015 if (session->ida_used)
2016 ida_simple_remove(&iscsi_sess_ida, target_id);
2017
Wu Bo13e60d32020-03-24 15:58:50 +08002018unbind_session_exit:
Mike Christie26974782007-12-13 12:43:29 -06002019 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -05002020 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
Mike Christie26974782007-12-13 12:43:29 -06002021}
2022
Frank Mayharcc6b32e2020-02-27 14:59:45 -05002023static void __iscsi_destroy_session(struct work_struct *work)
2024{
2025 struct iscsi_cls_session *session =
2026 container_of(work, struct iscsi_cls_session, destroy_work);
2027
2028 session->transport->destroy_session(session);
2029}
2030
Mike Christie7b8631b2006-01-13 18:05:50 -06002031struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -05002032iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
2033 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06002034{
2035 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Mike Christie5d91e202008-05-21 15:54:01 -05002037 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -05002038 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06002039 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -05002040 return NULL;
2041
Mike Christie7b8631b2006-01-13 18:05:50 -06002042 session->transport = transport;
Mike Christie0c70d842011-12-05 16:44:01 -06002043 session->creator = -1;
Mike Christie30a6c652006-04-06 21:13:39 -05002044 session->recovery_tmo = 120;
Chris Leech9c8108a2015-06-16 16:07:13 -07002045 session->recovery_tmo_sysfs_override = false;
Mike Christie6eabafb2008-01-31 13:36:43 -06002046 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +00002047 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -05002048 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -06002049 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
2050 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -06002051 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -06002052 INIT_WORK(&session->scan_work, iscsi_scan_session);
Frank Mayharcc6b32e2020-02-27 14:59:45 -05002053 INIT_WORK(&session->destroy_work, __iscsi_destroy_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002054 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -06002055
Mike Christie6a8a0d32006-06-28 12:00:31 -05002056 /* this is released in the dev's release function */
2057 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -05002058 session->dev.parent = &shost->shost_gendev;
2059 session->dev.release = iscsi_session_release;
2060 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -05002061 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -05002062 session->dd_data = &session[1];
Mike Christie632248a2009-08-20 15:11:01 -05002063
2064 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
Mike Christie8434aa82006-06-28 12:00:30 -05002065 return session;
2066}
2067EXPORT_SYMBOL_GPL(iscsi_alloc_session);
2068
Mike Christie6a8a0d32006-06-28 12:00:31 -05002069int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05002070{
Mike Christie26974782007-12-13 12:43:29 -06002071 unsigned long flags;
Mike Christie8d4a6902011-10-06 03:56:57 -05002072 int id = 0;
Mike Christie8434aa82006-06-28 12:00:30 -05002073 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -05002074
Mike Christie41be1442007-02-28 17:32:18 -06002075 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -05002076
Mike Christie8d4a6902011-10-06 03:56:57 -05002077 if (target_id == ISCSI_MAX_TARGET) {
2078 id = ida_simple_get(&iscsi_sess_ida, 0, 0, GFP_KERNEL);
Mike Christie79706342008-05-21 15:54:12 -05002079
Mike Christie8d4a6902011-10-06 03:56:57 -05002080 if (id < 0) {
Mike Christie79706342008-05-21 15:54:12 -05002081 iscsi_cls_session_printk(KERN_ERR, session,
Mike Christie8d4a6902011-10-06 03:56:57 -05002082 "Failure in Target ID Allocation\n");
2083 return id;
Mike Christie79706342008-05-21 15:54:12 -05002084 }
Mike Christie8d4a6902011-10-06 03:56:57 -05002085 session->target_id = (unsigned int)id;
2086 session->ida_used = true;
2087 } else
2088 session->target_id = target_id;
Mike Christie30a6c652006-04-06 21:13:39 -05002089
Kay Sievers71610f52008-12-03 22:41:36 +01002090 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -05002091 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06002092 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06002093 iscsi_cls_session_printk(KERN_ERR, session,
2094 "could not register session's dev\n");
Mike Christie8d4a6902011-10-06 03:56:57 -05002095 goto release_ida;
Mike Christie7b8631b2006-01-13 18:05:50 -06002096 }
Gabriel Krisman Bertazif3c893e2020-01-06 13:58:17 -05002097 err = transport_register_device(&session->dev);
2098 if (err) {
2099 iscsi_cls_session_printk(KERN_ERR, session,
2100 "could not register transport's dev\n");
2101 goto release_dev;
2102 }
Mike Christie7b8631b2006-01-13 18:05:50 -06002103
Mike Christie26974782007-12-13 12:43:29 -06002104 spin_lock_irqsave(&sesslock, flags);
2105 list_add(&session->sess_list, &sesslist);
2106 spin_unlock_irqrestore(&sesslock, flags);
2107
Mike Christie26974782007-12-13 12:43:29 -06002108 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -05002109 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
Mike Christie8434aa82006-06-28 12:00:30 -05002110 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -05002111
Gabriel Krisman Bertazif3c893e2020-01-06 13:58:17 -05002112release_dev:
2113 device_del(&session->dev);
Mike Christie8d4a6902011-10-06 03:56:57 -05002114release_ida:
2115 if (session->ida_used)
2116 ida_simple_remove(&iscsi_sess_ida, session->target_id);
2117
Mike Christie8434aa82006-06-28 12:00:30 -05002118 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -06002119}
Mike Christie8434aa82006-06-28 12:00:30 -05002120EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -06002121
2122/**
Mike Christie8434aa82006-06-28 12:00:30 -05002123 * iscsi_create_session - create iscsi class session
2124 * @shost: scsi host
2125 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -05002126 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -05002127 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -06002128 *
Mike Christie8434aa82006-06-28 12:00:30 -05002129 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05002130 */
Mike Christie8434aa82006-06-28 12:00:30 -05002131struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -05002132iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
2133 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05002134{
2135 struct iscsi_cls_session *session;
2136
Mike Christie5d91e202008-05-21 15:54:01 -05002137 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -05002138 if (!session)
2139 return NULL;
2140
Mike Christie6a8a0d32006-06-28 12:00:31 -05002141 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -05002142 iscsi_free_session(session);
2143 return NULL;
2144 }
2145 return session;
2146}
2147EXPORT_SYMBOL_GPL(iscsi_create_session);
2148
Mike Christie26974782007-12-13 12:43:29 -06002149static void iscsi_conn_release(struct device *dev)
2150{
2151 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
2152 struct device *parent = conn->dev.parent;
2153
Mike Christie632248a2009-08-20 15:11:01 -05002154 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
Mike Christie26974782007-12-13 12:43:29 -06002155 kfree(conn);
2156 put_device(parent);
2157}
2158
2159static int iscsi_is_conn_dev(const struct device *dev)
2160{
2161 return dev->release == iscsi_conn_release;
2162}
2163
2164static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
2165{
2166 if (!iscsi_is_conn_dev(dev))
2167 return 0;
2168 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
2169}
2170
Mike Christie8434aa82006-06-28 12:00:30 -05002171void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -06002172{
Mike Christie26974782007-12-13 12:43:29 -06002173 unsigned long flags;
2174 int err;
Mike Christie30a6c652006-04-06 21:13:39 -05002175
Mike Christie632248a2009-08-20 15:11:01 -05002176 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
2177
Mike Christie26974782007-12-13 12:43:29 -06002178 spin_lock_irqsave(&sesslock, flags);
Mike Christie93bf02e2020-07-01 14:47:48 -05002179 if (!list_empty(&session->sess_list))
2180 list_del(&session->sess_list);
Mike Christie26974782007-12-13 12:43:29 -06002181 spin_unlock_irqrestore(&sesslock, flags);
2182
Mike Christie1d726aa2020-07-01 14:47:47 -05002183 flush_work(&session->block_work);
2184 flush_work(&session->unblock_work);
2185 cancel_delayed_work_sync(&session->recovery_work);
Mike Christie26974782007-12-13 12:43:29 -06002186 /*
2187 * If we are blocked let commands flow again. The lld or iscsi
2188 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -06002189 * We assume that LLD will not be calling block/unblock while
2190 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -06002191 */
Mike Christie6eabafb2008-01-31 13:36:43 -06002192 spin_lock_irqsave(&session->lock, flags);
2193 session->state = ISCSI_SESSION_FREE;
2194 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -06002195
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002196 scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
Mike Christie45ab33b2008-03-04 13:26:55 -06002197 /* flush running scans then delete devices */
Jitendra Bhivare90e96312017-03-24 14:11:43 +05302198 flush_work(&session->scan_work);
Maurizio Lombardi165aa2b2019-01-28 15:24:42 +01002199 /* flush running unbind operations */
2200 flush_work(&session->unbind_work);
Mike Christie45ab33b2008-03-04 13:26:55 -06002201 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -05002202
Mike Christie26974782007-12-13 12:43:29 -06002203 /* hw iscsi may not have removed all connections from session */
2204 err = device_for_each_child(&session->dev, NULL,
2205 iscsi_iter_destroy_conn_fn);
2206 if (err)
Mike Christie322d7392008-01-31 13:36:52 -06002207 iscsi_cls_session_printk(KERN_ERR, session,
2208 "Could not delete all connections "
2209 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -05002210
Mike Christie7b8631b2006-01-13 18:05:50 -06002211 transport_unregister_device(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05002212
2213 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
Mike Christie8434aa82006-06-28 12:00:30 -05002214 device_del(&session->dev);
2215}
2216EXPORT_SYMBOL_GPL(iscsi_remove_session);
2217
Mike Christie23d6fef2021-05-25 13:18:00 -05002218static void iscsi_stop_conn(struct iscsi_cls_conn *conn, int flag)
2219{
2220 ISCSI_DBG_TRANS_CONN(conn, "Stopping conn.\n");
2221
2222 switch (flag) {
2223 case STOP_CONN_RECOVER:
2224 conn->state = ISCSI_CONN_FAILED;
2225 break;
2226 case STOP_CONN_TERM:
2227 conn->state = ISCSI_CONN_DOWN;
2228 break;
2229 default:
2230 iscsi_cls_conn_printk(KERN_ERR, conn, "invalid stop flag %d\n",
2231 flag);
2232 return;
2233 }
2234
2235 conn->transport->stop_conn(conn, flag);
2236 ISCSI_DBG_TRANS_CONN(conn, "Stopping conn done.\n");
2237}
2238
2239static int iscsi_if_stop_conn(struct iscsi_transport *transport,
2240 struct iscsi_uevent *ev)
2241{
2242 int flag = ev->u.stop_conn.flag;
2243 struct iscsi_cls_conn *conn;
2244
2245 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
2246 if (!conn)
2247 return -EINVAL;
2248
2249 ISCSI_DBG_TRANS_CONN(conn, "iscsi if conn stop.\n");
2250 /*
2251 * If this is a termination we have to call stop_conn with that flag
2252 * so the correct states get set. If we haven't run the work yet try to
2253 * avoid the extra run.
2254 */
2255 if (flag == STOP_CONN_TERM) {
2256 cancel_work_sync(&conn->cleanup_work);
2257 iscsi_stop_conn(conn, flag);
2258 } else {
2259 /*
2260 * Figure out if it was the kernel or userspace initiating this.
2261 */
2262 if (!test_and_set_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags)) {
2263 iscsi_stop_conn(conn, flag);
2264 } else {
2265 ISCSI_DBG_TRANS_CONN(conn,
2266 "flush kernel conn cleanup.\n");
2267 flush_work(&conn->cleanup_work);
2268 }
2269 /*
2270 * Only clear for recovery to avoid extra cleanup runs during
2271 * termination.
2272 */
2273 clear_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags);
2274 }
2275 ISCSI_DBG_TRANS_CONN(conn, "iscsi if conn stop done.\n");
2276 return 0;
2277}
2278
2279static void iscsi_ep_disconnect(struct iscsi_cls_conn *conn, bool is_active)
2280{
2281 struct iscsi_cls_session *session = iscsi_conn_to_session(conn);
2282 struct iscsi_endpoint *ep;
2283
2284 ISCSI_DBG_TRANS_CONN(conn, "disconnect ep.\n");
2285 conn->state = ISCSI_CONN_FAILED;
2286
2287 if (!conn->ep || !session->transport->ep_disconnect)
2288 return;
2289
2290 ep = conn->ep;
2291 conn->ep = NULL;
2292
2293 session->transport->unbind_conn(conn, is_active);
2294 session->transport->ep_disconnect(ep);
2295 ISCSI_DBG_TRANS_CONN(conn, "disconnect ep done.\n");
2296}
2297
2298static void iscsi_cleanup_conn_work_fn(struct work_struct *work)
2299{
2300 struct iscsi_cls_conn *conn = container_of(work, struct iscsi_cls_conn,
2301 cleanup_work);
2302 struct iscsi_cls_session *session = iscsi_conn_to_session(conn);
2303
2304 mutex_lock(&conn->ep_mutex);
2305 /*
2306 * If we are not at least bound there is nothing for us to do. Userspace
2307 * will do a ep_disconnect call if offload is used, but will not be
2308 * doing a stop since there is nothing to clean up, so we have to clear
2309 * the cleanup bit here.
2310 */
2311 if (conn->state != ISCSI_CONN_BOUND && conn->state != ISCSI_CONN_UP) {
2312 ISCSI_DBG_TRANS_CONN(conn, "Got error while conn is already failed. Ignoring.\n");
2313 clear_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags);
2314 mutex_unlock(&conn->ep_mutex);
2315 return;
2316 }
2317
2318 iscsi_ep_disconnect(conn, false);
2319
2320 if (system_state != SYSTEM_RUNNING) {
2321 /*
2322 * If the user has set up for the session to never timeout
2323 * then hang like they wanted. For all other cases fail right
2324 * away since userspace is not going to relogin.
2325 */
2326 if (session->recovery_tmo > 0)
2327 session->recovery_tmo = 0;
2328 }
2329
2330 iscsi_stop_conn(conn, STOP_CONN_RECOVER);
2331 mutex_unlock(&conn->ep_mutex);
2332 ISCSI_DBG_TRANS_CONN(conn, "cleanup done.\n");
2333}
2334
Mike Christie8434aa82006-06-28 12:00:30 -05002335void iscsi_free_session(struct iscsi_cls_session *session)
2336{
Mike Christie632248a2009-08-20 15:11:01 -05002337 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
Mike Christie26974782007-12-13 12:43:29 -06002338 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -05002339 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06002340}
Mike Christie8434aa82006-06-28 12:00:30 -05002341EXPORT_SYMBOL_GPL(iscsi_free_session);
2342
2343/**
Mike Christie7b8631b2006-01-13 18:05:50 -06002344 * iscsi_create_conn - create iscsi class connection
2345 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -05002346 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -06002347 * @cid: connection id
2348 *
2349 * This can be called from a LLD or iscsi_transport. The connection
2350 * is child of the session so cid must be unique for all connections
2351 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -05002352 *
2353 * Since we do not support MCS, cid will normally be zero. In some cases
2354 * for software iscsi we could be trying to preallocate a connection struct
2355 * in which case there could be two connection structs and cid would be
2356 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -05002357 */
Mike Christie7b8631b2006-01-13 18:05:50 -06002358struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002359iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -06002360{
2361 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -06002362 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -06002363 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -06002364 int err;
2365
Mike Christie5d91e202008-05-21 15:54:01 -05002366 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06002367 if (!conn)
2368 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -05002369 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06002370 conn->dd_data = &conn[1];
2371
Mike Christie22a39fb2011-02-16 15:04:33 -06002372 mutex_init(&conn->ep_mutex);
Mike Christie7b8631b2006-01-13 18:05:50 -06002373 INIT_LIST_HEAD(&conn->conn_list);
Mike Christie23d6fef2021-05-25 13:18:00 -05002374 INIT_WORK(&conn->cleanup_work, iscsi_cleanup_conn_work_fn);
Mike Christie7b8631b2006-01-13 18:05:50 -06002375 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -05002376 conn->cid = cid;
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04002377 conn->state = ISCSI_CONN_DOWN;
Mike Christie7b8631b2006-01-13 18:05:50 -06002378
2379 /* this is released in the dev's release function */
2380 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -04002381 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -05002382
Kay Sievers71610f52008-12-03 22:41:36 +01002383 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06002384 conn->dev.parent = &session->dev;
2385 conn->dev.release = iscsi_conn_release;
2386 err = device_register(&conn->dev);
2387 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06002388 iscsi_cls_session_printk(KERN_ERR, session, "could not "
2389 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002390 goto release_parent_ref;
2391 }
Gabriel Krisman Bertazif3c893e2020-01-06 13:58:17 -05002392 err = transport_register_device(&conn->dev);
2393 if (err) {
2394 iscsi_cls_session_printk(KERN_ERR, session, "could not "
2395 "register transport's dev\n");
2396 goto release_conn_ref;
2397 }
Mike Christie26974782007-12-13 12:43:29 -06002398
2399 spin_lock_irqsave(&connlock, flags);
2400 list_add(&conn->conn_list, &connlist);
Mike Christie26974782007-12-13 12:43:29 -06002401 spin_unlock_irqrestore(&connlock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05002402
2403 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002404 return conn;
2405
Gabriel Krisman Bertazif3c893e2020-01-06 13:58:17 -05002406release_conn_ref:
Qinglang Miao6dc1c7a2020-11-20 15:48:52 +08002407 device_unregister(&conn->dev);
2408 put_device(&session->dev);
2409 return NULL;
Mike Christie7b8631b2006-01-13 18:05:50 -06002410release_parent_ref:
2411 put_device(&session->dev);
2412free_conn:
2413 kfree(conn);
2414 return NULL;
2415}
2416
2417EXPORT_SYMBOL_GPL(iscsi_create_conn);
2418
2419/**
2420 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -05002421 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -06002422 *
Mike Christie26974782007-12-13 12:43:29 -06002423 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05002424 */
Mike Christie7b8631b2006-01-13 18:05:50 -06002425int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
2426{
Mike Christie26974782007-12-13 12:43:29 -06002427 unsigned long flags;
2428
2429 spin_lock_irqsave(&connlock, flags);
Mike Christie26974782007-12-13 12:43:29 -06002430 list_del(&conn->conn_list);
2431 spin_unlock_irqrestore(&connlock, flags);
2432
Mike Christie7b8631b2006-01-13 18:05:50 -06002433 transport_unregister_device(&conn->dev);
Mike Christie632248a2009-08-20 15:11:01 -05002434 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002435 device_unregister(&conn->dev);
2436 return 0;
2437}
Mike Christie7b8631b2006-01-13 18:05:50 -06002438EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
2439
Mike Christieb1d19e82021-05-25 13:18:03 -05002440void iscsi_put_conn(struct iscsi_cls_conn *conn)
2441{
2442 put_device(&conn->dev);
2443}
2444EXPORT_SYMBOL_GPL(iscsi_put_conn);
2445
2446void iscsi_get_conn(struct iscsi_cls_conn *conn)
2447{
2448 get_device(&conn->dev);
2449}
2450EXPORT_SYMBOL_GPL(iscsi_get_conn);
2451
Mike Christie7b8631b2006-01-13 18:05:50 -06002452/*
Mike Christie7b8631b2006-01-13 18:05:50 -06002453 * iscsi interface functions
2454 */
Alex Aizman0896b752005-08-04 19:33:07 -07002455static struct iscsi_internal *
2456iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
Alex Aizman0896b752005-08-04 19:33:07 -07002458 struct iscsi_internal *priv;
2459 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Alex Aizman0896b752005-08-04 19:33:07 -07002461 spin_lock_irqsave(&iscsi_transport_lock, flags);
2462 list_for_each_entry(priv, &iscsi_transports, list) {
2463 if (tt == priv->iscsi_transport) {
2464 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2465 return priv;
2466 }
2467 }
2468 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2469 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Alex Aizman0896b752005-08-04 19:33:07 -07002472static int
Michael Chan43514772009-06-08 18:14:41 -07002473iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -05002474{
Michael Chan43514772009-06-08 18:14:41 -07002475 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -07002476}
2477
Chris Leechaf170922018-04-09 15:15:28 -07002478static int
2479iscsi_unicast_skb(struct sk_buff *skb, u32 portid)
2480{
2481 return nlmsg_unicast(nls, skb, portid);
2482}
2483
Mike Christie7b7232f2006-02-01 21:06:49 -06002484int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -07002485 char *data, uint32_t data_size)
2486{
2487 struct nlmsghdr *nlh;
2488 struct sk_buff *skb;
2489 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07002490 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05002491 struct iscsi_internal *priv;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002492 int len = nlmsg_total_size(sizeof(*ev) + sizeof(struct iscsi_hdr) +
2493 data_size);
Alex Aizman0896b752005-08-04 19:33:07 -07002494
Mike Christie790f39a2006-05-18 20:31:39 -05002495 priv = iscsi_if_transport_lookup(conn->transport);
2496 if (!priv)
2497 return -EINVAL;
2498
Mike Christie43a145a2006-10-16 18:09:38 -04002499 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002500 if (!skb) {
Mike Christiee5bd7b542008-09-24 11:46:10 -05002501 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06002502 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
2503 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07002504 return -ENOMEM;
2505 }
2506
Michael Chan43514772009-06-08 18:14:41 -07002507 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002508 ev = nlmsg_data(nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07002509 memset(ev, 0, sizeof(*ev));
2510 ev->transport_handle = iscsi_handle(conn->transport);
2511 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05002512 ev->r.recv_req.cid = conn->cid;
2513 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07002514 pdu = (char*)ev + sizeof(*ev);
2515 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
2516 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
2517
Michael Chan43514772009-06-08 18:14:41 -07002518 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002519}
2520EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
2521
Michael Chan43514772009-06-08 18:14:41 -07002522int iscsi_offload_mesg(struct Scsi_Host *shost,
2523 struct iscsi_transport *transport, uint32_t type,
2524 char *data, uint16_t data_size)
2525{
2526 struct nlmsghdr *nlh;
2527 struct sk_buff *skb;
2528 struct iscsi_uevent *ev;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002529 int len = nlmsg_total_size(sizeof(*ev) + data_size);
Michael Chan43514772009-06-08 18:14:41 -07002530
Michael Chana541f842009-07-29 08:49:52 +00002531 skb = alloc_skb(len, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07002532 if (!skb) {
2533 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
2534 return -ENOMEM;
2535 }
2536
2537 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002538 ev = nlmsg_data(nlh);
Michael Chan43514772009-06-08 18:14:41 -07002539 memset(ev, 0, sizeof(*ev));
2540 ev->type = type;
2541 ev->transport_handle = iscsi_handle(transport);
2542 switch (type) {
2543 case ISCSI_KEVENT_PATH_REQ:
2544 ev->r.req_path.host_no = shost->host_no;
2545 break;
2546 case ISCSI_KEVENT_IF_DOWN:
2547 ev->r.notify_if_down.host_no = shost->host_no;
2548 break;
2549 }
2550
2551 memcpy((char *)ev + sizeof(*ev), data, data_size);
2552
Michael Chana541f842009-07-29 08:49:52 +00002553 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07002554}
2555EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
2556
Mike Christiee5bd7b542008-09-24 11:46:10 -05002557void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07002558{
2559 struct nlmsghdr *nlh;
2560 struct sk_buff *skb;
2561 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05002562 struct iscsi_internal *priv;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002563 int len = nlmsg_total_size(sizeof(*ev));
Bharath Ravi0ab71042020-01-25 01:19:25 -05002564
Mike Christie23d6fef2021-05-25 13:18:00 -05002565 if (!test_and_set_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags))
2566 queue_work(iscsi_conn_cleanup_workq, &conn->cleanup_work);
Alex Aizman0896b752005-08-04 19:33:07 -07002567
Mike Christie790f39a2006-05-18 20:31:39 -05002568 priv = iscsi_if_transport_lookup(conn->transport);
2569 if (!priv)
2570 return;
2571
Mike Christie43a145a2006-10-16 18:09:38 -04002572 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002573 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06002574 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2575 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07002576 return;
2577 }
2578
Michael Chan43514772009-06-08 18:14:41 -07002579 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002580 ev = nlmsg_data(nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07002581 ev->transport_handle = iscsi_handle(conn->transport);
2582 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07002583 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05002584 ev->r.connerror.cid = conn->cid;
2585 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07002586
Michael Chan43514772009-06-08 18:14:41 -07002587 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002588
Mike Christie322d7392008-01-31 13:36:52 -06002589 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
2590 error);
Alex Aizman0896b752005-08-04 19:33:07 -07002591}
Mike Christiee5bd7b542008-09-24 11:46:10 -05002592EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07002593
Manish Rangankar17fa5752011-07-25 13:48:52 -05002594void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
2595 enum iscsi_conn_state state)
2596{
2597 struct nlmsghdr *nlh;
2598 struct sk_buff *skb;
2599 struct iscsi_uevent *ev;
2600 struct iscsi_internal *priv;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002601 int len = nlmsg_total_size(sizeof(*ev));
Manish Rangankar17fa5752011-07-25 13:48:52 -05002602
2603 priv = iscsi_if_transport_lookup(conn->transport);
2604 if (!priv)
2605 return;
2606
2607 skb = alloc_skb(len, GFP_ATOMIC);
2608 if (!skb) {
2609 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2610 "conn login (%d)\n", state);
2611 return;
2612 }
2613
2614 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002615 ev = nlmsg_data(nlh);
Manish Rangankar17fa5752011-07-25 13:48:52 -05002616 ev->transport_handle = iscsi_handle(conn->transport);
2617 ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
2618 ev->r.conn_login.state = state;
2619 ev->r.conn_login.cid = conn->cid;
2620 ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
2621 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2622
2623 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
2624 state);
2625}
2626EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
2627
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302628void iscsi_post_host_event(uint32_t host_no, struct iscsi_transport *transport,
2629 enum iscsi_host_event_code code, uint32_t data_size,
2630 uint8_t *data)
2631{
2632 struct nlmsghdr *nlh;
2633 struct sk_buff *skb;
2634 struct iscsi_uevent *ev;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002635 int len = nlmsg_total_size(sizeof(*ev) + data_size);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302636
Mike Christie49d0e642012-03-06 16:09:01 -06002637 skb = alloc_skb(len, GFP_NOIO);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302638 if (!skb) {
2639 printk(KERN_ERR "gracefully ignored host event (%d):%d OOM\n",
2640 host_no, code);
2641 return;
2642 }
2643
2644 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002645 ev = nlmsg_data(nlh);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302646 ev->transport_handle = iscsi_handle(transport);
2647 ev->type = ISCSI_KEVENT_HOST_EVENT;
2648 ev->r.host_event.host_no = host_no;
2649 ev->r.host_event.code = code;
2650 ev->r.host_event.data_size = data_size;
2651
2652 if (data_size)
2653 memcpy((char *)ev + sizeof(*ev), data, data_size);
2654
Mike Christie49d0e642012-03-06 16:09:01 -06002655 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302656}
2657EXPORT_SYMBOL_GPL(iscsi_post_host_event);
2658
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302659void iscsi_ping_comp_event(uint32_t host_no, struct iscsi_transport *transport,
2660 uint32_t status, uint32_t pid, uint32_t data_size,
2661 uint8_t *data)
2662{
2663 struct nlmsghdr *nlh;
2664 struct sk_buff *skb;
2665 struct iscsi_uevent *ev;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002666 int len = nlmsg_total_size(sizeof(*ev) + data_size);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302667
Mike Christie49d0e642012-03-06 16:09:01 -06002668 skb = alloc_skb(len, GFP_NOIO);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302669 if (!skb) {
2670 printk(KERN_ERR "gracefully ignored ping comp: OOM\n");
2671 return;
2672 }
2673
2674 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002675 ev = nlmsg_data(nlh);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302676 ev->transport_handle = iscsi_handle(transport);
2677 ev->type = ISCSI_KEVENT_PING_COMP;
2678 ev->r.ping_comp.host_no = host_no;
2679 ev->r.ping_comp.status = status;
2680 ev->r.ping_comp.pid = pid;
2681 ev->r.ping_comp.data_size = data_size;
2682 memcpy((char *)ev + sizeof(*ev), data, data_size);
2683
Mike Christie49d0e642012-03-06 16:09:01 -06002684 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302685}
2686EXPORT_SYMBOL_GPL(iscsi_ping_comp_event);
2687
Alex Aizman0896b752005-08-04 19:33:07 -07002688static int
Chris Leechaf170922018-04-09 15:15:28 -07002689iscsi_if_send_reply(u32 portid, int type, void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07002690{
2691 struct sk_buff *skb;
2692 struct nlmsghdr *nlh;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002693 int len = nlmsg_total_size(size);
Alex Aizman0896b752005-08-04 19:33:07 -07002694
Mike Christie43a145a2006-10-16 18:09:38 -04002695 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05002696 if (!skb) {
2697 printk(KERN_ERR "Could not allocate skb to send reply.\n");
2698 return -ENOMEM;
2699 }
Alex Aizman0896b752005-08-04 19:33:07 -07002700
Chris Leechaf170922018-04-09 15:15:28 -07002701 nlh = __nlmsg_put(skb, 0, 0, type, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002702 memcpy(nlmsg_data(nlh), payload, size);
Chris Leechaf170922018-04-09 15:15:28 -07002703 return iscsi_unicast_skb(skb, portid);
Alex Aizman0896b752005-08-04 19:33:07 -07002704}
2705
2706static int
Mike Christie5b940ad2006-02-01 21:06:56 -06002707iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07002708{
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002709 struct iscsi_uevent *ev = nlmsg_data(nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07002710 struct iscsi_stats *stats;
2711 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06002712 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07002713 struct nlmsghdr *nlhstat;
2714 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05002715 struct iscsi_internal *priv;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002716 int len = nlmsg_total_size(sizeof(*ev) +
2717 sizeof(struct iscsi_stats) +
2718 sizeof(struct iscsi_stats_custom) *
2719 ISCSI_STATS_CUSTOM_MAX);
Alex Aizman0896b752005-08-04 19:33:07 -07002720 int err = 0;
2721
Mike Christie790f39a2006-05-18 20:31:39 -05002722 priv = iscsi_if_transport_lookup(transport);
2723 if (!priv)
2724 return -EINVAL;
2725
Mike Christieb5c7a122006-04-06 21:13:33 -05002726 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07002727 if (!conn)
2728 return -EEXIST;
2729
2730 do {
2731 int actual_size;
2732
Mike Christie43a145a2006-10-16 18:09:38 -04002733 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002734 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06002735 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
2736 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07002737 return -ENOMEM;
2738 }
2739
Michael Chan43514772009-06-08 18:14:41 -07002740 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07002741 (len - sizeof(*nlhstat)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002742 evstat = nlmsg_data(nlhstat);
Alex Aizman0896b752005-08-04 19:33:07 -07002743 memset(evstat, 0, sizeof(*evstat));
2744 evstat->transport_handle = iscsi_handle(conn->transport);
2745 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05002746 evstat->u.get_stats.cid =
2747 ev->u.get_stats.cid;
2748 evstat->u.get_stats.sid =
2749 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07002750 stats = (struct iscsi_stats *)
2751 ((char*)evstat + sizeof(*evstat));
2752 memset(stats, 0, sizeof(*stats));
2753
Mike Christie7b7232f2006-02-01 21:06:49 -06002754 transport->get_stats(conn, stats);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002755 actual_size = nlmsg_total_size(sizeof(struct iscsi_uevent) +
2756 sizeof(struct iscsi_stats) +
2757 sizeof(struct iscsi_stats_custom) *
2758 stats->custom_length);
Alex Aizman0896b752005-08-04 19:33:07 -07002759 actual_size -= sizeof(*nlhstat);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002760 actual_size = nlmsg_msg_size(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06002761 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07002762 nlhstat->nlmsg_len = actual_size;
2763
Michael Chan43514772009-06-08 18:14:41 -07002764 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
2765 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002766 } while (err < 0 && err != -ECONNREFUSED);
2767
2768 return err;
2769}
2770
Mike Christie53cb8a12006-06-28 12:00:32 -05002771/**
Mike Christie26974782007-12-13 12:43:29 -06002772 * iscsi_session_event - send session destr. completion event
2773 * @session: iscsi class session
2774 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05002775 */
Mike Christie26974782007-12-13 12:43:29 -06002776int iscsi_session_event(struct iscsi_cls_session *session,
2777 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05002778{
2779 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05002780 struct Scsi_Host *shost;
2781 struct iscsi_uevent *ev;
2782 struct sk_buff *skb;
2783 struct nlmsghdr *nlh;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002784 int rc, len = nlmsg_total_size(sizeof(*ev));
Mike Christie53cb8a12006-06-28 12:00:32 -05002785
Mike Christie26974782007-12-13 12:43:29 -06002786 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05002787 if (!priv)
2788 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05002789 shost = iscsi_session_to_shost(session);
2790
Mike Christie43a145a2006-10-16 18:09:38 -04002791 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05002792 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06002793 iscsi_cls_session_printk(KERN_ERR, session,
2794 "Cannot notify userspace of session "
2795 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05002796 return -ENOMEM;
2797 }
2798
Michael Chan43514772009-06-08 18:14:41 -07002799 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002800 ev = nlmsg_data(nlh);
Mike Christie26974782007-12-13 12:43:29 -06002801 ev->transport_handle = iscsi_handle(session->transport);
2802
2803 ev->type = event;
2804 switch (event) {
2805 case ISCSI_KEVENT_DESTROY_SESSION:
2806 ev->r.d_session.host_no = shost->host_no;
2807 ev->r.d_session.sid = session->sid;
2808 break;
2809 case ISCSI_KEVENT_CREATE_SESSION:
2810 ev->r.c_session_ret.host_no = shost->host_no;
2811 ev->r.c_session_ret.sid = session->sid;
2812 break;
2813 case ISCSI_KEVENT_UNBIND_SESSION:
2814 ev->r.unbind_session.host_no = shost->host_no;
2815 ev->r.unbind_session.sid = session->sid;
2816 break;
2817 default:
Mike Christie322d7392008-01-31 13:36:52 -06002818 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
2819 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06002820 kfree_skb(skb);
2821 return -EINVAL;
2822 }
Mike Christie53cb8a12006-06-28 12:00:32 -05002823
2824 /*
2825 * this will occur if the daemon is not up, so we just warn
2826 * the user and when the daemon is restarted it will handle it
2827 */
Michael Chan43514772009-06-08 18:14:41 -07002828 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08002829 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06002830 iscsi_cls_session_printk(KERN_ERR, session,
2831 "Cannot notify userspace of session "
2832 "event %u. Check iscsi daemon\n",
2833 event);
Mike Christie632248a2009-08-20 15:11:01 -05002834
2835 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
2836 event, rc);
Mike Christie53cb8a12006-06-28 12:00:32 -05002837 return rc;
2838}
Mike Christie26974782007-12-13 12:43:29 -06002839EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05002840
Alex Aizman0896b752005-08-04 19:33:07 -07002841static int
Mike Christied82ff9be2008-05-21 15:54:13 -05002842iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
Mike Christie0c70d842011-12-05 16:44:01 -06002843 struct iscsi_uevent *ev, pid_t pid,
2844 uint32_t initial_cmdsn, uint16_t cmds_max,
2845 uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06002846{
2847 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06002848 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06002849 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06002850
Mike Christied82ff9be2008-05-21 15:54:13 -05002851 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06002852 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06002853 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06002854 return -ENOMEM;
2855
Mike Christie0c70d842011-12-05 16:44:01 -06002856 session->creator = pid;
Mike Christie5e7facb2009-03-05 14:46:06 -06002857 shost = iscsi_session_to_shost(session);
2858 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05002859 ev->r.c_session_ret.sid = session->sid;
Mike Christie632248a2009-08-20 15:11:01 -05002860 ISCSI_DBG_TRANS_SESSION(session,
2861 "Completed creating transport session\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002862 return 0;
2863}
2864
2865static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002866iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06002867{
Mike Christie7b8631b2006-01-13 18:05:50 -06002868 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06002869 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06002870
Mike Christieb5c7a122006-04-06 21:13:33 -05002871 session = iscsi_session_lookup(ev->u.c_conn.sid);
2872 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002873 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05002874 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06002875 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05002876 }
Mike Christie7b8631b2006-01-13 18:05:50 -06002877
Mike Christie7b7232f2006-02-01 21:06:49 -06002878 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05002879 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06002880 iscsi_cls_session_printk(KERN_ERR, session,
2881 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06002882 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05002883 }
Mike Christie7b8631b2006-01-13 18:05:50 -06002884
Mike Christieb5c7a122006-04-06 21:13:33 -05002885 ev->r.c_conn_ret.sid = session->sid;
2886 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie632248a2009-08-20 15:11:01 -05002887
2888 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002889 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06002890}
2891
2892static int
2893iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2894{
Mike Christie7b8631b2006-01-13 18:05:50 -06002895 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06002896
Mike Christieb5c7a122006-04-06 21:13:33 -05002897 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06002898 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06002899 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06002900
Mike Christie23d6fef2021-05-25 13:18:00 -05002901 ISCSI_DBG_TRANS_CONN(conn, "Flushing cleanup during destruction\n");
2902 flush_work(&conn->cleanup_work);
Mike Christie632248a2009-08-20 15:11:01 -05002903 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04002904
Mike Christie7b8631b2006-01-13 18:05:50 -06002905 if (transport->destroy_conn)
2906 transport->destroy_conn(conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06002907 return 0;
2908}
2909
Mike Christiefd7255f2006-04-06 21:13:36 -05002910static int
2911iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2912{
2913 char *data = (char*)ev + sizeof(*ev);
2914 struct iscsi_cls_conn *conn;
2915 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05002916 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05002917
Chris Leechec98ea72021-02-23 18:00:17 -08002918 if (ev->u.set_param.len > PAGE_SIZE)
2919 return -EINVAL;
2920
Mike Christiefd7255f2006-04-06 21:13:36 -05002921 session = iscsi_session_lookup(ev->u.set_param.sid);
2922 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
2923 if (!conn || !session)
2924 return -EINVAL;
2925
2926 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05002927 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05002928 sscanf(data, "%d", &value);
Chris Leech9c8108a2015-06-16 16:07:13 -07002929 if (!session->recovery_tmo_sysfs_override)
2930 session->recovery_tmo = value;
Mike Christie30a6c652006-04-06 21:13:39 -05002931 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002932 default:
Gulam Mohamed9e676002021-03-25 09:32:48 +00002933 if ((conn->state == ISCSI_CONN_BOUND) ||
2934 (conn->state == ISCSI_CONN_UP)) {
2935 err = transport->set_param(conn, ev->u.set_param.param,
2936 data, ev->u.set_param.len);
2937 } else {
2938 return -ENOTCONN;
2939 }
Mike Christiefd7255f2006-04-06 21:13:36 -05002940 }
2941
2942 return err;
2943}
2944
Mike Christie10eb0f02009-05-13 17:57:38 -05002945static int iscsi_if_ep_connect(struct iscsi_transport *transport,
2946 struct iscsi_uevent *ev, int msg_type)
2947{
2948 struct iscsi_endpoint *ep;
2949 struct sockaddr *dst_addr;
2950 struct Scsi_Host *shost = NULL;
2951 int non_blocking, err = 0;
2952
2953 if (!transport->ep_connect)
2954 return -EINVAL;
2955
2956 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
2957 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
2958 if (!shost) {
2959 printk(KERN_ERR "ep connect failed. Could not find "
2960 "host no %u\n",
2961 ev->u.ep_connect_through_host.host_no);
2962 return -ENODEV;
2963 }
2964 non_blocking = ev->u.ep_connect_through_host.non_blocking;
2965 } else
2966 non_blocking = ev->u.ep_connect.non_blocking;
2967
2968 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
2969 ep = transport->ep_connect(shost, dst_addr, non_blocking);
2970 if (IS_ERR(ep)) {
2971 err = PTR_ERR(ep);
2972 goto release_host;
2973 }
2974
2975 ev->r.ep_connect_ret.handle = ep->id;
2976release_host:
2977 if (shost)
2978 scsi_host_put(shost);
2979 return err;
2980}
2981
Mike Christie22a39fb2011-02-16 15:04:33 -06002982static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
Mike Christie23d6fef2021-05-25 13:18:00 -05002983 u64 ep_handle)
Mike Christie22a39fb2011-02-16 15:04:33 -06002984{
2985 struct iscsi_cls_conn *conn;
2986 struct iscsi_endpoint *ep;
2987
2988 if (!transport->ep_disconnect)
2989 return -EINVAL;
2990
2991 ep = iscsi_lookup_endpoint(ep_handle);
2992 if (!ep)
2993 return -EINVAL;
Mike Christie891e2632021-05-25 13:17:55 -05002994
Mike Christie23d6fef2021-05-25 13:18:00 -05002995 conn = ep->conn;
2996 if (!conn) {
2997 /*
2998 * conn was not even bound yet, so we can't get iscsi conn
2999 * failures yet.
3000 */
3001 transport->ep_disconnect(ep);
3002 goto put_ep;
Mike Christie22a39fb2011-02-16 15:04:33 -06003003 }
3004
Mike Christie23d6fef2021-05-25 13:18:00 -05003005 mutex_lock(&conn->ep_mutex);
3006 /* Check if this was a conn error and the kernel took ownership */
3007 if (test_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags)) {
3008 ISCSI_DBG_TRANS_CONN(conn, "flush kernel conn cleanup.\n");
3009 mutex_unlock(&conn->ep_mutex);
3010
3011 flush_work(&conn->cleanup_work);
3012 goto put_ep;
3013 }
3014
3015 iscsi_ep_disconnect(conn, false);
3016 mutex_unlock(&conn->ep_mutex);
3017put_ep:
Mike Christie9e5fe1702021-05-25 13:17:59 -05003018 iscsi_put_endpoint(ep);
Mike Christie22a39fb2011-02-16 15:04:33 -06003019 return 0;
3020}
3021
Mike Christie7b8631b2006-01-13 18:05:50 -06003022static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05003023iscsi_if_transport_ep(struct iscsi_transport *transport,
3024 struct iscsi_uevent *ev, int msg_type)
3025{
Mike Christied82ff9be2008-05-21 15:54:13 -05003026 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05003027 int rc = 0;
3028
3029 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05003030 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05003031 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05003032 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05003033 break;
3034 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
3035 if (!transport->ep_poll)
3036 return -EINVAL;
3037
Mike Christied82ff9be2008-05-21 15:54:13 -05003038 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
3039 if (!ep)
3040 return -EINVAL;
3041
3042 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05003043 ev->u.ep_poll.timeout_ms);
Mike Christie9e5fe1702021-05-25 13:17:59 -05003044 iscsi_put_endpoint(ep);
Or Gerlitz264faaa2006-05-02 19:46:36 -05003045 break;
3046 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie22a39fb2011-02-16 15:04:33 -06003047 rc = iscsi_if_ep_disconnect(transport,
Mike Christie23d6fef2021-05-25 13:18:00 -05003048 ev->u.ep_disconnect.ep_handle);
Or Gerlitz264faaa2006-05-02 19:46:36 -05003049 break;
3050 }
3051 return rc;
3052}
3053
3054static int
Mike Christie01cb2252006-06-28 12:00:22 -05003055iscsi_tgt_dscvr(struct iscsi_transport *transport,
3056 struct iscsi_uevent *ev)
3057{
Mike Christie2174a042007-05-30 12:57:10 -05003058 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05003059 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05003060 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05003061
3062 if (!transport->tgt_dscvr)
3063 return -EINVAL;
3064
Mike Christie2174a042007-05-30 12:57:10 -05003065 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04003066 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05003067 printk(KERN_ERR "target discovery could not find host no %u\n",
3068 ev->u.tgt_dscvr.host_no);
3069 return -ENODEV;
3070 }
3071
3072
Mike Christie01cb2252006-06-28 12:00:22 -05003073 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05003074 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
3075 ev->u.tgt_dscvr.enable, dst_addr);
3076 scsi_host_put(shost);
3077 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05003078}
3079
3080static int
Mike Christie1d9bf132007-05-30 12:57:11 -05003081iscsi_set_host_param(struct iscsi_transport *transport,
3082 struct iscsi_uevent *ev)
3083{
3084 char *data = (char*)ev + sizeof(*ev);
3085 struct Scsi_Host *shost;
3086 int err;
3087
3088 if (!transport->set_host_param)
3089 return -ENOSYS;
3090
Chris Leechec98ea72021-02-23 18:00:17 -08003091 if (ev->u.set_host_param.len > PAGE_SIZE)
3092 return -EINVAL;
3093
Mike Christie1d9bf132007-05-30 12:57:11 -05003094 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04003095 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05003096 printk(KERN_ERR "set_host_param could not find host no %u\n",
3097 ev->u.set_host_param.host_no);
3098 return -ENODEV;
3099 }
3100
3101 err = transport->set_host_param(shost, ev->u.set_host_param.param,
3102 data, ev->u.set_host_param.len);
3103 scsi_host_put(shost);
3104 return err;
3105}
3106
3107static int
Michael Chan43514772009-06-08 18:14:41 -07003108iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
3109{
3110 struct Scsi_Host *shost;
3111 struct iscsi_path *params;
3112 int err;
3113
3114 if (!transport->set_path)
3115 return -ENOSYS;
3116
3117 shost = scsi_host_lookup(ev->u.set_path.host_no);
3118 if (!shost) {
3119 printk(KERN_ERR "set path could not find host no %u\n",
3120 ev->u.set_path.host_no);
3121 return -ENODEV;
3122 }
3123
3124 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
3125 err = transport->set_path(shost, params);
3126
3127 scsi_host_put(shost);
3128 return err;
3129}
3130
Nick Black54155ed2019-12-26 15:31:48 -05003131static int iscsi_session_has_conns(int sid)
3132{
3133 struct iscsi_cls_conn *conn;
3134 unsigned long flags;
3135 int found = 0;
3136
3137 spin_lock_irqsave(&connlock, flags);
3138 list_for_each_entry(conn, &connlist, conn_list) {
3139 if (iscsi_conn_get_sid(conn) == sid) {
3140 found = 1;
3141 break;
3142 }
3143 }
3144 spin_unlock_irqrestore(&connlock, flags);
3145
3146 return found;
3147}
3148
Michael Chan43514772009-06-08 18:14:41 -07003149static int
Mike Christie56c155b2011-07-25 13:48:37 -05003150iscsi_set_iface_params(struct iscsi_transport *transport,
Mike Christie00c31882011-10-06 03:56:59 -05003151 struct iscsi_uevent *ev, uint32_t len)
Mike Christie56c155b2011-07-25 13:48:37 -05003152{
3153 char *data = (char *)ev + sizeof(*ev);
3154 struct Scsi_Host *shost;
3155 int err;
3156
3157 if (!transport->set_iface_param)
3158 return -ENOSYS;
3159
3160 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
3161 if (!shost) {
3162 printk(KERN_ERR "set_iface_params could not find host no %u\n",
3163 ev->u.set_iface_params.host_no);
3164 return -ENODEV;
3165 }
3166
Mike Christie00c31882011-10-06 03:56:59 -05003167 err = transport->set_iface_param(shost, data, len);
Mike Christie56c155b2011-07-25 13:48:37 -05003168 scsi_host_put(shost);
3169 return err;
3170}
3171
3172static int
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05303173iscsi_send_ping(struct iscsi_transport *transport, struct iscsi_uevent *ev)
3174{
3175 struct Scsi_Host *shost;
3176 struct sockaddr *dst_addr;
3177 int err;
3178
3179 if (!transport->send_ping)
3180 return -ENOSYS;
3181
3182 shost = scsi_host_lookup(ev->u.iscsi_ping.host_no);
3183 if (!shost) {
3184 printk(KERN_ERR "iscsi_ping could not find host no %u\n",
3185 ev->u.iscsi_ping.host_no);
3186 return -ENODEV;
3187 }
3188
3189 dst_addr = (struct sockaddr *)((char *)ev + sizeof(*ev));
3190 err = transport->send_ping(shost, ev->u.iscsi_ping.iface_num,
3191 ev->u.iscsi_ping.iface_type,
3192 ev->u.iscsi_ping.payload_size,
3193 ev->u.iscsi_ping.pid,
3194 dst_addr);
3195 scsi_host_put(shost);
3196 return err;
3197}
3198
3199static int
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003200iscsi_get_chap(struct iscsi_transport *transport, struct nlmsghdr *nlh)
3201{
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003202 struct iscsi_uevent *ev = nlmsg_data(nlh);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003203 struct Scsi_Host *shost = NULL;
3204 struct iscsi_chap_rec *chap_rec;
3205 struct iscsi_internal *priv;
3206 struct sk_buff *skbchap;
3207 struct nlmsghdr *nlhchap;
3208 struct iscsi_uevent *evchap;
3209 uint32_t chap_buf_size;
3210 int len, err = 0;
3211 char *buf;
3212
3213 if (!transport->get_chap)
3214 return -EINVAL;
3215
3216 priv = iscsi_if_transport_lookup(transport);
3217 if (!priv)
3218 return -EINVAL;
3219
3220 chap_buf_size = (ev->u.get_chap.num_entries * sizeof(*chap_rec));
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003221 len = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003222
3223 shost = scsi_host_lookup(ev->u.get_chap.host_no);
3224 if (!shost) {
Masanari Iida804ff602015-05-07 23:21:27 +09003225 printk(KERN_ERR "%s: failed. Could not find host no %u\n",
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003226 __func__, ev->u.get_chap.host_no);
3227 return -ENODEV;
3228 }
3229
3230 do {
3231 int actual_size;
3232
3233 skbchap = alloc_skb(len, GFP_KERNEL);
3234 if (!skbchap) {
3235 printk(KERN_ERR "can not deliver chap: OOM\n");
3236 err = -ENOMEM;
3237 goto exit_get_chap;
3238 }
3239
3240 nlhchap = __nlmsg_put(skbchap, 0, 0, 0,
3241 (len - sizeof(*nlhchap)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003242 evchap = nlmsg_data(nlhchap);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003243 memset(evchap, 0, sizeof(*evchap));
3244 evchap->transport_handle = iscsi_handle(transport);
3245 evchap->type = nlh->nlmsg_type;
3246 evchap->u.get_chap.host_no = ev->u.get_chap.host_no;
3247 evchap->u.get_chap.chap_tbl_idx = ev->u.get_chap.chap_tbl_idx;
3248 evchap->u.get_chap.num_entries = ev->u.get_chap.num_entries;
Nick Black' via open-iscsia799cb72014-07-01 11:24:37 -05003249 buf = (char *)evchap + sizeof(*evchap);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003250 memset(buf, 0, chap_buf_size);
3251
3252 err = transport->get_chap(shost, ev->u.get_chap.chap_tbl_idx,
3253 &evchap->u.get_chap.num_entries, buf);
3254
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003255 actual_size = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003256 skb_trim(skbchap, NLMSG_ALIGN(actual_size));
3257 nlhchap->nlmsg_len = actual_size;
3258
3259 err = iscsi_multicast_skb(skbchap, ISCSI_NL_GRP_ISCSID,
3260 GFP_KERNEL);
3261 } while (err < 0 && err != -ECONNREFUSED);
3262
3263exit_get_chap:
3264 scsi_host_put(shost);
3265 return err;
3266}
3267
Adheer Chandravanshi3af142f2013-09-17 07:54:48 -04003268static int iscsi_set_chap(struct iscsi_transport *transport,
3269 struct iscsi_uevent *ev, uint32_t len)
3270{
3271 char *data = (char *)ev + sizeof(*ev);
3272 struct Scsi_Host *shost;
3273 int err = 0;
3274
3275 if (!transport->set_chap)
3276 return -ENOSYS;
3277
3278 shost = scsi_host_lookup(ev->u.set_path.host_no);
3279 if (!shost) {
3280 pr_err("%s could not find host no %u\n",
3281 __func__, ev->u.set_path.host_no);
3282 return -ENODEV;
3283 }
3284
3285 err = transport->set_chap(shost, data, len);
3286 scsi_host_put(shost);
3287 return err;
3288}
3289
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003290static int iscsi_delete_chap(struct iscsi_transport *transport,
3291 struct iscsi_uevent *ev)
3292{
3293 struct Scsi_Host *shost;
3294 int err = 0;
3295
3296 if (!transport->delete_chap)
3297 return -ENOSYS;
3298
3299 shost = scsi_host_lookup(ev->u.delete_chap.host_no);
3300 if (!shost) {
3301 printk(KERN_ERR "%s could not find host no %u\n",
3302 __func__, ev->u.delete_chap.host_no);
3303 return -ENODEV;
3304 }
3305
3306 err = transport->delete_chap(shost, ev->u.delete_chap.chap_tbl_idx);
3307 scsi_host_put(shost);
3308 return err;
3309}
3310
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003311static const struct {
3312 enum iscsi_discovery_parent_type value;
3313 char *name;
3314} iscsi_discovery_parent_names[] = {
3315 {ISCSI_DISC_PARENT_UNKNOWN, "Unknown" },
3316 {ISCSI_DISC_PARENT_SENDTGT, "Sendtarget" },
3317 {ISCSI_DISC_PARENT_ISNS, "isns" },
3318};
3319
3320char *iscsi_get_discovery_parent_name(int parent_type)
3321{
3322 int i;
3323 char *state = "Unknown!";
3324
3325 for (i = 0; i < ARRAY_SIZE(iscsi_discovery_parent_names); i++) {
3326 if (iscsi_discovery_parent_names[i].value & parent_type) {
3327 state = iscsi_discovery_parent_names[i].name;
3328 break;
3329 }
3330 }
3331 return state;
3332}
3333EXPORT_SYMBOL_GPL(iscsi_get_discovery_parent_name);
3334
3335static int iscsi_set_flashnode_param(struct iscsi_transport *transport,
3336 struct iscsi_uevent *ev, uint32_t len)
3337{
3338 char *data = (char *)ev + sizeof(*ev);
3339 struct Scsi_Host *shost;
3340 struct iscsi_bus_flash_session *fnode_sess;
3341 struct iscsi_bus_flash_conn *fnode_conn;
3342 struct device *dev;
Mike Christie8526cb12013-05-06 12:06:56 -05003343 uint32_t idx;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003344 int err = 0;
3345
3346 if (!transport->set_flashnode_param) {
3347 err = -ENOSYS;
3348 goto exit_set_fnode;
3349 }
3350
3351 shost = scsi_host_lookup(ev->u.set_flashnode.host_no);
3352 if (!shost) {
3353 pr_err("%s could not find host no %u\n",
3354 __func__, ev->u.set_flashnode.host_no);
3355 err = -ENODEV;
Jing Xiangfeng68e12e5f2020-06-15 16:12:26 +08003356 goto exit_set_fnode;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003357 }
3358
Mike Christie8526cb12013-05-06 12:06:56 -05003359 idx = ev->u.set_flashnode.flashnode_idx;
3360 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003361 if (!fnode_sess) {
3362 pr_err("%s could not find flashnode %u for host no %u\n",
Mike Christie8526cb12013-05-06 12:06:56 -05003363 __func__, idx, ev->u.set_flashnode.host_no);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003364 err = -ENODEV;
3365 goto put_host;
3366 }
3367
Mike Christie8526cb12013-05-06 12:06:56 -05003368 dev = iscsi_find_flashnode_conn(fnode_sess);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003369 if (!dev) {
3370 err = -ENODEV;
Mike Christie8526cb12013-05-06 12:06:56 -05003371 goto put_sess;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003372 }
3373
3374 fnode_conn = iscsi_dev_to_flash_conn(dev);
3375 err = transport->set_flashnode_param(fnode_sess, fnode_conn, data, len);
Mike Christie8526cb12013-05-06 12:06:56 -05003376 put_device(dev);
3377
3378put_sess:
3379 put_device(&fnode_sess->dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003380
3381put_host:
3382 scsi_host_put(shost);
3383
3384exit_set_fnode:
3385 return err;
3386}
3387
3388static int iscsi_new_flashnode(struct iscsi_transport *transport,
3389 struct iscsi_uevent *ev, uint32_t len)
3390{
3391 char *data = (char *)ev + sizeof(*ev);
3392 struct Scsi_Host *shost;
3393 int index;
3394 int err = 0;
3395
3396 if (!transport->new_flashnode) {
3397 err = -ENOSYS;
3398 goto exit_new_fnode;
3399 }
3400
3401 shost = scsi_host_lookup(ev->u.new_flashnode.host_no);
3402 if (!shost) {
3403 pr_err("%s could not find host no %u\n",
3404 __func__, ev->u.new_flashnode.host_no);
3405 err = -ENODEV;
3406 goto put_host;
3407 }
3408
3409 index = transport->new_flashnode(shost, data, len);
3410
3411 if (index >= 0)
3412 ev->r.new_flashnode_ret.flashnode_idx = index;
3413 else
3414 err = -EIO;
3415
3416put_host:
3417 scsi_host_put(shost);
3418
3419exit_new_fnode:
3420 return err;
3421}
3422
3423static int iscsi_del_flashnode(struct iscsi_transport *transport,
3424 struct iscsi_uevent *ev)
3425{
3426 struct Scsi_Host *shost;
3427 struct iscsi_bus_flash_session *fnode_sess;
Mike Christie8526cb12013-05-06 12:06:56 -05003428 uint32_t idx;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003429 int err = 0;
3430
3431 if (!transport->del_flashnode) {
3432 err = -ENOSYS;
3433 goto exit_del_fnode;
3434 }
3435
3436 shost = scsi_host_lookup(ev->u.del_flashnode.host_no);
3437 if (!shost) {
3438 pr_err("%s could not find host no %u\n",
3439 __func__, ev->u.del_flashnode.host_no);
3440 err = -ENODEV;
3441 goto put_host;
3442 }
3443
Mike Christie8526cb12013-05-06 12:06:56 -05003444 idx = ev->u.del_flashnode.flashnode_idx;
3445 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003446 if (!fnode_sess) {
3447 pr_err("%s could not find flashnode %u for host no %u\n",
Mike Christie8526cb12013-05-06 12:06:56 -05003448 __func__, idx, ev->u.del_flashnode.host_no);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003449 err = -ENODEV;
3450 goto put_host;
3451 }
3452
3453 err = transport->del_flashnode(fnode_sess);
Mike Christie8526cb12013-05-06 12:06:56 -05003454 put_device(&fnode_sess->dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003455
3456put_host:
3457 scsi_host_put(shost);
3458
3459exit_del_fnode:
3460 return err;
3461}
3462
3463static int iscsi_login_flashnode(struct iscsi_transport *transport,
3464 struct iscsi_uevent *ev)
3465{
3466 struct Scsi_Host *shost;
3467 struct iscsi_bus_flash_session *fnode_sess;
3468 struct iscsi_bus_flash_conn *fnode_conn;
3469 struct device *dev;
Mike Christie8526cb12013-05-06 12:06:56 -05003470 uint32_t idx;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003471 int err = 0;
3472
3473 if (!transport->login_flashnode) {
3474 err = -ENOSYS;
3475 goto exit_login_fnode;
3476 }
3477
3478 shost = scsi_host_lookup(ev->u.login_flashnode.host_no);
3479 if (!shost) {
3480 pr_err("%s could not find host no %u\n",
3481 __func__, ev->u.login_flashnode.host_no);
3482 err = -ENODEV;
3483 goto put_host;
3484 }
3485
Mike Christie8526cb12013-05-06 12:06:56 -05003486 idx = ev->u.login_flashnode.flashnode_idx;
3487 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003488 if (!fnode_sess) {
3489 pr_err("%s could not find flashnode %u for host no %u\n",
Mike Christie8526cb12013-05-06 12:06:56 -05003490 __func__, idx, ev->u.login_flashnode.host_no);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003491 err = -ENODEV;
3492 goto put_host;
3493 }
3494
Mike Christie8526cb12013-05-06 12:06:56 -05003495 dev = iscsi_find_flashnode_conn(fnode_sess);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003496 if (!dev) {
3497 err = -ENODEV;
Mike Christie8526cb12013-05-06 12:06:56 -05003498 goto put_sess;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003499 }
3500
3501 fnode_conn = iscsi_dev_to_flash_conn(dev);
3502 err = transport->login_flashnode(fnode_sess, fnode_conn);
Mike Christie8526cb12013-05-06 12:06:56 -05003503 put_device(dev);
3504
3505put_sess:
3506 put_device(&fnode_sess->dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003507
3508put_host:
3509 scsi_host_put(shost);
3510
3511exit_login_fnode:
3512 return err;
3513}
3514
3515static int iscsi_logout_flashnode(struct iscsi_transport *transport,
3516 struct iscsi_uevent *ev)
3517{
3518 struct Scsi_Host *shost;
3519 struct iscsi_bus_flash_session *fnode_sess;
3520 struct iscsi_bus_flash_conn *fnode_conn;
3521 struct device *dev;
Mike Christie8526cb12013-05-06 12:06:56 -05003522 uint32_t idx;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003523 int err = 0;
3524
3525 if (!transport->logout_flashnode) {
3526 err = -ENOSYS;
3527 goto exit_logout_fnode;
3528 }
3529
3530 shost = scsi_host_lookup(ev->u.logout_flashnode.host_no);
3531 if (!shost) {
3532 pr_err("%s could not find host no %u\n",
3533 __func__, ev->u.logout_flashnode.host_no);
3534 err = -ENODEV;
3535 goto put_host;
3536 }
3537
Mike Christie8526cb12013-05-06 12:06:56 -05003538 idx = ev->u.logout_flashnode.flashnode_idx;
3539 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003540 if (!fnode_sess) {
3541 pr_err("%s could not find flashnode %u for host no %u\n",
Mike Christie8526cb12013-05-06 12:06:56 -05003542 __func__, idx, ev->u.logout_flashnode.host_no);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003543 err = -ENODEV;
3544 goto put_host;
3545 }
3546
Mike Christie8526cb12013-05-06 12:06:56 -05003547 dev = iscsi_find_flashnode_conn(fnode_sess);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003548 if (!dev) {
3549 err = -ENODEV;
Mike Christie8526cb12013-05-06 12:06:56 -05003550 goto put_sess;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003551 }
3552
3553 fnode_conn = iscsi_dev_to_flash_conn(dev);
3554
3555 err = transport->logout_flashnode(fnode_sess, fnode_conn);
Mike Christie8526cb12013-05-06 12:06:56 -05003556 put_device(dev);
3557
3558put_sess:
3559 put_device(&fnode_sess->dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003560
3561put_host:
3562 scsi_host_put(shost);
3563
3564exit_logout_fnode:
3565 return err;
3566}
3567
3568static int iscsi_logout_flashnode_sid(struct iscsi_transport *transport,
3569 struct iscsi_uevent *ev)
3570{
3571 struct Scsi_Host *shost;
3572 struct iscsi_cls_session *session;
3573 int err = 0;
3574
3575 if (!transport->logout_flashnode_sid) {
3576 err = -ENOSYS;
3577 goto exit_logout_sid;
3578 }
3579
3580 shost = scsi_host_lookup(ev->u.logout_flashnode_sid.host_no);
3581 if (!shost) {
3582 pr_err("%s could not find host no %u\n",
3583 __func__, ev->u.logout_flashnode.host_no);
3584 err = -ENODEV;
3585 goto put_host;
3586 }
3587
3588 session = iscsi_session_lookup(ev->u.logout_flashnode_sid.sid);
3589 if (!session) {
3590 pr_err("%s could not find session id %u\n",
3591 __func__, ev->u.logout_flashnode_sid.sid);
3592 err = -EINVAL;
3593 goto put_host;
3594 }
3595
3596 err = transport->logout_flashnode_sid(session);
3597
3598put_host:
3599 scsi_host_put(shost);
3600
3601exit_logout_sid:
3602 return err;
3603}
3604
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003605static int
Lalit Chandivade6fa7c552013-11-22 05:28:18 -05003606iscsi_get_host_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
3607{
3608 struct iscsi_uevent *ev = nlmsg_data(nlh);
3609 struct Scsi_Host *shost = NULL;
3610 struct iscsi_internal *priv;
3611 struct sk_buff *skbhost_stats;
3612 struct nlmsghdr *nlhhost_stats;
3613 struct iscsi_uevent *evhost_stats;
3614 int host_stats_size = 0;
3615 int len, err = 0;
3616 char *buf;
3617
3618 if (!transport->get_host_stats)
Mike Christie719e5872014-07-12 15:51:50 -05003619 return -ENOSYS;
Lalit Chandivade6fa7c552013-11-22 05:28:18 -05003620
3621 priv = iscsi_if_transport_lookup(transport);
3622 if (!priv)
3623 return -EINVAL;
3624
3625 host_stats_size = sizeof(struct iscsi_offload_host_stats);
3626 len = nlmsg_total_size(sizeof(*ev) + host_stats_size);
3627
3628 shost = scsi_host_lookup(ev->u.get_host_stats.host_no);
3629 if (!shost) {
Arvind Yadav013ee632017-10-29 11:39:46 +05303630 pr_err("%s: failed. Could not find host no %u\n",
Lalit Chandivade6fa7c552013-11-22 05:28:18 -05003631 __func__, ev->u.get_host_stats.host_no);
3632 return -ENODEV;
3633 }
3634
3635 do {
3636 int actual_size;
3637
3638 skbhost_stats = alloc_skb(len, GFP_KERNEL);
3639 if (!skbhost_stats) {
3640 pr_err("cannot deliver host stats: OOM\n");
3641 err = -ENOMEM;
3642 goto exit_host_stats;
3643 }
3644
3645 nlhhost_stats = __nlmsg_put(skbhost_stats, 0, 0, 0,
3646 (len - sizeof(*nlhhost_stats)), 0);
3647 evhost_stats = nlmsg_data(nlhhost_stats);
3648 memset(evhost_stats, 0, sizeof(*evhost_stats));
3649 evhost_stats->transport_handle = iscsi_handle(transport);
3650 evhost_stats->type = nlh->nlmsg_type;
3651 evhost_stats->u.get_host_stats.host_no =
3652 ev->u.get_host_stats.host_no;
Nick Black' via open-iscsia799cb72014-07-01 11:24:37 -05003653 buf = (char *)evhost_stats + sizeof(*evhost_stats);
Lalit Chandivade6fa7c552013-11-22 05:28:18 -05003654 memset(buf, 0, host_stats_size);
3655
3656 err = transport->get_host_stats(shost, buf, host_stats_size);
Mike Christie2f7608f2014-07-12 15:51:48 -05003657 if (err) {
Mike Christiebfb50872014-08-03 18:37:52 -05003658 kfree_skb(skbhost_stats);
Mike Christie2f7608f2014-07-12 15:51:48 -05003659 goto exit_host_stats;
3660 }
Lalit Chandivade6fa7c552013-11-22 05:28:18 -05003661
3662 actual_size = nlmsg_total_size(sizeof(*ev) + host_stats_size);
3663 skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));
3664 nlhhost_stats->nlmsg_len = actual_size;
3665
3666 err = iscsi_multicast_skb(skbhost_stats, ISCSI_NL_GRP_ISCSID,
3667 GFP_KERNEL);
3668 } while (err < 0 && err != -ECONNREFUSED);
3669
3670exit_host_stats:
3671 scsi_host_put(shost);
3672 return err;
3673}
3674
Mike Christie23d6fef2021-05-25 13:18:00 -05003675static int iscsi_if_transport_conn(struct iscsi_transport *transport,
3676 struct nlmsghdr *nlh)
3677{
3678 struct iscsi_uevent *ev = nlmsg_data(nlh);
3679 struct iscsi_cls_session *session;
3680 struct iscsi_cls_conn *conn = NULL;
3681 struct iscsi_endpoint *ep;
3682 uint32_t pdu_len;
3683 int err = 0;
3684
3685 switch (nlh->nlmsg_type) {
3686 case ISCSI_UEVENT_CREATE_CONN:
3687 return iscsi_if_create_conn(transport, ev);
3688 case ISCSI_UEVENT_DESTROY_CONN:
3689 return iscsi_if_destroy_conn(transport, ev);
3690 case ISCSI_UEVENT_STOP_CONN:
3691 return iscsi_if_stop_conn(transport, ev);
3692 }
3693
3694 /*
3695 * The following cmds need to be run under the ep_mutex so in kernel
3696 * conn cleanup (ep_disconnect + unbind and conn) is not done while
3697 * these are running. They also must not run if we have just run a conn
3698 * cleanup because they would set the state in a way that might allow
3699 * IO or send IO themselves.
3700 */
3701 switch (nlh->nlmsg_type) {
3702 case ISCSI_UEVENT_START_CONN:
3703 conn = iscsi_conn_lookup(ev->u.start_conn.sid,
3704 ev->u.start_conn.cid);
3705 break;
3706 case ISCSI_UEVENT_BIND_CONN:
3707 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
3708 break;
3709 case ISCSI_UEVENT_SEND_PDU:
3710 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
3711 break;
3712 }
3713
3714 if (!conn)
3715 return -EINVAL;
3716
3717 mutex_lock(&conn->ep_mutex);
3718 if (test_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags)) {
3719 mutex_unlock(&conn->ep_mutex);
3720 ev->r.retcode = -ENOTCONN;
3721 return 0;
3722 }
3723
3724 switch (nlh->nlmsg_type) {
3725 case ISCSI_UEVENT_BIND_CONN:
3726 if (conn->ep) {
3727 /*
3728 * For offload boot support where iscsid is restarted
3729 * during the pivot root stage, the ep will be intact
3730 * here when the new iscsid instance starts up and
3731 * reconnects.
3732 */
3733 iscsi_ep_disconnect(conn, true);
3734 }
3735
3736 session = iscsi_session_lookup(ev->u.b_conn.sid);
3737 if (!session) {
3738 err = -EINVAL;
3739 break;
3740 }
3741
3742 ev->r.retcode = transport->bind_conn(session, conn,
3743 ev->u.b_conn.transport_eph,
3744 ev->u.b_conn.is_leading);
3745 if (!ev->r.retcode)
3746 conn->state = ISCSI_CONN_BOUND;
3747
3748 if (ev->r.retcode || !transport->ep_connect)
3749 break;
3750
3751 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
3752 if (ep) {
3753 ep->conn = conn;
3754 conn->ep = ep;
3755 iscsi_put_endpoint(ep);
3756 } else {
3757 err = -ENOTCONN;
3758 iscsi_cls_conn_printk(KERN_ERR, conn,
3759 "Could not set ep conn binding\n");
3760 }
3761 break;
3762 case ISCSI_UEVENT_START_CONN:
3763 ev->r.retcode = transport->start_conn(conn);
3764 if (!ev->r.retcode)
3765 conn->state = ISCSI_CONN_UP;
3766 break;
3767 case ISCSI_UEVENT_SEND_PDU:
3768 pdu_len = nlh->nlmsg_len - sizeof(*nlh) - sizeof(*ev);
3769
3770 if ((ev->u.send_pdu.hdr_size > pdu_len) ||
3771 (ev->u.send_pdu.data_size > (pdu_len - ev->u.send_pdu.hdr_size))) {
3772 err = -EINVAL;
3773 break;
3774 }
3775
3776 ev->r.retcode = transport->send_pdu(conn,
3777 (struct iscsi_hdr *)((char *)ev + sizeof(*ev)),
3778 (char *)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
3779 ev->u.send_pdu.data_size);
3780 break;
3781 default:
3782 err = -ENOSYS;
3783 }
3784
3785 mutex_unlock(&conn->ep_mutex);
3786 return err;
3787}
Lalit Chandivade6fa7c552013-11-22 05:28:18 -05003788
3789static int
Michael Chan43514772009-06-08 18:14:41 -07003790iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07003791{
3792 int err = 0;
Chris Leechaf170922018-04-09 15:15:28 -07003793 u32 portid;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003794 struct iscsi_uevent *ev = nlmsg_data(nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07003795 struct iscsi_transport *transport = NULL;
3796 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06003797 struct iscsi_cls_session *session;
Mike Christied82ff9be2008-05-21 15:54:13 -05003798 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07003799
Lee Duncan688e8122021-02-23 13:06:24 -08003800 if (!netlink_capable(skb, CAP_SYS_ADMIN))
3801 return -EPERM;
3802
Michael Chan43514772009-06-08 18:14:41 -07003803 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
3804 *group = ISCSI_NL_GRP_UIP;
3805 else
3806 *group = ISCSI_NL_GRP_ISCSID;
3807
Alex Aizman0896b752005-08-04 19:33:07 -07003808 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
3809 if (!priv)
3810 return -EINVAL;
3811 transport = priv->iscsi_transport;
3812
Mike Christie7b7232f2006-02-01 21:06:49 -06003813 if (!try_module_get(transport->owner))
3814 return -EINVAL;
3815
Chris Leechaf170922018-04-09 15:15:28 -07003816 portid = NETLINK_CB(skb).portid;
3817
Alex Aizman0896b752005-08-04 19:33:07 -07003818 switch (nlh->nlmsg_type) {
3819 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05003820 err = iscsi_if_create_session(priv, ep, ev,
Chris Leechaf170922018-04-09 15:15:28 -07003821 portid,
Mike Christie40753ca2008-05-21 15:53:56 -05003822 ev->u.c_session.initial_cmdsn,
3823 ev->u.c_session.cmds_max,
3824 ev->u.c_session.queue_depth);
3825 break;
3826 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05003827 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05003828 if (!ep) {
3829 err = -EINVAL;
3830 break;
3831 }
Mike Christied82ff9be2008-05-21 15:54:13 -05003832
3833 err = iscsi_if_create_session(priv, ep, ev,
Chris Leechaf170922018-04-09 15:15:28 -07003834 portid,
Mike Christie40753ca2008-05-21 15:53:56 -05003835 ev->u.c_bound_session.initial_cmdsn,
3836 ev->u.c_bound_session.cmds_max,
3837 ev->u.c_bound_session.queue_depth);
Mike Christie9e5fe1702021-05-25 13:17:59 -05003838 iscsi_put_endpoint(ep);
Alex Aizman0896b752005-08-04 19:33:07 -07003839 break;
3840 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05003841 session = iscsi_session_lookup(ev->u.d_session.sid);
Nick Black54155ed2019-12-26 15:31:48 -05003842 if (!session)
Mike Christie26974782007-12-13 12:43:29 -06003843 err = -EINVAL;
Nick Black54155ed2019-12-26 15:31:48 -05003844 else if (iscsi_session_has_conns(ev->u.d_session.sid))
3845 err = -EBUSY;
3846 else
3847 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06003848 break;
Frank Mayharcc6b32e2020-02-27 14:59:45 -05003849 case ISCSI_UEVENT_DESTROY_SESSION_ASYNC:
3850 session = iscsi_session_lookup(ev->u.d_session.sid);
3851 if (!session)
3852 err = -EINVAL;
3853 else if (iscsi_session_has_conns(ev->u.d_session.sid))
3854 err = -EBUSY;
3855 else {
3856 unsigned long flags;
3857
3858 /* Prevent this session from being found again */
3859 spin_lock_irqsave(&sesslock, flags);
Mike Christie93bf02e2020-07-01 14:47:48 -05003860 list_del_init(&session->sess_list);
Frank Mayharcc6b32e2020-02-27 14:59:45 -05003861 spin_unlock_irqrestore(&sesslock, flags);
3862
Mike Christieb25b9572021-05-25 13:17:58 -05003863 queue_work(system_unbound_wq, &session->destroy_work);
Frank Mayharcc6b32e2020-02-27 14:59:45 -05003864 }
3865 break;
Mike Christie26974782007-12-13 12:43:29 -06003866 case ISCSI_UEVENT_UNBIND_SESSION:
3867 session = iscsi_session_lookup(ev->u.d_session.sid);
3868 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06003869 scsi_queue_work(iscsi_session_to_shost(session),
3870 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06003871 else
Mike Christie7b7232f2006-02-01 21:06:49 -06003872 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07003873 break;
Alex Aizman0896b752005-08-04 19:33:07 -07003874 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05003875 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07003876 break;
Mike Christie23d6fef2021-05-25 13:18:00 -05003877 case ISCSI_UEVENT_CREATE_CONN:
3878 case ISCSI_UEVENT_DESTROY_CONN:
Alex Aizman0896b752005-08-04 19:33:07 -07003879 case ISCSI_UEVENT_STOP_CONN:
Mike Christie23d6fef2021-05-25 13:18:00 -05003880 case ISCSI_UEVENT_START_CONN:
3881 case ISCSI_UEVENT_BIND_CONN:
Alex Aizman0896b752005-08-04 19:33:07 -07003882 case ISCSI_UEVENT_SEND_PDU:
Mike Christie23d6fef2021-05-25 13:18:00 -05003883 err = iscsi_if_transport_conn(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07003884 break;
3885 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06003886 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07003887 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05003888 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
3889 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
3890 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05003891 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05003892 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
3893 break;
Mike Christie01cb2252006-06-28 12:00:22 -05003894 case ISCSI_UEVENT_TGT_DSCVR:
3895 err = iscsi_tgt_dscvr(transport, ev);
3896 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05003897 case ISCSI_UEVENT_SET_HOST_PARAM:
3898 err = iscsi_set_host_param(transport, ev);
3899 break;
Michael Chan43514772009-06-08 18:14:41 -07003900 case ISCSI_UEVENT_PATH_UPDATE:
3901 err = iscsi_set_path(transport, ev);
3902 break;
Mike Christie56c155b2011-07-25 13:48:37 -05003903 case ISCSI_UEVENT_SET_IFACE_PARAMS:
Mike Christie00c31882011-10-06 03:56:59 -05003904 err = iscsi_set_iface_params(transport, ev,
3905 nlmsg_attrlen(nlh, sizeof(*ev)));
Mike Christie56c155b2011-07-25 13:48:37 -05003906 break;
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05303907 case ISCSI_UEVENT_PING:
3908 err = iscsi_send_ping(transport, ev);
3909 break;
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003910 case ISCSI_UEVENT_GET_CHAP:
3911 err = iscsi_get_chap(transport, nlh);
3912 break;
3913 case ISCSI_UEVENT_DELETE_CHAP:
3914 err = iscsi_delete_chap(transport, ev);
3915 break;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003916 case ISCSI_UEVENT_SET_FLASHNODE_PARAMS:
3917 err = iscsi_set_flashnode_param(transport, ev,
3918 nlmsg_attrlen(nlh,
3919 sizeof(*ev)));
3920 break;
3921 case ISCSI_UEVENT_NEW_FLASHNODE:
3922 err = iscsi_new_flashnode(transport, ev,
3923 nlmsg_attrlen(nlh, sizeof(*ev)));
3924 break;
3925 case ISCSI_UEVENT_DEL_FLASHNODE:
3926 err = iscsi_del_flashnode(transport, ev);
3927 break;
3928 case ISCSI_UEVENT_LOGIN_FLASHNODE:
3929 err = iscsi_login_flashnode(transport, ev);
3930 break;
3931 case ISCSI_UEVENT_LOGOUT_FLASHNODE:
3932 err = iscsi_logout_flashnode(transport, ev);
3933 break;
3934 case ISCSI_UEVENT_LOGOUT_FLASHNODE_SID:
3935 err = iscsi_logout_flashnode_sid(transport, ev);
3936 break;
Adheer Chandravanshi3af142f2013-09-17 07:54:48 -04003937 case ISCSI_UEVENT_SET_CHAP:
3938 err = iscsi_set_chap(transport, ev,
3939 nlmsg_attrlen(nlh, sizeof(*ev)));
3940 break;
Lalit Chandivade6fa7c552013-11-22 05:28:18 -05003941 case ISCSI_UEVENT_GET_HOST_STATS:
3942 err = iscsi_get_host_stats(transport, nlh);
3943 break;
Alex Aizman0896b752005-08-04 19:33:07 -07003944 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05003945 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07003946 break;
3947 }
3948
Mike Christie7b7232f2006-02-01 21:06:49 -06003949 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07003950 return err;
3951}
3952
Mike Christieb5c7a122006-04-06 21:13:33 -05003953/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003954 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
3955 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05003956 */
Alex Aizman0896b752005-08-04 19:33:07 -07003957static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003958iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07003959{
Chris Leechaf170922018-04-09 15:15:28 -07003960 u32 portid = NETLINK_CB(skb).portid;
3961
Arjan van de Ven0b950672006-01-11 13:16:10 +01003962 mutex_lock(&rx_queue_mutex);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003963 while (skb->len >= NLMSG_HDRLEN) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003964 int err;
3965 uint32_t rlen;
3966 struct nlmsghdr *nlh;
3967 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07003968 uint32_t group;
Bo Wubba340c2019-11-20 13:26:17 +00003969 int retries = ISCSI_SEND_MAX_ALLOWED;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003970
3971 nlh = nlmsg_hdr(skb);
Xin Longc88f0e62017-08-27 20:25:26 +08003972 if (nlh->nlmsg_len < sizeof(*nlh) + sizeof(*ev) ||
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003973 skb->len < nlh->nlmsg_len) {
3974 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06003975 }
Mike Christieee7f8e42006-02-01 21:07:01 -06003976
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003977 ev = nlmsg_data(nlh);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003978 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
3979 if (rlen > skb->len)
3980 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07003981
Michael Chan43514772009-06-08 18:14:41 -07003982 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003983 if (err) {
3984 ev->type = ISCSI_KEVENT_IF_ERROR;
3985 ev->iferror = err;
3986 }
3987 do {
3988 /*
3989 * special case for GET_STATS:
3990 * on success - sending reply and stats from
3991 * inside of if_recv_msg(),
3992 * on error - fall through.
3993 */
3994 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07003995 break;
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003996 if (ev->type == ISCSI_UEVENT_GET_CHAP && !err)
3997 break;
Chris Leechaf170922018-04-09 15:15:28 -07003998 err = iscsi_if_send_reply(portid, nlh->nlmsg_type,
3999 ev, sizeof(*ev));
Bo Wubba340c2019-11-20 13:26:17 +00004000 if (err == -EAGAIN && --retries < 0) {
4001 printk(KERN_WARNING "Send reply failed, error %d\n", err);
4002 break;
4003 }
Sasha Levin46a7c172012-01-25 22:16:16 -05004004 } while (err < 0 && err != -ECONNREFUSED && err != -ESRCH);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004005 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07004006 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01004007 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07004008}
4009
Mike Christiefd7255f2006-04-06 21:13:36 -05004010#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01004011struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05004012 __ATTR(_name,_mode,_show,_store)
4013
Alex Aizman0896b752005-08-04 19:33:07 -07004014/*
4015 * iSCSI connection attrs
4016 */
Mike Christiea54a52c2006-06-28 12:00:23 -05004017#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01004019show_conn_param_##param(struct device *dev, \
4020 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05004021{ \
Tony Jonesee959b02008-02-22 00:13:36 +01004022 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05004023 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05004024 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05004025}
4026
Mike Christiea54a52c2006-06-28 12:00:23 -05004027#define iscsi_conn_attr(field, param) \
4028 iscsi_conn_attr_show(param) \
4029static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05004030 NULL);
4031
Mike Christiea54a52c2006-06-28 12:00:23 -05004032iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
4033iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
4034iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
4035iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
4036iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
4037iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
4038iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
Mike Christiea54a52c2006-06-28 12:00:23 -05004039iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
4040iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06004041iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
4042iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04004043iscsi_conn_attr(local_port, ISCSI_PARAM_LOCAL_PORT);
4044iscsi_conn_attr(statsn, ISCSI_PARAM_STATSN);
4045iscsi_conn_attr(keepalive_tmo, ISCSI_PARAM_KEEPALIVE_TMO);
4046iscsi_conn_attr(max_segment_size, ISCSI_PARAM_MAX_SEGMENT_SIZE);
4047iscsi_conn_attr(tcp_timestamp_stat, ISCSI_PARAM_TCP_TIMESTAMP_STAT);
4048iscsi_conn_attr(tcp_wsf_disable, ISCSI_PARAM_TCP_WSF_DISABLE);
4049iscsi_conn_attr(tcp_nagle_disable, ISCSI_PARAM_TCP_NAGLE_DISABLE);
4050iscsi_conn_attr(tcp_timer_scale, ISCSI_PARAM_TCP_TIMER_SCALE);
4051iscsi_conn_attr(tcp_timestamp_enable, ISCSI_PARAM_TCP_TIMESTAMP_EN);
4052iscsi_conn_attr(fragment_disable, ISCSI_PARAM_IP_FRAGMENT_DISABLE);
4053iscsi_conn_attr(ipv4_tos, ISCSI_PARAM_IPV4_TOS);
4054iscsi_conn_attr(ipv6_traffic_class, ISCSI_PARAM_IPV6_TC);
4055iscsi_conn_attr(ipv6_flow_label, ISCSI_PARAM_IPV6_FLOW_LABEL);
4056iscsi_conn_attr(is_fw_assigned_ipv6, ISCSI_PARAM_IS_FW_ASSIGNED_IPV6);
4057iscsi_conn_attr(tcp_xmit_wsf, ISCSI_PARAM_TCP_XMIT_WSF);
4058iscsi_conn_attr(tcp_recv_wsf, ISCSI_PARAM_TCP_RECV_WSF);
Adheer Chandravanshi5af62f22013-11-22 05:28:20 -05004059iscsi_conn_attr(local_ipaddr, ISCSI_PARAM_LOCAL_IPADDR);
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04004060
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04004061static const char *const connection_state_names[] = {
4062 [ISCSI_CONN_UP] = "up",
4063 [ISCSI_CONN_DOWN] = "down",
Gulam Mohamed9e676002021-03-25 09:32:48 +00004064 [ISCSI_CONN_FAILED] = "failed",
4065 [ISCSI_CONN_BOUND] = "bound"
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04004066};
4067
4068static ssize_t show_conn_state(struct device *dev,
4069 struct device_attribute *attr, char *buf)
4070{
4071 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);
4072 const char *state = "unknown";
4073
4074 if (conn->state >= 0 &&
4075 conn->state < ARRAY_SIZE(connection_state_names))
4076 state = connection_state_names[conn->state];
4077
Chris Leechec98ea72021-02-23 18:00:17 -08004078 return sysfs_emit(buf, "%s\n", state);
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04004079}
4080static ISCSI_CLASS_ATTR(conn, state, S_IRUGO, show_conn_state,
4081 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Mike Christie289324b2011-02-16 15:04:37 -06004083#define iscsi_conn_ep_attr_show(param) \
4084static ssize_t show_conn_ep_param_##param(struct device *dev, \
4085 struct device_attribute *attr,\
4086 char *buf) \
4087{ \
4088 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
4089 struct iscsi_transport *t = conn->transport; \
4090 struct iscsi_endpoint *ep; \
4091 ssize_t rc; \
4092 \
4093 /* \
4094 * Need to make sure ep_disconnect does not free the LLD's \
4095 * interconnect resources while we are trying to read them. \
4096 */ \
4097 mutex_lock(&conn->ep_mutex); \
4098 ep = conn->ep; \
4099 if (!ep && t->ep_connect) { \
4100 mutex_unlock(&conn->ep_mutex); \
4101 return -ENOTCONN; \
4102 } \
4103 \
4104 if (ep) \
4105 rc = t->get_ep_param(ep, param, buf); \
4106 else \
4107 rc = t->get_conn_param(conn, param, buf); \
4108 mutex_unlock(&conn->ep_mutex); \
4109 return rc; \
4110}
4111
4112#define iscsi_conn_ep_attr(field, param) \
4113 iscsi_conn_ep_attr_show(param) \
4114static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
4115 show_conn_ep_param_##param, NULL);
4116
4117iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
4118iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
4119
Mike Christie3128c6c2011-07-25 13:48:42 -05004120static struct attribute *iscsi_conn_attrs[] = {
4121 &dev_attr_conn_max_recv_dlength.attr,
4122 &dev_attr_conn_max_xmit_dlength.attr,
4123 &dev_attr_conn_header_digest.attr,
4124 &dev_attr_conn_data_digest.attr,
4125 &dev_attr_conn_ifmarker.attr,
4126 &dev_attr_conn_ofmarker.attr,
4127 &dev_attr_conn_address.attr,
4128 &dev_attr_conn_port.attr,
4129 &dev_attr_conn_exp_statsn.attr,
4130 &dev_attr_conn_persistent_address.attr,
4131 &dev_attr_conn_persistent_port.attr,
4132 &dev_attr_conn_ping_tmo.attr,
4133 &dev_attr_conn_recv_tmo.attr,
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04004134 &dev_attr_conn_local_port.attr,
4135 &dev_attr_conn_statsn.attr,
4136 &dev_attr_conn_keepalive_tmo.attr,
4137 &dev_attr_conn_max_segment_size.attr,
4138 &dev_attr_conn_tcp_timestamp_stat.attr,
4139 &dev_attr_conn_tcp_wsf_disable.attr,
4140 &dev_attr_conn_tcp_nagle_disable.attr,
4141 &dev_attr_conn_tcp_timer_scale.attr,
4142 &dev_attr_conn_tcp_timestamp_enable.attr,
4143 &dev_attr_conn_fragment_disable.attr,
4144 &dev_attr_conn_ipv4_tos.attr,
4145 &dev_attr_conn_ipv6_traffic_class.attr,
4146 &dev_attr_conn_ipv6_flow_label.attr,
4147 &dev_attr_conn_is_fw_assigned_ipv6.attr,
4148 &dev_attr_conn_tcp_xmit_wsf.attr,
4149 &dev_attr_conn_tcp_recv_wsf.attr,
Adheer Chandravanshi5af62f22013-11-22 05:28:20 -05004150 &dev_attr_conn_local_ipaddr.attr,
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04004151 &dev_attr_conn_state.attr,
Mike Christie3128c6c2011-07-25 13:48:42 -05004152 NULL,
4153};
4154
Al Viro587a1f12011-07-23 23:11:19 -04004155static umode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
Mike Christie3128c6c2011-07-25 13:48:42 -05004156 struct attribute *attr, int i)
4157{
4158 struct device *cdev = container_of(kobj, struct device, kobj);
4159 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
4160 struct iscsi_transport *t = conn->transport;
4161 int param;
4162
4163 if (attr == &dev_attr_conn_max_recv_dlength.attr)
4164 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
4165 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
4166 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
4167 else if (attr == &dev_attr_conn_header_digest.attr)
4168 param = ISCSI_PARAM_HDRDGST_EN;
4169 else if (attr == &dev_attr_conn_data_digest.attr)
4170 param = ISCSI_PARAM_DATADGST_EN;
4171 else if (attr == &dev_attr_conn_ifmarker.attr)
4172 param = ISCSI_PARAM_IFMARKER_EN;
4173 else if (attr == &dev_attr_conn_ofmarker.attr)
4174 param = ISCSI_PARAM_OFMARKER_EN;
4175 else if (attr == &dev_attr_conn_address.attr)
4176 param = ISCSI_PARAM_CONN_ADDRESS;
4177 else if (attr == &dev_attr_conn_port.attr)
4178 param = ISCSI_PARAM_CONN_PORT;
4179 else if (attr == &dev_attr_conn_exp_statsn.attr)
4180 param = ISCSI_PARAM_EXP_STATSN;
4181 else if (attr == &dev_attr_conn_persistent_address.attr)
4182 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
4183 else if (attr == &dev_attr_conn_persistent_port.attr)
4184 param = ISCSI_PARAM_PERSISTENT_PORT;
4185 else if (attr == &dev_attr_conn_ping_tmo.attr)
4186 param = ISCSI_PARAM_PING_TMO;
4187 else if (attr == &dev_attr_conn_recv_tmo.attr)
4188 param = ISCSI_PARAM_RECV_TMO;
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04004189 else if (attr == &dev_attr_conn_local_port.attr)
4190 param = ISCSI_PARAM_LOCAL_PORT;
4191 else if (attr == &dev_attr_conn_statsn.attr)
4192 param = ISCSI_PARAM_STATSN;
4193 else if (attr == &dev_attr_conn_keepalive_tmo.attr)
4194 param = ISCSI_PARAM_KEEPALIVE_TMO;
4195 else if (attr == &dev_attr_conn_max_segment_size.attr)
4196 param = ISCSI_PARAM_MAX_SEGMENT_SIZE;
4197 else if (attr == &dev_attr_conn_tcp_timestamp_stat.attr)
4198 param = ISCSI_PARAM_TCP_TIMESTAMP_STAT;
4199 else if (attr == &dev_attr_conn_tcp_wsf_disable.attr)
4200 param = ISCSI_PARAM_TCP_WSF_DISABLE;
4201 else if (attr == &dev_attr_conn_tcp_nagle_disable.attr)
4202 param = ISCSI_PARAM_TCP_NAGLE_DISABLE;
4203 else if (attr == &dev_attr_conn_tcp_timer_scale.attr)
4204 param = ISCSI_PARAM_TCP_TIMER_SCALE;
4205 else if (attr == &dev_attr_conn_tcp_timestamp_enable.attr)
4206 param = ISCSI_PARAM_TCP_TIMESTAMP_EN;
4207 else if (attr == &dev_attr_conn_fragment_disable.attr)
4208 param = ISCSI_PARAM_IP_FRAGMENT_DISABLE;
4209 else if (attr == &dev_attr_conn_ipv4_tos.attr)
4210 param = ISCSI_PARAM_IPV4_TOS;
4211 else if (attr == &dev_attr_conn_ipv6_traffic_class.attr)
4212 param = ISCSI_PARAM_IPV6_TC;
4213 else if (attr == &dev_attr_conn_ipv6_flow_label.attr)
4214 param = ISCSI_PARAM_IPV6_FLOW_LABEL;
4215 else if (attr == &dev_attr_conn_is_fw_assigned_ipv6.attr)
4216 param = ISCSI_PARAM_IS_FW_ASSIGNED_IPV6;
4217 else if (attr == &dev_attr_conn_tcp_xmit_wsf.attr)
4218 param = ISCSI_PARAM_TCP_XMIT_WSF;
4219 else if (attr == &dev_attr_conn_tcp_recv_wsf.attr)
4220 param = ISCSI_PARAM_TCP_RECV_WSF;
Adheer Chandravanshi5af62f22013-11-22 05:28:20 -05004221 else if (attr == &dev_attr_conn_local_ipaddr.attr)
4222 param = ISCSI_PARAM_LOCAL_IPADDR;
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04004223 else if (attr == &dev_attr_conn_state.attr)
4224 return S_IRUGO;
Mike Christie3128c6c2011-07-25 13:48:42 -05004225 else {
4226 WARN_ONCE(1, "Invalid conn attr");
4227 return 0;
4228 }
4229
4230 return t->attr_is_visible(ISCSI_PARAM, param);
4231}
4232
4233static struct attribute_group iscsi_conn_group = {
4234 .attrs = iscsi_conn_attrs,
4235 .is_visible = iscsi_conn_attr_is_visible,
4236};
4237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238/*
Alex Aizman0896b752005-08-04 19:33:07 -07004239 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 */
Mike Christieb2c64162007-05-30 12:57:16 -05004241#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01004243show_session_param_##param(struct device *dev, \
4244 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05004245{ \
Tony Jonesee959b02008-02-22 00:13:36 +01004246 struct iscsi_cls_session *session = \
4247 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05004248 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05004249 \
4250 if (perm && !capable(CAP_SYS_ADMIN)) \
4251 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05004252 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05004253}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Mike Christieb2c64162007-05-30 12:57:16 -05004255#define iscsi_session_attr(field, param, perm) \
4256 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05004257static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05004258 NULL);
Mike Christieb2c64162007-05-30 12:57:16 -05004259iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
4260iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
4261iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
4262iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
4263iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
4264iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
4265iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
4266iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
4267iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
4268iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
4269iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
4270iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
4271iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
4272iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie30534952012-02-27 03:08:53 -08004273iscsi_session_attr(chap_out_idx, ISCSI_PARAM_CHAP_OUT_IDX, 1);
4274iscsi_session_attr(chap_in_idx, ISCSI_PARAM_CHAP_IN_IDX, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06004275iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
4276iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
4277iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie3fe5ae82009-11-11 16:34:33 -06004278iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05004279iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05304280iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
4281iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
Eddie Wai3b9373e2013-06-20 10:21:26 -07004282iscsi_session_attr(boot_root, ISCSI_PARAM_BOOT_ROOT, 0);
4283iscsi_session_attr(boot_nic, ISCSI_PARAM_BOOT_NIC, 0);
4284iscsi_session_attr(boot_target, ISCSI_PARAM_BOOT_TARGET, 0);
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04004285iscsi_session_attr(auto_snd_tgt_disable, ISCSI_PARAM_AUTO_SND_TGT_DISABLE, 0);
4286iscsi_session_attr(discovery_session, ISCSI_PARAM_DISCOVERY_SESS, 0);
4287iscsi_session_attr(portal_type, ISCSI_PARAM_PORTAL_TYPE, 0);
4288iscsi_session_attr(chap_auth, ISCSI_PARAM_CHAP_AUTH_EN, 0);
4289iscsi_session_attr(discovery_logout, ISCSI_PARAM_DISCOVERY_LOGOUT_EN, 0);
4290iscsi_session_attr(bidi_chap, ISCSI_PARAM_BIDI_CHAP_EN, 0);
4291iscsi_session_attr(discovery_auth_optional,
4292 ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL, 0);
4293iscsi_session_attr(def_time2wait, ISCSI_PARAM_DEF_TIME2WAIT, 0);
4294iscsi_session_attr(def_time2retain, ISCSI_PARAM_DEF_TIME2RETAIN, 0);
4295iscsi_session_attr(isid, ISCSI_PARAM_ISID, 0);
4296iscsi_session_attr(tsid, ISCSI_PARAM_TSID, 0);
4297iscsi_session_attr(def_taskmgmt_tmo, ISCSI_PARAM_DEF_TASKMGMT_TMO, 0);
4298iscsi_session_attr(discovery_parent_idx, ISCSI_PARAM_DISCOVERY_PARENT_IDX, 0);
4299iscsi_session_attr(discovery_parent_type, ISCSI_PARAM_DISCOVERY_PARENT_TYPE, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05004300
Mike Christie6eabafb2008-01-31 13:36:43 -06004301static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01004302show_priv_session_state(struct device *dev, struct device_attribute *attr,
4303 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06004304{
Tony Jonesee959b02008-02-22 00:13:36 +01004305 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Chris Leechec98ea72021-02-23 18:00:17 -08004306 return sysfs_emit(buf, "%s\n", iscsi_session_state_name(session->state));
Mike Christie6eabafb2008-01-31 13:36:43 -06004307}
4308static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
4309 NULL);
Mike Christie0c70d842011-12-05 16:44:01 -06004310static ssize_t
4311show_priv_session_creator(struct device *dev, struct device_attribute *attr,
4312 char *buf)
4313{
4314 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Chris Leechec98ea72021-02-23 18:00:17 -08004315 return sysfs_emit(buf, "%d\n", session->creator);
Mike Christie0c70d842011-12-05 16:44:01 -06004316}
4317static ISCSI_CLASS_ATTR(priv_sess, creator, S_IRUGO, show_priv_session_creator,
4318 NULL);
Manish Rangankar930a9f02012-11-23 06:58:41 -05004319static ssize_t
4320show_priv_session_target_id(struct device *dev, struct device_attribute *attr,
4321 char *buf)
4322{
4323 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Chris Leechec98ea72021-02-23 18:00:17 -08004324 return sysfs_emit(buf, "%d\n", session->target_id);
Manish Rangankar930a9f02012-11-23 06:58:41 -05004325}
4326static ISCSI_CLASS_ATTR(priv_sess, target_id, S_IRUGO,
4327 show_priv_session_target_id, NULL);
Mike Christie6eabafb2008-01-31 13:36:43 -06004328
Mike Christiefd7255f2006-04-06 21:13:36 -05004329#define iscsi_priv_session_attr_show(field, format) \
4330static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01004331show_priv_session_##field(struct device *dev, \
4332 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05004333{ \
Tony Jonesee959b02008-02-22 00:13:36 +01004334 struct iscsi_cls_session *session = \
4335 iscsi_dev_to_session(dev->parent); \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304336 if (session->field == -1) \
Chris Leechec98ea72021-02-23 18:00:17 -08004337 return sysfs_emit(buf, "off\n"); \
4338 return sysfs_emit(buf, format"\n", session->field); \
Mike Christiefd7255f2006-04-06 21:13:36 -05004339}
4340
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304341#define iscsi_priv_session_attr_store(field) \
4342static ssize_t \
4343store_priv_session_##field(struct device *dev, \
4344 struct device_attribute *attr, \
4345 const char *buf, size_t count) \
4346{ \
4347 int val; \
4348 char *cp; \
4349 struct iscsi_cls_session *session = \
4350 iscsi_dev_to_session(dev->parent); \
4351 if ((session->state == ISCSI_SESSION_FREE) || \
4352 (session->state == ISCSI_SESSION_FAILED)) \
4353 return -EBUSY; \
Chris Leech9c8108a2015-06-16 16:07:13 -07004354 if (strncmp(buf, "off", 3) == 0) { \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304355 session->field = -1; \
Chris Leech9c8108a2015-06-16 16:07:13 -07004356 session->field##_sysfs_override = true; \
4357 } else { \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304358 val = simple_strtoul(buf, &cp, 0); \
4359 if (*cp != '\0' && *cp != '\n') \
4360 return -EINVAL; \
4361 session->field = val; \
Chris Leech9c8108a2015-06-16 16:07:13 -07004362 session->field##_sysfs_override = true; \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304363 } \
4364 return count; \
4365}
4366
4367#define iscsi_priv_session_rw_attr(field, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05004368 iscsi_priv_session_attr_show(field, format) \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304369 iscsi_priv_session_attr_store(field) \
Vasiliy Kulikov523f3c82011-02-04 15:24:14 +03004370static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304371 show_priv_session_##field, \
4372 store_priv_session_##field)
Chris Leech9c8108a2015-06-16 16:07:13 -07004373
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304374iscsi_priv_session_rw_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05004375
Mike Christie1d063c12011-07-25 13:48:43 -05004376static struct attribute *iscsi_session_attrs[] = {
4377 &dev_attr_sess_initial_r2t.attr,
4378 &dev_attr_sess_max_outstanding_r2t.attr,
4379 &dev_attr_sess_immediate_data.attr,
4380 &dev_attr_sess_first_burst_len.attr,
4381 &dev_attr_sess_max_burst_len.attr,
4382 &dev_attr_sess_data_pdu_in_order.attr,
4383 &dev_attr_sess_data_seq_in_order.attr,
4384 &dev_attr_sess_erl.attr,
4385 &dev_attr_sess_targetname.attr,
4386 &dev_attr_sess_tpgt.attr,
4387 &dev_attr_sess_password.attr,
4388 &dev_attr_sess_password_in.attr,
4389 &dev_attr_sess_username.attr,
4390 &dev_attr_sess_username_in.attr,
4391 &dev_attr_sess_fast_abort.attr,
4392 &dev_attr_sess_abort_tmo.attr,
4393 &dev_attr_sess_lu_reset_tmo.attr,
4394 &dev_attr_sess_tgt_reset_tmo.attr,
4395 &dev_attr_sess_ifacename.attr,
4396 &dev_attr_sess_initiatorname.attr,
4397 &dev_attr_sess_targetalias.attr,
Eddie Wai3b9373e2013-06-20 10:21:26 -07004398 &dev_attr_sess_boot_root.attr,
4399 &dev_attr_sess_boot_nic.attr,
4400 &dev_attr_sess_boot_target.attr,
Mike Christie1d063c12011-07-25 13:48:43 -05004401 &dev_attr_priv_sess_recovery_tmo.attr,
4402 &dev_attr_priv_sess_state.attr,
Mike Christie0c70d842011-12-05 16:44:01 -06004403 &dev_attr_priv_sess_creator.attr,
Mike Christie30534952012-02-27 03:08:53 -08004404 &dev_attr_sess_chap_out_idx.attr,
4405 &dev_attr_sess_chap_in_idx.attr,
Manish Rangankar930a9f02012-11-23 06:58:41 -05004406 &dev_attr_priv_sess_target_id.attr,
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04004407 &dev_attr_sess_auto_snd_tgt_disable.attr,
4408 &dev_attr_sess_discovery_session.attr,
4409 &dev_attr_sess_portal_type.attr,
4410 &dev_attr_sess_chap_auth.attr,
4411 &dev_attr_sess_discovery_logout.attr,
4412 &dev_attr_sess_bidi_chap.attr,
4413 &dev_attr_sess_discovery_auth_optional.attr,
4414 &dev_attr_sess_def_time2wait.attr,
4415 &dev_attr_sess_def_time2retain.attr,
4416 &dev_attr_sess_isid.attr,
4417 &dev_attr_sess_tsid.attr,
4418 &dev_attr_sess_def_taskmgmt_tmo.attr,
4419 &dev_attr_sess_discovery_parent_idx.attr,
4420 &dev_attr_sess_discovery_parent_type.attr,
Mike Christie1d063c12011-07-25 13:48:43 -05004421 NULL,
4422};
4423
Al Viro587a1f12011-07-23 23:11:19 -04004424static umode_t iscsi_session_attr_is_visible(struct kobject *kobj,
Mike Christie1d063c12011-07-25 13:48:43 -05004425 struct attribute *attr, int i)
4426{
4427 struct device *cdev = container_of(kobj, struct device, kobj);
4428 struct iscsi_cls_session *session = transport_class_to_session(cdev);
4429 struct iscsi_transport *t = session->transport;
4430 int param;
4431
4432 if (attr == &dev_attr_sess_initial_r2t.attr)
4433 param = ISCSI_PARAM_INITIAL_R2T_EN;
4434 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
4435 param = ISCSI_PARAM_MAX_R2T;
4436 else if (attr == &dev_attr_sess_immediate_data.attr)
4437 param = ISCSI_PARAM_IMM_DATA_EN;
4438 else if (attr == &dev_attr_sess_first_burst_len.attr)
4439 param = ISCSI_PARAM_FIRST_BURST;
4440 else if (attr == &dev_attr_sess_max_burst_len.attr)
4441 param = ISCSI_PARAM_MAX_BURST;
4442 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
4443 param = ISCSI_PARAM_PDU_INORDER_EN;
4444 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
4445 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
4446 else if (attr == &dev_attr_sess_erl.attr)
4447 param = ISCSI_PARAM_ERL;
4448 else if (attr == &dev_attr_sess_targetname.attr)
4449 param = ISCSI_PARAM_TARGET_NAME;
4450 else if (attr == &dev_attr_sess_tpgt.attr)
4451 param = ISCSI_PARAM_TPGT;
Mike Christie30534952012-02-27 03:08:53 -08004452 else if (attr == &dev_attr_sess_chap_in_idx.attr)
4453 param = ISCSI_PARAM_CHAP_IN_IDX;
4454 else if (attr == &dev_attr_sess_chap_out_idx.attr)
4455 param = ISCSI_PARAM_CHAP_OUT_IDX;
Mike Christie1d063c12011-07-25 13:48:43 -05004456 else if (attr == &dev_attr_sess_password.attr)
4457 param = ISCSI_PARAM_USERNAME;
4458 else if (attr == &dev_attr_sess_password_in.attr)
4459 param = ISCSI_PARAM_USERNAME_IN;
4460 else if (attr == &dev_attr_sess_username.attr)
4461 param = ISCSI_PARAM_PASSWORD;
4462 else if (attr == &dev_attr_sess_username_in.attr)
4463 param = ISCSI_PARAM_PASSWORD_IN;
4464 else if (attr == &dev_attr_sess_fast_abort.attr)
4465 param = ISCSI_PARAM_FAST_ABORT;
4466 else if (attr == &dev_attr_sess_abort_tmo.attr)
4467 param = ISCSI_PARAM_ABORT_TMO;
4468 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
4469 param = ISCSI_PARAM_LU_RESET_TMO;
4470 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
4471 param = ISCSI_PARAM_TGT_RESET_TMO;
4472 else if (attr == &dev_attr_sess_ifacename.attr)
4473 param = ISCSI_PARAM_IFACE_NAME;
4474 else if (attr == &dev_attr_sess_initiatorname.attr)
4475 param = ISCSI_PARAM_INITIATOR_NAME;
4476 else if (attr == &dev_attr_sess_targetalias.attr)
4477 param = ISCSI_PARAM_TARGET_ALIAS;
Eddie Wai3b9373e2013-06-20 10:21:26 -07004478 else if (attr == &dev_attr_sess_boot_root.attr)
4479 param = ISCSI_PARAM_BOOT_ROOT;
4480 else if (attr == &dev_attr_sess_boot_nic.attr)
4481 param = ISCSI_PARAM_BOOT_NIC;
4482 else if (attr == &dev_attr_sess_boot_target.attr)
4483 param = ISCSI_PARAM_BOOT_TARGET;
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04004484 else if (attr == &dev_attr_sess_auto_snd_tgt_disable.attr)
4485 param = ISCSI_PARAM_AUTO_SND_TGT_DISABLE;
4486 else if (attr == &dev_attr_sess_discovery_session.attr)
4487 param = ISCSI_PARAM_DISCOVERY_SESS;
4488 else if (attr == &dev_attr_sess_portal_type.attr)
4489 param = ISCSI_PARAM_PORTAL_TYPE;
4490 else if (attr == &dev_attr_sess_chap_auth.attr)
4491 param = ISCSI_PARAM_CHAP_AUTH_EN;
4492 else if (attr == &dev_attr_sess_discovery_logout.attr)
4493 param = ISCSI_PARAM_DISCOVERY_LOGOUT_EN;
4494 else if (attr == &dev_attr_sess_bidi_chap.attr)
4495 param = ISCSI_PARAM_BIDI_CHAP_EN;
4496 else if (attr == &dev_attr_sess_discovery_auth_optional.attr)
4497 param = ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL;
4498 else if (attr == &dev_attr_sess_def_time2wait.attr)
4499 param = ISCSI_PARAM_DEF_TIME2WAIT;
4500 else if (attr == &dev_attr_sess_def_time2retain.attr)
4501 param = ISCSI_PARAM_DEF_TIME2RETAIN;
4502 else if (attr == &dev_attr_sess_isid.attr)
4503 param = ISCSI_PARAM_ISID;
4504 else if (attr == &dev_attr_sess_tsid.attr)
4505 param = ISCSI_PARAM_TSID;
4506 else if (attr == &dev_attr_sess_def_taskmgmt_tmo.attr)
4507 param = ISCSI_PARAM_DEF_TASKMGMT_TMO;
4508 else if (attr == &dev_attr_sess_discovery_parent_idx.attr)
4509 param = ISCSI_PARAM_DISCOVERY_PARENT_IDX;
4510 else if (attr == &dev_attr_sess_discovery_parent_type.attr)
4511 param = ISCSI_PARAM_DISCOVERY_PARENT_TYPE;
Mike Christie1d063c12011-07-25 13:48:43 -05004512 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
4513 return S_IRUGO | S_IWUSR;
4514 else if (attr == &dev_attr_priv_sess_state.attr)
4515 return S_IRUGO;
Mike Christie0c70d842011-12-05 16:44:01 -06004516 else if (attr == &dev_attr_priv_sess_creator.attr)
4517 return S_IRUGO;
Manish Rangankar930a9f02012-11-23 06:58:41 -05004518 else if (attr == &dev_attr_priv_sess_target_id.attr)
4519 return S_IRUGO;
Mike Christie1d063c12011-07-25 13:48:43 -05004520 else {
4521 WARN_ONCE(1, "Invalid session attr");
4522 return 0;
4523 }
4524
4525 return t->attr_is_visible(ISCSI_PARAM, param);
4526}
4527
4528static struct attribute_group iscsi_session_group = {
4529 .attrs = iscsi_session_attrs,
4530 .is_visible = iscsi_session_attr_is_visible,
4531};
4532
Mike Christie1819dc82007-05-30 12:57:08 -05004533/*
4534 * iSCSI host attrs
4535 */
4536#define iscsi_host_attr_show(param) \
4537static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01004538show_host_param_##param(struct device *dev, \
4539 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05004540{ \
Tony Jonesee959b02008-02-22 00:13:36 +01004541 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05004542 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
4543 return priv->iscsi_transport->get_host_param(shost, param, buf); \
4544}
4545
4546#define iscsi_host_attr(field, param) \
4547 iscsi_host_attr_show(param) \
4548static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
4549 NULL);
4550
Mike Christied8196ed2007-05-30 12:57:25 -05004551iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05004552iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05004553iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05004554iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08004555iscsi_host_attr(port_state, ISCSI_HOST_PARAM_PORT_STATE);
4556iscsi_host_attr(port_speed, ISCSI_HOST_PARAM_PORT_SPEED);
Mike Christie1819dc82007-05-30 12:57:08 -05004557
Mike Christief27fb2e2011-07-25 13:48:45 -05004558static struct attribute *iscsi_host_attrs[] = {
4559 &dev_attr_host_netdev.attr,
4560 &dev_attr_host_hwaddress.attr,
4561 &dev_attr_host_ipaddress.attr,
4562 &dev_attr_host_initiatorname.attr,
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08004563 &dev_attr_host_port_state.attr,
4564 &dev_attr_host_port_speed.attr,
Mike Christief27fb2e2011-07-25 13:48:45 -05004565 NULL,
4566};
4567
Al Viro587a1f12011-07-23 23:11:19 -04004568static umode_t iscsi_host_attr_is_visible(struct kobject *kobj,
Mike Christief27fb2e2011-07-25 13:48:45 -05004569 struct attribute *attr, int i)
4570{
4571 struct device *cdev = container_of(kobj, struct device, kobj);
4572 struct Scsi_Host *shost = transport_class_to_shost(cdev);
4573 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
4574 int param;
4575
4576 if (attr == &dev_attr_host_netdev.attr)
4577 param = ISCSI_HOST_PARAM_NETDEV_NAME;
4578 else if (attr == &dev_attr_host_hwaddress.attr)
4579 param = ISCSI_HOST_PARAM_HWADDRESS;
4580 else if (attr == &dev_attr_host_ipaddress.attr)
4581 param = ISCSI_HOST_PARAM_IPADDRESS;
4582 else if (attr == &dev_attr_host_initiatorname.attr)
4583 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08004584 else if (attr == &dev_attr_host_port_state.attr)
4585 param = ISCSI_HOST_PARAM_PORT_STATE;
4586 else if (attr == &dev_attr_host_port_speed.attr)
4587 param = ISCSI_HOST_PARAM_PORT_SPEED;
Mike Christief27fb2e2011-07-25 13:48:45 -05004588 else {
4589 WARN_ONCE(1, "Invalid host attr");
4590 return 0;
4591 }
4592
4593 return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
4594}
4595
4596static struct attribute_group iscsi_host_group = {
4597 .attrs = iscsi_host_attrs,
4598 .is_visible = iscsi_host_attr_is_visible,
4599};
Mike Christie1819dc82007-05-30 12:57:08 -05004600
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08004601/* convert iscsi_port_speed values to ascii string name */
4602static const struct {
4603 enum iscsi_port_speed value;
4604 char *name;
4605} iscsi_port_speed_names[] = {
4606 {ISCSI_PORT_SPEED_UNKNOWN, "Unknown" },
4607 {ISCSI_PORT_SPEED_10MBPS, "10 Mbps" },
4608 {ISCSI_PORT_SPEED_100MBPS, "100 Mbps" },
4609 {ISCSI_PORT_SPEED_1GBPS, "1 Gbps" },
4610 {ISCSI_PORT_SPEED_10GBPS, "10 Gbps" },
Jitendra Bhivaref9e4fa42016-01-20 14:10:57 +05304611 {ISCSI_PORT_SPEED_25GBPS, "25 Gbps" },
4612 {ISCSI_PORT_SPEED_40GBPS, "40 Gbps" },
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08004613};
4614
4615char *iscsi_get_port_speed_name(struct Scsi_Host *shost)
4616{
4617 int i;
4618 char *speed = "Unknown!";
4619 struct iscsi_cls_host *ihost = shost->shost_data;
4620 uint32_t port_speed = ihost->port_speed;
4621
4622 for (i = 0; i < ARRAY_SIZE(iscsi_port_speed_names); i++) {
4623 if (iscsi_port_speed_names[i].value & port_speed) {
4624 speed = iscsi_port_speed_names[i].name;
4625 break;
4626 }
4627 }
4628 return speed;
4629}
4630EXPORT_SYMBOL_GPL(iscsi_get_port_speed_name);
4631
4632/* convert iscsi_port_state values to ascii string name */
4633static const struct {
4634 enum iscsi_port_state value;
4635 char *name;
4636} iscsi_port_state_names[] = {
4637 {ISCSI_PORT_STATE_DOWN, "LINK DOWN" },
4638 {ISCSI_PORT_STATE_UP, "LINK UP" },
4639};
4640
4641char *iscsi_get_port_state_name(struct Scsi_Host *shost)
4642{
4643 int i;
4644 char *state = "Unknown!";
4645 struct iscsi_cls_host *ihost = shost->shost_data;
4646 uint32_t port_state = ihost->port_state;
4647
4648 for (i = 0; i < ARRAY_SIZE(iscsi_port_state_names); i++) {
4649 if (iscsi_port_state_names[i].value & port_state) {
4650 state = iscsi_port_state_names[i].name;
4651 break;
4652 }
4653 }
4654 return state;
4655}
4656EXPORT_SYMBOL_GPL(iscsi_get_port_state_name);
4657
Alex Aizman0896b752005-08-04 19:33:07 -07004658static int iscsi_session_match(struct attribute_container *cont,
4659 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
Mike Christie7b8631b2006-01-13 18:05:50 -06004661 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07004663 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
Alex Aizman0896b752005-08-04 19:33:07 -07004665 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 return 0;
4667
Mike Christie7b8631b2006-01-13 18:05:50 -06004668 session = iscsi_dev_to_session(dev);
4669 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07004670 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 return 0;
4672
Alex Aizman0896b752005-08-04 19:33:07 -07004673 priv = to_iscsi_internal(shost->transportt);
4674 if (priv->session_cont.ac.class != &iscsi_session_class.class)
4675 return 0;
4676
4677 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678}
4679
Alex Aizman0896b752005-08-04 19:33:07 -07004680static int iscsi_conn_match(struct attribute_container *cont,
4681 struct device *dev)
4682{
Mike Christie7b8631b2006-01-13 18:05:50 -06004683 struct iscsi_cls_session *session;
4684 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07004685 struct Scsi_Host *shost;
4686 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Alex Aizman0896b752005-08-04 19:33:07 -07004688 if (!iscsi_is_conn_dev(dev))
4689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Mike Christie7b8631b2006-01-13 18:05:50 -06004691 conn = iscsi_dev_to_conn(dev);
4692 session = iscsi_dev_to_session(conn->dev.parent);
4693 shost = iscsi_session_to_shost(session);
4694
Alex Aizman0896b752005-08-04 19:33:07 -07004695 if (!shost->transportt)
4696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
Alex Aizman0896b752005-08-04 19:33:07 -07004698 priv = to_iscsi_internal(shost->transportt);
4699 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
4700 return 0;
4701
4702 return &priv->conn_cont.ac == cont;
4703}
4704
Mike Christie30a6c652006-04-06 21:13:39 -05004705static int iscsi_host_match(struct attribute_container *cont,
4706 struct device *dev)
4707{
4708 struct Scsi_Host *shost;
4709 struct iscsi_internal *priv;
4710
4711 if (!scsi_is_host_device(dev))
4712 return 0;
4713
4714 shost = dev_to_shost(dev);
4715 if (!shost->transportt ||
4716 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
4717 return 0;
4718
4719 priv = to_iscsi_internal(shost->transportt);
4720 return &priv->t.host_attrs.ac == cont;
4721}
4722
Mike Christie7b8631b2006-01-13 18:05:50 -06004723struct scsi_transport_template *
4724iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07004725{
4726 struct iscsi_internal *priv;
4727 unsigned long flags;
Mike Christief27fb2e2011-07-25 13:48:45 -05004728 int err;
Alex Aizman0896b752005-08-04 19:33:07 -07004729
4730 BUG_ON(!tt);
Mike Christie891e2632021-05-25 13:17:55 -05004731 WARN_ON(tt->ep_disconnect && !tt->unbind_conn);
Alex Aizman0896b752005-08-04 19:33:07 -07004732
4733 priv = iscsi_if_transport_lookup(tt);
4734 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06004735 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07004736
Jes Sorensen24669f752006-01-16 10:31:18 -05004737 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07004738 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06004739 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07004740 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07004741 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05004742 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06004743 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07004744
Tony Jonesee959b02008-02-22 00:13:36 +01004745 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01004746 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01004747 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07004748 if (err)
4749 goto free_priv;
4750
Tony Jonesee959b02008-02-22 00:13:36 +01004751 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07004752 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01004753 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07004754
Mike Christie30a6c652006-04-06 21:13:39 -05004755 /* host parameters */
Mike Christie30a6c652006-04-06 21:13:39 -05004756 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
4757 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christief27fb2e2011-07-25 13:48:45 -05004758 priv->t.host_attrs.ac.grp = &iscsi_host_group;
Mike Christie32c6e1b2008-05-21 15:53:58 -05004759 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05004760 transport_container_register(&priv->t.host_attrs);
4761
Alex Aizman0896b752005-08-04 19:33:07 -07004762 /* connection parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07004763 priv->conn_cont.ac.class = &iscsi_connection_class.class;
4764 priv->conn_cont.ac.match = iscsi_conn_match;
Mike Christie3128c6c2011-07-25 13:48:42 -05004765 priv->conn_cont.ac.grp = &iscsi_conn_group;
Alex Aizman0896b752005-08-04 19:33:07 -07004766 transport_container_register(&priv->conn_cont);
4767
Alex Aizman0896b752005-08-04 19:33:07 -07004768 /* session parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07004769 priv->session_cont.ac.class = &iscsi_session_class.class;
4770 priv->session_cont.ac.match = iscsi_session_match;
Mike Christie1d063c12011-07-25 13:48:43 -05004771 priv->session_cont.ac.grp = &iscsi_session_group;
Alex Aizman0896b752005-08-04 19:33:07 -07004772 transport_container_register(&priv->session_cont);
4773
Alex Aizman0896b752005-08-04 19:33:07 -07004774 spin_lock_irqsave(&iscsi_transport_lock, flags);
4775 list_add(&priv->list, &iscsi_transports);
4776 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Alex Aizman0896b752005-08-04 19:33:07 -07004778 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06004779 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
Tony Jonesee959b02008-02-22 00:13:36 +01004781unregister_dev:
4782 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05004783 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07004784free_priv:
4785 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06004786 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787}
Alex Aizman0896b752005-08-04 19:33:07 -07004788EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
Alex Aizman0896b752005-08-04 19:33:07 -07004790int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791{
Alex Aizman0896b752005-08-04 19:33:07 -07004792 struct iscsi_internal *priv;
4793 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Alex Aizman0896b752005-08-04 19:33:07 -07004795 BUG_ON(!tt);
4796
Arjan van de Ven0b950672006-01-11 13:16:10 +01004797 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07004798
4799 priv = iscsi_if_transport_lookup(tt);
4800 BUG_ON (!priv);
4801
Alex Aizman0896b752005-08-04 19:33:07 -07004802 spin_lock_irqsave(&iscsi_transport_lock, flags);
4803 list_del(&priv->list);
4804 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
4805
4806 transport_container_unregister(&priv->conn_cont);
4807 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05004808 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07004809
Tony Jonesee959b02008-02-22 00:13:36 +01004810 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
4811 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01004812 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07004813
4814 return 0;
4815}
4816EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
4817
Fred Herardc2332b02018-11-21 12:04:43 -05004818void iscsi_dbg_trace(void (*trace)(struct device *dev, struct va_format *),
4819 struct device *dev, const char *fmt, ...)
4820{
4821 struct va_format vaf;
4822 va_list args;
4823
4824 va_start(args, fmt);
4825 vaf.fmt = fmt;
4826 vaf.va = &args;
4827 trace(dev, &vaf);
4828 va_end(args);
4829}
4830EXPORT_SYMBOL_GPL(iscsi_dbg_trace);
4831
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832static __init int iscsi_transport_init(void)
4833{
Alex Aizman0896b752005-08-04 19:33:07 -07004834 int err;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004835 struct netlink_kernel_cfg cfg = {
4836 .groups = 1,
4837 .input = iscsi_if_rx,
4838 };
Meelis Roos09492602006-12-17 12:10:26 -06004839 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05004840 ISCSI_TRANSPORT_VERSION);
4841
Mike Christie41be1442007-02-28 17:32:18 -06004842 atomic_set(&iscsi_session_nr, 0);
4843
Alex Aizman0896b752005-08-04 19:33:07 -07004844 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 if (err)
4846 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07004847
Mike Christied82ff9be2008-05-21 15:54:13 -05004848 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07004849 if (err)
4850 goto unregister_transport_class;
4851
Mike Christie8d079132011-07-25 13:48:40 -05004852 err = class_register(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05004853 if (err)
4854 goto unregister_endpoint_class;
4855
Mike Christie8d079132011-07-25 13:48:40 -05004856 err = transport_class_register(&iscsi_host_class);
4857 if (err)
4858 goto unregister_iface_class;
4859
Mike Christie30a6c652006-04-06 21:13:39 -05004860 err = transport_class_register(&iscsi_connection_class);
4861 if (err)
4862 goto unregister_host_class;
4863
Alex Aizman0896b752005-08-04 19:33:07 -07004864 err = transport_class_register(&iscsi_session_class);
4865 if (err)
4866 goto unregister_conn_class;
4867
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04004868 err = bus_register(&iscsi_flashnode_bus);
4869 if (err)
4870 goto unregister_session_class;
4871
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004872 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, &cfg);
Alex Aizman0896b752005-08-04 19:33:07 -07004873 if (!nls) {
4874 err = -ENOBUFS;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04004875 goto unregister_flashnode_bus;
Alex Aizman0896b752005-08-04 19:33:07 -07004876 }
4877
Bob Liu3ce41962020-05-05 09:19:08 +08004878 iscsi_eh_timer_workq = alloc_workqueue("%s",
4879 WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND,
Bob Liu1a982622020-07-01 11:07:44 +08004880 1, "iscsi_eh");
Wei Yongjunbcab2cc2013-04-19 09:23:28 +08004881 if (!iscsi_eh_timer_workq) {
4882 err = -ENOMEM;
Mike Christied8bf5412007-12-13 12:43:27 -06004883 goto release_nls;
Wei Yongjunbcab2cc2013-04-19 09:23:28 +08004884 }
Mike Christied8bf5412007-12-13 12:43:27 -06004885
Mike Christie23d6fef2021-05-25 13:18:00 -05004886 iscsi_conn_cleanup_workq = alloc_workqueue("%s",
4887 WQ_SYSFS | WQ_MEM_RECLAIM | WQ_UNBOUND, 0,
4888 "iscsi_conn_cleanup");
4889 if (!iscsi_conn_cleanup_workq) {
4890 err = -ENOMEM;
4891 goto destroy_wq;
4892 }
4893
Mike Christie43a145a2006-10-16 18:09:38 -04004894 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07004895
Mike Christie23d6fef2021-05-25 13:18:00 -05004896destroy_wq:
4897 destroy_workqueue(iscsi_eh_timer_workq);
Mike Christied8bf5412007-12-13 12:43:27 -06004898release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08004899 netlink_kernel_release(nls);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04004900unregister_flashnode_bus:
4901 bus_unregister(&iscsi_flashnode_bus);
Alex Aizman0896b752005-08-04 19:33:07 -07004902unregister_session_class:
4903 transport_class_unregister(&iscsi_session_class);
4904unregister_conn_class:
4905 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05004906unregister_host_class:
4907 transport_class_unregister(&iscsi_host_class);
Mike Christie8d079132011-07-25 13:48:40 -05004908unregister_iface_class:
4909 class_unregister(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05004910unregister_endpoint_class:
4911 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07004912unregister_transport_class:
4913 class_unregister(&iscsi_transport_class);
4914 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915}
4916
4917static void __exit iscsi_transport_exit(void)
4918{
Mike Christie23d6fef2021-05-25 13:18:00 -05004919 destroy_workqueue(iscsi_conn_cleanup_workq);
Mike Christied8bf5412007-12-13 12:43:27 -06004920 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08004921 netlink_kernel_release(nls);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04004922 bus_unregister(&iscsi_flashnode_bus);
Alex Aizman0896b752005-08-04 19:33:07 -07004923 transport_class_unregister(&iscsi_connection_class);
4924 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05004925 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05004926 class_unregister(&iscsi_endpoint_class);
Mike Christie8d079132011-07-25 13:48:40 -05004927 class_unregister(&iscsi_iface_class);
Alex Aizman0896b752005-08-04 19:33:07 -07004928 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929}
4930
4931module_init(iscsi_transport_init);
4932module_exit(iscsi_transport_exit);
4933
Alex Aizman0896b752005-08-04 19:33:07 -07004934MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
4935 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
4936 "Alex Aizman <itn780@yahoo.com>");
4937MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05004939MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
Stephen Hemminger058548a2010-12-09 09:37:56 -08004940MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);