blob: a5759d0e388a851397ea2eb9fc9e8d72ea02d2f0 [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
Bharath Ravi0ab71042020-01-25 01:19:25 -050089/* Worker to perform connection failure on unresponsive connections
90 * completely in kernel space.
91 */
92static void stop_conn_work_fn(struct work_struct *work);
93static DECLARE_WORK(stop_conn_work, stop_conn_work_fn);
94
Mike Christie41be1442007-02-28 17:32:18 -060095static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060096static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050097
Frank Mayharcc6b32e2020-02-27 14:59:45 -050098static struct workqueue_struct *iscsi_destroy_workq;
99
Mike Christie8d4a6902011-10-06 03:56:57 -0500100static DEFINE_IDA(iscsi_sess_ida);
Alex Aizman0896b752005-08-04 19:33:07 -0700101/*
102 * list of registered transports and lock that must
103 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +0100104 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -0700105 */
106static LIST_HEAD(iscsi_transports);
107static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Alex Aizman0896b752005-08-04 19:33:07 -0700109#define to_iscsi_internal(tmpl) \
110 container_of(tmpl, struct iscsi_internal, t)
111
Tony Jonesee959b02008-02-22 00:13:36 +0100112#define dev_to_iscsi_internal(_dev) \
113 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700114
Tony Jonesee959b02008-02-22 00:13:36 +0100115static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -0700116{
Tony Jonesee959b02008-02-22 00:13:36 +0100117 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -0700118 kfree(priv);
119}
120
121/*
122 * iscsi_transport_class represents the iscsi_transports that are
123 * registered.
124 */
125static struct class iscsi_transport_class = {
126 .name = "iscsi_transport",
Tony Jonesee959b02008-02-22 00:13:36 +0100127 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -0700128};
129
130static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100131show_transport_handle(struct device *dev, struct device_attribute *attr,
132 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -0700133{
Tony Jonesee959b02008-02-22 00:13:36 +0100134 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Lee Duncanc71edc52021-02-23 13:06:24 -0800135
136 if (!capable(CAP_SYS_ADMIN))
137 return -EACCES;
Chris Leech76d92bf2021-02-23 18:00:17 -0800138 return sysfs_emit(buf, "%llu\n",
139 (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -0700140}
Tony Jonesee959b02008-02-22 00:13:36 +0100141static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700142
143#define show_transport_attr(name, format) \
144static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100145show_transport_##name(struct device *dev, \
146 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -0700147{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100148 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Chris Leech76d92bf2021-02-23 18:00:17 -0800149 return sysfs_emit(buf, format"\n", priv->iscsi_transport->name);\
Alex Aizman0896b752005-08-04 19:33:07 -0700150} \
Tony Jonesee959b02008-02-22 00:13:36 +0100151static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700152
153show_transport_attr(caps, "0x%x");
Alex Aizman0896b752005-08-04 19:33:07 -0700154
155static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100156 &dev_attr_handle.attr,
157 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700158 NULL,
159};
160
161static struct attribute_group iscsi_transport_group = {
162 .attrs = iscsi_transport_attrs,
163};
164
Mike Christied82ff9be2008-05-21 15:54:13 -0500165/*
166 * iSCSI endpoint attrs
167 */
168#define iscsi_dev_to_endpoint(_dev) \
169 container_of(_dev, struct iscsi_endpoint, dev)
170
171#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
172struct device_attribute dev_attr_##_prefix##_##_name = \
173 __ATTR(_name,_mode,_show,_store)
174
175static void iscsi_endpoint_release(struct device *dev)
176{
177 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
178 kfree(ep);
179}
180
181static struct class iscsi_endpoint_class = {
182 .name = "iscsi_endpoint",
183 .dev_release = iscsi_endpoint_release,
184};
185
186static ssize_t
187show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
188{
189 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Chris Leech76d92bf2021-02-23 18:00:17 -0800190 return sysfs_emit(buf, "%llu\n", (unsigned long long) ep->id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500191}
192static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
193
194static struct attribute *iscsi_endpoint_attrs[] = {
195 &dev_attr_ep_handle.attr,
196 NULL,
197};
198
199static struct attribute_group iscsi_endpoint_group = {
200 .attrs = iscsi_endpoint_attrs,
201};
202
203#define ISCSI_MAX_EPID -1
204
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100205static int iscsi_match_epid(struct device *dev, const void *data)
Mike Christied82ff9be2008-05-21 15:54:13 -0500206{
207 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100208 const uint64_t *epid = data;
Mike Christied82ff9be2008-05-21 15:54:13 -0500209
210 return *epid == ep->id;
211}
212
213struct iscsi_endpoint *
214iscsi_create_endpoint(int dd_size)
215{
216 struct device *dev;
217 struct iscsi_endpoint *ep;
Mike Christie21536062008-09-24 11:46:11 -0500218 uint64_t id;
Mike Christied82ff9be2008-05-21 15:54:13 -0500219 int err;
220
221 for (id = 1; id < ISCSI_MAX_EPID; id++) {
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400222 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
Mike Christied82ff9be2008-05-21 15:54:13 -0500223 iscsi_match_epid);
224 if (!dev)
225 break;
Sagi Grimberg4ce30872015-05-20 12:38:58 +0300226 else
227 put_device(dev);
Mike Christied82ff9be2008-05-21 15:54:13 -0500228 }
229 if (id == ISCSI_MAX_EPID) {
230 printk(KERN_ERR "Too many connections. Max supported %u\n",
231 ISCSI_MAX_EPID - 1);
232 return NULL;
233 }
234
235 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
236 if (!ep)
237 return NULL;
238
239 ep->id = id;
240 ep->dev.class = &iscsi_endpoint_class;
Kay Sievers71610f52008-12-03 22:41:36 +0100241 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
Mike Christied82ff9be2008-05-21 15:54:13 -0500242 err = device_register(&ep->dev);
243 if (err)
244 goto free_ep;
245
246 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
247 if (err)
248 goto unregister_dev;
249
250 if (dd_size)
251 ep->dd_data = &ep[1];
252 return ep;
253
254unregister_dev:
255 device_unregister(&ep->dev);
256 return NULL;
257
258free_ep:
259 kfree(ep);
260 return NULL;
261}
262EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
263
264void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
265{
266 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
267 device_unregister(&ep->dev);
268}
269EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
270
271struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
272{
Mike Christief80f8682008-06-16 10:11:35 -0500273 struct iscsi_endpoint *ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500274 struct device *dev;
275
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400276 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
Mike Christied82ff9be2008-05-21 15:54:13 -0500277 iscsi_match_epid);
278 if (!dev)
279 return NULL;
280
Mike Christief80f8682008-06-16 10:11:35 -0500281 ep = iscsi_dev_to_endpoint(dev);
282 /*
283 * we can drop this now because the interface will prevent
284 * removals and lookups from racing.
285 */
286 put_device(dev);
287 return ep;
Mike Christied82ff9be2008-05-21 15:54:13 -0500288}
289EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
290
Mike Christie8d079132011-07-25 13:48:40 -0500291/*
292 * Interface to display network param to sysfs
293 */
294
295static void iscsi_iface_release(struct device *dev)
296{
297 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
298 struct device *parent = iface->dev.parent;
299
300 kfree(iface);
301 put_device(parent);
302}
303
304
305static struct class iscsi_iface_class = {
306 .name = "iscsi_iface",
307 .dev_release = iscsi_iface_release,
308};
309
310#define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
311struct device_attribute dev_attr_##_prefix##_##_name = \
312 __ATTR(_name, _mode, _show, _store)
313
314/* iface attrs show */
315#define iscsi_iface_attr_show(type, name, param_type, param) \
316static ssize_t \
317show_##type##_##name(struct device *dev, struct device_attribute *attr, \
318 char *buf) \
319{ \
320 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
321 struct iscsi_transport *t = iface->transport; \
322 return t->get_iface_param(iface, param_type, param, buf); \
323} \
324
325#define iscsi_iface_net_attr(type, name, param) \
326 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
327static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
328
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400329#define iscsi_iface_attr(type, name, param) \
330 iscsi_iface_attr_show(type, name, ISCSI_IFACE_PARAM, param) \
331static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
332
333/* generic read only ipv4 attribute */
Mike Christie8d079132011-07-25 13:48:40 -0500334iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
335iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
336iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
337iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400338iscsi_iface_net_attr(ipv4_iface, dhcp_dns_address_en,
339 ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN);
340iscsi_iface_net_attr(ipv4_iface, dhcp_slp_da_info_en,
341 ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN);
342iscsi_iface_net_attr(ipv4_iface, tos_en, ISCSI_NET_PARAM_IPV4_TOS_EN);
343iscsi_iface_net_attr(ipv4_iface, tos, ISCSI_NET_PARAM_IPV4_TOS);
344iscsi_iface_net_attr(ipv4_iface, grat_arp_en,
345 ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN);
346iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id_en,
347 ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN);
348iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id,
349 ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID);
350iscsi_iface_net_attr(ipv4_iface, dhcp_req_vendor_id_en,
351 ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN);
352iscsi_iface_net_attr(ipv4_iface, dhcp_use_vendor_id_en,
353 ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN);
354iscsi_iface_net_attr(ipv4_iface, dhcp_vendor_id,
355 ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID);
356iscsi_iface_net_attr(ipv4_iface, dhcp_learn_iqn_en,
357 ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN);
358iscsi_iface_net_attr(ipv4_iface, fragment_disable,
359 ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE);
360iscsi_iface_net_attr(ipv4_iface, incoming_forwarding_en,
361 ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN);
362iscsi_iface_net_attr(ipv4_iface, ttl, ISCSI_NET_PARAM_IPV4_TTL);
Mike Christie8d079132011-07-25 13:48:40 -0500363
364/* generic read only ipv6 attribute */
365iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400366iscsi_iface_net_attr(ipv6_iface, link_local_addr,
367 ISCSI_NET_PARAM_IPV6_LINKLOCAL);
Mike Christie8d079132011-07-25 13:48:40 -0500368iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
369iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
370 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
Mike Christie88f4f512011-10-06 03:56:58 -0500371iscsi_iface_net_attr(ipv6_iface, link_local_autocfg,
Mike Christie8d079132011-07-25 13:48:40 -0500372 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400373iscsi_iface_net_attr(ipv6_iface, link_local_state,
374 ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE);
375iscsi_iface_net_attr(ipv6_iface, router_state,
376 ISCSI_NET_PARAM_IPV6_ROUTER_STATE);
377iscsi_iface_net_attr(ipv6_iface, grat_neighbor_adv_en,
378 ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN);
379iscsi_iface_net_attr(ipv6_iface, mld_en, ISCSI_NET_PARAM_IPV6_MLD_EN);
380iscsi_iface_net_attr(ipv6_iface, flow_label, ISCSI_NET_PARAM_IPV6_FLOW_LABEL);
381iscsi_iface_net_attr(ipv6_iface, traffic_class,
382 ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS);
383iscsi_iface_net_attr(ipv6_iface, hop_limit, ISCSI_NET_PARAM_IPV6_HOP_LIMIT);
384iscsi_iface_net_attr(ipv6_iface, nd_reachable_tmo,
385 ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO);
386iscsi_iface_net_attr(ipv6_iface, nd_rexmit_time,
387 ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME);
388iscsi_iface_net_attr(ipv6_iface, nd_stale_tmo,
389 ISCSI_NET_PARAM_IPV6_ND_STALE_TMO);
390iscsi_iface_net_attr(ipv6_iface, dup_addr_detect_cnt,
391 ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT);
392iscsi_iface_net_attr(ipv6_iface, router_adv_link_mtu,
393 ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU);
Mike Christie8d079132011-07-25 13:48:40 -0500394
395/* common read only iface attribute */
396iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
Mike Christie2d636732011-10-11 17:55:11 -0500397iscsi_iface_net_attr(iface, vlan_id, ISCSI_NET_PARAM_VLAN_ID);
Mike Christie4223b9e2011-07-25 13:48:47 -0500398iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
399iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
Vikas Chaudhary8c7d40f2011-08-01 03:26:12 -0700400iscsi_iface_net_attr(iface, mtu, ISCSI_NET_PARAM_MTU);
Vikas Chaudharyfcb51242011-08-01 03:26:18 -0700401iscsi_iface_net_attr(iface, port, ISCSI_NET_PARAM_PORT);
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400402iscsi_iface_net_attr(iface, ipaddress_state, ISCSI_NET_PARAM_IPADDR_STATE);
403iscsi_iface_net_attr(iface, delayed_ack_en, ISCSI_NET_PARAM_DELAYED_ACK_EN);
404iscsi_iface_net_attr(iface, tcp_nagle_disable,
405 ISCSI_NET_PARAM_TCP_NAGLE_DISABLE);
406iscsi_iface_net_attr(iface, tcp_wsf_disable, ISCSI_NET_PARAM_TCP_WSF_DISABLE);
407iscsi_iface_net_attr(iface, tcp_wsf, ISCSI_NET_PARAM_TCP_WSF);
408iscsi_iface_net_attr(iface, tcp_timer_scale, ISCSI_NET_PARAM_TCP_TIMER_SCALE);
409iscsi_iface_net_attr(iface, tcp_timestamp_en, ISCSI_NET_PARAM_TCP_TIMESTAMP_EN);
410iscsi_iface_net_attr(iface, cache_id, ISCSI_NET_PARAM_CACHE_ID);
411iscsi_iface_net_attr(iface, redirect_en, ISCSI_NET_PARAM_REDIRECT_EN);
412
413/* common iscsi specific settings attributes */
414iscsi_iface_attr(iface, def_taskmgmt_tmo, ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO);
415iscsi_iface_attr(iface, header_digest, ISCSI_IFACE_PARAM_HDRDGST_EN);
416iscsi_iface_attr(iface, data_digest, ISCSI_IFACE_PARAM_DATADGST_EN);
417iscsi_iface_attr(iface, immediate_data, ISCSI_IFACE_PARAM_IMM_DATA_EN);
418iscsi_iface_attr(iface, initial_r2t, ISCSI_IFACE_PARAM_INITIAL_R2T_EN);
419iscsi_iface_attr(iface, data_seq_in_order,
420 ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN);
421iscsi_iface_attr(iface, data_pdu_in_order, ISCSI_IFACE_PARAM_PDU_INORDER_EN);
422iscsi_iface_attr(iface, erl, ISCSI_IFACE_PARAM_ERL);
423iscsi_iface_attr(iface, max_recv_dlength, ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH);
424iscsi_iface_attr(iface, first_burst_len, ISCSI_IFACE_PARAM_FIRST_BURST);
425iscsi_iface_attr(iface, max_outstanding_r2t, ISCSI_IFACE_PARAM_MAX_R2T);
426iscsi_iface_attr(iface, max_burst_len, ISCSI_IFACE_PARAM_MAX_BURST);
427iscsi_iface_attr(iface, chap_auth, ISCSI_IFACE_PARAM_CHAP_AUTH_EN);
428iscsi_iface_attr(iface, bidi_chap, ISCSI_IFACE_PARAM_BIDI_CHAP_EN);
429iscsi_iface_attr(iface, discovery_auth_optional,
430 ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL);
431iscsi_iface_attr(iface, discovery_logout,
432 ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN);
433iscsi_iface_attr(iface, strict_login_comp_en,
434 ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN);
435iscsi_iface_attr(iface, initiator_name, ISCSI_IFACE_PARAM_INITIATOR_NAME);
Mike Christie8d079132011-07-25 13:48:40 -0500436
Al Viro587a1f12011-07-23 23:11:19 -0400437static umode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
Mike Christie8d079132011-07-25 13:48:40 -0500438 struct attribute *attr, int i)
439{
440 struct device *dev = container_of(kobj, struct device, kobj);
441 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
442 struct iscsi_transport *t = iface->transport;
Mike Christieb82a1a22021-06-30 19:25:59 -0500443 int param = -1;
Mike Christie8d079132011-07-25 13:48:40 -0500444
Baokun Li2d0305422021-09-01 16:53:36 +0800445 if (attr == &dev_attr_iface_def_taskmgmt_tmo.attr)
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400446 param = ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO;
447 else if (attr == &dev_attr_iface_header_digest.attr)
448 param = ISCSI_IFACE_PARAM_HDRDGST_EN;
449 else if (attr == &dev_attr_iface_data_digest.attr)
450 param = ISCSI_IFACE_PARAM_DATADGST_EN;
451 else if (attr == &dev_attr_iface_immediate_data.attr)
452 param = ISCSI_IFACE_PARAM_IMM_DATA_EN;
453 else if (attr == &dev_attr_iface_initial_r2t.attr)
454 param = ISCSI_IFACE_PARAM_INITIAL_R2T_EN;
455 else if (attr == &dev_attr_iface_data_seq_in_order.attr)
456 param = ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN;
457 else if (attr == &dev_attr_iface_data_pdu_in_order.attr)
458 param = ISCSI_IFACE_PARAM_PDU_INORDER_EN;
459 else if (attr == &dev_attr_iface_erl.attr)
460 param = ISCSI_IFACE_PARAM_ERL;
461 else if (attr == &dev_attr_iface_max_recv_dlength.attr)
462 param = ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH;
463 else if (attr == &dev_attr_iface_first_burst_len.attr)
464 param = ISCSI_IFACE_PARAM_FIRST_BURST;
465 else if (attr == &dev_attr_iface_max_outstanding_r2t.attr)
466 param = ISCSI_IFACE_PARAM_MAX_R2T;
467 else if (attr == &dev_attr_iface_max_burst_len.attr)
468 param = ISCSI_IFACE_PARAM_MAX_BURST;
469 else if (attr == &dev_attr_iface_chap_auth.attr)
470 param = ISCSI_IFACE_PARAM_CHAP_AUTH_EN;
471 else if (attr == &dev_attr_iface_bidi_chap.attr)
472 param = ISCSI_IFACE_PARAM_BIDI_CHAP_EN;
473 else if (attr == &dev_attr_iface_discovery_auth_optional.attr)
474 param = ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL;
475 else if (attr == &dev_attr_iface_discovery_logout.attr)
476 param = ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN;
477 else if (attr == &dev_attr_iface_strict_login_comp_en.attr)
478 param = ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN;
479 else if (attr == &dev_attr_iface_initiator_name.attr)
480 param = ISCSI_IFACE_PARAM_INITIATOR_NAME;
Mike Christieb82a1a22021-06-30 19:25:59 -0500481
482 if (param != -1)
483 return t->attr_is_visible(ISCSI_IFACE_PARAM, param);
484
Baokun Li2d0305422021-09-01 16:53:36 +0800485 if (attr == &dev_attr_iface_enabled.attr)
486 param = ISCSI_NET_PARAM_IFACE_ENABLE;
487 else if (attr == &dev_attr_iface_vlan_id.attr)
Mike Christieb82a1a22021-06-30 19:25:59 -0500488 param = ISCSI_NET_PARAM_VLAN_ID;
489 else if (attr == &dev_attr_iface_vlan_priority.attr)
490 param = ISCSI_NET_PARAM_VLAN_PRIORITY;
491 else if (attr == &dev_attr_iface_vlan_enabled.attr)
492 param = ISCSI_NET_PARAM_VLAN_ENABLED;
493 else if (attr == &dev_attr_iface_mtu.attr)
494 param = ISCSI_NET_PARAM_MTU;
495 else if (attr == &dev_attr_iface_port.attr)
496 param = ISCSI_NET_PARAM_PORT;
497 else if (attr == &dev_attr_iface_ipaddress_state.attr)
498 param = ISCSI_NET_PARAM_IPADDR_STATE;
499 else if (attr == &dev_attr_iface_delayed_ack_en.attr)
500 param = ISCSI_NET_PARAM_DELAYED_ACK_EN;
501 else if (attr == &dev_attr_iface_tcp_nagle_disable.attr)
502 param = ISCSI_NET_PARAM_TCP_NAGLE_DISABLE;
503 else if (attr == &dev_attr_iface_tcp_wsf_disable.attr)
504 param = ISCSI_NET_PARAM_TCP_WSF_DISABLE;
505 else if (attr == &dev_attr_iface_tcp_wsf.attr)
506 param = ISCSI_NET_PARAM_TCP_WSF;
507 else if (attr == &dev_attr_iface_tcp_timer_scale.attr)
508 param = ISCSI_NET_PARAM_TCP_TIMER_SCALE;
509 else if (attr == &dev_attr_iface_tcp_timestamp_en.attr)
510 param = ISCSI_NET_PARAM_TCP_TIMESTAMP_EN;
511 else if (attr == &dev_attr_iface_cache_id.attr)
512 param = ISCSI_NET_PARAM_CACHE_ID;
513 else if (attr == &dev_attr_iface_redirect_en.attr)
514 param = ISCSI_NET_PARAM_REDIRECT_EN;
Mike Christieb78dbba2011-07-25 13:48:44 -0500515 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
Mike Christie8d079132011-07-25 13:48:40 -0500516 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500517 param = ISCSI_NET_PARAM_IPV4_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500518 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500519 param = ISCSI_NET_PARAM_IPV4_GW;
Mike Christie8d079132011-07-25 13:48:40 -0500520 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500521 param = ISCSI_NET_PARAM_IPV4_SUBNET;
Mike Christie8d079132011-07-25 13:48:40 -0500522 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500523 param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400524 else if (attr ==
525 &dev_attr_ipv4_iface_dhcp_dns_address_en.attr)
526 param = ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN;
527 else if (attr ==
528 &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr)
529 param = ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN;
530 else if (attr == &dev_attr_ipv4_iface_tos_en.attr)
531 param = ISCSI_NET_PARAM_IPV4_TOS_EN;
532 else if (attr == &dev_attr_ipv4_iface_tos.attr)
533 param = ISCSI_NET_PARAM_IPV4_TOS;
534 else if (attr == &dev_attr_ipv4_iface_grat_arp_en.attr)
535 param = ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN;
536 else if (attr ==
537 &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr)
538 param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN;
539 else if (attr == &dev_attr_ipv4_iface_dhcp_alt_client_id.attr)
540 param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID;
541 else if (attr ==
542 &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr)
543 param = ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN;
544 else if (attr ==
545 &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr)
546 param = ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN;
547 else if (attr == &dev_attr_ipv4_iface_dhcp_vendor_id.attr)
548 param = ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID;
549 else if (attr ==
550 &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr)
551 param = ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN;
552 else if (attr ==
553 &dev_attr_ipv4_iface_fragment_disable.attr)
554 param = ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE;
555 else if (attr ==
556 &dev_attr_ipv4_iface_incoming_forwarding_en.attr)
557 param = ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN;
558 else if (attr == &dev_attr_ipv4_iface_ttl.attr)
559 param = ISCSI_NET_PARAM_IPV4_TTL;
Mike Christieb78dbba2011-07-25 13:48:44 -0500560 else
561 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500562 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
563 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500564 param = ISCSI_NET_PARAM_IPV6_ADDR;
Mike Christie8d079132011-07-25 13:48:40 -0500565 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500566 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
Mike Christie8d079132011-07-25 13:48:40 -0500567 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500568 param = ISCSI_NET_PARAM_IPV6_ROUTER;
Mike Christie8d079132011-07-25 13:48:40 -0500569 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500570 param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
Mike Christie88f4f512011-10-06 03:56:58 -0500571 else if (attr == &dev_attr_ipv6_iface_link_local_autocfg.attr)
Mike Christieb78dbba2011-07-25 13:48:44 -0500572 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400573 else if (attr == &dev_attr_ipv6_iface_link_local_state.attr)
574 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE;
575 else if (attr == &dev_attr_ipv6_iface_router_state.attr)
576 param = ISCSI_NET_PARAM_IPV6_ROUTER_STATE;
577 else if (attr ==
578 &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr)
579 param = ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN;
580 else if (attr == &dev_attr_ipv6_iface_mld_en.attr)
581 param = ISCSI_NET_PARAM_IPV6_MLD_EN;
582 else if (attr == &dev_attr_ipv6_iface_flow_label.attr)
583 param = ISCSI_NET_PARAM_IPV6_FLOW_LABEL;
584 else if (attr == &dev_attr_ipv6_iface_traffic_class.attr)
585 param = ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS;
586 else if (attr == &dev_attr_ipv6_iface_hop_limit.attr)
587 param = ISCSI_NET_PARAM_IPV6_HOP_LIMIT;
588 else if (attr == &dev_attr_ipv6_iface_nd_reachable_tmo.attr)
589 param = ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO;
590 else if (attr == &dev_attr_ipv6_iface_nd_rexmit_time.attr)
591 param = ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME;
592 else if (attr == &dev_attr_ipv6_iface_nd_stale_tmo.attr)
593 param = ISCSI_NET_PARAM_IPV6_ND_STALE_TMO;
594 else if (attr == &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr)
595 param = ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT;
596 else if (attr == &dev_attr_ipv6_iface_router_adv_link_mtu.attr)
597 param = ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU;
Mike Christieb78dbba2011-07-25 13:48:44 -0500598 else
599 return 0;
600 } else {
601 WARN_ONCE(1, "Invalid iface attr");
602 return 0;
Mike Christie8d079132011-07-25 13:48:40 -0500603 }
604
Mike Christieb82a1a22021-06-30 19:25:59 -0500605 return t->attr_is_visible(ISCSI_NET_PARAM, param);
Mike Christie8d079132011-07-25 13:48:40 -0500606}
607
608static struct attribute *iscsi_iface_attrs[] = {
609 &dev_attr_iface_enabled.attr,
Mike Christie2d636732011-10-11 17:55:11 -0500610 &dev_attr_iface_vlan_id.attr,
Mike Christie4223b9e2011-07-25 13:48:47 -0500611 &dev_attr_iface_vlan_priority.attr,
612 &dev_attr_iface_vlan_enabled.attr,
Mike Christie8d079132011-07-25 13:48:40 -0500613 &dev_attr_ipv4_iface_ipaddress.attr,
614 &dev_attr_ipv4_iface_gateway.attr,
615 &dev_attr_ipv4_iface_subnet.attr,
616 &dev_attr_ipv4_iface_bootproto.attr,
617 &dev_attr_ipv6_iface_ipaddress.attr,
618 &dev_attr_ipv6_iface_link_local_addr.attr,
619 &dev_attr_ipv6_iface_router_addr.attr,
620 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
Mike Christie88f4f512011-10-06 03:56:58 -0500621 &dev_attr_ipv6_iface_link_local_autocfg.attr,
Vikas Chaudhary8c7d40f2011-08-01 03:26:12 -0700622 &dev_attr_iface_mtu.attr,
Vikas Chaudharyfcb51242011-08-01 03:26:18 -0700623 &dev_attr_iface_port.attr,
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400624 &dev_attr_iface_ipaddress_state.attr,
625 &dev_attr_iface_delayed_ack_en.attr,
626 &dev_attr_iface_tcp_nagle_disable.attr,
627 &dev_attr_iface_tcp_wsf_disable.attr,
628 &dev_attr_iface_tcp_wsf.attr,
629 &dev_attr_iface_tcp_timer_scale.attr,
630 &dev_attr_iface_tcp_timestamp_en.attr,
631 &dev_attr_iface_cache_id.attr,
632 &dev_attr_iface_redirect_en.attr,
633 &dev_attr_iface_def_taskmgmt_tmo.attr,
634 &dev_attr_iface_header_digest.attr,
635 &dev_attr_iface_data_digest.attr,
636 &dev_attr_iface_immediate_data.attr,
637 &dev_attr_iface_initial_r2t.attr,
638 &dev_attr_iface_data_seq_in_order.attr,
639 &dev_attr_iface_data_pdu_in_order.attr,
640 &dev_attr_iface_erl.attr,
641 &dev_attr_iface_max_recv_dlength.attr,
642 &dev_attr_iface_first_burst_len.attr,
643 &dev_attr_iface_max_outstanding_r2t.attr,
644 &dev_attr_iface_max_burst_len.attr,
645 &dev_attr_iface_chap_auth.attr,
646 &dev_attr_iface_bidi_chap.attr,
647 &dev_attr_iface_discovery_auth_optional.attr,
648 &dev_attr_iface_discovery_logout.attr,
649 &dev_attr_iface_strict_login_comp_en.attr,
650 &dev_attr_iface_initiator_name.attr,
651 &dev_attr_ipv4_iface_dhcp_dns_address_en.attr,
652 &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr,
653 &dev_attr_ipv4_iface_tos_en.attr,
654 &dev_attr_ipv4_iface_tos.attr,
655 &dev_attr_ipv4_iface_grat_arp_en.attr,
656 &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr,
657 &dev_attr_ipv4_iface_dhcp_alt_client_id.attr,
658 &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr,
659 &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr,
660 &dev_attr_ipv4_iface_dhcp_vendor_id.attr,
661 &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr,
662 &dev_attr_ipv4_iface_fragment_disable.attr,
663 &dev_attr_ipv4_iface_incoming_forwarding_en.attr,
664 &dev_attr_ipv4_iface_ttl.attr,
665 &dev_attr_ipv6_iface_link_local_state.attr,
666 &dev_attr_ipv6_iface_router_state.attr,
667 &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr,
668 &dev_attr_ipv6_iface_mld_en.attr,
669 &dev_attr_ipv6_iface_flow_label.attr,
670 &dev_attr_ipv6_iface_traffic_class.attr,
671 &dev_attr_ipv6_iface_hop_limit.attr,
672 &dev_attr_ipv6_iface_nd_reachable_tmo.attr,
673 &dev_attr_ipv6_iface_nd_rexmit_time.attr,
674 &dev_attr_ipv6_iface_nd_stale_tmo.attr,
675 &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr,
676 &dev_attr_ipv6_iface_router_adv_link_mtu.attr,
Mike Christie8d079132011-07-25 13:48:40 -0500677 NULL,
678};
679
680static struct attribute_group iscsi_iface_group = {
681 .attrs = iscsi_iface_attrs,
682 .is_visible = iscsi_iface_attr_is_visible,
683};
684
Harish Zunjarrao3e0f65b2013-10-18 09:01:41 -0400685/* convert iscsi_ipaddress_state values to ascii string name */
686static const struct {
687 enum iscsi_ipaddress_state value;
688 char *name;
689} iscsi_ipaddress_state_names[] = {
690 {ISCSI_IPDDRESS_STATE_UNCONFIGURED, "Unconfigured" },
691 {ISCSI_IPDDRESS_STATE_ACQUIRING, "Acquiring" },
692 {ISCSI_IPDDRESS_STATE_TENTATIVE, "Tentative" },
693 {ISCSI_IPDDRESS_STATE_VALID, "Valid" },
694 {ISCSI_IPDDRESS_STATE_DISABLING, "Disabling" },
695 {ISCSI_IPDDRESS_STATE_INVALID, "Invalid" },
696 {ISCSI_IPDDRESS_STATE_DEPRECATED, "Deprecated" },
697};
698
699char *iscsi_get_ipaddress_state_name(enum iscsi_ipaddress_state port_state)
700{
701 int i;
702 char *state = NULL;
703
704 for (i = 0; i < ARRAY_SIZE(iscsi_ipaddress_state_names); i++) {
705 if (iscsi_ipaddress_state_names[i].value == port_state) {
706 state = iscsi_ipaddress_state_names[i].name;
707 break;
708 }
709 }
710 return state;
711}
712EXPORT_SYMBOL_GPL(iscsi_get_ipaddress_state_name);
713
714/* convert iscsi_router_state values to ascii string name */
715static const struct {
716 enum iscsi_router_state value;
717 char *name;
718} iscsi_router_state_names[] = {
719 {ISCSI_ROUTER_STATE_UNKNOWN, "Unknown" },
720 {ISCSI_ROUTER_STATE_ADVERTISED, "Advertised" },
721 {ISCSI_ROUTER_STATE_MANUAL, "Manual" },
722 {ISCSI_ROUTER_STATE_STALE, "Stale" },
723};
724
725char *iscsi_get_router_state_name(enum iscsi_router_state router_state)
726{
727 int i;
728 char *state = NULL;
729
730 for (i = 0; i < ARRAY_SIZE(iscsi_router_state_names); i++) {
731 if (iscsi_router_state_names[i].value == router_state) {
732 state = iscsi_router_state_names[i].name;
733 break;
734 }
735 }
736 return state;
737}
738EXPORT_SYMBOL_GPL(iscsi_get_router_state_name);
739
Mike Christie8d079132011-07-25 13:48:40 -0500740struct iscsi_iface *
741iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
742 uint32_t iface_type, uint32_t iface_num, int dd_size)
743{
744 struct iscsi_iface *iface;
745 int err;
746
747 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
748 if (!iface)
749 return NULL;
750
751 iface->transport = transport;
752 iface->iface_type = iface_type;
753 iface->iface_num = iface_num;
754 iface->dev.release = iscsi_iface_release;
755 iface->dev.class = &iscsi_iface_class;
756 /* parent reference released in iscsi_iface_release */
757 iface->dev.parent = get_device(&shost->shost_gendev);
758 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
759 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
760 iface_num);
761 else
762 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
763 iface_num);
764
765 err = device_register(&iface->dev);
766 if (err)
767 goto free_iface;
768
769 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
770 if (err)
771 goto unreg_iface;
772
773 if (dd_size)
774 iface->dd_data = &iface[1];
775 return iface;
776
777unreg_iface:
778 device_unregister(&iface->dev);
779 return NULL;
780
781free_iface:
782 put_device(iface->dev.parent);
783 kfree(iface);
784 return NULL;
785}
786EXPORT_SYMBOL_GPL(iscsi_create_iface);
787
788void iscsi_destroy_iface(struct iscsi_iface *iface)
789{
790 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
791 device_unregister(&iface->dev);
792}
793EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
794
Mike Christie90eeb012011-07-25 13:48:50 -0500795/*
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -0400796 * Interface to display flash node params to sysfs
797 */
798
799#define ISCSI_FLASHNODE_ATTR(_prefix, _name, _mode, _show, _store) \
800struct device_attribute dev_attr_##_prefix##_##_name = \
801 __ATTR(_name, _mode, _show, _store)
802
803/* flash node session attrs show */
804#define iscsi_flashnode_sess_attr_show(type, name, param) \
805static ssize_t \
806show_##type##_##name(struct device *dev, struct device_attribute *attr, \
807 char *buf) \
808{ \
809 struct iscsi_bus_flash_session *fnode_sess = \
810 iscsi_dev_to_flash_session(dev);\
811 struct iscsi_transport *t = fnode_sess->transport; \
812 return t->get_flashnode_param(fnode_sess, param, buf); \
813} \
814
815
816#define iscsi_flashnode_sess_attr(type, name, param) \
817 iscsi_flashnode_sess_attr_show(type, name, param) \
818static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
819 show_##type##_##name, NULL);
820
821/* Flash node session attributes */
822
823iscsi_flashnode_sess_attr(fnode, auto_snd_tgt_disable,
824 ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE);
825iscsi_flashnode_sess_attr(fnode, discovery_session,
826 ISCSI_FLASHNODE_DISCOVERY_SESS);
827iscsi_flashnode_sess_attr(fnode, portal_type, ISCSI_FLASHNODE_PORTAL_TYPE);
828iscsi_flashnode_sess_attr(fnode, entry_enable, ISCSI_FLASHNODE_ENTRY_EN);
829iscsi_flashnode_sess_attr(fnode, immediate_data, ISCSI_FLASHNODE_IMM_DATA_EN);
830iscsi_flashnode_sess_attr(fnode, initial_r2t, ISCSI_FLASHNODE_INITIAL_R2T_EN);
831iscsi_flashnode_sess_attr(fnode, data_seq_in_order,
832 ISCSI_FLASHNODE_DATASEQ_INORDER);
833iscsi_flashnode_sess_attr(fnode, data_pdu_in_order,
834 ISCSI_FLASHNODE_PDU_INORDER);
835iscsi_flashnode_sess_attr(fnode, chap_auth, ISCSI_FLASHNODE_CHAP_AUTH_EN);
836iscsi_flashnode_sess_attr(fnode, discovery_logout,
837 ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN);
838iscsi_flashnode_sess_attr(fnode, bidi_chap, ISCSI_FLASHNODE_BIDI_CHAP_EN);
839iscsi_flashnode_sess_attr(fnode, discovery_auth_optional,
840 ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL);
841iscsi_flashnode_sess_attr(fnode, erl, ISCSI_FLASHNODE_ERL);
842iscsi_flashnode_sess_attr(fnode, first_burst_len, ISCSI_FLASHNODE_FIRST_BURST);
843iscsi_flashnode_sess_attr(fnode, def_time2wait, ISCSI_FLASHNODE_DEF_TIME2WAIT);
844iscsi_flashnode_sess_attr(fnode, def_time2retain,
845 ISCSI_FLASHNODE_DEF_TIME2RETAIN);
846iscsi_flashnode_sess_attr(fnode, max_outstanding_r2t, ISCSI_FLASHNODE_MAX_R2T);
847iscsi_flashnode_sess_attr(fnode, isid, ISCSI_FLASHNODE_ISID);
848iscsi_flashnode_sess_attr(fnode, tsid, ISCSI_FLASHNODE_TSID);
849iscsi_flashnode_sess_attr(fnode, max_burst_len, ISCSI_FLASHNODE_MAX_BURST);
850iscsi_flashnode_sess_attr(fnode, def_taskmgmt_tmo,
851 ISCSI_FLASHNODE_DEF_TASKMGMT_TMO);
852iscsi_flashnode_sess_attr(fnode, targetalias, ISCSI_FLASHNODE_ALIAS);
853iscsi_flashnode_sess_attr(fnode, targetname, ISCSI_FLASHNODE_NAME);
854iscsi_flashnode_sess_attr(fnode, tpgt, ISCSI_FLASHNODE_TPGT);
855iscsi_flashnode_sess_attr(fnode, discovery_parent_idx,
856 ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX);
857iscsi_flashnode_sess_attr(fnode, discovery_parent_type,
858 ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE);
859iscsi_flashnode_sess_attr(fnode, chap_in_idx, ISCSI_FLASHNODE_CHAP_IN_IDX);
860iscsi_flashnode_sess_attr(fnode, chap_out_idx, ISCSI_FLASHNODE_CHAP_OUT_IDX);
861iscsi_flashnode_sess_attr(fnode, username, ISCSI_FLASHNODE_USERNAME);
862iscsi_flashnode_sess_attr(fnode, username_in, ISCSI_FLASHNODE_USERNAME_IN);
863iscsi_flashnode_sess_attr(fnode, password, ISCSI_FLASHNODE_PASSWORD);
864iscsi_flashnode_sess_attr(fnode, password_in, ISCSI_FLASHNODE_PASSWORD_IN);
865iscsi_flashnode_sess_attr(fnode, is_boot_target, ISCSI_FLASHNODE_IS_BOOT_TGT);
866
867static struct attribute *iscsi_flashnode_sess_attrs[] = {
868 &dev_attr_fnode_auto_snd_tgt_disable.attr,
869 &dev_attr_fnode_discovery_session.attr,
870 &dev_attr_fnode_portal_type.attr,
871 &dev_attr_fnode_entry_enable.attr,
872 &dev_attr_fnode_immediate_data.attr,
873 &dev_attr_fnode_initial_r2t.attr,
874 &dev_attr_fnode_data_seq_in_order.attr,
875 &dev_attr_fnode_data_pdu_in_order.attr,
876 &dev_attr_fnode_chap_auth.attr,
877 &dev_attr_fnode_discovery_logout.attr,
878 &dev_attr_fnode_bidi_chap.attr,
879 &dev_attr_fnode_discovery_auth_optional.attr,
880 &dev_attr_fnode_erl.attr,
881 &dev_attr_fnode_first_burst_len.attr,
882 &dev_attr_fnode_def_time2wait.attr,
883 &dev_attr_fnode_def_time2retain.attr,
884 &dev_attr_fnode_max_outstanding_r2t.attr,
885 &dev_attr_fnode_isid.attr,
886 &dev_attr_fnode_tsid.attr,
887 &dev_attr_fnode_max_burst_len.attr,
888 &dev_attr_fnode_def_taskmgmt_tmo.attr,
889 &dev_attr_fnode_targetalias.attr,
890 &dev_attr_fnode_targetname.attr,
891 &dev_attr_fnode_tpgt.attr,
892 &dev_attr_fnode_discovery_parent_idx.attr,
893 &dev_attr_fnode_discovery_parent_type.attr,
894 &dev_attr_fnode_chap_in_idx.attr,
895 &dev_attr_fnode_chap_out_idx.attr,
896 &dev_attr_fnode_username.attr,
897 &dev_attr_fnode_username_in.attr,
898 &dev_attr_fnode_password.attr,
899 &dev_attr_fnode_password_in.attr,
900 &dev_attr_fnode_is_boot_target.attr,
901 NULL,
902};
903
904static umode_t iscsi_flashnode_sess_attr_is_visible(struct kobject *kobj,
905 struct attribute *attr,
906 int i)
907{
908 struct device *dev = container_of(kobj, struct device, kobj);
909 struct iscsi_bus_flash_session *fnode_sess =
910 iscsi_dev_to_flash_session(dev);
911 struct iscsi_transport *t = fnode_sess->transport;
912 int param;
913
914 if (attr == &dev_attr_fnode_auto_snd_tgt_disable.attr) {
915 param = ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE;
916 } else if (attr == &dev_attr_fnode_discovery_session.attr) {
917 param = ISCSI_FLASHNODE_DISCOVERY_SESS;
918 } else if (attr == &dev_attr_fnode_portal_type.attr) {
919 param = ISCSI_FLASHNODE_PORTAL_TYPE;
920 } else if (attr == &dev_attr_fnode_entry_enable.attr) {
921 param = ISCSI_FLASHNODE_ENTRY_EN;
922 } else if (attr == &dev_attr_fnode_immediate_data.attr) {
923 param = ISCSI_FLASHNODE_IMM_DATA_EN;
924 } else if (attr == &dev_attr_fnode_initial_r2t.attr) {
925 param = ISCSI_FLASHNODE_INITIAL_R2T_EN;
926 } else if (attr == &dev_attr_fnode_data_seq_in_order.attr) {
927 param = ISCSI_FLASHNODE_DATASEQ_INORDER;
928 } else if (attr == &dev_attr_fnode_data_pdu_in_order.attr) {
929 param = ISCSI_FLASHNODE_PDU_INORDER;
930 } else if (attr == &dev_attr_fnode_chap_auth.attr) {
931 param = ISCSI_FLASHNODE_CHAP_AUTH_EN;
932 } else if (attr == &dev_attr_fnode_discovery_logout.attr) {
933 param = ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN;
934 } else if (attr == &dev_attr_fnode_bidi_chap.attr) {
935 param = ISCSI_FLASHNODE_BIDI_CHAP_EN;
936 } else if (attr == &dev_attr_fnode_discovery_auth_optional.attr) {
937 param = ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL;
938 } else if (attr == &dev_attr_fnode_erl.attr) {
939 param = ISCSI_FLASHNODE_ERL;
940 } else if (attr == &dev_attr_fnode_first_burst_len.attr) {
941 param = ISCSI_FLASHNODE_FIRST_BURST;
942 } else if (attr == &dev_attr_fnode_def_time2wait.attr) {
943 param = ISCSI_FLASHNODE_DEF_TIME2WAIT;
944 } else if (attr == &dev_attr_fnode_def_time2retain.attr) {
945 param = ISCSI_FLASHNODE_DEF_TIME2RETAIN;
946 } else if (attr == &dev_attr_fnode_max_outstanding_r2t.attr) {
947 param = ISCSI_FLASHNODE_MAX_R2T;
948 } else if (attr == &dev_attr_fnode_isid.attr) {
949 param = ISCSI_FLASHNODE_ISID;
950 } else if (attr == &dev_attr_fnode_tsid.attr) {
951 param = ISCSI_FLASHNODE_TSID;
952 } else if (attr == &dev_attr_fnode_max_burst_len.attr) {
953 param = ISCSI_FLASHNODE_MAX_BURST;
954 } else if (attr == &dev_attr_fnode_def_taskmgmt_tmo.attr) {
955 param = ISCSI_FLASHNODE_DEF_TASKMGMT_TMO;
956 } else if (attr == &dev_attr_fnode_targetalias.attr) {
957 param = ISCSI_FLASHNODE_ALIAS;
958 } else if (attr == &dev_attr_fnode_targetname.attr) {
959 param = ISCSI_FLASHNODE_NAME;
960 } else if (attr == &dev_attr_fnode_tpgt.attr) {
961 param = ISCSI_FLASHNODE_TPGT;
962 } else if (attr == &dev_attr_fnode_discovery_parent_idx.attr) {
963 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX;
964 } else if (attr == &dev_attr_fnode_discovery_parent_type.attr) {
965 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE;
966 } else if (attr == &dev_attr_fnode_chap_in_idx.attr) {
967 param = ISCSI_FLASHNODE_CHAP_IN_IDX;
968 } else if (attr == &dev_attr_fnode_chap_out_idx.attr) {
969 param = ISCSI_FLASHNODE_CHAP_OUT_IDX;
970 } else if (attr == &dev_attr_fnode_username.attr) {
971 param = ISCSI_FLASHNODE_USERNAME;
972 } else if (attr == &dev_attr_fnode_username_in.attr) {
973 param = ISCSI_FLASHNODE_USERNAME_IN;
974 } else if (attr == &dev_attr_fnode_password.attr) {
975 param = ISCSI_FLASHNODE_PASSWORD;
976 } else if (attr == &dev_attr_fnode_password_in.attr) {
977 param = ISCSI_FLASHNODE_PASSWORD_IN;
978 } else if (attr == &dev_attr_fnode_is_boot_target.attr) {
979 param = ISCSI_FLASHNODE_IS_BOOT_TGT;
980 } else {
981 WARN_ONCE(1, "Invalid flashnode session attr");
982 return 0;
983 }
984
985 return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
986}
987
988static struct attribute_group iscsi_flashnode_sess_attr_group = {
989 .attrs = iscsi_flashnode_sess_attrs,
990 .is_visible = iscsi_flashnode_sess_attr_is_visible,
991};
992
993static const struct attribute_group *iscsi_flashnode_sess_attr_groups[] = {
994 &iscsi_flashnode_sess_attr_group,
995 NULL,
996};
997
998static void iscsi_flashnode_sess_release(struct device *dev)
999{
1000 struct iscsi_bus_flash_session *fnode_sess =
1001 iscsi_dev_to_flash_session(dev);
1002
1003 kfree(fnode_sess->targetname);
1004 kfree(fnode_sess->targetalias);
1005 kfree(fnode_sess->portal_type);
1006 kfree(fnode_sess);
1007}
1008
Bhumika Goyal7988faf2017-08-19 13:52:24 +05301009static const struct device_type iscsi_flashnode_sess_dev_type = {
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001010 .name = "iscsi_flashnode_sess_dev_type",
1011 .groups = iscsi_flashnode_sess_attr_groups,
1012 .release = iscsi_flashnode_sess_release,
1013};
1014
1015/* flash node connection attrs show */
1016#define iscsi_flashnode_conn_attr_show(type, name, param) \
1017static ssize_t \
1018show_##type##_##name(struct device *dev, struct device_attribute *attr, \
1019 char *buf) \
1020{ \
1021 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);\
1022 struct iscsi_bus_flash_session *fnode_sess = \
1023 iscsi_flash_conn_to_flash_session(fnode_conn);\
1024 struct iscsi_transport *t = fnode_conn->transport; \
1025 return t->get_flashnode_param(fnode_sess, param, buf); \
1026} \
1027
1028
1029#define iscsi_flashnode_conn_attr(type, name, param) \
1030 iscsi_flashnode_conn_attr_show(type, name, param) \
1031static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
1032 show_##type##_##name, NULL);
1033
1034/* Flash node connection attributes */
1035
1036iscsi_flashnode_conn_attr(fnode, is_fw_assigned_ipv6,
1037 ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6);
1038iscsi_flashnode_conn_attr(fnode, header_digest, ISCSI_FLASHNODE_HDR_DGST_EN);
1039iscsi_flashnode_conn_attr(fnode, data_digest, ISCSI_FLASHNODE_DATA_DGST_EN);
1040iscsi_flashnode_conn_attr(fnode, snack_req, ISCSI_FLASHNODE_SNACK_REQ_EN);
1041iscsi_flashnode_conn_attr(fnode, tcp_timestamp_stat,
1042 ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT);
1043iscsi_flashnode_conn_attr(fnode, tcp_nagle_disable,
1044 ISCSI_FLASHNODE_TCP_NAGLE_DISABLE);
1045iscsi_flashnode_conn_attr(fnode, tcp_wsf_disable,
1046 ISCSI_FLASHNODE_TCP_WSF_DISABLE);
1047iscsi_flashnode_conn_attr(fnode, tcp_timer_scale,
1048 ISCSI_FLASHNODE_TCP_TIMER_SCALE);
1049iscsi_flashnode_conn_attr(fnode, tcp_timestamp_enable,
1050 ISCSI_FLASHNODE_TCP_TIMESTAMP_EN);
1051iscsi_flashnode_conn_attr(fnode, fragment_disable,
1052 ISCSI_FLASHNODE_IP_FRAG_DISABLE);
1053iscsi_flashnode_conn_attr(fnode, keepalive_tmo, ISCSI_FLASHNODE_KEEPALIVE_TMO);
1054iscsi_flashnode_conn_attr(fnode, port, ISCSI_FLASHNODE_PORT);
1055iscsi_flashnode_conn_attr(fnode, ipaddress, ISCSI_FLASHNODE_IPADDR);
1056iscsi_flashnode_conn_attr(fnode, max_recv_dlength,
1057 ISCSI_FLASHNODE_MAX_RECV_DLENGTH);
1058iscsi_flashnode_conn_attr(fnode, max_xmit_dlength,
1059 ISCSI_FLASHNODE_MAX_XMIT_DLENGTH);
1060iscsi_flashnode_conn_attr(fnode, local_port, ISCSI_FLASHNODE_LOCAL_PORT);
1061iscsi_flashnode_conn_attr(fnode, ipv4_tos, ISCSI_FLASHNODE_IPV4_TOS);
1062iscsi_flashnode_conn_attr(fnode, ipv6_traffic_class, ISCSI_FLASHNODE_IPV6_TC);
1063iscsi_flashnode_conn_attr(fnode, ipv6_flow_label,
1064 ISCSI_FLASHNODE_IPV6_FLOW_LABEL);
1065iscsi_flashnode_conn_attr(fnode, redirect_ipaddr,
1066 ISCSI_FLASHNODE_REDIRECT_IPADDR);
1067iscsi_flashnode_conn_attr(fnode, max_segment_size,
1068 ISCSI_FLASHNODE_MAX_SEGMENT_SIZE);
1069iscsi_flashnode_conn_attr(fnode, link_local_ipv6,
1070 ISCSI_FLASHNODE_LINK_LOCAL_IPV6);
1071iscsi_flashnode_conn_attr(fnode, tcp_xmit_wsf, ISCSI_FLASHNODE_TCP_XMIT_WSF);
1072iscsi_flashnode_conn_attr(fnode, tcp_recv_wsf, ISCSI_FLASHNODE_TCP_RECV_WSF);
1073iscsi_flashnode_conn_attr(fnode, statsn, ISCSI_FLASHNODE_STATSN);
1074iscsi_flashnode_conn_attr(fnode, exp_statsn, ISCSI_FLASHNODE_EXP_STATSN);
1075
1076static struct attribute *iscsi_flashnode_conn_attrs[] = {
1077 &dev_attr_fnode_is_fw_assigned_ipv6.attr,
1078 &dev_attr_fnode_header_digest.attr,
1079 &dev_attr_fnode_data_digest.attr,
1080 &dev_attr_fnode_snack_req.attr,
1081 &dev_attr_fnode_tcp_timestamp_stat.attr,
1082 &dev_attr_fnode_tcp_nagle_disable.attr,
1083 &dev_attr_fnode_tcp_wsf_disable.attr,
1084 &dev_attr_fnode_tcp_timer_scale.attr,
1085 &dev_attr_fnode_tcp_timestamp_enable.attr,
1086 &dev_attr_fnode_fragment_disable.attr,
1087 &dev_attr_fnode_max_recv_dlength.attr,
1088 &dev_attr_fnode_max_xmit_dlength.attr,
1089 &dev_attr_fnode_keepalive_tmo.attr,
1090 &dev_attr_fnode_port.attr,
1091 &dev_attr_fnode_ipaddress.attr,
1092 &dev_attr_fnode_redirect_ipaddr.attr,
1093 &dev_attr_fnode_max_segment_size.attr,
1094 &dev_attr_fnode_local_port.attr,
1095 &dev_attr_fnode_ipv4_tos.attr,
1096 &dev_attr_fnode_ipv6_traffic_class.attr,
1097 &dev_attr_fnode_ipv6_flow_label.attr,
1098 &dev_attr_fnode_link_local_ipv6.attr,
1099 &dev_attr_fnode_tcp_xmit_wsf.attr,
1100 &dev_attr_fnode_tcp_recv_wsf.attr,
1101 &dev_attr_fnode_statsn.attr,
1102 &dev_attr_fnode_exp_statsn.attr,
1103 NULL,
1104};
1105
1106static umode_t iscsi_flashnode_conn_attr_is_visible(struct kobject *kobj,
1107 struct attribute *attr,
1108 int i)
1109{
1110 struct device *dev = container_of(kobj, struct device, kobj);
1111 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
1112 struct iscsi_transport *t = fnode_conn->transport;
1113 int param;
1114
1115 if (attr == &dev_attr_fnode_is_fw_assigned_ipv6.attr) {
1116 param = ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6;
1117 } else if (attr == &dev_attr_fnode_header_digest.attr) {
1118 param = ISCSI_FLASHNODE_HDR_DGST_EN;
1119 } else if (attr == &dev_attr_fnode_data_digest.attr) {
1120 param = ISCSI_FLASHNODE_DATA_DGST_EN;
1121 } else if (attr == &dev_attr_fnode_snack_req.attr) {
1122 param = ISCSI_FLASHNODE_SNACK_REQ_EN;
1123 } else if (attr == &dev_attr_fnode_tcp_timestamp_stat.attr) {
1124 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT;
1125 } else if (attr == &dev_attr_fnode_tcp_nagle_disable.attr) {
1126 param = ISCSI_FLASHNODE_TCP_NAGLE_DISABLE;
1127 } else if (attr == &dev_attr_fnode_tcp_wsf_disable.attr) {
1128 param = ISCSI_FLASHNODE_TCP_WSF_DISABLE;
1129 } else if (attr == &dev_attr_fnode_tcp_timer_scale.attr) {
1130 param = ISCSI_FLASHNODE_TCP_TIMER_SCALE;
1131 } else if (attr == &dev_attr_fnode_tcp_timestamp_enable.attr) {
1132 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_EN;
1133 } else if (attr == &dev_attr_fnode_fragment_disable.attr) {
1134 param = ISCSI_FLASHNODE_IP_FRAG_DISABLE;
1135 } else if (attr == &dev_attr_fnode_max_recv_dlength.attr) {
1136 param = ISCSI_FLASHNODE_MAX_RECV_DLENGTH;
1137 } else if (attr == &dev_attr_fnode_max_xmit_dlength.attr) {
1138 param = ISCSI_FLASHNODE_MAX_XMIT_DLENGTH;
1139 } else if (attr == &dev_attr_fnode_keepalive_tmo.attr) {
1140 param = ISCSI_FLASHNODE_KEEPALIVE_TMO;
1141 } else if (attr == &dev_attr_fnode_port.attr) {
1142 param = ISCSI_FLASHNODE_PORT;
1143 } else if (attr == &dev_attr_fnode_ipaddress.attr) {
1144 param = ISCSI_FLASHNODE_IPADDR;
1145 } else if (attr == &dev_attr_fnode_redirect_ipaddr.attr) {
1146 param = ISCSI_FLASHNODE_REDIRECT_IPADDR;
1147 } else if (attr == &dev_attr_fnode_max_segment_size.attr) {
1148 param = ISCSI_FLASHNODE_MAX_SEGMENT_SIZE;
1149 } else if (attr == &dev_attr_fnode_local_port.attr) {
1150 param = ISCSI_FLASHNODE_LOCAL_PORT;
1151 } else if (attr == &dev_attr_fnode_ipv4_tos.attr) {
1152 param = ISCSI_FLASHNODE_IPV4_TOS;
1153 } else if (attr == &dev_attr_fnode_ipv6_traffic_class.attr) {
1154 param = ISCSI_FLASHNODE_IPV6_TC;
1155 } else if (attr == &dev_attr_fnode_ipv6_flow_label.attr) {
1156 param = ISCSI_FLASHNODE_IPV6_FLOW_LABEL;
1157 } else if (attr == &dev_attr_fnode_link_local_ipv6.attr) {
1158 param = ISCSI_FLASHNODE_LINK_LOCAL_IPV6;
1159 } else if (attr == &dev_attr_fnode_tcp_xmit_wsf.attr) {
1160 param = ISCSI_FLASHNODE_TCP_XMIT_WSF;
1161 } else if (attr == &dev_attr_fnode_tcp_recv_wsf.attr) {
1162 param = ISCSI_FLASHNODE_TCP_RECV_WSF;
1163 } else if (attr == &dev_attr_fnode_statsn.attr) {
1164 param = ISCSI_FLASHNODE_STATSN;
1165 } else if (attr == &dev_attr_fnode_exp_statsn.attr) {
1166 param = ISCSI_FLASHNODE_EXP_STATSN;
1167 } else {
1168 WARN_ONCE(1, "Invalid flashnode connection attr");
1169 return 0;
1170 }
1171
1172 return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
1173}
1174
1175static struct attribute_group iscsi_flashnode_conn_attr_group = {
1176 .attrs = iscsi_flashnode_conn_attrs,
1177 .is_visible = iscsi_flashnode_conn_attr_is_visible,
1178};
1179
1180static const struct attribute_group *iscsi_flashnode_conn_attr_groups[] = {
1181 &iscsi_flashnode_conn_attr_group,
1182 NULL,
1183};
1184
1185static void iscsi_flashnode_conn_release(struct device *dev)
1186{
1187 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
1188
1189 kfree(fnode_conn->ipaddress);
1190 kfree(fnode_conn->redirect_ipaddr);
1191 kfree(fnode_conn->link_local_ipv6_addr);
1192 kfree(fnode_conn);
1193}
1194
Bhumika Goyal7988faf2017-08-19 13:52:24 +05301195static const struct device_type iscsi_flashnode_conn_dev_type = {
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001196 .name = "iscsi_flashnode_conn_dev_type",
1197 .groups = iscsi_flashnode_conn_attr_groups,
1198 .release = iscsi_flashnode_conn_release,
1199};
1200
Bart Van Assche4c1340a2016-03-30 11:28:11 -07001201static struct bus_type iscsi_flashnode_bus;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001202
1203int iscsi_flashnode_bus_match(struct device *dev,
1204 struct device_driver *drv)
1205{
1206 if (dev->bus == &iscsi_flashnode_bus)
1207 return 1;
1208 return 0;
1209}
1210EXPORT_SYMBOL_GPL(iscsi_flashnode_bus_match);
1211
Bart Van Assche4c1340a2016-03-30 11:28:11 -07001212static struct bus_type iscsi_flashnode_bus = {
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001213 .name = "iscsi_flashnode",
1214 .match = &iscsi_flashnode_bus_match,
1215};
1216
1217/**
1218 * iscsi_create_flashnode_sess - Add flashnode session entry in sysfs
1219 * @shost: pointer to host data
1220 * @index: index of flashnode to add in sysfs
1221 * @transport: pointer to transport data
1222 * @dd_size: total size to allocate
1223 *
1224 * Adds a sysfs entry for the flashnode session attributes
1225 *
1226 * Returns:
Masanari Iidae2278672014-02-18 22:54:36 +09001227 * pointer to allocated flashnode sess on success
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001228 * %NULL on failure
1229 */
1230struct iscsi_bus_flash_session *
1231iscsi_create_flashnode_sess(struct Scsi_Host *shost, int index,
1232 struct iscsi_transport *transport,
1233 int dd_size)
1234{
1235 struct iscsi_bus_flash_session *fnode_sess;
1236 int err;
1237
1238 fnode_sess = kzalloc(sizeof(*fnode_sess) + dd_size, GFP_KERNEL);
1239 if (!fnode_sess)
1240 return NULL;
1241
1242 fnode_sess->transport = transport;
1243 fnode_sess->target_id = index;
1244 fnode_sess->dev.type = &iscsi_flashnode_sess_dev_type;
1245 fnode_sess->dev.bus = &iscsi_flashnode_bus;
1246 fnode_sess->dev.parent = &shost->shost_gendev;
1247 dev_set_name(&fnode_sess->dev, "flashnode_sess-%u:%u",
1248 shost->host_no, index);
1249
1250 err = device_register(&fnode_sess->dev);
1251 if (err)
1252 goto free_fnode_sess;
1253
1254 if (dd_size)
1255 fnode_sess->dd_data = &fnode_sess[1];
1256
1257 return fnode_sess;
1258
1259free_fnode_sess:
1260 kfree(fnode_sess);
1261 return NULL;
1262}
1263EXPORT_SYMBOL_GPL(iscsi_create_flashnode_sess);
1264
1265/**
1266 * iscsi_create_flashnode_conn - Add flashnode conn entry in sysfs
1267 * @shost: pointer to host data
1268 * @fnode_sess: pointer to the parent flashnode session entry
1269 * @transport: pointer to transport data
1270 * @dd_size: total size to allocate
1271 *
1272 * Adds a sysfs entry for the flashnode connection attributes
1273 *
1274 * Returns:
1275 * pointer to allocated flashnode conn on success
1276 * %NULL on failure
1277 */
1278struct iscsi_bus_flash_conn *
1279iscsi_create_flashnode_conn(struct Scsi_Host *shost,
1280 struct iscsi_bus_flash_session *fnode_sess,
1281 struct iscsi_transport *transport,
1282 int dd_size)
1283{
1284 struct iscsi_bus_flash_conn *fnode_conn;
1285 int err;
1286
1287 fnode_conn = kzalloc(sizeof(*fnode_conn) + dd_size, GFP_KERNEL);
1288 if (!fnode_conn)
1289 return NULL;
1290
1291 fnode_conn->transport = transport;
1292 fnode_conn->dev.type = &iscsi_flashnode_conn_dev_type;
1293 fnode_conn->dev.bus = &iscsi_flashnode_bus;
1294 fnode_conn->dev.parent = &fnode_sess->dev;
1295 dev_set_name(&fnode_conn->dev, "flashnode_conn-%u:%u:0",
1296 shost->host_no, fnode_sess->target_id);
1297
1298 err = device_register(&fnode_conn->dev);
1299 if (err)
1300 goto free_fnode_conn;
1301
1302 if (dd_size)
1303 fnode_conn->dd_data = &fnode_conn[1];
1304
1305 return fnode_conn;
1306
1307free_fnode_conn:
1308 kfree(fnode_conn);
1309 return NULL;
1310}
1311EXPORT_SYMBOL_GPL(iscsi_create_flashnode_conn);
1312
1313/**
1314 * iscsi_is_flashnode_conn_dev - verify passed device is to be flashnode conn
1315 * @dev: device to verify
1316 * @data: pointer to data containing value to use for verification
1317 *
1318 * Verifies if the passed device is flashnode conn device
1319 *
1320 * Returns:
1321 * 1 on success
1322 * 0 on failure
1323 */
Bart Van Assche7a15fdf2016-03-30 11:27:47 -07001324static int iscsi_is_flashnode_conn_dev(struct device *dev, void *data)
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001325{
1326 return dev->bus == &iscsi_flashnode_bus;
1327}
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001328
1329static int iscsi_destroy_flashnode_conn(struct iscsi_bus_flash_conn *fnode_conn)
1330{
1331 device_unregister(&fnode_conn->dev);
1332 return 0;
1333}
1334
1335static int flashnode_match_index(struct device *dev, void *data)
1336{
1337 struct iscsi_bus_flash_session *fnode_sess = NULL;
1338 int ret = 0;
1339
1340 if (!iscsi_flashnode_bus_match(dev, NULL))
1341 goto exit_match_index;
1342
1343 fnode_sess = iscsi_dev_to_flash_session(dev);
1344 ret = (fnode_sess->target_id == *((int *)data)) ? 1 : 0;
1345
1346exit_match_index:
1347 return ret;
1348}
1349
1350/**
1351 * iscsi_get_flashnode_by_index -finds flashnode session entry by index
1352 * @shost: pointer to host data
Mike Christie8526cb12013-05-06 12:06:56 -05001353 * @idx: index to match
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001354 *
1355 * Finds the flashnode session object for the passed index
1356 *
1357 * Returns:
1358 * pointer to found flashnode session object on success
1359 * %NULL on failure
1360 */
1361static struct iscsi_bus_flash_session *
Mike Christie8526cb12013-05-06 12:06:56 -05001362iscsi_get_flashnode_by_index(struct Scsi_Host *shost, uint32_t idx)
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001363{
1364 struct iscsi_bus_flash_session *fnode_sess = NULL;
1365 struct device *dev;
1366
Mike Christie8526cb12013-05-06 12:06:56 -05001367 dev = device_find_child(&shost->shost_gendev, &idx,
1368 flashnode_match_index);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001369 if (dev)
1370 fnode_sess = iscsi_dev_to_flash_session(dev);
1371
1372 return fnode_sess;
1373}
1374
1375/**
1376 * iscsi_find_flashnode_sess - finds flashnode session entry
1377 * @shost: pointer to host data
1378 * @data: pointer to data containing value to use for comparison
1379 * @fn: function pointer that does actual comparison
1380 *
1381 * Finds the flashnode session object comparing the data passed using logic
1382 * defined in passed function pointer
1383 *
1384 * Returns:
1385 * pointer to found flashnode session device object on success
1386 * %NULL on failure
1387 */
1388struct device *
1389iscsi_find_flashnode_sess(struct Scsi_Host *shost, void *data,
1390 int (*fn)(struct device *dev, void *data))
1391{
Mike Christie8526cb12013-05-06 12:06:56 -05001392 return device_find_child(&shost->shost_gendev, data, fn);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001393}
1394EXPORT_SYMBOL_GPL(iscsi_find_flashnode_sess);
1395
1396/**
1397 * iscsi_find_flashnode_conn - finds flashnode connection entry
1398 * @fnode_sess: pointer to parent flashnode session entry
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001399 *
1400 * Finds the flashnode connection object comparing the data passed using logic
1401 * defined in passed function pointer
1402 *
1403 * Returns:
1404 * pointer to found flashnode connection device object on success
1405 * %NULL on failure
1406 */
1407struct device *
Mike Christie8526cb12013-05-06 12:06:56 -05001408iscsi_find_flashnode_conn(struct iscsi_bus_flash_session *fnode_sess)
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001409{
Mike Christie8526cb12013-05-06 12:06:56 -05001410 return device_find_child(&fnode_sess->dev, NULL,
1411 iscsi_is_flashnode_conn_dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001412}
1413EXPORT_SYMBOL_GPL(iscsi_find_flashnode_conn);
1414
1415static int iscsi_iter_destroy_flashnode_conn_fn(struct device *dev, void *data)
1416{
1417 if (!iscsi_is_flashnode_conn_dev(dev, NULL))
1418 return 0;
1419
1420 return iscsi_destroy_flashnode_conn(iscsi_dev_to_flash_conn(dev));
1421}
1422
1423/**
Masanari Iidae2278672014-02-18 22:54:36 +09001424 * iscsi_destroy_flashnode_sess - destroy flashnode session entry
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001425 * @fnode_sess: pointer to flashnode session entry to be destroyed
1426 *
1427 * Deletes the flashnode session entry and all children flashnode connection
1428 * entries from sysfs
1429 */
1430void iscsi_destroy_flashnode_sess(struct iscsi_bus_flash_session *fnode_sess)
1431{
1432 int err;
1433
1434 err = device_for_each_child(&fnode_sess->dev, NULL,
1435 iscsi_iter_destroy_flashnode_conn_fn);
1436 if (err)
1437 pr_err("Could not delete all connections for %s. Error %d.\n",
1438 fnode_sess->dev.kobj.name, err);
1439
1440 device_unregister(&fnode_sess->dev);
1441}
1442EXPORT_SYMBOL_GPL(iscsi_destroy_flashnode_sess);
1443
1444static int iscsi_iter_destroy_flashnode_fn(struct device *dev, void *data)
1445{
1446 if (!iscsi_flashnode_bus_match(dev, NULL))
1447 return 0;
1448
1449 iscsi_destroy_flashnode_sess(iscsi_dev_to_flash_session(dev));
1450 return 0;
1451}
1452
1453/**
Masanari Iidae2278672014-02-18 22:54:36 +09001454 * iscsi_destroy_all_flashnode - destroy all flashnode session entries
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04001455 * @shost: pointer to host data
1456 *
1457 * Destroys all the flashnode session entries and all corresponding children
1458 * flashnode connection entries from sysfs
1459 */
1460void iscsi_destroy_all_flashnode(struct Scsi_Host *shost)
1461{
1462 device_for_each_child(&shost->shost_gendev, NULL,
1463 iscsi_iter_destroy_flashnode_fn);
1464}
1465EXPORT_SYMBOL_GPL(iscsi_destroy_all_flashnode);
1466
1467/*
Mike Christie90eeb012011-07-25 13:48:50 -05001468 * BSG support
1469 */
1470/**
1471 * iscsi_bsg_host_dispatch - Dispatch command to LLD.
1472 * @job: bsg job to be processed
1473 */
1474static int iscsi_bsg_host_dispatch(struct bsg_job *job)
1475{
1476 struct Scsi_Host *shost = iscsi_job_to_shost(job);
1477 struct iscsi_bsg_request *req = job->request;
1478 struct iscsi_bsg_reply *reply = job->reply;
1479 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1480 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
1481 int ret;
1482
1483 /* check if we have the msgcode value at least */
1484 if (job->request_len < sizeof(uint32_t)) {
1485 ret = -ENOMSG;
1486 goto fail_host_msg;
1487 }
1488
1489 /* Validate the host command */
1490 switch (req->msgcode) {
1491 case ISCSI_BSG_HST_VENDOR:
1492 cmdlen += sizeof(struct iscsi_bsg_host_vendor);
1493 if ((shost->hostt->vendor_id == 0L) ||
1494 (req->rqst_data.h_vendor.vendor_id !=
1495 shost->hostt->vendor_id)) {
1496 ret = -ESRCH;
1497 goto fail_host_msg;
1498 }
1499 break;
1500 default:
1501 ret = -EBADR;
1502 goto fail_host_msg;
1503 }
1504
1505 /* check if we really have all the request data needed */
1506 if (job->request_len < cmdlen) {
1507 ret = -ENOMSG;
1508 goto fail_host_msg;
1509 }
1510
1511 ret = i->iscsi_transport->bsg_request(job);
1512 if (!ret)
1513 return 0;
1514
1515fail_host_msg:
1516 /* return the errno failure code as the only status */
1517 BUG_ON(job->reply_len < sizeof(uint32_t));
1518 reply->reply_payload_rcv_len = 0;
1519 reply->result = ret;
1520 job->reply_len = sizeof(uint32_t);
1521 bsg_job_done(job, ret, 0);
1522 return 0;
1523}
1524
1525/**
1526 * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
1527 * @shost: shost for iscsi_host
Marcos Paulo de Souzaeae627e2011-11-02 11:17:17 -02001528 * @ihost: iscsi_cls_host adding the structures to
Mike Christie90eeb012011-07-25 13:48:50 -05001529 */
1530static int
1531iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
1532{
1533 struct device *dev = &shost->shost_gendev;
1534 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1535 struct request_queue *q;
1536 char bsg_name[20];
Mike Christie90eeb012011-07-25 13:48:50 -05001537
1538 if (!i->iscsi_transport->bsg_request)
1539 return -ENOTSUPP;
1540
1541 snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
Jens Axboeaae3b062018-10-26 11:26:25 -06001542 q = bsg_setup_queue(dev, bsg_name, iscsi_bsg_host_dispatch, NULL, 0);
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +03001543 if (IS_ERR(q)) {
Mike Christie90eeb012011-07-25 13:48:50 -05001544 shost_printk(KERN_ERR, shost, "bsg interface failed to "
1545 "initialize - no request queue\n");
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +03001546 return PTR_ERR(q);
Mike Christie90eeb012011-07-25 13:48:50 -05001547 }
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +03001548 __scsi_init_queue(shost, q);
Mike Christie90eeb012011-07-25 13:48:50 -05001549
1550 ihost->bsg_q = q;
1551 return 0;
1552}
1553
Mike Christie30a6c652006-04-06 21:13:39 -05001554static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +01001555 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -05001556{
1557 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001558 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -05001559
1560 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -06001561 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -05001562 mutex_init(&ihost->mutex);
Mike Christie90eeb012011-07-25 13:48:50 -05001563
1564 iscsi_bsg_host_add(shost, ihost);
1565 /* ignore any bsg add error - we just can't do sgio */
1566
1567 return 0;
1568}
1569
1570static int iscsi_remove_host(struct transport_container *tc,
1571 struct device *dev, struct device *cdev)
1572{
1573 struct Scsi_Host *shost = dev_to_shost(dev);
1574 struct iscsi_cls_host *ihost = shost->shost_data;
1575
Jens Axboe5e28b8d2018-10-26 11:27:02 -06001576 bsg_remove_queue(ihost->bsg_q);
Mike Christie30a6c652006-04-06 21:13:39 -05001577 return 0;
1578}
1579
1580static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
1581 "iscsi_host",
1582 iscsi_setup_host,
Mike Christie90eeb012011-07-25 13:48:50 -05001583 iscsi_remove_host,
Mike Christie30a6c652006-04-06 21:13:39 -05001584 NULL);
1585
Alex Aizman0896b752005-08-04 19:33:07 -07001586static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
1587 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 NULL,
1589 NULL,
1590 NULL);
1591
Alex Aizman0896b752005-08-04 19:33:07 -07001592static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
1593 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 NULL,
1595 NULL,
1596 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -07001597
1598static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +01001599static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001600
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04001601/*
1602 * conn_mutex protects the {start,bind,stop,destroy}_conn from racing
1603 * against the kernel stop_connection recovery mechanism
1604 */
1605static DEFINE_MUTEX(conn_mutex);
1606
Mike Christie7b7232f2006-02-01 21:06:49 -06001607static LIST_HEAD(sesslist);
1608static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -07001609static LIST_HEAD(connlist);
Bharath Ravi0ab71042020-01-25 01:19:25 -05001610static LIST_HEAD(connlist_err);
Alex Aizman0896b752005-08-04 19:33:07 -07001611static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Mike Christieb5c7a122006-04-06 21:13:33 -05001613static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
1614{
1615 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
1616 return sess->sid;
1617}
1618
1619/*
1620 * Returns the matching session to a given sid
1621 */
1622static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -06001623{
1624 unsigned long flags;
1625 struct iscsi_cls_session *sess;
1626
1627 spin_lock_irqsave(&sesslock, flags);
1628 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -05001629 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001630 spin_unlock_irqrestore(&sesslock, flags);
1631 return sess;
1632 }
1633 }
1634 spin_unlock_irqrestore(&sesslock, flags);
1635 return NULL;
1636}
1637
Mike Christieb5c7a122006-04-06 21:13:33 -05001638/*
1639 * Returns the matching connection to a given sid / cid tuple
1640 */
1641static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -06001642{
1643 unsigned long flags;
1644 struct iscsi_cls_conn *conn;
1645
1646 spin_lock_irqsave(&connlock, flags);
1647 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -05001648 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -06001649 spin_unlock_irqrestore(&connlock, flags);
1650 return conn;
1651 }
1652 }
1653 spin_unlock_irqrestore(&connlock, flags);
1654 return NULL;
1655}
1656
Mike Christie7b8631b2006-01-13 18:05:50 -06001657/*
1658 * The following functions can be used by LLDs that allocate
1659 * their own scsi_hosts or by software iscsi LLDs
1660 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001661static struct {
1662 int value;
1663 char *name;
1664} iscsi_session_state_names[] = {
1665 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
1666 { ISCSI_SESSION_FAILED, "FAILED" },
1667 { ISCSI_SESSION_FREE, "FREE" },
1668};
1669
Adrian Bunk3b0f2082008-02-13 23:30:17 +02001670static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -06001671{
1672 int i;
1673 char *name = NULL;
1674
1675 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
1676 if (iscsi_session_state_names[i].value == state) {
1677 name = iscsi_session_state_names[i].name;
1678 break;
1679 }
1680 }
1681 return name;
1682}
1683
1684int iscsi_session_chkready(struct iscsi_cls_session *session)
1685{
1686 unsigned long flags;
1687 int err;
1688
1689 spin_lock_irqsave(&session->lock, flags);
1690 switch (session->state) {
1691 case ISCSI_SESSION_LOGGED_IN:
1692 err = 0;
1693 break;
1694 case ISCSI_SESSION_FAILED:
Andrew Vasquez9a1a69a12009-04-29 13:12:39 -05001695 err = DID_IMM_RETRY << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -06001696 break;
1697 case ISCSI_SESSION_FREE:
Mike Christie56d7fcf2008-08-19 18:45:26 -05001698 err = DID_TRANSPORT_FAILFAST << 16;
Mike Christie6eabafb2008-01-31 13:36:43 -06001699 break;
1700 default:
1701 err = DID_NO_CONNECT << 16;
1702 break;
1703 }
1704 spin_unlock_irqrestore(&session->lock, flags);
1705 return err;
1706}
1707EXPORT_SYMBOL_GPL(iscsi_session_chkready);
1708
Manish Rangankar17fa5752011-07-25 13:48:52 -05001709int iscsi_is_session_online(struct iscsi_cls_session *session)
1710{
1711 unsigned long flags;
1712 int ret = 0;
1713
1714 spin_lock_irqsave(&session->lock, flags);
1715 if (session->state == ISCSI_SESSION_LOGGED_IN)
1716 ret = 1;
1717 spin_unlock_irqrestore(&session->lock, flags);
1718 return ret;
1719}
1720EXPORT_SYMBOL_GPL(iscsi_is_session_online);
1721
Mike Christie7b8631b2006-01-13 18:05:50 -06001722static void iscsi_session_release(struct device *dev)
1723{
1724 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06001725 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Mike Christie7b8631b2006-01-13 18:05:50 -06001727 shost = iscsi_session_to_shost(session);
1728 scsi_host_put(shost);
Mike Christie632248a2009-08-20 15:11:01 -05001729 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06001730 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -06001731}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Nilesh Javali6260a5d2012-02-27 03:08:51 -08001733int iscsi_is_session_dev(const struct device *dev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001734{
1735 return dev->release == iscsi_session_release;
1736}
Nilesh Javali6260a5d2012-02-27 03:08:51 -08001737EXPORT_SYMBOL_GPL(iscsi_is_session_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Mike Christiea4804cd2008-05-21 15:54:00 -05001739static int iscsi_iter_session_fn(struct device *dev, void *data)
1740{
1741 void (* fn) (struct iscsi_cls_session *) = data;
1742
1743 if (!iscsi_is_session_dev(dev))
1744 return 0;
1745 fn(iscsi_dev_to_session(dev));
1746 return 0;
1747}
1748
1749void iscsi_host_for_each_session(struct Scsi_Host *shost,
1750 void (*fn)(struct iscsi_cls_session *))
1751{
1752 device_for_each_child(&shost->shost_gendev, fn,
1753 iscsi_iter_session_fn);
1754}
1755EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
1756
Mike Christie8aae18a2008-01-31 13:36:48 -06001757/**
1758 * iscsi_scan_finished - helper to report when running scans are done
1759 * @shost: scsi host
1760 * @time: scan run time
1761 *
1762 * This function can be used by drives like qla4xxx to report to the scsi
1763 * layer when the scans it kicked off at module load time are done.
1764 */
1765int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
1766{
Mike Christie32c6e1b2008-05-21 15:53:58 -05001767 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -06001768 /*
1769 * qla4xxx will have kicked off some session unblocks before calling
1770 * scsi_scan_host, so just wait for them to complete.
1771 */
1772 return !atomic_read(&ihost->nr_scans);
1773}
1774EXPORT_SYMBOL_GPL(iscsi_scan_finished);
1775
Mike Christie79706342008-05-21 15:54:12 -05001776struct iscsi_scan_data {
1777 unsigned int channel;
1778 unsigned int id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001779 u64 lun;
Hannes Reinecke1d645082016-03-17 08:39:45 +01001780 enum scsi_scan_mode rescan;
Mike Christie79706342008-05-21 15:54:12 -05001781};
1782
1783static int iscsi_user_scan_session(struct device *dev, void *data)
1784{
1785 struct iscsi_scan_data *scan_data = data;
1786 struct iscsi_cls_session *session;
1787 struct Scsi_Host *shost;
1788 struct iscsi_cls_host *ihost;
1789 unsigned long flags;
1790 unsigned int id;
1791
1792 if (!iscsi_is_session_dev(dev))
1793 return 0;
1794
1795 session = iscsi_dev_to_session(dev);
Mike Christie632248a2009-08-20 15:11:01 -05001796
1797 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
1798
Mike Christie79706342008-05-21 15:54:12 -05001799 shost = iscsi_session_to_shost(session);
1800 ihost = shost->shost_data;
1801
1802 mutex_lock(&ihost->mutex);
1803 spin_lock_irqsave(&session->lock, flags);
1804 if (session->state != ISCSI_SESSION_LOGGED_IN) {
1805 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05001806 goto user_scan_exit;
Mike Christie79706342008-05-21 15:54:12 -05001807 }
1808 id = session->target_id;
1809 spin_unlock_irqrestore(&session->lock, flags);
1810
1811 if (id != ISCSI_MAX_TARGET) {
1812 if ((scan_data->channel == SCAN_WILD_CARD ||
1813 scan_data->channel == 0) &&
1814 (scan_data->id == SCAN_WILD_CARD ||
1815 scan_data->id == id))
1816 scsi_scan_target(&session->dev, 0, id,
Hannes Reinecke1d645082016-03-17 08:39:45 +01001817 scan_data->lun, scan_data->rescan);
Mike Christie79706342008-05-21 15:54:12 -05001818 }
Mike Christie632248a2009-08-20 15:11:01 -05001819
1820user_scan_exit:
Mike Christie79706342008-05-21 15:54:12 -05001821 mutex_unlock(&ihost->mutex);
Mike Christie632248a2009-08-20 15:11:01 -05001822 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
Mike Christie79706342008-05-21 15:54:12 -05001823 return 0;
1824}
1825
Mike Christie30a6c652006-04-06 21:13:39 -05001826static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001827 uint id, u64 lun)
Mike Christie30a6c652006-04-06 21:13:39 -05001828{
Mike Christie79706342008-05-21 15:54:12 -05001829 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -05001830
Mike Christie79706342008-05-21 15:54:12 -05001831 scan_data.channel = channel;
1832 scan_data.id = id;
1833 scan_data.lun = lun;
Hannes Reinecke1d645082016-03-17 08:39:45 +01001834 scan_data.rescan = SCSI_SCAN_MANUAL;
Mike Christie30a6c652006-04-06 21:13:39 -05001835
Mike Christie79706342008-05-21 15:54:12 -05001836 return device_for_each_child(&shost->shost_gendev, &scan_data,
1837 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -05001838}
1839
Mike Christiebd976f62008-01-31 13:36:46 -06001840static void iscsi_scan_session(struct work_struct *work)
1841{
1842 struct iscsi_cls_session *session =
1843 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -06001844 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001845 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -05001846 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -06001847
Mike Christie79706342008-05-21 15:54:12 -05001848 scan_data.channel = 0;
1849 scan_data.id = SCAN_WILD_CARD;
1850 scan_data.lun = SCAN_WILD_CARD;
Hannes Reinecke1d645082016-03-17 08:39:45 +01001851 scan_data.rescan = SCSI_SCAN_RESCAN;
Mike Christiebd976f62008-01-31 13:36:46 -06001852
Mike Christie79706342008-05-21 15:54:12 -05001853 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -06001854 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -06001855}
1856
Mike Christiec01be6d2010-07-22 16:59:49 +05301857/**
1858 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
Randy Dunlape6d4ef42010-08-14 13:05:41 -07001859 * @cmd: scsi cmd passed to scsi eh handler
Mike Christiec01be6d2010-07-22 16:59:49 +05301860 *
1861 * If the session is down this function will wait for the recovery
1862 * timer to fire or for the session to be logged back in. If the
1863 * recovery timer fires then FAST_IO_FAIL is returned. The caller
1864 * should pass this error value to the scsi eh.
1865 */
1866int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
1867{
1868 struct iscsi_cls_session *session =
1869 starget_to_session(scsi_target(cmd->device));
1870 unsigned long flags;
1871 int ret = 0;
1872
1873 spin_lock_irqsave(&session->lock, flags);
1874 while (session->state != ISCSI_SESSION_LOGGED_IN) {
1875 if (session->state == ISCSI_SESSION_FREE) {
1876 ret = FAST_IO_FAIL;
1877 break;
1878 }
1879 spin_unlock_irqrestore(&session->lock, flags);
1880 msleep(1000);
1881 spin_lock_irqsave(&session->lock, flags);
1882 }
1883 spin_unlock_irqrestore(&session->lock, flags);
1884 return ret;
1885}
1886EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
1887
David Howellsc4028952006-11-22 14:57:56 +00001888static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -05001889{
David Howellsc4028952006-11-22 14:57:56 +00001890 struct iscsi_cls_session *session =
1891 container_of(work, struct iscsi_cls_session,
1892 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -06001893 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -05001894
Mike Christie322d7392008-01-31 13:36:52 -06001895 iscsi_cls_session_printk(KERN_INFO, session,
1896 "session recovery timed out after %d secs\n",
1897 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -05001898
Mike Christie6eabafb2008-01-31 13:36:43 -06001899 spin_lock_irqsave(&session->lock, flags);
1900 switch (session->state) {
1901 case ISCSI_SESSION_FAILED:
1902 session->state = ISCSI_SESSION_FREE;
1903 break;
1904 case ISCSI_SESSION_LOGGED_IN:
1905 case ISCSI_SESSION_FREE:
1906 /* we raced with the unblock's flush */
1907 spin_unlock_irqrestore(&session->lock, flags);
1908 return;
1909 }
1910 spin_unlock_irqrestore(&session->lock, flags);
1911
Mike Christie632248a2009-08-20 15:11:01 -05001912 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
Mike Christie5d9fb5c2012-05-17 23:56:57 -05001913 scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
Mike Christie632248a2009-08-20 15:11:01 -05001914 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
Mike Christief0c9f492021-11-05 17:10:47 -05001915
1916 if (session->transport->session_recovery_timedout)
1917 session->transport->session_recovery_timedout(session);
Mike Christie30a6c652006-04-06 21:13:39 -05001918}
1919
Mike Christie45ab33b2008-03-04 13:26:55 -06001920static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -05001921{
Mike Christie45ab33b2008-03-04 13:26:55 -06001922 struct iscsi_cls_session *session =
1923 container_of(work, struct iscsi_cls_session,
1924 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -06001925 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05001926 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -06001927 unsigned long flags;
1928
Mike Christie632248a2009-08-20 15:11:01 -05001929 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
Mike Christie45ab33b2008-03-04 13:26:55 -06001930 /*
1931 * The recovery and unblock work get run from the same workqueue,
1932 * so try to cancel it if it was going to run after this unblock.
1933 */
1934 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -06001935 spin_lock_irqsave(&session->lock, flags);
1936 session->state = ISCSI_SESSION_LOGGED_IN;
1937 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -06001938 /* start IO */
Mike Christie5d9fb5c2012-05-17 23:56:57 -05001939 scsi_target_unblock(&session->dev, SDEV_RUNNING);
Mike Christie8aae18a2008-01-31 13:36:48 -06001940 /*
1941 * Only do kernel scanning if the driver is properly hooked into
1942 * the async scanning code (drivers like iscsi_tcp do login and
1943 * scanning from userspace).
1944 */
1945 if (shost->hostt->scan_finished) {
Mike Christie06d25af2009-03-05 14:46:02 -06001946 if (scsi_queue_work(shost, &session->scan_work))
Mike Christie8aae18a2008-01-31 13:36:48 -06001947 atomic_inc(&ihost->nr_scans);
1948 }
Mike Christie632248a2009-08-20 15:11:01 -05001949 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -06001950}
Mike Christie45ab33b2008-03-04 13:26:55 -06001951
1952/**
1953 * iscsi_unblock_session - set a session as logged in and start IO.
1954 * @session: iscsi session
1955 *
1956 * Mark a session as ready to accept IO.
1957 */
1958void iscsi_unblock_session(struct iscsi_cls_session *session)
1959{
Mike Christie68f2f832021-05-25 13:18:09 -05001960 flush_work(&session->block_work);
1961
Mike Christie45ab33b2008-03-04 13:26:55 -06001962 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
1963 /*
Mike Christie1d726aa2020-07-01 14:47:47 -05001964 * Blocking the session can be done from any context so we only
1965 * queue the block work. Make sure the unblock work has completed
1966 * because it flushes/cancels the other works and updates the state.
Mike Christie45ab33b2008-03-04 13:26:55 -06001967 */
Mike Christie1d726aa2020-07-01 14:47:47 -05001968 flush_work(&session->unblock_work);
Mike Christie45ab33b2008-03-04 13:26:55 -06001969}
Mike Christie30a6c652006-04-06 21:13:39 -05001970EXPORT_SYMBOL_GPL(iscsi_unblock_session);
1971
Mike Christie45ab33b2008-03-04 13:26:55 -06001972static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -05001973{
Mike Christie45ab33b2008-03-04 13:26:55 -06001974 struct iscsi_cls_session *session =
1975 container_of(work, struct iscsi_cls_session,
1976 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -06001977 unsigned long flags;
1978
Mike Christie632248a2009-08-20 15:11:01 -05001979 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
Mike Christie6eabafb2008-01-31 13:36:43 -06001980 spin_lock_irqsave(&session->lock, flags);
1981 session->state = ISCSI_SESSION_FAILED;
1982 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -05001983 scsi_target_block(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05001984 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
Mike Christiefdd46dc2009-11-11 16:34:34 -06001985 if (session->recovery_tmo >= 0)
1986 queue_delayed_work(iscsi_eh_timer_workq,
1987 &session->recovery_work,
1988 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -05001989}
Mike Christie45ab33b2008-03-04 13:26:55 -06001990
1991void iscsi_block_session(struct iscsi_cls_session *session)
1992{
1993 queue_work(iscsi_eh_timer_workq, &session->block_work);
1994}
Mike Christie30a6c652006-04-06 21:13:39 -05001995EXPORT_SYMBOL_GPL(iscsi_block_session);
1996
Mike Christie26974782007-12-13 12:43:29 -06001997static void __iscsi_unbind_session(struct work_struct *work)
1998{
1999 struct iscsi_cls_session *session =
2000 container_of(work, struct iscsi_cls_session,
2001 unbind_work);
2002 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -05002003 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -05002004 unsigned long flags;
Mike Christie8d4a6902011-10-06 03:56:57 -05002005 unsigned int target_id;
Mike Christie26974782007-12-13 12:43:29 -06002006
Mike Christie632248a2009-08-20 15:11:01 -05002007 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
2008
Mike Christie26974782007-12-13 12:43:29 -06002009 /* Prevent new scans and make sure scanning is not in progress */
2010 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -05002011 spin_lock_irqsave(&session->lock, flags);
2012 if (session->target_id == ISCSI_MAX_TARGET) {
2013 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -06002014 mutex_unlock(&ihost->mutex);
Wu Bo13e60d32020-03-24 15:58:50 +08002015 goto unbind_session_exit;
Mike Christie26974782007-12-13 12:43:29 -06002016 }
Mike Christie8d4a6902011-10-06 03:56:57 -05002017
2018 target_id = session->target_id;
Mike Christie79706342008-05-21 15:54:12 -05002019 session->target_id = ISCSI_MAX_TARGET;
2020 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -06002021 mutex_unlock(&ihost->mutex);
2022
Mike Christiee463f962020-07-01 14:47:46 -05002023 scsi_remove_target(&session->dev);
2024
Mike Christie8d4a6902011-10-06 03:56:57 -05002025 if (session->ida_used)
2026 ida_simple_remove(&iscsi_sess_ida, target_id);
2027
Wu Bo13e60d32020-03-24 15:58:50 +08002028unbind_session_exit:
Mike Christie26974782007-12-13 12:43:29 -06002029 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -05002030 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
Mike Christie26974782007-12-13 12:43:29 -06002031}
2032
Frank Mayharcc6b32e2020-02-27 14:59:45 -05002033static void __iscsi_destroy_session(struct work_struct *work)
2034{
2035 struct iscsi_cls_session *session =
2036 container_of(work, struct iscsi_cls_session, destroy_work);
2037
2038 session->transport->destroy_session(session);
2039}
2040
Mike Christie7b8631b2006-01-13 18:05:50 -06002041struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -05002042iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
2043 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06002044{
2045 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Mike Christie5d91e202008-05-21 15:54:01 -05002047 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -05002048 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06002049 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -05002050 return NULL;
2051
Mike Christie7b8631b2006-01-13 18:05:50 -06002052 session->transport = transport;
Mike Christie0c70d842011-12-05 16:44:01 -06002053 session->creator = -1;
Mike Christie30a6c652006-04-06 21:13:39 -05002054 session->recovery_tmo = 120;
Chris Leech9c8108a2015-06-16 16:07:13 -07002055 session->recovery_tmo_sysfs_override = false;
Mike Christie6eabafb2008-01-31 13:36:43 -06002056 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +00002057 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -05002058 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -06002059 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
2060 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -06002061 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -06002062 INIT_WORK(&session->scan_work, iscsi_scan_session);
Frank Mayharcc6b32e2020-02-27 14:59:45 -05002063 INIT_WORK(&session->destroy_work, __iscsi_destroy_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002064 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -06002065
Mike Christie6a8a0d32006-06-28 12:00:31 -05002066 /* this is released in the dev's release function */
2067 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -05002068 session->dev.parent = &shost->shost_gendev;
2069 session->dev.release = iscsi_session_release;
2070 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -05002071 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -05002072 session->dd_data = &session[1];
Mike Christie632248a2009-08-20 15:11:01 -05002073
2074 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
Mike Christie8434aa82006-06-28 12:00:30 -05002075 return session;
2076}
2077EXPORT_SYMBOL_GPL(iscsi_alloc_session);
2078
Mike Christie6a8a0d32006-06-28 12:00:31 -05002079int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05002080{
Mike Christie26974782007-12-13 12:43:29 -06002081 unsigned long flags;
Mike Christie8d4a6902011-10-06 03:56:57 -05002082 int id = 0;
Mike Christie8434aa82006-06-28 12:00:30 -05002083 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -05002084
Mike Christie41be1442007-02-28 17:32:18 -06002085 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -05002086
Mike Christie8d4a6902011-10-06 03:56:57 -05002087 if (target_id == ISCSI_MAX_TARGET) {
2088 id = ida_simple_get(&iscsi_sess_ida, 0, 0, GFP_KERNEL);
Mike Christie79706342008-05-21 15:54:12 -05002089
Mike Christie8d4a6902011-10-06 03:56:57 -05002090 if (id < 0) {
Mike Christie79706342008-05-21 15:54:12 -05002091 iscsi_cls_session_printk(KERN_ERR, session,
Mike Christie8d4a6902011-10-06 03:56:57 -05002092 "Failure in Target ID Allocation\n");
2093 return id;
Mike Christie79706342008-05-21 15:54:12 -05002094 }
Mike Christie8d4a6902011-10-06 03:56:57 -05002095 session->target_id = (unsigned int)id;
2096 session->ida_used = true;
2097 } else
2098 session->target_id = target_id;
Mike Christie30a6c652006-04-06 21:13:39 -05002099
Kay Sievers71610f52008-12-03 22:41:36 +01002100 dev_set_name(&session->dev, "session%u", session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -05002101 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06002102 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06002103 iscsi_cls_session_printk(KERN_ERR, session,
2104 "could not register session's dev\n");
Mike Christie8d4a6902011-10-06 03:56:57 -05002105 goto release_ida;
Mike Christie7b8631b2006-01-13 18:05:50 -06002106 }
Gabriel Krisman Bertazif3c893e2020-01-06 13:58:17 -05002107 err = transport_register_device(&session->dev);
2108 if (err) {
2109 iscsi_cls_session_printk(KERN_ERR, session,
2110 "could not register transport's dev\n");
2111 goto release_dev;
2112 }
Mike Christie7b8631b2006-01-13 18:05:50 -06002113
Mike Christie26974782007-12-13 12:43:29 -06002114 spin_lock_irqsave(&sesslock, flags);
2115 list_add(&session->sess_list, &sesslist);
2116 spin_unlock_irqrestore(&sesslock, flags);
2117
Mike Christie26974782007-12-13 12:43:29 -06002118 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie632248a2009-08-20 15:11:01 -05002119 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
Mike Christie8434aa82006-06-28 12:00:30 -05002120 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -05002121
Gabriel Krisman Bertazif3c893e2020-01-06 13:58:17 -05002122release_dev:
2123 device_del(&session->dev);
Mike Christie8d4a6902011-10-06 03:56:57 -05002124release_ida:
2125 if (session->ida_used)
2126 ida_simple_remove(&iscsi_sess_ida, session->target_id);
2127
Mike Christie8434aa82006-06-28 12:00:30 -05002128 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -06002129}
Mike Christie8434aa82006-06-28 12:00:30 -05002130EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -06002131
2132/**
Mike Christie8434aa82006-06-28 12:00:30 -05002133 * iscsi_create_session - create iscsi class session
2134 * @shost: scsi host
2135 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -05002136 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -05002137 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -06002138 *
Mike Christie8434aa82006-06-28 12:00:30 -05002139 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05002140 */
Mike Christie8434aa82006-06-28 12:00:30 -05002141struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -05002142iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
2143 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -05002144{
2145 struct iscsi_cls_session *session;
2146
Mike Christie5d91e202008-05-21 15:54:01 -05002147 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -05002148 if (!session)
2149 return NULL;
2150
Mike Christie6a8a0d32006-06-28 12:00:31 -05002151 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -05002152 iscsi_free_session(session);
2153 return NULL;
2154 }
2155 return session;
2156}
2157EXPORT_SYMBOL_GPL(iscsi_create_session);
2158
Mike Christie26974782007-12-13 12:43:29 -06002159static void iscsi_conn_release(struct device *dev)
2160{
2161 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
2162 struct device *parent = conn->dev.parent;
2163
Mike Christie632248a2009-08-20 15:11:01 -05002164 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
Mike Christie26974782007-12-13 12:43:29 -06002165 kfree(conn);
2166 put_device(parent);
2167}
2168
2169static int iscsi_is_conn_dev(const struct device *dev)
2170{
2171 return dev->release == iscsi_conn_release;
2172}
2173
2174static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
2175{
2176 if (!iscsi_is_conn_dev(dev))
2177 return 0;
2178 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
2179}
2180
Mike Christie8434aa82006-06-28 12:00:30 -05002181void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -06002182{
Mike Christie26974782007-12-13 12:43:29 -06002183 unsigned long flags;
2184 int err;
Mike Christie30a6c652006-04-06 21:13:39 -05002185
Mike Christie632248a2009-08-20 15:11:01 -05002186 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
2187
Mike Christie26974782007-12-13 12:43:29 -06002188 spin_lock_irqsave(&sesslock, flags);
Mike Christie93bf02e2020-07-01 14:47:48 -05002189 if (!list_empty(&session->sess_list))
2190 list_del(&session->sess_list);
Mike Christie26974782007-12-13 12:43:29 -06002191 spin_unlock_irqrestore(&sesslock, flags);
2192
Mike Christie1d726aa2020-07-01 14:47:47 -05002193 flush_work(&session->block_work);
2194 flush_work(&session->unblock_work);
2195 cancel_delayed_work_sync(&session->recovery_work);
Mike Christie26974782007-12-13 12:43:29 -06002196 /*
2197 * If we are blocked let commands flow again. The lld or iscsi
2198 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -06002199 * We assume that LLD will not be calling block/unblock while
2200 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -06002201 */
Mike Christie6eabafb2008-01-31 13:36:43 -06002202 spin_lock_irqsave(&session->lock, flags);
2203 session->state = ISCSI_SESSION_FREE;
2204 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -06002205
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002206 scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
Mike Christie45ab33b2008-03-04 13:26:55 -06002207 /* flush running scans then delete devices */
Jitendra Bhivare90e96312017-03-24 14:11:43 +05302208 flush_work(&session->scan_work);
Maurizio Lombardi165aa2b2019-01-28 15:24:42 +01002209 /* flush running unbind operations */
2210 flush_work(&session->unbind_work);
Mike Christie45ab33b2008-03-04 13:26:55 -06002211 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -05002212
Mike Christie26974782007-12-13 12:43:29 -06002213 /* hw iscsi may not have removed all connections from session */
2214 err = device_for_each_child(&session->dev, NULL,
2215 iscsi_iter_destroy_conn_fn);
2216 if (err)
Mike Christie322d7392008-01-31 13:36:52 -06002217 iscsi_cls_session_printk(KERN_ERR, session,
2218 "Could not delete all connections "
2219 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -05002220
Mike Christie7b8631b2006-01-13 18:05:50 -06002221 transport_unregister_device(&session->dev);
Mike Christie632248a2009-08-20 15:11:01 -05002222
2223 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
Mike Christie8434aa82006-06-28 12:00:30 -05002224 device_del(&session->dev);
2225}
2226EXPORT_SYMBOL_GPL(iscsi_remove_session);
2227
2228void iscsi_free_session(struct iscsi_cls_session *session)
2229{
Mike Christie632248a2009-08-20 15:11:01 -05002230 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
Mike Christie26974782007-12-13 12:43:29 -06002231 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -05002232 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -06002233}
Mike Christie8434aa82006-06-28 12:00:30 -05002234EXPORT_SYMBOL_GPL(iscsi_free_session);
2235
2236/**
Mike Christie7b8631b2006-01-13 18:05:50 -06002237 * iscsi_create_conn - create iscsi class connection
2238 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -05002239 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -06002240 * @cid: connection id
2241 *
2242 * This can be called from a LLD or iscsi_transport. The connection
2243 * is child of the session so cid must be unique for all connections
2244 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -05002245 *
2246 * Since we do not support MCS, cid will normally be zero. In some cases
2247 * for software iscsi we could be trying to preallocate a connection struct
2248 * in which case there could be two connection structs and cid would be
2249 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -05002250 */
Mike Christie7b8631b2006-01-13 18:05:50 -06002251struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002252iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -06002253{
2254 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -06002255 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -06002256 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -06002257 int err;
2258
Mike Christie5d91e202008-05-21 15:54:01 -05002259 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -06002260 if (!conn)
2261 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -05002262 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -06002263 conn->dd_data = &conn[1];
2264
Mike Christie22a39fb2011-02-16 15:04:33 -06002265 mutex_init(&conn->ep_mutex);
Mike Christie7b8631b2006-01-13 18:05:50 -06002266 INIT_LIST_HEAD(&conn->conn_list);
Bharath Ravi0ab71042020-01-25 01:19:25 -05002267 INIT_LIST_HEAD(&conn->conn_list_err);
Mike Christie7b8631b2006-01-13 18:05:50 -06002268 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -05002269 conn->cid = cid;
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04002270 conn->state = ISCSI_CONN_DOWN;
Mike Christie7b8631b2006-01-13 18:05:50 -06002271
2272 /* this is released in the dev's release function */
2273 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -04002274 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -05002275
Kay Sievers71610f52008-12-03 22:41:36 +01002276 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06002277 conn->dev.parent = &session->dev;
2278 conn->dev.release = iscsi_conn_release;
2279 err = device_register(&conn->dev);
2280 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -06002281 iscsi_cls_session_printk(KERN_ERR, session, "could not "
2282 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002283 goto release_parent_ref;
2284 }
Gabriel Krisman Bertazif3c893e2020-01-06 13:58:17 -05002285 err = transport_register_device(&conn->dev);
2286 if (err) {
2287 iscsi_cls_session_printk(KERN_ERR, session, "could not "
2288 "register transport's dev\n");
2289 goto release_conn_ref;
2290 }
Mike Christie26974782007-12-13 12:43:29 -06002291
2292 spin_lock_irqsave(&connlock, flags);
2293 list_add(&conn->conn_list, &connlist);
Mike Christie26974782007-12-13 12:43:29 -06002294 spin_unlock_irqrestore(&connlock, flags);
Mike Christie632248a2009-08-20 15:11:01 -05002295
2296 ISCSI_DBG_TRANS_CONN(conn, "Completed conn creation\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002297 return conn;
2298
Gabriel Krisman Bertazif3c893e2020-01-06 13:58:17 -05002299release_conn_ref:
Qinglang Miaodcda6622020-11-20 15:48:52 +08002300 device_unregister(&conn->dev);
2301 put_device(&session->dev);
2302 return NULL;
Mike Christie7b8631b2006-01-13 18:05:50 -06002303release_parent_ref:
2304 put_device(&session->dev);
2305free_conn:
2306 kfree(conn);
2307 return NULL;
2308}
2309
2310EXPORT_SYMBOL_GPL(iscsi_create_conn);
2311
2312/**
2313 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -05002314 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -06002315 *
Mike Christie26974782007-12-13 12:43:29 -06002316 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -05002317 */
Mike Christie7b8631b2006-01-13 18:05:50 -06002318int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
2319{
Mike Christie26974782007-12-13 12:43:29 -06002320 unsigned long flags;
2321
2322 spin_lock_irqsave(&connlock, flags);
Mike Christie26974782007-12-13 12:43:29 -06002323 list_del(&conn->conn_list);
Bharath Ravi0ab71042020-01-25 01:19:25 -05002324 list_del(&conn->conn_list_err);
Mike Christie26974782007-12-13 12:43:29 -06002325 spin_unlock_irqrestore(&connlock, flags);
2326
Mike Christie7b8631b2006-01-13 18:05:50 -06002327 transport_unregister_device(&conn->dev);
Mike Christie632248a2009-08-20 15:11:01 -05002328 ISCSI_DBG_TRANS_CONN(conn, "Completing conn destruction\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002329 device_unregister(&conn->dev);
2330 return 0;
2331}
Mike Christie7b8631b2006-01-13 18:05:50 -06002332EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
2333
Mike Christie21962a52021-05-25 13:18:03 -05002334void iscsi_put_conn(struct iscsi_cls_conn *conn)
2335{
2336 put_device(&conn->dev);
2337}
2338EXPORT_SYMBOL_GPL(iscsi_put_conn);
2339
2340void iscsi_get_conn(struct iscsi_cls_conn *conn)
2341{
2342 get_device(&conn->dev);
2343}
2344EXPORT_SYMBOL_GPL(iscsi_get_conn);
2345
Mike Christie7b8631b2006-01-13 18:05:50 -06002346/*
Mike Christie7b8631b2006-01-13 18:05:50 -06002347 * iscsi interface functions
2348 */
Alex Aizman0896b752005-08-04 19:33:07 -07002349static struct iscsi_internal *
2350iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Alex Aizman0896b752005-08-04 19:33:07 -07002352 struct iscsi_internal *priv;
2353 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Alex Aizman0896b752005-08-04 19:33:07 -07002355 spin_lock_irqsave(&iscsi_transport_lock, flags);
2356 list_for_each_entry(priv, &iscsi_transports, list) {
2357 if (tt == priv->iscsi_transport) {
2358 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2359 return priv;
2360 }
2361 }
2362 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2363 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Alex Aizman0896b752005-08-04 19:33:07 -07002366static int
Michael Chan43514772009-06-08 18:14:41 -07002367iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -05002368{
Michael Chan43514772009-06-08 18:14:41 -07002369 return nlmsg_multicast(nls, skb, 0, group, gfp);
Alex Aizman0896b752005-08-04 19:33:07 -07002370}
2371
Chris Leechaf170922018-04-09 15:15:28 -07002372static int
2373iscsi_unicast_skb(struct sk_buff *skb, u32 portid)
2374{
2375 return nlmsg_unicast(nls, skb, portid);
2376}
2377
Mike Christie7b7232f2006-02-01 21:06:49 -06002378int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -07002379 char *data, uint32_t data_size)
2380{
2381 struct nlmsghdr *nlh;
2382 struct sk_buff *skb;
2383 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -07002384 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -05002385 struct iscsi_internal *priv;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002386 int len = nlmsg_total_size(sizeof(*ev) + sizeof(struct iscsi_hdr) +
2387 data_size);
Alex Aizman0896b752005-08-04 19:33:07 -07002388
Mike Christie790f39a2006-05-18 20:31:39 -05002389 priv = iscsi_if_transport_lookup(conn->transport);
2390 if (!priv)
2391 return -EINVAL;
2392
Mike Christie43a145a2006-10-16 18:09:38 -04002393 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002394 if (!skb) {
Mike Christiee5bd7b52008-09-24 11:46:10 -05002395 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -06002396 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
2397 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07002398 return -ENOMEM;
2399 }
2400
Michael Chan43514772009-06-08 18:14:41 -07002401 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002402 ev = nlmsg_data(nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07002403 memset(ev, 0, sizeof(*ev));
2404 ev->transport_handle = iscsi_handle(conn->transport);
2405 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -05002406 ev->r.recv_req.cid = conn->cid;
2407 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07002408 pdu = (char*)ev + sizeof(*ev);
2409 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
2410 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
2411
Michael Chan43514772009-06-08 18:14:41 -07002412 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002413}
2414EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
2415
Michael Chan43514772009-06-08 18:14:41 -07002416int iscsi_offload_mesg(struct Scsi_Host *shost,
2417 struct iscsi_transport *transport, uint32_t type,
2418 char *data, uint16_t data_size)
2419{
2420 struct nlmsghdr *nlh;
2421 struct sk_buff *skb;
2422 struct iscsi_uevent *ev;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002423 int len = nlmsg_total_size(sizeof(*ev) + data_size);
Michael Chan43514772009-06-08 18:14:41 -07002424
Michael Chana541f842009-07-29 08:49:52 +00002425 skb = alloc_skb(len, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07002426 if (!skb) {
2427 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
2428 return -ENOMEM;
2429 }
2430
2431 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002432 ev = nlmsg_data(nlh);
Michael Chan43514772009-06-08 18:14:41 -07002433 memset(ev, 0, sizeof(*ev));
2434 ev->type = type;
2435 ev->transport_handle = iscsi_handle(transport);
2436 switch (type) {
2437 case ISCSI_KEVENT_PATH_REQ:
2438 ev->r.req_path.host_no = shost->host_no;
2439 break;
2440 case ISCSI_KEVENT_IF_DOWN:
2441 ev->r.notify_if_down.host_no = shost->host_no;
2442 break;
2443 }
2444
2445 memcpy((char *)ev + sizeof(*ev), data, data_size);
2446
Michael Chana541f842009-07-29 08:49:52 +00002447 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
Michael Chan43514772009-06-08 18:14:41 -07002448}
2449EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
2450
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04002451/*
2452 * This can be called without the rx_queue_mutex, if invoked by the kernel
2453 * stop work. But, in that case, it is guaranteed not to race with
2454 * iscsi_destroy by conn_mutex.
2455 */
2456static void iscsi_if_stop_conn(struct iscsi_cls_conn *conn, int flag)
2457{
2458 /*
2459 * It is important that this path doesn't rely on
2460 * rx_queue_mutex, otherwise, a thread doing allocation on a
2461 * start_session/start_connection could sleep waiting on a
2462 * writeback to a failed iscsi device, that cannot be recovered
2463 * because the lock is held. If we don't hold it here, the
2464 * kernel stop_conn_work_fn has a chance to stop the broken
2465 * session and resolve the allocation.
2466 *
2467 * Still, the user invoked .stop_conn() needs to be serialized
2468 * with stop_conn_work_fn by a private mutex. Not pretty, but
2469 * it works.
2470 */
2471 mutex_lock(&conn_mutex);
Mike Christie4ad382b2021-07-13 17:18:37 +08002472 switch (flag) {
2473 case STOP_CONN_RECOVER:
2474 conn->state = ISCSI_CONN_FAILED;
2475 break;
2476 case STOP_CONN_TERM:
2477 conn->state = ISCSI_CONN_DOWN;
2478 break;
2479 default:
2480 iscsi_cls_conn_printk(KERN_ERR, conn,
2481 "invalid stop flag %d\n", flag);
2482 goto unlock;
2483 }
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04002484
Mike Christie4ad382b2021-07-13 17:18:37 +08002485 conn->transport->stop_conn(conn, flag);
2486unlock:
2487 mutex_unlock(&conn_mutex);
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04002488}
2489
Bharath Ravi0ab71042020-01-25 01:19:25 -05002490static void stop_conn_work_fn(struct work_struct *work)
2491{
2492 struct iscsi_cls_conn *conn, *tmp;
2493 unsigned long flags;
2494 LIST_HEAD(recovery_list);
2495
2496 spin_lock_irqsave(&connlock, flags);
2497 if (list_empty(&connlist_err)) {
2498 spin_unlock_irqrestore(&connlock, flags);
2499 return;
2500 }
2501 list_splice_init(&connlist_err, &recovery_list);
2502 spin_unlock_irqrestore(&connlock, flags);
2503
2504 list_for_each_entry_safe(conn, tmp, &recovery_list, conn_list_err) {
2505 uint32_t sid = iscsi_conn_get_sid(conn);
2506 struct iscsi_cls_session *session;
2507
Bharath Ravi0ab71042020-01-25 01:19:25 -05002508 session = iscsi_session_lookup(sid);
2509 if (session) {
2510 if (system_state != SYSTEM_RUNNING) {
2511 session->recovery_tmo = 0;
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04002512 iscsi_if_stop_conn(conn, STOP_CONN_TERM);
Bharath Ravi0ab71042020-01-25 01:19:25 -05002513 } else {
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04002514 iscsi_if_stop_conn(conn, STOP_CONN_RECOVER);
Bharath Ravi0ab71042020-01-25 01:19:25 -05002515 }
2516 }
2517
2518 list_del_init(&conn->conn_list_err);
Bharath Ravi0ab71042020-01-25 01:19:25 -05002519 }
2520}
2521
Mike Christiee5bd7b52008-09-24 11:46:10 -05002522void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -07002523{
2524 struct nlmsghdr *nlh;
2525 struct sk_buff *skb;
2526 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -05002527 struct iscsi_internal *priv;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002528 int len = nlmsg_total_size(sizeof(*ev));
Bharath Ravi0ab71042020-01-25 01:19:25 -05002529 unsigned long flags;
2530
2531 spin_lock_irqsave(&connlock, flags);
2532 list_add(&conn->conn_list_err, &connlist_err);
2533 spin_unlock_irqrestore(&connlock, flags);
2534 queue_work(system_unbound_wq, &stop_conn_work);
Alex Aizman0896b752005-08-04 19:33:07 -07002535
Mike Christie790f39a2006-05-18 20:31:39 -05002536 priv = iscsi_if_transport_lookup(conn->transport);
2537 if (!priv)
2538 return;
2539
Mike Christie43a145a2006-10-16 18:09:38 -04002540 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002541 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06002542 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2543 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -07002544 return;
2545 }
2546
Michael Chan43514772009-06-08 18:14:41 -07002547 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002548 ev = nlmsg_data(nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07002549 ev->transport_handle = iscsi_handle(conn->transport);
2550 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -07002551 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -05002552 ev->r.connerror.cid = conn->cid;
2553 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -07002554
Michael Chan43514772009-06-08 18:14:41 -07002555 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002556
Mike Christie322d7392008-01-31 13:36:52 -06002557 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
2558 error);
Alex Aizman0896b752005-08-04 19:33:07 -07002559}
Mike Christiee5bd7b52008-09-24 11:46:10 -05002560EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
Alex Aizman0896b752005-08-04 19:33:07 -07002561
Manish Rangankar17fa5752011-07-25 13:48:52 -05002562void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
2563 enum iscsi_conn_state state)
2564{
2565 struct nlmsghdr *nlh;
2566 struct sk_buff *skb;
2567 struct iscsi_uevent *ev;
2568 struct iscsi_internal *priv;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002569 int len = nlmsg_total_size(sizeof(*ev));
Manish Rangankar17fa5752011-07-25 13:48:52 -05002570
2571 priv = iscsi_if_transport_lookup(conn->transport);
2572 if (!priv)
2573 return;
2574
2575 skb = alloc_skb(len, GFP_ATOMIC);
2576 if (!skb) {
2577 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2578 "conn login (%d)\n", state);
2579 return;
2580 }
2581
2582 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002583 ev = nlmsg_data(nlh);
Manish Rangankar17fa5752011-07-25 13:48:52 -05002584 ev->transport_handle = iscsi_handle(conn->transport);
2585 ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
2586 ev->r.conn_login.state = state;
2587 ev->r.conn_login.cid = conn->cid;
2588 ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
2589 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2590
2591 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
2592 state);
2593}
2594EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
2595
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302596void iscsi_post_host_event(uint32_t host_no, struct iscsi_transport *transport,
2597 enum iscsi_host_event_code code, uint32_t data_size,
2598 uint8_t *data)
2599{
2600 struct nlmsghdr *nlh;
2601 struct sk_buff *skb;
2602 struct iscsi_uevent *ev;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002603 int len = nlmsg_total_size(sizeof(*ev) + data_size);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302604
Mike Christie49d0e642012-03-06 16:09:01 -06002605 skb = alloc_skb(len, GFP_NOIO);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302606 if (!skb) {
2607 printk(KERN_ERR "gracefully ignored host event (%d):%d OOM\n",
2608 host_no, code);
2609 return;
2610 }
2611
2612 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002613 ev = nlmsg_data(nlh);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302614 ev->transport_handle = iscsi_handle(transport);
2615 ev->type = ISCSI_KEVENT_HOST_EVENT;
2616 ev->r.host_event.host_no = host_no;
2617 ev->r.host_event.code = code;
2618 ev->r.host_event.data_size = data_size;
2619
2620 if (data_size)
2621 memcpy((char *)ev + sizeof(*ev), data, data_size);
2622
Mike Christie49d0e642012-03-06 16:09:01 -06002623 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
Vikas Chaudharya11e2542012-02-13 18:30:46 +05302624}
2625EXPORT_SYMBOL_GPL(iscsi_post_host_event);
2626
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302627void iscsi_ping_comp_event(uint32_t host_no, struct iscsi_transport *transport,
2628 uint32_t status, uint32_t pid, uint32_t data_size,
2629 uint8_t *data)
2630{
2631 struct nlmsghdr *nlh;
2632 struct sk_buff *skb;
2633 struct iscsi_uevent *ev;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002634 int len = nlmsg_total_size(sizeof(*ev) + data_size);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302635
Mike Christie49d0e642012-03-06 16:09:01 -06002636 skb = alloc_skb(len, GFP_NOIO);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302637 if (!skb) {
2638 printk(KERN_ERR "gracefully ignored ping comp: OOM\n");
2639 return;
2640 }
2641
2642 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002643 ev = nlmsg_data(nlh);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302644 ev->transport_handle = iscsi_handle(transport);
2645 ev->type = ISCSI_KEVENT_PING_COMP;
2646 ev->r.ping_comp.host_no = host_no;
2647 ev->r.ping_comp.status = status;
2648 ev->r.ping_comp.pid = pid;
2649 ev->r.ping_comp.data_size = data_size;
2650 memcpy((char *)ev + sizeof(*ev), data, data_size);
2651
Mike Christie49d0e642012-03-06 16:09:01 -06002652 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05302653}
2654EXPORT_SYMBOL_GPL(iscsi_ping_comp_event);
2655
Alex Aizman0896b752005-08-04 19:33:07 -07002656static int
Chris Leechaf170922018-04-09 15:15:28 -07002657iscsi_if_send_reply(u32 portid, int type, void *payload, int size)
Alex Aizman0896b752005-08-04 19:33:07 -07002658{
2659 struct sk_buff *skb;
2660 struct nlmsghdr *nlh;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002661 int len = nlmsg_total_size(size);
Alex Aizman0896b752005-08-04 19:33:07 -07002662
Mike Christie43a145a2006-10-16 18:09:38 -04002663 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -05002664 if (!skb) {
2665 printk(KERN_ERR "Could not allocate skb to send reply.\n");
2666 return -ENOMEM;
2667 }
Alex Aizman0896b752005-08-04 19:33:07 -07002668
Chris Leechaf170922018-04-09 15:15:28 -07002669 nlh = __nlmsg_put(skb, 0, 0, type, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002670 memcpy(nlmsg_data(nlh), payload, size);
Chris Leechaf170922018-04-09 15:15:28 -07002671 return iscsi_unicast_skb(skb, portid);
Alex Aizman0896b752005-08-04 19:33:07 -07002672}
2673
2674static int
Mike Christie5b940ad2006-02-01 21:06:56 -06002675iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -07002676{
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002677 struct iscsi_uevent *ev = nlmsg_data(nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07002678 struct iscsi_stats *stats;
2679 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -06002680 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07002681 struct nlmsghdr *nlhstat;
2682 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -05002683 struct iscsi_internal *priv;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002684 int len = nlmsg_total_size(sizeof(*ev) +
2685 sizeof(struct iscsi_stats) +
2686 sizeof(struct iscsi_stats_custom) *
2687 ISCSI_STATS_CUSTOM_MAX);
Alex Aizman0896b752005-08-04 19:33:07 -07002688 int err = 0;
2689
Mike Christie790f39a2006-05-18 20:31:39 -05002690 priv = iscsi_if_transport_lookup(transport);
2691 if (!priv)
2692 return -EINVAL;
2693
Mike Christieb5c7a122006-04-06 21:13:33 -05002694 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -07002695 if (!conn)
2696 return -EEXIST;
2697
2698 do {
2699 int actual_size;
2700
Mike Christie43a145a2006-10-16 18:09:38 -04002701 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002702 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -06002703 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
2704 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -07002705 return -ENOMEM;
2706 }
2707
Michael Chan43514772009-06-08 18:14:41 -07002708 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07002709 (len - sizeof(*nlhstat)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002710 evstat = nlmsg_data(nlhstat);
Alex Aizman0896b752005-08-04 19:33:07 -07002711 memset(evstat, 0, sizeof(*evstat));
2712 evstat->transport_handle = iscsi_handle(conn->transport);
2713 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05002714 evstat->u.get_stats.cid =
2715 ev->u.get_stats.cid;
2716 evstat->u.get_stats.sid =
2717 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07002718 stats = (struct iscsi_stats *)
2719 ((char*)evstat + sizeof(*evstat));
2720 memset(stats, 0, sizeof(*stats));
2721
Mike Christie7b7232f2006-02-01 21:06:49 -06002722 transport->get_stats(conn, stats);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002723 actual_size = nlmsg_total_size(sizeof(struct iscsi_uevent) +
2724 sizeof(struct iscsi_stats) +
2725 sizeof(struct iscsi_stats_custom) *
2726 stats->custom_length);
Alex Aizman0896b752005-08-04 19:33:07 -07002727 actual_size -= sizeof(*nlhstat);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002728 actual_size = nlmsg_msg_size(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06002729 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07002730 nlhstat->nlmsg_len = actual_size;
2731
Michael Chan43514772009-06-08 18:14:41 -07002732 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
2733 GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -07002734 } while (err < 0 && err != -ECONNREFUSED);
2735
2736 return err;
2737}
2738
Mike Christie53cb8a12006-06-28 12:00:32 -05002739/**
Mike Christie26974782007-12-13 12:43:29 -06002740 * iscsi_session_event - send session destr. completion event
2741 * @session: iscsi class session
2742 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05002743 */
Mike Christie26974782007-12-13 12:43:29 -06002744int iscsi_session_event(struct iscsi_cls_session *session,
2745 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05002746{
2747 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05002748 struct Scsi_Host *shost;
2749 struct iscsi_uevent *ev;
2750 struct sk_buff *skb;
2751 struct nlmsghdr *nlh;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002752 int rc, len = nlmsg_total_size(sizeof(*ev));
Mike Christie53cb8a12006-06-28 12:00:32 -05002753
Mike Christie26974782007-12-13 12:43:29 -06002754 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05002755 if (!priv)
2756 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05002757 shost = iscsi_session_to_shost(session);
2758
Mike Christie43a145a2006-10-16 18:09:38 -04002759 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05002760 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06002761 iscsi_cls_session_printk(KERN_ERR, session,
2762 "Cannot notify userspace of session "
2763 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05002764 return -ENOMEM;
2765 }
2766
Michael Chan43514772009-06-08 18:14:41 -07002767 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00002768 ev = nlmsg_data(nlh);
Mike Christie26974782007-12-13 12:43:29 -06002769 ev->transport_handle = iscsi_handle(session->transport);
2770
2771 ev->type = event;
2772 switch (event) {
2773 case ISCSI_KEVENT_DESTROY_SESSION:
2774 ev->r.d_session.host_no = shost->host_no;
2775 ev->r.d_session.sid = session->sid;
2776 break;
2777 case ISCSI_KEVENT_CREATE_SESSION:
2778 ev->r.c_session_ret.host_no = shost->host_no;
2779 ev->r.c_session_ret.sid = session->sid;
2780 break;
2781 case ISCSI_KEVENT_UNBIND_SESSION:
2782 ev->r.unbind_session.host_no = shost->host_no;
2783 ev->r.unbind_session.sid = session->sid;
2784 break;
2785 default:
Mike Christie322d7392008-01-31 13:36:52 -06002786 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
2787 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06002788 kfree_skb(skb);
2789 return -EINVAL;
2790 }
Mike Christie53cb8a12006-06-28 12:00:32 -05002791
2792 /*
2793 * this will occur if the daemon is not up, so we just warn
2794 * the user and when the daemon is restarted it will handle it
2795 */
Michael Chan43514772009-06-08 18:14:41 -07002796 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08002797 if (rc == -ESRCH)
Mike Christie322d7392008-01-31 13:36:52 -06002798 iscsi_cls_session_printk(KERN_ERR, session,
2799 "Cannot notify userspace of session "
2800 "event %u. Check iscsi daemon\n",
2801 event);
Mike Christie632248a2009-08-20 15:11:01 -05002802
2803 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
2804 event, rc);
Mike Christie53cb8a12006-06-28 12:00:32 -05002805 return rc;
2806}
Mike Christie26974782007-12-13 12:43:29 -06002807EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05002808
Alex Aizman0896b752005-08-04 19:33:07 -07002809static int
Mike Christied82ff9be2008-05-21 15:54:13 -05002810iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
Mike Christie0c70d842011-12-05 16:44:01 -06002811 struct iscsi_uevent *ev, pid_t pid,
2812 uint32_t initial_cmdsn, uint16_t cmds_max,
2813 uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06002814{
2815 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06002816 struct iscsi_cls_session *session;
Mike Christie5e7facb2009-03-05 14:46:06 -06002817 struct Scsi_Host *shost;
Mike Christie7b8631b2006-01-13 18:05:50 -06002818
Mike Christied82ff9be2008-05-21 15:54:13 -05002819 session = transport->create_session(ep, cmds_max, queue_depth,
Mike Christie5e7facb2009-03-05 14:46:06 -06002820 initial_cmdsn);
Mike Christie7b7232f2006-02-01 21:06:49 -06002821 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06002822 return -ENOMEM;
2823
Mike Christie0c70d842011-12-05 16:44:01 -06002824 session->creator = pid;
Mike Christie5e7facb2009-03-05 14:46:06 -06002825 shost = iscsi_session_to_shost(session);
2826 ev->r.c_session_ret.host_no = shost->host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05002827 ev->r.c_session_ret.sid = session->sid;
Mike Christie632248a2009-08-20 15:11:01 -05002828 ISCSI_DBG_TRANS_SESSION(session,
2829 "Completed creating transport session\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002830 return 0;
2831}
2832
2833static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002834iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06002835{
Mike Christie7b8631b2006-01-13 18:05:50 -06002836 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06002837 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06002838
Mike Christieb5c7a122006-04-06 21:13:33 -05002839 session = iscsi_session_lookup(ev->u.c_conn.sid);
2840 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002841 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05002842 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06002843 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05002844 }
Mike Christie7b8631b2006-01-13 18:05:50 -06002845
Mike Christie7b7232f2006-02-01 21:06:49 -06002846 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05002847 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06002848 iscsi_cls_session_printk(KERN_ERR, session,
2849 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06002850 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05002851 }
Mike Christie7b8631b2006-01-13 18:05:50 -06002852
Mike Christieb5c7a122006-04-06 21:13:33 -05002853 ev->r.c_conn_ret.sid = session->sid;
2854 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie632248a2009-08-20 15:11:01 -05002855
2856 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
Mike Christie7b8631b2006-01-13 18:05:50 -06002857 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06002858}
2859
2860static int
2861iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2862{
Mike Christie7b8631b2006-01-13 18:05:50 -06002863 struct iscsi_cls_conn *conn;
Bharath Ravi0ab71042020-01-25 01:19:25 -05002864 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -06002865
Mike Christieb5c7a122006-04-06 21:13:33 -05002866 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06002867 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06002868 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06002869
Bharath Ravi0ab71042020-01-25 01:19:25 -05002870 spin_lock_irqsave(&connlock, flags);
2871 if (!list_empty(&conn->conn_list_err)) {
2872 spin_unlock_irqrestore(&connlock, flags);
2873 return -EAGAIN;
2874 }
2875 spin_unlock_irqrestore(&connlock, flags);
2876
Mike Christie632248a2009-08-20 15:11:01 -05002877 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04002878
2879 mutex_lock(&conn_mutex);
Mike Christie7b8631b2006-01-13 18:05:50 -06002880 if (transport->destroy_conn)
2881 transport->destroy_conn(conn);
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04002882 mutex_unlock(&conn_mutex);
Mike Christie632248a2009-08-20 15:11:01 -05002883
Mike Christie7b8631b2006-01-13 18:05:50 -06002884 return 0;
2885}
2886
Mike Christiefd7255f2006-04-06 21:13:36 -05002887static int
2888iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2889{
2890 char *data = (char*)ev + sizeof(*ev);
2891 struct iscsi_cls_conn *conn;
2892 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05002893 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05002894
Chris Leech76d92bf2021-02-23 18:00:17 -08002895 if (ev->u.set_param.len > PAGE_SIZE)
2896 return -EINVAL;
2897
Mike Christiefd7255f2006-04-06 21:13:36 -05002898 session = iscsi_session_lookup(ev->u.set_param.sid);
2899 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
2900 if (!conn || !session)
2901 return -EINVAL;
2902
2903 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05002904 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05002905 sscanf(data, "%d", &value);
Chris Leech9c8108a2015-06-16 16:07:13 -07002906 if (!session->recovery_tmo_sysfs_override)
2907 session->recovery_tmo = value;
Mike Christie30a6c652006-04-06 21:13:39 -05002908 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002909 default:
Gulam Mohamed221b7e12021-07-13 17:18:36 +08002910 if ((conn->state == ISCSI_CONN_BOUND) ||
2911 (conn->state == ISCSI_CONN_UP)) {
2912 err = transport->set_param(conn, ev->u.set_param.param,
2913 data, ev->u.set_param.len);
2914 } else {
2915 return -ENOTCONN;
2916 }
Mike Christiefd7255f2006-04-06 21:13:36 -05002917 }
2918
2919 return err;
2920}
2921
Mike Christie10eb0f02009-05-13 17:57:38 -05002922static int iscsi_if_ep_connect(struct iscsi_transport *transport,
2923 struct iscsi_uevent *ev, int msg_type)
2924{
2925 struct iscsi_endpoint *ep;
2926 struct sockaddr *dst_addr;
2927 struct Scsi_Host *shost = NULL;
2928 int non_blocking, err = 0;
2929
2930 if (!transport->ep_connect)
2931 return -EINVAL;
2932
2933 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
2934 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
2935 if (!shost) {
2936 printk(KERN_ERR "ep connect failed. Could not find "
2937 "host no %u\n",
2938 ev->u.ep_connect_through_host.host_no);
2939 return -ENODEV;
2940 }
2941 non_blocking = ev->u.ep_connect_through_host.non_blocking;
2942 } else
2943 non_blocking = ev->u.ep_connect.non_blocking;
2944
2945 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
2946 ep = transport->ep_connect(shost, dst_addr, non_blocking);
2947 if (IS_ERR(ep)) {
2948 err = PTR_ERR(ep);
2949 goto release_host;
2950 }
2951
2952 ev->r.ep_connect_ret.handle = ep->id;
2953release_host:
2954 if (shost)
2955 scsi_host_put(shost);
2956 return err;
2957}
2958
Mike Christie22a39fb2011-02-16 15:04:33 -06002959static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
2960 u64 ep_handle)
2961{
2962 struct iscsi_cls_conn *conn;
2963 struct iscsi_endpoint *ep;
2964
2965 if (!transport->ep_disconnect)
2966 return -EINVAL;
2967
2968 ep = iscsi_lookup_endpoint(ep_handle);
2969 if (!ep)
2970 return -EINVAL;
2971 conn = ep->conn;
2972 if (conn) {
2973 mutex_lock(&conn->ep_mutex);
2974 conn->ep = NULL;
2975 mutex_unlock(&conn->ep_mutex);
Mike Christie4ad382b2021-07-13 17:18:37 +08002976 conn->state = ISCSI_CONN_FAILED;
Mike Christie22a39fb2011-02-16 15:04:33 -06002977 }
2978
2979 transport->ep_disconnect(ep);
2980 return 0;
2981}
2982
Mike Christie7b8631b2006-01-13 18:05:50 -06002983static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05002984iscsi_if_transport_ep(struct iscsi_transport *transport,
2985 struct iscsi_uevent *ev, int msg_type)
2986{
Mike Christied82ff9be2008-05-21 15:54:13 -05002987 struct iscsi_endpoint *ep;
Or Gerlitz264faaa2006-05-02 19:46:36 -05002988 int rc = 0;
2989
2990 switch (msg_type) {
Mike Christie10eb0f02009-05-13 17:57:38 -05002991 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05002992 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05002993 rc = iscsi_if_ep_connect(transport, ev, msg_type);
Or Gerlitz264faaa2006-05-02 19:46:36 -05002994 break;
2995 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
2996 if (!transport->ep_poll)
2997 return -EINVAL;
2998
Mike Christied82ff9be2008-05-21 15:54:13 -05002999 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
3000 if (!ep)
3001 return -EINVAL;
3002
3003 ev->r.retcode = transport->ep_poll(ep,
Or Gerlitz264faaa2006-05-02 19:46:36 -05003004 ev->u.ep_poll.timeout_ms);
3005 break;
3006 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie22a39fb2011-02-16 15:04:33 -06003007 rc = iscsi_if_ep_disconnect(transport,
3008 ev->u.ep_disconnect.ep_handle);
Or Gerlitz264faaa2006-05-02 19:46:36 -05003009 break;
3010 }
3011 return rc;
3012}
3013
3014static int
Mike Christie01cb2252006-06-28 12:00:22 -05003015iscsi_tgt_dscvr(struct iscsi_transport *transport,
3016 struct iscsi_uevent *ev)
3017{
Mike Christie2174a042007-05-30 12:57:10 -05003018 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05003019 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05003020 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05003021
3022 if (!transport->tgt_dscvr)
3023 return -EINVAL;
3024
Mike Christie2174a042007-05-30 12:57:10 -05003025 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04003026 if (!shost) {
Mike Christie2174a042007-05-30 12:57:10 -05003027 printk(KERN_ERR "target discovery could not find host no %u\n",
3028 ev->u.tgt_dscvr.host_no);
3029 return -ENODEV;
3030 }
3031
3032
Mike Christie01cb2252006-06-28 12:00:22 -05003033 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05003034 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
3035 ev->u.tgt_dscvr.enable, dst_addr);
3036 scsi_host_put(shost);
3037 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05003038}
3039
3040static int
Mike Christie1d9bf132007-05-30 12:57:11 -05003041iscsi_set_host_param(struct iscsi_transport *transport,
3042 struct iscsi_uevent *ev)
3043{
3044 char *data = (char*)ev + sizeof(*ev);
3045 struct Scsi_Host *shost;
3046 int err;
3047
3048 if (!transport->set_host_param)
3049 return -ENOSYS;
3050
Chris Leech76d92bf2021-02-23 18:00:17 -08003051 if (ev->u.set_host_param.len > PAGE_SIZE)
3052 return -EINVAL;
3053
Mike Christie1d9bf132007-05-30 12:57:11 -05003054 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
James Smart315cb0a2008-08-07 20:49:30 -04003055 if (!shost) {
Mike Christie1d9bf132007-05-30 12:57:11 -05003056 printk(KERN_ERR "set_host_param could not find host no %u\n",
3057 ev->u.set_host_param.host_no);
3058 return -ENODEV;
3059 }
3060
3061 err = transport->set_host_param(shost, ev->u.set_host_param.param,
3062 data, ev->u.set_host_param.len);
3063 scsi_host_put(shost);
3064 return err;
3065}
3066
3067static int
Michael Chan43514772009-06-08 18:14:41 -07003068iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
3069{
3070 struct Scsi_Host *shost;
3071 struct iscsi_path *params;
3072 int err;
3073
3074 if (!transport->set_path)
3075 return -ENOSYS;
3076
3077 shost = scsi_host_lookup(ev->u.set_path.host_no);
3078 if (!shost) {
3079 printk(KERN_ERR "set path could not find host no %u\n",
3080 ev->u.set_path.host_no);
3081 return -ENODEV;
3082 }
3083
3084 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
3085 err = transport->set_path(shost, params);
3086
3087 scsi_host_put(shost);
3088 return err;
3089}
3090
Nick Black54155ed2019-12-26 15:31:48 -05003091static int iscsi_session_has_conns(int sid)
3092{
3093 struct iscsi_cls_conn *conn;
3094 unsigned long flags;
3095 int found = 0;
3096
3097 spin_lock_irqsave(&connlock, flags);
3098 list_for_each_entry(conn, &connlist, conn_list) {
3099 if (iscsi_conn_get_sid(conn) == sid) {
3100 found = 1;
3101 break;
3102 }
3103 }
3104 spin_unlock_irqrestore(&connlock, flags);
3105
3106 return found;
3107}
3108
Michael Chan43514772009-06-08 18:14:41 -07003109static int
Mike Christie56c155b2011-07-25 13:48:37 -05003110iscsi_set_iface_params(struct iscsi_transport *transport,
Mike Christie00c31882011-10-06 03:56:59 -05003111 struct iscsi_uevent *ev, uint32_t len)
Mike Christie56c155b2011-07-25 13:48:37 -05003112{
3113 char *data = (char *)ev + sizeof(*ev);
3114 struct Scsi_Host *shost;
3115 int err;
3116
3117 if (!transport->set_iface_param)
3118 return -ENOSYS;
3119
3120 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
3121 if (!shost) {
3122 printk(KERN_ERR "set_iface_params could not find host no %u\n",
3123 ev->u.set_iface_params.host_no);
3124 return -ENODEV;
3125 }
3126
Mike Christie00c31882011-10-06 03:56:59 -05003127 err = transport->set_iface_param(shost, data, len);
Mike Christie56c155b2011-07-25 13:48:37 -05003128 scsi_host_put(shost);
3129 return err;
3130}
3131
3132static int
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05303133iscsi_send_ping(struct iscsi_transport *transport, struct iscsi_uevent *ev)
3134{
3135 struct Scsi_Host *shost;
3136 struct sockaddr *dst_addr;
3137 int err;
3138
3139 if (!transport->send_ping)
3140 return -ENOSYS;
3141
3142 shost = scsi_host_lookup(ev->u.iscsi_ping.host_no);
3143 if (!shost) {
3144 printk(KERN_ERR "iscsi_ping could not find host no %u\n",
3145 ev->u.iscsi_ping.host_no);
3146 return -ENODEV;
3147 }
3148
3149 dst_addr = (struct sockaddr *)((char *)ev + sizeof(*ev));
3150 err = transport->send_ping(shost, ev->u.iscsi_ping.iface_num,
3151 ev->u.iscsi_ping.iface_type,
3152 ev->u.iscsi_ping.payload_size,
3153 ev->u.iscsi_ping.pid,
3154 dst_addr);
3155 scsi_host_put(shost);
3156 return err;
3157}
3158
3159static int
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003160iscsi_get_chap(struct iscsi_transport *transport, struct nlmsghdr *nlh)
3161{
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003162 struct iscsi_uevent *ev = nlmsg_data(nlh);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003163 struct Scsi_Host *shost = NULL;
3164 struct iscsi_chap_rec *chap_rec;
3165 struct iscsi_internal *priv;
3166 struct sk_buff *skbchap;
3167 struct nlmsghdr *nlhchap;
3168 struct iscsi_uevent *evchap;
3169 uint32_t chap_buf_size;
3170 int len, err = 0;
3171 char *buf;
3172
3173 if (!transport->get_chap)
3174 return -EINVAL;
3175
3176 priv = iscsi_if_transport_lookup(transport);
3177 if (!priv)
3178 return -EINVAL;
3179
3180 chap_buf_size = (ev->u.get_chap.num_entries * sizeof(*chap_rec));
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003181 len = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003182
3183 shost = scsi_host_lookup(ev->u.get_chap.host_no);
3184 if (!shost) {
Masanari Iida804ff602015-05-07 23:21:27 +09003185 printk(KERN_ERR "%s: failed. Could not find host no %u\n",
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003186 __func__, ev->u.get_chap.host_no);
3187 return -ENODEV;
3188 }
3189
3190 do {
3191 int actual_size;
3192
3193 skbchap = alloc_skb(len, GFP_KERNEL);
3194 if (!skbchap) {
3195 printk(KERN_ERR "can not deliver chap: OOM\n");
3196 err = -ENOMEM;
3197 goto exit_get_chap;
3198 }
3199
3200 nlhchap = __nlmsg_put(skbchap, 0, 0, 0,
3201 (len - sizeof(*nlhchap)), 0);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003202 evchap = nlmsg_data(nlhchap);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003203 memset(evchap, 0, sizeof(*evchap));
3204 evchap->transport_handle = iscsi_handle(transport);
3205 evchap->type = nlh->nlmsg_type;
3206 evchap->u.get_chap.host_no = ev->u.get_chap.host_no;
3207 evchap->u.get_chap.chap_tbl_idx = ev->u.get_chap.chap_tbl_idx;
3208 evchap->u.get_chap.num_entries = ev->u.get_chap.num_entries;
Nick Black' via open-iscsia799cb72014-07-01 11:24:37 -05003209 buf = (char *)evchap + sizeof(*evchap);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003210 memset(buf, 0, chap_buf_size);
3211
3212 err = transport->get_chap(shost, ev->u.get_chap.chap_tbl_idx,
3213 &evchap->u.get_chap.num_entries, buf);
3214
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003215 actual_size = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003216 skb_trim(skbchap, NLMSG_ALIGN(actual_size));
3217 nlhchap->nlmsg_len = actual_size;
3218
3219 err = iscsi_multicast_skb(skbchap, ISCSI_NL_GRP_ISCSID,
3220 GFP_KERNEL);
3221 } while (err < 0 && err != -ECONNREFUSED);
3222
3223exit_get_chap:
3224 scsi_host_put(shost);
3225 return err;
3226}
3227
Adheer Chandravanshi3af142f2013-09-17 07:54:48 -04003228static int iscsi_set_chap(struct iscsi_transport *transport,
3229 struct iscsi_uevent *ev, uint32_t len)
3230{
3231 char *data = (char *)ev + sizeof(*ev);
3232 struct Scsi_Host *shost;
3233 int err = 0;
3234
3235 if (!transport->set_chap)
3236 return -ENOSYS;
3237
3238 shost = scsi_host_lookup(ev->u.set_path.host_no);
3239 if (!shost) {
3240 pr_err("%s could not find host no %u\n",
3241 __func__, ev->u.set_path.host_no);
3242 return -ENODEV;
3243 }
3244
3245 err = transport->set_chap(shost, data, len);
3246 scsi_host_put(shost);
3247 return err;
3248}
3249
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003250static int iscsi_delete_chap(struct iscsi_transport *transport,
3251 struct iscsi_uevent *ev)
3252{
3253 struct Scsi_Host *shost;
3254 int err = 0;
3255
3256 if (!transport->delete_chap)
3257 return -ENOSYS;
3258
3259 shost = scsi_host_lookup(ev->u.delete_chap.host_no);
3260 if (!shost) {
3261 printk(KERN_ERR "%s could not find host no %u\n",
3262 __func__, ev->u.delete_chap.host_no);
3263 return -ENODEV;
3264 }
3265
3266 err = transport->delete_chap(shost, ev->u.delete_chap.chap_tbl_idx);
3267 scsi_host_put(shost);
3268 return err;
3269}
3270
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003271static const struct {
3272 enum iscsi_discovery_parent_type value;
3273 char *name;
3274} iscsi_discovery_parent_names[] = {
3275 {ISCSI_DISC_PARENT_UNKNOWN, "Unknown" },
3276 {ISCSI_DISC_PARENT_SENDTGT, "Sendtarget" },
3277 {ISCSI_DISC_PARENT_ISNS, "isns" },
3278};
3279
3280char *iscsi_get_discovery_parent_name(int parent_type)
3281{
3282 int i;
3283 char *state = "Unknown!";
3284
3285 for (i = 0; i < ARRAY_SIZE(iscsi_discovery_parent_names); i++) {
3286 if (iscsi_discovery_parent_names[i].value & parent_type) {
3287 state = iscsi_discovery_parent_names[i].name;
3288 break;
3289 }
3290 }
3291 return state;
3292}
3293EXPORT_SYMBOL_GPL(iscsi_get_discovery_parent_name);
3294
3295static int iscsi_set_flashnode_param(struct iscsi_transport *transport,
3296 struct iscsi_uevent *ev, uint32_t len)
3297{
3298 char *data = (char *)ev + sizeof(*ev);
3299 struct Scsi_Host *shost;
3300 struct iscsi_bus_flash_session *fnode_sess;
3301 struct iscsi_bus_flash_conn *fnode_conn;
3302 struct device *dev;
Mike Christie8526cb12013-05-06 12:06:56 -05003303 uint32_t idx;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003304 int err = 0;
3305
3306 if (!transport->set_flashnode_param) {
3307 err = -ENOSYS;
3308 goto exit_set_fnode;
3309 }
3310
3311 shost = scsi_host_lookup(ev->u.set_flashnode.host_no);
3312 if (!shost) {
3313 pr_err("%s could not find host no %u\n",
3314 __func__, ev->u.set_flashnode.host_no);
3315 err = -ENODEV;
Jing Xiangfeng68e12e5f2020-06-15 16:12:26 +08003316 goto exit_set_fnode;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003317 }
3318
Mike Christie8526cb12013-05-06 12:06:56 -05003319 idx = ev->u.set_flashnode.flashnode_idx;
3320 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003321 if (!fnode_sess) {
3322 pr_err("%s could not find flashnode %u for host no %u\n",
Mike Christie8526cb12013-05-06 12:06:56 -05003323 __func__, idx, ev->u.set_flashnode.host_no);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003324 err = -ENODEV;
3325 goto put_host;
3326 }
3327
Mike Christie8526cb12013-05-06 12:06:56 -05003328 dev = iscsi_find_flashnode_conn(fnode_sess);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003329 if (!dev) {
3330 err = -ENODEV;
Mike Christie8526cb12013-05-06 12:06:56 -05003331 goto put_sess;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003332 }
3333
3334 fnode_conn = iscsi_dev_to_flash_conn(dev);
3335 err = transport->set_flashnode_param(fnode_sess, fnode_conn, data, len);
Mike Christie8526cb12013-05-06 12:06:56 -05003336 put_device(dev);
3337
3338put_sess:
3339 put_device(&fnode_sess->dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003340
3341put_host:
3342 scsi_host_put(shost);
3343
3344exit_set_fnode:
3345 return err;
3346}
3347
3348static int iscsi_new_flashnode(struct iscsi_transport *transport,
3349 struct iscsi_uevent *ev, uint32_t len)
3350{
3351 char *data = (char *)ev + sizeof(*ev);
3352 struct Scsi_Host *shost;
3353 int index;
3354 int err = 0;
3355
3356 if (!transport->new_flashnode) {
3357 err = -ENOSYS;
3358 goto exit_new_fnode;
3359 }
3360
3361 shost = scsi_host_lookup(ev->u.new_flashnode.host_no);
3362 if (!shost) {
3363 pr_err("%s could not find host no %u\n",
3364 __func__, ev->u.new_flashnode.host_no);
3365 err = -ENODEV;
3366 goto put_host;
3367 }
3368
3369 index = transport->new_flashnode(shost, data, len);
3370
3371 if (index >= 0)
3372 ev->r.new_flashnode_ret.flashnode_idx = index;
3373 else
3374 err = -EIO;
3375
3376put_host:
3377 scsi_host_put(shost);
3378
3379exit_new_fnode:
3380 return err;
3381}
3382
3383static int iscsi_del_flashnode(struct iscsi_transport *transport,
3384 struct iscsi_uevent *ev)
3385{
3386 struct Scsi_Host *shost;
3387 struct iscsi_bus_flash_session *fnode_sess;
Mike Christie8526cb12013-05-06 12:06:56 -05003388 uint32_t idx;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003389 int err = 0;
3390
3391 if (!transport->del_flashnode) {
3392 err = -ENOSYS;
3393 goto exit_del_fnode;
3394 }
3395
3396 shost = scsi_host_lookup(ev->u.del_flashnode.host_no);
3397 if (!shost) {
3398 pr_err("%s could not find host no %u\n",
3399 __func__, ev->u.del_flashnode.host_no);
3400 err = -ENODEV;
3401 goto put_host;
3402 }
3403
Mike Christie8526cb12013-05-06 12:06:56 -05003404 idx = ev->u.del_flashnode.flashnode_idx;
3405 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003406 if (!fnode_sess) {
3407 pr_err("%s could not find flashnode %u for host no %u\n",
Mike Christie8526cb12013-05-06 12:06:56 -05003408 __func__, idx, ev->u.del_flashnode.host_no);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003409 err = -ENODEV;
3410 goto put_host;
3411 }
3412
3413 err = transport->del_flashnode(fnode_sess);
Mike Christie8526cb12013-05-06 12:06:56 -05003414 put_device(&fnode_sess->dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003415
3416put_host:
3417 scsi_host_put(shost);
3418
3419exit_del_fnode:
3420 return err;
3421}
3422
3423static int iscsi_login_flashnode(struct iscsi_transport *transport,
3424 struct iscsi_uevent *ev)
3425{
3426 struct Scsi_Host *shost;
3427 struct iscsi_bus_flash_session *fnode_sess;
3428 struct iscsi_bus_flash_conn *fnode_conn;
3429 struct device *dev;
Mike Christie8526cb12013-05-06 12:06:56 -05003430 uint32_t idx;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003431 int err = 0;
3432
3433 if (!transport->login_flashnode) {
3434 err = -ENOSYS;
3435 goto exit_login_fnode;
3436 }
3437
3438 shost = scsi_host_lookup(ev->u.login_flashnode.host_no);
3439 if (!shost) {
3440 pr_err("%s could not find host no %u\n",
3441 __func__, ev->u.login_flashnode.host_no);
3442 err = -ENODEV;
3443 goto put_host;
3444 }
3445
Mike Christie8526cb12013-05-06 12:06:56 -05003446 idx = ev->u.login_flashnode.flashnode_idx;
3447 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003448 if (!fnode_sess) {
3449 pr_err("%s could not find flashnode %u for host no %u\n",
Mike Christie8526cb12013-05-06 12:06:56 -05003450 __func__, idx, ev->u.login_flashnode.host_no);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003451 err = -ENODEV;
3452 goto put_host;
3453 }
3454
Mike Christie8526cb12013-05-06 12:06:56 -05003455 dev = iscsi_find_flashnode_conn(fnode_sess);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003456 if (!dev) {
3457 err = -ENODEV;
Mike Christie8526cb12013-05-06 12:06:56 -05003458 goto put_sess;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003459 }
3460
3461 fnode_conn = iscsi_dev_to_flash_conn(dev);
3462 err = transport->login_flashnode(fnode_sess, fnode_conn);
Mike Christie8526cb12013-05-06 12:06:56 -05003463 put_device(dev);
3464
3465put_sess:
3466 put_device(&fnode_sess->dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003467
3468put_host:
3469 scsi_host_put(shost);
3470
3471exit_login_fnode:
3472 return err;
3473}
3474
3475static int iscsi_logout_flashnode(struct iscsi_transport *transport,
3476 struct iscsi_uevent *ev)
3477{
3478 struct Scsi_Host *shost;
3479 struct iscsi_bus_flash_session *fnode_sess;
3480 struct iscsi_bus_flash_conn *fnode_conn;
3481 struct device *dev;
Mike Christie8526cb12013-05-06 12:06:56 -05003482 uint32_t idx;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003483 int err = 0;
3484
3485 if (!transport->logout_flashnode) {
3486 err = -ENOSYS;
3487 goto exit_logout_fnode;
3488 }
3489
3490 shost = scsi_host_lookup(ev->u.logout_flashnode.host_no);
3491 if (!shost) {
3492 pr_err("%s could not find host no %u\n",
3493 __func__, ev->u.logout_flashnode.host_no);
3494 err = -ENODEV;
3495 goto put_host;
3496 }
3497
Mike Christie8526cb12013-05-06 12:06:56 -05003498 idx = ev->u.logout_flashnode.flashnode_idx;
3499 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003500 if (!fnode_sess) {
3501 pr_err("%s could not find flashnode %u for host no %u\n",
Mike Christie8526cb12013-05-06 12:06:56 -05003502 __func__, idx, ev->u.logout_flashnode.host_no);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003503 err = -ENODEV;
3504 goto put_host;
3505 }
3506
Mike Christie8526cb12013-05-06 12:06:56 -05003507 dev = iscsi_find_flashnode_conn(fnode_sess);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003508 if (!dev) {
3509 err = -ENODEV;
Mike Christie8526cb12013-05-06 12:06:56 -05003510 goto put_sess;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003511 }
3512
3513 fnode_conn = iscsi_dev_to_flash_conn(dev);
3514
3515 err = transport->logout_flashnode(fnode_sess, fnode_conn);
Mike Christie8526cb12013-05-06 12:06:56 -05003516 put_device(dev);
3517
3518put_sess:
3519 put_device(&fnode_sess->dev);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003520
3521put_host:
3522 scsi_host_put(shost);
3523
3524exit_logout_fnode:
3525 return err;
3526}
3527
3528static int iscsi_logout_flashnode_sid(struct iscsi_transport *transport,
3529 struct iscsi_uevent *ev)
3530{
3531 struct Scsi_Host *shost;
3532 struct iscsi_cls_session *session;
3533 int err = 0;
3534
3535 if (!transport->logout_flashnode_sid) {
3536 err = -ENOSYS;
3537 goto exit_logout_sid;
3538 }
3539
3540 shost = scsi_host_lookup(ev->u.logout_flashnode_sid.host_no);
3541 if (!shost) {
3542 pr_err("%s could not find host no %u\n",
3543 __func__, ev->u.logout_flashnode.host_no);
3544 err = -ENODEV;
3545 goto put_host;
3546 }
3547
3548 session = iscsi_session_lookup(ev->u.logout_flashnode_sid.sid);
3549 if (!session) {
3550 pr_err("%s could not find session id %u\n",
3551 __func__, ev->u.logout_flashnode_sid.sid);
3552 err = -EINVAL;
3553 goto put_host;
3554 }
3555
3556 err = transport->logout_flashnode_sid(session);
3557
3558put_host:
3559 scsi_host_put(shost);
3560
3561exit_logout_sid:
3562 return err;
3563}
3564
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003565static int
Lalit Chandivade6fa7c552013-11-22 05:28:18 -05003566iscsi_get_host_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
3567{
3568 struct iscsi_uevent *ev = nlmsg_data(nlh);
3569 struct Scsi_Host *shost = NULL;
3570 struct iscsi_internal *priv;
3571 struct sk_buff *skbhost_stats;
3572 struct nlmsghdr *nlhhost_stats;
3573 struct iscsi_uevent *evhost_stats;
3574 int host_stats_size = 0;
3575 int len, err = 0;
3576 char *buf;
3577
3578 if (!transport->get_host_stats)
Mike Christie719e5872014-07-12 15:51:50 -05003579 return -ENOSYS;
Lalit Chandivade6fa7c552013-11-22 05:28:18 -05003580
3581 priv = iscsi_if_transport_lookup(transport);
3582 if (!priv)
3583 return -EINVAL;
3584
3585 host_stats_size = sizeof(struct iscsi_offload_host_stats);
3586 len = nlmsg_total_size(sizeof(*ev) + host_stats_size);
3587
3588 shost = scsi_host_lookup(ev->u.get_host_stats.host_no);
3589 if (!shost) {
Arvind Yadav013ee632017-10-29 11:39:46 +05303590 pr_err("%s: failed. Could not find host no %u\n",
Lalit Chandivade6fa7c552013-11-22 05:28:18 -05003591 __func__, ev->u.get_host_stats.host_no);
3592 return -ENODEV;
3593 }
3594
3595 do {
3596 int actual_size;
3597
3598 skbhost_stats = alloc_skb(len, GFP_KERNEL);
3599 if (!skbhost_stats) {
3600 pr_err("cannot deliver host stats: OOM\n");
3601 err = -ENOMEM;
3602 goto exit_host_stats;
3603 }
3604
3605 nlhhost_stats = __nlmsg_put(skbhost_stats, 0, 0, 0,
3606 (len - sizeof(*nlhhost_stats)), 0);
3607 evhost_stats = nlmsg_data(nlhhost_stats);
3608 memset(evhost_stats, 0, sizeof(*evhost_stats));
3609 evhost_stats->transport_handle = iscsi_handle(transport);
3610 evhost_stats->type = nlh->nlmsg_type;
3611 evhost_stats->u.get_host_stats.host_no =
3612 ev->u.get_host_stats.host_no;
Nick Black' via open-iscsia799cb72014-07-01 11:24:37 -05003613 buf = (char *)evhost_stats + sizeof(*evhost_stats);
Lalit Chandivade6fa7c552013-11-22 05:28:18 -05003614 memset(buf, 0, host_stats_size);
3615
3616 err = transport->get_host_stats(shost, buf, host_stats_size);
Mike Christie2f7608f2014-07-12 15:51:48 -05003617 if (err) {
Mike Christiebfb50872014-08-03 18:37:52 -05003618 kfree_skb(skbhost_stats);
Mike Christie2f7608f2014-07-12 15:51:48 -05003619 goto exit_host_stats;
3620 }
Lalit Chandivade6fa7c552013-11-22 05:28:18 -05003621
3622 actual_size = nlmsg_total_size(sizeof(*ev) + host_stats_size);
3623 skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));
3624 nlhhost_stats->nlmsg_len = actual_size;
3625
3626 err = iscsi_multicast_skb(skbhost_stats, ISCSI_NL_GRP_ISCSID,
3627 GFP_KERNEL);
3628 } while (err < 0 && err != -ECONNREFUSED);
3629
3630exit_host_stats:
3631 scsi_host_put(shost);
3632 return err;
3633}
3634
3635
3636static int
Michael Chan43514772009-06-08 18:14:41 -07003637iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
Alex Aizman0896b752005-08-04 19:33:07 -07003638{
3639 int err = 0;
Chris Leechaf170922018-04-09 15:15:28 -07003640 u32 portid;
Chris Leechf40bbcb2021-02-23 21:39:01 -08003641 u32 pdu_len;
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003642 struct iscsi_uevent *ev = nlmsg_data(nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07003643 struct iscsi_transport *transport = NULL;
3644 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06003645 struct iscsi_cls_session *session;
3646 struct iscsi_cls_conn *conn;
Mike Christied82ff9be2008-05-21 15:54:13 -05003647 struct iscsi_endpoint *ep = NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07003648
Lee Duncanc71edc52021-02-23 13:06:24 -08003649 if (!netlink_capable(skb, CAP_SYS_ADMIN))
3650 return -EPERM;
3651
Michael Chan43514772009-06-08 18:14:41 -07003652 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
3653 *group = ISCSI_NL_GRP_UIP;
3654 else
3655 *group = ISCSI_NL_GRP_ISCSID;
3656
Alex Aizman0896b752005-08-04 19:33:07 -07003657 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
3658 if (!priv)
3659 return -EINVAL;
3660 transport = priv->iscsi_transport;
3661
Mike Christie7b7232f2006-02-01 21:06:49 -06003662 if (!try_module_get(transport->owner))
3663 return -EINVAL;
3664
Chris Leechaf170922018-04-09 15:15:28 -07003665 portid = NETLINK_CB(skb).portid;
3666
Alex Aizman0896b752005-08-04 19:33:07 -07003667 switch (nlh->nlmsg_type) {
3668 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05003669 err = iscsi_if_create_session(priv, ep, ev,
Chris Leechaf170922018-04-09 15:15:28 -07003670 portid,
Mike Christie40753ca2008-05-21 15:53:56 -05003671 ev->u.c_session.initial_cmdsn,
3672 ev->u.c_session.cmds_max,
3673 ev->u.c_session.queue_depth);
3674 break;
3675 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
Mike Christied82ff9be2008-05-21 15:54:13 -05003676 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
Mike Christiec95fddc2008-06-16 10:11:32 -05003677 if (!ep) {
3678 err = -EINVAL;
3679 break;
3680 }
Mike Christied82ff9be2008-05-21 15:54:13 -05003681
3682 err = iscsi_if_create_session(priv, ep, ev,
Chris Leechaf170922018-04-09 15:15:28 -07003683 portid,
Mike Christie40753ca2008-05-21 15:53:56 -05003684 ev->u.c_bound_session.initial_cmdsn,
3685 ev->u.c_bound_session.cmds_max,
3686 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07003687 break;
3688 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05003689 session = iscsi_session_lookup(ev->u.d_session.sid);
Nick Black54155ed2019-12-26 15:31:48 -05003690 if (!session)
Mike Christie26974782007-12-13 12:43:29 -06003691 err = -EINVAL;
Nick Black54155ed2019-12-26 15:31:48 -05003692 else if (iscsi_session_has_conns(ev->u.d_session.sid))
3693 err = -EBUSY;
3694 else
3695 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06003696 break;
Frank Mayharcc6b32e2020-02-27 14:59:45 -05003697 case ISCSI_UEVENT_DESTROY_SESSION_ASYNC:
3698 session = iscsi_session_lookup(ev->u.d_session.sid);
3699 if (!session)
3700 err = -EINVAL;
3701 else if (iscsi_session_has_conns(ev->u.d_session.sid))
3702 err = -EBUSY;
3703 else {
3704 unsigned long flags;
3705
3706 /* Prevent this session from being found again */
3707 spin_lock_irqsave(&sesslock, flags);
Mike Christie93bf02e2020-07-01 14:47:48 -05003708 list_del_init(&session->sess_list);
Frank Mayharcc6b32e2020-02-27 14:59:45 -05003709 spin_unlock_irqrestore(&sesslock, flags);
3710
3711 queue_work(iscsi_destroy_workq, &session->destroy_work);
3712 }
3713 break;
Mike Christie26974782007-12-13 12:43:29 -06003714 case ISCSI_UEVENT_UNBIND_SESSION:
3715 session = iscsi_session_lookup(ev->u.d_session.sid);
3716 if (session)
Mike Christie06d25af2009-03-05 14:46:02 -06003717 scsi_queue_work(iscsi_session_to_shost(session),
3718 &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -06003719 else
Mike Christie7b7232f2006-02-01 21:06:49 -06003720 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07003721 break;
3722 case ISCSI_UEVENT_CREATE_CONN:
3723 err = iscsi_if_create_conn(transport, ev);
3724 break;
3725 case ISCSI_UEVENT_DESTROY_CONN:
3726 err = iscsi_if_destroy_conn(transport, ev);
3727 break;
3728 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05003729 session = iscsi_session_lookup(ev->u.b_conn.sid);
3730 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06003731
Mike Christie22a39fb2011-02-16 15:04:33 -06003732 if (conn && conn->ep)
3733 iscsi_if_ep_disconnect(transport, conn->ep->id);
3734
3735 if (!session || !conn) {
Mike Christie7b7232f2006-02-01 21:06:49 -06003736 err = -EINVAL;
Mike Christie22a39fb2011-02-16 15:04:33 -06003737 break;
3738 }
3739
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04003740 mutex_lock(&conn_mutex);
Mike Christie22a39fb2011-02-16 15:04:33 -06003741 ev->r.retcode = transport->bind_conn(session, conn,
3742 ev->u.b_conn.transport_eph,
3743 ev->u.b_conn.is_leading);
Gulam Mohamed221b7e12021-07-13 17:18:36 +08003744 if (!ev->r.retcode)
3745 conn->state = ISCSI_CONN_BOUND;
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04003746 mutex_unlock(&conn_mutex);
3747
Mike Christie22a39fb2011-02-16 15:04:33 -06003748 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
3755 mutex_lock(&conn->ep_mutex);
3756 conn->ep = ep;
3757 mutex_unlock(&conn->ep_mutex);
3758 } else
3759 iscsi_cls_conn_printk(KERN_ERR, conn,
3760 "Could not set ep conn "
3761 "binding\n");
Alex Aizman0896b752005-08-04 19:33:07 -07003762 break;
3763 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05003764 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07003765 break;
3766 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05003767 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04003768 if (conn) {
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04003769 mutex_lock(&conn_mutex);
Mike Christie7b7232f2006-02-01 21:06:49 -06003770 ev->r.retcode = transport->start_conn(conn);
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04003771 if (!ev->r.retcode)
3772 conn->state = ISCSI_CONN_UP;
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04003773 mutex_unlock(&conn_mutex);
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04003774 }
Mike Christie7b7232f2006-02-01 21:06:49 -06003775 else
3776 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07003777 break;
3778 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05003779 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06003780 if (conn)
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04003781 iscsi_if_stop_conn(conn, ev->u.stop_conn.flag);
Mike Christie7b7232f2006-02-01 21:06:49 -06003782 else
3783 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07003784 break;
3785 case ISCSI_UEVENT_SEND_PDU:
Chris Leechf40bbcb2021-02-23 21:39:01 -08003786 pdu_len = nlh->nlmsg_len - sizeof(*nlh) - sizeof(*ev);
3787
3788 if ((ev->u.send_pdu.hdr_size > pdu_len) ||
3789 (ev->u.send_pdu.data_size > (pdu_len - ev->u.send_pdu.hdr_size))) {
3790 err = -EINVAL;
3791 break;
3792 }
3793
Mike Christieb5c7a122006-04-06 21:13:33 -05003794 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04003795 if (conn) {
3796 mutex_lock(&conn_mutex);
Mike Christie7b7232f2006-02-01 21:06:49 -06003797 ev->r.retcode = transport->send_pdu(conn,
3798 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
3799 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
3800 ev->u.send_pdu.data_size);
Gabriel Krisman Bertazi7e7cd7962020-05-19 22:29:59 -04003801 mutex_unlock(&conn_mutex);
3802 }
Mike Christie7b7232f2006-02-01 21:06:49 -06003803 else
3804 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07003805 break;
3806 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06003807 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07003808 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05003809 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
3810 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
3811 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
Mike Christie10eb0f02009-05-13 17:57:38 -05003812 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
Or Gerlitz264faaa2006-05-02 19:46:36 -05003813 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
3814 break;
Mike Christie01cb2252006-06-28 12:00:22 -05003815 case ISCSI_UEVENT_TGT_DSCVR:
3816 err = iscsi_tgt_dscvr(transport, ev);
3817 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05003818 case ISCSI_UEVENT_SET_HOST_PARAM:
3819 err = iscsi_set_host_param(transport, ev);
3820 break;
Michael Chan43514772009-06-08 18:14:41 -07003821 case ISCSI_UEVENT_PATH_UPDATE:
3822 err = iscsi_set_path(transport, ev);
3823 break;
Mike Christie56c155b2011-07-25 13:48:37 -05003824 case ISCSI_UEVENT_SET_IFACE_PARAMS:
Mike Christie00c31882011-10-06 03:56:59 -05003825 err = iscsi_set_iface_params(transport, ev,
3826 nlmsg_attrlen(nlh, sizeof(*ev)));
Mike Christie56c155b2011-07-25 13:48:37 -05003827 break;
Vikas Chaudharyac20c7b2012-02-13 18:30:48 +05303828 case ISCSI_UEVENT_PING:
3829 err = iscsi_send_ping(transport, ev);
3830 break;
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003831 case ISCSI_UEVENT_GET_CHAP:
3832 err = iscsi_get_chap(transport, nlh);
3833 break;
3834 case ISCSI_UEVENT_DELETE_CHAP:
3835 err = iscsi_delete_chap(transport, ev);
3836 break;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04003837 case ISCSI_UEVENT_SET_FLASHNODE_PARAMS:
3838 err = iscsi_set_flashnode_param(transport, ev,
3839 nlmsg_attrlen(nlh,
3840 sizeof(*ev)));
3841 break;
3842 case ISCSI_UEVENT_NEW_FLASHNODE:
3843 err = iscsi_new_flashnode(transport, ev,
3844 nlmsg_attrlen(nlh, sizeof(*ev)));
3845 break;
3846 case ISCSI_UEVENT_DEL_FLASHNODE:
3847 err = iscsi_del_flashnode(transport, ev);
3848 break;
3849 case ISCSI_UEVENT_LOGIN_FLASHNODE:
3850 err = iscsi_login_flashnode(transport, ev);
3851 break;
3852 case ISCSI_UEVENT_LOGOUT_FLASHNODE:
3853 err = iscsi_logout_flashnode(transport, ev);
3854 break;
3855 case ISCSI_UEVENT_LOGOUT_FLASHNODE_SID:
3856 err = iscsi_logout_flashnode_sid(transport, ev);
3857 break;
Adheer Chandravanshi3af142f2013-09-17 07:54:48 -04003858 case ISCSI_UEVENT_SET_CHAP:
3859 err = iscsi_set_chap(transport, ev,
3860 nlmsg_attrlen(nlh, sizeof(*ev)));
3861 break;
Lalit Chandivade6fa7c552013-11-22 05:28:18 -05003862 case ISCSI_UEVENT_GET_HOST_STATS:
3863 err = iscsi_get_host_stats(transport, nlh);
3864 break;
Alex Aizman0896b752005-08-04 19:33:07 -07003865 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05003866 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07003867 break;
3868 }
3869
Mike Christie7b7232f2006-02-01 21:06:49 -06003870 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07003871 return err;
3872}
3873
Mike Christieb5c7a122006-04-06 21:13:33 -05003874/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003875 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
3876 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05003877 */
Alex Aizman0896b752005-08-04 19:33:07 -07003878static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003879iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07003880{
Chris Leechaf170922018-04-09 15:15:28 -07003881 u32 portid = NETLINK_CB(skb).portid;
3882
Arjan van de Ven0b950672006-01-11 13:16:10 +01003883 mutex_lock(&rx_queue_mutex);
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003884 while (skb->len >= NLMSG_HDRLEN) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003885 int err;
3886 uint32_t rlen;
3887 struct nlmsghdr *nlh;
3888 struct iscsi_uevent *ev;
Michael Chan43514772009-06-08 18:14:41 -07003889 uint32_t group;
Bo Wubba340c2019-11-20 13:26:17 +00003890 int retries = ISCSI_SEND_MAX_ALLOWED;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003891
3892 nlh = nlmsg_hdr(skb);
Xin Longc88f0e62017-08-27 20:25:26 +08003893 if (nlh->nlmsg_len < sizeof(*nlh) + sizeof(*ev) ||
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003894 skb->len < nlh->nlmsg_len) {
3895 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06003896 }
Mike Christieee7f8e42006-02-01 21:07:01 -06003897
Hong zhi guoe07ebea2013-03-27 06:53:15 +00003898 ev = nlmsg_data(nlh);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003899 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
3900 if (rlen > skb->len)
3901 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07003902
Michael Chan43514772009-06-08 18:14:41 -07003903 err = iscsi_if_recv_msg(skb, nlh, &group);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003904 if (err) {
3905 ev->type = ISCSI_KEVENT_IF_ERROR;
3906 ev->iferror = err;
3907 }
3908 do {
3909 /*
3910 * special case for GET_STATS:
3911 * on success - sending reply and stats from
3912 * inside of if_recv_msg(),
3913 * on error - fall through.
3914 */
3915 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07003916 break;
Nilesh Javali6260a5d2012-02-27 03:08:51 -08003917 if (ev->type == ISCSI_UEVENT_GET_CHAP && !err)
3918 break;
Chris Leechaf170922018-04-09 15:15:28 -07003919 err = iscsi_if_send_reply(portid, nlh->nlmsg_type,
3920 ev, sizeof(*ev));
Bo Wubba340c2019-11-20 13:26:17 +00003921 if (err == -EAGAIN && --retries < 0) {
3922 printk(KERN_WARNING "Send reply failed, error %d\n", err);
3923 break;
3924 }
Sasha Levin46a7c172012-01-25 22:16:16 -05003925 } while (err < 0 && err != -ECONNREFUSED && err != -ESRCH);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003926 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07003927 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01003928 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07003929}
3930
Mike Christiefd7255f2006-04-06 21:13:36 -05003931#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01003932struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05003933 __ATTR(_name,_mode,_show,_store)
3934
Alex Aizman0896b752005-08-04 19:33:07 -07003935/*
3936 * iSCSI connection attrs
3937 */
Mike Christiea54a52c2006-06-28 12:00:23 -05003938#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01003940show_conn_param_##param(struct device *dev, \
3941 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05003942{ \
Tony Jonesee959b02008-02-22 00:13:36 +01003943 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05003944 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05003945 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05003946}
3947
Mike Christiea54a52c2006-06-28 12:00:23 -05003948#define iscsi_conn_attr(field, param) \
3949 iscsi_conn_attr_show(param) \
3950static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05003951 NULL);
3952
Mike Christiea54a52c2006-06-28 12:00:23 -05003953iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
3954iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
3955iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
3956iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
3957iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
3958iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
3959iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
Mike Christiea54a52c2006-06-28 12:00:23 -05003960iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
3961iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06003962iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
3963iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04003964iscsi_conn_attr(local_port, ISCSI_PARAM_LOCAL_PORT);
3965iscsi_conn_attr(statsn, ISCSI_PARAM_STATSN);
3966iscsi_conn_attr(keepalive_tmo, ISCSI_PARAM_KEEPALIVE_TMO);
3967iscsi_conn_attr(max_segment_size, ISCSI_PARAM_MAX_SEGMENT_SIZE);
3968iscsi_conn_attr(tcp_timestamp_stat, ISCSI_PARAM_TCP_TIMESTAMP_STAT);
3969iscsi_conn_attr(tcp_wsf_disable, ISCSI_PARAM_TCP_WSF_DISABLE);
3970iscsi_conn_attr(tcp_nagle_disable, ISCSI_PARAM_TCP_NAGLE_DISABLE);
3971iscsi_conn_attr(tcp_timer_scale, ISCSI_PARAM_TCP_TIMER_SCALE);
3972iscsi_conn_attr(tcp_timestamp_enable, ISCSI_PARAM_TCP_TIMESTAMP_EN);
3973iscsi_conn_attr(fragment_disable, ISCSI_PARAM_IP_FRAGMENT_DISABLE);
3974iscsi_conn_attr(ipv4_tos, ISCSI_PARAM_IPV4_TOS);
3975iscsi_conn_attr(ipv6_traffic_class, ISCSI_PARAM_IPV6_TC);
3976iscsi_conn_attr(ipv6_flow_label, ISCSI_PARAM_IPV6_FLOW_LABEL);
3977iscsi_conn_attr(is_fw_assigned_ipv6, ISCSI_PARAM_IS_FW_ASSIGNED_IPV6);
3978iscsi_conn_attr(tcp_xmit_wsf, ISCSI_PARAM_TCP_XMIT_WSF);
3979iscsi_conn_attr(tcp_recv_wsf, ISCSI_PARAM_TCP_RECV_WSF);
Adheer Chandravanshi5af62f22013-11-22 05:28:20 -05003980iscsi_conn_attr(local_ipaddr, ISCSI_PARAM_LOCAL_IPADDR);
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04003981
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04003982static const char *const connection_state_names[] = {
3983 [ISCSI_CONN_UP] = "up",
3984 [ISCSI_CONN_DOWN] = "down",
Gulam Mohamed221b7e12021-07-13 17:18:36 +08003985 [ISCSI_CONN_FAILED] = "failed",
3986 [ISCSI_CONN_BOUND] = "bound"
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04003987};
3988
3989static ssize_t show_conn_state(struct device *dev,
3990 struct device_attribute *attr, char *buf)
3991{
3992 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);
3993 const char *state = "unknown";
3994
3995 if (conn->state >= 0 &&
3996 conn->state < ARRAY_SIZE(connection_state_names))
3997 state = connection_state_names[conn->state];
3998
Chris Leech76d92bf2021-02-23 18:00:17 -08003999 return sysfs_emit(buf, "%s\n", state);
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04004000}
4001static ISCSI_CLASS_ATTR(conn, state, S_IRUGO, show_conn_state,
4002 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Mike Christie289324b2011-02-16 15:04:37 -06004004#define iscsi_conn_ep_attr_show(param) \
4005static ssize_t show_conn_ep_param_##param(struct device *dev, \
4006 struct device_attribute *attr,\
4007 char *buf) \
4008{ \
4009 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
4010 struct iscsi_transport *t = conn->transport; \
4011 struct iscsi_endpoint *ep; \
4012 ssize_t rc; \
4013 \
4014 /* \
4015 * Need to make sure ep_disconnect does not free the LLD's \
4016 * interconnect resources while we are trying to read them. \
4017 */ \
4018 mutex_lock(&conn->ep_mutex); \
4019 ep = conn->ep; \
4020 if (!ep && t->ep_connect) { \
4021 mutex_unlock(&conn->ep_mutex); \
4022 return -ENOTCONN; \
4023 } \
4024 \
4025 if (ep) \
4026 rc = t->get_ep_param(ep, param, buf); \
4027 else \
4028 rc = t->get_conn_param(conn, param, buf); \
4029 mutex_unlock(&conn->ep_mutex); \
4030 return rc; \
4031}
4032
4033#define iscsi_conn_ep_attr(field, param) \
4034 iscsi_conn_ep_attr_show(param) \
4035static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
4036 show_conn_ep_param_##param, NULL);
4037
4038iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
4039iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
4040
Mike Christie3128c6c2011-07-25 13:48:42 -05004041static struct attribute *iscsi_conn_attrs[] = {
4042 &dev_attr_conn_max_recv_dlength.attr,
4043 &dev_attr_conn_max_xmit_dlength.attr,
4044 &dev_attr_conn_header_digest.attr,
4045 &dev_attr_conn_data_digest.attr,
4046 &dev_attr_conn_ifmarker.attr,
4047 &dev_attr_conn_ofmarker.attr,
4048 &dev_attr_conn_address.attr,
4049 &dev_attr_conn_port.attr,
4050 &dev_attr_conn_exp_statsn.attr,
4051 &dev_attr_conn_persistent_address.attr,
4052 &dev_attr_conn_persistent_port.attr,
4053 &dev_attr_conn_ping_tmo.attr,
4054 &dev_attr_conn_recv_tmo.attr,
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04004055 &dev_attr_conn_local_port.attr,
4056 &dev_attr_conn_statsn.attr,
4057 &dev_attr_conn_keepalive_tmo.attr,
4058 &dev_attr_conn_max_segment_size.attr,
4059 &dev_attr_conn_tcp_timestamp_stat.attr,
4060 &dev_attr_conn_tcp_wsf_disable.attr,
4061 &dev_attr_conn_tcp_nagle_disable.attr,
4062 &dev_attr_conn_tcp_timer_scale.attr,
4063 &dev_attr_conn_tcp_timestamp_enable.attr,
4064 &dev_attr_conn_fragment_disable.attr,
4065 &dev_attr_conn_ipv4_tos.attr,
4066 &dev_attr_conn_ipv6_traffic_class.attr,
4067 &dev_attr_conn_ipv6_flow_label.attr,
4068 &dev_attr_conn_is_fw_assigned_ipv6.attr,
4069 &dev_attr_conn_tcp_xmit_wsf.attr,
4070 &dev_attr_conn_tcp_recv_wsf.attr,
Adheer Chandravanshi5af62f22013-11-22 05:28:20 -05004071 &dev_attr_conn_local_ipaddr.attr,
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04004072 &dev_attr_conn_state.attr,
Mike Christie3128c6c2011-07-25 13:48:42 -05004073 NULL,
4074};
4075
Al Viro587a1f12011-07-23 23:11:19 -04004076static umode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
Mike Christie3128c6c2011-07-25 13:48:42 -05004077 struct attribute *attr, int i)
4078{
4079 struct device *cdev = container_of(kobj, struct device, kobj);
4080 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
4081 struct iscsi_transport *t = conn->transport;
4082 int param;
4083
4084 if (attr == &dev_attr_conn_max_recv_dlength.attr)
4085 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
4086 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
4087 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
4088 else if (attr == &dev_attr_conn_header_digest.attr)
4089 param = ISCSI_PARAM_HDRDGST_EN;
4090 else if (attr == &dev_attr_conn_data_digest.attr)
4091 param = ISCSI_PARAM_DATADGST_EN;
4092 else if (attr == &dev_attr_conn_ifmarker.attr)
4093 param = ISCSI_PARAM_IFMARKER_EN;
4094 else if (attr == &dev_attr_conn_ofmarker.attr)
4095 param = ISCSI_PARAM_OFMARKER_EN;
4096 else if (attr == &dev_attr_conn_address.attr)
4097 param = ISCSI_PARAM_CONN_ADDRESS;
4098 else if (attr == &dev_attr_conn_port.attr)
4099 param = ISCSI_PARAM_CONN_PORT;
4100 else if (attr == &dev_attr_conn_exp_statsn.attr)
4101 param = ISCSI_PARAM_EXP_STATSN;
4102 else if (attr == &dev_attr_conn_persistent_address.attr)
4103 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
4104 else if (attr == &dev_attr_conn_persistent_port.attr)
4105 param = ISCSI_PARAM_PERSISTENT_PORT;
4106 else if (attr == &dev_attr_conn_ping_tmo.attr)
4107 param = ISCSI_PARAM_PING_TMO;
4108 else if (attr == &dev_attr_conn_recv_tmo.attr)
4109 param = ISCSI_PARAM_RECV_TMO;
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04004110 else if (attr == &dev_attr_conn_local_port.attr)
4111 param = ISCSI_PARAM_LOCAL_PORT;
4112 else if (attr == &dev_attr_conn_statsn.attr)
4113 param = ISCSI_PARAM_STATSN;
4114 else if (attr == &dev_attr_conn_keepalive_tmo.attr)
4115 param = ISCSI_PARAM_KEEPALIVE_TMO;
4116 else if (attr == &dev_attr_conn_max_segment_size.attr)
4117 param = ISCSI_PARAM_MAX_SEGMENT_SIZE;
4118 else if (attr == &dev_attr_conn_tcp_timestamp_stat.attr)
4119 param = ISCSI_PARAM_TCP_TIMESTAMP_STAT;
4120 else if (attr == &dev_attr_conn_tcp_wsf_disable.attr)
4121 param = ISCSI_PARAM_TCP_WSF_DISABLE;
4122 else if (attr == &dev_attr_conn_tcp_nagle_disable.attr)
4123 param = ISCSI_PARAM_TCP_NAGLE_DISABLE;
4124 else if (attr == &dev_attr_conn_tcp_timer_scale.attr)
4125 param = ISCSI_PARAM_TCP_TIMER_SCALE;
4126 else if (attr == &dev_attr_conn_tcp_timestamp_enable.attr)
4127 param = ISCSI_PARAM_TCP_TIMESTAMP_EN;
4128 else if (attr == &dev_attr_conn_fragment_disable.attr)
4129 param = ISCSI_PARAM_IP_FRAGMENT_DISABLE;
4130 else if (attr == &dev_attr_conn_ipv4_tos.attr)
4131 param = ISCSI_PARAM_IPV4_TOS;
4132 else if (attr == &dev_attr_conn_ipv6_traffic_class.attr)
4133 param = ISCSI_PARAM_IPV6_TC;
4134 else if (attr == &dev_attr_conn_ipv6_flow_label.attr)
4135 param = ISCSI_PARAM_IPV6_FLOW_LABEL;
4136 else if (attr == &dev_attr_conn_is_fw_assigned_ipv6.attr)
4137 param = ISCSI_PARAM_IS_FW_ASSIGNED_IPV6;
4138 else if (attr == &dev_attr_conn_tcp_xmit_wsf.attr)
4139 param = ISCSI_PARAM_TCP_XMIT_WSF;
4140 else if (attr == &dev_attr_conn_tcp_recv_wsf.attr)
4141 param = ISCSI_PARAM_TCP_RECV_WSF;
Adheer Chandravanshi5af62f22013-11-22 05:28:20 -05004142 else if (attr == &dev_attr_conn_local_ipaddr.attr)
4143 param = ISCSI_PARAM_LOCAL_IPADDR;
Gabriel Krisman Bertazi82b8cf42020-03-17 19:34:22 -04004144 else if (attr == &dev_attr_conn_state.attr)
4145 return S_IRUGO;
Mike Christie3128c6c2011-07-25 13:48:42 -05004146 else {
4147 WARN_ONCE(1, "Invalid conn attr");
4148 return 0;
4149 }
4150
4151 return t->attr_is_visible(ISCSI_PARAM, param);
4152}
4153
4154static struct attribute_group iscsi_conn_group = {
4155 .attrs = iscsi_conn_attrs,
4156 .is_visible = iscsi_conn_attr_is_visible,
4157};
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159/*
Alex Aizman0896b752005-08-04 19:33:07 -07004160 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 */
Mike Christieb2c64162007-05-30 12:57:16 -05004162#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01004164show_session_param_##param(struct device *dev, \
4165 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05004166{ \
Tony Jonesee959b02008-02-22 00:13:36 +01004167 struct iscsi_cls_session *session = \
4168 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05004169 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05004170 \
4171 if (perm && !capable(CAP_SYS_ADMIN)) \
4172 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05004173 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05004174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Mike Christieb2c64162007-05-30 12:57:16 -05004176#define iscsi_session_attr(field, param, perm) \
4177 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05004178static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05004179 NULL);
Mike Christieb2c64162007-05-30 12:57:16 -05004180iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
4181iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
4182iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
4183iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
4184iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
4185iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
4186iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
4187iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
4188iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
4189iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
4190iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
4191iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
4192iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
4193iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie30534952012-02-27 03:08:53 -08004194iscsi_session_attr(chap_out_idx, ISCSI_PARAM_CHAP_OUT_IDX, 1);
4195iscsi_session_attr(chap_in_idx, ISCSI_PARAM_CHAP_IN_IDX, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06004196iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
4197iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
4198iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christie3fe5ae82009-11-11 16:34:33 -06004199iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
Mike Christie88dfd342008-05-21 15:54:16 -05004200iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
Vikas Chaudhary3b2bef12010-07-10 14:51:30 +05304201iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
4202iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
Eddie Wai3b9373e2013-06-20 10:21:26 -07004203iscsi_session_attr(boot_root, ISCSI_PARAM_BOOT_ROOT, 0);
4204iscsi_session_attr(boot_nic, ISCSI_PARAM_BOOT_NIC, 0);
4205iscsi_session_attr(boot_target, ISCSI_PARAM_BOOT_TARGET, 0);
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04004206iscsi_session_attr(auto_snd_tgt_disable, ISCSI_PARAM_AUTO_SND_TGT_DISABLE, 0);
4207iscsi_session_attr(discovery_session, ISCSI_PARAM_DISCOVERY_SESS, 0);
4208iscsi_session_attr(portal_type, ISCSI_PARAM_PORTAL_TYPE, 0);
4209iscsi_session_attr(chap_auth, ISCSI_PARAM_CHAP_AUTH_EN, 0);
4210iscsi_session_attr(discovery_logout, ISCSI_PARAM_DISCOVERY_LOGOUT_EN, 0);
4211iscsi_session_attr(bidi_chap, ISCSI_PARAM_BIDI_CHAP_EN, 0);
4212iscsi_session_attr(discovery_auth_optional,
4213 ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL, 0);
4214iscsi_session_attr(def_time2wait, ISCSI_PARAM_DEF_TIME2WAIT, 0);
4215iscsi_session_attr(def_time2retain, ISCSI_PARAM_DEF_TIME2RETAIN, 0);
4216iscsi_session_attr(isid, ISCSI_PARAM_ISID, 0);
4217iscsi_session_attr(tsid, ISCSI_PARAM_TSID, 0);
4218iscsi_session_attr(def_taskmgmt_tmo, ISCSI_PARAM_DEF_TASKMGMT_TMO, 0);
4219iscsi_session_attr(discovery_parent_idx, ISCSI_PARAM_DISCOVERY_PARENT_IDX, 0);
4220iscsi_session_attr(discovery_parent_type, ISCSI_PARAM_DISCOVERY_PARENT_TYPE, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05004221
Mike Christie6eabafb2008-01-31 13:36:43 -06004222static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01004223show_priv_session_state(struct device *dev, struct device_attribute *attr,
4224 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06004225{
Tony Jonesee959b02008-02-22 00:13:36 +01004226 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Chris Leech76d92bf2021-02-23 18:00:17 -08004227 return sysfs_emit(buf, "%s\n", iscsi_session_state_name(session->state));
Mike Christie6eabafb2008-01-31 13:36:43 -06004228}
4229static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
4230 NULL);
Mike Christie0c70d842011-12-05 16:44:01 -06004231static ssize_t
4232show_priv_session_creator(struct device *dev, struct device_attribute *attr,
4233 char *buf)
4234{
4235 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Chris Leech76d92bf2021-02-23 18:00:17 -08004236 return sysfs_emit(buf, "%d\n", session->creator);
Mike Christie0c70d842011-12-05 16:44:01 -06004237}
4238static ISCSI_CLASS_ATTR(priv_sess, creator, S_IRUGO, show_priv_session_creator,
4239 NULL);
Manish Rangankar930a9f02012-11-23 06:58:41 -05004240static ssize_t
4241show_priv_session_target_id(struct device *dev, struct device_attribute *attr,
4242 char *buf)
4243{
4244 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Chris Leech76d92bf2021-02-23 18:00:17 -08004245 return sysfs_emit(buf, "%d\n", session->target_id);
Manish Rangankar930a9f02012-11-23 06:58:41 -05004246}
4247static ISCSI_CLASS_ATTR(priv_sess, target_id, S_IRUGO,
4248 show_priv_session_target_id, NULL);
Mike Christie6eabafb2008-01-31 13:36:43 -06004249
Mike Christiefd7255f2006-04-06 21:13:36 -05004250#define iscsi_priv_session_attr_show(field, format) \
4251static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01004252show_priv_session_##field(struct device *dev, \
4253 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05004254{ \
Tony Jonesee959b02008-02-22 00:13:36 +01004255 struct iscsi_cls_session *session = \
4256 iscsi_dev_to_session(dev->parent); \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304257 if (session->field == -1) \
Chris Leech76d92bf2021-02-23 18:00:17 -08004258 return sysfs_emit(buf, "off\n"); \
4259 return sysfs_emit(buf, format"\n", session->field); \
Mike Christiefd7255f2006-04-06 21:13:36 -05004260}
4261
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304262#define iscsi_priv_session_attr_store(field) \
4263static ssize_t \
4264store_priv_session_##field(struct device *dev, \
4265 struct device_attribute *attr, \
4266 const char *buf, size_t count) \
4267{ \
4268 int val; \
4269 char *cp; \
4270 struct iscsi_cls_session *session = \
4271 iscsi_dev_to_session(dev->parent); \
4272 if ((session->state == ISCSI_SESSION_FREE) || \
4273 (session->state == ISCSI_SESSION_FAILED)) \
4274 return -EBUSY; \
Chris Leech9c8108a2015-06-16 16:07:13 -07004275 if (strncmp(buf, "off", 3) == 0) { \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304276 session->field = -1; \
Chris Leech9c8108a2015-06-16 16:07:13 -07004277 session->field##_sysfs_override = true; \
4278 } else { \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304279 val = simple_strtoul(buf, &cp, 0); \
4280 if (*cp != '\0' && *cp != '\n') \
4281 return -EINVAL; \
4282 session->field = val; \
Chris Leech9c8108a2015-06-16 16:07:13 -07004283 session->field##_sysfs_override = true; \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304284 } \
4285 return count; \
4286}
4287
4288#define iscsi_priv_session_rw_attr(field, format) \
Mike Christiefd7255f2006-04-06 21:13:36 -05004289 iscsi_priv_session_attr_show(field, format) \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304290 iscsi_priv_session_attr_store(field) \
Vasiliy Kulikov523f3c82011-02-04 15:24:14 +03004291static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304292 show_priv_session_##field, \
4293 store_priv_session_##field)
Chris Leech9c8108a2015-06-16 16:07:13 -07004294
Vikas Chaudharyfe4f0bd2010-07-22 16:57:43 +05304295iscsi_priv_session_rw_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05004296
Mike Christie1d063c12011-07-25 13:48:43 -05004297static struct attribute *iscsi_session_attrs[] = {
4298 &dev_attr_sess_initial_r2t.attr,
4299 &dev_attr_sess_max_outstanding_r2t.attr,
4300 &dev_attr_sess_immediate_data.attr,
4301 &dev_attr_sess_first_burst_len.attr,
4302 &dev_attr_sess_max_burst_len.attr,
4303 &dev_attr_sess_data_pdu_in_order.attr,
4304 &dev_attr_sess_data_seq_in_order.attr,
4305 &dev_attr_sess_erl.attr,
4306 &dev_attr_sess_targetname.attr,
4307 &dev_attr_sess_tpgt.attr,
4308 &dev_attr_sess_password.attr,
4309 &dev_attr_sess_password_in.attr,
4310 &dev_attr_sess_username.attr,
4311 &dev_attr_sess_username_in.attr,
4312 &dev_attr_sess_fast_abort.attr,
4313 &dev_attr_sess_abort_tmo.attr,
4314 &dev_attr_sess_lu_reset_tmo.attr,
4315 &dev_attr_sess_tgt_reset_tmo.attr,
4316 &dev_attr_sess_ifacename.attr,
4317 &dev_attr_sess_initiatorname.attr,
4318 &dev_attr_sess_targetalias.attr,
Eddie Wai3b9373e2013-06-20 10:21:26 -07004319 &dev_attr_sess_boot_root.attr,
4320 &dev_attr_sess_boot_nic.attr,
4321 &dev_attr_sess_boot_target.attr,
Mike Christie1d063c12011-07-25 13:48:43 -05004322 &dev_attr_priv_sess_recovery_tmo.attr,
4323 &dev_attr_priv_sess_state.attr,
Mike Christie0c70d842011-12-05 16:44:01 -06004324 &dev_attr_priv_sess_creator.attr,
Mike Christie30534952012-02-27 03:08:53 -08004325 &dev_attr_sess_chap_out_idx.attr,
4326 &dev_attr_sess_chap_in_idx.attr,
Manish Rangankar930a9f02012-11-23 06:58:41 -05004327 &dev_attr_priv_sess_target_id.attr,
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04004328 &dev_attr_sess_auto_snd_tgt_disable.attr,
4329 &dev_attr_sess_discovery_session.attr,
4330 &dev_attr_sess_portal_type.attr,
4331 &dev_attr_sess_chap_auth.attr,
4332 &dev_attr_sess_discovery_logout.attr,
4333 &dev_attr_sess_bidi_chap.attr,
4334 &dev_attr_sess_discovery_auth_optional.attr,
4335 &dev_attr_sess_def_time2wait.attr,
4336 &dev_attr_sess_def_time2retain.attr,
4337 &dev_attr_sess_isid.attr,
4338 &dev_attr_sess_tsid.attr,
4339 &dev_attr_sess_def_taskmgmt_tmo.attr,
4340 &dev_attr_sess_discovery_parent_idx.attr,
4341 &dev_attr_sess_discovery_parent_type.attr,
Mike Christie1d063c12011-07-25 13:48:43 -05004342 NULL,
4343};
4344
Al Viro587a1f12011-07-23 23:11:19 -04004345static umode_t iscsi_session_attr_is_visible(struct kobject *kobj,
Mike Christie1d063c12011-07-25 13:48:43 -05004346 struct attribute *attr, int i)
4347{
4348 struct device *cdev = container_of(kobj, struct device, kobj);
4349 struct iscsi_cls_session *session = transport_class_to_session(cdev);
4350 struct iscsi_transport *t = session->transport;
4351 int param;
4352
4353 if (attr == &dev_attr_sess_initial_r2t.attr)
4354 param = ISCSI_PARAM_INITIAL_R2T_EN;
4355 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
4356 param = ISCSI_PARAM_MAX_R2T;
4357 else if (attr == &dev_attr_sess_immediate_data.attr)
4358 param = ISCSI_PARAM_IMM_DATA_EN;
4359 else if (attr == &dev_attr_sess_first_burst_len.attr)
4360 param = ISCSI_PARAM_FIRST_BURST;
4361 else if (attr == &dev_attr_sess_max_burst_len.attr)
4362 param = ISCSI_PARAM_MAX_BURST;
4363 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
4364 param = ISCSI_PARAM_PDU_INORDER_EN;
4365 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
4366 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
4367 else if (attr == &dev_attr_sess_erl.attr)
4368 param = ISCSI_PARAM_ERL;
4369 else if (attr == &dev_attr_sess_targetname.attr)
4370 param = ISCSI_PARAM_TARGET_NAME;
4371 else if (attr == &dev_attr_sess_tpgt.attr)
4372 param = ISCSI_PARAM_TPGT;
Mike Christie30534952012-02-27 03:08:53 -08004373 else if (attr == &dev_attr_sess_chap_in_idx.attr)
4374 param = ISCSI_PARAM_CHAP_IN_IDX;
4375 else if (attr == &dev_attr_sess_chap_out_idx.attr)
4376 param = ISCSI_PARAM_CHAP_OUT_IDX;
Mike Christie1d063c12011-07-25 13:48:43 -05004377 else if (attr == &dev_attr_sess_password.attr)
4378 param = ISCSI_PARAM_USERNAME;
4379 else if (attr == &dev_attr_sess_password_in.attr)
4380 param = ISCSI_PARAM_USERNAME_IN;
4381 else if (attr == &dev_attr_sess_username.attr)
4382 param = ISCSI_PARAM_PASSWORD;
4383 else if (attr == &dev_attr_sess_username_in.attr)
4384 param = ISCSI_PARAM_PASSWORD_IN;
4385 else if (attr == &dev_attr_sess_fast_abort.attr)
4386 param = ISCSI_PARAM_FAST_ABORT;
4387 else if (attr == &dev_attr_sess_abort_tmo.attr)
4388 param = ISCSI_PARAM_ABORT_TMO;
4389 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
4390 param = ISCSI_PARAM_LU_RESET_TMO;
4391 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
4392 param = ISCSI_PARAM_TGT_RESET_TMO;
4393 else if (attr == &dev_attr_sess_ifacename.attr)
4394 param = ISCSI_PARAM_IFACE_NAME;
4395 else if (attr == &dev_attr_sess_initiatorname.attr)
4396 param = ISCSI_PARAM_INITIATOR_NAME;
4397 else if (attr == &dev_attr_sess_targetalias.attr)
4398 param = ISCSI_PARAM_TARGET_ALIAS;
Eddie Wai3b9373e2013-06-20 10:21:26 -07004399 else if (attr == &dev_attr_sess_boot_root.attr)
4400 param = ISCSI_PARAM_BOOT_ROOT;
4401 else if (attr == &dev_attr_sess_boot_nic.attr)
4402 param = ISCSI_PARAM_BOOT_NIC;
4403 else if (attr == &dev_attr_sess_boot_target.attr)
4404 param = ISCSI_PARAM_BOOT_TARGET;
Adheer Chandravanshiae542ed2013-07-01 05:54:11 -04004405 else if (attr == &dev_attr_sess_auto_snd_tgt_disable.attr)
4406 param = ISCSI_PARAM_AUTO_SND_TGT_DISABLE;
4407 else if (attr == &dev_attr_sess_discovery_session.attr)
4408 param = ISCSI_PARAM_DISCOVERY_SESS;
4409 else if (attr == &dev_attr_sess_portal_type.attr)
4410 param = ISCSI_PARAM_PORTAL_TYPE;
4411 else if (attr == &dev_attr_sess_chap_auth.attr)
4412 param = ISCSI_PARAM_CHAP_AUTH_EN;
4413 else if (attr == &dev_attr_sess_discovery_logout.attr)
4414 param = ISCSI_PARAM_DISCOVERY_LOGOUT_EN;
4415 else if (attr == &dev_attr_sess_bidi_chap.attr)
4416 param = ISCSI_PARAM_BIDI_CHAP_EN;
4417 else if (attr == &dev_attr_sess_discovery_auth_optional.attr)
4418 param = ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL;
4419 else if (attr == &dev_attr_sess_def_time2wait.attr)
4420 param = ISCSI_PARAM_DEF_TIME2WAIT;
4421 else if (attr == &dev_attr_sess_def_time2retain.attr)
4422 param = ISCSI_PARAM_DEF_TIME2RETAIN;
4423 else if (attr == &dev_attr_sess_isid.attr)
4424 param = ISCSI_PARAM_ISID;
4425 else if (attr == &dev_attr_sess_tsid.attr)
4426 param = ISCSI_PARAM_TSID;
4427 else if (attr == &dev_attr_sess_def_taskmgmt_tmo.attr)
4428 param = ISCSI_PARAM_DEF_TASKMGMT_TMO;
4429 else if (attr == &dev_attr_sess_discovery_parent_idx.attr)
4430 param = ISCSI_PARAM_DISCOVERY_PARENT_IDX;
4431 else if (attr == &dev_attr_sess_discovery_parent_type.attr)
4432 param = ISCSI_PARAM_DISCOVERY_PARENT_TYPE;
Mike Christie1d063c12011-07-25 13:48:43 -05004433 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
4434 return S_IRUGO | S_IWUSR;
4435 else if (attr == &dev_attr_priv_sess_state.attr)
4436 return S_IRUGO;
Mike Christie0c70d842011-12-05 16:44:01 -06004437 else if (attr == &dev_attr_priv_sess_creator.attr)
4438 return S_IRUGO;
Manish Rangankar930a9f02012-11-23 06:58:41 -05004439 else if (attr == &dev_attr_priv_sess_target_id.attr)
4440 return S_IRUGO;
Mike Christie1d063c12011-07-25 13:48:43 -05004441 else {
4442 WARN_ONCE(1, "Invalid session attr");
4443 return 0;
4444 }
4445
4446 return t->attr_is_visible(ISCSI_PARAM, param);
4447}
4448
4449static struct attribute_group iscsi_session_group = {
4450 .attrs = iscsi_session_attrs,
4451 .is_visible = iscsi_session_attr_is_visible,
4452};
4453
Mike Christie1819dc82007-05-30 12:57:08 -05004454/*
4455 * iSCSI host attrs
4456 */
4457#define iscsi_host_attr_show(param) \
4458static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01004459show_host_param_##param(struct device *dev, \
4460 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05004461{ \
Tony Jonesee959b02008-02-22 00:13:36 +01004462 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05004463 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
4464 return priv->iscsi_transport->get_host_param(shost, param, buf); \
4465}
4466
4467#define iscsi_host_attr(field, param) \
4468 iscsi_host_attr_show(param) \
4469static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
4470 NULL);
4471
Mike Christied8196ed2007-05-30 12:57:25 -05004472iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05004473iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05004474iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05004475iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08004476iscsi_host_attr(port_state, ISCSI_HOST_PARAM_PORT_STATE);
4477iscsi_host_attr(port_speed, ISCSI_HOST_PARAM_PORT_SPEED);
Mike Christie1819dc82007-05-30 12:57:08 -05004478
Mike Christief27fb2e2011-07-25 13:48:45 -05004479static struct attribute *iscsi_host_attrs[] = {
4480 &dev_attr_host_netdev.attr,
4481 &dev_attr_host_hwaddress.attr,
4482 &dev_attr_host_ipaddress.attr,
4483 &dev_attr_host_initiatorname.attr,
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08004484 &dev_attr_host_port_state.attr,
4485 &dev_attr_host_port_speed.attr,
Mike Christief27fb2e2011-07-25 13:48:45 -05004486 NULL,
4487};
4488
Al Viro587a1f12011-07-23 23:11:19 -04004489static umode_t iscsi_host_attr_is_visible(struct kobject *kobj,
Mike Christief27fb2e2011-07-25 13:48:45 -05004490 struct attribute *attr, int i)
4491{
4492 struct device *cdev = container_of(kobj, struct device, kobj);
4493 struct Scsi_Host *shost = transport_class_to_shost(cdev);
4494 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
4495 int param;
4496
4497 if (attr == &dev_attr_host_netdev.attr)
4498 param = ISCSI_HOST_PARAM_NETDEV_NAME;
4499 else if (attr == &dev_attr_host_hwaddress.attr)
4500 param = ISCSI_HOST_PARAM_HWADDRESS;
4501 else if (attr == &dev_attr_host_ipaddress.attr)
4502 param = ISCSI_HOST_PARAM_IPADDRESS;
4503 else if (attr == &dev_attr_host_initiatorname.attr)
4504 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08004505 else if (attr == &dev_attr_host_port_state.attr)
4506 param = ISCSI_HOST_PARAM_PORT_STATE;
4507 else if (attr == &dev_attr_host_port_speed.attr)
4508 param = ISCSI_HOST_PARAM_PORT_SPEED;
Mike Christief27fb2e2011-07-25 13:48:45 -05004509 else {
4510 WARN_ONCE(1, "Invalid host attr");
4511 return 0;
4512 }
4513
4514 return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
4515}
4516
4517static struct attribute_group iscsi_host_group = {
4518 .attrs = iscsi_host_attrs,
4519 .is_visible = iscsi_host_attr_is_visible,
4520};
Mike Christie1819dc82007-05-30 12:57:08 -05004521
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08004522/* convert iscsi_port_speed values to ascii string name */
4523static const struct {
4524 enum iscsi_port_speed value;
4525 char *name;
4526} iscsi_port_speed_names[] = {
4527 {ISCSI_PORT_SPEED_UNKNOWN, "Unknown" },
4528 {ISCSI_PORT_SPEED_10MBPS, "10 Mbps" },
4529 {ISCSI_PORT_SPEED_100MBPS, "100 Mbps" },
4530 {ISCSI_PORT_SPEED_1GBPS, "1 Gbps" },
4531 {ISCSI_PORT_SPEED_10GBPS, "10 Gbps" },
Jitendra Bhivaref9e4fa42016-01-20 14:10:57 +05304532 {ISCSI_PORT_SPEED_25GBPS, "25 Gbps" },
4533 {ISCSI_PORT_SPEED_40GBPS, "40 Gbps" },
Vikas Chaudharyaeddde22012-01-19 03:06:55 -08004534};
4535
4536char *iscsi_get_port_speed_name(struct Scsi_Host *shost)
4537{
4538 int i;
4539 char *speed = "Unknown!";
4540 struct iscsi_cls_host *ihost = shost->shost_data;
4541 uint32_t port_speed = ihost->port_speed;
4542
4543 for (i = 0; i < ARRAY_SIZE(iscsi_port_speed_names); i++) {
4544 if (iscsi_port_speed_names[i].value & port_speed) {
4545 speed = iscsi_port_speed_names[i].name;
4546 break;
4547 }
4548 }
4549 return speed;
4550}
4551EXPORT_SYMBOL_GPL(iscsi_get_port_speed_name);
4552
4553/* convert iscsi_port_state values to ascii string name */
4554static const struct {
4555 enum iscsi_port_state value;
4556 char *name;
4557} iscsi_port_state_names[] = {
4558 {ISCSI_PORT_STATE_DOWN, "LINK DOWN" },
4559 {ISCSI_PORT_STATE_UP, "LINK UP" },
4560};
4561
4562char *iscsi_get_port_state_name(struct Scsi_Host *shost)
4563{
4564 int i;
4565 char *state = "Unknown!";
4566 struct iscsi_cls_host *ihost = shost->shost_data;
4567 uint32_t port_state = ihost->port_state;
4568
4569 for (i = 0; i < ARRAY_SIZE(iscsi_port_state_names); i++) {
4570 if (iscsi_port_state_names[i].value & port_state) {
4571 state = iscsi_port_state_names[i].name;
4572 break;
4573 }
4574 }
4575 return state;
4576}
4577EXPORT_SYMBOL_GPL(iscsi_get_port_state_name);
4578
Alex Aizman0896b752005-08-04 19:33:07 -07004579static int iscsi_session_match(struct attribute_container *cont,
4580 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581{
Mike Christie7b8631b2006-01-13 18:05:50 -06004582 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07004584 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Alex Aizman0896b752005-08-04 19:33:07 -07004586 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 return 0;
4588
Mike Christie7b8631b2006-01-13 18:05:50 -06004589 session = iscsi_dev_to_session(dev);
4590 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07004591 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 return 0;
4593
Alex Aizman0896b752005-08-04 19:33:07 -07004594 priv = to_iscsi_internal(shost->transportt);
4595 if (priv->session_cont.ac.class != &iscsi_session_class.class)
4596 return 0;
4597
4598 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599}
4600
Alex Aizman0896b752005-08-04 19:33:07 -07004601static int iscsi_conn_match(struct attribute_container *cont,
4602 struct device *dev)
4603{
Mike Christie7b8631b2006-01-13 18:05:50 -06004604 struct iscsi_cls_session *session;
4605 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07004606 struct Scsi_Host *shost;
4607 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Alex Aizman0896b752005-08-04 19:33:07 -07004609 if (!iscsi_is_conn_dev(dev))
4610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Mike Christie7b8631b2006-01-13 18:05:50 -06004612 conn = iscsi_dev_to_conn(dev);
4613 session = iscsi_dev_to_session(conn->dev.parent);
4614 shost = iscsi_session_to_shost(session);
4615
Alex Aizman0896b752005-08-04 19:33:07 -07004616 if (!shost->transportt)
4617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Alex Aizman0896b752005-08-04 19:33:07 -07004619 priv = to_iscsi_internal(shost->transportt);
4620 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
4621 return 0;
4622
4623 return &priv->conn_cont.ac == cont;
4624}
4625
Mike Christie30a6c652006-04-06 21:13:39 -05004626static int iscsi_host_match(struct attribute_container *cont,
4627 struct device *dev)
4628{
4629 struct Scsi_Host *shost;
4630 struct iscsi_internal *priv;
4631
4632 if (!scsi_is_host_device(dev))
4633 return 0;
4634
4635 shost = dev_to_shost(dev);
4636 if (!shost->transportt ||
4637 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
4638 return 0;
4639
4640 priv = to_iscsi_internal(shost->transportt);
4641 return &priv->t.host_attrs.ac == cont;
4642}
4643
Mike Christie7b8631b2006-01-13 18:05:50 -06004644struct scsi_transport_template *
4645iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07004646{
4647 struct iscsi_internal *priv;
4648 unsigned long flags;
Mike Christief27fb2e2011-07-25 13:48:45 -05004649 int err;
Alex Aizman0896b752005-08-04 19:33:07 -07004650
4651 BUG_ON(!tt);
4652
4653 priv = iscsi_if_transport_lookup(tt);
4654 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06004655 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07004656
Jes Sorensen24669f752006-01-16 10:31:18 -05004657 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07004658 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06004659 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07004660 INIT_LIST_HEAD(&priv->list);
Alex Aizman0896b752005-08-04 19:33:07 -07004661 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05004662 priv->t.user_scan = iscsi_user_scan;
Mike Christie06d25af2009-03-05 14:46:02 -06004663 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07004664
Tony Jonesee959b02008-02-22 00:13:36 +01004665 priv->dev.class = &iscsi_transport_class;
Kay Sievers71610f52008-12-03 22:41:36 +01004666 dev_set_name(&priv->dev, "%s", tt->name);
Tony Jonesee959b02008-02-22 00:13:36 +01004667 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07004668 if (err)
4669 goto free_priv;
4670
Tony Jonesee959b02008-02-22 00:13:36 +01004671 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07004672 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01004673 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07004674
Mike Christie30a6c652006-04-06 21:13:39 -05004675 /* host parameters */
Mike Christie30a6c652006-04-06 21:13:39 -05004676 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
4677 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christief27fb2e2011-07-25 13:48:45 -05004678 priv->t.host_attrs.ac.grp = &iscsi_host_group;
Mike Christie32c6e1b2008-05-21 15:53:58 -05004679 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05004680 transport_container_register(&priv->t.host_attrs);
4681
Alex Aizman0896b752005-08-04 19:33:07 -07004682 /* connection parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07004683 priv->conn_cont.ac.class = &iscsi_connection_class.class;
4684 priv->conn_cont.ac.match = iscsi_conn_match;
Mike Christie3128c6c2011-07-25 13:48:42 -05004685 priv->conn_cont.ac.grp = &iscsi_conn_group;
Alex Aizman0896b752005-08-04 19:33:07 -07004686 transport_container_register(&priv->conn_cont);
4687
Alex Aizman0896b752005-08-04 19:33:07 -07004688 /* session parameters */
Alex Aizman0896b752005-08-04 19:33:07 -07004689 priv->session_cont.ac.class = &iscsi_session_class.class;
4690 priv->session_cont.ac.match = iscsi_session_match;
Mike Christie1d063c12011-07-25 13:48:43 -05004691 priv->session_cont.ac.grp = &iscsi_session_group;
Alex Aizman0896b752005-08-04 19:33:07 -07004692 transport_container_register(&priv->session_cont);
4693
Alex Aizman0896b752005-08-04 19:33:07 -07004694 spin_lock_irqsave(&iscsi_transport_lock, flags);
4695 list_add(&priv->list, &iscsi_transports);
4696 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
Alex Aizman0896b752005-08-04 19:33:07 -07004698 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06004699 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
Tony Jonesee959b02008-02-22 00:13:36 +01004701unregister_dev:
4702 device_unregister(&priv->dev);
Mike Christied82ff9be2008-05-21 15:54:13 -05004703 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07004704free_priv:
4705 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06004706 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707}
Alex Aizman0896b752005-08-04 19:33:07 -07004708EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
Alex Aizman0896b752005-08-04 19:33:07 -07004710int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711{
Alex Aizman0896b752005-08-04 19:33:07 -07004712 struct iscsi_internal *priv;
4713 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
Alex Aizman0896b752005-08-04 19:33:07 -07004715 BUG_ON(!tt);
4716
Arjan van de Ven0b950672006-01-11 13:16:10 +01004717 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07004718
4719 priv = iscsi_if_transport_lookup(tt);
4720 BUG_ON (!priv);
4721
Alex Aizman0896b752005-08-04 19:33:07 -07004722 spin_lock_irqsave(&iscsi_transport_lock, flags);
4723 list_del(&priv->list);
4724 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
4725
4726 transport_container_unregister(&priv->conn_cont);
4727 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05004728 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07004729
Tony Jonesee959b02008-02-22 00:13:36 +01004730 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
4731 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01004732 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07004733
4734 return 0;
4735}
4736EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
4737
Fred Herardc2332b02018-11-21 12:04:43 -05004738void iscsi_dbg_trace(void (*trace)(struct device *dev, struct va_format *),
4739 struct device *dev, const char *fmt, ...)
4740{
4741 struct va_format vaf;
4742 va_list args;
4743
4744 va_start(args, fmt);
4745 vaf.fmt = fmt;
4746 vaf.va = &args;
4747 trace(dev, &vaf);
4748 va_end(args);
4749}
4750EXPORT_SYMBOL_GPL(iscsi_dbg_trace);
4751
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752static __init int iscsi_transport_init(void)
4753{
Alex Aizman0896b752005-08-04 19:33:07 -07004754 int err;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004755 struct netlink_kernel_cfg cfg = {
4756 .groups = 1,
4757 .input = iscsi_if_rx,
4758 };
Meelis Roos09492602006-12-17 12:10:26 -06004759 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05004760 ISCSI_TRANSPORT_VERSION);
4761
Mike Christie41be1442007-02-28 17:32:18 -06004762 atomic_set(&iscsi_session_nr, 0);
4763
Alex Aizman0896b752005-08-04 19:33:07 -07004764 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 if (err)
4766 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07004767
Mike Christied82ff9be2008-05-21 15:54:13 -05004768 err = class_register(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07004769 if (err)
4770 goto unregister_transport_class;
4771
Mike Christie8d079132011-07-25 13:48:40 -05004772 err = class_register(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05004773 if (err)
4774 goto unregister_endpoint_class;
4775
Mike Christie8d079132011-07-25 13:48:40 -05004776 err = transport_class_register(&iscsi_host_class);
4777 if (err)
4778 goto unregister_iface_class;
4779
Mike Christie30a6c652006-04-06 21:13:39 -05004780 err = transport_class_register(&iscsi_connection_class);
4781 if (err)
4782 goto unregister_host_class;
4783
Alex Aizman0896b752005-08-04 19:33:07 -07004784 err = transport_class_register(&iscsi_session_class);
4785 if (err)
4786 goto unregister_conn_class;
4787
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04004788 err = bus_register(&iscsi_flashnode_bus);
4789 if (err)
4790 goto unregister_session_class;
4791
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004792 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, &cfg);
Alex Aizman0896b752005-08-04 19:33:07 -07004793 if (!nls) {
4794 err = -ENOBUFS;
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04004795 goto unregister_flashnode_bus;
Alex Aizman0896b752005-08-04 19:33:07 -07004796 }
4797
Bob Liu3ce41962020-05-05 09:19:08 +08004798 iscsi_eh_timer_workq = alloc_workqueue("%s",
4799 WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND,
Bob Liu1a982622020-07-01 11:07:44 +08004800 1, "iscsi_eh");
Wei Yongjunbcab2cc2013-04-19 09:23:28 +08004801 if (!iscsi_eh_timer_workq) {
4802 err = -ENOMEM;
Mike Christied8bf5412007-12-13 12:43:27 -06004803 goto release_nls;
Wei Yongjunbcab2cc2013-04-19 09:23:28 +08004804 }
Mike Christied8bf5412007-12-13 12:43:27 -06004805
Bob Liu919a2952020-07-03 13:16:03 +08004806 iscsi_destroy_workq = alloc_workqueue("%s",
4807 WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND,
4808 1, "iscsi_destroy");
Frank Mayharcc6b32e2020-02-27 14:59:45 -05004809 if (!iscsi_destroy_workq) {
4810 err = -ENOMEM;
4811 goto destroy_wq;
4812 }
4813
Mike Christie43a145a2006-10-16 18:09:38 -04004814 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07004815
Frank Mayharcc6b32e2020-02-27 14:59:45 -05004816destroy_wq:
4817 destroy_workqueue(iscsi_eh_timer_workq);
Mike Christied8bf5412007-12-13 12:43:27 -06004818release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08004819 netlink_kernel_release(nls);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04004820unregister_flashnode_bus:
4821 bus_unregister(&iscsi_flashnode_bus);
Alex Aizman0896b752005-08-04 19:33:07 -07004822unregister_session_class:
4823 transport_class_unregister(&iscsi_session_class);
4824unregister_conn_class:
4825 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05004826unregister_host_class:
4827 transport_class_unregister(&iscsi_host_class);
Mike Christie8d079132011-07-25 13:48:40 -05004828unregister_iface_class:
4829 class_unregister(&iscsi_iface_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05004830unregister_endpoint_class:
4831 class_unregister(&iscsi_endpoint_class);
Alex Aizman0896b752005-08-04 19:33:07 -07004832unregister_transport_class:
4833 class_unregister(&iscsi_transport_class);
4834 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835}
4836
4837static void __exit iscsi_transport_exit(void)
4838{
Frank Mayharcc6b32e2020-02-27 14:59:45 -05004839 destroy_workqueue(iscsi_destroy_workq);
Mike Christied8bf5412007-12-13 12:43:27 -06004840 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08004841 netlink_kernel_release(nls);
Adheer Chandravanshic6a4bb22013-03-22 07:41:29 -04004842 bus_unregister(&iscsi_flashnode_bus);
Alex Aizman0896b752005-08-04 19:33:07 -07004843 transport_class_unregister(&iscsi_connection_class);
4844 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05004845 transport_class_unregister(&iscsi_host_class);
Mike Christied82ff9be2008-05-21 15:54:13 -05004846 class_unregister(&iscsi_endpoint_class);
Mike Christie8d079132011-07-25 13:48:40 -05004847 class_unregister(&iscsi_iface_class);
Alex Aizman0896b752005-08-04 19:33:07 -07004848 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849}
4850
4851module_init(iscsi_transport_init);
4852module_exit(iscsi_transport_exit);
4853
Alex Aizman0896b752005-08-04 19:33:07 -07004854MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
4855 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
4856 "Alex Aizman <itn780@yahoo.com>");
4857MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05004859MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
Stephen Hemminger058548a2010-12-09 09:37:56 -08004860MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);