1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * iSCSI transport class definitions
5 * Copyright (C) IBM Corporation, 2004
6 * Copyright (C) Mike Christie, 2004 - 2005
7 * Copyright (C) Dmitry Yusupov, 2004 - 2005
8 * Copyright (C) Alex Aizman, 2004 - 2005
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/bsg-lib.h>
14 #include <linux/idr.h>
16 #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>
21 #include <scsi/iscsi_if.h>
22 #include <scsi/scsi_cmnd.h>
23 #include <scsi/scsi_bsg_iscsi.h>
25 #define ISCSI_TRANSPORT_VERSION "2.0-870"
27 #define ISCSI_SEND_MAX_ALLOWED 10
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/iscsi.h>
33 * Export tracepoint symbols to be used by other modules.
35 EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_conn);
36 EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_eh);
37 EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_session);
38 EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_tcp);
39 EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_sw_tcp);
41 static int dbg_session;
42 module_param_named(debug_session, dbg_session, int,
44 MODULE_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 "
50 module_param_named(debug_conn, dbg_conn, int,
52 MODULE_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 "
57 #define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
60 iscsi_cls_session_printk(KERN_INFO, _session, \
63 iscsi_dbg_trace(trace_iscsi_dbg_trans_session, \
65 "%s " dbg_fmt, __func__, ##arg); \
68 #define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
71 iscsi_cls_conn_printk(KERN_INFO, _conn, \
74 iscsi_dbg_trace(trace_iscsi_dbg_trans_conn, \
76 "%s " dbg_fmt, __func__, ##arg); \
79 struct iscsi_internal {
80 struct scsi_transport_template t;
81 struct iscsi_transport *iscsi_transport;
82 struct list_head list;
85 struct transport_container conn_cont;
86 struct transport_container session_cont;
89 static DEFINE_IDR(iscsi_ep_idr);
90 static DEFINE_MUTEX(iscsi_ep_idr_mutex);
92 static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
94 static struct workqueue_struct *iscsi_conn_cleanup_workq;
96 static DEFINE_IDA(iscsi_sess_ida);
98 * list of registered transports and lock that must
99 * be held while accessing list. The iscsi_transport_lock must
100 * be acquired after the rx_queue_mutex.
102 static LIST_HEAD(iscsi_transports);
103 static DEFINE_SPINLOCK(iscsi_transport_lock);
105 #define to_iscsi_internal(tmpl) \
106 container_of(tmpl, struct iscsi_internal, t)
108 #define dev_to_iscsi_internal(_dev) \
109 container_of(_dev, struct iscsi_internal, dev)
111 static void iscsi_transport_release(struct device *dev)
113 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
118 * iscsi_transport_class represents the iscsi_transports that are
121 static struct class iscsi_transport_class = {
122 .name = "iscsi_transport",
123 .dev_release = iscsi_transport_release,
127 show_transport_handle(struct device *dev, struct device_attribute *attr,
130 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
132 if (!capable(CAP_SYS_ADMIN))
134 return sysfs_emit(buf, "%llu\n",
135 (unsigned long long)iscsi_handle(priv->iscsi_transport));
137 static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
139 #define show_transport_attr(name, format) \
141 show_transport_##name(struct device *dev, \
142 struct device_attribute *attr,char *buf) \
144 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
145 return sysfs_emit(buf, format"\n", priv->iscsi_transport->name);\
147 static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
149 show_transport_attr(caps, "0x%x");
151 static struct attribute *iscsi_transport_attrs[] = {
152 &dev_attr_handle.attr,
157 static struct attribute_group iscsi_transport_group = {
158 .attrs = iscsi_transport_attrs,
162 * iSCSI endpoint attrs
164 #define iscsi_dev_to_endpoint(_dev) \
165 container_of(_dev, struct iscsi_endpoint, dev)
167 #define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
168 struct device_attribute dev_attr_##_prefix##_##_name = \
169 __ATTR(_name,_mode,_show,_store)
171 static void iscsi_endpoint_release(struct device *dev)
173 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
175 mutex_lock(&iscsi_ep_idr_mutex);
176 idr_remove(&iscsi_ep_idr, ep->id);
177 mutex_unlock(&iscsi_ep_idr_mutex);
182 static struct class iscsi_endpoint_class = {
183 .name = "iscsi_endpoint",
184 .dev_release = iscsi_endpoint_release,
188 show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
190 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
191 return sysfs_emit(buf, "%d\n", ep->id);
193 static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
195 static struct attribute *iscsi_endpoint_attrs[] = {
196 &dev_attr_ep_handle.attr,
200 static struct attribute_group iscsi_endpoint_group = {
201 .attrs = iscsi_endpoint_attrs,
204 struct iscsi_endpoint *
205 iscsi_create_endpoint(int dd_size)
207 struct iscsi_endpoint *ep;
210 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
214 mutex_lock(&iscsi_ep_idr_mutex);
215 id = idr_alloc(&iscsi_ep_idr, ep, 0, -1, GFP_NOIO);
217 mutex_unlock(&iscsi_ep_idr_mutex);
218 printk(KERN_ERR "Could not allocate endpoint ID. Error %d.\n",
222 mutex_unlock(&iscsi_ep_idr_mutex);
225 ep->dev.class = &iscsi_endpoint_class;
226 dev_set_name(&ep->dev, "ep-%d", id);
227 err = device_register(&ep->dev);
231 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
236 ep->dd_data = &ep[1];
240 device_unregister(&ep->dev);
244 mutex_lock(&iscsi_ep_idr_mutex);
245 idr_remove(&iscsi_ep_idr, id);
246 mutex_unlock(&iscsi_ep_idr_mutex);
251 EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
253 void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
255 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
256 device_unregister(&ep->dev);
258 EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
260 void iscsi_put_endpoint(struct iscsi_endpoint *ep)
262 put_device(&ep->dev);
264 EXPORT_SYMBOL_GPL(iscsi_put_endpoint);
267 * iscsi_lookup_endpoint - get ep from handle
268 * @handle: endpoint handle
270 * Caller must do a iscsi_put_endpoint.
272 struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
274 struct iscsi_endpoint *ep;
276 mutex_lock(&iscsi_ep_idr_mutex);
277 ep = idr_find(&iscsi_ep_idr, handle);
281 get_device(&ep->dev);
283 mutex_unlock(&iscsi_ep_idr_mutex);
286 EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
289 * Interface to display network param to sysfs
292 static void iscsi_iface_release(struct device *dev)
294 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
295 struct device *parent = iface->dev.parent;
302 static struct class iscsi_iface_class = {
303 .name = "iscsi_iface",
304 .dev_release = iscsi_iface_release,
307 #define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
308 struct device_attribute dev_attr_##_prefix##_##_name = \
309 __ATTR(_name, _mode, _show, _store)
311 /* iface attrs show */
312 #define iscsi_iface_attr_show(type, name, param_type, param) \
314 show_##type##_##name(struct device *dev, struct device_attribute *attr, \
317 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
318 struct iscsi_transport *t = iface->transport; \
319 return t->get_iface_param(iface, param_type, param, buf); \
322 #define iscsi_iface_net_attr(type, name, param) \
323 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
324 static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
326 #define iscsi_iface_attr(type, name, param) \
327 iscsi_iface_attr_show(type, name, ISCSI_IFACE_PARAM, param) \
328 static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
330 /* generic read only ipv4 attribute */
331 iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
332 iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
333 iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
334 iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
335 iscsi_iface_net_attr(ipv4_iface, dhcp_dns_address_en,
336 ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN);
337 iscsi_iface_net_attr(ipv4_iface, dhcp_slp_da_info_en,
338 ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN);
339 iscsi_iface_net_attr(ipv4_iface, tos_en, ISCSI_NET_PARAM_IPV4_TOS_EN);
340 iscsi_iface_net_attr(ipv4_iface, tos, ISCSI_NET_PARAM_IPV4_TOS);
341 iscsi_iface_net_attr(ipv4_iface, grat_arp_en,
342 ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN);
343 iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id_en,
344 ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN);
345 iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id,
346 ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID);
347 iscsi_iface_net_attr(ipv4_iface, dhcp_req_vendor_id_en,
348 ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN);
349 iscsi_iface_net_attr(ipv4_iface, dhcp_use_vendor_id_en,
350 ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN);
351 iscsi_iface_net_attr(ipv4_iface, dhcp_vendor_id,
352 ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID);
353 iscsi_iface_net_attr(ipv4_iface, dhcp_learn_iqn_en,
354 ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN);
355 iscsi_iface_net_attr(ipv4_iface, fragment_disable,
356 ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE);
357 iscsi_iface_net_attr(ipv4_iface, incoming_forwarding_en,
358 ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN);
359 iscsi_iface_net_attr(ipv4_iface, ttl, ISCSI_NET_PARAM_IPV4_TTL);
361 /* generic read only ipv6 attribute */
362 iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
363 iscsi_iface_net_attr(ipv6_iface, link_local_addr,
364 ISCSI_NET_PARAM_IPV6_LINKLOCAL);
365 iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
366 iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
367 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
368 iscsi_iface_net_attr(ipv6_iface, link_local_autocfg,
369 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
370 iscsi_iface_net_attr(ipv6_iface, link_local_state,
371 ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE);
372 iscsi_iface_net_attr(ipv6_iface, router_state,
373 ISCSI_NET_PARAM_IPV6_ROUTER_STATE);
374 iscsi_iface_net_attr(ipv6_iface, grat_neighbor_adv_en,
375 ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN);
376 iscsi_iface_net_attr(ipv6_iface, mld_en, ISCSI_NET_PARAM_IPV6_MLD_EN);
377 iscsi_iface_net_attr(ipv6_iface, flow_label, ISCSI_NET_PARAM_IPV6_FLOW_LABEL);
378 iscsi_iface_net_attr(ipv6_iface, traffic_class,
379 ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS);
380 iscsi_iface_net_attr(ipv6_iface, hop_limit, ISCSI_NET_PARAM_IPV6_HOP_LIMIT);
381 iscsi_iface_net_attr(ipv6_iface, nd_reachable_tmo,
382 ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO);
383 iscsi_iface_net_attr(ipv6_iface, nd_rexmit_time,
384 ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME);
385 iscsi_iface_net_attr(ipv6_iface, nd_stale_tmo,
386 ISCSI_NET_PARAM_IPV6_ND_STALE_TMO);
387 iscsi_iface_net_attr(ipv6_iface, dup_addr_detect_cnt,
388 ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT);
389 iscsi_iface_net_attr(ipv6_iface, router_adv_link_mtu,
390 ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU);
392 /* common read only iface attribute */
393 iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
394 iscsi_iface_net_attr(iface, vlan_id, ISCSI_NET_PARAM_VLAN_ID);
395 iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
396 iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
397 iscsi_iface_net_attr(iface, mtu, ISCSI_NET_PARAM_MTU);
398 iscsi_iface_net_attr(iface, port, ISCSI_NET_PARAM_PORT);
399 iscsi_iface_net_attr(iface, ipaddress_state, ISCSI_NET_PARAM_IPADDR_STATE);
400 iscsi_iface_net_attr(iface, delayed_ack_en, ISCSI_NET_PARAM_DELAYED_ACK_EN);
401 iscsi_iface_net_attr(iface, tcp_nagle_disable,
402 ISCSI_NET_PARAM_TCP_NAGLE_DISABLE);
403 iscsi_iface_net_attr(iface, tcp_wsf_disable, ISCSI_NET_PARAM_TCP_WSF_DISABLE);
404 iscsi_iface_net_attr(iface, tcp_wsf, ISCSI_NET_PARAM_TCP_WSF);
405 iscsi_iface_net_attr(iface, tcp_timer_scale, ISCSI_NET_PARAM_TCP_TIMER_SCALE);
406 iscsi_iface_net_attr(iface, tcp_timestamp_en, ISCSI_NET_PARAM_TCP_TIMESTAMP_EN);
407 iscsi_iface_net_attr(iface, cache_id, ISCSI_NET_PARAM_CACHE_ID);
408 iscsi_iface_net_attr(iface, redirect_en, ISCSI_NET_PARAM_REDIRECT_EN);
410 /* common iscsi specific settings attributes */
411 iscsi_iface_attr(iface, def_taskmgmt_tmo, ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO);
412 iscsi_iface_attr(iface, header_digest, ISCSI_IFACE_PARAM_HDRDGST_EN);
413 iscsi_iface_attr(iface, data_digest, ISCSI_IFACE_PARAM_DATADGST_EN);
414 iscsi_iface_attr(iface, immediate_data, ISCSI_IFACE_PARAM_IMM_DATA_EN);
415 iscsi_iface_attr(iface, initial_r2t, ISCSI_IFACE_PARAM_INITIAL_R2T_EN);
416 iscsi_iface_attr(iface, data_seq_in_order,
417 ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN);
418 iscsi_iface_attr(iface, data_pdu_in_order, ISCSI_IFACE_PARAM_PDU_INORDER_EN);
419 iscsi_iface_attr(iface, erl, ISCSI_IFACE_PARAM_ERL);
420 iscsi_iface_attr(iface, max_recv_dlength, ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH);
421 iscsi_iface_attr(iface, first_burst_len, ISCSI_IFACE_PARAM_FIRST_BURST);
422 iscsi_iface_attr(iface, max_outstanding_r2t, ISCSI_IFACE_PARAM_MAX_R2T);
423 iscsi_iface_attr(iface, max_burst_len, ISCSI_IFACE_PARAM_MAX_BURST);
424 iscsi_iface_attr(iface, chap_auth, ISCSI_IFACE_PARAM_CHAP_AUTH_EN);
425 iscsi_iface_attr(iface, bidi_chap, ISCSI_IFACE_PARAM_BIDI_CHAP_EN);
426 iscsi_iface_attr(iface, discovery_auth_optional,
427 ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL);
428 iscsi_iface_attr(iface, discovery_logout,
429 ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN);
430 iscsi_iface_attr(iface, strict_login_comp_en,
431 ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN);
432 iscsi_iface_attr(iface, initiator_name, ISCSI_IFACE_PARAM_INITIATOR_NAME);
434 static umode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
435 struct attribute *attr, int i)
437 struct device *dev = container_of(kobj, struct device, kobj);
438 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
439 struct iscsi_transport *t = iface->transport;
442 if (attr == &dev_attr_iface_def_taskmgmt_tmo.attr)
443 param = ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO;
444 else if (attr == &dev_attr_iface_header_digest.attr)
445 param = ISCSI_IFACE_PARAM_HDRDGST_EN;
446 else if (attr == &dev_attr_iface_data_digest.attr)
447 param = ISCSI_IFACE_PARAM_DATADGST_EN;
448 else if (attr == &dev_attr_iface_immediate_data.attr)
449 param = ISCSI_IFACE_PARAM_IMM_DATA_EN;
450 else if (attr == &dev_attr_iface_initial_r2t.attr)
451 param = ISCSI_IFACE_PARAM_INITIAL_R2T_EN;
452 else if (attr == &dev_attr_iface_data_seq_in_order.attr)
453 param = ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN;
454 else if (attr == &dev_attr_iface_data_pdu_in_order.attr)
455 param = ISCSI_IFACE_PARAM_PDU_INORDER_EN;
456 else if (attr == &dev_attr_iface_erl.attr)
457 param = ISCSI_IFACE_PARAM_ERL;
458 else if (attr == &dev_attr_iface_max_recv_dlength.attr)
459 param = ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH;
460 else if (attr == &dev_attr_iface_first_burst_len.attr)
461 param = ISCSI_IFACE_PARAM_FIRST_BURST;
462 else if (attr == &dev_attr_iface_max_outstanding_r2t.attr)
463 param = ISCSI_IFACE_PARAM_MAX_R2T;
464 else if (attr == &dev_attr_iface_max_burst_len.attr)
465 param = ISCSI_IFACE_PARAM_MAX_BURST;
466 else if (attr == &dev_attr_iface_chap_auth.attr)
467 param = ISCSI_IFACE_PARAM_CHAP_AUTH_EN;
468 else if (attr == &dev_attr_iface_bidi_chap.attr)
469 param = ISCSI_IFACE_PARAM_BIDI_CHAP_EN;
470 else if (attr == &dev_attr_iface_discovery_auth_optional.attr)
471 param = ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL;
472 else if (attr == &dev_attr_iface_discovery_logout.attr)
473 param = ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN;
474 else if (attr == &dev_attr_iface_strict_login_comp_en.attr)
475 param = ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN;
476 else if (attr == &dev_attr_iface_initiator_name.attr)
477 param = ISCSI_IFACE_PARAM_INITIATOR_NAME;
480 return t->attr_is_visible(ISCSI_IFACE_PARAM, param);
482 if (attr == &dev_attr_iface_enabled.attr)
483 param = ISCSI_NET_PARAM_IFACE_ENABLE;
484 else if (attr == &dev_attr_iface_vlan_id.attr)
485 param = ISCSI_NET_PARAM_VLAN_ID;
486 else if (attr == &dev_attr_iface_vlan_priority.attr)
487 param = ISCSI_NET_PARAM_VLAN_PRIORITY;
488 else if (attr == &dev_attr_iface_vlan_enabled.attr)
489 param = ISCSI_NET_PARAM_VLAN_ENABLED;
490 else if (attr == &dev_attr_iface_mtu.attr)
491 param = ISCSI_NET_PARAM_MTU;
492 else if (attr == &dev_attr_iface_port.attr)
493 param = ISCSI_NET_PARAM_PORT;
494 else if (attr == &dev_attr_iface_ipaddress_state.attr)
495 param = ISCSI_NET_PARAM_IPADDR_STATE;
496 else if (attr == &dev_attr_iface_delayed_ack_en.attr)
497 param = ISCSI_NET_PARAM_DELAYED_ACK_EN;
498 else if (attr == &dev_attr_iface_tcp_nagle_disable.attr)
499 param = ISCSI_NET_PARAM_TCP_NAGLE_DISABLE;
500 else if (attr == &dev_attr_iface_tcp_wsf_disable.attr)
501 param = ISCSI_NET_PARAM_TCP_WSF_DISABLE;
502 else if (attr == &dev_attr_iface_tcp_wsf.attr)
503 param = ISCSI_NET_PARAM_TCP_WSF;
504 else if (attr == &dev_attr_iface_tcp_timer_scale.attr)
505 param = ISCSI_NET_PARAM_TCP_TIMER_SCALE;
506 else if (attr == &dev_attr_iface_tcp_timestamp_en.attr)
507 param = ISCSI_NET_PARAM_TCP_TIMESTAMP_EN;
508 else if (attr == &dev_attr_iface_cache_id.attr)
509 param = ISCSI_NET_PARAM_CACHE_ID;
510 else if (attr == &dev_attr_iface_redirect_en.attr)
511 param = ISCSI_NET_PARAM_REDIRECT_EN;
512 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
513 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
514 param = ISCSI_NET_PARAM_IPV4_ADDR;
515 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
516 param = ISCSI_NET_PARAM_IPV4_GW;
517 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
518 param = ISCSI_NET_PARAM_IPV4_SUBNET;
519 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
520 param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
522 &dev_attr_ipv4_iface_dhcp_dns_address_en.attr)
523 param = ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN;
525 &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr)
526 param = ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN;
527 else if (attr == &dev_attr_ipv4_iface_tos_en.attr)
528 param = ISCSI_NET_PARAM_IPV4_TOS_EN;
529 else if (attr == &dev_attr_ipv4_iface_tos.attr)
530 param = ISCSI_NET_PARAM_IPV4_TOS;
531 else if (attr == &dev_attr_ipv4_iface_grat_arp_en.attr)
532 param = ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN;
534 &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr)
535 param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN;
536 else if (attr == &dev_attr_ipv4_iface_dhcp_alt_client_id.attr)
537 param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID;
539 &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr)
540 param = ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN;
542 &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr)
543 param = ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN;
544 else if (attr == &dev_attr_ipv4_iface_dhcp_vendor_id.attr)
545 param = ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID;
547 &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr)
548 param = ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN;
550 &dev_attr_ipv4_iface_fragment_disable.attr)
551 param = ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE;
553 &dev_attr_ipv4_iface_incoming_forwarding_en.attr)
554 param = ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN;
555 else if (attr == &dev_attr_ipv4_iface_ttl.attr)
556 param = ISCSI_NET_PARAM_IPV4_TTL;
559 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
560 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
561 param = ISCSI_NET_PARAM_IPV6_ADDR;
562 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
563 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
564 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
565 param = ISCSI_NET_PARAM_IPV6_ROUTER;
566 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
567 param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
568 else if (attr == &dev_attr_ipv6_iface_link_local_autocfg.attr)
569 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
570 else if (attr == &dev_attr_ipv6_iface_link_local_state.attr)
571 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE;
572 else if (attr == &dev_attr_ipv6_iface_router_state.attr)
573 param = ISCSI_NET_PARAM_IPV6_ROUTER_STATE;
575 &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr)
576 param = ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN;
577 else if (attr == &dev_attr_ipv6_iface_mld_en.attr)
578 param = ISCSI_NET_PARAM_IPV6_MLD_EN;
579 else if (attr == &dev_attr_ipv6_iface_flow_label.attr)
580 param = ISCSI_NET_PARAM_IPV6_FLOW_LABEL;
581 else if (attr == &dev_attr_ipv6_iface_traffic_class.attr)
582 param = ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS;
583 else if (attr == &dev_attr_ipv6_iface_hop_limit.attr)
584 param = ISCSI_NET_PARAM_IPV6_HOP_LIMIT;
585 else if (attr == &dev_attr_ipv6_iface_nd_reachable_tmo.attr)
586 param = ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO;
587 else if (attr == &dev_attr_ipv6_iface_nd_rexmit_time.attr)
588 param = ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME;
589 else if (attr == &dev_attr_ipv6_iface_nd_stale_tmo.attr)
590 param = ISCSI_NET_PARAM_IPV6_ND_STALE_TMO;
591 else if (attr == &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr)
592 param = ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT;
593 else if (attr == &dev_attr_ipv6_iface_router_adv_link_mtu.attr)
594 param = ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU;
598 WARN_ONCE(1, "Invalid iface attr");
602 return t->attr_is_visible(ISCSI_NET_PARAM, param);
605 static struct attribute *iscsi_iface_attrs[] = {
606 &dev_attr_iface_enabled.attr,
607 &dev_attr_iface_vlan_id.attr,
608 &dev_attr_iface_vlan_priority.attr,
609 &dev_attr_iface_vlan_enabled.attr,
610 &dev_attr_ipv4_iface_ipaddress.attr,
611 &dev_attr_ipv4_iface_gateway.attr,
612 &dev_attr_ipv4_iface_subnet.attr,
613 &dev_attr_ipv4_iface_bootproto.attr,
614 &dev_attr_ipv6_iface_ipaddress.attr,
615 &dev_attr_ipv6_iface_link_local_addr.attr,
616 &dev_attr_ipv6_iface_router_addr.attr,
617 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
618 &dev_attr_ipv6_iface_link_local_autocfg.attr,
619 &dev_attr_iface_mtu.attr,
620 &dev_attr_iface_port.attr,
621 &dev_attr_iface_ipaddress_state.attr,
622 &dev_attr_iface_delayed_ack_en.attr,
623 &dev_attr_iface_tcp_nagle_disable.attr,
624 &dev_attr_iface_tcp_wsf_disable.attr,
625 &dev_attr_iface_tcp_wsf.attr,
626 &dev_attr_iface_tcp_timer_scale.attr,
627 &dev_attr_iface_tcp_timestamp_en.attr,
628 &dev_attr_iface_cache_id.attr,
629 &dev_attr_iface_redirect_en.attr,
630 &dev_attr_iface_def_taskmgmt_tmo.attr,
631 &dev_attr_iface_header_digest.attr,
632 &dev_attr_iface_data_digest.attr,
633 &dev_attr_iface_immediate_data.attr,
634 &dev_attr_iface_initial_r2t.attr,
635 &dev_attr_iface_data_seq_in_order.attr,
636 &dev_attr_iface_data_pdu_in_order.attr,
637 &dev_attr_iface_erl.attr,
638 &dev_attr_iface_max_recv_dlength.attr,
639 &dev_attr_iface_first_burst_len.attr,
640 &dev_attr_iface_max_outstanding_r2t.attr,
641 &dev_attr_iface_max_burst_len.attr,
642 &dev_attr_iface_chap_auth.attr,
643 &dev_attr_iface_bidi_chap.attr,
644 &dev_attr_iface_discovery_auth_optional.attr,
645 &dev_attr_iface_discovery_logout.attr,
646 &dev_attr_iface_strict_login_comp_en.attr,
647 &dev_attr_iface_initiator_name.attr,
648 &dev_attr_ipv4_iface_dhcp_dns_address_en.attr,
649 &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr,
650 &dev_attr_ipv4_iface_tos_en.attr,
651 &dev_attr_ipv4_iface_tos.attr,
652 &dev_attr_ipv4_iface_grat_arp_en.attr,
653 &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr,
654 &dev_attr_ipv4_iface_dhcp_alt_client_id.attr,
655 &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr,
656 &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr,
657 &dev_attr_ipv4_iface_dhcp_vendor_id.attr,
658 &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr,
659 &dev_attr_ipv4_iface_fragment_disable.attr,
660 &dev_attr_ipv4_iface_incoming_forwarding_en.attr,
661 &dev_attr_ipv4_iface_ttl.attr,
662 &dev_attr_ipv6_iface_link_local_state.attr,
663 &dev_attr_ipv6_iface_router_state.attr,
664 &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr,
665 &dev_attr_ipv6_iface_mld_en.attr,
666 &dev_attr_ipv6_iface_flow_label.attr,
667 &dev_attr_ipv6_iface_traffic_class.attr,
668 &dev_attr_ipv6_iface_hop_limit.attr,
669 &dev_attr_ipv6_iface_nd_reachable_tmo.attr,
670 &dev_attr_ipv6_iface_nd_rexmit_time.attr,
671 &dev_attr_ipv6_iface_nd_stale_tmo.attr,
672 &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr,
673 &dev_attr_ipv6_iface_router_adv_link_mtu.attr,
677 static struct attribute_group iscsi_iface_group = {
678 .attrs = iscsi_iface_attrs,
679 .is_visible = iscsi_iface_attr_is_visible,
682 /* convert iscsi_ipaddress_state values to ascii string name */
683 static const struct {
684 enum iscsi_ipaddress_state value;
686 } iscsi_ipaddress_state_names[] = {
687 {ISCSI_IPDDRESS_STATE_UNCONFIGURED, "Unconfigured" },
688 {ISCSI_IPDDRESS_STATE_ACQUIRING, "Acquiring" },
689 {ISCSI_IPDDRESS_STATE_TENTATIVE, "Tentative" },
690 {ISCSI_IPDDRESS_STATE_VALID, "Valid" },
691 {ISCSI_IPDDRESS_STATE_DISABLING, "Disabling" },
692 {ISCSI_IPDDRESS_STATE_INVALID, "Invalid" },
693 {ISCSI_IPDDRESS_STATE_DEPRECATED, "Deprecated" },
696 char *iscsi_get_ipaddress_state_name(enum iscsi_ipaddress_state port_state)
701 for (i = 0; i < ARRAY_SIZE(iscsi_ipaddress_state_names); i++) {
702 if (iscsi_ipaddress_state_names[i].value == port_state) {
703 state = iscsi_ipaddress_state_names[i].name;
709 EXPORT_SYMBOL_GPL(iscsi_get_ipaddress_state_name);
711 /* convert iscsi_router_state values to ascii string name */
712 static const struct {
713 enum iscsi_router_state value;
715 } iscsi_router_state_names[] = {
716 {ISCSI_ROUTER_STATE_UNKNOWN, "Unknown" },
717 {ISCSI_ROUTER_STATE_ADVERTISED, "Advertised" },
718 {ISCSI_ROUTER_STATE_MANUAL, "Manual" },
719 {ISCSI_ROUTER_STATE_STALE, "Stale" },
722 char *iscsi_get_router_state_name(enum iscsi_router_state router_state)
727 for (i = 0; i < ARRAY_SIZE(iscsi_router_state_names); i++) {
728 if (iscsi_router_state_names[i].value == router_state) {
729 state = iscsi_router_state_names[i].name;
735 EXPORT_SYMBOL_GPL(iscsi_get_router_state_name);
738 iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
739 uint32_t iface_type, uint32_t iface_num, int dd_size)
741 struct iscsi_iface *iface;
744 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
748 iface->transport = transport;
749 iface->iface_type = iface_type;
750 iface->iface_num = iface_num;
751 iface->dev.release = iscsi_iface_release;
752 iface->dev.class = &iscsi_iface_class;
753 /* parent reference released in iscsi_iface_release */
754 iface->dev.parent = get_device(&shost->shost_gendev);
755 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
756 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
759 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
762 err = device_register(&iface->dev);
766 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
771 iface->dd_data = &iface[1];
775 device_unregister(&iface->dev);
779 put_device(iface->dev.parent);
783 EXPORT_SYMBOL_GPL(iscsi_create_iface);
785 void iscsi_destroy_iface(struct iscsi_iface *iface)
787 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
788 device_unregister(&iface->dev);
790 EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
793 * Interface to display flash node params to sysfs
796 #define ISCSI_FLASHNODE_ATTR(_prefix, _name, _mode, _show, _store) \
797 struct device_attribute dev_attr_##_prefix##_##_name = \
798 __ATTR(_name, _mode, _show, _store)
800 /* flash node session attrs show */
801 #define iscsi_flashnode_sess_attr_show(type, name, param) \
803 show_##type##_##name(struct device *dev, struct device_attribute *attr, \
806 struct iscsi_bus_flash_session *fnode_sess = \
807 iscsi_dev_to_flash_session(dev);\
808 struct iscsi_transport *t = fnode_sess->transport; \
809 return t->get_flashnode_param(fnode_sess, param, buf); \
813 #define iscsi_flashnode_sess_attr(type, name, param) \
814 iscsi_flashnode_sess_attr_show(type, name, param) \
815 static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
816 show_##type##_##name, NULL);
818 /* Flash node session attributes */
820 iscsi_flashnode_sess_attr(fnode, auto_snd_tgt_disable,
821 ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE);
822 iscsi_flashnode_sess_attr(fnode, discovery_session,
823 ISCSI_FLASHNODE_DISCOVERY_SESS);
824 iscsi_flashnode_sess_attr(fnode, portal_type, ISCSI_FLASHNODE_PORTAL_TYPE);
825 iscsi_flashnode_sess_attr(fnode, entry_enable, ISCSI_FLASHNODE_ENTRY_EN);
826 iscsi_flashnode_sess_attr(fnode, immediate_data, ISCSI_FLASHNODE_IMM_DATA_EN);
827 iscsi_flashnode_sess_attr(fnode, initial_r2t, ISCSI_FLASHNODE_INITIAL_R2T_EN);
828 iscsi_flashnode_sess_attr(fnode, data_seq_in_order,
829 ISCSI_FLASHNODE_DATASEQ_INORDER);
830 iscsi_flashnode_sess_attr(fnode, data_pdu_in_order,
831 ISCSI_FLASHNODE_PDU_INORDER);
832 iscsi_flashnode_sess_attr(fnode, chap_auth, ISCSI_FLASHNODE_CHAP_AUTH_EN);
833 iscsi_flashnode_sess_attr(fnode, discovery_logout,
834 ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN);
835 iscsi_flashnode_sess_attr(fnode, bidi_chap, ISCSI_FLASHNODE_BIDI_CHAP_EN);
836 iscsi_flashnode_sess_attr(fnode, discovery_auth_optional,
837 ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL);
838 iscsi_flashnode_sess_attr(fnode, erl, ISCSI_FLASHNODE_ERL);
839 iscsi_flashnode_sess_attr(fnode, first_burst_len, ISCSI_FLASHNODE_FIRST_BURST);
840 iscsi_flashnode_sess_attr(fnode, def_time2wait, ISCSI_FLASHNODE_DEF_TIME2WAIT);
841 iscsi_flashnode_sess_attr(fnode, def_time2retain,
842 ISCSI_FLASHNODE_DEF_TIME2RETAIN);
843 iscsi_flashnode_sess_attr(fnode, max_outstanding_r2t, ISCSI_FLASHNODE_MAX_R2T);
844 iscsi_flashnode_sess_attr(fnode, isid, ISCSI_FLASHNODE_ISID);
845 iscsi_flashnode_sess_attr(fnode, tsid, ISCSI_FLASHNODE_TSID);
846 iscsi_flashnode_sess_attr(fnode, max_burst_len, ISCSI_FLASHNODE_MAX_BURST);
847 iscsi_flashnode_sess_attr(fnode, def_taskmgmt_tmo,
848 ISCSI_FLASHNODE_DEF_TASKMGMT_TMO);
849 iscsi_flashnode_sess_attr(fnode, targetalias, ISCSI_FLASHNODE_ALIAS);
850 iscsi_flashnode_sess_attr(fnode, targetname, ISCSI_FLASHNODE_NAME);
851 iscsi_flashnode_sess_attr(fnode, tpgt, ISCSI_FLASHNODE_TPGT);
852 iscsi_flashnode_sess_attr(fnode, discovery_parent_idx,
853 ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX);
854 iscsi_flashnode_sess_attr(fnode, discovery_parent_type,
855 ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE);
856 iscsi_flashnode_sess_attr(fnode, chap_in_idx, ISCSI_FLASHNODE_CHAP_IN_IDX);
857 iscsi_flashnode_sess_attr(fnode, chap_out_idx, ISCSI_FLASHNODE_CHAP_OUT_IDX);
858 iscsi_flashnode_sess_attr(fnode, username, ISCSI_FLASHNODE_USERNAME);
859 iscsi_flashnode_sess_attr(fnode, username_in, ISCSI_FLASHNODE_USERNAME_IN);
860 iscsi_flashnode_sess_attr(fnode, password, ISCSI_FLASHNODE_PASSWORD);
861 iscsi_flashnode_sess_attr(fnode, password_in, ISCSI_FLASHNODE_PASSWORD_IN);
862 iscsi_flashnode_sess_attr(fnode, is_boot_target, ISCSI_FLASHNODE_IS_BOOT_TGT);
864 static struct attribute *iscsi_flashnode_sess_attrs[] = {
865 &dev_attr_fnode_auto_snd_tgt_disable.attr,
866 &dev_attr_fnode_discovery_session.attr,
867 &dev_attr_fnode_portal_type.attr,
868 &dev_attr_fnode_entry_enable.attr,
869 &dev_attr_fnode_immediate_data.attr,
870 &dev_attr_fnode_initial_r2t.attr,
871 &dev_attr_fnode_data_seq_in_order.attr,
872 &dev_attr_fnode_data_pdu_in_order.attr,
873 &dev_attr_fnode_chap_auth.attr,
874 &dev_attr_fnode_discovery_logout.attr,
875 &dev_attr_fnode_bidi_chap.attr,
876 &dev_attr_fnode_discovery_auth_optional.attr,
877 &dev_attr_fnode_erl.attr,
878 &dev_attr_fnode_first_burst_len.attr,
879 &dev_attr_fnode_def_time2wait.attr,
880 &dev_attr_fnode_def_time2retain.attr,
881 &dev_attr_fnode_max_outstanding_r2t.attr,
882 &dev_attr_fnode_isid.attr,
883 &dev_attr_fnode_tsid.attr,
884 &dev_attr_fnode_max_burst_len.attr,
885 &dev_attr_fnode_def_taskmgmt_tmo.attr,
886 &dev_attr_fnode_targetalias.attr,
887 &dev_attr_fnode_targetname.attr,
888 &dev_attr_fnode_tpgt.attr,
889 &dev_attr_fnode_discovery_parent_idx.attr,
890 &dev_attr_fnode_discovery_parent_type.attr,
891 &dev_attr_fnode_chap_in_idx.attr,
892 &dev_attr_fnode_chap_out_idx.attr,
893 &dev_attr_fnode_username.attr,
894 &dev_attr_fnode_username_in.attr,
895 &dev_attr_fnode_password.attr,
896 &dev_attr_fnode_password_in.attr,
897 &dev_attr_fnode_is_boot_target.attr,
901 static umode_t iscsi_flashnode_sess_attr_is_visible(struct kobject *kobj,
902 struct attribute *attr,
905 struct device *dev = container_of(kobj, struct device, kobj);
906 struct iscsi_bus_flash_session *fnode_sess =
907 iscsi_dev_to_flash_session(dev);
908 struct iscsi_transport *t = fnode_sess->transport;
911 if (attr == &dev_attr_fnode_auto_snd_tgt_disable.attr) {
912 param = ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE;
913 } else if (attr == &dev_attr_fnode_discovery_session.attr) {
914 param = ISCSI_FLASHNODE_DISCOVERY_SESS;
915 } else if (attr == &dev_attr_fnode_portal_type.attr) {
916 param = ISCSI_FLASHNODE_PORTAL_TYPE;
917 } else if (attr == &dev_attr_fnode_entry_enable.attr) {
918 param = ISCSI_FLASHNODE_ENTRY_EN;
919 } else if (attr == &dev_attr_fnode_immediate_data.attr) {
920 param = ISCSI_FLASHNODE_IMM_DATA_EN;
921 } else if (attr == &dev_attr_fnode_initial_r2t.attr) {
922 param = ISCSI_FLASHNODE_INITIAL_R2T_EN;
923 } else if (attr == &dev_attr_fnode_data_seq_in_order.attr) {
924 param = ISCSI_FLASHNODE_DATASEQ_INORDER;
925 } else if (attr == &dev_attr_fnode_data_pdu_in_order.attr) {
926 param = ISCSI_FLASHNODE_PDU_INORDER;
927 } else if (attr == &dev_attr_fnode_chap_auth.attr) {
928 param = ISCSI_FLASHNODE_CHAP_AUTH_EN;
929 } else if (attr == &dev_attr_fnode_discovery_logout.attr) {
930 param = ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN;
931 } else if (attr == &dev_attr_fnode_bidi_chap.attr) {
932 param = ISCSI_FLASHNODE_BIDI_CHAP_EN;
933 } else if (attr == &dev_attr_fnode_discovery_auth_optional.attr) {
934 param = ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL;
935 } else if (attr == &dev_attr_fnode_erl.attr) {
936 param = ISCSI_FLASHNODE_ERL;
937 } else if (attr == &dev_attr_fnode_first_burst_len.attr) {
938 param = ISCSI_FLASHNODE_FIRST_BURST;
939 } else if (attr == &dev_attr_fnode_def_time2wait.attr) {
940 param = ISCSI_FLASHNODE_DEF_TIME2WAIT;
941 } else if (attr == &dev_attr_fnode_def_time2retain.attr) {
942 param = ISCSI_FLASHNODE_DEF_TIME2RETAIN;
943 } else if (attr == &dev_attr_fnode_max_outstanding_r2t.attr) {
944 param = ISCSI_FLASHNODE_MAX_R2T;
945 } else if (attr == &dev_attr_fnode_isid.attr) {
946 param = ISCSI_FLASHNODE_ISID;
947 } else if (attr == &dev_attr_fnode_tsid.attr) {
948 param = ISCSI_FLASHNODE_TSID;
949 } else if (attr == &dev_attr_fnode_max_burst_len.attr) {
950 param = ISCSI_FLASHNODE_MAX_BURST;
951 } else if (attr == &dev_attr_fnode_def_taskmgmt_tmo.attr) {
952 param = ISCSI_FLASHNODE_DEF_TASKMGMT_TMO;
953 } else if (attr == &dev_attr_fnode_targetalias.attr) {
954 param = ISCSI_FLASHNODE_ALIAS;
955 } else if (attr == &dev_attr_fnode_targetname.attr) {
956 param = ISCSI_FLASHNODE_NAME;
957 } else if (attr == &dev_attr_fnode_tpgt.attr) {
958 param = ISCSI_FLASHNODE_TPGT;
959 } else if (attr == &dev_attr_fnode_discovery_parent_idx.attr) {
960 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX;
961 } else if (attr == &dev_attr_fnode_discovery_parent_type.attr) {
962 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE;
963 } else if (attr == &dev_attr_fnode_chap_in_idx.attr) {
964 param = ISCSI_FLASHNODE_CHAP_IN_IDX;
965 } else if (attr == &dev_attr_fnode_chap_out_idx.attr) {
966 param = ISCSI_FLASHNODE_CHAP_OUT_IDX;
967 } else if (attr == &dev_attr_fnode_username.attr) {
968 param = ISCSI_FLASHNODE_USERNAME;
969 } else if (attr == &dev_attr_fnode_username_in.attr) {
970 param = ISCSI_FLASHNODE_USERNAME_IN;
971 } else if (attr == &dev_attr_fnode_password.attr) {
972 param = ISCSI_FLASHNODE_PASSWORD;
973 } else if (attr == &dev_attr_fnode_password_in.attr) {
974 param = ISCSI_FLASHNODE_PASSWORD_IN;
975 } else if (attr == &dev_attr_fnode_is_boot_target.attr) {
976 param = ISCSI_FLASHNODE_IS_BOOT_TGT;
978 WARN_ONCE(1, "Invalid flashnode session attr");
982 return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
985 static struct attribute_group iscsi_flashnode_sess_attr_group = {
986 .attrs = iscsi_flashnode_sess_attrs,
987 .is_visible = iscsi_flashnode_sess_attr_is_visible,
990 static const struct attribute_group *iscsi_flashnode_sess_attr_groups[] = {
991 &iscsi_flashnode_sess_attr_group,
995 static void iscsi_flashnode_sess_release(struct device *dev)
997 struct iscsi_bus_flash_session *fnode_sess =
998 iscsi_dev_to_flash_session(dev);
1000 kfree(fnode_sess->targetname);
1001 kfree(fnode_sess->targetalias);
1002 kfree(fnode_sess->portal_type);
1006 static const struct device_type iscsi_flashnode_sess_dev_type = {
1007 .name = "iscsi_flashnode_sess_dev_type",
1008 .groups = iscsi_flashnode_sess_attr_groups,
1009 .release = iscsi_flashnode_sess_release,
1012 /* flash node connection attrs show */
1013 #define iscsi_flashnode_conn_attr_show(type, name, param) \
1015 show_##type##_##name(struct device *dev, struct device_attribute *attr, \
1018 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);\
1019 struct iscsi_bus_flash_session *fnode_sess = \
1020 iscsi_flash_conn_to_flash_session(fnode_conn);\
1021 struct iscsi_transport *t = fnode_conn->transport; \
1022 return t->get_flashnode_param(fnode_sess, param, buf); \
1026 #define iscsi_flashnode_conn_attr(type, name, param) \
1027 iscsi_flashnode_conn_attr_show(type, name, param) \
1028 static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
1029 show_##type##_##name, NULL);
1031 /* Flash node connection attributes */
1033 iscsi_flashnode_conn_attr(fnode, is_fw_assigned_ipv6,
1034 ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6);
1035 iscsi_flashnode_conn_attr(fnode, header_digest, ISCSI_FLASHNODE_HDR_DGST_EN);
1036 iscsi_flashnode_conn_attr(fnode, data_digest, ISCSI_FLASHNODE_DATA_DGST_EN);
1037 iscsi_flashnode_conn_attr(fnode, snack_req, ISCSI_FLASHNODE_SNACK_REQ_EN);
1038 iscsi_flashnode_conn_attr(fnode, tcp_timestamp_stat,
1039 ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT);
1040 iscsi_flashnode_conn_attr(fnode, tcp_nagle_disable,
1041 ISCSI_FLASHNODE_TCP_NAGLE_DISABLE);
1042 iscsi_flashnode_conn_attr(fnode, tcp_wsf_disable,
1043 ISCSI_FLASHNODE_TCP_WSF_DISABLE);
1044 iscsi_flashnode_conn_attr(fnode, tcp_timer_scale,
1045 ISCSI_FLASHNODE_TCP_TIMER_SCALE);
1046 iscsi_flashnode_conn_attr(fnode, tcp_timestamp_enable,
1047 ISCSI_FLASHNODE_TCP_TIMESTAMP_EN);
1048 iscsi_flashnode_conn_attr(fnode, fragment_disable,
1049 ISCSI_FLASHNODE_IP_FRAG_DISABLE);
1050 iscsi_flashnode_conn_attr(fnode, keepalive_tmo, ISCSI_FLASHNODE_KEEPALIVE_TMO);
1051 iscsi_flashnode_conn_attr(fnode, port, ISCSI_FLASHNODE_PORT);
1052 iscsi_flashnode_conn_attr(fnode, ipaddress, ISCSI_FLASHNODE_IPADDR);
1053 iscsi_flashnode_conn_attr(fnode, max_recv_dlength,
1054 ISCSI_FLASHNODE_MAX_RECV_DLENGTH);
1055 iscsi_flashnode_conn_attr(fnode, max_xmit_dlength,
1056 ISCSI_FLASHNODE_MAX_XMIT_DLENGTH);
1057 iscsi_flashnode_conn_attr(fnode, local_port, ISCSI_FLASHNODE_LOCAL_PORT);
1058 iscsi_flashnode_conn_attr(fnode, ipv4_tos, ISCSI_FLASHNODE_IPV4_TOS);
1059 iscsi_flashnode_conn_attr(fnode, ipv6_traffic_class, ISCSI_FLASHNODE_IPV6_TC);
1060 iscsi_flashnode_conn_attr(fnode, ipv6_flow_label,
1061 ISCSI_FLASHNODE_IPV6_FLOW_LABEL);
1062 iscsi_flashnode_conn_attr(fnode, redirect_ipaddr,
1063 ISCSI_FLASHNODE_REDIRECT_IPADDR);
1064 iscsi_flashnode_conn_attr(fnode, max_segment_size,
1065 ISCSI_FLASHNODE_MAX_SEGMENT_SIZE);
1066 iscsi_flashnode_conn_attr(fnode, link_local_ipv6,
1067 ISCSI_FLASHNODE_LINK_LOCAL_IPV6);
1068 iscsi_flashnode_conn_attr(fnode, tcp_xmit_wsf, ISCSI_FLASHNODE_TCP_XMIT_WSF);
1069 iscsi_flashnode_conn_attr(fnode, tcp_recv_wsf, ISCSI_FLASHNODE_TCP_RECV_WSF);
1070 iscsi_flashnode_conn_attr(fnode, statsn, ISCSI_FLASHNODE_STATSN);
1071 iscsi_flashnode_conn_attr(fnode, exp_statsn, ISCSI_FLASHNODE_EXP_STATSN);
1073 static struct attribute *iscsi_flashnode_conn_attrs[] = {
1074 &dev_attr_fnode_is_fw_assigned_ipv6.attr,
1075 &dev_attr_fnode_header_digest.attr,
1076 &dev_attr_fnode_data_digest.attr,
1077 &dev_attr_fnode_snack_req.attr,
1078 &dev_attr_fnode_tcp_timestamp_stat.attr,
1079 &dev_attr_fnode_tcp_nagle_disable.attr,
1080 &dev_attr_fnode_tcp_wsf_disable.attr,
1081 &dev_attr_fnode_tcp_timer_scale.attr,
1082 &dev_attr_fnode_tcp_timestamp_enable.attr,
1083 &dev_attr_fnode_fragment_disable.attr,
1084 &dev_attr_fnode_max_recv_dlength.attr,
1085 &dev_attr_fnode_max_xmit_dlength.attr,
1086 &dev_attr_fnode_keepalive_tmo.attr,
1087 &dev_attr_fnode_port.attr,
1088 &dev_attr_fnode_ipaddress.attr,
1089 &dev_attr_fnode_redirect_ipaddr.attr,
1090 &dev_attr_fnode_max_segment_size.attr,
1091 &dev_attr_fnode_local_port.attr,
1092 &dev_attr_fnode_ipv4_tos.attr,
1093 &dev_attr_fnode_ipv6_traffic_class.attr,
1094 &dev_attr_fnode_ipv6_flow_label.attr,
1095 &dev_attr_fnode_link_local_ipv6.attr,
1096 &dev_attr_fnode_tcp_xmit_wsf.attr,
1097 &dev_attr_fnode_tcp_recv_wsf.attr,
1098 &dev_attr_fnode_statsn.attr,
1099 &dev_attr_fnode_exp_statsn.attr,
1103 static umode_t iscsi_flashnode_conn_attr_is_visible(struct kobject *kobj,
1104 struct attribute *attr,
1107 struct device *dev = container_of(kobj, struct device, kobj);
1108 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
1109 struct iscsi_transport *t = fnode_conn->transport;
1112 if (attr == &dev_attr_fnode_is_fw_assigned_ipv6.attr) {
1113 param = ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6;
1114 } else if (attr == &dev_attr_fnode_header_digest.attr) {
1115 param = ISCSI_FLASHNODE_HDR_DGST_EN;
1116 } else if (attr == &dev_attr_fnode_data_digest.attr) {
1117 param = ISCSI_FLASHNODE_DATA_DGST_EN;
1118 } else if (attr == &dev_attr_fnode_snack_req.attr) {
1119 param = ISCSI_FLASHNODE_SNACK_REQ_EN;
1120 } else if (attr == &dev_attr_fnode_tcp_timestamp_stat.attr) {
1121 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT;
1122 } else if (attr == &dev_attr_fnode_tcp_nagle_disable.attr) {
1123 param = ISCSI_FLASHNODE_TCP_NAGLE_DISABLE;
1124 } else if (attr == &dev_attr_fnode_tcp_wsf_disable.attr) {
1125 param = ISCSI_FLASHNODE_TCP_WSF_DISABLE;
1126 } else if (attr == &dev_attr_fnode_tcp_timer_scale.attr) {
1127 param = ISCSI_FLASHNODE_TCP_TIMER_SCALE;
1128 } else if (attr == &dev_attr_fnode_tcp_timestamp_enable.attr) {
1129 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_EN;
1130 } else if (attr == &dev_attr_fnode_fragment_disable.attr) {
1131 param = ISCSI_FLASHNODE_IP_FRAG_DISABLE;
1132 } else if (attr == &dev_attr_fnode_max_recv_dlength.attr) {
1133 param = ISCSI_FLASHNODE_MAX_RECV_DLENGTH;
1134 } else if (attr == &dev_attr_fnode_max_xmit_dlength.attr) {
1135 param = ISCSI_FLASHNODE_MAX_XMIT_DLENGTH;
1136 } else if (attr == &dev_attr_fnode_keepalive_tmo.attr) {
1137 param = ISCSI_FLASHNODE_KEEPALIVE_TMO;
1138 } else if (attr == &dev_attr_fnode_port.attr) {
1139 param = ISCSI_FLASHNODE_PORT;
1140 } else if (attr == &dev_attr_fnode_ipaddress.attr) {
1141 param = ISCSI_FLASHNODE_IPADDR;
1142 } else if (attr == &dev_attr_fnode_redirect_ipaddr.attr) {
1143 param = ISCSI_FLASHNODE_REDIRECT_IPADDR;
1144 } else if (attr == &dev_attr_fnode_max_segment_size.attr) {
1145 param = ISCSI_FLASHNODE_MAX_SEGMENT_SIZE;
1146 } else if (attr == &dev_attr_fnode_local_port.attr) {
1147 param = ISCSI_FLASHNODE_LOCAL_PORT;
1148 } else if (attr == &dev_attr_fnode_ipv4_tos.attr) {
1149 param = ISCSI_FLASHNODE_IPV4_TOS;
1150 } else if (attr == &dev_attr_fnode_ipv6_traffic_class.attr) {
1151 param = ISCSI_FLASHNODE_IPV6_TC;
1152 } else if (attr == &dev_attr_fnode_ipv6_flow_label.attr) {
1153 param = ISCSI_FLASHNODE_IPV6_FLOW_LABEL;
1154 } else if (attr == &dev_attr_fnode_link_local_ipv6.attr) {
1155 param = ISCSI_FLASHNODE_LINK_LOCAL_IPV6;
1156 } else if (attr == &dev_attr_fnode_tcp_xmit_wsf.attr) {
1157 param = ISCSI_FLASHNODE_TCP_XMIT_WSF;
1158 } else if (attr == &dev_attr_fnode_tcp_recv_wsf.attr) {
1159 param = ISCSI_FLASHNODE_TCP_RECV_WSF;
1160 } else if (attr == &dev_attr_fnode_statsn.attr) {
1161 param = ISCSI_FLASHNODE_STATSN;
1162 } else if (attr == &dev_attr_fnode_exp_statsn.attr) {
1163 param = ISCSI_FLASHNODE_EXP_STATSN;
1165 WARN_ONCE(1, "Invalid flashnode connection attr");
1169 return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
1172 static struct attribute_group iscsi_flashnode_conn_attr_group = {
1173 .attrs = iscsi_flashnode_conn_attrs,
1174 .is_visible = iscsi_flashnode_conn_attr_is_visible,
1177 static const struct attribute_group *iscsi_flashnode_conn_attr_groups[] = {
1178 &iscsi_flashnode_conn_attr_group,
1182 static void iscsi_flashnode_conn_release(struct device *dev)
1184 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
1186 kfree(fnode_conn->ipaddress);
1187 kfree(fnode_conn->redirect_ipaddr);
1188 kfree(fnode_conn->link_local_ipv6_addr);
1192 static const struct device_type iscsi_flashnode_conn_dev_type = {
1193 .name = "iscsi_flashnode_conn_dev_type",
1194 .groups = iscsi_flashnode_conn_attr_groups,
1195 .release = iscsi_flashnode_conn_release,
1198 static struct bus_type iscsi_flashnode_bus;
1200 int iscsi_flashnode_bus_match(struct device *dev,
1201 struct device_driver *drv)
1203 if (dev->bus == &iscsi_flashnode_bus)
1207 EXPORT_SYMBOL_GPL(iscsi_flashnode_bus_match);
1209 static struct bus_type iscsi_flashnode_bus = {
1210 .name = "iscsi_flashnode",
1211 .match = &iscsi_flashnode_bus_match,
1215 * iscsi_create_flashnode_sess - Add flashnode session entry in sysfs
1216 * @shost: pointer to host data
1217 * @index: index of flashnode to add in sysfs
1218 * @transport: pointer to transport data
1219 * @dd_size: total size to allocate
1221 * Adds a sysfs entry for the flashnode session attributes
1224 * pointer to allocated flashnode sess on success
1227 struct iscsi_bus_flash_session *
1228 iscsi_create_flashnode_sess(struct Scsi_Host *shost, int index,
1229 struct iscsi_transport *transport,
1232 struct iscsi_bus_flash_session *fnode_sess;
1235 fnode_sess = kzalloc(sizeof(*fnode_sess) + dd_size, GFP_KERNEL);
1239 fnode_sess->transport = transport;
1240 fnode_sess->target_id = index;
1241 fnode_sess->dev.type = &iscsi_flashnode_sess_dev_type;
1242 fnode_sess->dev.bus = &iscsi_flashnode_bus;
1243 fnode_sess->dev.parent = &shost->shost_gendev;
1244 dev_set_name(&fnode_sess->dev, "flashnode_sess-%u:%u",
1245 shost->host_no, index);
1247 err = device_register(&fnode_sess->dev);
1249 goto free_fnode_sess;
1252 fnode_sess->dd_data = &fnode_sess[1];
1260 EXPORT_SYMBOL_GPL(iscsi_create_flashnode_sess);
1263 * iscsi_create_flashnode_conn - Add flashnode conn entry in sysfs
1264 * @shost: pointer to host data
1265 * @fnode_sess: pointer to the parent flashnode session entry
1266 * @transport: pointer to transport data
1267 * @dd_size: total size to allocate
1269 * Adds a sysfs entry for the flashnode connection attributes
1272 * pointer to allocated flashnode conn on success
1275 struct iscsi_bus_flash_conn *
1276 iscsi_create_flashnode_conn(struct Scsi_Host *shost,
1277 struct iscsi_bus_flash_session *fnode_sess,
1278 struct iscsi_transport *transport,
1281 struct iscsi_bus_flash_conn *fnode_conn;
1284 fnode_conn = kzalloc(sizeof(*fnode_conn) + dd_size, GFP_KERNEL);
1288 fnode_conn->transport = transport;
1289 fnode_conn->dev.type = &iscsi_flashnode_conn_dev_type;
1290 fnode_conn->dev.bus = &iscsi_flashnode_bus;
1291 fnode_conn->dev.parent = &fnode_sess->dev;
1292 dev_set_name(&fnode_conn->dev, "flashnode_conn-%u:%u:0",
1293 shost->host_no, fnode_sess->target_id);
1295 err = device_register(&fnode_conn->dev);
1297 goto free_fnode_conn;
1300 fnode_conn->dd_data = &fnode_conn[1];
1308 EXPORT_SYMBOL_GPL(iscsi_create_flashnode_conn);
1311 * iscsi_is_flashnode_conn_dev - verify passed device is to be flashnode conn
1312 * @dev: device to verify
1313 * @data: pointer to data containing value to use for verification
1315 * Verifies if the passed device is flashnode conn device
1321 static int iscsi_is_flashnode_conn_dev(struct device *dev, void *data)
1323 return dev->bus == &iscsi_flashnode_bus;
1326 static int iscsi_destroy_flashnode_conn(struct iscsi_bus_flash_conn *fnode_conn)
1328 device_unregister(&fnode_conn->dev);
1332 static int flashnode_match_index(struct device *dev, void *data)
1334 struct iscsi_bus_flash_session *fnode_sess = NULL;
1337 if (!iscsi_flashnode_bus_match(dev, NULL))
1338 goto exit_match_index;
1340 fnode_sess = iscsi_dev_to_flash_session(dev);
1341 ret = (fnode_sess->target_id == *((int *)data)) ? 1 : 0;
1348 * iscsi_get_flashnode_by_index -finds flashnode session entry by index
1349 * @shost: pointer to host data
1350 * @idx: index to match
1352 * Finds the flashnode session object for the passed index
1355 * pointer to found flashnode session object on success
1358 static struct iscsi_bus_flash_session *
1359 iscsi_get_flashnode_by_index(struct Scsi_Host *shost, uint32_t idx)
1361 struct iscsi_bus_flash_session *fnode_sess = NULL;
1364 dev = device_find_child(&shost->shost_gendev, &idx,
1365 flashnode_match_index);
1367 fnode_sess = iscsi_dev_to_flash_session(dev);
1373 * iscsi_find_flashnode_sess - finds flashnode session entry
1374 * @shost: pointer to host data
1375 * @data: pointer to data containing value to use for comparison
1376 * @fn: function pointer that does actual comparison
1378 * Finds the flashnode session object comparing the data passed using logic
1379 * defined in passed function pointer
1382 * pointer to found flashnode session device object on success
1386 iscsi_find_flashnode_sess(struct Scsi_Host *shost, void *data,
1387 int (*fn)(struct device *dev, void *data))
1389 return device_find_child(&shost->shost_gendev, data, fn);
1391 EXPORT_SYMBOL_GPL(iscsi_find_flashnode_sess);
1394 * iscsi_find_flashnode_conn - finds flashnode connection entry
1395 * @fnode_sess: pointer to parent flashnode session entry
1397 * Finds the flashnode connection object comparing the data passed using logic
1398 * defined in passed function pointer
1401 * pointer to found flashnode connection device object on success
1405 iscsi_find_flashnode_conn(struct iscsi_bus_flash_session *fnode_sess)
1407 return device_find_child(&fnode_sess->dev, NULL,
1408 iscsi_is_flashnode_conn_dev);
1410 EXPORT_SYMBOL_GPL(iscsi_find_flashnode_conn);
1412 static int iscsi_iter_destroy_flashnode_conn_fn(struct device *dev, void *data)
1414 if (!iscsi_is_flashnode_conn_dev(dev, NULL))
1417 return iscsi_destroy_flashnode_conn(iscsi_dev_to_flash_conn(dev));
1421 * iscsi_destroy_flashnode_sess - destroy flashnode session entry
1422 * @fnode_sess: pointer to flashnode session entry to be destroyed
1424 * Deletes the flashnode session entry and all children flashnode connection
1425 * entries from sysfs
1427 void iscsi_destroy_flashnode_sess(struct iscsi_bus_flash_session *fnode_sess)
1431 err = device_for_each_child(&fnode_sess->dev, NULL,
1432 iscsi_iter_destroy_flashnode_conn_fn);
1434 pr_err("Could not delete all connections for %s. Error %d.\n",
1435 fnode_sess->dev.kobj.name, err);
1437 device_unregister(&fnode_sess->dev);
1439 EXPORT_SYMBOL_GPL(iscsi_destroy_flashnode_sess);
1441 static int iscsi_iter_destroy_flashnode_fn(struct device *dev, void *data)
1443 if (!iscsi_flashnode_bus_match(dev, NULL))
1446 iscsi_destroy_flashnode_sess(iscsi_dev_to_flash_session(dev));
1451 * iscsi_destroy_all_flashnode - destroy all flashnode session entries
1452 * @shost: pointer to host data
1454 * Destroys all the flashnode session entries and all corresponding children
1455 * flashnode connection entries from sysfs
1457 void iscsi_destroy_all_flashnode(struct Scsi_Host *shost)
1459 device_for_each_child(&shost->shost_gendev, NULL,
1460 iscsi_iter_destroy_flashnode_fn);
1462 EXPORT_SYMBOL_GPL(iscsi_destroy_all_flashnode);
1468 * iscsi_bsg_host_dispatch - Dispatch command to LLD.
1469 * @job: bsg job to be processed
1471 static int iscsi_bsg_host_dispatch(struct bsg_job *job)
1473 struct Scsi_Host *shost = iscsi_job_to_shost(job);
1474 struct iscsi_bsg_request *req = job->request;
1475 struct iscsi_bsg_reply *reply = job->reply;
1476 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1477 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
1480 /* check if we have the msgcode value at least */
1481 if (job->request_len < sizeof(uint32_t)) {
1486 /* Validate the host command */
1487 switch (req->msgcode) {
1488 case ISCSI_BSG_HST_VENDOR:
1489 cmdlen += sizeof(struct iscsi_bsg_host_vendor);
1490 if ((shost->hostt->vendor_id == 0L) ||
1491 (req->rqst_data.h_vendor.vendor_id !=
1492 shost->hostt->vendor_id)) {
1502 /* check if we really have all the request data needed */
1503 if (job->request_len < cmdlen) {
1508 ret = i->iscsi_transport->bsg_request(job);
1513 /* return the errno failure code as the only status */
1514 BUG_ON(job->reply_len < sizeof(uint32_t));
1515 reply->reply_payload_rcv_len = 0;
1516 reply->result = ret;
1517 job->reply_len = sizeof(uint32_t);
1518 bsg_job_done(job, ret, 0);
1523 * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
1524 * @shost: shost for iscsi_host
1525 * @ihost: iscsi_cls_host adding the structures to
1528 iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
1530 struct device *dev = &shost->shost_gendev;
1531 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1532 struct request_queue *q;
1535 if (!i->iscsi_transport->bsg_request)
1538 snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
1539 q = bsg_setup_queue(dev, bsg_name, iscsi_bsg_host_dispatch, NULL, 0);
1541 shost_printk(KERN_ERR, shost, "bsg interface failed to "
1542 "initialize - no request queue\n");
1545 __scsi_init_queue(shost, q);
1551 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
1552 struct device *cdev)
1554 struct Scsi_Host *shost = dev_to_shost(dev);
1555 struct iscsi_cls_host *ihost = shost->shost_data;
1557 memset(ihost, 0, sizeof(*ihost));
1558 mutex_init(&ihost->mutex);
1560 iscsi_bsg_host_add(shost, ihost);
1561 /* ignore any bsg add error - we just can't do sgio */
1566 static int iscsi_remove_host(struct transport_container *tc,
1567 struct device *dev, struct device *cdev)
1569 struct Scsi_Host *shost = dev_to_shost(dev);
1570 struct iscsi_cls_host *ihost = shost->shost_data;
1572 bsg_remove_queue(ihost->bsg_q);
1576 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
1582 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
1588 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
1594 static struct sock *nls;
1595 static DEFINE_MUTEX(rx_queue_mutex);
1597 static LIST_HEAD(sesslist);
1598 static DEFINE_SPINLOCK(sesslock);
1599 static LIST_HEAD(connlist);
1600 static LIST_HEAD(connlist_err);
1601 static DEFINE_SPINLOCK(connlock);
1603 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
1605 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
1610 * Returns the matching session to a given sid
1612 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
1614 unsigned long flags;
1615 struct iscsi_cls_session *sess;
1617 spin_lock_irqsave(&sesslock, flags);
1618 list_for_each_entry(sess, &sesslist, sess_list) {
1619 if (sess->sid == sid) {
1620 spin_unlock_irqrestore(&sesslock, flags);
1624 spin_unlock_irqrestore(&sesslock, flags);
1629 * Returns the matching connection to a given sid / cid tuple
1631 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
1633 unsigned long flags;
1634 struct iscsi_cls_conn *conn;
1636 spin_lock_irqsave(&connlock, flags);
1637 list_for_each_entry(conn, &connlist, conn_list) {
1638 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
1639 spin_unlock_irqrestore(&connlock, flags);
1643 spin_unlock_irqrestore(&connlock, flags);
1648 * The following functions can be used by LLDs that allocate
1649 * their own scsi_hosts or by software iscsi LLDs
1654 } iscsi_session_state_names[] = {
1655 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
1656 { ISCSI_SESSION_FAILED, "FAILED" },
1657 { ISCSI_SESSION_FREE, "FREE" },
1660 static const char *iscsi_session_state_name(int state)
1665 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
1666 if (iscsi_session_state_names[i].value == state) {
1667 name = iscsi_session_state_names[i].name;
1674 int iscsi_session_chkready(struct iscsi_cls_session *session)
1678 switch (session->state) {
1679 case ISCSI_SESSION_LOGGED_IN:
1682 case ISCSI_SESSION_FAILED:
1683 err = DID_IMM_RETRY << 16;
1685 case ISCSI_SESSION_FREE:
1686 err = DID_TRANSPORT_FAILFAST << 16;
1689 err = DID_NO_CONNECT << 16;
1694 EXPORT_SYMBOL_GPL(iscsi_session_chkready);
1696 int iscsi_is_session_online(struct iscsi_cls_session *session)
1698 unsigned long flags;
1701 spin_lock_irqsave(&session->lock, flags);
1702 if (session->state == ISCSI_SESSION_LOGGED_IN)
1704 spin_unlock_irqrestore(&session->lock, flags);
1707 EXPORT_SYMBOL_GPL(iscsi_is_session_online);
1709 static void iscsi_session_release(struct device *dev)
1711 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
1712 struct Scsi_Host *shost;
1714 shost = iscsi_session_to_shost(session);
1715 scsi_host_put(shost);
1716 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
1720 int iscsi_is_session_dev(const struct device *dev)
1722 return dev->release == iscsi_session_release;
1724 EXPORT_SYMBOL_GPL(iscsi_is_session_dev);
1726 static int iscsi_iter_session_fn(struct device *dev, void *data)
1728 void (* fn) (struct iscsi_cls_session *) = data;
1730 if (!iscsi_is_session_dev(dev))
1732 fn(iscsi_dev_to_session(dev));
1736 void iscsi_host_for_each_session(struct Scsi_Host *shost,
1737 void (*fn)(struct iscsi_cls_session *))
1739 device_for_each_child(&shost->shost_gendev, fn,
1740 iscsi_iter_session_fn);
1742 EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
1744 struct iscsi_scan_data {
1745 unsigned int channel;
1748 enum scsi_scan_mode rescan;
1751 static int iscsi_user_scan_session(struct device *dev, void *data)
1753 struct iscsi_scan_data *scan_data = data;
1754 struct iscsi_cls_session *session;
1755 struct Scsi_Host *shost;
1756 struct iscsi_cls_host *ihost;
1757 unsigned long flags;
1760 if (!iscsi_is_session_dev(dev))
1763 session = iscsi_dev_to_session(dev);
1765 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
1767 shost = iscsi_session_to_shost(session);
1768 ihost = shost->shost_data;
1770 mutex_lock(&ihost->mutex);
1771 spin_lock_irqsave(&session->lock, flags);
1772 if (session->state != ISCSI_SESSION_LOGGED_IN) {
1773 spin_unlock_irqrestore(&session->lock, flags);
1774 goto user_scan_exit;
1776 id = session->target_id;
1777 spin_unlock_irqrestore(&session->lock, flags);
1779 if (id != ISCSI_MAX_TARGET) {
1780 if ((scan_data->channel == SCAN_WILD_CARD ||
1781 scan_data->channel == 0) &&
1782 (scan_data->id == SCAN_WILD_CARD ||
1783 scan_data->id == id))
1784 scsi_scan_target(&session->dev, 0, id,
1785 scan_data->lun, scan_data->rescan);
1789 mutex_unlock(&ihost->mutex);
1790 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
1794 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
1797 struct iscsi_scan_data scan_data;
1799 scan_data.channel = channel;
1801 scan_data.lun = lun;
1802 scan_data.rescan = SCSI_SCAN_MANUAL;
1804 return device_for_each_child(&shost->shost_gendev, &scan_data,
1805 iscsi_user_scan_session);
1808 static void iscsi_scan_session(struct work_struct *work)
1810 struct iscsi_cls_session *session =
1811 container_of(work, struct iscsi_cls_session, scan_work);
1812 struct iscsi_scan_data scan_data;
1814 scan_data.channel = 0;
1815 scan_data.id = SCAN_WILD_CARD;
1816 scan_data.lun = SCAN_WILD_CARD;
1817 scan_data.rescan = SCSI_SCAN_RESCAN;
1819 iscsi_user_scan_session(&session->dev, &scan_data);
1823 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
1824 * @cmd: scsi cmd passed to scsi eh handler
1826 * If the session is down this function will wait for the recovery
1827 * timer to fire or for the session to be logged back in. If the
1828 * recovery timer fires then FAST_IO_FAIL is returned. The caller
1829 * should pass this error value to the scsi eh.
1831 int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
1833 struct iscsi_cls_session *session =
1834 starget_to_session(scsi_target(cmd->device));
1835 unsigned long flags;
1838 spin_lock_irqsave(&session->lock, flags);
1839 while (session->state != ISCSI_SESSION_LOGGED_IN) {
1840 if (session->state == ISCSI_SESSION_FREE) {
1844 spin_unlock_irqrestore(&session->lock, flags);
1846 spin_lock_irqsave(&session->lock, flags);
1848 spin_unlock_irqrestore(&session->lock, flags);
1851 EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
1853 static void session_recovery_timedout(struct work_struct *work)
1855 struct iscsi_cls_session *session =
1856 container_of(work, struct iscsi_cls_session,
1857 recovery_work.work);
1858 unsigned long flags;
1860 iscsi_cls_session_printk(KERN_INFO, session,
1861 "session recovery timed out after %d secs\n",
1862 session->recovery_tmo);
1864 spin_lock_irqsave(&session->lock, flags);
1865 switch (session->state) {
1866 case ISCSI_SESSION_FAILED:
1867 session->state = ISCSI_SESSION_FREE;
1869 case ISCSI_SESSION_LOGGED_IN:
1870 case ISCSI_SESSION_FREE:
1871 /* we raced with the unblock's flush */
1872 spin_unlock_irqrestore(&session->lock, flags);
1875 spin_unlock_irqrestore(&session->lock, flags);
1877 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
1878 scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
1879 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking SCSI target\n");
1881 if (session->transport->session_recovery_timedout)
1882 session->transport->session_recovery_timedout(session);
1885 static void __iscsi_unblock_session(struct work_struct *work)
1887 struct iscsi_cls_session *session =
1888 container_of(work, struct iscsi_cls_session,
1890 unsigned long flags;
1892 ISCSI_DBG_TRANS_SESSION(session, "Unblocking session\n");
1894 cancel_delayed_work_sync(&session->recovery_work);
1895 spin_lock_irqsave(&session->lock, flags);
1896 session->state = ISCSI_SESSION_LOGGED_IN;
1897 spin_unlock_irqrestore(&session->lock, flags);
1899 scsi_target_unblock(&session->dev, SDEV_RUNNING);
1900 ISCSI_DBG_TRANS_SESSION(session, "Completed unblocking session\n");
1904 * iscsi_unblock_session - set a session as logged in and start IO.
1905 * @session: iscsi session
1907 * Mark a session as ready to accept IO.
1909 void iscsi_unblock_session(struct iscsi_cls_session *session)
1911 if (!cancel_work_sync(&session->block_work))
1912 cancel_delayed_work_sync(&session->recovery_work);
1914 queue_work(session->workq, &session->unblock_work);
1916 * Blocking the session can be done from any context so we only
1917 * queue the block work. Make sure the unblock work has completed
1918 * because it flushes/cancels the other works and updates the state.
1920 flush_work(&session->unblock_work);
1922 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
1924 static void __iscsi_block_session(struct work_struct *work)
1926 struct iscsi_cls_session *session =
1927 container_of(work, struct iscsi_cls_session,
1929 unsigned long flags;
1931 ISCSI_DBG_TRANS_SESSION(session, "Blocking session\n");
1932 spin_lock_irqsave(&session->lock, flags);
1933 session->state = ISCSI_SESSION_FAILED;
1934 spin_unlock_irqrestore(&session->lock, flags);
1935 scsi_target_block(&session->dev);
1936 ISCSI_DBG_TRANS_SESSION(session, "Completed SCSI target blocking\n");
1937 if (session->recovery_tmo >= 0)
1938 queue_delayed_work(session->workq,
1939 &session->recovery_work,
1940 session->recovery_tmo * HZ);
1943 void iscsi_block_session(struct iscsi_cls_session *session)
1945 queue_work(session->workq, &session->block_work);
1947 EXPORT_SYMBOL_GPL(iscsi_block_session);
1949 static void __iscsi_unbind_session(struct work_struct *work)
1951 struct iscsi_cls_session *session =
1952 container_of(work, struct iscsi_cls_session,
1954 struct Scsi_Host *shost = iscsi_session_to_shost(session);
1955 struct iscsi_cls_host *ihost = shost->shost_data;
1956 unsigned long flags;
1957 unsigned int target_id;
1959 ISCSI_DBG_TRANS_SESSION(session, "Unbinding session\n");
1961 /* Prevent new scans and make sure scanning is not in progress */
1962 mutex_lock(&ihost->mutex);
1963 spin_lock_irqsave(&session->lock, flags);
1964 if (session->target_id == ISCSI_MAX_TARGET) {
1965 spin_unlock_irqrestore(&session->lock, flags);
1966 mutex_unlock(&ihost->mutex);
1967 goto unbind_session_exit;
1970 target_id = session->target_id;
1971 session->target_id = ISCSI_MAX_TARGET;
1972 spin_unlock_irqrestore(&session->lock, flags);
1973 mutex_unlock(&ihost->mutex);
1975 scsi_remove_target(&session->dev);
1977 if (session->ida_used)
1978 ida_simple_remove(&iscsi_sess_ida, target_id);
1980 unbind_session_exit:
1981 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
1982 ISCSI_DBG_TRANS_SESSION(session, "Completed target removal\n");
1985 static void __iscsi_destroy_session(struct work_struct *work)
1987 struct iscsi_cls_session *session =
1988 container_of(work, struct iscsi_cls_session, destroy_work);
1990 session->transport->destroy_session(session);
1993 struct iscsi_cls_session *
1994 iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
1997 struct iscsi_cls_session *session;
1999 session = kzalloc(sizeof(*session) + dd_size,
2004 session->transport = transport;
2005 session->creator = -1;
2006 session->recovery_tmo = 120;
2007 session->recovery_tmo_sysfs_override = false;
2008 session->state = ISCSI_SESSION_FREE;
2009 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
2010 INIT_LIST_HEAD(&session->sess_list);
2011 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
2012 INIT_WORK(&session->block_work, __iscsi_block_session);
2013 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
2014 INIT_WORK(&session->scan_work, iscsi_scan_session);
2015 INIT_WORK(&session->destroy_work, __iscsi_destroy_session);
2016 spin_lock_init(&session->lock);
2018 /* this is released in the dev's release function */
2019 scsi_host_get(shost);
2020 session->dev.parent = &shost->shost_gendev;
2021 session->dev.release = iscsi_session_release;
2022 device_initialize(&session->dev);
2024 session->dd_data = &session[1];
2026 ISCSI_DBG_TRANS_SESSION(session, "Completed session allocation\n");
2029 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
2031 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
2033 struct Scsi_Host *shost = iscsi_session_to_shost(session);
2034 unsigned long flags;
2038 session->sid = atomic_add_return(1, &iscsi_session_nr);
2040 session->workq = alloc_workqueue("iscsi_ctrl_%d:%d",
2041 WQ_SYSFS | WQ_MEM_RECLAIM | WQ_UNBOUND, 0,
2042 shost->host_no, session->sid);
2043 if (!session->workq)
2046 if (target_id == ISCSI_MAX_TARGET) {
2047 id = ida_simple_get(&iscsi_sess_ida, 0, 0, GFP_KERNEL);
2050 iscsi_cls_session_printk(KERN_ERR, session,
2051 "Failure in Target ID Allocation\n");
2055 session->target_id = (unsigned int)id;
2056 session->ida_used = true;
2058 session->target_id = target_id;
2060 dev_set_name(&session->dev, "session%u", session->sid);
2061 err = device_add(&session->dev);
2063 iscsi_cls_session_printk(KERN_ERR, session,
2064 "could not register session's dev\n");
2067 err = transport_register_device(&session->dev);
2069 iscsi_cls_session_printk(KERN_ERR, session,
2070 "could not register transport's dev\n");
2074 spin_lock_irqsave(&sesslock, flags);
2075 list_add(&session->sess_list, &sesslist);
2076 spin_unlock_irqrestore(&sesslock, flags);
2078 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
2079 ISCSI_DBG_TRANS_SESSION(session, "Completed session adding\n");
2083 device_del(&session->dev);
2085 if (session->ida_used)
2086 ida_simple_remove(&iscsi_sess_ida, session->target_id);
2088 destroy_workqueue(session->workq);
2091 EXPORT_SYMBOL_GPL(iscsi_add_session);
2094 * iscsi_create_session - create iscsi class session
2096 * @transport: iscsi transport
2097 * @dd_size: private driver data size
2098 * @target_id: which target
2100 * This can be called from a LLD or iscsi_transport.
2102 struct iscsi_cls_session *
2103 iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
2104 int dd_size, unsigned int target_id)
2106 struct iscsi_cls_session *session;
2108 session = iscsi_alloc_session(shost, transport, dd_size);
2112 if (iscsi_add_session(session, target_id)) {
2113 iscsi_free_session(session);
2118 EXPORT_SYMBOL_GPL(iscsi_create_session);
2120 static void iscsi_conn_release(struct device *dev)
2122 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
2123 struct device *parent = conn->dev.parent;
2125 ISCSI_DBG_TRANS_CONN(conn, "Releasing conn\n");
2130 static int iscsi_is_conn_dev(const struct device *dev)
2132 return dev->release == iscsi_conn_release;
2135 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
2137 if (!iscsi_is_conn_dev(dev))
2140 iscsi_remove_conn(iscsi_dev_to_conn(dev));
2141 iscsi_put_conn(iscsi_dev_to_conn(dev));
2146 void iscsi_remove_session(struct iscsi_cls_session *session)
2148 unsigned long flags;
2151 ISCSI_DBG_TRANS_SESSION(session, "Removing session\n");
2153 spin_lock_irqsave(&sesslock, flags);
2154 if (!list_empty(&session->sess_list))
2155 list_del(&session->sess_list);
2156 spin_unlock_irqrestore(&sesslock, flags);
2158 if (!cancel_work_sync(&session->block_work))
2159 cancel_delayed_work_sync(&session->recovery_work);
2160 cancel_work_sync(&session->unblock_work);
2162 * If we are blocked let commands flow again. The lld or iscsi
2163 * layer should set up the queuecommand to fail commands.
2164 * We assume that LLD will not be calling block/unblock while
2165 * removing the session.
2167 spin_lock_irqsave(&session->lock, flags);
2168 session->state = ISCSI_SESSION_FREE;
2169 spin_unlock_irqrestore(&session->lock, flags);
2171 scsi_target_unblock(&session->dev, SDEV_TRANSPORT_OFFLINE);
2173 * qla4xxx can perform it's own scans when it runs in kernel only
2174 * mode. Make sure to flush those scans.
2176 flush_work(&session->scan_work);
2177 /* flush running unbind operations */
2178 flush_work(&session->unbind_work);
2179 __iscsi_unbind_session(&session->unbind_work);
2181 /* hw iscsi may not have removed all connections from session */
2182 err = device_for_each_child(&session->dev, NULL,
2183 iscsi_iter_destroy_conn_fn);
2185 iscsi_cls_session_printk(KERN_ERR, session,
2186 "Could not delete all connections "
2187 "for session. Error %d.\n", err);
2189 transport_unregister_device(&session->dev);
2191 destroy_workqueue(session->workq);
2193 ISCSI_DBG_TRANS_SESSION(session, "Completing session removal\n");
2194 device_del(&session->dev);
2196 EXPORT_SYMBOL_GPL(iscsi_remove_session);
2198 static void iscsi_stop_conn(struct iscsi_cls_conn *conn, int flag)
2200 ISCSI_DBG_TRANS_CONN(conn, "Stopping conn.\n");
2203 case STOP_CONN_RECOVER:
2204 WRITE_ONCE(conn->state, ISCSI_CONN_FAILED);
2206 case STOP_CONN_TERM:
2207 WRITE_ONCE(conn->state, ISCSI_CONN_DOWN);
2210 iscsi_cls_conn_printk(KERN_ERR, conn, "invalid stop flag %d\n",
2215 conn->transport->stop_conn(conn, flag);
2216 ISCSI_DBG_TRANS_CONN(conn, "Stopping conn done.\n");
2219 static void iscsi_ep_disconnect(struct iscsi_cls_conn *conn, bool is_active)
2221 struct iscsi_cls_session *session = iscsi_conn_to_session(conn);
2222 struct iscsi_endpoint *ep;
2224 ISCSI_DBG_TRANS_CONN(conn, "disconnect ep.\n");
2225 WRITE_ONCE(conn->state, ISCSI_CONN_FAILED);
2227 if (!conn->ep || !session->transport->ep_disconnect)
2233 session->transport->unbind_conn(conn, is_active);
2234 session->transport->ep_disconnect(ep);
2235 ISCSI_DBG_TRANS_CONN(conn, "disconnect ep done.\n");
2238 static void iscsi_if_disconnect_bound_ep(struct iscsi_cls_conn *conn,
2239 struct iscsi_endpoint *ep,
2242 /* Check if this was a conn error and the kernel took ownership */
2243 spin_lock_irq(&conn->lock);
2244 if (!test_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags)) {
2245 spin_unlock_irq(&conn->lock);
2246 iscsi_ep_disconnect(conn, is_active);
2248 spin_unlock_irq(&conn->lock);
2249 ISCSI_DBG_TRANS_CONN(conn, "flush kernel conn cleanup.\n");
2250 mutex_unlock(&conn->ep_mutex);
2252 flush_work(&conn->cleanup_work);
2254 * Userspace is now done with the EP so we can release the ref
2255 * iscsi_cleanup_conn_work_fn took.
2257 iscsi_put_endpoint(ep);
2258 mutex_lock(&conn->ep_mutex);
2262 static int iscsi_if_stop_conn(struct iscsi_transport *transport,
2263 struct iscsi_uevent *ev)
2265 int flag = ev->u.stop_conn.flag;
2266 struct iscsi_cls_conn *conn;
2268 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
2272 ISCSI_DBG_TRANS_CONN(conn, "iscsi if conn stop.\n");
2274 * If this is a termination we have to call stop_conn with that flag
2275 * so the correct states get set. If we haven't run the work yet try to
2276 * avoid the extra run.
2278 if (flag == STOP_CONN_TERM) {
2279 cancel_work_sync(&conn->cleanup_work);
2280 iscsi_stop_conn(conn, flag);
2283 * For offload, when iscsid is restarted it won't know about
2284 * existing endpoints so it can't do a ep_disconnect. We clean
2285 * it up here for userspace.
2287 mutex_lock(&conn->ep_mutex);
2289 iscsi_if_disconnect_bound_ep(conn, conn->ep, true);
2290 mutex_unlock(&conn->ep_mutex);
2293 * Figure out if it was the kernel or userspace initiating this.
2295 spin_lock_irq(&conn->lock);
2296 if (!test_and_set_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags)) {
2297 spin_unlock_irq(&conn->lock);
2298 iscsi_stop_conn(conn, flag);
2300 spin_unlock_irq(&conn->lock);
2301 ISCSI_DBG_TRANS_CONN(conn,
2302 "flush kernel conn cleanup.\n");
2303 flush_work(&conn->cleanup_work);
2306 * Only clear for recovery to avoid extra cleanup runs during
2309 spin_lock_irq(&conn->lock);
2310 clear_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags);
2311 spin_unlock_irq(&conn->lock);
2313 ISCSI_DBG_TRANS_CONN(conn, "iscsi if conn stop done.\n");
2317 static void iscsi_cleanup_conn_work_fn(struct work_struct *work)
2319 struct iscsi_cls_conn *conn = container_of(work, struct iscsi_cls_conn,
2321 struct iscsi_cls_session *session = iscsi_conn_to_session(conn);
2323 mutex_lock(&conn->ep_mutex);
2325 * Get a ref to the ep, so we don't release its ID until after
2326 * userspace is done referencing it in iscsi_if_disconnect_bound_ep.
2329 get_device(&conn->ep->dev);
2330 iscsi_ep_disconnect(conn, false);
2332 if (system_state != SYSTEM_RUNNING) {
2334 * If the user has set up for the session to never timeout
2335 * then hang like they wanted. For all other cases fail right
2336 * away since userspace is not going to relogin.
2338 if (session->recovery_tmo > 0)
2339 session->recovery_tmo = 0;
2342 iscsi_stop_conn(conn, STOP_CONN_RECOVER);
2343 mutex_unlock(&conn->ep_mutex);
2344 ISCSI_DBG_TRANS_CONN(conn, "cleanup done.\n");
2347 void iscsi_free_session(struct iscsi_cls_session *session)
2349 ISCSI_DBG_TRANS_SESSION(session, "Freeing session\n");
2350 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
2351 put_device(&session->dev);
2353 EXPORT_SYMBOL_GPL(iscsi_free_session);
2356 * iscsi_alloc_conn - alloc iscsi class connection
2357 * @session: iscsi cls session
2358 * @dd_size: private driver data size
2359 * @cid: connection id
2361 struct iscsi_cls_conn *
2362 iscsi_alloc_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
2364 struct iscsi_transport *transport = session->transport;
2365 struct iscsi_cls_conn *conn;
2367 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
2371 conn->dd_data = &conn[1];
2373 mutex_init(&conn->ep_mutex);
2374 spin_lock_init(&conn->lock);
2375 INIT_LIST_HEAD(&conn->conn_list);
2376 INIT_WORK(&conn->cleanup_work, iscsi_cleanup_conn_work_fn);
2377 conn->transport = transport;
2379 WRITE_ONCE(conn->state, ISCSI_CONN_DOWN);
2381 /* this is released in the dev's release function */
2382 if (!get_device(&session->dev))
2385 dev_set_name(&conn->dev, "connection%d:%u", session->sid, cid);
2386 device_initialize(&conn->dev);
2387 conn->dev.parent = &session->dev;
2388 conn->dev.release = iscsi_conn_release;
2396 EXPORT_SYMBOL_GPL(iscsi_alloc_conn);
2399 * iscsi_add_conn - add iscsi class connection
2400 * @conn: iscsi cls connection
2402 * This will expose iscsi_cls_conn to sysfs so make sure the related
2403 * resources for sysfs attributes are initialized before calling this.
2405 int iscsi_add_conn(struct iscsi_cls_conn *conn)
2408 unsigned long flags;
2409 struct iscsi_cls_session *session = iscsi_dev_to_session(conn->dev.parent);
2411 err = device_add(&conn->dev);
2413 iscsi_cls_session_printk(KERN_ERR, session,
2414 "could not register connection's dev\n");
2417 err = transport_register_device(&conn->dev);
2419 iscsi_cls_session_printk(KERN_ERR, session,
2420 "could not register transport's dev\n");
2421 device_del(&conn->dev);
2425 spin_lock_irqsave(&connlock, flags);
2426 list_add(&conn->conn_list, &connlist);
2427 spin_unlock_irqrestore(&connlock, flags);
2431 EXPORT_SYMBOL_GPL(iscsi_add_conn);
2434 * iscsi_remove_conn - remove iscsi class connection from sysfs
2435 * @conn: iscsi cls connection
2437 * Remove iscsi_cls_conn from sysfs, and wait for previous
2438 * read/write of iscsi_cls_conn's attributes in sysfs to finish.
2440 void iscsi_remove_conn(struct iscsi_cls_conn *conn)
2442 unsigned long flags;
2444 spin_lock_irqsave(&connlock, flags);
2445 list_del(&conn->conn_list);
2446 spin_unlock_irqrestore(&connlock, flags);
2448 transport_unregister_device(&conn->dev);
2449 device_del(&conn->dev);
2451 EXPORT_SYMBOL_GPL(iscsi_remove_conn);
2453 void iscsi_put_conn(struct iscsi_cls_conn *conn)
2455 put_device(&conn->dev);
2457 EXPORT_SYMBOL_GPL(iscsi_put_conn);
2459 void iscsi_get_conn(struct iscsi_cls_conn *conn)
2461 get_device(&conn->dev);
2463 EXPORT_SYMBOL_GPL(iscsi_get_conn);
2466 * iscsi interface functions
2468 static struct iscsi_internal *
2469 iscsi_if_transport_lookup(struct iscsi_transport *tt)
2471 struct iscsi_internal *priv;
2472 unsigned long flags;
2474 spin_lock_irqsave(&iscsi_transport_lock, flags);
2475 list_for_each_entry(priv, &iscsi_transports, list) {
2476 if (tt == priv->iscsi_transport) {
2477 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2481 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
2486 iscsi_multicast_skb(struct sk_buff *skb, uint32_t group, gfp_t gfp)
2488 return nlmsg_multicast(nls, skb, 0, group, gfp);
2492 iscsi_unicast_skb(struct sk_buff *skb, u32 portid)
2494 return nlmsg_unicast(nls, skb, portid);
2497 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
2498 char *data, uint32_t data_size)
2500 struct nlmsghdr *nlh;
2501 struct sk_buff *skb;
2502 struct iscsi_uevent *ev;
2504 struct iscsi_internal *priv;
2505 int len = nlmsg_total_size(sizeof(*ev) + sizeof(struct iscsi_hdr) +
2508 priv = iscsi_if_transport_lookup(conn->transport);
2512 skb = alloc_skb(len, GFP_ATOMIC);
2514 iscsi_conn_error_event(conn, ISCSI_ERR_CONN_FAILED);
2515 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
2516 "control PDU: OOM\n");
2520 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2521 ev = nlmsg_data(nlh);
2522 memset(ev, 0, sizeof(*ev));
2523 ev->transport_handle = iscsi_handle(conn->transport);
2524 ev->type = ISCSI_KEVENT_RECV_PDU;
2525 ev->r.recv_req.cid = conn->cid;
2526 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
2527 pdu = (char*)ev + sizeof(*ev);
2528 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
2529 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
2531 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2533 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
2535 int iscsi_offload_mesg(struct Scsi_Host *shost,
2536 struct iscsi_transport *transport, uint32_t type,
2537 char *data, uint16_t data_size)
2539 struct nlmsghdr *nlh;
2540 struct sk_buff *skb;
2541 struct iscsi_uevent *ev;
2542 int len = nlmsg_total_size(sizeof(*ev) + data_size);
2544 skb = alloc_skb(len, GFP_ATOMIC);
2546 printk(KERN_ERR "can not deliver iscsi offload message:OOM\n");
2550 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2551 ev = nlmsg_data(nlh);
2552 memset(ev, 0, sizeof(*ev));
2554 ev->transport_handle = iscsi_handle(transport);
2556 case ISCSI_KEVENT_PATH_REQ:
2557 ev->r.req_path.host_no = shost->host_no;
2559 case ISCSI_KEVENT_IF_DOWN:
2560 ev->r.notify_if_down.host_no = shost->host_no;
2564 memcpy((char *)ev + sizeof(*ev), data, data_size);
2566 return iscsi_multicast_skb(skb, ISCSI_NL_GRP_UIP, GFP_ATOMIC);
2568 EXPORT_SYMBOL_GPL(iscsi_offload_mesg);
2570 void iscsi_conn_error_event(struct iscsi_cls_conn *conn, enum iscsi_err error)
2572 struct nlmsghdr *nlh;
2573 struct sk_buff *skb;
2574 struct iscsi_uevent *ev;
2575 struct iscsi_internal *priv;
2576 int len = nlmsg_total_size(sizeof(*ev));
2577 unsigned long flags;
2580 spin_lock_irqsave(&conn->lock, flags);
2582 * Userspace will only do a stop call if we are at least bound. And, we
2583 * only need to do the in kernel cleanup if in the UP state so cmds can
2584 * be released to upper layers. If in other states just wait for
2585 * userspace to avoid races that can leave the cleanup_work queued.
2587 state = READ_ONCE(conn->state);
2589 case ISCSI_CONN_BOUND:
2591 if (!test_and_set_bit(ISCSI_CLS_CONN_BIT_CLEANUP,
2593 queue_work(iscsi_conn_cleanup_workq,
2594 &conn->cleanup_work);
2598 ISCSI_DBG_TRANS_CONN(conn, "Got conn error in state %d\n",
2602 spin_unlock_irqrestore(&conn->lock, flags);
2604 priv = iscsi_if_transport_lookup(conn->transport);
2608 skb = alloc_skb(len, GFP_ATOMIC);
2610 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2611 "conn error (%d)\n", error);
2615 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2616 ev = nlmsg_data(nlh);
2617 ev->transport_handle = iscsi_handle(conn->transport);
2618 ev->type = ISCSI_KEVENT_CONN_ERROR;
2619 ev->r.connerror.error = error;
2620 ev->r.connerror.cid = conn->cid;
2621 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
2623 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2625 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
2628 EXPORT_SYMBOL_GPL(iscsi_conn_error_event);
2630 void iscsi_conn_login_event(struct iscsi_cls_conn *conn,
2631 enum iscsi_conn_state state)
2633 struct nlmsghdr *nlh;
2634 struct sk_buff *skb;
2635 struct iscsi_uevent *ev;
2636 struct iscsi_internal *priv;
2637 int len = nlmsg_total_size(sizeof(*ev));
2639 priv = iscsi_if_transport_lookup(conn->transport);
2643 skb = alloc_skb(len, GFP_ATOMIC);
2645 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
2646 "conn login (%d)\n", state);
2650 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2651 ev = nlmsg_data(nlh);
2652 ev->transport_handle = iscsi_handle(conn->transport);
2653 ev->type = ISCSI_KEVENT_CONN_LOGIN_STATE;
2654 ev->r.conn_login.state = state;
2655 ev->r.conn_login.cid = conn->cid;
2656 ev->r.conn_login.sid = iscsi_conn_get_sid(conn);
2657 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_ATOMIC);
2659 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn login (%d)\n",
2662 EXPORT_SYMBOL_GPL(iscsi_conn_login_event);
2664 void iscsi_post_host_event(uint32_t host_no, struct iscsi_transport *transport,
2665 enum iscsi_host_event_code code, uint32_t data_size,
2668 struct nlmsghdr *nlh;
2669 struct sk_buff *skb;
2670 struct iscsi_uevent *ev;
2671 int len = nlmsg_total_size(sizeof(*ev) + data_size);
2673 skb = alloc_skb(len, GFP_NOIO);
2675 printk(KERN_ERR "gracefully ignored host event (%d):%d OOM\n",
2680 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2681 ev = nlmsg_data(nlh);
2682 ev->transport_handle = iscsi_handle(transport);
2683 ev->type = ISCSI_KEVENT_HOST_EVENT;
2684 ev->r.host_event.host_no = host_no;
2685 ev->r.host_event.code = code;
2686 ev->r.host_event.data_size = data_size;
2689 memcpy((char *)ev + sizeof(*ev), data, data_size);
2691 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
2693 EXPORT_SYMBOL_GPL(iscsi_post_host_event);
2695 void iscsi_ping_comp_event(uint32_t host_no, struct iscsi_transport *transport,
2696 uint32_t status, uint32_t pid, uint32_t data_size,
2699 struct nlmsghdr *nlh;
2700 struct sk_buff *skb;
2701 struct iscsi_uevent *ev;
2702 int len = nlmsg_total_size(sizeof(*ev) + data_size);
2704 skb = alloc_skb(len, GFP_NOIO);
2706 printk(KERN_ERR "gracefully ignored ping comp: OOM\n");
2710 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2711 ev = nlmsg_data(nlh);
2712 ev->transport_handle = iscsi_handle(transport);
2713 ev->type = ISCSI_KEVENT_PING_COMP;
2714 ev->r.ping_comp.host_no = host_no;
2715 ev->r.ping_comp.status = status;
2716 ev->r.ping_comp.pid = pid;
2717 ev->r.ping_comp.data_size = data_size;
2718 memcpy((char *)ev + sizeof(*ev), data, data_size);
2720 iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_NOIO);
2722 EXPORT_SYMBOL_GPL(iscsi_ping_comp_event);
2725 iscsi_if_send_reply(u32 portid, int type, void *payload, int size)
2727 struct sk_buff *skb;
2728 struct nlmsghdr *nlh;
2729 int len = nlmsg_total_size(size);
2731 skb = alloc_skb(len, GFP_ATOMIC);
2733 printk(KERN_ERR "Could not allocate skb to send reply.\n");
2737 nlh = __nlmsg_put(skb, 0, 0, type, (len - sizeof(*nlh)), 0);
2738 memcpy(nlmsg_data(nlh), payload, size);
2739 return iscsi_unicast_skb(skb, portid);
2743 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
2745 struct iscsi_uevent *ev = nlmsg_data(nlh);
2746 struct iscsi_stats *stats;
2747 struct sk_buff *skbstat;
2748 struct iscsi_cls_conn *conn;
2749 struct nlmsghdr *nlhstat;
2750 struct iscsi_uevent *evstat;
2751 struct iscsi_internal *priv;
2752 int len = nlmsg_total_size(sizeof(*ev) +
2753 sizeof(struct iscsi_stats) +
2754 sizeof(struct iscsi_stats_custom) *
2755 ISCSI_STATS_CUSTOM_MAX);
2758 priv = iscsi_if_transport_lookup(transport);
2762 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
2769 skbstat = alloc_skb(len, GFP_ATOMIC);
2771 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
2772 "deliver stats: OOM\n");
2776 nlhstat = __nlmsg_put(skbstat, 0, 0, 0,
2777 (len - sizeof(*nlhstat)), 0);
2778 evstat = nlmsg_data(nlhstat);
2779 memset(evstat, 0, sizeof(*evstat));
2780 evstat->transport_handle = iscsi_handle(conn->transport);
2781 evstat->type = nlh->nlmsg_type;
2782 evstat->u.get_stats.cid =
2783 ev->u.get_stats.cid;
2784 evstat->u.get_stats.sid =
2785 ev->u.get_stats.sid;
2786 stats = (struct iscsi_stats *)
2787 ((char*)evstat + sizeof(*evstat));
2788 memset(stats, 0, sizeof(*stats));
2790 transport->get_stats(conn, stats);
2791 actual_size = nlmsg_total_size(sizeof(struct iscsi_uevent) +
2792 sizeof(struct iscsi_stats) +
2793 sizeof(struct iscsi_stats_custom) *
2794 stats->custom_length);
2795 actual_size -= sizeof(*nlhstat);
2796 actual_size = nlmsg_msg_size(actual_size);
2797 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
2798 nlhstat->nlmsg_len = actual_size;
2800 err = iscsi_multicast_skb(skbstat, ISCSI_NL_GRP_ISCSID,
2802 } while (err < 0 && err != -ECONNREFUSED);
2808 * iscsi_session_event - send session destr. completion event
2809 * @session: iscsi class session
2810 * @event: type of event
2812 int iscsi_session_event(struct iscsi_cls_session *session,
2813 enum iscsi_uevent_e event)
2815 struct iscsi_internal *priv;
2816 struct Scsi_Host *shost;
2817 struct iscsi_uevent *ev;
2818 struct sk_buff *skb;
2819 struct nlmsghdr *nlh;
2820 int rc, len = nlmsg_total_size(sizeof(*ev));
2822 priv = iscsi_if_transport_lookup(session->transport);
2825 shost = iscsi_session_to_shost(session);
2827 skb = alloc_skb(len, GFP_KERNEL);
2829 iscsi_cls_session_printk(KERN_ERR, session,
2830 "Cannot notify userspace of session "
2831 "event %u\n", event);
2835 nlh = __nlmsg_put(skb, 0, 0, 0, (len - sizeof(*nlh)), 0);
2836 ev = nlmsg_data(nlh);
2837 ev->transport_handle = iscsi_handle(session->transport);
2841 case ISCSI_KEVENT_DESTROY_SESSION:
2842 ev->r.d_session.host_no = shost->host_no;
2843 ev->r.d_session.sid = session->sid;
2845 case ISCSI_KEVENT_CREATE_SESSION:
2846 ev->r.c_session_ret.host_no = shost->host_no;
2847 ev->r.c_session_ret.sid = session->sid;
2849 case ISCSI_KEVENT_UNBIND_SESSION:
2850 ev->r.unbind_session.host_no = shost->host_no;
2851 ev->r.unbind_session.sid = session->sid;
2854 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
2861 * this will occur if the daemon is not up, so we just warn
2862 * the user and when the daemon is restarted it will handle it
2864 rc = iscsi_multicast_skb(skb, ISCSI_NL_GRP_ISCSID, GFP_KERNEL);
2866 iscsi_cls_session_printk(KERN_ERR, session,
2867 "Cannot notify userspace of session "
2868 "event %u. Check iscsi daemon\n",
2871 ISCSI_DBG_TRANS_SESSION(session, "Completed handling event %d rc %d\n",
2875 EXPORT_SYMBOL_GPL(iscsi_session_event);
2878 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_endpoint *ep,
2879 struct iscsi_uevent *ev, pid_t pid,
2880 uint32_t initial_cmdsn, uint16_t cmds_max,
2881 uint16_t queue_depth)
2883 struct iscsi_transport *transport = priv->iscsi_transport;
2884 struct iscsi_cls_session *session;
2885 struct Scsi_Host *shost;
2887 session = transport->create_session(ep, cmds_max, queue_depth,
2892 session->creator = pid;
2893 shost = iscsi_session_to_shost(session);
2894 ev->r.c_session_ret.host_no = shost->host_no;
2895 ev->r.c_session_ret.sid = session->sid;
2896 ISCSI_DBG_TRANS_SESSION(session,
2897 "Completed creating transport session\n");
2902 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2904 struct iscsi_cls_conn *conn;
2905 struct iscsi_cls_session *session;
2907 session = iscsi_session_lookup(ev->u.c_conn.sid);
2909 printk(KERN_ERR "iscsi: invalid session %d.\n",
2914 conn = transport->create_conn(session, ev->u.c_conn.cid);
2916 iscsi_cls_session_printk(KERN_ERR, session,
2917 "couldn't create a new connection.");
2921 ev->r.c_conn_ret.sid = session->sid;
2922 ev->r.c_conn_ret.cid = conn->cid;
2924 ISCSI_DBG_TRANS_CONN(conn, "Completed creating transport conn\n");
2929 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2931 struct iscsi_cls_conn *conn;
2933 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
2937 ISCSI_DBG_TRANS_CONN(conn, "Flushing cleanup during destruction\n");
2938 flush_work(&conn->cleanup_work);
2939 ISCSI_DBG_TRANS_CONN(conn, "Destroying transport conn\n");
2941 if (transport->destroy_conn)
2942 transport->destroy_conn(conn);
2947 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
2949 char *data = (char*)ev + sizeof(*ev);
2950 struct iscsi_cls_conn *conn;
2951 struct iscsi_cls_session *session;
2952 int err = 0, value = 0, state;
2954 if (ev->u.set_param.len > PAGE_SIZE)
2957 session = iscsi_session_lookup(ev->u.set_param.sid);
2958 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
2959 if (!conn || !session)
2962 switch (ev->u.set_param.param) {
2963 case ISCSI_PARAM_SESS_RECOVERY_TMO:
2964 sscanf(data, "%d", &value);
2965 if (!session->recovery_tmo_sysfs_override)
2966 session->recovery_tmo = value;
2969 state = READ_ONCE(conn->state);
2970 if (state == ISCSI_CONN_BOUND || state == ISCSI_CONN_UP) {
2971 err = transport->set_param(conn, ev->u.set_param.param,
2972 data, ev->u.set_param.len);
2981 static int iscsi_if_ep_connect(struct iscsi_transport *transport,
2982 struct iscsi_uevent *ev, int msg_type)
2984 struct iscsi_endpoint *ep;
2985 struct sockaddr *dst_addr;
2986 struct Scsi_Host *shost = NULL;
2987 int non_blocking, err = 0;
2989 if (!transport->ep_connect)
2992 if (msg_type == ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST) {
2993 shost = scsi_host_lookup(ev->u.ep_connect_through_host.host_no);
2995 printk(KERN_ERR "ep connect failed. Could not find "
2997 ev->u.ep_connect_through_host.host_no);
3000 non_blocking = ev->u.ep_connect_through_host.non_blocking;
3002 non_blocking = ev->u.ep_connect.non_blocking;
3004 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
3005 ep = transport->ep_connect(shost, dst_addr, non_blocking);
3011 ev->r.ep_connect_ret.handle = ep->id;
3014 scsi_host_put(shost);
3018 static int iscsi_if_ep_disconnect(struct iscsi_transport *transport,
3021 struct iscsi_cls_conn *conn;
3022 struct iscsi_endpoint *ep;
3024 if (!transport->ep_disconnect)
3027 ep = iscsi_lookup_endpoint(ep_handle);
3034 * conn was not even bound yet, so we can't get iscsi conn
3037 transport->ep_disconnect(ep);
3041 mutex_lock(&conn->ep_mutex);
3042 iscsi_if_disconnect_bound_ep(conn, ep, false);
3043 mutex_unlock(&conn->ep_mutex);
3045 iscsi_put_endpoint(ep);
3050 iscsi_if_transport_ep(struct iscsi_transport *transport,
3051 struct iscsi_uevent *ev, int msg_type)
3053 struct iscsi_endpoint *ep;
3057 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
3058 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
3059 rc = iscsi_if_ep_connect(transport, ev, msg_type);
3061 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
3062 if (!transport->ep_poll)
3065 ep = iscsi_lookup_endpoint(ev->u.ep_poll.ep_handle);
3069 ev->r.retcode = transport->ep_poll(ep,
3070 ev->u.ep_poll.timeout_ms);
3071 iscsi_put_endpoint(ep);
3073 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
3074 rc = iscsi_if_ep_disconnect(transport,
3075 ev->u.ep_disconnect.ep_handle);
3082 iscsi_tgt_dscvr(struct iscsi_transport *transport,
3083 struct iscsi_uevent *ev)
3085 struct Scsi_Host *shost;
3086 struct sockaddr *dst_addr;
3089 if (!transport->tgt_dscvr)
3092 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
3094 printk(KERN_ERR "target discovery could not find host no %u\n",
3095 ev->u.tgt_dscvr.host_no);
3100 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
3101 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
3102 ev->u.tgt_dscvr.enable, dst_addr);
3103 scsi_host_put(shost);
3108 iscsi_set_host_param(struct iscsi_transport *transport,
3109 struct iscsi_uevent *ev)
3111 char *data = (char*)ev + sizeof(*ev);
3112 struct Scsi_Host *shost;
3115 if (!transport->set_host_param)
3118 if (ev->u.set_host_param.len > PAGE_SIZE)
3121 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
3123 printk(KERN_ERR "set_host_param could not find host no %u\n",
3124 ev->u.set_host_param.host_no);
3128 err = transport->set_host_param(shost, ev->u.set_host_param.param,
3129 data, ev->u.set_host_param.len);
3130 scsi_host_put(shost);
3135 iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
3137 struct Scsi_Host *shost;
3138 struct iscsi_path *params;
3141 if (!transport->set_path)
3144 shost = scsi_host_lookup(ev->u.set_path.host_no);
3146 printk(KERN_ERR "set path could not find host no %u\n",
3147 ev->u.set_path.host_no);
3151 params = (struct iscsi_path *)((char *)ev + sizeof(*ev));
3152 err = transport->set_path(shost, params);
3154 scsi_host_put(shost);
3158 static int iscsi_session_has_conns(int sid)
3160 struct iscsi_cls_conn *conn;
3161 unsigned long flags;
3164 spin_lock_irqsave(&connlock, flags);
3165 list_for_each_entry(conn, &connlist, conn_list) {
3166 if (iscsi_conn_get_sid(conn) == sid) {
3171 spin_unlock_irqrestore(&connlock, flags);
3177 iscsi_set_iface_params(struct iscsi_transport *transport,
3178 struct iscsi_uevent *ev, uint32_t len)
3180 char *data = (char *)ev + sizeof(*ev);
3181 struct Scsi_Host *shost;
3184 if (!transport->set_iface_param)
3187 shost = scsi_host_lookup(ev->u.set_iface_params.host_no);
3189 printk(KERN_ERR "set_iface_params could not find host no %u\n",
3190 ev->u.set_iface_params.host_no);
3194 err = transport->set_iface_param(shost, data, len);
3195 scsi_host_put(shost);
3200 iscsi_send_ping(struct iscsi_transport *transport, struct iscsi_uevent *ev)
3202 struct Scsi_Host *shost;
3203 struct sockaddr *dst_addr;
3206 if (!transport->send_ping)
3209 shost = scsi_host_lookup(ev->u.iscsi_ping.host_no);
3211 printk(KERN_ERR "iscsi_ping could not find host no %u\n",
3212 ev->u.iscsi_ping.host_no);
3216 dst_addr = (struct sockaddr *)((char *)ev + sizeof(*ev));
3217 err = transport->send_ping(shost, ev->u.iscsi_ping.iface_num,
3218 ev->u.iscsi_ping.iface_type,
3219 ev->u.iscsi_ping.payload_size,
3220 ev->u.iscsi_ping.pid,
3222 scsi_host_put(shost);
3227 iscsi_get_chap(struct iscsi_transport *transport, struct nlmsghdr *nlh)
3229 struct iscsi_uevent *ev = nlmsg_data(nlh);
3230 struct Scsi_Host *shost = NULL;
3231 struct iscsi_chap_rec *chap_rec;
3232 struct iscsi_internal *priv;
3233 struct sk_buff *skbchap;
3234 struct nlmsghdr *nlhchap;
3235 struct iscsi_uevent *evchap;
3236 uint32_t chap_buf_size;
3240 if (!transport->get_chap)
3243 priv = iscsi_if_transport_lookup(transport);
3247 chap_buf_size = (ev->u.get_chap.num_entries * sizeof(*chap_rec));
3248 len = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
3250 shost = scsi_host_lookup(ev->u.get_chap.host_no);
3252 printk(KERN_ERR "%s: failed. Could not find host no %u\n",
3253 __func__, ev->u.get_chap.host_no);
3260 skbchap = alloc_skb(len, GFP_KERNEL);
3262 printk(KERN_ERR "can not deliver chap: OOM\n");
3267 nlhchap = __nlmsg_put(skbchap, 0, 0, 0,
3268 (len - sizeof(*nlhchap)), 0);
3269 evchap = nlmsg_data(nlhchap);
3270 memset(evchap, 0, sizeof(*evchap));
3271 evchap->transport_handle = iscsi_handle(transport);
3272 evchap->type = nlh->nlmsg_type;
3273 evchap->u.get_chap.host_no = ev->u.get_chap.host_no;
3274 evchap->u.get_chap.chap_tbl_idx = ev->u.get_chap.chap_tbl_idx;
3275 evchap->u.get_chap.num_entries = ev->u.get_chap.num_entries;
3276 buf = (char *)evchap + sizeof(*evchap);
3277 memset(buf, 0, chap_buf_size);
3279 err = transport->get_chap(shost, ev->u.get_chap.chap_tbl_idx,
3280 &evchap->u.get_chap.num_entries, buf);
3282 actual_size = nlmsg_total_size(sizeof(*ev) + chap_buf_size);
3283 skb_trim(skbchap, NLMSG_ALIGN(actual_size));
3284 nlhchap->nlmsg_len = actual_size;
3286 err = iscsi_multicast_skb(skbchap, ISCSI_NL_GRP_ISCSID,
3288 } while (err < 0 && err != -ECONNREFUSED);
3291 scsi_host_put(shost);
3295 static int iscsi_set_chap(struct iscsi_transport *transport,
3296 struct iscsi_uevent *ev, uint32_t len)
3298 char *data = (char *)ev + sizeof(*ev);
3299 struct Scsi_Host *shost;
3302 if (!transport->set_chap)
3305 shost = scsi_host_lookup(ev->u.set_path.host_no);
3307 pr_err("%s could not find host no %u\n",
3308 __func__, ev->u.set_path.host_no);
3312 err = transport->set_chap(shost, data, len);
3313 scsi_host_put(shost);
3317 static int iscsi_delete_chap(struct iscsi_transport *transport,
3318 struct iscsi_uevent *ev)
3320 struct Scsi_Host *shost;
3323 if (!transport->delete_chap)
3326 shost = scsi_host_lookup(ev->u.delete_chap.host_no);
3328 printk(KERN_ERR "%s could not find host no %u\n",
3329 __func__, ev->u.delete_chap.host_no);
3333 err = transport->delete_chap(shost, ev->u.delete_chap.chap_tbl_idx);
3334 scsi_host_put(shost);
3338 static const struct {
3339 enum iscsi_discovery_parent_type value;
3341 } iscsi_discovery_parent_names[] = {
3342 {ISCSI_DISC_PARENT_UNKNOWN, "Unknown" },
3343 {ISCSI_DISC_PARENT_SENDTGT, "Sendtarget" },
3344 {ISCSI_DISC_PARENT_ISNS, "isns" },
3347 char *iscsi_get_discovery_parent_name(int parent_type)
3350 char *state = "Unknown!";
3352 for (i = 0; i < ARRAY_SIZE(iscsi_discovery_parent_names); i++) {
3353 if (iscsi_discovery_parent_names[i].value & parent_type) {
3354 state = iscsi_discovery_parent_names[i].name;
3360 EXPORT_SYMBOL_GPL(iscsi_get_discovery_parent_name);
3362 static int iscsi_set_flashnode_param(struct iscsi_transport *transport,
3363 struct iscsi_uevent *ev, uint32_t len)
3365 char *data = (char *)ev + sizeof(*ev);
3366 struct Scsi_Host *shost;
3367 struct iscsi_bus_flash_session *fnode_sess;
3368 struct iscsi_bus_flash_conn *fnode_conn;
3373 if (!transport->set_flashnode_param) {
3375 goto exit_set_fnode;
3378 shost = scsi_host_lookup(ev->u.set_flashnode.host_no);
3380 pr_err("%s could not find host no %u\n",
3381 __func__, ev->u.set_flashnode.host_no);
3383 goto exit_set_fnode;
3386 idx = ev->u.set_flashnode.flashnode_idx;
3387 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3389 pr_err("%s could not find flashnode %u for host no %u\n",
3390 __func__, idx, ev->u.set_flashnode.host_no);
3395 dev = iscsi_find_flashnode_conn(fnode_sess);
3401 fnode_conn = iscsi_dev_to_flash_conn(dev);
3402 err = transport->set_flashnode_param(fnode_sess, fnode_conn, data, len);
3406 put_device(&fnode_sess->dev);
3409 scsi_host_put(shost);
3415 static int iscsi_new_flashnode(struct iscsi_transport *transport,
3416 struct iscsi_uevent *ev, uint32_t len)
3418 char *data = (char *)ev + sizeof(*ev);
3419 struct Scsi_Host *shost;
3423 if (!transport->new_flashnode) {
3425 goto exit_new_fnode;
3428 shost = scsi_host_lookup(ev->u.new_flashnode.host_no);
3430 pr_err("%s could not find host no %u\n",
3431 __func__, ev->u.new_flashnode.host_no);
3436 index = transport->new_flashnode(shost, data, len);
3439 ev->r.new_flashnode_ret.flashnode_idx = index;
3444 scsi_host_put(shost);
3450 static int iscsi_del_flashnode(struct iscsi_transport *transport,
3451 struct iscsi_uevent *ev)
3453 struct Scsi_Host *shost;
3454 struct iscsi_bus_flash_session *fnode_sess;
3458 if (!transport->del_flashnode) {
3460 goto exit_del_fnode;
3463 shost = scsi_host_lookup(ev->u.del_flashnode.host_no);
3465 pr_err("%s could not find host no %u\n",
3466 __func__, ev->u.del_flashnode.host_no);
3471 idx = ev->u.del_flashnode.flashnode_idx;
3472 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3474 pr_err("%s could not find flashnode %u for host no %u\n",
3475 __func__, idx, ev->u.del_flashnode.host_no);
3480 err = transport->del_flashnode(fnode_sess);
3481 put_device(&fnode_sess->dev);
3484 scsi_host_put(shost);
3490 static int iscsi_login_flashnode(struct iscsi_transport *transport,
3491 struct iscsi_uevent *ev)
3493 struct Scsi_Host *shost;
3494 struct iscsi_bus_flash_session *fnode_sess;
3495 struct iscsi_bus_flash_conn *fnode_conn;
3500 if (!transport->login_flashnode) {
3502 goto exit_login_fnode;
3505 shost = scsi_host_lookup(ev->u.login_flashnode.host_no);
3507 pr_err("%s could not find host no %u\n",
3508 __func__, ev->u.login_flashnode.host_no);
3513 idx = ev->u.login_flashnode.flashnode_idx;
3514 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3516 pr_err("%s could not find flashnode %u for host no %u\n",
3517 __func__, idx, ev->u.login_flashnode.host_no);
3522 dev = iscsi_find_flashnode_conn(fnode_sess);
3528 fnode_conn = iscsi_dev_to_flash_conn(dev);
3529 err = transport->login_flashnode(fnode_sess, fnode_conn);
3533 put_device(&fnode_sess->dev);
3536 scsi_host_put(shost);
3542 static int iscsi_logout_flashnode(struct iscsi_transport *transport,
3543 struct iscsi_uevent *ev)
3545 struct Scsi_Host *shost;
3546 struct iscsi_bus_flash_session *fnode_sess;
3547 struct iscsi_bus_flash_conn *fnode_conn;
3552 if (!transport->logout_flashnode) {
3554 goto exit_logout_fnode;
3557 shost = scsi_host_lookup(ev->u.logout_flashnode.host_no);
3559 pr_err("%s could not find host no %u\n",
3560 __func__, ev->u.logout_flashnode.host_no);
3565 idx = ev->u.logout_flashnode.flashnode_idx;
3566 fnode_sess = iscsi_get_flashnode_by_index(shost, idx);
3568 pr_err("%s could not find flashnode %u for host no %u\n",
3569 __func__, idx, ev->u.logout_flashnode.host_no);
3574 dev = iscsi_find_flashnode_conn(fnode_sess);
3580 fnode_conn = iscsi_dev_to_flash_conn(dev);
3582 err = transport->logout_flashnode(fnode_sess, fnode_conn);
3586 put_device(&fnode_sess->dev);
3589 scsi_host_put(shost);
3595 static int iscsi_logout_flashnode_sid(struct iscsi_transport *transport,
3596 struct iscsi_uevent *ev)
3598 struct Scsi_Host *shost;
3599 struct iscsi_cls_session *session;
3602 if (!transport->logout_flashnode_sid) {
3604 goto exit_logout_sid;
3607 shost = scsi_host_lookup(ev->u.logout_flashnode_sid.host_no);
3609 pr_err("%s could not find host no %u\n",
3610 __func__, ev->u.logout_flashnode.host_no);
3615 session = iscsi_session_lookup(ev->u.logout_flashnode_sid.sid);
3617 pr_err("%s could not find session id %u\n",
3618 __func__, ev->u.logout_flashnode_sid.sid);
3623 err = transport->logout_flashnode_sid(session);
3626 scsi_host_put(shost);
3633 iscsi_get_host_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
3635 struct iscsi_uevent *ev = nlmsg_data(nlh);
3636 struct Scsi_Host *shost = NULL;
3637 struct iscsi_internal *priv;
3638 struct sk_buff *skbhost_stats;
3639 struct nlmsghdr *nlhhost_stats;
3640 struct iscsi_uevent *evhost_stats;
3641 int host_stats_size = 0;
3645 if (!transport->get_host_stats)
3648 priv = iscsi_if_transport_lookup(transport);
3652 host_stats_size = sizeof(struct iscsi_offload_host_stats);
3653 len = nlmsg_total_size(sizeof(*ev) + host_stats_size);
3655 shost = scsi_host_lookup(ev->u.get_host_stats.host_no);
3657 pr_err("%s: failed. Could not find host no %u\n",
3658 __func__, ev->u.get_host_stats.host_no);
3665 skbhost_stats = alloc_skb(len, GFP_KERNEL);
3666 if (!skbhost_stats) {
3667 pr_err("cannot deliver host stats: OOM\n");
3669 goto exit_host_stats;
3672 nlhhost_stats = __nlmsg_put(skbhost_stats, 0, 0, 0,
3673 (len - sizeof(*nlhhost_stats)), 0);
3674 evhost_stats = nlmsg_data(nlhhost_stats);
3675 memset(evhost_stats, 0, sizeof(*evhost_stats));
3676 evhost_stats->transport_handle = iscsi_handle(transport);
3677 evhost_stats->type = nlh->nlmsg_type;
3678 evhost_stats->u.get_host_stats.host_no =
3679 ev->u.get_host_stats.host_no;
3680 buf = (char *)evhost_stats + sizeof(*evhost_stats);
3681 memset(buf, 0, host_stats_size);
3683 err = transport->get_host_stats(shost, buf, host_stats_size);
3685 kfree_skb(skbhost_stats);
3686 goto exit_host_stats;
3689 actual_size = nlmsg_total_size(sizeof(*ev) + host_stats_size);
3690 skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));
3691 nlhhost_stats->nlmsg_len = actual_size;
3693 err = iscsi_multicast_skb(skbhost_stats, ISCSI_NL_GRP_ISCSID,
3695 } while (err < 0 && err != -ECONNREFUSED);
3698 scsi_host_put(shost);
3702 static int iscsi_if_transport_conn(struct iscsi_transport *transport,
3703 struct nlmsghdr *nlh)
3705 struct iscsi_uevent *ev = nlmsg_data(nlh);
3706 struct iscsi_cls_session *session;
3707 struct iscsi_cls_conn *conn = NULL;
3708 struct iscsi_endpoint *ep;
3712 switch (nlh->nlmsg_type) {
3713 case ISCSI_UEVENT_CREATE_CONN:
3714 return iscsi_if_create_conn(transport, ev);
3715 case ISCSI_UEVENT_DESTROY_CONN:
3716 return iscsi_if_destroy_conn(transport, ev);
3717 case ISCSI_UEVENT_STOP_CONN:
3718 return iscsi_if_stop_conn(transport, ev);
3722 * The following cmds need to be run under the ep_mutex so in kernel
3723 * conn cleanup (ep_disconnect + unbind and conn) is not done while
3724 * these are running. They also must not run if we have just run a conn
3725 * cleanup because they would set the state in a way that might allow
3726 * IO or send IO themselves.
3728 switch (nlh->nlmsg_type) {
3729 case ISCSI_UEVENT_START_CONN:
3730 conn = iscsi_conn_lookup(ev->u.start_conn.sid,
3731 ev->u.start_conn.cid);
3733 case ISCSI_UEVENT_BIND_CONN:
3734 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
3736 case ISCSI_UEVENT_SEND_PDU:
3737 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
3744 mutex_lock(&conn->ep_mutex);
3745 spin_lock_irq(&conn->lock);
3746 if (test_bit(ISCSI_CLS_CONN_BIT_CLEANUP, &conn->flags)) {
3747 spin_unlock_irq(&conn->lock);
3748 mutex_unlock(&conn->ep_mutex);
3749 ev->r.retcode = -ENOTCONN;
3752 spin_unlock_irq(&conn->lock);
3754 switch (nlh->nlmsg_type) {
3755 case ISCSI_UEVENT_BIND_CONN:
3756 session = iscsi_session_lookup(ev->u.b_conn.sid);
3762 ev->r.retcode = transport->bind_conn(session, conn,
3763 ev->u.b_conn.transport_eph,
3764 ev->u.b_conn.is_leading);
3766 WRITE_ONCE(conn->state, ISCSI_CONN_BOUND);
3768 if (ev->r.retcode || !transport->ep_connect)
3771 ep = iscsi_lookup_endpoint(ev->u.b_conn.transport_eph);
3775 iscsi_put_endpoint(ep);
3778 iscsi_cls_conn_printk(KERN_ERR, conn,
3779 "Could not set ep conn binding\n");
3782 case ISCSI_UEVENT_START_CONN:
3783 ev->r.retcode = transport->start_conn(conn);
3785 WRITE_ONCE(conn->state, ISCSI_CONN_UP);
3788 case ISCSI_UEVENT_SEND_PDU:
3789 pdu_len = nlh->nlmsg_len - sizeof(*nlh) - sizeof(*ev);
3791 if ((ev->u.send_pdu.hdr_size > pdu_len) ||
3792 (ev->u.send_pdu.data_size > (pdu_len - ev->u.send_pdu.hdr_size))) {
3797 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);
3806 mutex_unlock(&conn->ep_mutex);
3811 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
3815 struct iscsi_uevent *ev = nlmsg_data(nlh);
3816 struct iscsi_transport *transport = NULL;
3817 struct iscsi_internal *priv;
3818 struct iscsi_cls_session *session;
3819 struct iscsi_endpoint *ep = NULL;
3821 if (!netlink_capable(skb, CAP_SYS_ADMIN))
3824 if (nlh->nlmsg_type == ISCSI_UEVENT_PATH_UPDATE)
3825 *group = ISCSI_NL_GRP_UIP;
3827 *group = ISCSI_NL_GRP_ISCSID;
3829 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
3832 transport = priv->iscsi_transport;
3834 if (!try_module_get(transport->owner))
3837 portid = NETLINK_CB(skb).portid;
3839 switch (nlh->nlmsg_type) {
3840 case ISCSI_UEVENT_CREATE_SESSION:
3841 err = iscsi_if_create_session(priv, ep, ev,
3843 ev->u.c_session.initial_cmdsn,
3844 ev->u.c_session.cmds_max,
3845 ev->u.c_session.queue_depth);
3847 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
3848 ep = iscsi_lookup_endpoint(ev->u.c_bound_session.ep_handle);
3854 err = iscsi_if_create_session(priv, ep, ev,
3856 ev->u.c_bound_session.initial_cmdsn,
3857 ev->u.c_bound_session.cmds_max,
3858 ev->u.c_bound_session.queue_depth);
3859 iscsi_put_endpoint(ep);
3861 case ISCSI_UEVENT_DESTROY_SESSION:
3862 session = iscsi_session_lookup(ev->u.d_session.sid);
3865 else if (iscsi_session_has_conns(ev->u.d_session.sid))
3868 transport->destroy_session(session);
3870 case ISCSI_UEVENT_DESTROY_SESSION_ASYNC:
3871 session = iscsi_session_lookup(ev->u.d_session.sid);
3874 else if (iscsi_session_has_conns(ev->u.d_session.sid))
3877 unsigned long flags;
3879 /* Prevent this session from being found again */
3880 spin_lock_irqsave(&sesslock, flags);
3881 list_del_init(&session->sess_list);
3882 spin_unlock_irqrestore(&sesslock, flags);
3884 queue_work(system_unbound_wq, &session->destroy_work);
3887 case ISCSI_UEVENT_UNBIND_SESSION:
3888 session = iscsi_session_lookup(ev->u.d_session.sid);
3890 queue_work(session->workq, &session->unbind_work);
3894 case ISCSI_UEVENT_SET_PARAM:
3895 err = iscsi_set_param(transport, ev);
3897 case ISCSI_UEVENT_CREATE_CONN:
3898 case ISCSI_UEVENT_DESTROY_CONN:
3899 case ISCSI_UEVENT_STOP_CONN:
3900 case ISCSI_UEVENT_START_CONN:
3901 case ISCSI_UEVENT_BIND_CONN:
3902 case ISCSI_UEVENT_SEND_PDU:
3903 err = iscsi_if_transport_conn(transport, nlh);
3905 case ISCSI_UEVENT_GET_STATS:
3906 err = iscsi_if_get_stats(transport, nlh);
3908 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
3909 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
3910 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
3911 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT_THROUGH_HOST:
3912 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
3914 case ISCSI_UEVENT_TGT_DSCVR:
3915 err = iscsi_tgt_dscvr(transport, ev);
3917 case ISCSI_UEVENT_SET_HOST_PARAM:
3918 err = iscsi_set_host_param(transport, ev);
3920 case ISCSI_UEVENT_PATH_UPDATE:
3921 err = iscsi_set_path(transport, ev);
3923 case ISCSI_UEVENT_SET_IFACE_PARAMS:
3924 err = iscsi_set_iface_params(transport, ev,
3925 nlmsg_attrlen(nlh, sizeof(*ev)));
3927 case ISCSI_UEVENT_PING:
3928 err = iscsi_send_ping(transport, ev);
3930 case ISCSI_UEVENT_GET_CHAP:
3931 err = iscsi_get_chap(transport, nlh);
3933 case ISCSI_UEVENT_DELETE_CHAP:
3934 err = iscsi_delete_chap(transport, ev);
3936 case ISCSI_UEVENT_SET_FLASHNODE_PARAMS:
3937 err = iscsi_set_flashnode_param(transport, ev,
3941 case ISCSI_UEVENT_NEW_FLASHNODE:
3942 err = iscsi_new_flashnode(transport, ev,
3943 nlmsg_attrlen(nlh, sizeof(*ev)));
3945 case ISCSI_UEVENT_DEL_FLASHNODE:
3946 err = iscsi_del_flashnode(transport, ev);
3948 case ISCSI_UEVENT_LOGIN_FLASHNODE:
3949 err = iscsi_login_flashnode(transport, ev);
3951 case ISCSI_UEVENT_LOGOUT_FLASHNODE:
3952 err = iscsi_logout_flashnode(transport, ev);
3954 case ISCSI_UEVENT_LOGOUT_FLASHNODE_SID:
3955 err = iscsi_logout_flashnode_sid(transport, ev);
3957 case ISCSI_UEVENT_SET_CHAP:
3958 err = iscsi_set_chap(transport, ev,
3959 nlmsg_attrlen(nlh, sizeof(*ev)));
3961 case ISCSI_UEVENT_GET_HOST_STATS:
3962 err = iscsi_get_host_stats(transport, nlh);
3969 module_put(transport->owner);
3974 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
3975 * Malformed skbs with wrong lengths or invalid creds are not processed.
3978 iscsi_if_rx(struct sk_buff *skb)
3980 u32 portid = NETLINK_CB(skb).portid;
3982 mutex_lock(&rx_queue_mutex);
3983 while (skb->len >= NLMSG_HDRLEN) {
3986 struct nlmsghdr *nlh;
3987 struct iscsi_uevent *ev;
3989 int retries = ISCSI_SEND_MAX_ALLOWED;
3991 nlh = nlmsg_hdr(skb);
3992 if (nlh->nlmsg_len < sizeof(*nlh) + sizeof(*ev) ||
3993 skb->len < nlh->nlmsg_len) {
3997 ev = nlmsg_data(nlh);
3998 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
3999 if (rlen > skb->len)
4002 err = iscsi_if_recv_msg(skb, nlh, &group);
4004 ev->type = ISCSI_KEVENT_IF_ERROR;
4009 * special case for GET_STATS:
4010 * on success - sending reply and stats from
4011 * inside of if_recv_msg(),
4012 * on error - fall through.
4014 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
4016 if (ev->type == ISCSI_UEVENT_GET_CHAP && !err)
4018 err = iscsi_if_send_reply(portid, nlh->nlmsg_type,
4020 if (err == -EAGAIN && --retries < 0) {
4021 printk(KERN_WARNING "Send reply failed, error %d\n", err);
4024 } while (err < 0 && err != -ECONNREFUSED && err != -ESRCH);
4025 skb_pull(skb, rlen);
4027 mutex_unlock(&rx_queue_mutex);
4030 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
4031 struct device_attribute dev_attr_##_prefix##_##_name = \
4032 __ATTR(_name,_mode,_show,_store)
4035 * iSCSI connection attrs
4037 #define iscsi_conn_attr_show(param) \
4039 show_conn_param_##param(struct device *dev, \
4040 struct device_attribute *attr, char *buf) \
4042 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
4043 struct iscsi_transport *t = conn->transport; \
4044 return t->get_conn_param(conn, param, buf); \
4047 #define iscsi_conn_attr(field, param) \
4048 iscsi_conn_attr_show(param) \
4049 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
4052 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
4053 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
4054 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
4055 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
4056 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
4057 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
4058 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
4059 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
4060 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
4061 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
4062 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
4063 iscsi_conn_attr(local_port, ISCSI_PARAM_LOCAL_PORT);
4064 iscsi_conn_attr(statsn, ISCSI_PARAM_STATSN);
4065 iscsi_conn_attr(keepalive_tmo, ISCSI_PARAM_KEEPALIVE_TMO);
4066 iscsi_conn_attr(max_segment_size, ISCSI_PARAM_MAX_SEGMENT_SIZE);
4067 iscsi_conn_attr(tcp_timestamp_stat, ISCSI_PARAM_TCP_TIMESTAMP_STAT);
4068 iscsi_conn_attr(tcp_wsf_disable, ISCSI_PARAM_TCP_WSF_DISABLE);
4069 iscsi_conn_attr(tcp_nagle_disable, ISCSI_PARAM_TCP_NAGLE_DISABLE);
4070 iscsi_conn_attr(tcp_timer_scale, ISCSI_PARAM_TCP_TIMER_SCALE);
4071 iscsi_conn_attr(tcp_timestamp_enable, ISCSI_PARAM_TCP_TIMESTAMP_EN);
4072 iscsi_conn_attr(fragment_disable, ISCSI_PARAM_IP_FRAGMENT_DISABLE);
4073 iscsi_conn_attr(ipv4_tos, ISCSI_PARAM_IPV4_TOS);
4074 iscsi_conn_attr(ipv6_traffic_class, ISCSI_PARAM_IPV6_TC);
4075 iscsi_conn_attr(ipv6_flow_label, ISCSI_PARAM_IPV6_FLOW_LABEL);
4076 iscsi_conn_attr(is_fw_assigned_ipv6, ISCSI_PARAM_IS_FW_ASSIGNED_IPV6);
4077 iscsi_conn_attr(tcp_xmit_wsf, ISCSI_PARAM_TCP_XMIT_WSF);
4078 iscsi_conn_attr(tcp_recv_wsf, ISCSI_PARAM_TCP_RECV_WSF);
4079 iscsi_conn_attr(local_ipaddr, ISCSI_PARAM_LOCAL_IPADDR);
4081 static const char *const connection_state_names[] = {
4082 [ISCSI_CONN_UP] = "up",
4083 [ISCSI_CONN_DOWN] = "down",
4084 [ISCSI_CONN_FAILED] = "failed",
4085 [ISCSI_CONN_BOUND] = "bound"
4088 static ssize_t show_conn_state(struct device *dev,
4089 struct device_attribute *attr, char *buf)
4091 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);
4092 const char *state = "unknown";
4093 int conn_state = READ_ONCE(conn->state);
4095 if (conn_state >= 0 &&
4096 conn_state < ARRAY_SIZE(connection_state_names))
4097 state = connection_state_names[conn_state];
4099 return sysfs_emit(buf, "%s\n", state);
4101 static ISCSI_CLASS_ATTR(conn, state, S_IRUGO, show_conn_state,
4104 #define iscsi_conn_ep_attr_show(param) \
4105 static ssize_t show_conn_ep_param_##param(struct device *dev, \
4106 struct device_attribute *attr,\
4109 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
4110 struct iscsi_transport *t = conn->transport; \
4111 struct iscsi_endpoint *ep; \
4115 * Need to make sure ep_disconnect does not free the LLD's \
4116 * interconnect resources while we are trying to read them. \
4118 mutex_lock(&conn->ep_mutex); \
4120 if (!ep && t->ep_connect) { \
4121 mutex_unlock(&conn->ep_mutex); \
4126 rc = t->get_ep_param(ep, param, buf); \
4128 rc = t->get_conn_param(conn, param, buf); \
4129 mutex_unlock(&conn->ep_mutex); \
4133 #define iscsi_conn_ep_attr(field, param) \
4134 iscsi_conn_ep_attr_show(param) \
4135 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, \
4136 show_conn_ep_param_##param, NULL);
4138 iscsi_conn_ep_attr(address, ISCSI_PARAM_CONN_ADDRESS);
4139 iscsi_conn_ep_attr(port, ISCSI_PARAM_CONN_PORT);
4141 static struct attribute *iscsi_conn_attrs[] = {
4142 &dev_attr_conn_max_recv_dlength.attr,
4143 &dev_attr_conn_max_xmit_dlength.attr,
4144 &dev_attr_conn_header_digest.attr,
4145 &dev_attr_conn_data_digest.attr,
4146 &dev_attr_conn_ifmarker.attr,
4147 &dev_attr_conn_ofmarker.attr,
4148 &dev_attr_conn_address.attr,
4149 &dev_attr_conn_port.attr,
4150 &dev_attr_conn_exp_statsn.attr,
4151 &dev_attr_conn_persistent_address.attr,
4152 &dev_attr_conn_persistent_port.attr,
4153 &dev_attr_conn_ping_tmo.attr,
4154 &dev_attr_conn_recv_tmo.attr,
4155 &dev_attr_conn_local_port.attr,
4156 &dev_attr_conn_statsn.attr,
4157 &dev_attr_conn_keepalive_tmo.attr,
4158 &dev_attr_conn_max_segment_size.attr,
4159 &dev_attr_conn_tcp_timestamp_stat.attr,
4160 &dev_attr_conn_tcp_wsf_disable.attr,
4161 &dev_attr_conn_tcp_nagle_disable.attr,
4162 &dev_attr_conn_tcp_timer_scale.attr,
4163 &dev_attr_conn_tcp_timestamp_enable.attr,
4164 &dev_attr_conn_fragment_disable.attr,
4165 &dev_attr_conn_ipv4_tos.attr,
4166 &dev_attr_conn_ipv6_traffic_class.attr,
4167 &dev_attr_conn_ipv6_flow_label.attr,
4168 &dev_attr_conn_is_fw_assigned_ipv6.attr,
4169 &dev_attr_conn_tcp_xmit_wsf.attr,
4170 &dev_attr_conn_tcp_recv_wsf.attr,
4171 &dev_attr_conn_local_ipaddr.attr,
4172 &dev_attr_conn_state.attr,
4176 static umode_t iscsi_conn_attr_is_visible(struct kobject *kobj,
4177 struct attribute *attr, int i)
4179 struct device *cdev = container_of(kobj, struct device, kobj);
4180 struct iscsi_cls_conn *conn = transport_class_to_conn(cdev);
4181 struct iscsi_transport *t = conn->transport;
4184 if (attr == &dev_attr_conn_max_recv_dlength.attr)
4185 param = ISCSI_PARAM_MAX_RECV_DLENGTH;
4186 else if (attr == &dev_attr_conn_max_xmit_dlength.attr)
4187 param = ISCSI_PARAM_MAX_XMIT_DLENGTH;
4188 else if (attr == &dev_attr_conn_header_digest.attr)
4189 param = ISCSI_PARAM_HDRDGST_EN;
4190 else if (attr == &dev_attr_conn_data_digest.attr)
4191 param = ISCSI_PARAM_DATADGST_EN;
4192 else if (attr == &dev_attr_conn_ifmarker.attr)
4193 param = ISCSI_PARAM_IFMARKER_EN;
4194 else if (attr == &dev_attr_conn_ofmarker.attr)
4195 param = ISCSI_PARAM_OFMARKER_EN;
4196 else if (attr == &dev_attr_conn_address.attr)
4197 param = ISCSI_PARAM_CONN_ADDRESS;
4198 else if (attr == &dev_attr_conn_port.attr)
4199 param = ISCSI_PARAM_CONN_PORT;
4200 else if (attr == &dev_attr_conn_exp_statsn.attr)
4201 param = ISCSI_PARAM_EXP_STATSN;
4202 else if (attr == &dev_attr_conn_persistent_address.attr)
4203 param = ISCSI_PARAM_PERSISTENT_ADDRESS;
4204 else if (attr == &dev_attr_conn_persistent_port.attr)
4205 param = ISCSI_PARAM_PERSISTENT_PORT;
4206 else if (attr == &dev_attr_conn_ping_tmo.attr)
4207 param = ISCSI_PARAM_PING_TMO;
4208 else if (attr == &dev_attr_conn_recv_tmo.attr)
4209 param = ISCSI_PARAM_RECV_TMO;
4210 else if (attr == &dev_attr_conn_local_port.attr)
4211 param = ISCSI_PARAM_LOCAL_PORT;
4212 else if (attr == &dev_attr_conn_statsn.attr)
4213 param = ISCSI_PARAM_STATSN;
4214 else if (attr == &dev_attr_conn_keepalive_tmo.attr)
4215 param = ISCSI_PARAM_KEEPALIVE_TMO;
4216 else if (attr == &dev_attr_conn_max_segment_size.attr)
4217 param = ISCSI_PARAM_MAX_SEGMENT_SIZE;
4218 else if (attr == &dev_attr_conn_tcp_timestamp_stat.attr)
4219 param = ISCSI_PARAM_TCP_TIMESTAMP_STAT;
4220 else if (attr == &dev_attr_conn_tcp_wsf_disable.attr)
4221 param = ISCSI_PARAM_TCP_WSF_DISABLE;
4222 else if (attr == &dev_attr_conn_tcp_nagle_disable.attr)
4223 param = ISCSI_PARAM_TCP_NAGLE_DISABLE;
4224 else if (attr == &dev_attr_conn_tcp_timer_scale.attr)
4225 param = ISCSI_PARAM_TCP_TIMER_SCALE;
4226 else if (attr == &dev_attr_conn_tcp_timestamp_enable.attr)
4227 param = ISCSI_PARAM_TCP_TIMESTAMP_EN;
4228 else if (attr == &dev_attr_conn_fragment_disable.attr)
4229 param = ISCSI_PARAM_IP_FRAGMENT_DISABLE;
4230 else if (attr == &dev_attr_conn_ipv4_tos.attr)
4231 param = ISCSI_PARAM_IPV4_TOS;
4232 else if (attr == &dev_attr_conn_ipv6_traffic_class.attr)
4233 param = ISCSI_PARAM_IPV6_TC;
4234 else if (attr == &dev_attr_conn_ipv6_flow_label.attr)
4235 param = ISCSI_PARAM_IPV6_FLOW_LABEL;
4236 else if (attr == &dev_attr_conn_is_fw_assigned_ipv6.attr)
4237 param = ISCSI_PARAM_IS_FW_ASSIGNED_IPV6;
4238 else if (attr == &dev_attr_conn_tcp_xmit_wsf.attr)
4239 param = ISCSI_PARAM_TCP_XMIT_WSF;
4240 else if (attr == &dev_attr_conn_tcp_recv_wsf.attr)
4241 param = ISCSI_PARAM_TCP_RECV_WSF;
4242 else if (attr == &dev_attr_conn_local_ipaddr.attr)
4243 param = ISCSI_PARAM_LOCAL_IPADDR;
4244 else if (attr == &dev_attr_conn_state.attr)
4247 WARN_ONCE(1, "Invalid conn attr");
4251 return t->attr_is_visible(ISCSI_PARAM, param);
4254 static struct attribute_group iscsi_conn_group = {
4255 .attrs = iscsi_conn_attrs,
4256 .is_visible = iscsi_conn_attr_is_visible,
4260 * iSCSI session attrs
4262 #define iscsi_session_attr_show(param, perm) \
4264 show_session_param_##param(struct device *dev, \
4265 struct device_attribute *attr, char *buf) \
4267 struct iscsi_cls_session *session = \
4268 iscsi_dev_to_session(dev->parent); \
4269 struct iscsi_transport *t = session->transport; \
4271 if (perm && !capable(CAP_SYS_ADMIN)) \
4273 return t->get_session_param(session, param, buf); \
4276 #define iscsi_session_attr(field, param, perm) \
4277 iscsi_session_attr_show(param, perm) \
4278 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
4280 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
4281 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
4282 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
4283 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
4284 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
4285 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
4286 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
4287 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
4288 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
4289 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
4290 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
4291 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
4292 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
4293 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
4294 iscsi_session_attr(chap_out_idx, ISCSI_PARAM_CHAP_OUT_IDX, 1);
4295 iscsi_session_attr(chap_in_idx, ISCSI_PARAM_CHAP_IN_IDX, 1);
4296 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
4297 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
4298 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
4299 iscsi_session_attr(tgt_reset_tmo, ISCSI_PARAM_TGT_RESET_TMO, 0);
4300 iscsi_session_attr(ifacename, ISCSI_PARAM_IFACE_NAME, 0);
4301 iscsi_session_attr(initiatorname, ISCSI_PARAM_INITIATOR_NAME, 0);
4302 iscsi_session_attr(targetalias, ISCSI_PARAM_TARGET_ALIAS, 0);
4303 iscsi_session_attr(boot_root, ISCSI_PARAM_BOOT_ROOT, 0);
4304 iscsi_session_attr(boot_nic, ISCSI_PARAM_BOOT_NIC, 0);
4305 iscsi_session_attr(boot_target, ISCSI_PARAM_BOOT_TARGET, 0);
4306 iscsi_session_attr(auto_snd_tgt_disable, ISCSI_PARAM_AUTO_SND_TGT_DISABLE, 0);
4307 iscsi_session_attr(discovery_session, ISCSI_PARAM_DISCOVERY_SESS, 0);
4308 iscsi_session_attr(portal_type, ISCSI_PARAM_PORTAL_TYPE, 0);
4309 iscsi_session_attr(chap_auth, ISCSI_PARAM_CHAP_AUTH_EN, 0);
4310 iscsi_session_attr(discovery_logout, ISCSI_PARAM_DISCOVERY_LOGOUT_EN, 0);
4311 iscsi_session_attr(bidi_chap, ISCSI_PARAM_BIDI_CHAP_EN, 0);
4312 iscsi_session_attr(discovery_auth_optional,
4313 ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL, 0);
4314 iscsi_session_attr(def_time2wait, ISCSI_PARAM_DEF_TIME2WAIT, 0);
4315 iscsi_session_attr(def_time2retain, ISCSI_PARAM_DEF_TIME2RETAIN, 0);
4316 iscsi_session_attr(isid, ISCSI_PARAM_ISID, 0);
4317 iscsi_session_attr(tsid, ISCSI_PARAM_TSID, 0);
4318 iscsi_session_attr(def_taskmgmt_tmo, ISCSI_PARAM_DEF_TASKMGMT_TMO, 0);
4319 iscsi_session_attr(discovery_parent_idx, ISCSI_PARAM_DISCOVERY_PARENT_IDX, 0);
4320 iscsi_session_attr(discovery_parent_type, ISCSI_PARAM_DISCOVERY_PARENT_TYPE, 0);
4323 show_priv_session_state(struct device *dev, struct device_attribute *attr,
4326 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
4327 return sysfs_emit(buf, "%s\n", iscsi_session_state_name(session->state));
4329 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
4332 show_priv_session_creator(struct device *dev, struct device_attribute *attr,
4335 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
4336 return sysfs_emit(buf, "%d\n", session->creator);
4338 static ISCSI_CLASS_ATTR(priv_sess, creator, S_IRUGO, show_priv_session_creator,
4341 show_priv_session_target_id(struct device *dev, struct device_attribute *attr,
4344 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
4345 return sysfs_emit(buf, "%d\n", session->target_id);
4347 static ISCSI_CLASS_ATTR(priv_sess, target_id, S_IRUGO,
4348 show_priv_session_target_id, NULL);
4350 #define iscsi_priv_session_attr_show(field, format) \
4352 show_priv_session_##field(struct device *dev, \
4353 struct device_attribute *attr, char *buf) \
4355 struct iscsi_cls_session *session = \
4356 iscsi_dev_to_session(dev->parent); \
4357 if (session->field == -1) \
4358 return sysfs_emit(buf, "off\n"); \
4359 return sysfs_emit(buf, format"\n", session->field); \
4362 #define iscsi_priv_session_attr_store(field) \
4364 store_priv_session_##field(struct device *dev, \
4365 struct device_attribute *attr, \
4366 const char *buf, size_t count) \
4370 struct iscsi_cls_session *session = \
4371 iscsi_dev_to_session(dev->parent); \
4372 if ((session->state == ISCSI_SESSION_FREE) || \
4373 (session->state == ISCSI_SESSION_FAILED)) \
4375 if (strncmp(buf, "off", 3) == 0) { \
4376 session->field = -1; \
4377 session->field##_sysfs_override = true; \
4379 val = simple_strtoul(buf, &cp, 0); \
4380 if (*cp != '\0' && *cp != '\n') \
4382 session->field = val; \
4383 session->field##_sysfs_override = true; \
4388 #define iscsi_priv_session_rw_attr(field, format) \
4389 iscsi_priv_session_attr_show(field, format) \
4390 iscsi_priv_session_attr_store(field) \
4391 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO | S_IWUSR, \
4392 show_priv_session_##field, \
4393 store_priv_session_##field)
4395 iscsi_priv_session_rw_attr(recovery_tmo, "%d");
4397 static struct attribute *iscsi_session_attrs[] = {
4398 &dev_attr_sess_initial_r2t.attr,
4399 &dev_attr_sess_max_outstanding_r2t.attr,
4400 &dev_attr_sess_immediate_data.attr,
4401 &dev_attr_sess_first_burst_len.attr,
4402 &dev_attr_sess_max_burst_len.attr,
4403 &dev_attr_sess_data_pdu_in_order.attr,
4404 &dev_attr_sess_data_seq_in_order.attr,
4405 &dev_attr_sess_erl.attr,
4406 &dev_attr_sess_targetname.attr,
4407 &dev_attr_sess_tpgt.attr,
4408 &dev_attr_sess_password.attr,
4409 &dev_attr_sess_password_in.attr,
4410 &dev_attr_sess_username.attr,
4411 &dev_attr_sess_username_in.attr,
4412 &dev_attr_sess_fast_abort.attr,
4413 &dev_attr_sess_abort_tmo.attr,
4414 &dev_attr_sess_lu_reset_tmo.attr,
4415 &dev_attr_sess_tgt_reset_tmo.attr,
4416 &dev_attr_sess_ifacename.attr,
4417 &dev_attr_sess_initiatorname.attr,
4418 &dev_attr_sess_targetalias.attr,
4419 &dev_attr_sess_boot_root.attr,
4420 &dev_attr_sess_boot_nic.attr,
4421 &dev_attr_sess_boot_target.attr,
4422 &dev_attr_priv_sess_recovery_tmo.attr,
4423 &dev_attr_priv_sess_state.attr,
4424 &dev_attr_priv_sess_creator.attr,
4425 &dev_attr_sess_chap_out_idx.attr,
4426 &dev_attr_sess_chap_in_idx.attr,
4427 &dev_attr_priv_sess_target_id.attr,
4428 &dev_attr_sess_auto_snd_tgt_disable.attr,
4429 &dev_attr_sess_discovery_session.attr,
4430 &dev_attr_sess_portal_type.attr,
4431 &dev_attr_sess_chap_auth.attr,
4432 &dev_attr_sess_discovery_logout.attr,
4433 &dev_attr_sess_bidi_chap.attr,
4434 &dev_attr_sess_discovery_auth_optional.attr,
4435 &dev_attr_sess_def_time2wait.attr,
4436 &dev_attr_sess_def_time2retain.attr,
4437 &dev_attr_sess_isid.attr,
4438 &dev_attr_sess_tsid.attr,
4439 &dev_attr_sess_def_taskmgmt_tmo.attr,
4440 &dev_attr_sess_discovery_parent_idx.attr,
4441 &dev_attr_sess_discovery_parent_type.attr,
4445 static umode_t iscsi_session_attr_is_visible(struct kobject *kobj,
4446 struct attribute *attr, int i)
4448 struct device *cdev = container_of(kobj, struct device, kobj);
4449 struct iscsi_cls_session *session = transport_class_to_session(cdev);
4450 struct iscsi_transport *t = session->transport;
4453 if (attr == &dev_attr_sess_initial_r2t.attr)
4454 param = ISCSI_PARAM_INITIAL_R2T_EN;
4455 else if (attr == &dev_attr_sess_max_outstanding_r2t.attr)
4456 param = ISCSI_PARAM_MAX_R2T;
4457 else if (attr == &dev_attr_sess_immediate_data.attr)
4458 param = ISCSI_PARAM_IMM_DATA_EN;
4459 else if (attr == &dev_attr_sess_first_burst_len.attr)
4460 param = ISCSI_PARAM_FIRST_BURST;
4461 else if (attr == &dev_attr_sess_max_burst_len.attr)
4462 param = ISCSI_PARAM_MAX_BURST;
4463 else if (attr == &dev_attr_sess_data_pdu_in_order.attr)
4464 param = ISCSI_PARAM_PDU_INORDER_EN;
4465 else if (attr == &dev_attr_sess_data_seq_in_order.attr)
4466 param = ISCSI_PARAM_DATASEQ_INORDER_EN;
4467 else if (attr == &dev_attr_sess_erl.attr)
4468 param = ISCSI_PARAM_ERL;
4469 else if (attr == &dev_attr_sess_targetname.attr)
4470 param = ISCSI_PARAM_TARGET_NAME;
4471 else if (attr == &dev_attr_sess_tpgt.attr)
4472 param = ISCSI_PARAM_TPGT;
4473 else if (attr == &dev_attr_sess_chap_in_idx.attr)
4474 param = ISCSI_PARAM_CHAP_IN_IDX;
4475 else if (attr == &dev_attr_sess_chap_out_idx.attr)
4476 param = ISCSI_PARAM_CHAP_OUT_IDX;
4477 else if (attr == &dev_attr_sess_password.attr)
4478 param = ISCSI_PARAM_USERNAME;
4479 else if (attr == &dev_attr_sess_password_in.attr)
4480 param = ISCSI_PARAM_USERNAME_IN;
4481 else if (attr == &dev_attr_sess_username.attr)
4482 param = ISCSI_PARAM_PASSWORD;
4483 else if (attr == &dev_attr_sess_username_in.attr)
4484 param = ISCSI_PARAM_PASSWORD_IN;
4485 else if (attr == &dev_attr_sess_fast_abort.attr)
4486 param = ISCSI_PARAM_FAST_ABORT;
4487 else if (attr == &dev_attr_sess_abort_tmo.attr)
4488 param = ISCSI_PARAM_ABORT_TMO;
4489 else if (attr == &dev_attr_sess_lu_reset_tmo.attr)
4490 param = ISCSI_PARAM_LU_RESET_TMO;
4491 else if (attr == &dev_attr_sess_tgt_reset_tmo.attr)
4492 param = ISCSI_PARAM_TGT_RESET_TMO;
4493 else if (attr == &dev_attr_sess_ifacename.attr)
4494 param = ISCSI_PARAM_IFACE_NAME;
4495 else if (attr == &dev_attr_sess_initiatorname.attr)
4496 param = ISCSI_PARAM_INITIATOR_NAME;
4497 else if (attr == &dev_attr_sess_targetalias.attr)
4498 param = ISCSI_PARAM_TARGET_ALIAS;
4499 else if (attr == &dev_attr_sess_boot_root.attr)
4500 param = ISCSI_PARAM_BOOT_ROOT;
4501 else if (attr == &dev_attr_sess_boot_nic.attr)
4502 param = ISCSI_PARAM_BOOT_NIC;
4503 else if (attr == &dev_attr_sess_boot_target.attr)
4504 param = ISCSI_PARAM_BOOT_TARGET;
4505 else if (attr == &dev_attr_sess_auto_snd_tgt_disable.attr)
4506 param = ISCSI_PARAM_AUTO_SND_TGT_DISABLE;
4507 else if (attr == &dev_attr_sess_discovery_session.attr)
4508 param = ISCSI_PARAM_DISCOVERY_SESS;
4509 else if (attr == &dev_attr_sess_portal_type.attr)
4510 param = ISCSI_PARAM_PORTAL_TYPE;
4511 else if (attr == &dev_attr_sess_chap_auth.attr)
4512 param = ISCSI_PARAM_CHAP_AUTH_EN;
4513 else if (attr == &dev_attr_sess_discovery_logout.attr)
4514 param = ISCSI_PARAM_DISCOVERY_LOGOUT_EN;
4515 else if (attr == &dev_attr_sess_bidi_chap.attr)
4516 param = ISCSI_PARAM_BIDI_CHAP_EN;
4517 else if (attr == &dev_attr_sess_discovery_auth_optional.attr)
4518 param = ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL;
4519 else if (attr == &dev_attr_sess_def_time2wait.attr)
4520 param = ISCSI_PARAM_DEF_TIME2WAIT;
4521 else if (attr == &dev_attr_sess_def_time2retain.attr)
4522 param = ISCSI_PARAM_DEF_TIME2RETAIN;
4523 else if (attr == &dev_attr_sess_isid.attr)
4524 param = ISCSI_PARAM_ISID;
4525 else if (attr == &dev_attr_sess_tsid.attr)
4526 param = ISCSI_PARAM_TSID;
4527 else if (attr == &dev_attr_sess_def_taskmgmt_tmo.attr)
4528 param = ISCSI_PARAM_DEF_TASKMGMT_TMO;
4529 else if (attr == &dev_attr_sess_discovery_parent_idx.attr)
4530 param = ISCSI_PARAM_DISCOVERY_PARENT_IDX;
4531 else if (attr == &dev_attr_sess_discovery_parent_type.attr)
4532 param = ISCSI_PARAM_DISCOVERY_PARENT_TYPE;
4533 else if (attr == &dev_attr_priv_sess_recovery_tmo.attr)
4534 return S_IRUGO | S_IWUSR;
4535 else if (attr == &dev_attr_priv_sess_state.attr)
4537 else if (attr == &dev_attr_priv_sess_creator.attr)
4539 else if (attr == &dev_attr_priv_sess_target_id.attr)
4542 WARN_ONCE(1, "Invalid session attr");
4546 return t->attr_is_visible(ISCSI_PARAM, param);
4549 static struct attribute_group iscsi_session_group = {
4550 .attrs = iscsi_session_attrs,
4551 .is_visible = iscsi_session_attr_is_visible,
4557 #define iscsi_host_attr_show(param) \
4559 show_host_param_##param(struct device *dev, \
4560 struct device_attribute *attr, char *buf) \
4562 struct Scsi_Host *shost = transport_class_to_shost(dev); \
4563 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
4564 return priv->iscsi_transport->get_host_param(shost, param, buf); \
4567 #define iscsi_host_attr(field, param) \
4568 iscsi_host_attr_show(param) \
4569 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
4572 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
4573 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
4574 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
4575 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
4576 iscsi_host_attr(port_state, ISCSI_HOST_PARAM_PORT_STATE);
4577 iscsi_host_attr(port_speed, ISCSI_HOST_PARAM_PORT_SPEED);
4579 static struct attribute *iscsi_host_attrs[] = {
4580 &dev_attr_host_netdev.attr,
4581 &dev_attr_host_hwaddress.attr,
4582 &dev_attr_host_ipaddress.attr,
4583 &dev_attr_host_initiatorname.attr,
4584 &dev_attr_host_port_state.attr,
4585 &dev_attr_host_port_speed.attr,
4589 static umode_t iscsi_host_attr_is_visible(struct kobject *kobj,
4590 struct attribute *attr, int i)
4592 struct device *cdev = container_of(kobj, struct device, kobj);
4593 struct Scsi_Host *shost = transport_class_to_shost(cdev);
4594 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt);
4597 if (attr == &dev_attr_host_netdev.attr)
4598 param = ISCSI_HOST_PARAM_NETDEV_NAME;
4599 else if (attr == &dev_attr_host_hwaddress.attr)
4600 param = ISCSI_HOST_PARAM_HWADDRESS;
4601 else if (attr == &dev_attr_host_ipaddress.attr)
4602 param = ISCSI_HOST_PARAM_IPADDRESS;
4603 else if (attr == &dev_attr_host_initiatorname.attr)
4604 param = ISCSI_HOST_PARAM_INITIATOR_NAME;
4605 else if (attr == &dev_attr_host_port_state.attr)
4606 param = ISCSI_HOST_PARAM_PORT_STATE;
4607 else if (attr == &dev_attr_host_port_speed.attr)
4608 param = ISCSI_HOST_PARAM_PORT_SPEED;
4610 WARN_ONCE(1, "Invalid host attr");
4614 return priv->iscsi_transport->attr_is_visible(ISCSI_HOST_PARAM, param);
4617 static struct attribute_group iscsi_host_group = {
4618 .attrs = iscsi_host_attrs,
4619 .is_visible = iscsi_host_attr_is_visible,
4622 /* convert iscsi_port_speed values to ascii string name */
4623 static const struct {
4624 enum iscsi_port_speed value;
4626 } iscsi_port_speed_names[] = {
4627 {ISCSI_PORT_SPEED_UNKNOWN, "Unknown" },
4628 {ISCSI_PORT_SPEED_10MBPS, "10 Mbps" },
4629 {ISCSI_PORT_SPEED_100MBPS, "100 Mbps" },
4630 {ISCSI_PORT_SPEED_1GBPS, "1 Gbps" },
4631 {ISCSI_PORT_SPEED_10GBPS, "10 Gbps" },
4632 {ISCSI_PORT_SPEED_25GBPS, "25 Gbps" },
4633 {ISCSI_PORT_SPEED_40GBPS, "40 Gbps" },
4636 char *iscsi_get_port_speed_name(struct Scsi_Host *shost)
4639 char *speed = "Unknown!";
4640 struct iscsi_cls_host *ihost = shost->shost_data;
4641 uint32_t port_speed = ihost->port_speed;
4643 for (i = 0; i < ARRAY_SIZE(iscsi_port_speed_names); i++) {
4644 if (iscsi_port_speed_names[i].value & port_speed) {
4645 speed = iscsi_port_speed_names[i].name;
4651 EXPORT_SYMBOL_GPL(iscsi_get_port_speed_name);
4653 /* convert iscsi_port_state values to ascii string name */
4654 static const struct {
4655 enum iscsi_port_state value;
4657 } iscsi_port_state_names[] = {
4658 {ISCSI_PORT_STATE_DOWN, "LINK DOWN" },
4659 {ISCSI_PORT_STATE_UP, "LINK UP" },
4662 char *iscsi_get_port_state_name(struct Scsi_Host *shost)
4665 char *state = "Unknown!";
4666 struct iscsi_cls_host *ihost = shost->shost_data;
4667 uint32_t port_state = ihost->port_state;
4669 for (i = 0; i < ARRAY_SIZE(iscsi_port_state_names); i++) {
4670 if (iscsi_port_state_names[i].value & port_state) {
4671 state = iscsi_port_state_names[i].name;
4677 EXPORT_SYMBOL_GPL(iscsi_get_port_state_name);
4679 static int iscsi_session_match(struct attribute_container *cont,
4682 struct iscsi_cls_session *session;
4683 struct Scsi_Host *shost;
4684 struct iscsi_internal *priv;
4686 if (!iscsi_is_session_dev(dev))
4689 session = iscsi_dev_to_session(dev);
4690 shost = iscsi_session_to_shost(session);
4691 if (!shost->transportt)
4694 priv = to_iscsi_internal(shost->transportt);
4695 if (priv->session_cont.ac.class != &iscsi_session_class.class)
4698 return &priv->session_cont.ac == cont;
4701 static int iscsi_conn_match(struct attribute_container *cont,
4704 struct iscsi_cls_session *session;
4705 struct iscsi_cls_conn *conn;
4706 struct Scsi_Host *shost;
4707 struct iscsi_internal *priv;
4709 if (!iscsi_is_conn_dev(dev))
4712 conn = iscsi_dev_to_conn(dev);
4713 session = iscsi_dev_to_session(conn->dev.parent);
4714 shost = iscsi_session_to_shost(session);
4716 if (!shost->transportt)
4719 priv = to_iscsi_internal(shost->transportt);
4720 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
4723 return &priv->conn_cont.ac == cont;
4726 static int iscsi_host_match(struct attribute_container *cont,
4729 struct Scsi_Host *shost;
4730 struct iscsi_internal *priv;
4732 if (!scsi_is_host_device(dev))
4735 shost = dev_to_shost(dev);
4736 if (!shost->transportt ||
4737 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
4740 priv = to_iscsi_internal(shost->transportt);
4741 return &priv->t.host_attrs.ac == cont;
4744 struct scsi_transport_template *
4745 iscsi_register_transport(struct iscsi_transport *tt)
4747 struct iscsi_internal *priv;
4748 unsigned long flags;
4752 WARN_ON(tt->ep_disconnect && !tt->unbind_conn);
4754 priv = iscsi_if_transport_lookup(tt);
4758 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
4761 INIT_LIST_HEAD(&priv->list);
4762 priv->iscsi_transport = tt;
4763 priv->t.user_scan = iscsi_user_scan;
4765 priv->dev.class = &iscsi_transport_class;
4766 dev_set_name(&priv->dev, "%s", tt->name);
4767 err = device_register(&priv->dev);
4771 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
4773 goto unregister_dev;
4775 /* host parameters */
4776 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
4777 priv->t.host_attrs.ac.match = iscsi_host_match;
4778 priv->t.host_attrs.ac.grp = &iscsi_host_group;
4779 priv->t.host_size = sizeof(struct iscsi_cls_host);
4780 transport_container_register(&priv->t.host_attrs);
4782 /* connection parameters */
4783 priv->conn_cont.ac.class = &iscsi_connection_class.class;
4784 priv->conn_cont.ac.match = iscsi_conn_match;
4785 priv->conn_cont.ac.grp = &iscsi_conn_group;
4786 transport_container_register(&priv->conn_cont);
4788 /* session parameters */
4789 priv->session_cont.ac.class = &iscsi_session_class.class;
4790 priv->session_cont.ac.match = iscsi_session_match;
4791 priv->session_cont.ac.grp = &iscsi_session_group;
4792 transport_container_register(&priv->session_cont);
4794 spin_lock_irqsave(&iscsi_transport_lock, flags);
4795 list_add(&priv->list, &iscsi_transports);
4796 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
4798 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
4802 device_unregister(&priv->dev);
4808 EXPORT_SYMBOL_GPL(iscsi_register_transport);
4810 int iscsi_unregister_transport(struct iscsi_transport *tt)
4812 struct iscsi_internal *priv;
4813 unsigned long flags;
4817 mutex_lock(&rx_queue_mutex);
4819 priv = iscsi_if_transport_lookup(tt);
4822 spin_lock_irqsave(&iscsi_transport_lock, flags);
4823 list_del(&priv->list);
4824 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
4826 transport_container_unregister(&priv->conn_cont);
4827 transport_container_unregister(&priv->session_cont);
4828 transport_container_unregister(&priv->t.host_attrs);
4830 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
4831 device_unregister(&priv->dev);
4832 mutex_unlock(&rx_queue_mutex);
4836 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
4838 void iscsi_dbg_trace(void (*trace)(struct device *dev, struct va_format *),
4839 struct device *dev, const char *fmt, ...)
4841 struct va_format vaf;
4844 va_start(args, fmt);
4850 EXPORT_SYMBOL_GPL(iscsi_dbg_trace);
4852 static __init int iscsi_transport_init(void)
4855 struct netlink_kernel_cfg cfg = {
4857 .input = iscsi_if_rx,
4859 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
4860 ISCSI_TRANSPORT_VERSION);
4862 atomic_set(&iscsi_session_nr, 0);
4864 err = class_register(&iscsi_transport_class);
4868 err = class_register(&iscsi_endpoint_class);
4870 goto unregister_transport_class;
4872 err = class_register(&iscsi_iface_class);
4874 goto unregister_endpoint_class;
4876 err = transport_class_register(&iscsi_host_class);
4878 goto unregister_iface_class;
4880 err = transport_class_register(&iscsi_connection_class);
4882 goto unregister_host_class;
4884 err = transport_class_register(&iscsi_session_class);
4886 goto unregister_conn_class;
4888 err = bus_register(&iscsi_flashnode_bus);
4890 goto unregister_session_class;
4892 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, &cfg);
4895 goto unregister_flashnode_bus;
4898 iscsi_conn_cleanup_workq = alloc_workqueue("%s",
4899 WQ_SYSFS | WQ_MEM_RECLAIM | WQ_UNBOUND, 0,
4900 "iscsi_conn_cleanup");
4901 if (!iscsi_conn_cleanup_workq) {
4909 netlink_kernel_release(nls);
4910 unregister_flashnode_bus:
4911 bus_unregister(&iscsi_flashnode_bus);
4912 unregister_session_class:
4913 transport_class_unregister(&iscsi_session_class);
4914 unregister_conn_class:
4915 transport_class_unregister(&iscsi_connection_class);
4916 unregister_host_class:
4917 transport_class_unregister(&iscsi_host_class);
4918 unregister_iface_class:
4919 class_unregister(&iscsi_iface_class);
4920 unregister_endpoint_class:
4921 class_unregister(&iscsi_endpoint_class);
4922 unregister_transport_class:
4923 class_unregister(&iscsi_transport_class);
4927 static void __exit iscsi_transport_exit(void)
4929 destroy_workqueue(iscsi_conn_cleanup_workq);
4930 netlink_kernel_release(nls);
4931 bus_unregister(&iscsi_flashnode_bus);
4932 transport_class_unregister(&iscsi_connection_class);
4933 transport_class_unregister(&iscsi_session_class);
4934 transport_class_unregister(&iscsi_host_class);
4935 class_unregister(&iscsi_endpoint_class);
4936 class_unregister(&iscsi_iface_class);
4937 class_unregister(&iscsi_transport_class);
4940 module_init(iscsi_transport_init);
4941 module_exit(iscsi_transport_exit);
4943 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
4944 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
4945 "Alex Aizman <itn780@yahoo.com>");
4946 MODULE_DESCRIPTION("iSCSI Transport Interface");
4947 MODULE_LICENSE("GPL");
4948 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
4949 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_ISCSI);