1/*
2 * iSCSI transport class definitions
3 *
4 * Copyright (C) IBM Corporation, 2004
5 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23#include <linux/module.h>
24#include <linux/mutex.h>
25#include <linux/slab.h>
26#include <linux/bsg-lib.h>
27#include <linux/idr.h>
28#include <net/tcp.h>
29#include <scsi/scsi.h>
30#include <scsi/scsi_host.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_transport.h>
33#include <scsi/scsi_transport_iscsi.h>
34#include <scsi/iscsi_if.h>
35#include <scsi/scsi_cmnd.h>
36#include <scsi/scsi_bsg_iscsi.h>
37
38#define ISCSI_TRANSPORT_VERSION "2.0-870"
39
40#define CREATE_TRACE_POINTS
41#include <trace/events/iscsi.h>
42
43/*
44 * Export tracepoint symbols to be used by other modules.
45 */
46EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_conn);
47EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_eh);
48EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_session);
49EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_tcp);
50EXPORT_TRACEPOINT_SYMBOL_GPL(iscsi_dbg_sw_tcp);
51
52static int dbg_session;
53module_param_named(debug_session, dbg_session, int,
54 S_IRUGO | S_IWUSR);
55MODULE_PARM_DESC(debug_session,
56 "Turn on debugging for sessions in scsi_transport_iscsi "
57 "module. Set to 1 to turn on, and zero to turn off. Default "
58 "is off.");
59
60static int dbg_conn;
61module_param_named(debug_conn, dbg_conn, int,
62 S_IRUGO | S_IWUSR);
63MODULE_PARM_DESC(debug_conn,
64 "Turn on debugging for connections in scsi_transport_iscsi "
65 "module. Set to 1 to turn on, and zero to turn off. Default "
66 "is off.");
67
68#define ISCSI_DBG_TRANS_SESSION(_session, dbg_fmt, arg...) \
69 do { \
70 if (dbg_session) \
71 iscsi_cls_session_printk(KERN_INFO, _session, \
72 "%s: " dbg_fmt, \
73 __func__, ##arg); \
74 iscsi_dbg_trace(trace_iscsi_dbg_trans_session, \
75 &(_session)->dev, \
76 "%s " dbg_fmt, __func__, ##arg); \
77 } while (0);
78
79#define ISCSI_DBG_TRANS_CONN(_conn, dbg_fmt, arg...) \
80 do { \
81 if (dbg_conn) \
82 iscsi_cls_conn_printk(KERN_INFO, _conn, \
83 "%s: " dbg_fmt, \
84 __func__, ##arg); \
85 iscsi_dbg_trace(trace_iscsi_dbg_trans_conn, \
86 &(_conn)->dev, \
87 "%s " dbg_fmt, __func__, ##arg); \
88 } while (0);
89
90struct iscsi_internal {
91 struct scsi_transport_template t;
92 struct iscsi_transport *iscsi_transport;
93 struct list_head list;
94 struct device dev;
95
96 struct transport_container conn_cont;
97 struct transport_container session_cont;
98};
99
100static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
101static struct workqueue_struct *iscsi_eh_timer_workq;
102
103static DEFINE_IDA(iscsi_sess_ida);
104/*
105 * list of registered transports and lock that must
106 * be held while accessing list. The iscsi_transport_lock must
107 * be acquired after the rx_queue_mutex.
108 */
109static LIST_HEAD(iscsi_transports);
110static DEFINE_SPINLOCK(iscsi_transport_lock);
111
112#define to_iscsi_internal(tmpl) \
113 container_of(tmpl, struct iscsi_internal, t)
114
115#define dev_to_iscsi_internal(_dev) \
116 container_of(_dev, struct iscsi_internal, dev)
117
118static void iscsi_transport_release(struct device *dev)
119{
120 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
121 kfree(priv);
122}
123
124/*
125 * iscsi_transport_class represents the iscsi_transports that are
126 * registered.
127 */
128static struct class iscsi_transport_class = {
129 .name = "iscsi_transport",
130 .dev_release = iscsi_transport_release,
131};
132
133static ssize_t
134show_transport_handle(struct device *dev, struct device_attribute *attr,
135 char *buf)
136{
137 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
138 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
139}
140static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
141
142#define show_transport_attr(name, format) \
143static ssize_t \
144show_transport_##name(struct device *dev, \
145 struct device_attribute *attr,char *buf) \
146{ \
147 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
148 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
149} \
150static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
151
152show_transport_attr(caps, "0x%x");
153
154static struct attribute *iscsi_transport_attrs[] = {
155 &dev_attr_handle.attr,
156 &dev_attr_caps.attr,
157 NULL,
158};
159
160static struct attribute_group iscsi_transport_group = {
161 .attrs = iscsi_transport_attrs,
162};
163
164/*
165 * iSCSI endpoint attrs
166 */
167#define iscsi_dev_to_endpoint(_dev) \
168 container_of(_dev, struct iscsi_endpoint, dev)
169
170#define ISCSI_ATTR(_prefix,_name,_mode,_show,_store) \
171struct device_attribute dev_attr_##_prefix##_##_name = \
172 __ATTR(_name,_mode,_show,_store)
173
174static void iscsi_endpoint_release(struct device *dev)
175{
176 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
177 kfree(ep);
178}
179
180static struct class iscsi_endpoint_class = {
181 .name = "iscsi_endpoint",
182 .dev_release = iscsi_endpoint_release,
183};
184
185static ssize_t
186show_ep_handle(struct device *dev, struct device_attribute *attr, char *buf)
187{
188 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
189 return sprintf(buf, "%llu\n", (unsigned long long) ep->id);
190}
191static ISCSI_ATTR(ep, handle, S_IRUGO, show_ep_handle, NULL);
192
193static struct attribute *iscsi_endpoint_attrs[] = {
194 &dev_attr_ep_handle.attr,
195 NULL,
196};
197
198static struct attribute_group iscsi_endpoint_group = {
199 .attrs = iscsi_endpoint_attrs,
200};
201
202#define ISCSI_MAX_EPID -1
203
204static int iscsi_match_epid(struct device *dev, const void *data)
205{
206 struct iscsi_endpoint *ep = iscsi_dev_to_endpoint(dev);
207 const uint64_t *epid = data;
208
209 return *epid == ep->id;
210}
211
212struct iscsi_endpoint *
213iscsi_create_endpoint(int dd_size)
214{
215 struct device *dev;
216 struct iscsi_endpoint *ep;
217 uint64_t id;
218 int err;
219
220 for (id = 1; id < ISCSI_MAX_EPID; id++) {
221 dev = class_find_device(&iscsi_endpoint_class, NULL, &id,
222 iscsi_match_epid);
223 if (!dev)
224 break;
225 else
226 put_device(dev);
227 }
228 if (id == ISCSI_MAX_EPID) {
229 printk(KERN_ERR "Too many connections. Max supported %u\n",
230 ISCSI_MAX_EPID - 1);
231 return NULL;
232 }
233
234 ep = kzalloc(sizeof(*ep) + dd_size, GFP_KERNEL);
235 if (!ep)
236 return NULL;
237
238 ep->id = id;
239 ep->dev.class = &iscsi_endpoint_class;
240 dev_set_name(&ep->dev, "ep-%llu", (unsigned long long) id);
241 err = device_register(&ep->dev);
242 if (err)
243 goto free_ep;
244
245 err = sysfs_create_group(&ep->dev.kobj, &iscsi_endpoint_group);
246 if (err)
247 goto unregister_dev;
248
249 if (dd_size)
250 ep->dd_data = &ep[1];
251 return ep;
252
253unregister_dev:
254 device_unregister(&ep->dev);
255 return NULL;
256
257free_ep:
258 kfree(ep);
259 return NULL;
260}
261EXPORT_SYMBOL_GPL(iscsi_create_endpoint);
262
263void iscsi_destroy_endpoint(struct iscsi_endpoint *ep)
264{
265 sysfs_remove_group(&ep->dev.kobj, &iscsi_endpoint_group);
266 device_unregister(&ep->dev);
267}
268EXPORT_SYMBOL_GPL(iscsi_destroy_endpoint);
269
270struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle)
271{
272 struct iscsi_endpoint *ep;
273 struct device *dev;
274
275 dev = class_find_device(&iscsi_endpoint_class, NULL, &handle,
276 iscsi_match_epid);
277 if (!dev)
278 return NULL;
279
280 ep = iscsi_dev_to_endpoint(dev);
281 /*
282 * we can drop this now because the interface will prevent
283 * removals and lookups from racing.
284 */
285 put_device(dev);
286 return ep;
287}
288EXPORT_SYMBOL_GPL(iscsi_lookup_endpoint);
289
290/*
291 * Interface to display network param to sysfs
292 */
293
294static void iscsi_iface_release(struct device *dev)
295{
296 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
297 struct device *parent = iface->dev.parent;
298
299 kfree(iface);
300 put_device(parent);
301}
302
303
304static struct class iscsi_iface_class = {
305 .name = "iscsi_iface",
306 .dev_release = iscsi_iface_release,
307};
308
309#define ISCSI_IFACE_ATTR(_prefix, _name, _mode, _show, _store) \
310struct device_attribute dev_attr_##_prefix##_##_name = \
311 __ATTR(_name, _mode, _show, _store)
312
313/* iface attrs show */
314#define iscsi_iface_attr_show(type, name, param_type, param) \
315static ssize_t \
316show_##type##_##name(struct device *dev, struct device_attribute *attr, \
317 char *buf) \
318{ \
319 struct iscsi_iface *iface = iscsi_dev_to_iface(dev); \
320 struct iscsi_transport *t = iface->transport; \
321 return t->get_iface_param(iface, param_type, param, buf); \
322} \
323
324#define iscsi_iface_net_attr(type, name, param) \
325 iscsi_iface_attr_show(type, name, ISCSI_NET_PARAM, param) \
326static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
327
328#define iscsi_iface_attr(type, name, param) \
329 iscsi_iface_attr_show(type, name, ISCSI_IFACE_PARAM, param) \
330static ISCSI_IFACE_ATTR(type, name, S_IRUGO, show_##type##_##name, NULL);
331
332/* generic read only ipv4 attribute */
333iscsi_iface_net_attr(ipv4_iface, ipaddress, ISCSI_NET_PARAM_IPV4_ADDR);
334iscsi_iface_net_attr(ipv4_iface, gateway, ISCSI_NET_PARAM_IPV4_GW);
335iscsi_iface_net_attr(ipv4_iface, subnet, ISCSI_NET_PARAM_IPV4_SUBNET);
336iscsi_iface_net_attr(ipv4_iface, bootproto, ISCSI_NET_PARAM_IPV4_BOOTPROTO);
337iscsi_iface_net_attr(ipv4_iface, dhcp_dns_address_en,
338 ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN);
339iscsi_iface_net_attr(ipv4_iface, dhcp_slp_da_info_en,
340 ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN);
341iscsi_iface_net_attr(ipv4_iface, tos_en, ISCSI_NET_PARAM_IPV4_TOS_EN);
342iscsi_iface_net_attr(ipv4_iface, tos, ISCSI_NET_PARAM_IPV4_TOS);
343iscsi_iface_net_attr(ipv4_iface, grat_arp_en,
344 ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN);
345iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id_en,
346 ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN);
347iscsi_iface_net_attr(ipv4_iface, dhcp_alt_client_id,
348 ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID);
349iscsi_iface_net_attr(ipv4_iface, dhcp_req_vendor_id_en,
350 ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN);
351iscsi_iface_net_attr(ipv4_iface, dhcp_use_vendor_id_en,
352 ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN);
353iscsi_iface_net_attr(ipv4_iface, dhcp_vendor_id,
354 ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID);
355iscsi_iface_net_attr(ipv4_iface, dhcp_learn_iqn_en,
356 ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN);
357iscsi_iface_net_attr(ipv4_iface, fragment_disable,
358 ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE);
359iscsi_iface_net_attr(ipv4_iface, incoming_forwarding_en,
360 ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN);
361iscsi_iface_net_attr(ipv4_iface, ttl, ISCSI_NET_PARAM_IPV4_TTL);
362
363/* generic read only ipv6 attribute */
364iscsi_iface_net_attr(ipv6_iface, ipaddress, ISCSI_NET_PARAM_IPV6_ADDR);
365iscsi_iface_net_attr(ipv6_iface, link_local_addr,
366 ISCSI_NET_PARAM_IPV6_LINKLOCAL);
367iscsi_iface_net_attr(ipv6_iface, router_addr, ISCSI_NET_PARAM_IPV6_ROUTER);
368iscsi_iface_net_attr(ipv6_iface, ipaddr_autocfg,
369 ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG);
370iscsi_iface_net_attr(ipv6_iface, link_local_autocfg,
371 ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG);
372iscsi_iface_net_attr(ipv6_iface, link_local_state,
373 ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE);
374iscsi_iface_net_attr(ipv6_iface, router_state,
375 ISCSI_NET_PARAM_IPV6_ROUTER_STATE);
376iscsi_iface_net_attr(ipv6_iface, grat_neighbor_adv_en,
377 ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN);
378iscsi_iface_net_attr(ipv6_iface, mld_en, ISCSI_NET_PARAM_IPV6_MLD_EN);
379iscsi_iface_net_attr(ipv6_iface, flow_label, ISCSI_NET_PARAM_IPV6_FLOW_LABEL);
380iscsi_iface_net_attr(ipv6_iface, traffic_class,
381 ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS);
382iscsi_iface_net_attr(ipv6_iface, hop_limit, ISCSI_NET_PARAM_IPV6_HOP_LIMIT);
383iscsi_iface_net_attr(ipv6_iface, nd_reachable_tmo,
384 ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO);
385iscsi_iface_net_attr(ipv6_iface, nd_rexmit_time,
386 ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME);
387iscsi_iface_net_attr(ipv6_iface, nd_stale_tmo,
388 ISCSI_NET_PARAM_IPV6_ND_STALE_TMO);
389iscsi_iface_net_attr(ipv6_iface, dup_addr_detect_cnt,
390 ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT);
391iscsi_iface_net_attr(ipv6_iface, router_adv_link_mtu,
392 ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU);
393
394/* common read only iface attribute */
395iscsi_iface_net_attr(iface, enabled, ISCSI_NET_PARAM_IFACE_ENABLE);
396iscsi_iface_net_attr(iface, vlan_id, ISCSI_NET_PARAM_VLAN_ID);
397iscsi_iface_net_attr(iface, vlan_priority, ISCSI_NET_PARAM_VLAN_PRIORITY);
398iscsi_iface_net_attr(iface, vlan_enabled, ISCSI_NET_PARAM_VLAN_ENABLED);
399iscsi_iface_net_attr(iface, mtu, ISCSI_NET_PARAM_MTU);
400iscsi_iface_net_attr(iface, port, ISCSI_NET_PARAM_PORT);
401iscsi_iface_net_attr(iface, ipaddress_state, ISCSI_NET_PARAM_IPADDR_STATE);
402iscsi_iface_net_attr(iface, delayed_ack_en, ISCSI_NET_PARAM_DELAYED_ACK_EN);
403iscsi_iface_net_attr(iface, tcp_nagle_disable,
404 ISCSI_NET_PARAM_TCP_NAGLE_DISABLE);
405iscsi_iface_net_attr(iface, tcp_wsf_disable, ISCSI_NET_PARAM_TCP_WSF_DISABLE);
406iscsi_iface_net_attr(iface, tcp_wsf, ISCSI_NET_PARAM_TCP_WSF);
407iscsi_iface_net_attr(iface, tcp_timer_scale, ISCSI_NET_PARAM_TCP_TIMER_SCALE);
408iscsi_iface_net_attr(iface, tcp_timestamp_en, ISCSI_NET_PARAM_TCP_TIMESTAMP_EN);
409iscsi_iface_net_attr(iface, cache_id, ISCSI_NET_PARAM_CACHE_ID);
410iscsi_iface_net_attr(iface, redirect_en, ISCSI_NET_PARAM_REDIRECT_EN);
411
412/* common iscsi specific settings attributes */
413iscsi_iface_attr(iface, def_taskmgmt_tmo, ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO);
414iscsi_iface_attr(iface, header_digest, ISCSI_IFACE_PARAM_HDRDGST_EN);
415iscsi_iface_attr(iface, data_digest, ISCSI_IFACE_PARAM_DATADGST_EN);
416iscsi_iface_attr(iface, immediate_data, ISCSI_IFACE_PARAM_IMM_DATA_EN);
417iscsi_iface_attr(iface, initial_r2t, ISCSI_IFACE_PARAM_INITIAL_R2T_EN);
418iscsi_iface_attr(iface, data_seq_in_order,
419 ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN);
420iscsi_iface_attr(iface, data_pdu_in_order, ISCSI_IFACE_PARAM_PDU_INORDER_EN);
421iscsi_iface_attr(iface, erl, ISCSI_IFACE_PARAM_ERL);
422iscsi_iface_attr(iface, max_recv_dlength, ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH);
423iscsi_iface_attr(iface, first_burst_len, ISCSI_IFACE_PARAM_FIRST_BURST);
424iscsi_iface_attr(iface, max_outstanding_r2t, ISCSI_IFACE_PARAM_MAX_R2T);
425iscsi_iface_attr(iface, max_burst_len, ISCSI_IFACE_PARAM_MAX_BURST);
426iscsi_iface_attr(iface, chap_auth, ISCSI_IFACE_PARAM_CHAP_AUTH_EN);
427iscsi_iface_attr(iface, bidi_chap, ISCSI_IFACE_PARAM_BIDI_CHAP_EN);
428iscsi_iface_attr(iface, discovery_auth_optional,
429 ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL);
430iscsi_iface_attr(iface, discovery_logout,
431 ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN);
432iscsi_iface_attr(iface, strict_login_comp_en,
433 ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN);
434iscsi_iface_attr(iface, initiator_name, ISCSI_IFACE_PARAM_INITIATOR_NAME);
435
436static umode_t iscsi_iface_attr_is_visible(struct kobject *kobj,
437 struct attribute *attr, int i)
438{
439 struct device *dev = container_of(kobj, struct device, kobj);
440 struct iscsi_iface *iface = iscsi_dev_to_iface(dev);
441 struct iscsi_transport *t = iface->transport;
442 int param;
443 int param_type;
444
445 if (attr == &dev_attr_iface_enabled.attr)
446 param = ISCSI_NET_PARAM_IFACE_ENABLE;
447 else if (attr == &dev_attr_iface_vlan_id.attr)
448 param = ISCSI_NET_PARAM_VLAN_ID;
449 else if (attr == &dev_attr_iface_vlan_priority.attr)
450 param = ISCSI_NET_PARAM_VLAN_PRIORITY;
451 else if (attr == &dev_attr_iface_vlan_enabled.attr)
452 param = ISCSI_NET_PARAM_VLAN_ENABLED;
453 else if (attr == &dev_attr_iface_mtu.attr)
454 param = ISCSI_NET_PARAM_MTU;
455 else if (attr == &dev_attr_iface_port.attr)
456 param = ISCSI_NET_PARAM_PORT;
457 else if (attr == &dev_attr_iface_ipaddress_state.attr)
458 param = ISCSI_NET_PARAM_IPADDR_STATE;
459 else if (attr == &dev_attr_iface_delayed_ack_en.attr)
460 param = ISCSI_NET_PARAM_DELAYED_ACK_EN;
461 else if (attr == &dev_attr_iface_tcp_nagle_disable.attr)
462 param = ISCSI_NET_PARAM_TCP_NAGLE_DISABLE;
463 else if (attr == &dev_attr_iface_tcp_wsf_disable.attr)
464 param = ISCSI_NET_PARAM_TCP_WSF_DISABLE;
465 else if (attr == &dev_attr_iface_tcp_wsf.attr)
466 param = ISCSI_NET_PARAM_TCP_WSF;
467 else if (attr == &dev_attr_iface_tcp_timer_scale.attr)
468 param = ISCSI_NET_PARAM_TCP_TIMER_SCALE;
469 else if (attr == &dev_attr_iface_tcp_timestamp_en.attr)
470 param = ISCSI_NET_PARAM_TCP_TIMESTAMP_EN;
471 else if (attr == &dev_attr_iface_cache_id.attr)
472 param = ISCSI_NET_PARAM_CACHE_ID;
473 else if (attr == &dev_attr_iface_redirect_en.attr)
474 param = ISCSI_NET_PARAM_REDIRECT_EN;
475 else if (attr == &dev_attr_iface_def_taskmgmt_tmo.attr)
476 param = ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO;
477 else if (attr == &dev_attr_iface_header_digest.attr)
478 param = ISCSI_IFACE_PARAM_HDRDGST_EN;
479 else if (attr == &dev_attr_iface_data_digest.attr)
480 param = ISCSI_IFACE_PARAM_DATADGST_EN;
481 else if (attr == &dev_attr_iface_immediate_data.attr)
482 param = ISCSI_IFACE_PARAM_IMM_DATA_EN;
483 else if (attr == &dev_attr_iface_initial_r2t.attr)
484 param = ISCSI_IFACE_PARAM_INITIAL_R2T_EN;
485 else if (attr == &dev_attr_iface_data_seq_in_order.attr)
486 param = ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN;
487 else if (attr == &dev_attr_iface_data_pdu_in_order.attr)
488 param = ISCSI_IFACE_PARAM_PDU_INORDER_EN;
489 else if (attr == &dev_attr_iface_erl.attr)
490 param = ISCSI_IFACE_PARAM_ERL;
491 else if (attr == &dev_attr_iface_max_recv_dlength.attr)
492 param = ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH;
493 else if (attr == &dev_attr_iface_first_burst_len.attr)
494 param = ISCSI_IFACE_PARAM_FIRST_BURST;
495 else if (attr == &dev_attr_iface_max_outstanding_r2t.attr)
496 param = ISCSI_IFACE_PARAM_MAX_R2T;
497 else if (attr == &dev_attr_iface_max_burst_len.attr)
498 param = ISCSI_IFACE_PARAM_MAX_BURST;
499 else if (attr == &dev_attr_iface_chap_auth.attr)
500 param = ISCSI_IFACE_PARAM_CHAP_AUTH_EN;
501 else if (attr == &dev_attr_iface_bidi_chap.attr)
502 param = ISCSI_IFACE_PARAM_BIDI_CHAP_EN;
503 else if (attr == &dev_attr_iface_discovery_auth_optional.attr)
504 param = ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL;
505 else if (attr == &dev_attr_iface_discovery_logout.attr)
506 param = ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN;
507 else if (attr == &dev_attr_iface_strict_login_comp_en.attr)
508 param = ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN;
509 else if (attr == &dev_attr_iface_initiator_name.attr)
510 param = ISCSI_IFACE_PARAM_INITIATOR_NAME;
511 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4) {
512 if (attr == &dev_attr_ipv4_iface_ipaddress.attr)
513 param = ISCSI_NET_PARAM_IPV4_ADDR;
514 else if (attr == &dev_attr_ipv4_iface_gateway.attr)
515 param = ISCSI_NET_PARAM_IPV4_GW;
516 else if (attr == &dev_attr_ipv4_iface_subnet.attr)
517 param = ISCSI_NET_PARAM_IPV4_SUBNET;
518 else if (attr == &dev_attr_ipv4_iface_bootproto.attr)
519 param = ISCSI_NET_PARAM_IPV4_BOOTPROTO;
520 else if (attr ==
521 &dev_attr_ipv4_iface_dhcp_dns_address_en.attr)
522 param = ISCSI_NET_PARAM_IPV4_DHCP_DNS_ADDR_EN;
523 else if (attr ==
524 &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr)
525 param = ISCSI_NET_PARAM_IPV4_DHCP_SLP_DA_EN;
526 else if (attr == &dev_attr_ipv4_iface_tos_en.attr)
527 param = ISCSI_NET_PARAM_IPV4_TOS_EN;
528 else if (attr == &dev_attr_ipv4_iface_tos.attr)
529 param = ISCSI_NET_PARAM_IPV4_TOS;
530 else if (attr == &dev_attr_ipv4_iface_grat_arp_en.attr)
531 param = ISCSI_NET_PARAM_IPV4_GRAT_ARP_EN;
532 else if (attr ==
533 &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr)
534 param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID_EN;
535 else if (attr == &dev_attr_ipv4_iface_dhcp_alt_client_id.attr)
536 param = ISCSI_NET_PARAM_IPV4_DHCP_ALT_CLIENT_ID;
537 else if (attr ==
538 &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr)
539 param = ISCSI_NET_PARAM_IPV4_DHCP_REQ_VENDOR_ID_EN;
540 else if (attr ==
541 &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr)
542 param = ISCSI_NET_PARAM_IPV4_DHCP_USE_VENDOR_ID_EN;
543 else if (attr == &dev_attr_ipv4_iface_dhcp_vendor_id.attr)
544 param = ISCSI_NET_PARAM_IPV4_DHCP_VENDOR_ID;
545 else if (attr ==
546 &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr)
547 param = ISCSI_NET_PARAM_IPV4_DHCP_LEARN_IQN_EN;
548 else if (attr ==
549 &dev_attr_ipv4_iface_fragment_disable.attr)
550 param = ISCSI_NET_PARAM_IPV4_FRAGMENT_DISABLE;
551 else if (attr ==
552 &dev_attr_ipv4_iface_incoming_forwarding_en.attr)
553 param = ISCSI_NET_PARAM_IPV4_IN_FORWARD_EN;
554 else if (attr == &dev_attr_ipv4_iface_ttl.attr)
555 param = ISCSI_NET_PARAM_IPV4_TTL;
556 else
557 return 0;
558 } else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6) {
559 if (attr == &dev_attr_ipv6_iface_ipaddress.attr)
560 param = ISCSI_NET_PARAM_IPV6_ADDR;
561 else if (attr == &dev_attr_ipv6_iface_link_local_addr.attr)
562 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL;
563 else if (attr == &dev_attr_ipv6_iface_router_addr.attr)
564 param = ISCSI_NET_PARAM_IPV6_ROUTER;
565 else if (attr == &dev_attr_ipv6_iface_ipaddr_autocfg.attr)
566 param = ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG;
567 else if (attr == &dev_attr_ipv6_iface_link_local_autocfg.attr)
568 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG;
569 else if (attr == &dev_attr_ipv6_iface_link_local_state.attr)
570 param = ISCSI_NET_PARAM_IPV6_LINKLOCAL_STATE;
571 else if (attr == &dev_attr_ipv6_iface_router_state.attr)
572 param = ISCSI_NET_PARAM_IPV6_ROUTER_STATE;
573 else if (attr ==
574 &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr)
575 param = ISCSI_NET_PARAM_IPV6_GRAT_NEIGHBOR_ADV_EN;
576 else if (attr == &dev_attr_ipv6_iface_mld_en.attr)
577 param = ISCSI_NET_PARAM_IPV6_MLD_EN;
578 else if (attr == &dev_attr_ipv6_iface_flow_label.attr)
579 param = ISCSI_NET_PARAM_IPV6_FLOW_LABEL;
580 else if (attr == &dev_attr_ipv6_iface_traffic_class.attr)
581 param = ISCSI_NET_PARAM_IPV6_TRAFFIC_CLASS;
582 else if (attr == &dev_attr_ipv6_iface_hop_limit.attr)
583 param = ISCSI_NET_PARAM_IPV6_HOP_LIMIT;
584 else if (attr == &dev_attr_ipv6_iface_nd_reachable_tmo.attr)
585 param = ISCSI_NET_PARAM_IPV6_ND_REACHABLE_TMO;
586 else if (attr == &dev_attr_ipv6_iface_nd_rexmit_time.attr)
587 param = ISCSI_NET_PARAM_IPV6_ND_REXMIT_TIME;
588 else if (attr == &dev_attr_ipv6_iface_nd_stale_tmo.attr)
589 param = ISCSI_NET_PARAM_IPV6_ND_STALE_TMO;
590 else if (attr == &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr)
591 param = ISCSI_NET_PARAM_IPV6_DUP_ADDR_DETECT_CNT;
592 else if (attr == &dev_attr_ipv6_iface_router_adv_link_mtu.attr)
593 param = ISCSI_NET_PARAM_IPV6_RTR_ADV_LINK_MTU;
594 else
595 return 0;
596 } else {
597 WARN_ONCE(1, "Invalid iface attr");
598 return 0;
599 }
600
601 switch (param) {
602 case ISCSI_IFACE_PARAM_DEF_TASKMGMT_TMO:
603 case ISCSI_IFACE_PARAM_HDRDGST_EN:
604 case ISCSI_IFACE_PARAM_DATADGST_EN:
605 case ISCSI_IFACE_PARAM_IMM_DATA_EN:
606 case ISCSI_IFACE_PARAM_INITIAL_R2T_EN:
607 case ISCSI_IFACE_PARAM_DATASEQ_INORDER_EN:
608 case ISCSI_IFACE_PARAM_PDU_INORDER_EN:
609 case ISCSI_IFACE_PARAM_ERL:
610 case ISCSI_IFACE_PARAM_MAX_RECV_DLENGTH:
611 case ISCSI_IFACE_PARAM_FIRST_BURST:
612 case ISCSI_IFACE_PARAM_MAX_R2T:
613 case ISCSI_IFACE_PARAM_MAX_BURST:
614 case ISCSI_IFACE_PARAM_CHAP_AUTH_EN:
615 case ISCSI_IFACE_PARAM_BIDI_CHAP_EN:
616 case ISCSI_IFACE_PARAM_DISCOVERY_AUTH_OPTIONAL:
617 case ISCSI_IFACE_PARAM_DISCOVERY_LOGOUT_EN:
618 case ISCSI_IFACE_PARAM_STRICT_LOGIN_COMP_EN:
619 case ISCSI_IFACE_PARAM_INITIATOR_NAME:
620 param_type = ISCSI_IFACE_PARAM;
621 break;
622 default:
623 param_type = ISCSI_NET_PARAM;
624 }
625
626 return t->attr_is_visible(param_type, param);
627}
628
629static struct attribute *iscsi_iface_attrs[] = {
630 &dev_attr_iface_enabled.attr,
631 &dev_attr_iface_vlan_id.attr,
632 &dev_attr_iface_vlan_priority.attr,
633 &dev_attr_iface_vlan_enabled.attr,
634 &dev_attr_ipv4_iface_ipaddress.attr,
635 &dev_attr_ipv4_iface_gateway.attr,
636 &dev_attr_ipv4_iface_subnet.attr,
637 &dev_attr_ipv4_iface_bootproto.attr,
638 &dev_attr_ipv6_iface_ipaddress.attr,
639 &dev_attr_ipv6_iface_link_local_addr.attr,
640 &dev_attr_ipv6_iface_router_addr.attr,
641 &dev_attr_ipv6_iface_ipaddr_autocfg.attr,
642 &dev_attr_ipv6_iface_link_local_autocfg.attr,
643 &dev_attr_iface_mtu.attr,
644 &dev_attr_iface_port.attr,
645 &dev_attr_iface_ipaddress_state.attr,
646 &dev_attr_iface_delayed_ack_en.attr,
647 &dev_attr_iface_tcp_nagle_disable.attr,
648 &dev_attr_iface_tcp_wsf_disable.attr,
649 &dev_attr_iface_tcp_wsf.attr,
650 &dev_attr_iface_tcp_timer_scale.attr,
651 &dev_attr_iface_tcp_timestamp_en.attr,
652 &dev_attr_iface_cache_id.attr,
653 &dev_attr_iface_redirect_en.attr,
654 &dev_attr_iface_def_taskmgmt_tmo.attr,
655 &dev_attr_iface_header_digest.attr,
656 &dev_attr_iface_data_digest.attr,
657 &dev_attr_iface_immediate_data.attr,
658 &dev_attr_iface_initial_r2t.attr,
659 &dev_attr_iface_data_seq_in_order.attr,
660 &dev_attr_iface_data_pdu_in_order.attr,
661 &dev_attr_iface_erl.attr,
662 &dev_attr_iface_max_recv_dlength.attr,
663 &dev_attr_iface_first_burst_len.attr,
664 &dev_attr_iface_max_outstanding_r2t.attr,
665 &dev_attr_iface_max_burst_len.attr,
666 &dev_attr_iface_chap_auth.attr,
667 &dev_attr_iface_bidi_chap.attr,
668 &dev_attr_iface_discovery_auth_optional.attr,
669 &dev_attr_iface_discovery_logout.attr,
670 &dev_attr_iface_strict_login_comp_en.attr,
671 &dev_attr_iface_initiator_name.attr,
672 &dev_attr_ipv4_iface_dhcp_dns_address_en.attr,
673 &dev_attr_ipv4_iface_dhcp_slp_da_info_en.attr,
674 &dev_attr_ipv4_iface_tos_en.attr,
675 &dev_attr_ipv4_iface_tos.attr,
676 &dev_attr_ipv4_iface_grat_arp_en.attr,
677 &dev_attr_ipv4_iface_dhcp_alt_client_id_en.attr,
678 &dev_attr_ipv4_iface_dhcp_alt_client_id.attr,
679 &dev_attr_ipv4_iface_dhcp_req_vendor_id_en.attr,
680 &dev_attr_ipv4_iface_dhcp_use_vendor_id_en.attr,
681 &dev_attr_ipv4_iface_dhcp_vendor_id.attr,
682 &dev_attr_ipv4_iface_dhcp_learn_iqn_en.attr,
683 &dev_attr_ipv4_iface_fragment_disable.attr,
684 &dev_attr_ipv4_iface_incoming_forwarding_en.attr,
685 &dev_attr_ipv4_iface_ttl.attr,
686 &dev_attr_ipv6_iface_link_local_state.attr,
687 &dev_attr_ipv6_iface_router_state.attr,
688 &dev_attr_ipv6_iface_grat_neighbor_adv_en.attr,
689 &dev_attr_ipv6_iface_mld_en.attr,
690 &dev_attr_ipv6_iface_flow_label.attr,
691 &dev_attr_ipv6_iface_traffic_class.attr,
692 &dev_attr_ipv6_iface_hop_limit.attr,
693 &dev_attr_ipv6_iface_nd_reachable_tmo.attr,
694 &dev_attr_ipv6_iface_nd_rexmit_time.attr,
695 &dev_attr_ipv6_iface_nd_stale_tmo.attr,
696 &dev_attr_ipv6_iface_dup_addr_detect_cnt.attr,
697 &dev_attr_ipv6_iface_router_adv_link_mtu.attr,
698 NULL,
699};
700
701static struct attribute_group iscsi_iface_group = {
702 .attrs = iscsi_iface_attrs,
703 .is_visible = iscsi_iface_attr_is_visible,
704};
705
706/* convert iscsi_ipaddress_state values to ascii string name */
707static const struct {
708 enum iscsi_ipaddress_state value;
709 char *name;
710} iscsi_ipaddress_state_names[] = {
711 {ISCSI_IPDDRESS_STATE_UNCONFIGURED, "Unconfigured" },
712 {ISCSI_IPDDRESS_STATE_ACQUIRING, "Acquiring" },
713 {ISCSI_IPDDRESS_STATE_TENTATIVE, "Tentative" },
714 {ISCSI_IPDDRESS_STATE_VALID, "Valid" },
715 {ISCSI_IPDDRESS_STATE_DISABLING, "Disabling" },
716 {ISCSI_IPDDRESS_STATE_INVALID, "Invalid" },
717 {ISCSI_IPDDRESS_STATE_DEPRECATED, "Deprecated" },
718};
719
720char *iscsi_get_ipaddress_state_name(enum iscsi_ipaddress_state port_state)
721{
722 int i;
723 char *state = NULL;
724
725 for (i = 0; i < ARRAY_SIZE(iscsi_ipaddress_state_names); i++) {
726 if (iscsi_ipaddress_state_names[i].value == port_state) {
727 state = iscsi_ipaddress_state_names[i].name;
728 break;
729 }
730 }
731 return state;
732}
733EXPORT_SYMBOL_GPL(iscsi_get_ipaddress_state_name);
734
735/* convert iscsi_router_state values to ascii string name */
736static const struct {
737 enum iscsi_router_state value;
738 char *name;
739} iscsi_router_state_names[] = {
740 {ISCSI_ROUTER_STATE_UNKNOWN, "Unknown" },
741 {ISCSI_ROUTER_STATE_ADVERTISED, "Advertised" },
742 {ISCSI_ROUTER_STATE_MANUAL, "Manual" },
743 {ISCSI_ROUTER_STATE_STALE, "Stale" },
744};
745
746char *iscsi_get_router_state_name(enum iscsi_router_state router_state)
747{
748 int i;
749 char *state = NULL;
750
751 for (i = 0; i < ARRAY_SIZE(iscsi_router_state_names); i++) {
752 if (iscsi_router_state_names[i].value == router_state) {
753 state = iscsi_router_state_names[i].name;
754 break;
755 }
756 }
757 return state;
758}
759EXPORT_SYMBOL_GPL(iscsi_get_router_state_name);
760
761struct iscsi_iface *
762iscsi_create_iface(struct Scsi_Host *shost, struct iscsi_transport *transport,
763 uint32_t iface_type, uint32_t iface_num, int dd_size)
764{
765 struct iscsi_iface *iface;
766 int err;
767
768 iface = kzalloc(sizeof(*iface) + dd_size, GFP_KERNEL);
769 if (!iface)
770 return NULL;
771
772 iface->transport = transport;
773 iface->iface_type = iface_type;
774 iface->iface_num = iface_num;
775 iface->dev.release = iscsi_iface_release;
776 iface->dev.class = &iscsi_iface_class;
777 /* parent reference released in iscsi_iface_release */
778 iface->dev.parent = get_device(&shost->shost_gendev);
779 if (iface_type == ISCSI_IFACE_TYPE_IPV4)
780 dev_set_name(&iface->dev, "ipv4-iface-%u-%u", shost->host_no,
781 iface_num);
782 else
783 dev_set_name(&iface->dev, "ipv6-iface-%u-%u", shost->host_no,
784 iface_num);
785
786 err = device_register(&iface->dev);
787 if (err)
788 goto free_iface;
789
790 err = sysfs_create_group(&iface->dev.kobj, &iscsi_iface_group);
791 if (err)
792 goto unreg_iface;
793
794 if (dd_size)
795 iface->dd_data = &iface[1];
796 return iface;
797
798unreg_iface:
799 device_unregister(&iface->dev);
800 return NULL;
801
802free_iface:
803 put_device(iface->dev.parent);
804 kfree(iface);
805 return NULL;
806}
807EXPORT_SYMBOL_GPL(iscsi_create_iface);
808
809void iscsi_destroy_iface(struct iscsi_iface *iface)
810{
811 sysfs_remove_group(&iface->dev.kobj, &iscsi_iface_group);
812 device_unregister(&iface->dev);
813}
814EXPORT_SYMBOL_GPL(iscsi_destroy_iface);
815
816/*
817 * Interface to display flash node params to sysfs
818 */
819
820#define ISCSI_FLASHNODE_ATTR(_prefix, _name, _mode, _show, _store) \
821struct device_attribute dev_attr_##_prefix##_##_name = \
822 __ATTR(_name, _mode, _show, _store)
823
824/* flash node session attrs show */
825#define iscsi_flashnode_sess_attr_show(type, name, param) \
826static ssize_t \
827show_##type##_##name(struct device *dev, struct device_attribute *attr, \
828 char *buf) \
829{ \
830 struct iscsi_bus_flash_session *fnode_sess = \
831 iscsi_dev_to_flash_session(dev);\
832 struct iscsi_transport *t = fnode_sess->transport; \
833 return t->get_flashnode_param(fnode_sess, param, buf); \
834} \
835
836
837#define iscsi_flashnode_sess_attr(type, name, param) \
838 iscsi_flashnode_sess_attr_show(type, name, param) \
839static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
840 show_##type##_##name, NULL);
841
842/* Flash node session attributes */
843
844iscsi_flashnode_sess_attr(fnode, auto_snd_tgt_disable,
845 ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE);
846iscsi_flashnode_sess_attr(fnode, discovery_session,
847 ISCSI_FLASHNODE_DISCOVERY_SESS);
848iscsi_flashnode_sess_attr(fnode, portal_type, ISCSI_FLASHNODE_PORTAL_TYPE);
849iscsi_flashnode_sess_attr(fnode, entry_enable, ISCSI_FLASHNODE_ENTRY_EN);
850iscsi_flashnode_sess_attr(fnode, immediate_data, ISCSI_FLASHNODE_IMM_DATA_EN);
851iscsi_flashnode_sess_attr(fnode, initial_r2t, ISCSI_FLASHNODE_INITIAL_R2T_EN);
852iscsi_flashnode_sess_attr(fnode, data_seq_in_order,
853 ISCSI_FLASHNODE_DATASEQ_INORDER);
854iscsi_flashnode_sess_attr(fnode, data_pdu_in_order,
855 ISCSI_FLASHNODE_PDU_INORDER);
856iscsi_flashnode_sess_attr(fnode, chap_auth, ISCSI_FLASHNODE_CHAP_AUTH_EN);
857iscsi_flashnode_sess_attr(fnode, discovery_logout,
858 ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN);
859iscsi_flashnode_sess_attr(fnode, bidi_chap, ISCSI_FLASHNODE_BIDI_CHAP_EN);
860iscsi_flashnode_sess_attr(fnode, discovery_auth_optional,
861 ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL);
862iscsi_flashnode_sess_attr(fnode, erl, ISCSI_FLASHNODE_ERL);
863iscsi_flashnode_sess_attr(fnode, first_burst_len, ISCSI_FLASHNODE_FIRST_BURST);
864iscsi_flashnode_sess_attr(fnode, def_time2wait, ISCSI_FLASHNODE_DEF_TIME2WAIT);
865iscsi_flashnode_sess_attr(fnode, def_time2retain,
866 ISCSI_FLASHNODE_DEF_TIME2RETAIN);
867iscsi_flashnode_sess_attr(fnode, max_outstanding_r2t, ISCSI_FLASHNODE_MAX_R2T);
868iscsi_flashnode_sess_attr(fnode, isid, ISCSI_FLASHNODE_ISID);
869iscsi_flashnode_sess_attr(fnode, tsid, ISCSI_FLASHNODE_TSID);
870iscsi_flashnode_sess_attr(fnode, max_burst_len, ISCSI_FLASHNODE_MAX_BURST);
871iscsi_flashnode_sess_attr(fnode, def_taskmgmt_tmo,
872 ISCSI_FLASHNODE_DEF_TASKMGMT_TMO);
873iscsi_flashnode_sess_attr(fnode, targetalias, ISCSI_FLASHNODE_ALIAS);
874iscsi_flashnode_sess_attr(fnode, targetname, ISCSI_FLASHNODE_NAME);
875iscsi_flashnode_sess_attr(fnode, tpgt, ISCSI_FLASHNODE_TPGT);
876iscsi_flashnode_sess_attr(fnode, discovery_parent_idx,
877 ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX);
878iscsi_flashnode_sess_attr(fnode, discovery_parent_type,
879 ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE);
880iscsi_flashnode_sess_attr(fnode, chap_in_idx, ISCSI_FLASHNODE_CHAP_IN_IDX);
881iscsi_flashnode_sess_attr(fnode, chap_out_idx, ISCSI_FLASHNODE_CHAP_OUT_IDX);
882iscsi_flashnode_sess_attr(fnode, username, ISCSI_FLASHNODE_USERNAME);
883iscsi_flashnode_sess_attr(fnode, username_in, ISCSI_FLASHNODE_USERNAME_IN);
884iscsi_flashnode_sess_attr(fnode, password, ISCSI_FLASHNODE_PASSWORD);
885iscsi_flashnode_sess_attr(fnode, password_in, ISCSI_FLASHNODE_PASSWORD_IN);
886iscsi_flashnode_sess_attr(fnode, is_boot_target, ISCSI_FLASHNODE_IS_BOOT_TGT);
887
888static struct attribute *iscsi_flashnode_sess_attrs[] = {
889 &dev_attr_fnode_auto_snd_tgt_disable.attr,
890 &dev_attr_fnode_discovery_session.attr,
891 &dev_attr_fnode_portal_type.attr,
892 &dev_attr_fnode_entry_enable.attr,
893 &dev_attr_fnode_immediate_data.attr,
894 &dev_attr_fnode_initial_r2t.attr,
895 &dev_attr_fnode_data_seq_in_order.attr,
896 &dev_attr_fnode_data_pdu_in_order.attr,
897 &dev_attr_fnode_chap_auth.attr,
898 &dev_attr_fnode_discovery_logout.attr,
899 &dev_attr_fnode_bidi_chap.attr,
900 &dev_attr_fnode_discovery_auth_optional.attr,
901 &dev_attr_fnode_erl.attr,
902 &dev_attr_fnode_first_burst_len.attr,
903 &dev_attr_fnode_def_time2wait.attr,
904 &dev_attr_fnode_def_time2retain.attr,
905 &dev_attr_fnode_max_outstanding_r2t.attr,
906 &dev_attr_fnode_isid.attr,
907 &dev_attr_fnode_tsid.attr,
908 &dev_attr_fnode_max_burst_len.attr,
909 &dev_attr_fnode_def_taskmgmt_tmo.attr,
910 &dev_attr_fnode_targetalias.attr,
911 &dev_attr_fnode_targetname.attr,
912 &dev_attr_fnode_tpgt.attr,
913 &dev_attr_fnode_discovery_parent_idx.attr,
914 &dev_attr_fnode_discovery_parent_type.attr,
915 &dev_attr_fnode_chap_in_idx.attr,
916 &dev_attr_fnode_chap_out_idx.attr,
917 &dev_attr_fnode_username.attr,
918 &dev_attr_fnode_username_in.attr,
919 &dev_attr_fnode_password.attr,
920 &dev_attr_fnode_password_in.attr,
921 &dev_attr_fnode_is_boot_target.attr,
922 NULL,
923};
924
925static umode_t iscsi_flashnode_sess_attr_is_visible(struct kobject *kobj,
926 struct attribute *attr,
927 int i)
928{
929 struct device *dev = container_of(kobj, struct device, kobj);
930 struct iscsi_bus_flash_session *fnode_sess =
931 iscsi_dev_to_flash_session(dev);
932 struct iscsi_transport *t = fnode_sess->transport;
933 int param;
934
935 if (attr == &dev_attr_fnode_auto_snd_tgt_disable.attr) {
936 param = ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE;
937 } else if (attr == &dev_attr_fnode_discovery_session.attr) {
938 param = ISCSI_FLASHNODE_DISCOVERY_SESS;
939 } else if (attr == &dev_attr_fnode_portal_type.attr) {
940 param = ISCSI_FLASHNODE_PORTAL_TYPE;
941 } else if (attr == &dev_attr_fnode_entry_enable.attr) {
942 param = ISCSI_FLASHNODE_ENTRY_EN;
943 } else if (attr == &dev_attr_fnode_immediate_data.attr) {
944 param = ISCSI_FLASHNODE_IMM_DATA_EN;
945 } else if (attr == &dev_attr_fnode_initial_r2t.attr) {
946 param = ISCSI_FLASHNODE_INITIAL_R2T_EN;
947 } else if (attr == &dev_attr_fnode_data_seq_in_order.attr) {
948 param = ISCSI_FLASHNODE_DATASEQ_INORDER;
949 } else if (attr == &dev_attr_fnode_data_pdu_in_order.attr) {
950 param = ISCSI_FLASHNODE_PDU_INORDER;
951 } else if (attr == &dev_attr_fnode_chap_auth.attr) {
952 param = ISCSI_FLASHNODE_CHAP_AUTH_EN;
953 } else if (attr == &dev_attr_fnode_discovery_logout.attr) {
954 param = ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN;
955 } else if (attr == &dev_attr_fnode_bidi_chap.attr) {
956 param = ISCSI_FLASHNODE_BIDI_CHAP_EN;
957 } else if (attr == &dev_attr_fnode_discovery_auth_optional.attr) {
958 param = ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL;
959 } else if (attr == &dev_attr_fnode_erl.attr) {
960 param = ISCSI_FLASHNODE_ERL;
961 } else if (attr == &dev_attr_fnode_first_burst_len.attr) {
962 param = ISCSI_FLASHNODE_FIRST_BURST;
963 } else if (attr == &dev_attr_fnode_def_time2wait.attr) {
964 param = ISCSI_FLASHNODE_DEF_TIME2WAIT;
965 } else if (attr == &dev_attr_fnode_def_time2retain.attr) {
966 param = ISCSI_FLASHNODE_DEF_TIME2RETAIN;
967 } else if (attr == &dev_attr_fnode_max_outstanding_r2t.attr) {
968 param = ISCSI_FLASHNODE_MAX_R2T;
969 } else if (attr == &dev_attr_fnode_isid.attr) {
970 param = ISCSI_FLASHNODE_ISID;
971 } else if (attr == &dev_attr_fnode_tsid.attr) {
972 param = ISCSI_FLASHNODE_TSID;
973 } else if (attr == &dev_attr_fnode_max_burst_len.attr) {
974 param = ISCSI_FLASHNODE_MAX_BURST;
975 } else if (attr == &dev_attr_fnode_def_taskmgmt_tmo.attr) {
976 param = ISCSI_FLASHNODE_DEF_TASKMGMT_TMO;
977 } else if (attr == &dev_attr_fnode_targetalias.attr) {
978 param = ISCSI_FLASHNODE_ALIAS;
979 } else if (attr == &dev_attr_fnode_targetname.attr) {
980 param = ISCSI_FLASHNODE_NAME;
981 } else if (attr == &dev_attr_fnode_tpgt.attr) {
982 param = ISCSI_FLASHNODE_TPGT;
983 } else if (attr == &dev_attr_fnode_discovery_parent_idx.attr) {
984 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX;
985 } else if (attr == &dev_attr_fnode_discovery_parent_type.attr) {
986 param = ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE;
987 } else if (attr == &dev_attr_fnode_chap_in_idx.attr) {
988 param = ISCSI_FLASHNODE_CHAP_IN_IDX;
989 } else if (attr == &dev_attr_fnode_chap_out_idx.attr) {
990 param = ISCSI_FLASHNODE_CHAP_OUT_IDX;
991 } else if (attr == &dev_attr_fnode_username.attr) {
992 param = ISCSI_FLASHNODE_USERNAME;
993 } else if (attr == &dev_attr_fnode_username_in.attr) {
994 param = ISCSI_FLASHNODE_USERNAME_IN;
995 } else if (attr == &dev_attr_fnode_password.attr) {
996 param = ISCSI_FLASHNODE_PASSWORD;
997 } else if (attr == &dev_attr_fnode_password_in.attr) {
998 param = ISCSI_FLASHNODE_PASSWORD_IN;
999 } else if (attr == &dev_attr_fnode_is_boot_target.attr) {
1000 param = ISCSI_FLASHNODE_IS_BOOT_TGT;
1001 } else {
1002 WARN_ONCE(1, "Invalid flashnode session attr");
1003 return 0;
1004 }
1005
1006 return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
1007}
1008
1009static struct attribute_group iscsi_flashnode_sess_attr_group = {
1010 .attrs = iscsi_flashnode_sess_attrs,
1011 .is_visible = iscsi_flashnode_sess_attr_is_visible,
1012};
1013
1014static const struct attribute_group *iscsi_flashnode_sess_attr_groups[] = {
1015 &iscsi_flashnode_sess_attr_group,
1016 NULL,
1017};
1018
1019static void iscsi_flashnode_sess_release(struct device *dev)
1020{
1021 struct iscsi_bus_flash_session *fnode_sess =
1022 iscsi_dev_to_flash_session(dev);
1023
1024 kfree(fnode_sess->targetname);
1025 kfree(fnode_sess->targetalias);
1026 kfree(fnode_sess->portal_type);
1027 kfree(fnode_sess);
1028}
1029
1030static const struct device_type iscsi_flashnode_sess_dev_type = {
1031 .name = "iscsi_flashnode_sess_dev_type",
1032 .groups = iscsi_flashnode_sess_attr_groups,
1033 .release = iscsi_flashnode_sess_release,
1034};
1035
1036/* flash node connection attrs show */
1037#define iscsi_flashnode_conn_attr_show(type, name, param) \
1038static ssize_t \
1039show_##type##_##name(struct device *dev, struct device_attribute *attr, \
1040 char *buf) \
1041{ \
1042 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);\
1043 struct iscsi_bus_flash_session *fnode_sess = \
1044 iscsi_flash_conn_to_flash_session(fnode_conn);\
1045 struct iscsi_transport *t = fnode_conn->transport; \
1046 return t->get_flashnode_param(fnode_sess, param, buf); \
1047} \
1048
1049
1050#define iscsi_flashnode_conn_attr(type, name, param) \
1051 iscsi_flashnode_conn_attr_show(type, name, param) \
1052static ISCSI_FLASHNODE_ATTR(type, name, S_IRUGO, \
1053 show_##type##_##name, NULL);
1054
1055/* Flash node connection attributes */
1056
1057iscsi_flashnode_conn_attr(fnode, is_fw_assigned_ipv6,
1058 ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6);
1059iscsi_flashnode_conn_attr(fnode, header_digest, ISCSI_FLASHNODE_HDR_DGST_EN);
1060iscsi_flashnode_conn_attr(fnode, data_digest, ISCSI_FLASHNODE_DATA_DGST_EN);
1061iscsi_flashnode_conn_attr(fnode, snack_req, ISCSI_FLASHNODE_SNACK_REQ_EN);
1062iscsi_flashnode_conn_attr(fnode, tcp_timestamp_stat,
1063 ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT);
1064iscsi_flashnode_conn_attr(fnode, tcp_nagle_disable,
1065 ISCSI_FLASHNODE_TCP_NAGLE_DISABLE);
1066iscsi_flashnode_conn_attr(fnode, tcp_wsf_disable,
1067 ISCSI_FLASHNODE_TCP_WSF_DISABLE);
1068iscsi_flashnode_conn_attr(fnode, tcp_timer_scale,
1069 ISCSI_FLASHNODE_TCP_TIMER_SCALE);
1070iscsi_flashnode_conn_attr(fnode, tcp_timestamp_enable,
1071 ISCSI_FLASHNODE_TCP_TIMESTAMP_EN);
1072iscsi_flashnode_conn_attr(fnode, fragment_disable,
1073 ISCSI_FLASHNODE_IP_FRAG_DISABLE);
1074iscsi_flashnode_conn_attr(fnode, keepalive_tmo, ISCSI_FLASHNODE_KEEPALIVE_TMO);
1075iscsi_flashnode_conn_attr(fnode, port, ISCSI_FLASHNODE_PORT);
1076iscsi_flashnode_conn_attr(fnode, ipaddress, ISCSI_FLASHNODE_IPADDR);
1077iscsi_flashnode_conn_attr(fnode, max_recv_dlength,
1078 ISCSI_FLASHNODE_MAX_RECV_DLENGTH);
1079iscsi_flashnode_conn_attr(fnode, max_xmit_dlength,
1080 ISCSI_FLASHNODE_MAX_XMIT_DLENGTH);
1081iscsi_flashnode_conn_attr(fnode, local_port, ISCSI_FLASHNODE_LOCAL_PORT);
1082iscsi_flashnode_conn_attr(fnode, ipv4_tos, ISCSI_FLASHNODE_IPV4_TOS);
1083iscsi_flashnode_conn_attr(fnode, ipv6_traffic_class, ISCSI_FLASHNODE_IPV6_TC);
1084iscsi_flashnode_conn_attr(fnode, ipv6_flow_label,
1085 ISCSI_FLASHNODE_IPV6_FLOW_LABEL);
1086iscsi_flashnode_conn_attr(fnode, redirect_ipaddr,
1087 ISCSI_FLASHNODE_REDIRECT_IPADDR);
1088iscsi_flashnode_conn_attr(fnode, max_segment_size,
1089 ISCSI_FLASHNODE_MAX_SEGMENT_SIZE);
1090iscsi_flashnode_conn_attr(fnode, link_local_ipv6,
1091 ISCSI_FLASHNODE_LINK_LOCAL_IPV6);
1092iscsi_flashnode_conn_attr(fnode, tcp_xmit_wsf, ISCSI_FLASHNODE_TCP_XMIT_WSF);
1093iscsi_flashnode_conn_attr(fnode, tcp_recv_wsf, ISCSI_FLASHNODE_TCP_RECV_WSF);
1094iscsi_flashnode_conn_attr(fnode, statsn, ISCSI_FLASHNODE_STATSN);
1095iscsi_flashnode_conn_attr(fnode, exp_statsn, ISCSI_FLASHNODE_EXP_STATSN);
1096
1097static struct attribute *iscsi_flashnode_conn_attrs[] = {
1098 &dev_attr_fnode_is_fw_assigned_ipv6.attr,
1099 &dev_attr_fnode_header_digest.attr,
1100 &dev_attr_fnode_data_digest.attr,
1101 &dev_attr_fnode_snack_req.attr,
1102 &dev_attr_fnode_tcp_timestamp_stat.attr,
1103 &dev_attr_fnode_tcp_nagle_disable.attr,
1104 &dev_attr_fnode_tcp_wsf_disable.attr,
1105 &dev_attr_fnode_tcp_timer_scale.attr,
1106 &dev_attr_fnode_tcp_timestamp_enable.attr,
1107 &dev_attr_fnode_fragment_disable.attr,
1108 &dev_attr_fnode_max_recv_dlength.attr,
1109 &dev_attr_fnode_max_xmit_dlength.attr,
1110 &dev_attr_fnode_keepalive_tmo.attr,
1111 &dev_attr_fnode_port.attr,
1112 &dev_attr_fnode_ipaddress.attr,
1113 &dev_attr_fnode_redirect_ipaddr.attr,
1114 &dev_attr_fnode_max_segment_size.attr,
1115 &dev_attr_fnode_local_port.attr,
1116 &dev_attr_fnode_ipv4_tos.attr,
1117 &dev_attr_fnode_ipv6_traffic_class.attr,
1118 &dev_attr_fnode_ipv6_flow_label.attr,
1119 &dev_attr_fnode_link_local_ipv6.attr,
1120 &dev_attr_fnode_tcp_xmit_wsf.attr,
1121 &dev_attr_fnode_tcp_recv_wsf.attr,
1122 &dev_attr_fnode_statsn.attr,
1123 &dev_attr_fnode_exp_statsn.attr,
1124 NULL,
1125};
1126
1127static umode_t iscsi_flashnode_conn_attr_is_visible(struct kobject *kobj,
1128 struct attribute *attr,
1129 int i)
1130{
1131 struct device *dev = container_of(kobj, struct device, kobj);
1132 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
1133 struct iscsi_transport *t = fnode_conn->transport;
1134 int param;
1135
1136 if (attr == &dev_attr_fnode_is_fw_assigned_ipv6.attr) {
1137 param = ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6;
1138 } else if (attr == &dev_attr_fnode_header_digest.attr) {
1139 param = ISCSI_FLASHNODE_HDR_DGST_EN;
1140 } else if (attr == &dev_attr_fnode_data_digest.attr) {
1141 param = ISCSI_FLASHNODE_DATA_DGST_EN;
1142 } else if (attr == &dev_attr_fnode_snack_req.attr) {
1143 param = ISCSI_FLASHNODE_SNACK_REQ_EN;
1144 } else if (attr == &dev_attr_fnode_tcp_timestamp_stat.attr) {
1145 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT;
1146 } else if (attr == &dev_attr_fnode_tcp_nagle_disable.attr) {
1147 param = ISCSI_FLASHNODE_TCP_NAGLE_DISABLE;
1148 } else if (attr == &dev_attr_fnode_tcp_wsf_disable.attr) {
1149 param = ISCSI_FLASHNODE_TCP_WSF_DISABLE;
1150 } else if (attr == &dev_attr_fnode_tcp_timer_scale.attr) {
1151 param = ISCSI_FLASHNODE_TCP_TIMER_SCALE;
1152 } else if (attr == &dev_attr_fnode_tcp_timestamp_enable.attr) {
1153 param = ISCSI_FLASHNODE_TCP_TIMESTAMP_EN;
1154 } else if (attr == &dev_attr_fnode_fragment_disable.attr) {
1155 param = ISCSI_FLASHNODE_IP_FRAG_DISABLE;
1156 } else if (attr == &dev_attr_fnode_max_recv_dlength.attr) {
1157 param = ISCSI_FLASHNODE_MAX_RECV_DLENGTH;
1158 } else if (attr == &dev_attr_fnode_max_xmit_dlength.attr) {
1159 param = ISCSI_FLASHNODE_MAX_XMIT_DLENGTH;
1160 } else if (attr == &dev_attr_fnode_keepalive_tmo.attr) {
1161 param = ISCSI_FLASHNODE_KEEPALIVE_TMO;
1162 } else if (attr == &dev_attr_fnode_port.attr) {
1163 param = ISCSI_FLASHNODE_PORT;
1164 } else if (attr == &dev_attr_fnode_ipaddress.attr) {
1165 param = ISCSI_FLASHNODE_IPADDR;
1166 } else if (attr == &dev_attr_fnode_redirect_ipaddr.attr) {
1167 param = ISCSI_FLASHNODE_REDIRECT_IPADDR;
1168 } else if (attr == &dev_attr_fnode_max_segment_size.attr) {
1169 param = ISCSI_FLASHNODE_MAX_SEGMENT_SIZE;
1170 } else if (attr == &dev_attr_fnode_local_port.attr) {
1171 param = ISCSI_FLASHNODE_LOCAL_PORT;
1172 } else if (attr == &dev_attr_fnode_ipv4_tos.attr) {
1173 param = ISCSI_FLASHNODE_IPV4_TOS;
1174 } else if (attr == &dev_attr_fnode_ipv6_traffic_class.attr) {
1175 param = ISCSI_FLASHNODE_IPV6_TC;
1176 } else if (attr == &dev_attr_fnode_ipv6_flow_label.attr) {
1177 param = ISCSI_FLASHNODE_IPV6_FLOW_LABEL;
1178 } else if (attr == &dev_attr_fnode_link_local_ipv6.attr) {
1179 param = ISCSI_FLASHNODE_LINK_LOCAL_IPV6;
1180 } else if (attr == &dev_attr_fnode_tcp_xmit_wsf.attr) {
1181 param = ISCSI_FLASHNODE_TCP_XMIT_WSF;
1182 } else if (attr == &dev_attr_fnode_tcp_recv_wsf.attr) {
1183 param = ISCSI_FLASHNODE_TCP_RECV_WSF;
1184 } else if (attr == &dev_attr_fnode_statsn.attr) {
1185 param = ISCSI_FLASHNODE_STATSN;
1186 } else if (attr == &dev_attr_fnode_exp_statsn.attr) {
1187 param = ISCSI_FLASHNODE_EXP_STATSN;
1188 } else {
1189 WARN_ONCE(1, "Invalid flashnode connection attr");
1190 return 0;
1191 }
1192
1193 return t->attr_is_visible(ISCSI_FLASHNODE_PARAM, param);
1194}
1195
1196static struct attribute_group iscsi_flashnode_conn_attr_group = {
1197 .attrs = iscsi_flashnode_conn_attrs,
1198 .is_visible = iscsi_flashnode_conn_attr_is_visible,
1199};
1200
1201static const struct attribute_group *iscsi_flashnode_conn_attr_groups[] = {
1202 &iscsi_flashnode_conn_attr_group,
1203 NULL,
1204};
1205
1206static void iscsi_flashnode_conn_release(struct device *dev)
1207{
1208 struct iscsi_bus_flash_conn *fnode_conn = iscsi_dev_to_flash_conn(dev);
1209
1210 kfree(fnode_conn->ipaddress);
1211 kfree(fnode_conn->redirect_ipaddr);
1212 kfree(fnode_conn->link_local_ipv6_addr);
1213 kfree(fnode_conn);
1214}
1215
1216static const struct device_type iscsi_flashnode_conn_dev_type = {
1217 .name = "iscsi_flashnode_conn_dev_type",
1218 .groups = iscsi_flashnode_conn_attr_groups,
1219 .release = iscsi_flashnode_conn_release,
1220};
1221
1222static struct bus_type iscsi_flashnode_bus;
1223
1224int iscsi_flashnode_bus_match(struct device *dev,
1225 struct device_driver *drv)
1226{
1227 if (dev->bus == &iscsi_flashnode_bus)
1228 return 1;
1229 return 0;
1230}
1231EXPORT_SYMBOL_GPL(iscsi_flashnode_bus_match);
1232
1233static struct bus_type iscsi_flashnode_bus = {
1234 .name = "iscsi_flashnode",
1235 .match = &iscsi_flashnode_bus_match,
1236};
1237
1238/**
1239 * iscsi_create_flashnode_sess - Add flashnode session entry in sysfs
1240 * @shost: pointer to host data
1241 * @index: index of flashnode to add in sysfs
1242 * @transport: pointer to transport data
1243 * @dd_size: total size to allocate
1244 *
1245 * Adds a sysfs entry for the flashnode session attributes
1246 *
1247 * Returns:
1248 * pointer to allocated flashnode sess on success
1249 * %NULL on failure
1250 */
1251struct iscsi_bus_flash_session *
1252iscsi_create_flashnode_sess(struct Scsi_Host *shost, int index,
1253 struct iscsi_transport *transport,
1254 int dd_size)
1255{
1256 struct iscsi_bus_flash_session *fnode_sess;
1257 int err;
1258
1259 fnode_sess = kzalloc(sizeof(*fnode_sess) + dd_size, GFP_KERNEL);
1260 if (!fnode_sess)
1261 return NULL;
1262
1263 fnode_sess->transport = transport;
1264 fnode_sess->target_id = index;
1265 fnode_sess->dev.type = &iscsi_flashnode_sess_dev_type;
1266 fnode_sess->dev.bus = &iscsi_flashnode_bus;
1267 fnode_sess->dev.parent = &shost->shost_gendev;
1268 dev_set_name(&fnode_sess->dev, "flashnode_sess-%u:%u",
1269 shost->host_no, index);
1270
1271 err = device_register(&fnode_sess->dev);
1272 if (err)
1273 goto free_fnode_sess;
1274
1275 if (dd_size)
1276 fnode_sess->dd_data = &fnode_sess[1];
1277
1278 return fnode_sess;
1279
1280free_fnode_sess:
1281 kfree(fnode_sess);
1282 return NULL;
1283}
1284EXPORT_SYMBOL_GPL(iscsi_create_flashnode_sess);
1285
1286/**
1287 * iscsi_create_flashnode_conn - Add flashnode conn entry in sysfs
1288 * @shost: pointer to host data
1289 * @fnode_sess: pointer to the parent flashnode session entry
1290 * @transport: pointer to transport data
1291 * @dd_size: total size to allocate
1292 *
1293 * Adds a sysfs entry for the flashnode connection attributes
1294 *
1295 * Returns:
1296 * pointer to allocated flashnode conn on success
1297 * %NULL on failure
1298 */
1299struct iscsi_bus_flash_conn *
1300iscsi_create_flashnode_conn(struct Scsi_Host *shost,
1301 struct iscsi_bus_flash_session *fnode_sess,
1302 struct iscsi_transport *transport,
1303 int dd_size)
1304{
1305 struct iscsi_bus_flash_conn *fnode_conn;
1306 int err;
1307
1308 fnode_conn = kzalloc(sizeof(*fnode_conn) + dd_size, GFP_KERNEL);
1309 if (!fnode_conn)
1310 return NULL;
1311
1312 fnode_conn->transport = transport;
1313 fnode_conn->dev.type = &iscsi_flashnode_conn_dev_type;
1314 fnode_conn->dev.bus = &iscsi_flashnode_bus;
1315 fnode_conn->dev.parent = &fnode_sess->dev;
1316 dev_set_name(&fnode_conn->dev, "flashnode_conn-%u:%u:0",
1317 shost->host_no, fnode_sess->target_id);
1318
1319 err = device_register(&fnode_conn->dev);
1320 if (err)
1321 goto free_fnode_conn;
1322
1323 if (dd_size)
1324 fnode_conn->dd_data = &fnode_conn[1];
1325
1326 return fnode_conn;
1327
1328free_fnode_conn:
1329 kfree(fnode_conn);
1330 return NULL;
1331}
1332EXPORT_SYMBOL_GPL(iscsi_create_flashnode_conn);
1333
1334/**
1335 * iscsi_is_flashnode_conn_dev - verify passed device is to be flashnode conn
1336 * @dev: device to verify
1337 * @data: pointer to data containing value to use for verification
1338 *
1339 * Verifies if the passed device is flashnode conn device
1340 *
1341 * Returns:
1342 * 1 on success
1343 * 0 on failure
1344 */
1345static int iscsi_is_flashnode_conn_dev(struct device *dev, void *data)
1346{
1347 return dev->bus == &iscsi_flashnode_bus;
1348}
1349
1350static int iscsi_destroy_flashnode_conn(struct iscsi_bus_flash_conn *fnode_conn)
1351{
1352 device_unregister(&fnode_conn->dev);
1353 return 0;
1354}
1355
1356static int flashnode_match_index(struct device *dev, void *data)
1357{
1358 struct iscsi_bus_flash_session *fnode_sess = NULL;
1359 int ret = 0;
1360
1361 if (!iscsi_flashnode_bus_match(dev, NULL))
1362 goto exit_match_index;
1363
1364 fnode_sess = iscsi_dev_to_flash_session(dev);
1365 ret = (fnode_sess->target_id == *((int *)data)) ? 1 : 0;
1366
1367exit_match_index:
1368 return ret;
1369}
1370
1371/**
1372 * iscsi_get_flashnode_by_index -finds flashnode session entry by index
1373 * @shost: pointer to host data
1374 * @idx: index to match
1375 *
1376 * Finds the flashnode session object for the passed index
1377 *
1378 * Returns:
1379 * pointer to found flashnode session object on success
1380 * %NULL on failure
1381 */
1382static struct iscsi_bus_flash_session *
1383iscsi_get_flashnode_by_index(struct Scsi_Host *shost, uint32_t idx)
1384{
1385 struct iscsi_bus_flash_session *fnode_sess = NULL;
1386 struct device *dev;
1387
1388 dev = device_find_child(&shost->shost_gendev, &idx,
1389 flashnode_match_index);
1390 if (dev)
1391 fnode_sess = iscsi_dev_to_flash_session(dev);
1392
1393 return fnode_sess;
1394}
1395
1396/**
1397 * iscsi_find_flashnode_sess - finds flashnode session entry
1398 * @shost: pointer to host data
1399 * @data: pointer to data containing value to use for comparison
1400 * @fn: function pointer that does actual comparison
1401 *
1402 * Finds the flashnode session object comparing the data passed using logic
1403 * defined in passed function pointer
1404 *
1405 * Returns:
1406 * pointer to found flashnode session device object on success
1407 * %NULL on failure
1408 */
1409struct device *
1410iscsi_find_flashnode_sess(struct Scsi_Host *shost, void *data,
1411 int (*fn)(struct device *dev, void *data))
1412{
1413 return device_find_child(&shost->shost_gendev, data, fn);
1414}
1415EXPORT_SYMBOL_GPL(iscsi_find_flashnode_sess);
1416
1417/**
1418 * iscsi_find_flashnode_conn - finds flashnode connection entry
1419 * @fnode_sess: pointer to parent flashnode session entry
1420 *
1421 * Finds the flashnode connection object comparing the data passed using logic
1422 * defined in passed function pointer
1423 *
1424 * Returns:
1425 * pointer to found flashnode connection device object on success
1426 * %NULL on failure
1427 */
1428struct device *
1429iscsi_find_flashnode_conn(struct iscsi_bus_flash_session *fnode_sess)
1430{
1431 return device_find_child(&fnode_sess->dev, NULL,
1432 iscsi_is_flashnode_conn_dev);
1433}
1434EXPORT_SYMBOL_GPL(iscsi_find_flashnode_conn);
1435
1436static int iscsi_iter_destroy_flashnode_conn_fn(struct device *dev, void *data)
1437{
1438 if (!iscsi_is_flashnode_conn_dev(dev, NULL))
1439 return 0;
1440
1441 return iscsi_destroy_flashnode_conn(iscsi_dev_to_flash_conn(dev));
1442}
1443
1444/**
1445 * iscsi_destroy_flashnode_sess - destroy flashnode session entry
1446 * @fnode_sess: pointer to flashnode session entry to be destroyed
1447 *
1448 * Deletes the flashnode session entry and all children flashnode connection
1449 * entries from sysfs
1450 */
1451void iscsi_destroy_flashnode_sess(struct iscsi_bus_flash_session *fnode_sess)
1452{
1453 int err;
1454
1455 err = device_for_each_child(&fnode_sess->dev, NULL,
1456 iscsi_iter_destroy_flashnode_conn_fn);
1457 if (err)
1458 pr_err("Could not delete all connections for %s. Error %d.\n",
1459 fnode_sess->dev.kobj.name, err);
1460
1461 device_unregister(&fnode_sess->dev);
1462}
1463EXPORT_SYMBOL_GPL(iscsi_destroy_flashnode_sess);
1464
1465static int iscsi_iter_destroy_flashnode_fn(struct device *dev, void *data)
1466{
1467 if (!iscsi_flashnode_bus_match(dev, NULL))
1468 return 0;
1469
1470 iscsi_destroy_flashnode_sess(iscsi_dev_to_flash_session(dev));
1471 return 0;
1472}
1473
1474/**
1475 * iscsi_destroy_all_flashnode - destroy all flashnode session entries
1476 * @shost: pointer to host data
1477 *
1478 * Destroys all the flashnode session entries and all corresponding children
1479 * flashnode connection entries from sysfs
1480 */
1481void iscsi_destroy_all_flashnode(struct Scsi_Host *shost)
1482{
1483 device_for_each_child(&shost->shost_gendev, NULL,
1484 iscsi_iter_destroy_flashnode_fn);
1485}
1486EXPORT_SYMBOL_GPL(iscsi_destroy_all_flashnode);
1487
1488/*
1489 * BSG support
1490 */
1491/**
1492 * iscsi_bsg_host_dispatch - Dispatch command to LLD.
1493 * @job: bsg job to be processed
1494 */
1495static int iscsi_bsg_host_dispatch(struct bsg_job *job)
1496{
1497 struct Scsi_Host *shost = iscsi_job_to_shost(job);
1498 struct iscsi_bsg_request *req = job->request;
1499 struct iscsi_bsg_reply *reply = job->reply;
1500 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1501 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
1502 int ret;
1503
1504 /* check if we have the msgcode value at least */
1505 if (job->request_len < sizeof(uint32_t)) {
1506 ret = -ENOMSG;
1507 goto fail_host_msg;
1508 }
1509
1510 /* Validate the host command */
1511 switch (req->msgcode) {
1512 case ISCSI_BSG_HST_VENDOR:
1513 cmdlen += sizeof(struct iscsi_bsg_host_vendor);
1514 if ((shost->hostt->vendor_id == 0L) ||
1515 (req->rqst_data.h_vendor.vendor_id !=
1516 shost->hostt->vendor_id)) {
1517 ret = -ESRCH;
1518 goto fail_host_msg;
1519 }
1520 break;
1521 default:
1522 ret = -EBADR;
1523 goto fail_host_msg;
1524 }
1525
1526 /* check if we really have all the request data needed */
1527 if (job->request_len < cmdlen) {
1528 ret = -ENOMSG;
1529 goto fail_host_msg;
1530 }
1531
1532 ret = i->iscsi_transport->bsg_request(job);
1533 if (!ret)
1534 return 0;
1535
1536fail_host_msg:
1537 /* return the errno failure code as the only status */
1538 BUG_ON(job->reply_len < sizeof(uint32_t));
1539 reply->reply_payload_rcv_len = 0;
1540 reply->result = ret;
1541 job->reply_len = sizeof(uint32_t);
1542 bsg_job_done(job, ret, 0);
1543 return 0;
1544}
1545
1546/**
1547 * iscsi_bsg_host_add - Create and add the bsg hooks to receive requests
1548 * @shost: shost for iscsi_host
1549 * @ihost: iscsi_cls_host adding the structures to
1550 */
1551static int
1552iscsi_bsg_host_add(struct Scsi_Host *shost, struct iscsi_cls_host *ihost)
1553{
1554 struct device *dev = &shost->shost_gendev;
1555 struct iscsi_internal *i = to_iscsi_internal(shost->transportt);
1556 struct request_queue *q;
1557 char bsg_name[20];
1558
1559 if (!i->iscsi_transport->bsg_request)
1560 return -ENOTSUPP;
1561
1562 snprintf(bsg_name, sizeof(bsg_name), "iscsi_host%d", shost->host_no);
1563 q = bsg_setup_queue(dev, bsg_name, iscsi_bsg_host_dispatch, NULL, 0);
1564 if (IS_ERR(q)) {
1565 shost_printk(KERN_ERR, shost, "bsg interface failed to "
1566 "initialize - no request queue\n");
1567 return PTR_ERR(q);
1568 }
1569 __scsi_init_queue(shost, q);
1570
1571 ihost->bsg_q = q;
1572 return 0;
1573}
1574
1575static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
1576 struct device *cdev)
1577{
1578 struct Scsi_Host *shost = dev_to_shost(dev);
1579 struct iscsi_cls_host *ihost = shost->shost_data;
1580
1581 memset(ihost, 0, sizeof(*ihost));
1582 atomic_set(&ihost->nr_scans, 0);
1583 mutex_init(&ihost->mutex);
1584
1585 iscsi_bsg_host_add(shost, ihost);
1586 /* ignore any bsg add error - we just can't do sgio */
1587
1588 return 0;
1589}
1590
1591static int iscsi_remove_host(struct transport_container *tc,
1592 struct device *dev, struct device *cdev)
1593{
1594 struct Scsi_Host *shost = dev_to_shost(dev);
1595 struct iscsi_cls_host *ihost = shost->shost_data;
1596
1597 bsg_remove_queue(ihost->bsg_q);
1598 return 0;
1599}
1600
1601static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
1602 "iscsi_host",
1603 iscsi_setup_host,
1604 iscsi_remove_host,
1605 NULL);
1606
1607static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
1608 "iscsi_session",
1609 NULL,
1610 NULL,
1611 NULL);
1612
1613static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
1614 "iscsi_connection",
1615 NULL,
1616 NULL,
1617 NULL);
1618
1619static struct sock *nls;
1620static DEFINE_MUTEX(rx_queue_mutex);
1621
1622static LIST_HEAD(sesslist);
1623static DEFINE_SPINLOCK(sesslock);
1624static LIST_HEAD(connlist);
1625static DEFINE_SPINLOCK(connlock);
1626
1627static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
1628{
1629 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
1630 return sess->sid;
1631}
1632
1633/*
1634 * Returns the matching session to a given sid
1635 */
1636static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
1637{
1638 unsigned long flags;
1639 struct iscsi_cls_session *sess;
1640
1641 spin_lock_irqsave(&sesslock, flags);
1642 list_for_each_entry(sess, &sesslist, sess_list) {
1643 if (sess->sid == sid) {
1644 spin_unlock_irqrestore(&sesslock, flags);
1645 return sess;
1646 }
1647 }
1648 spin_unlock_irqrestore(&sesslock, flags);
1649 return NULL;
1650}
1651
1652/*
1653 * Returns the matching connection to a given sid / cid tuple
1654 */
1655static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
1656{
1657 unsigned long flags;
1658 struct iscsi_cls_conn *conn;
1659
1660 spin_lock_irqsave(&connlock, flags);
1661 list_for_each_entry(conn, &connlist, conn_list) {
1662 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
1663 spin_unlock_irqrestore(&connlock, flags);
1664 return conn;
1665 }
1666 }
1667 spin_unlock_irqrestore(&connlock, flags);
1668 return NULL;
1669}
1670
1671/*
1672 * The following functions can be used by LLDs that allocate
1673 * their own scsi_hosts or by software iscsi LLDs
1674 */
1675static struct {
1676 int value;
1677 char *name;
1678} iscsi_session_state_names[] = {
1679 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
1680 { ISCSI_SESSION_FAILED, "FAILED" },
1681 { ISCSI_SESSION_FREE, "FREE" },
1682};
1683
1684static const char *iscsi_session_state_name(int state)
1685{
1686 int i;
1687 char *name = NULL;
1688
1689 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
1690 if (iscsi_session_state_names[i].value == state) {
1691 name = iscsi_session_state_names[i].name;
1692 break;
1693 }
1694 }
1695 return name;
1696}
1697
1698int iscsi_session_chkready(struct iscsi_cls_session *session)
1699{
1700 unsigned long flags;
1701 int err;
1702
1703 spin_lock_irqsave(&session->lock, flags);
1704 switch (session->state) {
1705 case ISCSI_SESSION_LOGGED_IN:
1706 err = 0;
1707 break;
1708 case ISCSI_SESSION_FAILED:
1709 err = DID_IMM_RETRY << 16;
1710 break;
1711 case ISCSI_SESSION_FREE:
1712 err = DID_TRANSPORT_FAILFAST << 16;
1713 break;
1714 default:
1715 err = DID_NO_CONNECT << 16;
1716 break;
1717 }
1718 spin_unlock_irqrestore(&session->lock, flags);
1719 return err;
1720}
1721EXPORT_SYMBOL_GPL(iscsi_session_chkready);
1722
1723int iscsi_is_session_online(struct iscsi_cls_session *session)
1724{
1725 unsigned long flags;
1726 int ret = 0;
1727
1728 spin_lock_irqsave(&session->lock, flags);
1729 if (session->state == ISCSI_SESSION_LOGGED_IN)
1730 ret = 1;
1731 spin_unlock_irqrestore(&session->lock, flags);
1732 return ret;
1733}
1734EXPORT_SYMBOL_GPL(iscsi_is_session_online);
1735
1736static void iscsi_session_release(struct device *dev)
1737{
1738 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
1739 struct Scsi_Host *shost;
1740
1741 shost = iscsi_session_to_shost(session);
1742 scsi_host_put(shost);
1743 ISCSI_DBG_TRANS_SESSION(session, "Completing session release\n");
1744 kfree(session);
1745}
1746
1747int iscsi_is_session_dev(const struct device *dev)
1748{
1749 return dev->release == iscsi_session_release;
1750}
1751EXPORT_SYMBOL_GPL(iscsi_is_session_dev);
1752
1753static int iscsi_iter_session_fn(struct device *dev, void *data)
1754{
1755 void (* fn) (struct iscsi_cls_session *) = data;
1756
1757 if (!iscsi_is_session_dev(dev))
1758 return 0;
1759 fn(iscsi_dev_to_session(dev));
1760 return 0;
1761}
1762
1763void iscsi_host_for_each_session(struct Scsi_Host *shost,
1764 void (*fn)(struct iscsi_cls_session *))
1765{
1766 device_for_each_child(&shost->shost_gendev, fn,
1767 iscsi_iter_session_fn);
1768}
1769EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
1770
1771/**
1772 * iscsi_scan_finished - helper to report when running scans are done
1773 * @shost: scsi host
1774 * @time: scan run time
1775 *
1776 * This function can be used by drives like qla4xxx to report to the scsi
1777 * layer when the scans it kicked off at module load time are done.
1778 */
1779int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
1780{
1781 struct iscsi_cls_host *ihost = shost->shost_data;
1782 /*
1783 * qla4xxx will have kicked off some session unblocks before calling
1784 * scsi_scan_host, so just wait for them to complete.
1785 */
1786 return !atomic_read(&ihost->nr_scans);
1787}
1788EXPORT_SYMBOL_GPL(iscsi_scan_finished);
1789
1790struct iscsi_scan_data {
1791 unsigned int channel;
1792 unsigned int id;
1793 u64 lun;
1794 enum scsi_scan_mode rescan;
1795};
1796
1797static int iscsi_user_scan_session(struct device *dev, void *data)
1798{
1799 struct iscsi_scan_data *scan_data = data;
1800 struct iscsi_cls_session *session;
1801 struct Scsi_Host *shost;
1802 struct iscsi_cls_host *ihost;
1803 unsigned long flags;
1804 unsigned int id;
1805
1806 if (!iscsi_is_session_dev(dev))
1807 return 0;
1808
1809 session = iscsi_dev_to_session(dev);
1810
1811 ISCSI_DBG_TRANS_SESSION(session, "Scanning session\n");
1812
1813 shost = iscsi_session_to_shost(session);
1814 ihost = shost->shost_data;
1815
1816 mutex_lock(&ihost->mutex);
1817 spin_lock_irqsave(&session->lock, flags);
1818 if (session->state != ISCSI_SESSION_LOGGED_IN) {
1819 spin_unlock_irqrestore(&session->lock, flags);
1820 goto user_scan_exit;
1821 }
1822 id = session->target_id;
1823 spin_unlock_irqrestore(&session->lock, flags);
1824
1825 if (id != ISCSI_MAX_TARGET) {
1826 if ((scan_data->channel == SCAN_WILD_CARD ||
1827 scan_data->channel == 0) &&
1828 (scan_data->id == SCAN_WILD_CARD ||
1829 scan_data->id == id))
1830 scsi_scan_target(&session->dev, 0, id,
1831 scan_data->lun, scan_data->rescan);
1832 }
1833
1834user_scan_exit:
1835 mutex_unlock(&ihost->mutex);
1836 ISCSI_DBG_TRANS_SESSION(session, "Completed session scan\n");
1837 return 0;
1838}
1839
1840static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
1841 uint id, u64 lun)
1842{
1843 struct iscsi_scan_data scan_data;
1844
1845 scan_data.channel = channel;
1846 scan_data.id = id;
1847 scan_data.lun = lun;
1848 scan_data.rescan = SCSI_SCAN_MANUAL;
1849
1850 return device_for_each_child(&shost->shost_gendev, &scan_data,
1851 iscsi_user_scan_session);
1852}
1853
1854static void iscsi_scan_session(struct work_struct *work)
1855{
1856 struct iscsi_cls_session *session =
1857 container_of(work, struct iscsi_cls_session, scan_work);
1858 struct Scsi_Host *shost = iscsi_session_to_shost(session);
1859 struct iscsi_cls_host *ihost = shost->shost_data;
1860 struct iscsi_scan_data scan_data;
1861
1862 scan_data.channel = 0;
1863 scan_data.id = SCAN_WILD_CARD;
1864 scan_data.lun = SCAN_WILD_CARD;
1865 scan_data.rescan = SCSI_SCAN_RESCAN;
1866
1867 iscsi_user_scan_session(&session->dev, &scan_data);
1868 atomic_dec(&ihost->nr_scans);
1869}
1870
1871/**
1872 * iscsi_block_scsi_eh - block scsi eh until session state has transistioned
1873 * @cmd: scsi cmd passed to scsi eh handler
1874 *
1875 * If the session is down this function will wait for the recovery
1876 * timer to fire or for the session to be logged back in. If the
1877 * recovery timer fires then FAST_IO_FAIL is returned. The caller
1878 * should pass this error value to the scsi eh.
1879 */
1880int iscsi_block_scsi_eh(struct scsi_cmnd *cmd)
1881{
1882 struct iscsi_cls_session *session =
1883 starget_to_session(scsi_target(cmd->device));
1884 unsigned long flags;
1885 int ret = 0;
1886
1887 spin_lock_irqsave(&session->lock, flags);
1888 while (session->state != ISCSI_SESSION_LOGGED_IN) {
1889 if (session->state == ISCSI_SESSION_FREE) {
1890 ret = FAST_IO_FAIL;
1891 break;
1892 }
1893 spin_unlock_irqrestore(&session->lock, flags);
1894 msleep(1000);
1895 spin_lock_irqsave(&session->lock, flags);
1896 }
1897 spin_unlock_irqrestore(&session->lock, flags);
1898 return ret;
1899}
1900EXPORT_SYMBOL_GPL(iscsi_block_scsi_eh);
1901
1902static void session_recovery_timedout(struct work_struct *work)
1903{
1904 struct iscsi_cls_session *session =
1905 container_of(work, struct iscsi_cls_session,
1906 recovery_work.work);
1907 unsigned long flags;
1908
1909 iscsi_cls_session_printk(KERN_INFO, session,
1910 "session recovery timed out after %d secs\n",
1911 session->recovery_tmo);
1912
1913 spin_lock_irqsave(&session->lock, flags);
1914 switch (session->state) {
1915 case ISCSI_SESSION_FAILED:
1916 session->state = ISCSI_SESSION_FREE;
1917 break;
1918 case ISCSI_SESSION_LOGGED_IN:
1919 case ISCSI_SESSION_FREE:
1920 /* we raced with the unblock's flush */
1921 spin_unlock_irqrestore(&session->lock, flags);
1922 return;
1923 }
1924 spin_unlock_irqrestore(&session->lock, flags);
1925
1926 if (session->transport->session_recovery_timedout)
1927 session->transport->session_recovery_timedout(session);
1928
1929 ISCSI_DBG_TRANS_SESSION(session, "Unblocking SCSI target\n");
1930 scsi_target_unb