blob: da5b503dc7a1236b014ac67efacdd9dace6504d1 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
James Smart9ef3e4a2007-05-24 19:04:44 -04002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * FiberChannel transport specific attributes exported to sysfs.
4 *
5 * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
James Smarta53eb5e2007-04-27 12:41:09 -04006 * Copyright (C) 2004-2007 James Smart, Emulex Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Rewrite for host, target, device, and remote port attributes,
8 * statistics, and service functions...
James Smart9ef3e4a2007-05-24 19:04:44 -04009 * Add vports, etc
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11#include <linux/module.h>
12#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Christof Schmitt65d430f2009-10-30 17:59:29 +010014#include <linux/delay.h>
Andy Shevchenkoecc30992010-08-10 18:01:27 -070015#include <linux/kernel.h>
Johannes Thumshirnc00da4c2016-11-17 10:31:20 +010016#include <linux/bsg-lib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <scsi/scsi_device.h>
18#include <scsi/scsi_host.h>
19#include <scsi/scsi_transport.h>
20#include <scsi/scsi_transport_fc.h>
James Smartc829c392006-03-13 08:28:57 -050021#include <scsi/scsi_cmnd.h>
James Smart84314fd2006-08-18 17:30:09 -040022#include <net/netlink.h>
23#include <scsi/scsi_netlink_fc.h>
James Smart9e4f5e22009-03-26 13:33:19 -040024#include <scsi/scsi_bsg_fc.h>
Shyam Sundar547aab52020-10-21 02:27:12 -070025#include <uapi/scsi/fc/fc_els.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "scsi_priv.h"
27
James Smartaedf3492006-04-10 10:14:05 -040028static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
James Smart9ef3e4a2007-05-24 19:04:44 -040029static void fc_vport_sched_delete(struct work_struct *work);
Andrew Vasqueza30c3f62008-07-18 08:32:52 -070030static int fc_vport_setup(struct Scsi_Host *shost, int channel,
James Smarta53eb5e2007-04-27 12:41:09 -040031 struct device *pdev, struct fc_vport_identifiers *ids,
32 struct fc_vport **vport);
James Smart9e4f5e22009-03-26 13:33:19 -040033static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
34static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
35static void fc_bsg_remove(struct request_queue *);
36static void fc_bsg_goose_queue(struct fc_rport *);
Shyam Sundar3dcfe0d2020-10-21 02:27:13 -070037static void fc_li_stats_update(struct fc_fn_li_desc *li_desc,
38 struct fc_fpin_stats *stats);
39static void fc_delivery_stats_update(u32 reason_code,
40 struct fc_fpin_stats *stats);
41static void fc_cn_stats_update(u16 event_type, struct fc_fpin_stats *stats);
James Smarta53eb5e2007-04-27 12:41:09 -040042
43/*
Mike Christie43ca9102010-09-15 16:52:32 -050044 * Module Parameters
45 */
46
47/*
48 * dev_loss_tmo: the default number of seconds that the FC transport
49 * should insulate the loss of a remote port.
50 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
51 */
52static unsigned int fc_dev_loss_tmo = 60; /* seconds */
53
54module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
55MODULE_PARM_DESC(dev_loss_tmo,
56 "Maximum number of seconds that the FC transport should"
57 " insulate the loss of a remote port. Once this value is"
58 " exceeded, the scsi target is removed. Value should be"
59 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
60 " fast_io_fail_tmo is not set.");
61
62/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * Redefine so that we can have same named attributes in the
64 * sdev/starget/host objects.
65 */
Tony Jonesee959b02008-02-22 00:13:36 +010066#define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
67struct device_attribute device_attr_##_prefix##_##_name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 __ATTR(_name,_mode,_show,_store)
69
70#define fc_enum_name_search(title, table_type, table) \
71static const char *get_fc_##title##_name(enum table_type table_key) \
72{ \
73 int i; \
74 char *name = NULL; \
75 \
Tobias Klauser6391a112006-06-08 22:23:48 -070076 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (table[i].value == table_key) { \
78 name = table[i].name; \
79 break; \
80 } \
81 } \
82 return name; \
83}
84
85#define fc_enum_name_match(title, table_type, table) \
86static int get_fc_##title##_match(const char *table_key, \
87 enum table_type *value) \
88{ \
89 int i; \
90 \
Tobias Klauser6391a112006-06-08 22:23:48 -070091 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (strncmp(table_key, table[i].name, \
93 table[i].matchlen) == 0) { \
94 *value = table[i].value; \
95 return 0; /* success */ \
96 } \
97 } \
98 return 1; /* failure */ \
99}
100
101
102/* Convert fc_port_type values to ascii string name */
103static struct {
104 enum fc_port_type value;
105 char *name;
106} fc_port_type_names[] = {
107 { FC_PORTTYPE_UNKNOWN, "Unknown" },
108 { FC_PORTTYPE_OTHER, "Other" },
109 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
110 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
111 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
112 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
Christof Schmitt951948a2009-01-15 16:51:48 +0100113 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" },
James Smarta53eb5e2007-04-27 12:41:09 -0400114 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
117#define FC_PORTTYPE_MAX_NAMELEN 50
118
James Smarta53eb5e2007-04-27 12:41:09 -0400119/* Reuse fc_port_type enum function for vport_type */
120#define get_fc_vport_type_name get_fc_port_type_name
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
James Smart84314fd2006-08-18 17:30:09 -0400123/* Convert fc_host_event_code values to ascii string name */
124static const struct {
125 enum fc_host_event_code value;
126 char *name;
127} fc_host_event_code_names[] = {
128 { FCH_EVT_LIP, "lip" },
129 { FCH_EVT_LINKUP, "link_up" },
130 { FCH_EVT_LINKDOWN, "link_down" },
131 { FCH_EVT_LIPRESET, "lip_reset" },
132 { FCH_EVT_RSCN, "rscn" },
133 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
134 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
135 { FCH_EVT_PORT_ONLINE, "port_online" },
136 { FCH_EVT_PORT_OFFLINE, "port_offline" },
137 { FCH_EVT_PORT_FABRIC, "port_fabric" },
138 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
James Smartc39e0af2019-04-05 16:04:22 -0700139 { FCH_EVT_LINK_FPIN, "link_FPIN" },
James Smart84314fd2006-08-18 17:30:09 -0400140 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
141};
142fc_enum_name_search(host_event_code, fc_host_event_code,
143 fc_host_event_code_names)
144#define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
145
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* Convert fc_port_state values to ascii string name */
148static struct {
149 enum fc_port_state value;
150 char *name;
Muneendra Kumar02c66322021-01-07 03:19:06 +0530151 int matchlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152} fc_port_state_names[] = {
Muneendra Kumar02c66322021-01-07 03:19:06 +0530153 { FC_PORTSTATE_UNKNOWN, "Unknown", 7},
154 { FC_PORTSTATE_NOTPRESENT, "Not Present", 11 },
155 { FC_PORTSTATE_ONLINE, "Online", 6 },
156 { FC_PORTSTATE_OFFLINE, "Offline", 7 },
157 { FC_PORTSTATE_BLOCKED, "Blocked", 7 },
158 { FC_PORTSTATE_BYPASSED, "Bypassed", 8 },
159 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics", 11 },
160 { FC_PORTSTATE_LINKDOWN, "Linkdown", 8 },
161 { FC_PORTSTATE_ERROR, "Error", 5 },
162 { FC_PORTSTATE_LOOPBACK, "Loopback", 8 },
163 { FC_PORTSTATE_DELETED, "Deleted", 7 },
164 { FC_PORTSTATE_MARGINAL, "Marginal", 8 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
Muneendra Kumar02c66322021-01-07 03:19:06 +0530167fc_enum_name_match(port_state, fc_port_state, fc_port_state_names)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#define FC_PORTSTATE_MAX_NAMELEN 20
169
170
James Smarta53eb5e2007-04-27 12:41:09 -0400171/* Convert fc_vport_state values to ascii string name */
172static struct {
173 enum fc_vport_state value;
174 char *name;
175} fc_vport_state_names[] = {
176 { FC_VPORT_UNKNOWN, "Unknown" },
177 { FC_VPORT_ACTIVE, "Active" },
178 { FC_VPORT_DISABLED, "Disabled" },
179 { FC_VPORT_LINKDOWN, "Linkdown" },
180 { FC_VPORT_INITIALIZING, "Initializing" },
181 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
182 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
183 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
184 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
185 { FC_VPORT_FAILED, "VPort Failed" },
186};
187fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
188#define FC_VPORTSTATE_MAX_NAMELEN 24
189
190/* Reuse fc_vport_state enum function for vport_last_state */
191#define get_fc_vport_last_state_name get_fc_vport_state_name
192
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/* Convert fc_tgtid_binding_type values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100195static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 enum fc_tgtid_binding_type value;
197 char *name;
198 int matchlen;
199} fc_tgtid_binding_type_names[] = {
200 { FC_TGTID_BIND_NONE, "none", 4 },
201 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
202 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
203 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
204};
205fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
206 fc_tgtid_binding_type_names)
207fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
208 fc_tgtid_binding_type_names)
209#define FC_BINDTYPE_MAX_NAMELEN 30
210
211
212#define fc_bitfield_name_search(title, table) \
213static ssize_t \
214get_fc_##title##_names(u32 table_key, char *buf) \
215{ \
216 char *prefix = ""; \
217 ssize_t len = 0; \
218 int i; \
219 \
Tobias Klauser6391a112006-06-08 22:23:48 -0700220 for (i = 0; i < ARRAY_SIZE(table); i++) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (table[i].value & table_key) { \
222 len += sprintf(buf + len, "%s%s", \
223 prefix, table[i].name); \
224 prefix = ", "; \
225 } \
226 } \
227 len += sprintf(buf + len, "\n"); \
228 return len; \
229}
230
231
232/* Convert FC_COS bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100233static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 u32 value;
235 char *name;
236} fc_cos_names[] = {
237 { FC_COS_CLASS1, "Class 1" },
238 { FC_COS_CLASS2, "Class 2" },
239 { FC_COS_CLASS3, "Class 3" },
240 { FC_COS_CLASS4, "Class 4" },
241 { FC_COS_CLASS6, "Class 6" },
242};
243fc_bitfield_name_search(cos, fc_cos_names)
244
245
246/* Convert FC_PORTSPEED bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100247static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 u32 value;
249 char *name;
250} fc_port_speed_names[] = {
251 { FC_PORTSPEED_1GBIT, "1 Gbit" },
252 { FC_PORTSPEED_2GBIT, "2 Gbit" },
253 { FC_PORTSPEED_4GBIT, "4 Gbit" },
254 { FC_PORTSPEED_10GBIT, "10 Gbit" },
James Smartc3d23502007-03-12 14:16:35 -0500255 { FC_PORTSPEED_8GBIT, "8 Gbit" },
256 { FC_PORTSPEED_16GBIT, "16 Gbit" },
Chad Dupuis624f28b2014-02-26 04:15:05 -0500257 { FC_PORTSPEED_32GBIT, "32 Gbit" },
Dick Kennedyc21a2c12014-06-13 16:40:36 +0000258 { FC_PORTSPEED_20GBIT, "20 Gbit" },
259 { FC_PORTSPEED_40GBIT, "40 Gbit" },
260 { FC_PORTSPEED_50GBIT, "50 Gbit" },
261 { FC_PORTSPEED_100GBIT, "100 Gbit" },
James Smartc749e6b2015-04-02 15:50:52 -0400262 { FC_PORTSPEED_25GBIT, "25 Gbit" },
James Smartcc019a52017-12-21 14:25:52 -0800263 { FC_PORTSPEED_64GBIT, "64 Gbit" },
264 { FC_PORTSPEED_128GBIT, "128 Gbit" },
James Smart2a5c98d2020-08-31 14:35:18 -0700265 { FC_PORTSPEED_256GBIT, "256 Gbit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
267};
268fc_bitfield_name_search(port_speed, fc_port_speed_names)
269
270
271static int
272show_fc_fc4s (char *buf, u8 *fc4_list)
273{
274 int i, len=0;
275
276 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
277 len += sprintf(buf + len , "0x%02x ", *fc4_list);
278 len += sprintf(buf + len, "\n");
279 return len;
280}
281
282
James Smarta53eb5e2007-04-27 12:41:09 -0400283/* Convert FC_PORT_ROLE bit values to ascii string name */
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100284static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 u32 value;
286 char *name;
James Smarta53eb5e2007-04-27 12:41:09 -0400287} fc_port_role_names[] = {
Cathy Avery0c3ae262017-04-17 14:37:45 -0400288 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
289 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
290 { FC_PORT_ROLE_IP_PORT, "IP Port" },
291 { FC_PORT_ROLE_FCP_DUMMY_INITIATOR, "FCP Dummy Initiator" },
Hannes Reineckea6a6d052019-04-10 16:16:19 +0200292 { FC_PORT_ROLE_NVME_INITIATOR, "NVMe Initiator" },
293 { FC_PORT_ROLE_NVME_TARGET, "NVMe Target" },
294 { FC_PORT_ROLE_NVME_DISCOVERY, "NVMe Discovery" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295};
James Smarta53eb5e2007-04-27 12:41:09 -0400296fc_bitfield_name_search(port_roles, fc_port_role_names)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298/*
299 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
300 */
301#define FC_WELLKNOWN_PORTID_MASK 0xfffff0
302#define FC_WELLKNOWN_ROLE_MASK 0x00000f
303#define FC_FPORT_PORTID 0x00000e
304#define FC_FABCTLR_PORTID 0x00000d
305#define FC_DIRSRVR_PORTID 0x00000c
306#define FC_TIMESRVR_PORTID 0x00000b
307#define FC_MGMTSRVR_PORTID 0x00000a
308
309
David Howellsc4028952006-11-22 14:57:56 +0000310static void fc_timeout_deleted_rport(struct work_struct *work);
311static void fc_timeout_fail_rport_io(struct work_struct *work);
312static void fc_scsi_scan_rport(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314/*
315 * Attribute counts pre object type...
316 * Increase these values if you add attributes
317 */
318#define FC_STARGET_NUM_ATTRS 3
James Smart0f29b962006-08-18 17:33:29 -0400319#define FC_RPORT_NUM_ATTRS 10
James Smarta53eb5e2007-04-27 12:41:09 -0400320#define FC_VPORT_NUM_ATTRS 9
Neerav Parikhbb8ef582012-01-22 17:29:50 -0800321#define FC_HOST_NUM_ATTRS 29
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323struct fc_internal {
324 struct scsi_transport_template t;
325 struct fc_function_template *f;
326
327 /*
328 * For attributes : each object has :
329 * An array of the actual attributes structures
330 * An array of null-terminated pointers to the attribute
331 * structures - used for mid-layer interaction.
332 *
333 * The attribute containers for the starget and host are are
334 * part of the midlayer. As the remote port is specific to the
335 * fc transport, we must provide the attribute container.
336 */
Tony Jonesee959b02008-02-22 00:13:36 +0100337 struct device_attribute private_starget_attrs[
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 FC_STARGET_NUM_ATTRS];
Tony Jonesee959b02008-02-22 00:13:36 +0100339 struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Tony Jonesee959b02008-02-22 00:13:36 +0100341 struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
342 struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 struct transport_container rport_attr_cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100345 struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
346 struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
James Smarta53eb5e2007-04-27 12:41:09 -0400347
348 struct transport_container vport_attr_cont;
Tony Jonesee959b02008-02-22 00:13:36 +0100349 struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
350 struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351};
352
353#define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
354
James Bottomleyd0a7e572005-08-14 17:09:01 -0500355static int fc_target_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100356 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct scsi_target *starget = to_scsi_target(dev);
359 struct fc_rport *rport = starget_to_rport(starget);
360
361 /*
362 * if parent is remote port, use values from remote port.
363 * Otherwise, this host uses the fc_transport, but not the
364 * remote port interface. As such, initialize to known non-values.
365 */
366 if (rport) {
367 fc_starget_node_name(starget) = rport->node_name;
368 fc_starget_port_name(starget) = rport->port_name;
369 fc_starget_port_id(starget) = rport->port_id;
370 } else {
371 fc_starget_node_name(starget) = -1;
372 fc_starget_port_name(starget) = -1;
373 fc_starget_port_id(starget) = -1;
374 }
375
376 return 0;
377}
378
379static DECLARE_TRANSPORT_CLASS(fc_transport_class,
380 "fc_transport",
381 fc_target_setup,
382 NULL,
383 NULL);
384
James Bottomleyd0a7e572005-08-14 17:09:01 -0500385static int fc_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100386 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 struct Scsi_Host *shost = dev_to_shost(dev);
James Smartaedf3492006-04-10 10:14:05 -0400389 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
James Smart9ef3e4a2007-05-24 19:04:44 -0400391 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * Set default values easily detected by the midlayer as
393 * failure cases. The scsi lldd is responsible for initializing
394 * all transport attributes to valid values per host.
395 */
James Smartaedf3492006-04-10 10:14:05 -0400396 fc_host->node_name = -1;
397 fc_host->port_name = -1;
398 fc_host->permanent_port_name = -1;
399 fc_host->supported_classes = FC_COS_UNSPECIFIED;
400 memset(fc_host->supported_fc4s, 0,
401 sizeof(fc_host->supported_fc4s));
James Smartaedf3492006-04-10 10:14:05 -0400402 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
403 fc_host->maxframe_size = -1;
James Smarta53eb5e2007-04-27 12:41:09 -0400404 fc_host->max_npiv_vports = 0;
James Smartaedf3492006-04-10 10:14:05 -0400405 memset(fc_host->serial_number, 0,
406 sizeof(fc_host->serial_number));
Neerav Parikhbb8ef582012-01-22 17:29:50 -0800407 memset(fc_host->manufacturer, 0,
408 sizeof(fc_host->manufacturer));
409 memset(fc_host->model, 0,
410 sizeof(fc_host->model));
411 memset(fc_host->model_description, 0,
412 sizeof(fc_host->model_description));
413 memset(fc_host->hardware_version, 0,
414 sizeof(fc_host->hardware_version));
415 memset(fc_host->driver_version, 0,
416 sizeof(fc_host->driver_version));
417 memset(fc_host->firmware_version, 0,
418 sizeof(fc_host->firmware_version));
419 memset(fc_host->optionrom_version, 0,
420 sizeof(fc_host->optionrom_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
James Smartaedf3492006-04-10 10:14:05 -0400422 fc_host->port_id = -1;
423 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
424 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
425 memset(fc_host->active_fc4s, 0,
426 sizeof(fc_host->active_fc4s));
427 fc_host->speed = FC_PORTSPEED_UNKNOWN;
428 fc_host->fabric_name = -1;
James Smartb8d08212006-08-17 08:00:43 -0400429 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
430 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
Shyam Sundar547aab52020-10-21 02:27:12 -0700431 memset(&fc_host->fpin_stats, 0, sizeof(fc_host->fpin_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
James Smartaedf3492006-04-10 10:14:05 -0400433 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
James Smartaedf3492006-04-10 10:14:05 -0400435 INIT_LIST_HEAD(&fc_host->rports);
436 INIT_LIST_HEAD(&fc_host->rport_bindings);
James Smarta53eb5e2007-04-27 12:41:09 -0400437 INIT_LIST_HEAD(&fc_host->vports);
James Smartaedf3492006-04-10 10:14:05 -0400438 fc_host->next_rport_number = 0;
439 fc_host->next_target_id = 0;
James Smarta53eb5e2007-04-27 12:41:09 -0400440 fc_host->next_vport_number = 0;
441 fc_host->npiv_vports_inuse = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Kay Sieversaab0de22008-05-02 06:02:41 +0200443 snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
444 "fc_wq_%d", shost->host_no);
Kees Cookd8537542013-07-03 15:04:57 -0700445 fc_host->work_q = alloc_workqueue("%s", 0, 0, fc_host->work_q_name);
James Smartaedf3492006-04-10 10:14:05 -0400446 if (!fc_host->work_q)
447 return -ENOMEM;
448
Mike Christie43ca9102010-09-15 16:52:32 -0500449 fc_host->dev_loss_tmo = fc_dev_loss_tmo;
Kay Sieversaab0de22008-05-02 06:02:41 +0200450 snprintf(fc_host->devloss_work_q_name,
451 sizeof(fc_host->devloss_work_q_name),
452 "fc_dl_%d", shost->host_no);
Kees Cookd8537542013-07-03 15:04:57 -0700453 fc_host->devloss_work_q = alloc_workqueue("%s", 0, 0,
454 fc_host->devloss_work_q_name);
James Smartaedf3492006-04-10 10:14:05 -0400455 if (!fc_host->devloss_work_q) {
456 destroy_workqueue(fc_host->work_q);
457 fc_host->work_q = NULL;
458 return -ENOMEM;
459 }
460
James Smart9e4f5e22009-03-26 13:33:19 -0400461 fc_bsg_hostadd(shost, fc_host);
462 /* ignore any bsg add error - we just can't do sgio */
463
464 return 0;
465}
466
467static int fc_host_remove(struct transport_container *tc, struct device *dev,
468 struct device *cdev)
469{
470 struct Scsi_Host *shost = dev_to_shost(dev);
471 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
472
473 fc_bsg_remove(fc_host->rqst_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return 0;
475}
476
477static DECLARE_TRANSPORT_CLASS(fc_host_class,
478 "fc_host",
479 fc_host_setup,
James Smart9e4f5e22009-03-26 13:33:19 -0400480 fc_host_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 NULL);
482
483/*
484 * Setup and Remove actions for remote ports are handled
485 * in the service functions below.
486 */
487static DECLARE_TRANSPORT_CLASS(fc_rport_class,
488 "fc_remote_ports",
489 NULL,
490 NULL,
491 NULL);
492
493/*
James Smarta53eb5e2007-04-27 12:41:09 -0400494 * Setup and Remove actions for virtual ports are handled
495 * in the service functions below.
496 */
497static DECLARE_TRANSPORT_CLASS(fc_vport_class,
498 "fc_vports",
499 NULL,
500 NULL,
501 NULL);
502
503/*
James Smart84314fd2006-08-18 17:30:09 -0400504 * Netlink Infrastructure
Rob Landleyeb448202007-11-03 13:30:39 -0500505 */
James Smart84314fd2006-08-18 17:30:09 -0400506
507static atomic_t fc_event_seq;
508
509/**
510 * fc_get_event_number - Obtain the next sequential FC event number
511 *
512 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -0500513 * We could have inlined this, but it would have required fc_event_seq to
James Smart84314fd2006-08-18 17:30:09 -0400514 * be exposed. For now, live with the subroutine call.
515 * Atomic used to avoid lock/unlock...
Rob Landleyeb448202007-11-03 13:30:39 -0500516 */
James Smart84314fd2006-08-18 17:30:09 -0400517u32
518fc_get_event_number(void)
519{
520 return atomic_add_return(1, &fc_event_seq);
521}
522EXPORT_SYMBOL(fc_get_event_number);
523
James Smart84314fd2006-08-18 17:30:09 -0400524/**
James Smart2b1be552019-04-05 16:04:21 -0700525 * fc_host_post_fc_event - routine to do the work of posting an event
526 * on an fc_host.
James Smart84314fd2006-08-18 17:30:09 -0400527 * @shost: host the event occurred on
528 * @event_number: fc event number obtained from get_fc_event_number()
529 * @event_code: fc_host event being posted
James Smart2b1be552019-04-05 16:04:21 -0700530 * @data_len: amount, in bytes, of event data
531 * @data_buf: pointer to event data
532 * @vendor_id: value for Vendor id
James Smart84314fd2006-08-18 17:30:09 -0400533 *
534 * Notes:
535 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500536 */
James Smart84314fd2006-08-18 17:30:09 -0400537void
James Smart2b1be552019-04-05 16:04:21 -0700538fc_host_post_fc_event(struct Scsi_Host *shost, u32 event_number,
539 enum fc_host_event_code event_code,
540 u32 data_len, char *data_buf, u64 vendor_id)
James Smart84314fd2006-08-18 17:30:09 -0400541{
542 struct sk_buff *skb;
543 struct nlmsghdr *nlh;
544 struct fc_nl_event *event;
545 const char *name;
Hong zhi guoe07ebea2013-03-27 06:53:15 +0000546 u32 len;
James Smart84314fd2006-08-18 17:30:09 -0400547 int err;
548
James Smart2b1be552019-04-05 16:04:21 -0700549 if (!data_buf || data_len < 4)
550 data_len = 0;
551
James Smart84314fd2006-08-18 17:30:09 -0400552 if (!scsi_nl_sock) {
553 err = -ENOENT;
554 goto send_fail;
555 }
556
James Smart2b1be552019-04-05 16:04:21 -0700557 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
James Smart84314fd2006-08-18 17:30:09 -0400558
Hong zhi guoe07ebea2013-03-27 06:53:15 +0000559 skb = nlmsg_new(len, GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400560 if (!skb) {
561 err = -ENOBUFS;
562 goto send_fail;
563 }
564
Hong zhi guoe07ebea2013-03-27 06:53:15 +0000565 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0);
James Smart84314fd2006-08-18 17:30:09 -0400566 if (!nlh) {
567 err = -ENOBUFS;
568 goto send_fail_skb;
569 }
Hong zhi guoe07ebea2013-03-27 06:53:15 +0000570 event = nlmsg_data(nlh);
James Smart84314fd2006-08-18 17:30:09 -0400571
572 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
573 FC_NL_ASYNC_EVENT, len);
Arnd Bergmannfe7f4e52018-06-18 17:29:51 +0200574 event->seconds = ktime_get_real_seconds();
James Smart2b1be552019-04-05 16:04:21 -0700575 event->vendor_id = vendor_id;
James Smart84314fd2006-08-18 17:30:09 -0400576 event->host_no = shost->host_no;
James Smart2b1be552019-04-05 16:04:21 -0700577 event->event_datalen = data_len; /* bytes */
James Smart84314fd2006-08-18 17:30:09 -0400578 event->event_num = event_number;
579 event->event_code = event_code;
James Smart2b1be552019-04-05 16:04:21 -0700580 if (data_len)
581 memcpy(&event->event_data, data_buf, data_len);
James Smart84314fd2006-08-18 17:30:09 -0400582
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -0800583 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
584 GFP_KERNEL);
James Smart84314fd2006-08-18 17:30:09 -0400585 return;
586
587send_fail_skb:
588 kfree_skb(skb);
589send_fail:
590 name = get_fc_host_event_code_name(event_code);
591 printk(KERN_WARNING
592 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700593 __func__, shost->host_no,
James Smart2b1be552019-04-05 16:04:21 -0700594 (name) ? name : "<unknown>",
595 (data_len) ? *((u32 *)data_buf) : 0xFFFFFFFF, err);
James Smart84314fd2006-08-18 17:30:09 -0400596 return;
597}
James Smart2b1be552019-04-05 16:04:21 -0700598EXPORT_SYMBOL(fc_host_post_fc_event);
599
600/**
601 * fc_host_post_event - called to post an even on an fc_host.
602 * @shost: host the event occurred on
603 * @event_number: fc event number obtained from get_fc_event_number()
604 * @event_code: fc_host event being posted
605 * @event_data: 32bits of data for the event being posted
606 *
607 * Notes:
608 * This routine assumes no locks are held on entry.
609 */
610void
611fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
612 enum fc_host_event_code event_code, u32 event_data)
613{
614 fc_host_post_fc_event(shost, event_number, event_code,
615 (u32)sizeof(u32), (char *)&event_data, 0);
616}
James Smart84314fd2006-08-18 17:30:09 -0400617EXPORT_SYMBOL(fc_host_post_event);
618
619
620/**
James Smart2b1be552019-04-05 16:04:21 -0700621 * fc_host_post_vendor_event - called to post a vendor unique event
622 * on an fc_host
James Smart84314fd2006-08-18 17:30:09 -0400623 * @shost: host the event occurred on
624 * @event_number: fc event number obtained from get_fc_event_number()
625 * @data_len: amount, in bytes, of vendor unique data
626 * @data_buf: pointer to vendor unique data
Rob Landleyeb448202007-11-03 13:30:39 -0500627 * @vendor_id: Vendor id
James Smart84314fd2006-08-18 17:30:09 -0400628 *
629 * Notes:
630 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -0500631 */
James Smart84314fd2006-08-18 17:30:09 -0400632void
633fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
James Smartf14e2e22006-08-22 09:55:23 -0400634 u32 data_len, char * data_buf, u64 vendor_id)
James Smart84314fd2006-08-18 17:30:09 -0400635{
James Smart2b1be552019-04-05 16:04:21 -0700636 fc_host_post_fc_event(shost, event_number, FCH_EVT_VENDOR_UNIQUE,
637 data_len, data_buf, vendor_id);
James Smart84314fd2006-08-18 17:30:09 -0400638}
639EXPORT_SYMBOL(fc_host_post_vendor_event);
640
James Smartc39e0af2019-04-05 16:04:22 -0700641/**
Shyam Sundar3dcfe0d2020-10-21 02:27:13 -0700642 * fc_find_rport_by_wwpn - find the fc_rport pointer for a given wwpn
643 * @shost: host the fc_rport is associated with
644 * @wwpn: wwpn of the fc_rport device
645 *
646 * Notes:
647 * This routine assumes no locks are held on entry.
648 */
649struct fc_rport *
650fc_find_rport_by_wwpn(struct Scsi_Host *shost, u64 wwpn)
651{
652 struct fc_rport *rport;
653 unsigned long flags;
654
655 spin_lock_irqsave(shost->host_lock, flags);
656
657 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
658 if (rport->port_state != FC_PORTSTATE_ONLINE)
659 continue;
660
661 if (rport->port_name == wwpn) {
662 spin_unlock_irqrestore(shost->host_lock, flags);
663 return rport;
664 }
665 }
666
667 spin_unlock_irqrestore(shost->host_lock, flags);
668 return NULL;
669}
670EXPORT_SYMBOL(fc_find_rport_by_wwpn);
671
672static void
673fc_li_stats_update(struct fc_fn_li_desc *li_desc,
674 struct fc_fpin_stats *stats)
675{
676 stats->li += be32_to_cpu(li_desc->event_count);
677 switch (be16_to_cpu(li_desc->event_type)) {
678 case FPIN_LI_UNKNOWN:
679 stats->li_failure_unknown +=
680 be32_to_cpu(li_desc->event_count);
681 break;
682 case FPIN_LI_LINK_FAILURE:
683 stats->li_link_failure_count +=
684 be32_to_cpu(li_desc->event_count);
685 break;
686 case FPIN_LI_LOSS_OF_SYNC:
687 stats->li_loss_of_sync_count +=
688 be32_to_cpu(li_desc->event_count);
689 break;
690 case FPIN_LI_LOSS_OF_SIG:
691 stats->li_loss_of_signals_count +=
692 be32_to_cpu(li_desc->event_count);
693 break;
694 case FPIN_LI_PRIM_SEQ_ERR:
695 stats->li_prim_seq_err_count +=
696 be32_to_cpu(li_desc->event_count);
697 break;
698 case FPIN_LI_INVALID_TX_WD:
699 stats->li_invalid_tx_word_count +=
700 be32_to_cpu(li_desc->event_count);
701 break;
702 case FPIN_LI_INVALID_CRC:
703 stats->li_invalid_crc_count +=
704 be32_to_cpu(li_desc->event_count);
705 break;
706 case FPIN_LI_DEVICE_SPEC:
707 stats->li_device_specific +=
708 be32_to_cpu(li_desc->event_count);
709 break;
710 }
711}
712
713static void
714fc_delivery_stats_update(u32 reason_code, struct fc_fpin_stats *stats)
715{
716 stats->dn++;
717 switch (reason_code) {
718 case FPIN_DELI_UNKNOWN:
719 stats->dn_unknown++;
720 break;
721 case FPIN_DELI_TIMEOUT:
722 stats->dn_timeout++;
723 break;
724 case FPIN_DELI_UNABLE_TO_ROUTE:
725 stats->dn_unable_to_route++;
726 break;
727 case FPIN_DELI_DEVICE_SPEC:
728 stats->dn_device_specific++;
729 break;
730 }
731}
732
733static void
734fc_cn_stats_update(u16 event_type, struct fc_fpin_stats *stats)
735{
736 stats->cn++;
737 switch (event_type) {
738 case FPIN_CONGN_CLEAR:
739 stats->cn_clear++;
740 break;
741 case FPIN_CONGN_LOST_CREDIT:
742 stats->cn_lost_credit++;
743 break;
744 case FPIN_CONGN_CREDIT_STALL:
745 stats->cn_credit_stall++;
746 break;
747 case FPIN_CONGN_OVERSUBSCRIPTION:
748 stats->cn_oversubscription++;
749 break;
750 case FPIN_CONGN_DEVICE_SPEC:
751 stats->cn_device_specific++;
752 }
753}
754
755/*
756 * fc_fpin_li_stats_update - routine to update Link Integrity
757 * event statistics.
758 * @shost: host the FPIN was received on
759 * @tlv: pointer to link integrity descriptor
760 *
761 */
762static void
763fc_fpin_li_stats_update(struct Scsi_Host *shost, struct fc_tlv_desc *tlv)
764{
765 u8 i;
766 struct fc_rport *rport = NULL;
767 struct fc_rport *attach_rport = NULL;
768 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
769 struct fc_fn_li_desc *li_desc = (struct fc_fn_li_desc *)tlv;
770 u64 wwpn;
771
772 rport = fc_find_rport_by_wwpn(shost,
773 be64_to_cpu(li_desc->attached_wwpn));
774 if (rport &&
775 (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
776 rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
777 attach_rport = rport;
778 fc_li_stats_update(li_desc, &attach_rport->fpin_stats);
779 }
780
781 if (be32_to_cpu(li_desc->pname_count) > 0) {
782 for (i = 0;
783 i < be32_to_cpu(li_desc->pname_count);
784 i++) {
785 wwpn = be64_to_cpu(li_desc->pname_list[i]);
786 rport = fc_find_rport_by_wwpn(shost, wwpn);
787 if (rport &&
788 (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
789 rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
790 if (rport == attach_rport)
791 continue;
792 fc_li_stats_update(li_desc,
793 &rport->fpin_stats);
794 }
795 }
796 }
797
798 if (fc_host->port_name == be64_to_cpu(li_desc->attached_wwpn))
799 fc_li_stats_update(li_desc, &fc_host->fpin_stats);
800}
801
802/*
803 * fc_fpin_delivery_stats_update - routine to update Delivery Notification
804 * event statistics.
805 * @shost: host the FPIN was received on
806 * @tlv: pointer to delivery descriptor
807 *
808 */
809static void
810fc_fpin_delivery_stats_update(struct Scsi_Host *shost,
811 struct fc_tlv_desc *tlv)
812{
813 struct fc_rport *rport = NULL;
814 struct fc_rport *attach_rport = NULL;
815 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
816 struct fc_fn_deli_desc *dn_desc = (struct fc_fn_deli_desc *)tlv;
817 u32 reason_code = be32_to_cpu(dn_desc->deli_reason_code);
818
819 rport = fc_find_rport_by_wwpn(shost,
820 be64_to_cpu(dn_desc->attached_wwpn));
821 if (rport &&
822 (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
823 rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
824 attach_rport = rport;
825 fc_delivery_stats_update(reason_code,
826 &attach_rport->fpin_stats);
827 }
828
829 if (fc_host->port_name == be64_to_cpu(dn_desc->attached_wwpn))
830 fc_delivery_stats_update(reason_code, &fc_host->fpin_stats);
831}
832
833/*
834 * fc_fpin_peer_congn_stats_update - routine to update Peer Congestion
835 * event statistics.
836 * @shost: host the FPIN was received on
837 * @tlv: pointer to peer congestion descriptor
838 *
839 */
840static void
841fc_fpin_peer_congn_stats_update(struct Scsi_Host *shost,
842 struct fc_tlv_desc *tlv)
843{
844 u8 i;
845 struct fc_rport *rport = NULL;
846 struct fc_rport *attach_rport = NULL;
847 struct fc_fn_peer_congn_desc *pc_desc =
848 (struct fc_fn_peer_congn_desc *)tlv;
849 u16 event_type = be16_to_cpu(pc_desc->event_type);
850 u64 wwpn;
851
852 rport = fc_find_rport_by_wwpn(shost,
853 be64_to_cpu(pc_desc->attached_wwpn));
854 if (rport &&
855 (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
856 rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
857 attach_rport = rport;
858 fc_cn_stats_update(event_type, &attach_rport->fpin_stats);
859 }
860
861 if (be32_to_cpu(pc_desc->pname_count) > 0) {
862 for (i = 0;
863 i < be32_to_cpu(pc_desc->pname_count);
864 i++) {
865 wwpn = be64_to_cpu(pc_desc->pname_list[i]);
866 rport = fc_find_rport_by_wwpn(shost, wwpn);
867 if (rport &&
868 (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
869 rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
870 if (rport == attach_rport)
871 continue;
872 fc_cn_stats_update(event_type,
873 &rport->fpin_stats);
874 }
875 }
876 }
877}
878
879/*
880 * fc_fpin_congn_stats_update - routine to update Congestion
881 * event statistics.
882 * @shost: host the FPIN was received on
883 * @tlv: pointer to congestion descriptor
884 *
885 */
886static void
887fc_fpin_congn_stats_update(struct Scsi_Host *shost,
888 struct fc_tlv_desc *tlv)
889{
890 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
891 struct fc_fn_congn_desc *congn = (struct fc_fn_congn_desc *)tlv;
892
893 fc_cn_stats_update(be16_to_cpu(congn->event_type),
894 &fc_host->fpin_stats);
895}
896
897/**
Mauro Carvalho Chehabae6b4e62020-10-23 18:33:14 +0200898 * fc_host_fpin_rcv - routine to process a received FPIN.
James Smartc39e0af2019-04-05 16:04:22 -0700899 * @shost: host the FPIN was received on
900 * @fpin_len: length of FPIN payload, in bytes
901 * @fpin_buf: pointer to FPIN payload
902 *
903 * Notes:
904 * This routine assumes no locks are held on entry.
905 */
906void
907fc_host_fpin_rcv(struct Scsi_Host *shost, u32 fpin_len, char *fpin_buf)
908{
Shyam Sundar3dcfe0d2020-10-21 02:27:13 -0700909 struct fc_els_fpin *fpin = (struct fc_els_fpin *)fpin_buf;
910 struct fc_tlv_desc *tlv;
911 u32 desc_cnt = 0, bytes_remain;
912 u32 dtag;
913
914 /* Update Statistics */
915 tlv = (struct fc_tlv_desc *)&fpin->fpin_desc[0];
916 bytes_remain = fpin_len - offsetof(struct fc_els_fpin, fpin_desc);
917 bytes_remain = min_t(u32, bytes_remain, be32_to_cpu(fpin->desc_len));
918
919 while (bytes_remain >= FC_TLV_DESC_HDR_SZ &&
920 bytes_remain >= FC_TLV_DESC_SZ_FROM_LENGTH(tlv)) {
921 dtag = be32_to_cpu(tlv->desc_tag);
922 switch (dtag) {
923 case ELS_DTAG_LNK_INTEGRITY:
924 fc_fpin_li_stats_update(shost, tlv);
925 break;
926 case ELS_DTAG_DELIVERY:
927 fc_fpin_delivery_stats_update(shost, tlv);
928 break;
929 case ELS_DTAG_PEER_CONGEST:
930 fc_fpin_peer_congn_stats_update(shost, tlv);
931 break;
932 case ELS_DTAG_CONGESTION:
933 fc_fpin_congn_stats_update(shost, tlv);
934 }
935
936 desc_cnt++;
937 bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
938 tlv = fc_tlv_next_desc(tlv);
939 }
940
James Smartc39e0af2019-04-05 16:04:22 -0700941 fc_host_post_fc_event(shost, fc_get_event_number(),
942 FCH_EVT_LINK_FPIN, fpin_len, fpin_buf, 0);
943}
944EXPORT_SYMBOL(fc_host_fpin_rcv);
945
James Smart84314fd2006-08-18 17:30:09 -0400946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947static __init int fc_transport_init(void)
948{
James Smart84314fd2006-08-18 17:30:09 -0400949 int error;
950
951 atomic_set(&fc_event_seq, 0);
952
953 error = transport_class_register(&fc_host_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (error)
955 return error;
James Smarta53eb5e2007-04-27 12:41:09 -0400956 error = transport_class_register(&fc_vport_class);
957 if (error)
Mike Christie48de68a2009-11-17 21:25:16 -0600958 goto unreg_host_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 error = transport_class_register(&fc_rport_class);
960 if (error)
Mike Christie48de68a2009-11-17 21:25:16 -0600961 goto unreg_vport_class;
962 error = transport_class_register(&fc_transport_class);
963 if (error)
964 goto unreg_rport_class;
965 return 0;
966
967unreg_rport_class:
968 transport_class_unregister(&fc_rport_class);
969unreg_vport_class:
970 transport_class_unregister(&fc_vport_class);
971unreg_host_class:
972 transport_class_unregister(&fc_host_class);
973 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976static void __exit fc_transport_exit(void)
977{
978 transport_class_unregister(&fc_transport_class);
979 transport_class_unregister(&fc_rport_class);
980 transport_class_unregister(&fc_host_class);
James Smarta53eb5e2007-04-27 12:41:09 -0400981 transport_class_unregister(&fc_vport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
984/*
985 * FC Remote Port Attribute Management
986 */
987
988#define fc_rport_show_function(field, format_string, sz, cast) \
989static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100990show_fc_rport_##field (struct device *dev, \
991 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100993 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct Scsi_Host *shost = rport_to_shost(rport); \
995 struct fc_internal *i = to_fc_internal(shost->transportt); \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400996 if ((i->f->get_rport_##field) && \
997 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -0500998 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400999 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 i->f->get_rport_##field(rport); \
1001 return snprintf(buf, sz, format_string, cast rport->field); \
1002}
1003
1004#define fc_rport_store_function(field) \
1005static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001006store_fc_rport_##field(struct device *dev, \
1007 struct device_attribute *attr, \
1008 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{ \
1010 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001011 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 struct Scsi_Host *shost = rport_to_shost(rport); \
1013 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -04001014 char *cp; \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001015 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05001016 (rport->port_state == FC_PORTSTATE_DELETED) || \
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001017 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
1018 return -EBUSY; \
James Smart0f29b962006-08-18 17:33:29 -04001019 val = simple_strtoul(buf, &cp, 0); \
1020 if (*cp && (*cp != '\n')) \
1021 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 i->f->set_rport_##field(rport, val); \
1023 return count; \
1024}
1025
1026#define fc_rport_rd_attr(field, format_string, sz) \
1027 fc_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001028static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 show_fc_rport_##field, NULL)
1030
1031#define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
1032 fc_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001033static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 show_fc_rport_##field, NULL)
1035
1036#define fc_rport_rw_attr(field, format_string, sz) \
1037 fc_rport_show_function(field, format_string, sz, ) \
1038 fc_rport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001039static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 show_fc_rport_##field, \
1041 store_fc_rport_##field)
1042
1043
1044#define fc_private_rport_show_function(field, format_string, sz, cast) \
1045static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001046show_fc_rport_##field (struct device *dev, \
1047 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001049 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return snprintf(buf, sz, format_string, cast rport->field); \
1051}
1052
1053#define fc_private_rport_rd_attr(field, format_string, sz) \
1054 fc_private_rport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001055static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 show_fc_rport_##field, NULL)
1057
1058#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
1059 fc_private_rport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001060static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 show_fc_rport_##field, NULL)
1062
1063
1064#define fc_private_rport_rd_enum_attr(title, maxlen) \
1065static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001066show_fc_rport_##title (struct device *dev, \
1067 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001069 struct fc_rport *rport = transport_class_to_rport(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 const char *name; \
1071 name = get_fc_##title##_name(rport->title); \
1072 if (!name) \
1073 return -EINVAL; \
1074 return snprintf(buf, maxlen, "%s\n", name); \
1075} \
Tony Jonesee959b02008-02-22 00:13:36 +01001076static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 show_fc_rport_##title, NULL)
1078
1079
1080#define SETUP_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001081 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
1083 i->private_rport_attrs[count].store = NULL; \
1084 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
1085 if (i->f->show_rport_##field) \
1086 count++
1087
1088#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001089 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
1091 i->private_rport_attrs[count].store = NULL; \
1092 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
1093 count++
1094
1095#define SETUP_RPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001096 i->private_rport_attrs[count] = device_attr_rport_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (!i->f->set_rport_##field) { \
1098 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
1099 i->private_rport_attrs[count].store = NULL; \
1100 } \
1101 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
1102 if (i->f->show_rport_##field) \
1103 count++
1104
James Smart0f29b962006-08-18 17:33:29 -04001105#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
1106{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001107 i->private_rport_attrs[count] = device_attr_rport_##field; \
James Smart0f29b962006-08-18 17:33:29 -04001108 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
1109 count++; \
1110}
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113/* The FC Transport Remote Port Attributes: */
1114
1115/* Fixed Remote Port Attributes */
1116
1117fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
1118
1119static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001120show_fc_rport_supported_classes (struct device *dev,
1121 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Tony Jonesee959b02008-02-22 00:13:36 +01001123 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (rport->supported_classes == FC_COS_UNSPECIFIED)
1125 return snprintf(buf, 20, "unspecified\n");
1126 return get_fc_cos_names(rport->supported_classes, buf);
1127}
Tony Jonesee959b02008-02-22 00:13:36 +01001128static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 show_fc_rport_supported_classes, NULL);
1130
1131/* Dynamic Remote Port Attributes */
1132
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001133/*
1134 * dev_loss_tmo attribute
1135 */
Mike Christie43ca9102010-09-15 16:52:32 -05001136static int fc_str_to_dev_loss(const char *buf, unsigned long *val)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001137{
Mike Christie43ca9102010-09-15 16:52:32 -05001138 char *cp;
1139
1140 *val = simple_strtoul(buf, &cp, 0);
Colin Ian Kingeea42272017-04-14 14:58:02 +01001141 if (*cp && (*cp != '\n'))
Mike Christie43ca9102010-09-15 16:52:32 -05001142 return -EINVAL;
1143 /*
1144 * Check for overflow; dev_loss_tmo is u32
1145 */
1146 if (*val > UINT_MAX)
1147 return -EINVAL;
1148
1149 return 0;
1150}
1151
1152static int fc_rport_set_dev_loss_tmo(struct fc_rport *rport,
1153 unsigned long val)
1154{
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001155 struct Scsi_Host *shost = rport_to_shost(rport);
1156 struct fc_internal *i = to_fc_internal(shost->transportt);
Mike Christie43ca9102010-09-15 16:52:32 -05001157
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001158 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05001159 (rport->port_state == FC_PORTSTATE_DELETED) ||
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001160 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
1161 return -EBUSY;
Hannes Reineckef28186632009-12-15 09:26:06 +01001162 /*
Hannes Reinecke36dd2882010-03-09 10:18:48 +01001163 * Check for overflow; dev_loss_tmo is u32
1164 */
1165 if (val > UINT_MAX)
1166 return -EINVAL;
1167
1168 /*
Hannes Reineckef28186632009-12-15 09:26:06 +01001169 * If fast_io_fail is off we have to cap
1170 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
1171 */
1172 if (rport->fast_io_fail_tmo == -1 &&
1173 val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
1174 return -EINVAL;
1175
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001176 i->f->set_rport_dev_loss_tmo(rport, val);
Mike Christie43ca9102010-09-15 16:52:32 -05001177 return 0;
1178}
1179
1180fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
1181static ssize_t
1182store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
1183 const char *buf, size_t count)
1184{
1185 struct fc_rport *rport = transport_class_to_rport(dev);
1186 unsigned long val;
1187 int rc;
1188
1189 rc = fc_str_to_dev_loss(buf, &val);
1190 if (rc)
1191 return rc;
1192
1193 rc = fc_rport_set_dev_loss_tmo(rport, val);
1194 if (rc)
1195 return rc;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001196 return count;
1197}
Tony Jonesee959b02008-02-22 00:13:36 +01001198static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001199 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201
1202/* Private Remote Port Attributes */
1203
1204fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1205fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1206fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
1207
1208static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001209show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
1210 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Tony Jonesee959b02008-02-22 00:13:36 +01001212 struct fc_rport *rport = transport_class_to_rport(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 /* identify any roles that are port_id specific */
1215 if ((rport->port_id != -1) &&
1216 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
1217 FC_WELLKNOWN_PORTID_MASK) {
1218 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
1219 case FC_FPORT_PORTID:
1220 return snprintf(buf, 30, "Fabric Port\n");
1221 case FC_FABCTLR_PORTID:
1222 return snprintf(buf, 30, "Fabric Controller\n");
1223 case FC_DIRSRVR_PORTID:
1224 return snprintf(buf, 30, "Directory Server\n");
1225 case FC_TIMESRVR_PORTID:
1226 return snprintf(buf, 30, "Time Server\n");
1227 case FC_MGMTSRVR_PORTID:
1228 return snprintf(buf, 30, "Management Server\n");
1229 default:
1230 return snprintf(buf, 30, "Unknown Fabric Entity\n");
1231 }
1232 } else {
James Smarta53eb5e2007-04-27 12:41:09 -04001233 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return snprintf(buf, 20, "unknown\n");
James Smarta53eb5e2007-04-27 12:41:09 -04001235 return get_fc_port_roles_names(rport->roles, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237}
Tony Jonesee959b02008-02-22 00:13:36 +01001238static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 show_fc_rport_roles, NULL);
1240
Muneendra Kumarafdd1122021-01-07 03:19:07 +05301241static ssize_t fc_rport_set_marginal_state(struct device *dev,
1242 struct device_attribute *attr,
1243 const char *buf, size_t count)
1244{
1245 struct fc_rport *rport = transport_class_to_rport(dev);
1246 enum fc_port_state port_state;
1247 int ret = 0;
1248
1249 ret = get_fc_port_state_match(buf, &port_state);
1250 if (ret)
1251 return -EINVAL;
1252 if (port_state == FC_PORTSTATE_MARGINAL) {
1253 /*
1254 * Change the state to Marginal only if the
1255 * current rport state is Online
1256 * Allow only Online->Marginal
1257 */
1258 if (rport->port_state == FC_PORTSTATE_ONLINE)
1259 rport->port_state = port_state;
1260 else
1261 return -EINVAL;
1262 } else if (port_state == FC_PORTSTATE_ONLINE) {
1263 /*
1264 * Change the state to Online only if the
1265 * current rport state is Marginal
1266 * Allow only Marginal->Online
1267 */
1268 if (rport->port_state == FC_PORTSTATE_MARGINAL)
1269 rport->port_state = port_state;
1270 else
1271 return -EINVAL;
1272 } else
1273 return -EINVAL;
1274 return count;
1275}
1276
1277static ssize_t
1278show_fc_rport_port_state(struct device *dev,
1279 struct device_attribute *attr, char *buf)
1280{
1281 const char *name;
1282 struct fc_rport *rport = transport_class_to_rport(dev);
1283
1284 name = get_fc_port_state_name(rport->port_state);
1285 if (!name)
1286 return -EINVAL;
1287
1288 return snprintf(buf, 20, "%s\n", name);
1289}
1290
1291static FC_DEVICE_ATTR(rport, port_state, 0444 | 0200,
1292 show_fc_rport_port_state, fc_rport_set_marginal_state);
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
1295
James Smart0f29b962006-08-18 17:33:29 -04001296/*
1297 * fast_io_fail_tmo attribute
1298 */
1299static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001300show_fc_rport_fast_io_fail_tmo (struct device *dev,
1301 struct device_attribute *attr, char *buf)
James Smart0f29b962006-08-18 17:33:29 -04001302{
Tony Jonesee959b02008-02-22 00:13:36 +01001303 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -04001304
1305 if (rport->fast_io_fail_tmo == -1)
1306 return snprintf(buf, 5, "off\n");
1307 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
1308}
1309
1310static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001311store_fc_rport_fast_io_fail_tmo(struct device *dev,
1312 struct device_attribute *attr, const char *buf,
1313 size_t count)
James Smart0f29b962006-08-18 17:33:29 -04001314{
1315 int val;
1316 char *cp;
Tony Jonesee959b02008-02-22 00:13:36 +01001317 struct fc_rport *rport = transport_class_to_rport(dev);
James Smart0f29b962006-08-18 17:33:29 -04001318
1319 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
1320 (rport->port_state == FC_PORTSTATE_DELETED) ||
1321 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
1322 return -EBUSY;
1323 if (strncmp(buf, "off", 3) == 0)
1324 rport->fast_io_fail_tmo = -1;
1325 else {
1326 val = simple_strtoul(buf, &cp, 0);
Hannes Reineckef28186632009-12-15 09:26:06 +01001327 if ((*cp && (*cp != '\n')) || (val < 0))
James Smart0f29b962006-08-18 17:33:29 -04001328 return -EINVAL;
Hannes Reineckef28186632009-12-15 09:26:06 +01001329 /*
1330 * Cap fast_io_fail by dev_loss_tmo or
1331 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
1332 */
1333 if ((val >= rport->dev_loss_tmo) ||
1334 (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
1335 return -EINVAL;
1336
James Smart0f29b962006-08-18 17:33:29 -04001337 rport->fast_io_fail_tmo = val;
1338 }
1339 return count;
1340}
Tony Jonesee959b02008-02-22 00:13:36 +01001341static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
James Smart0f29b962006-08-18 17:33:29 -04001342 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Shyam Sundar547aab52020-10-21 02:27:12 -07001344#define fc_rport_fpin_statistic(name) \
1345static ssize_t fc_rport_fpinstat_##name(struct device *cd, \
1346 struct device_attribute *attr, \
1347 char *buf) \
1348{ \
1349 struct fc_rport *rport = transport_class_to_rport(cd); \
1350 \
1351 return snprintf(buf, 20, "0x%llx\n", rport->fpin_stats.name); \
1352} \
1353static FC_DEVICE_ATTR(rport, fpin_##name, 0444, fc_rport_fpinstat_##name, NULL)
1354
1355fc_rport_fpin_statistic(dn);
1356fc_rport_fpin_statistic(dn_unknown);
1357fc_rport_fpin_statistic(dn_timeout);
1358fc_rport_fpin_statistic(dn_unable_to_route);
1359fc_rport_fpin_statistic(dn_device_specific);
1360fc_rport_fpin_statistic(cn);
1361fc_rport_fpin_statistic(cn_clear);
1362fc_rport_fpin_statistic(cn_lost_credit);
1363fc_rport_fpin_statistic(cn_credit_stall);
1364fc_rport_fpin_statistic(cn_oversubscription);
1365fc_rport_fpin_statistic(cn_device_specific);
1366fc_rport_fpin_statistic(li);
1367fc_rport_fpin_statistic(li_failure_unknown);
1368fc_rport_fpin_statistic(li_link_failure_count);
1369fc_rport_fpin_statistic(li_loss_of_sync_count);
1370fc_rport_fpin_statistic(li_loss_of_signals_count);
1371fc_rport_fpin_statistic(li_prim_seq_err_count);
1372fc_rport_fpin_statistic(li_invalid_tx_word_count);
1373fc_rport_fpin_statistic(li_invalid_crc_count);
1374fc_rport_fpin_statistic(li_device_specific);
1375
1376static struct attribute *fc_rport_statistics_attrs[] = {
1377 &device_attr_rport_fpin_dn.attr,
1378 &device_attr_rport_fpin_dn_unknown.attr,
1379 &device_attr_rport_fpin_dn_timeout.attr,
1380 &device_attr_rport_fpin_dn_unable_to_route.attr,
1381 &device_attr_rport_fpin_dn_device_specific.attr,
1382 &device_attr_rport_fpin_li.attr,
1383 &device_attr_rport_fpin_li_failure_unknown.attr,
1384 &device_attr_rport_fpin_li_link_failure_count.attr,
1385 &device_attr_rport_fpin_li_loss_of_sync_count.attr,
1386 &device_attr_rport_fpin_li_loss_of_signals_count.attr,
1387 &device_attr_rport_fpin_li_prim_seq_err_count.attr,
1388 &device_attr_rport_fpin_li_invalid_tx_word_count.attr,
1389 &device_attr_rport_fpin_li_invalid_crc_count.attr,
1390 &device_attr_rport_fpin_li_device_specific.attr,
1391 &device_attr_rport_fpin_cn.attr,
1392 &device_attr_rport_fpin_cn_clear.attr,
1393 &device_attr_rport_fpin_cn_lost_credit.attr,
1394 &device_attr_rport_fpin_cn_credit_stall.attr,
1395 &device_attr_rport_fpin_cn_oversubscription.attr,
1396 &device_attr_rport_fpin_cn_device_specific.attr,
1397 NULL
1398};
1399
1400static struct attribute_group fc_rport_statistics_group = {
1401 .name = "statistics",
1402 .attrs = fc_rport_statistics_attrs,
1403};
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406/*
1407 * FC SCSI Target Attribute Management
1408 */
1409
1410/*
1411 * Note: in the target show function we recognize when the remote
Uwe Kleine-König732bee72010-06-11 12:16:59 +02001412 * port is in the hierarchy and do not allow the driver to get
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 * involved in sysfs functions. The driver only gets involved if
1414 * it's the "old" style that doesn't use rports.
1415 */
1416#define fc_starget_show_function(field, format_string, sz, cast) \
1417static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001418show_fc_starget_##field (struct device *dev, \
1419 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001421 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
1423 struct fc_internal *i = to_fc_internal(shost->transportt); \
1424 struct fc_rport *rport = starget_to_rport(starget); \
1425 if (rport) \
1426 fc_starget_##field(starget) = rport->field; \
1427 else if (i->f->get_starget_##field) \
1428 i->f->get_starget_##field(starget); \
1429 return snprintf(buf, sz, format_string, \
1430 cast fc_starget_##field(starget)); \
1431}
1432
1433#define fc_starget_rd_attr(field, format_string, sz) \
1434 fc_starget_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001435static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 show_fc_starget_##field, NULL)
1437
1438#define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
1439 fc_starget_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001440static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 show_fc_starget_##field, NULL)
1442
1443#define SETUP_STARGET_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001444 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1446 i->private_starget_attrs[count].store = NULL; \
1447 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1448 if (i->f->show_starget_##field) \
1449 count++
1450
1451#define SETUP_STARGET_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001452 i->private_starget_attrs[count] = device_attr_starget_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (!i->f->set_starget_##field) { \
1454 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
1455 i->private_starget_attrs[count].store = NULL; \
1456 } \
1457 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
1458 if (i->f->show_starget_##field) \
1459 count++
1460
1461/* The FC Transport SCSI Target Attributes: */
1462fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1463fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1464fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1465
1466
1467/*
James Smarta53eb5e2007-04-27 12:41:09 -04001468 * FC Virtual Port Attribute Management
1469 */
1470
1471#define fc_vport_show_function(field, format_string, sz, cast) \
1472static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001473show_fc_vport_##field (struct device *dev, \
1474 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001475{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001476 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001477 struct Scsi_Host *shost = vport_to_shost(vport); \
1478 struct fc_internal *i = to_fc_internal(shost->transportt); \
1479 if ((i->f->get_vport_##field) && \
1480 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1481 i->f->get_vport_##field(vport); \
1482 return snprintf(buf, sz, format_string, cast vport->field); \
1483}
1484
1485#define fc_vport_store_function(field) \
1486static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001487store_fc_vport_##field(struct device *dev, \
1488 struct device_attribute *attr, \
1489 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001490{ \
1491 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001492 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001493 struct Scsi_Host *shost = vport_to_shost(vport); \
1494 struct fc_internal *i = to_fc_internal(shost->transportt); \
1495 char *cp; \
1496 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1497 return -EBUSY; \
1498 val = simple_strtoul(buf, &cp, 0); \
1499 if (*cp && (*cp != '\n')) \
1500 return -EINVAL; \
1501 i->f->set_vport_##field(vport, val); \
1502 return count; \
1503}
1504
1505#define fc_vport_store_str_function(field, slen) \
1506static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001507store_fc_vport_##field(struct device *dev, \
1508 struct device_attribute *attr, \
1509 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001510{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001511 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001512 struct Scsi_Host *shost = vport_to_shost(vport); \
1513 struct fc_internal *i = to_fc_internal(shost->transportt); \
1514 unsigned int cnt=count; \
1515 \
1516 /* count may include a LF at end of string */ \
1517 if (buf[cnt-1] == '\n') \
1518 cnt--; \
1519 if (cnt > ((slen) - 1)) \
1520 return -EINVAL; \
1521 memcpy(vport->field, buf, cnt); \
1522 i->f->set_vport_##field(vport); \
1523 return count; \
1524}
1525
1526#define fc_vport_rd_attr(field, format_string, sz) \
1527 fc_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001528static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001529 show_fc_vport_##field, NULL)
1530
1531#define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1532 fc_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001533static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001534 show_fc_vport_##field, NULL)
1535
1536#define fc_vport_rw_attr(field, format_string, sz) \
1537 fc_vport_show_function(field, format_string, sz, ) \
1538 fc_vport_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001539static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001540 show_fc_vport_##field, \
1541 store_fc_vport_##field)
1542
1543#define fc_private_vport_show_function(field, format_string, sz, cast) \
1544static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001545show_fc_vport_##field (struct device *dev, \
1546 struct device_attribute *attr, char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001547{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001548 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001549 return snprintf(buf, sz, format_string, cast vport->field); \
1550}
1551
1552#define fc_private_vport_store_u32_function(field) \
1553static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001554store_fc_vport_##field(struct device *dev, \
1555 struct device_attribute *attr, \
1556 const char *buf, size_t count) \
James Smarta53eb5e2007-04-27 12:41:09 -04001557{ \
1558 u32 val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001559 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001560 char *cp; \
1561 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1562 return -EBUSY; \
1563 val = simple_strtoul(buf, &cp, 0); \
1564 if (*cp && (*cp != '\n')) \
1565 return -EINVAL; \
1566 vport->field = val; \
1567 return count; \
1568}
1569
1570
1571#define fc_private_vport_rd_attr(field, format_string, sz) \
1572 fc_private_vport_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001573static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001574 show_fc_vport_##field, NULL)
1575
1576#define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1577 fc_private_vport_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001578static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001579 show_fc_vport_##field, NULL)
1580
1581#define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1582 fc_private_vport_show_function(field, format_string, sz, ) \
1583 fc_private_vport_store_u32_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001584static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
James Smarta53eb5e2007-04-27 12:41:09 -04001585 show_fc_vport_##field, \
1586 store_fc_vport_##field)
1587
1588
1589#define fc_private_vport_rd_enum_attr(title, maxlen) \
1590static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001591show_fc_vport_##title (struct device *dev, \
1592 struct device_attribute *attr, \
1593 char *buf) \
James Smarta53eb5e2007-04-27 12:41:09 -04001594{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001595 struct fc_vport *vport = transport_class_to_vport(dev); \
James Smarta53eb5e2007-04-27 12:41:09 -04001596 const char *name; \
1597 name = get_fc_##title##_name(vport->title); \
1598 if (!name) \
1599 return -EINVAL; \
1600 return snprintf(buf, maxlen, "%s\n", name); \
1601} \
Tony Jonesee959b02008-02-22 00:13:36 +01001602static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
James Smarta53eb5e2007-04-27 12:41:09 -04001603 show_fc_vport_##title, NULL)
1604
1605
1606#define SETUP_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001607 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001608 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1609 i->private_vport_attrs[count].store = NULL; \
1610 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1611 if (i->f->get_##field) \
1612 count++
1613 /* NOTE: Above MACRO differs: checks function not show bit */
1614
1615#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001616 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001617 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1618 i->private_vport_attrs[count].store = NULL; \
1619 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1620 count++
1621
1622#define SETUP_VPORT_ATTRIBUTE_WR(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001623 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001624 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1625 if (i->f->field) \
1626 count++
1627 /* NOTE: Above MACRO differs: checks function */
1628
1629#define SETUP_VPORT_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001630 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001631 if (!i->f->set_vport_##field) { \
1632 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1633 i->private_vport_attrs[count].store = NULL; \
1634 } \
1635 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1636 count++
1637 /* NOTE: Above MACRO differs: does not check show bit */
1638
1639#define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1640{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001641 i->private_vport_attrs[count] = device_attr_vport_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001642 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1643 count++; \
1644}
1645
1646
1647/* The FC Transport Virtual Port Attributes: */
1648
1649/* Fixed Virtual Port Attributes */
1650
1651/* Dynamic Virtual Port Attributes */
1652
1653/* Private Virtual Port Attributes */
1654
1655fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1656fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1657fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1658fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1659
1660static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001661show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1662 char *buf)
James Smarta53eb5e2007-04-27 12:41:09 -04001663{
Tony Jonesee959b02008-02-22 00:13:36 +01001664 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001665
1666 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1667 return snprintf(buf, 20, "unknown\n");
1668 return get_fc_port_roles_names(vport->roles, buf);
1669}
Tony Jonesee959b02008-02-22 00:13:36 +01001670static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
James Smarta53eb5e2007-04-27 12:41:09 -04001671
1672fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1673
1674fc_private_vport_show_function(symbolic_name, "%s\n",
1675 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1676fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
Tony Jonesee959b02008-02-22 00:13:36 +01001677static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001678 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1679
1680static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001681store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1682 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04001683{
Tony Jonesee959b02008-02-22 00:13:36 +01001684 struct fc_vport *vport = transport_class_to_vport(dev);
James Smart9ef3e4a2007-05-24 19:04:44 -04001685 struct Scsi_Host *shost = vport_to_shost(vport);
Gal Rosen0d9dc7c2010-01-21 10:15:32 +02001686 unsigned long flags;
1687
1688 spin_lock_irqsave(shost->host_lock, flags);
Hannes Reinecke260f4ae2017-07-24 12:09:36 +02001689 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING | FC_VPORT_DELETING)) {
Gal Rosen0d9dc7c2010-01-21 10:15:32 +02001690 spin_unlock_irqrestore(shost->host_lock, flags);
1691 return -EBUSY;
1692 }
1693 vport->flags |= FC_VPORT_DELETING;
1694 spin_unlock_irqrestore(shost->host_lock, flags);
James Smarta53eb5e2007-04-27 12:41:09 -04001695
James Smart9ef3e4a2007-05-24 19:04:44 -04001696 fc_queue_work(shost, &vport->vport_delete_work);
James Smarta53eb5e2007-04-27 12:41:09 -04001697 return count;
1698}
Tony Jonesee959b02008-02-22 00:13:36 +01001699static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001700 NULL, store_fc_vport_delete);
1701
1702
1703/*
1704 * Enable/Disable vport
1705 * Write "1" to disable, write "0" to enable
1706 */
1707static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001708store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1709 const char *buf,
James Smarta53eb5e2007-04-27 12:41:09 -04001710 size_t count)
1711{
Tony Jonesee959b02008-02-22 00:13:36 +01001712 struct fc_vport *vport = transport_class_to_vport(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04001713 struct Scsi_Host *shost = vport_to_shost(vport);
1714 struct fc_internal *i = to_fc_internal(shost->transportt);
1715 int stat;
1716
1717 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1718 return -EBUSY;
1719
1720 if (*buf == '0') {
1721 if (vport->vport_state != FC_VPORT_DISABLED)
1722 return -EALREADY;
1723 } else if (*buf == '1') {
1724 if (vport->vport_state == FC_VPORT_DISABLED)
1725 return -EALREADY;
1726 } else
1727 return -EINVAL;
1728
1729 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1730 return stat ? stat : count;
1731}
Tony Jonesee959b02008-02-22 00:13:36 +01001732static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
James Smarta53eb5e2007-04-27 12:41:09 -04001733 NULL, store_fc_vport_disable);
1734
1735
1736/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 * Host Attribute Management
1738 */
1739
1740#define fc_host_show_function(field, format_string, sz, cast) \
1741static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001742show_fc_host_##field (struct device *dev, \
1743 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001745 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 struct fc_internal *i = to_fc_internal(shost->transportt); \
1747 if (i->f->get_host_##field) \
1748 i->f->get_host_##field(shost); \
1749 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1750}
1751
1752#define fc_host_store_function(field) \
1753static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001754store_fc_host_##field(struct device *dev, \
1755 struct device_attribute *attr, \
1756 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{ \
1758 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +01001759 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 struct fc_internal *i = to_fc_internal(shost->transportt); \
James Smart0f29b962006-08-18 17:33:29 -04001761 char *cp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 \
James Smart0f29b962006-08-18 17:33:29 -04001763 val = simple_strtoul(buf, &cp, 0); \
1764 if (*cp && (*cp != '\n')) \
1765 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 i->f->set_host_##field(shost, val); \
1767 return count; \
1768}
1769
James Smartb8d08212006-08-17 08:00:43 -04001770#define fc_host_store_str_function(field, slen) \
1771static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001772store_fc_host_##field(struct device *dev, \
1773 struct device_attribute *attr, \
1774 const char *buf, size_t count) \
James Smartb8d08212006-08-17 08:00:43 -04001775{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001776 struct Scsi_Host *shost = transport_class_to_shost(dev); \
James Smartb8d08212006-08-17 08:00:43 -04001777 struct fc_internal *i = to_fc_internal(shost->transportt); \
1778 unsigned int cnt=count; \
1779 \
1780 /* count may include a LF at end of string */ \
1781 if (buf[cnt-1] == '\n') \
1782 cnt--; \
1783 if (cnt > ((slen) - 1)) \
1784 return -EINVAL; \
1785 memcpy(fc_host_##field(shost), buf, cnt); \
1786 i->f->set_host_##field(shost); \
1787 return count; \
1788}
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790#define fc_host_rd_attr(field, format_string, sz) \
1791 fc_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001792static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 show_fc_host_##field, NULL)
1794
1795#define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1796 fc_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001797static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 show_fc_host_##field, NULL)
1799
1800#define fc_host_rw_attr(field, format_string, sz) \
1801 fc_host_show_function(field, format_string, sz, ) \
1802 fc_host_store_function(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001803static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 show_fc_host_##field, \
1805 store_fc_host_##field)
1806
1807#define fc_host_rd_enum_attr(title, maxlen) \
1808static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001809show_fc_host_##title (struct device *dev, \
1810 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001812 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct fc_internal *i = to_fc_internal(shost->transportt); \
1814 const char *name; \
1815 if (i->f->get_host_##title) \
1816 i->f->get_host_##title(shost); \
1817 name = get_fc_##title##_name(fc_host_##title(shost)); \
1818 if (!name) \
1819 return -EINVAL; \
1820 return snprintf(buf, maxlen, "%s\n", name); \
1821} \
Tony Jonesee959b02008-02-22 00:13:36 +01001822static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824#define SETUP_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001825 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1827 i->private_host_attrs[count].store = NULL; \
1828 i->host_attrs[count] = &i->private_host_attrs[count]; \
1829 if (i->f->show_host_##field) \
1830 count++
1831
James Smarta53eb5e2007-04-27 12:41:09 -04001832#define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001833 i->private_host_attrs[count] = device_attr_host_##field; \
James Smarta53eb5e2007-04-27 12:41:09 -04001834 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1835 i->private_host_attrs[count].store = NULL; \
1836 i->host_attrs[count] = &i->private_host_attrs[count]; \
1837 count++
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839#define SETUP_HOST_ATTRIBUTE_RW(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001840 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (!i->f->set_host_##field) { \
1842 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1843 i->private_host_attrs[count].store = NULL; \
1844 } \
1845 i->host_attrs[count] = &i->private_host_attrs[count]; \
1846 if (i->f->show_host_##field) \
1847 count++
1848
1849
1850#define fc_private_host_show_function(field, format_string, sz, cast) \
1851static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001852show_fc_host_##field (struct device *dev, \
1853 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001855 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1857}
1858
1859#define fc_private_host_rd_attr(field, format_string, sz) \
1860 fc_private_host_show_function(field, format_string, sz, ) \
Tony Jonesee959b02008-02-22 00:13:36 +01001861static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 show_fc_host_##field, NULL)
1863
1864#define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1865 fc_private_host_show_function(field, format_string, sz, (cast)) \
Tony Jonesee959b02008-02-22 00:13:36 +01001866static FC_DEVICE_ATTR(host, field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 show_fc_host_##field, NULL)
1868
1869#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
Tony Jonesee959b02008-02-22 00:13:36 +01001870 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1872 i->private_host_attrs[count].store = NULL; \
1873 i->host_attrs[count] = &i->private_host_attrs[count]; \
1874 count++
1875
1876#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001877{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001878 i->private_host_attrs[count] = device_attr_host_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 i->host_attrs[count] = &i->private_host_attrs[count]; \
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07001880 count++; \
1881}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883
1884/* Fixed Host Attributes */
1885
1886static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001887show_fc_host_supported_classes (struct device *dev,
1888 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Tony Jonesee959b02008-02-22 00:13:36 +01001890 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1893 return snprintf(buf, 20, "unspecified\n");
1894
1895 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1896}
Tony Jonesee959b02008-02-22 00:13:36 +01001897static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 show_fc_host_supported_classes, NULL);
1899
1900static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001901show_fc_host_supported_fc4s (struct device *dev,
1902 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Tony Jonesee959b02008-02-22 00:13:36 +01001904 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1906}
Tony Jonesee959b02008-02-22 00:13:36 +01001907static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 show_fc_host_supported_fc4s, NULL);
1909
1910static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001911show_fc_host_supported_speeds (struct device *dev,
1912 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Tony Jonesee959b02008-02-22 00:13:36 +01001914 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1917 return snprintf(buf, 20, "unknown\n");
1918
1919 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1920}
Tony Jonesee959b02008-02-22 00:13:36 +01001921static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 show_fc_host_supported_speeds, NULL);
1923
1924
1925fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1926fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01001927fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1928 unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
James Smarta53eb5e2007-04-27 12:41:09 -04001930fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
Neerav Parikhbb8ef582012-01-22 17:29:50 -08001932fc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1);
1933fc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1934fc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1935fc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1936fc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1937fc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1938fc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940
1941/* Dynamic Host Attributes */
1942
1943static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001944show_fc_host_active_fc4s (struct device *dev,
1945 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Tony Jonesee959b02008-02-22 00:13:36 +01001947 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 struct fc_internal *i = to_fc_internal(shost->transportt);
1949
1950 if (i->f->get_host_active_fc4s)
1951 i->f->get_host_active_fc4s(shost);
1952
1953 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1954}
Tony Jonesee959b02008-02-22 00:13:36 +01001955static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 show_fc_host_active_fc4s, NULL);
1957
1958static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001959show_fc_host_speed (struct device *dev,
1960 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Tony Jonesee959b02008-02-22 00:13:36 +01001962 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 struct fc_internal *i = to_fc_internal(shost->transportt);
1964
1965 if (i->f->get_host_speed)
1966 i->f->get_host_speed(shost);
1967
1968 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1969 return snprintf(buf, 20, "unknown\n");
1970
1971 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1972}
Tony Jonesee959b02008-02-22 00:13:36 +01001973static FC_DEVICE_ATTR(host, speed, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 show_fc_host_speed, NULL);
1975
1976
1977fc_host_rd_attr(port_id, "0x%06x\n", 20);
1978fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1979fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1980fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
James Smart016131b2006-08-14 08:20:25 -04001981fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
James Smartb8d08212006-08-17 08:00:43 -04001983fc_private_host_show_function(system_hostname, "%s\n",
1984 FC_SYMBOLIC_NAME_SIZE + 1, )
1985fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
Tony Jonesee959b02008-02-22 00:13:36 +01001986static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
James Smartb8d08212006-08-17 08:00:43 -04001987 show_fc_host_system_hostname, store_fc_host_system_hostname);
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990/* Private Host Attributes */
1991
1992static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001993show_fc_private_host_tgtid_bind_type(struct device *dev,
1994 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
Tony Jonesee959b02008-02-22 00:13:36 +01001996 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 const char *name;
1998
1999 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
2000 if (!name)
2001 return -EINVAL;
2002 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
2003}
2004
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04002005#define get_list_head_entry(pos, head, member) \
2006 pos = list_entry((head)->next, typeof(*pos), member)
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01002009store_fc_private_host_tgtid_bind_type(struct device *dev,
2010 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Tony Jonesee959b02008-02-22 00:13:36 +01002012 struct Scsi_Host *shost = transport_class_to_shost(dev);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04002013 struct fc_rport *rport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 enum fc_tgtid_binding_type val;
2015 unsigned long flags;
2016
2017 if (get_fc_tgtid_bind_type_match(buf, &val))
2018 return -EINVAL;
2019
2020 /* if changing bind type, purge all unused consistent bindings */
2021 if (val != fc_host_tgtid_bind_type(shost)) {
2022 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04002023 while (!list_empty(&fc_host_rport_bindings(shost))) {
2024 get_list_head_entry(rport,
2025 &fc_host_rport_bindings(shost), peers);
James Smartaedf3492006-04-10 10:14:05 -04002026 list_del(&rport->peers);
2027 rport->port_state = FC_PORTSTATE_DELETED;
2028 fc_queue_work(shost, &rport->rport_delete_work);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -04002029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 spin_unlock_irqrestore(shost->host_lock, flags);
2031 }
2032
2033 fc_host_tgtid_bind_type(shost) = val;
2034 return count;
2035}
2036
Tony Jonesee959b02008-02-22 00:13:36 +01002037static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 show_fc_private_host_tgtid_bind_type,
2039 store_fc_private_host_tgtid_bind_type);
2040
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002041static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01002042store_fc_private_host_issue_lip(struct device *dev,
2043 struct device_attribute *attr, const char *buf, size_t count)
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002044{
Tony Jonesee959b02008-02-22 00:13:36 +01002045 struct Scsi_Host *shost = transport_class_to_shost(dev);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002046 struct fc_internal *i = to_fc_internal(shost->transportt);
2047 int ret;
2048
2049 /* ignore any data value written to the attribute */
2050 if (i->f->issue_fc_host_lip) {
2051 ret = i->f->issue_fc_host_lip(shost);
2052 return ret ? ret: count;
2053 }
2054
2055 return -ENOENT;
2056}
2057
Tony Jonesee959b02008-02-22 00:13:36 +01002058static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002059 store_fc_private_host_issue_lip);
2060
Mike Christie43ca9102010-09-15 16:52:32 -05002061static ssize_t
2062store_fc_private_host_dev_loss_tmo(struct device *dev,
2063 struct device_attribute *attr,
2064 const char *buf, size_t count)
2065{
2066 struct Scsi_Host *shost = transport_class_to_shost(dev);
2067 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2068 struct fc_rport *rport;
2069 unsigned long val, flags;
2070 int rc;
James Smarta53eb5e2007-04-27 12:41:09 -04002071
Mike Christie43ca9102010-09-15 16:52:32 -05002072 rc = fc_str_to_dev_loss(buf, &val);
2073 if (rc)
2074 return rc;
2075
2076 fc_host_dev_loss_tmo(shost) = val;
2077 spin_lock_irqsave(shost->host_lock, flags);
2078 list_for_each_entry(rport, &fc_host->rports, peers)
2079 fc_rport_set_dev_loss_tmo(rport, val);
2080 spin_unlock_irqrestore(shost->host_lock, flags);
2081 return count;
2082}
2083
2084fc_private_host_show_function(dev_loss_tmo, "%d\n", 20, );
2085static FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR,
2086 show_fc_host_dev_loss_tmo,
2087 store_fc_private_host_dev_loss_tmo);
2088
2089fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
James Smarta53eb5e2007-04-27 12:41:09 -04002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091/*
2092 * Host Statistics Management
2093 */
2094
Tyrel Datwyler134699d2017-06-21 16:24:39 -04002095/* Show a given attribute in the statistics group */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01002097fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Tony Jonesee959b02008-02-22 00:13:36 +01002099 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 struct fc_internal *i = to_fc_internal(shost->transportt);
2101 struct fc_host_statistics *stats;
2102 ssize_t ret = -ENOENT;
2103
2104 if (offset > sizeof(struct fc_host_statistics) ||
2105 offset % sizeof(u64) != 0)
2106 WARN_ON(1);
2107
2108 if (i->f->get_fc_host_stats) {
2109 stats = (i->f->get_fc_host_stats)(shost);
2110 if (stats)
2111 ret = snprintf(buf, 20, "0x%llx\n",
2112 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
2113 }
2114 return ret;
2115}
2116
2117
2118/* generate a read-only statistics attribute */
2119#define fc_host_statistic(name) \
Tony Jonesee959b02008-02-22 00:13:36 +01002120static ssize_t show_fcstat_##name(struct device *cd, \
2121 struct device_attribute *attr, \
2122 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{ \
2124 return fc_stat_show(cd, buf, \
2125 offsetof(struct fc_host_statistics, name)); \
2126} \
Tony Jonesee959b02008-02-22 00:13:36 +01002127static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129fc_host_statistic(seconds_since_last_reset);
2130fc_host_statistic(tx_frames);
2131fc_host_statistic(tx_words);
2132fc_host_statistic(rx_frames);
2133fc_host_statistic(rx_words);
2134fc_host_statistic(lip_count);
2135fc_host_statistic(nos_count);
2136fc_host_statistic(error_frames);
2137fc_host_statistic(dumped_frames);
2138fc_host_statistic(link_failure_count);
2139fc_host_statistic(loss_of_sync_count);
2140fc_host_statistic(loss_of_signal_count);
2141fc_host_statistic(prim_seq_protocol_err_count);
2142fc_host_statistic(invalid_tx_word_count);
2143fc_host_statistic(invalid_crc_count);
2144fc_host_statistic(fcp_input_requests);
2145fc_host_statistic(fcp_output_requests);
2146fc_host_statistic(fcp_control_requests);
2147fc_host_statistic(fcp_input_megabytes);
2148fc_host_statistic(fcp_output_megabytes);
Vasu Deve58abb02012-05-25 10:26:38 -07002149fc_host_statistic(fcp_packet_alloc_failures);
2150fc_host_statistic(fcp_packet_aborts);
2151fc_host_statistic(fcp_frame_alloc_failures);
2152fc_host_statistic(fc_no_free_exch);
2153fc_host_statistic(fc_no_free_exch_xid);
2154fc_host_statistic(fc_xid_not_found);
2155fc_host_statistic(fc_xid_busy);
2156fc_host_statistic(fc_seq_not_found);
2157fc_host_statistic(fc_non_bls_resp);
Shyam Sundar84610192020-10-21 02:27:14 -07002158fc_host_statistic(cn_sig_warn);
2159fc_host_statistic(cn_sig_alarm);
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Shyam Sundar547aab52020-10-21 02:27:12 -07002162#define fc_host_fpin_statistic(name) \
2163static ssize_t fc_host_fpinstat_##name(struct device *cd, \
2164 struct device_attribute *attr, \
2165 char *buf) \
2166{ \
2167 struct Scsi_Host *shost = transport_class_to_shost(cd); \
2168 struct fc_host_attrs *fc_host = shost_to_fc_host(shost); \
2169 \
2170 return snprintf(buf, 20, "0x%llx\n", fc_host->fpin_stats.name); \
2171} \
2172static FC_DEVICE_ATTR(host, fpin_##name, 0444, fc_host_fpinstat_##name, NULL)
2173
2174fc_host_fpin_statistic(dn);
2175fc_host_fpin_statistic(dn_unknown);
2176fc_host_fpin_statistic(dn_timeout);
2177fc_host_fpin_statistic(dn_unable_to_route);
2178fc_host_fpin_statistic(dn_device_specific);
2179fc_host_fpin_statistic(cn);
2180fc_host_fpin_statistic(cn_clear);
2181fc_host_fpin_statistic(cn_lost_credit);
2182fc_host_fpin_statistic(cn_credit_stall);
2183fc_host_fpin_statistic(cn_oversubscription);
2184fc_host_fpin_statistic(cn_device_specific);
2185fc_host_fpin_statistic(li);
2186fc_host_fpin_statistic(li_failure_unknown);
2187fc_host_fpin_statistic(li_link_failure_count);
2188fc_host_fpin_statistic(li_loss_of_sync_count);
2189fc_host_fpin_statistic(li_loss_of_signals_count);
2190fc_host_fpin_statistic(li_prim_seq_err_count);
2191fc_host_fpin_statistic(li_invalid_tx_word_count);
2192fc_host_fpin_statistic(li_invalid_crc_count);
2193fc_host_fpin_statistic(li_device_specific);
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01002196fc_reset_statistics(struct device *dev, struct device_attribute *attr,
2197 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Tony Jonesee959b02008-02-22 00:13:36 +01002199 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 struct fc_internal *i = to_fc_internal(shost->transportt);
2201
2202 /* ignore any data value written to the attribute */
2203 if (i->f->reset_fc_host_stats) {
2204 i->f->reset_fc_host_stats(shost);
2205 return count;
2206 }
2207
2208 return -ENOENT;
2209}
Tony Jonesee959b02008-02-22 00:13:36 +01002210static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 fc_reset_statistics);
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213static struct attribute *fc_statistics_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01002214 &device_attr_host_seconds_since_last_reset.attr,
2215 &device_attr_host_tx_frames.attr,
2216 &device_attr_host_tx_words.attr,
2217 &device_attr_host_rx_frames.attr,
2218 &device_attr_host_rx_words.attr,
2219 &device_attr_host_lip_count.attr,
2220 &device_attr_host_nos_count.attr,
2221 &device_attr_host_error_frames.attr,
2222 &device_attr_host_dumped_frames.attr,
2223 &device_attr_host_link_failure_count.attr,
2224 &device_attr_host_loss_of_sync_count.attr,
2225 &device_attr_host_loss_of_signal_count.attr,
2226 &device_attr_host_prim_seq_protocol_err_count.attr,
2227 &device_attr_host_invalid_tx_word_count.attr,
2228 &device_attr_host_invalid_crc_count.attr,
2229 &device_attr_host_fcp_input_requests.attr,
2230 &device_attr_host_fcp_output_requests.attr,
2231 &device_attr_host_fcp_control_requests.attr,
2232 &device_attr_host_fcp_input_megabytes.attr,
2233 &device_attr_host_fcp_output_megabytes.attr,
Vasu Deve58abb02012-05-25 10:26:38 -07002234 &device_attr_host_fcp_packet_alloc_failures.attr,
2235 &device_attr_host_fcp_packet_aborts.attr,
2236 &device_attr_host_fcp_frame_alloc_failures.attr,
2237 &device_attr_host_fc_no_free_exch.attr,
2238 &device_attr_host_fc_no_free_exch_xid.attr,
2239 &device_attr_host_fc_xid_not_found.attr,
2240 &device_attr_host_fc_xid_busy.attr,
2241 &device_attr_host_fc_seq_not_found.attr,
2242 &device_attr_host_fc_non_bls_resp.attr,
Shyam Sundar84610192020-10-21 02:27:14 -07002243 &device_attr_host_cn_sig_warn.attr,
2244 &device_attr_host_cn_sig_alarm.attr,
Tony Jonesee959b02008-02-22 00:13:36 +01002245 &device_attr_host_reset_statistics.attr,
Shyam Sundar547aab52020-10-21 02:27:12 -07002246 &device_attr_host_fpin_dn.attr,
2247 &device_attr_host_fpin_dn_unknown.attr,
2248 &device_attr_host_fpin_dn_timeout.attr,
2249 &device_attr_host_fpin_dn_unable_to_route.attr,
2250 &device_attr_host_fpin_dn_device_specific.attr,
2251 &device_attr_host_fpin_li.attr,
2252 &device_attr_host_fpin_li_failure_unknown.attr,
2253 &device_attr_host_fpin_li_link_failure_count.attr,
2254 &device_attr_host_fpin_li_loss_of_sync_count.attr,
2255 &device_attr_host_fpin_li_loss_of_signals_count.attr,
2256 &device_attr_host_fpin_li_prim_seq_err_count.attr,
2257 &device_attr_host_fpin_li_invalid_tx_word_count.attr,
2258 &device_attr_host_fpin_li_invalid_crc_count.attr,
2259 &device_attr_host_fpin_li_device_specific.attr,
2260 &device_attr_host_fpin_cn.attr,
2261 &device_attr_host_fpin_cn_clear.attr,
2262 &device_attr_host_fpin_cn_lost_credit.attr,
2263 &device_attr_host_fpin_cn_credit_stall.attr,
2264 &device_attr_host_fpin_cn_oversubscription.attr,
2265 &device_attr_host_fpin_cn_device_specific.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 NULL
2267};
2268
2269static struct attribute_group fc_statistics_group = {
2270 .name = "statistics",
2271 .attrs = fc_statistics_attrs,
2272};
2273
James Smarta53eb5e2007-04-27 12:41:09 -04002274
2275/* Host Vport Attributes */
2276
2277static int
2278fc_parse_wwn(const char *ns, u64 *nm)
2279{
2280 unsigned int i, j;
2281 u8 wwn[8];
2282
2283 memset(wwn, 0, sizeof(wwn));
2284
2285 /* Validate and store the new name */
2286 for (i=0, j=0; i < 16; i++) {
Andy Shevchenkoecc30992010-08-10 18:01:27 -07002287 int value;
2288
2289 value = hex_to_bin(*ns++);
2290 if (value >= 0)
2291 j = (j << 4) | value;
James Smarta53eb5e2007-04-27 12:41:09 -04002292 else
2293 return -EINVAL;
2294 if (i % 2) {
2295 wwn[i/2] = j & 0xff;
2296 j = 0;
2297 }
2298 }
2299
2300 *nm = wwn_to_u64(wwn);
2301
2302 return 0;
2303}
2304
2305
2306/*
2307 * "Short-cut" sysfs variable to create a new vport on a FC Host.
2308 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
2309 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
2310 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
2311 */
2312static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01002313store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
2314 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04002315{
Tony Jonesee959b02008-02-22 00:13:36 +01002316 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04002317 struct fc_vport_identifiers vid;
2318 struct fc_vport *vport;
2319 unsigned int cnt=count;
2320 int stat;
2321
2322 memset(&vid, 0, sizeof(vid));
2323
2324 /* count may include a LF at end of string */
2325 if (buf[cnt-1] == '\n')
2326 cnt--;
2327
2328 /* validate we have enough characters for WWPN */
2329 if ((cnt != (16+1+16)) || (buf[16] != ':'))
2330 return -EINVAL;
2331
2332 stat = fc_parse_wwn(&buf[0], &vid.port_name);
2333 if (stat)
2334 return stat;
2335
2336 stat = fc_parse_wwn(&buf[17], &vid.node_name);
2337 if (stat)
2338 return stat;
2339
2340 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
2341 vid.vport_type = FC_PORTTYPE_NPIV;
2342 /* vid.symbolic_name is already zero/NULL's */
2343 vid.disable = false; /* always enabled */
2344
2345 /* we only allow support on Channel 0 !!! */
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07002346 stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
James Smarta53eb5e2007-04-27 12:41:09 -04002347 return stat ? stat : count;
2348}
Tony Jonesee959b02008-02-22 00:13:36 +01002349static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04002350 store_fc_host_vport_create);
2351
2352
2353/*
2354 * "Short-cut" sysfs variable to delete a vport on a FC Host.
2355 * Vport is identified by a string containing "<WWPN>:<WWNN>".
2356 * The WWNs are specified as hex characters, and may *not* contain
2357 * any prefixes (e.g. 0x, x, etc)
2358 */
2359static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01002360store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
2361 const char *buf, size_t count)
James Smarta53eb5e2007-04-27 12:41:09 -04002362{
Tony Jonesee959b02008-02-22 00:13:36 +01002363 struct Scsi_Host *shost = transport_class_to_shost(dev);
James Smarta53eb5e2007-04-27 12:41:09 -04002364 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2365 struct fc_vport *vport;
2366 u64 wwpn, wwnn;
2367 unsigned long flags;
2368 unsigned int cnt=count;
2369 int stat, match;
2370
2371 /* count may include a LF at end of string */
2372 if (buf[cnt-1] == '\n')
2373 cnt--;
2374
2375 /* validate we have enough characters for WWPN */
2376 if ((cnt != (16+1+16)) || (buf[16] != ':'))
2377 return -EINVAL;
2378
2379 stat = fc_parse_wwn(&buf[0], &wwpn);
2380 if (stat)
2381 return stat;
2382
2383 stat = fc_parse_wwn(&buf[17], &wwnn);
2384 if (stat)
2385 return stat;
2386
2387 spin_lock_irqsave(shost->host_lock, flags);
2388 match = 0;
2389 /* we only allow support on Channel 0 !!! */
2390 list_for_each_entry(vport, &fc_host->vports, peers) {
2391 if ((vport->channel == 0) &&
2392 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
Gal Rosen0d9dc7c2010-01-21 10:15:32 +02002393 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
2394 break;
2395 vport->flags |= FC_VPORT_DELETING;
James Smarta53eb5e2007-04-27 12:41:09 -04002396 match = 1;
2397 break;
2398 }
2399 }
2400 spin_unlock_irqrestore(shost->host_lock, flags);
2401
2402 if (!match)
2403 return -ENODEV;
2404
2405 stat = fc_vport_terminate(vport);
2406 return stat ? stat : count;
2407}
Tony Jonesee959b02008-02-22 00:13:36 +01002408static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
James Smarta53eb5e2007-04-27 12:41:09 -04002409 store_fc_host_vport_delete);
2410
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412static int fc_host_match(struct attribute_container *cont,
2413 struct device *dev)
2414{
2415 struct Scsi_Host *shost;
2416 struct fc_internal *i;
2417
2418 if (!scsi_is_host_device(dev))
2419 return 0;
2420
2421 shost = dev_to_shost(dev);
2422 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2423 != &fc_host_class.class)
2424 return 0;
2425
2426 i = to_fc_internal(shost->transportt);
2427
2428 return &i->t.host_attrs.ac == cont;
2429}
2430
2431static int fc_target_match(struct attribute_container *cont,
2432 struct device *dev)
2433{
2434 struct Scsi_Host *shost;
2435 struct fc_internal *i;
2436
2437 if (!scsi_is_target_device(dev))
2438 return 0;
2439
2440 shost = dev_to_shost(dev->parent);
2441 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2442 != &fc_host_class.class)
2443 return 0;
2444
2445 i = to_fc_internal(shost->transportt);
2446
2447 return &i->t.target_attrs.ac == cont;
2448}
2449
2450static void fc_rport_dev_release(struct device *dev)
2451{
2452 struct fc_rport *rport = dev_to_rport(dev);
2453 put_device(dev->parent);
2454 kfree(rport);
2455}
2456
2457int scsi_is_fc_rport(const struct device *dev)
2458{
2459 return dev->release == fc_rport_dev_release;
2460}
2461EXPORT_SYMBOL(scsi_is_fc_rport);
2462
2463static int fc_rport_match(struct attribute_container *cont,
2464 struct device *dev)
2465{
2466 struct Scsi_Host *shost;
2467 struct fc_internal *i;
2468
2469 if (!scsi_is_fc_rport(dev))
2470 return 0;
2471
2472 shost = dev_to_shost(dev->parent);
2473 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2474 != &fc_host_class.class)
2475 return 0;
2476
2477 i = to_fc_internal(shost->transportt);
2478
2479 return &i->rport_attr_cont.ac == cont;
2480}
2481
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002482
James Smarta53eb5e2007-04-27 12:41:09 -04002483static void fc_vport_dev_release(struct device *dev)
2484{
2485 struct fc_vport *vport = dev_to_vport(dev);
2486 put_device(dev->parent); /* release kobj parent */
2487 kfree(vport);
2488}
2489
Bart Van Assche62055172016-03-28 14:37:28 -07002490static int scsi_is_fc_vport(const struct device *dev)
James Smarta53eb5e2007-04-27 12:41:09 -04002491{
2492 return dev->release == fc_vport_dev_release;
2493}
James Smarta53eb5e2007-04-27 12:41:09 -04002494
2495static int fc_vport_match(struct attribute_container *cont,
2496 struct device *dev)
2497{
2498 struct fc_vport *vport;
2499 struct Scsi_Host *shost;
2500 struct fc_internal *i;
2501
2502 if (!scsi_is_fc_vport(dev))
2503 return 0;
2504 vport = dev_to_vport(dev);
2505
2506 shost = vport_to_shost(vport);
2507 if (!shost->transportt || shost->transportt->host_attrs.ac.class
2508 != &fc_host_class.class)
2509 return 0;
2510
2511 i = to_fc_internal(shost->transportt);
2512 return &i->vport_attr_cont.ac == cont;
2513}
2514
2515
James Smartc829c392006-03-13 08:28:57 -05002516/**
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01002517 * fc_eh_timed_out - FC Transport I/O timeout intercept handler
James Smartc829c392006-03-13 08:28:57 -05002518 * @scmd: The SCSI command which timed out
2519 *
2520 * This routine protects against error handlers getting invoked while a
2521 * rport is in a blocked state, typically due to a temporarily loss of
2522 * connectivity. If the error handlers are allowed to proceed, requests
2523 * to abort i/o, reset the target, etc will likely fail as there is no way
2524 * to communicate with the device to perform the requested function. These
2525 * failures may result in the midlayer taking the device offline, requiring
2526 * manual intervention to restore operation.
2527 *
2528 * This routine, called whenever an i/o times out, validates the state of
2529 * the underlying rport. If the rport is blocked, it returns
2530 * EH_RESET_TIMER, which will continue to reschedule the timeout.
2531 * Eventually, either the device will return, or devloss_tmo will fire,
2532 * and when the timeout then fires, it will be handled normally.
2533 * If the rport is not blocked, normal error handling continues.
2534 *
2535 * Notes:
2536 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002537 */
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01002538enum blk_eh_timer_return
2539fc_eh_timed_out(struct scsi_cmnd *scmd)
James Smartc829c392006-03-13 08:28:57 -05002540{
2541 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
2542
2543 if (rport->port_state == FC_PORTSTATE_BLOCKED)
Jens Axboe242f9dc2008-09-14 05:55:09 -07002544 return BLK_EH_RESET_TIMER;
James Smartc829c392006-03-13 08:28:57 -05002545
Christoph Hellwig66005932018-05-29 15:52:29 +02002546 return BLK_EH_DONE;
James Smartc829c392006-03-13 08:28:57 -05002547}
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01002548EXPORT_SYMBOL(fc_eh_timed_out);
James Smartc829c392006-03-13 08:28:57 -05002549
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002550/*
James Smartbda23252008-04-24 12:12:46 -04002551 * Called by fc_user_scan to locate an rport on the shost that
2552 * matches the channel and target id, and invoke scsi_scan_target()
2553 * on the rport.
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002554 */
James Smartbda23252008-04-24 12:12:46 -04002555static void
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002556fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002557{
2558 struct fc_rport *rport;
James Smartbda23252008-04-24 12:12:46 -04002559 unsigned long flags;
2560
2561 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002562
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002563 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
2564 if (rport->scsi_target_id == -1)
2565 continue;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002566
Muneendra Kumar02c66322021-01-07 03:19:06 +05302567 if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
2568 (rport->port_state != FC_PORTSTATE_MARGINAL))
Hannes Reinecke0d2fcd92007-06-14 15:16:45 +02002569 continue;
2570
James Smartbda23252008-04-24 12:12:46 -04002571 if ((channel == rport->channel) &&
2572 (id == rport->scsi_target_id)) {
2573 spin_unlock_irqrestore(shost->host_lock, flags);
Hannes Reinecke1d645082016-03-17 08:39:45 +01002574 scsi_scan_target(&rport->dev, channel, id, lun,
2575 SCSI_SCAN_MANUAL);
James Smartbda23252008-04-24 12:12:46 -04002576 return;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002577 }
2578 }
2579
James Smartbda23252008-04-24 12:12:46 -04002580 spin_unlock_irqrestore(shost->host_lock, flags);
2581}
2582
2583/*
2584 * Called via sysfs scan routines. Necessary, as the FC transport
2585 * wants to place all target objects below the rport object. So this
2586 * routine must invoke the scsi_scan_target() routine with the rport
2587 * object as the parent.
2588 */
2589static int
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002590fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
James Smartbda23252008-04-24 12:12:46 -04002591{
2592 uint chlo, chhi;
2593 uint tgtlo, tgthi;
2594
2595 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2596 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2597 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2598 return -EINVAL;
2599
2600 if (channel == SCAN_WILD_CARD) {
2601 chlo = 0;
2602 chhi = shost->max_channel + 1;
2603 } else {
2604 chlo = channel;
2605 chhi = channel + 1;
2606 }
2607
2608 if (id == SCAN_WILD_CARD) {
2609 tgtlo = 0;
2610 tgthi = shost->max_id;
2611 } else {
2612 tgtlo = id;
2613 tgthi = id + 1;
2614 }
2615
2616 for ( ; chlo < chhi; chlo++)
2617 for ( ; tgtlo < tgthi; tgtlo++)
2618 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2619
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002620 return 0;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002621}
2622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623struct scsi_transport_template *
2624fc_attach_transport(struct fc_function_template *ft)
2625{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 int count;
Jes Sorensen24669f752006-01-16 10:31:18 -05002627 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2628 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 if (unlikely(!i))
2631 return NULL;
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2634 i->t.target_attrs.ac.class = &fc_transport_class.class;
2635 i->t.target_attrs.ac.match = fc_target_match;
2636 i->t.target_size = sizeof(struct fc_starget_attrs);
2637 transport_container_register(&i->t.target_attrs);
2638
2639 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2640 i->t.host_attrs.ac.class = &fc_host_class.class;
2641 i->t.host_attrs.ac.match = fc_host_match;
2642 i->t.host_size = sizeof(struct fc_host_attrs);
2643 if (ft->get_fc_host_stats)
2644 i->t.host_attrs.statistics = &fc_statistics_group;
2645 transport_container_register(&i->t.host_attrs);
2646
2647 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2648 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2649 i->rport_attr_cont.ac.match = fc_rport_match;
Shyam Sundar547aab52020-10-21 02:27:12 -07002650 i->rport_attr_cont.statistics = &fc_rport_statistics_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 transport_container_register(&i->rport_attr_cont);
2652
James Smarta53eb5e2007-04-27 12:41:09 -04002653 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2654 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2655 i->vport_attr_cont.ac.match = fc_vport_match;
2656 transport_container_register(&i->vport_attr_cont);
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 i->f = ft;
2659
2660 /* Transport uses the shost workq for scsi scanning */
2661 i->t.create_work_queue = 1;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04002662
Christoph Hellwige02f3f52006-01-13 19:04:00 +01002663 i->t.user_scan = fc_user_scan;
James Smart9ef3e4a2007-05-24 19:04:44 -04002664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 /*
2666 * Setup SCSI Target Attributes.
2667 */
2668 count = 0;
2669 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2670 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2671 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2672
2673 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2674
2675 i->starget_attrs[count] = NULL;
2676
2677
2678 /*
2679 * Setup SCSI Host Attributes.
2680 */
2681 count=0;
2682 SETUP_HOST_ATTRIBUTE_RD(node_name);
2683 SETUP_HOST_ATTRIBUTE_RD(port_name);
Andreas Herrmann6b7281d2006-01-13 02:16:54 +01002684 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2686 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2688 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
James Smarta53eb5e2007-04-27 12:41:09 -04002689 if (ft->vport_create) {
2690 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2691 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 SETUP_HOST_ATTRIBUTE_RD(serial_number);
Neerav Parikhbb8ef582012-01-22 17:29:50 -08002694 SETUP_HOST_ATTRIBUTE_RD(manufacturer);
2695 SETUP_HOST_ATTRIBUTE_RD(model);
2696 SETUP_HOST_ATTRIBUTE_RD(model_description);
2697 SETUP_HOST_ATTRIBUTE_RD(hardware_version);
2698 SETUP_HOST_ATTRIBUTE_RD(driver_version);
2699 SETUP_HOST_ATTRIBUTE_RD(firmware_version);
2700 SETUP_HOST_ATTRIBUTE_RD(optionrom_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 SETUP_HOST_ATTRIBUTE_RD(port_id);
2703 SETUP_HOST_ATTRIBUTE_RD(port_type);
2704 SETUP_HOST_ATTRIBUTE_RD(port_state);
2705 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2706 SETUP_HOST_ATTRIBUTE_RD(speed);
2707 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
James Smart016131b2006-08-14 08:20:25 -04002708 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
James Smartb8d08212006-08-17 08:00:43 -04002709 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711 /* Transport-managed attributes */
Mike Christie43ca9102010-09-15 16:52:32 -05002712 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(dev_loss_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002714 if (ft->issue_fc_host_lip)
2715 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
James Smarta53eb5e2007-04-27 12:41:09 -04002716 if (ft->vport_create)
2717 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2718 if (ft->vport_delete)
2719 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721 BUG_ON(count > FC_HOST_NUM_ATTRS);
2722
2723 i->host_attrs[count] = NULL;
2724
2725 /*
2726 * Setup Remote Port Attributes.
2727 */
2728 count=0;
2729 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2730 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2731 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2732 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2733 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2734 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2735 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
Muneendra Kumarafdd1122021-01-07 03:19:07 +05302736 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(port_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
Mike Christiefff9d402008-08-19 18:45:23 -05002738 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2741
2742 i->rport_attrs[count] = NULL;
2743
James Smarta53eb5e2007-04-27 12:41:09 -04002744 /*
2745 * Setup Virtual Port Attributes.
2746 */
2747 count=0;
2748 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2749 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2750 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2751 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2752 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2753 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2754 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2755 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2756 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2757
2758 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2759
2760 i->vport_attrs[count] = NULL;
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 return &i->t;
2763}
2764EXPORT_SYMBOL(fc_attach_transport);
2765
2766void fc_release_transport(struct scsi_transport_template *t)
2767{
2768 struct fc_internal *i = to_fc_internal(t);
2769
2770 transport_container_unregister(&i->t.target_attrs);
2771 transport_container_unregister(&i->t.host_attrs);
2772 transport_container_unregister(&i->rport_attr_cont);
James Smarta53eb5e2007-04-27 12:41:09 -04002773 transport_container_unregister(&i->vport_attr_cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 kfree(i);
2776}
2777EXPORT_SYMBOL(fc_release_transport);
2778
James Smartaedf3492006-04-10 10:14:05 -04002779/**
2780 * fc_queue_work - Queue work to the fc_host workqueue.
2781 * @shost: Pointer to Scsi_Host bound to fc_host.
2782 * @work: Work to queue for execution.
2783 *
2784 * Return value:
James Smarta0785ed2006-05-11 13:27:09 -04002785 * 1 - work queued for execution
2786 * 0 - work is already queued
2787 * -EINVAL - work queue doesn't exist
Rob Landleyeb448202007-11-03 13:30:39 -05002788 */
James Smartaedf3492006-04-10 10:14:05 -04002789static int
2790fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2791{
2792 if (unlikely(!fc_host_work_q(shost))) {
2793 printk(KERN_ERR
2794 "ERROR: FC host '%s' attempted to queue work, "
2795 "when no workqueue created.\n", shost->hostt->name);
2796 dump_stack();
2797
2798 return -EINVAL;
2799 }
2800
2801 return queue_work(fc_host_work_q(shost), work);
2802}
2803
2804/**
2805 * fc_flush_work - Flush a fc_host's workqueue.
2806 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002807 */
James Smartaedf3492006-04-10 10:14:05 -04002808static void
2809fc_flush_work(struct Scsi_Host *shost)
2810{
2811 if (!fc_host_work_q(shost)) {
2812 printk(KERN_ERR
2813 "ERROR: FC host '%s' attempted to flush work, "
2814 "when no workqueue created.\n", shost->hostt->name);
2815 dump_stack();
2816 return;
2817 }
2818
2819 flush_workqueue(fc_host_work_q(shost));
2820}
2821
2822/**
2823 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2824 * @shost: Pointer to Scsi_Host bound to fc_host.
2825 * @work: Work to queue for execution.
2826 * @delay: jiffies to delay the work queuing
2827 *
2828 * Return value:
James Smart0f29b962006-08-18 17:33:29 -04002829 * 1 on success / 0 already queued / < 0 for error
Rob Landleyeb448202007-11-03 13:30:39 -05002830 */
James Smartaedf3492006-04-10 10:14:05 -04002831static int
David Howellsc4028952006-11-22 14:57:56 +00002832fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
James Smartaedf3492006-04-10 10:14:05 -04002833 unsigned long delay)
2834{
2835 if (unlikely(!fc_host_devloss_work_q(shost))) {
2836 printk(KERN_ERR
2837 "ERROR: FC host '%s' attempted to queue work, "
2838 "when no workqueue created.\n", shost->hostt->name);
2839 dump_stack();
2840
2841 return -EINVAL;
2842 }
2843
2844 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2845}
2846
2847/**
2848 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2849 * @shost: Pointer to Scsi_Host bound to fc_host.
Rob Landleyeb448202007-11-03 13:30:39 -05002850 */
James Smartaedf3492006-04-10 10:14:05 -04002851static void
2852fc_flush_devloss(struct Scsi_Host *shost)
2853{
2854 if (!fc_host_devloss_work_q(shost)) {
2855 printk(KERN_ERR
2856 "ERROR: FC host '%s' attempted to flush work, "
2857 "when no workqueue created.\n", shost->hostt->name);
2858 dump_stack();
2859 return;
2860 }
2861
2862 flush_workqueue(fc_host_devloss_work_q(shost));
2863}
2864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866/**
Rob Landleyeb448202007-11-03 13:30:39 -05002867 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2868 * @shost: Which &Scsi_Host
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002870 * This routine is expected to be called immediately preceding the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 * a driver's call to scsi_remove_host().
2872 *
2873 * WARNING: A driver utilizing the fc_transport, which fails to call
Rob Landleyeb448202007-11-03 13:30:39 -05002874 * this routine prior to scsi_remove_host(), will leave dangling
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 * objects in /sys/class/fc_remote_ports. Access to any of these
2876 * objects can result in a system crash !!!
2877 *
2878 * Notes:
2879 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05002880 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881void
2882fc_remove_host(struct Scsi_Host *shost)
2883{
James Smarta53eb5e2007-04-27 12:41:09 -04002884 struct fc_vport *vport = NULL, *next_vport = NULL;
2885 struct fc_rport *rport = NULL, *next_rport = NULL;
James Smartaedf3492006-04-10 10:14:05 -04002886 struct workqueue_struct *work_q;
2887 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James Smarta53eb5e2007-04-27 12:41:09 -04002888 unsigned long flags;
James Smarta53eb5e2007-04-27 12:41:09 -04002889
2890 spin_lock_irqsave(shost->host_lock, flags);
2891
2892 /* Remove any vports */
Hannes Reinecke260f4ae2017-07-24 12:09:36 +02002893 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2894 vport->flags |= FC_VPORT_DELETING;
James Smart9ef3e4a2007-05-24 19:04:44 -04002895 fc_queue_work(shost, &vport->vport_delete_work);
Hannes Reinecke260f4ae2017-07-24 12:09:36 +02002896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
2898 /* Remove any remote ports */
2899 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002900 &fc_host->rports, peers) {
2901 list_del(&rport->peers);
2902 rport->port_state = FC_PORTSTATE_DELETED;
2903 fc_queue_work(shost, &rport->rport_delete_work);
2904 }
2905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 list_for_each_entry_safe(rport, next_rport,
James Smartaedf3492006-04-10 10:14:05 -04002907 &fc_host->rport_bindings, peers) {
2908 list_del(&rport->peers);
2909 rport->port_state = FC_PORTSTATE_DELETED;
2910 fc_queue_work(shost, &rport->rport_delete_work);
2911 }
2912
James Smarta53eb5e2007-04-27 12:41:09 -04002913 spin_unlock_irqrestore(shost->host_lock, flags);
2914
James Smartaedf3492006-04-10 10:14:05 -04002915 /* flush all scan work items */
2916 scsi_flush_work(shost);
2917
2918 /* flush all stgt delete, and rport delete work items, then kill it */
2919 if (fc_host->work_q) {
2920 work_q = fc_host->work_q;
2921 fc_host->work_q = NULL;
2922 destroy_workqueue(work_q);
2923 }
2924
2925 /* flush all devloss work items, then kill it */
2926 if (fc_host->devloss_work_q) {
2927 work_q = fc_host->devloss_work_q;
2928 fc_host->devloss_work_q = NULL;
2929 destroy_workqueue(work_q);
2930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932EXPORT_SYMBOL(fc_remove_host);
2933
Mike Christiefff9d402008-08-19 18:45:23 -05002934static void fc_terminate_rport_io(struct fc_rport *rport)
2935{
2936 struct Scsi_Host *shost = rport_to_shost(rport);
2937 struct fc_internal *i = to_fc_internal(shost->transportt);
2938
2939 /* Involve the LLDD if possible to terminate all io on the rport. */
2940 if (i->f->terminate_rport_io)
2941 i->f->terminate_rport_io(rport);
2942
2943 /*
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002944 * Must unblock to flush queued IO. scsi-ml will fail incoming reqs.
Mike Christiefff9d402008-08-19 18:45:23 -05002945 */
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002946 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
Mike Christiefff9d402008-08-19 18:45:23 -05002947}
James Smartaedf3492006-04-10 10:14:05 -04002948
2949/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002950 * fc_starget_delete - called to delete the scsi descendants of an rport
David Howellsc4028952006-11-22 14:57:56 +00002951 * @work: remote port to be operated on.
Rob Landleyeb448202007-11-03 13:30:39 -05002952 *
2953 * Deletes target and all sdevs.
2954 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002955static void
David Howellsc4028952006-11-22 14:57:56 +00002956fc_starget_delete(struct work_struct *work)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002957{
David Howellsc4028952006-11-22 14:57:56 +00002958 struct fc_rport *rport =
2959 container_of(work, struct fc_rport, stgt_delete_work);
James Smart0f29b962006-08-18 17:33:29 -04002960
Mike Christiefff9d402008-08-19 18:45:23 -05002961 fc_terminate_rport_io(rport);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002962 scsi_remove_target(&rport->dev);
2963}
2964
James Smartaedf3492006-04-10 10:14:05 -04002965
2966/**
2967 * fc_rport_final_delete - finish rport termination and delete it.
David Howellsc4028952006-11-22 14:57:56 +00002968 * @work: remote port to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05002969 */
James Smartaedf3492006-04-10 10:14:05 -04002970static void
David Howellsc4028952006-11-22 14:57:56 +00002971fc_rport_final_delete(struct work_struct *work)
James Smartaedf3492006-04-10 10:14:05 -04002972{
David Howellsc4028952006-11-22 14:57:56 +00002973 struct fc_rport *rport =
2974 container_of(work, struct fc_rport, rport_delete_work);
James Smartaedf3492006-04-10 10:14:05 -04002975 struct device *dev = &rport->dev;
2976 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart0f29b962006-08-18 17:33:29 -04002977 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smart92740b22007-04-27 11:53:17 -04002978 unsigned long flags;
Michael Reed8798a692009-10-09 14:15:59 -05002979 int do_callback = 0;
James Smartaedf3492006-04-10 10:14:05 -04002980
Nithin Nayak Sujir112f6612011-04-25 12:30:06 -07002981 fc_terminate_rport_io(rport);
2982
James Smartaedf3492006-04-10 10:14:05 -04002983 /*
James Smart9ef3e4a2007-05-24 19:04:44 -04002984 * if a scan is pending, flush the SCSI Host work_q so that
James Smartaedf3492006-04-10 10:14:05 -04002985 * that we can reclaim the rport scan work element.
2986 */
2987 if (rport->flags & FC_RPORT_SCAN_PENDING)
2988 scsi_flush_work(shost);
2989
James Smart92740b22007-04-27 11:53:17 -04002990 /*
2991 * Cancel any outstanding timers. These should really exist
2992 * only when rmmod'ing the LLDD and we're asking for
2993 * immediate termination of the rports
2994 */
2995 spin_lock_irqsave(shost->host_lock, flags);
2996 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2997 spin_unlock_irqrestore(shost->host_lock, flags);
2998 if (!cancel_delayed_work(&rport->fail_io_work))
2999 fc_flush_devloss(shost);
3000 if (!cancel_delayed_work(&rport->dev_loss_work))
3001 fc_flush_devloss(shost);
Neil Horman0353e082014-06-23 10:40:25 -04003002 cancel_work_sync(&rport->scan_work);
James Smart92740b22007-04-27 11:53:17 -04003003 spin_lock_irqsave(shost->host_lock, flags);
3004 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3005 }
3006 spin_unlock_irqrestore(shost->host_lock, flags);
3007
James Smart0f29b962006-08-18 17:33:29 -04003008 /* Delete SCSI target and sdevs */
3009 if (rport->scsi_target_id != -1)
David Howellsc4028952006-11-22 14:57:56 +00003010 fc_starget_delete(&rport->stgt_delete_work);
James Smart92740b22007-04-27 11:53:17 -04003011
3012 /*
3013 * Notify the driver that the rport is now dead. The LLDD will
3014 * also guarantee that any communication to the rport is terminated
James Smart4be98c02009-01-05 12:14:18 -05003015 *
3016 * Avoid this call if we already called it when we preserved the
3017 * rport for the binding.
James Smart92740b22007-04-27 11:53:17 -04003018 */
Michael Reed8798a692009-10-09 14:15:59 -05003019 spin_lock_irqsave(shost->host_lock, flags);
James Smart4be98c02009-01-05 12:14:18 -05003020 if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
Michael Reed8798a692009-10-09 14:15:59 -05003021 (i->f->dev_loss_tmo_callbk)) {
3022 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3023 do_callback = 1;
3024 }
3025 spin_unlock_irqrestore(shost->host_lock, flags);
3026
3027 if (do_callback)
James Smart0f29b962006-08-18 17:33:29 -04003028 i->f->dev_loss_tmo_callbk(rport);
James Smart0f29b962006-08-18 17:33:29 -04003029
James Smart9e4f5e22009-03-26 13:33:19 -04003030 fc_bsg_remove(rport->rqst_q);
3031
James Smartaedf3492006-04-10 10:14:05 -04003032 transport_remove_device(dev);
3033 device_del(dev);
3034 transport_destroy_device(dev);
Bart Van Assche4cd38e32015-11-20 13:33:04 -08003035 scsi_host_put(shost); /* for fc_host->rport list */
James Smart3bdad7b2006-06-26 14:19:59 -04003036 put_device(dev); /* for self-reference */
James Smartaedf3492006-04-10 10:14:05 -04003037}
3038
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040/**
Hannes Reinecke558b88f2016-10-18 10:01:41 +02003041 * fc_remote_port_create - allocates and creates a remote FC port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 * @shost: scsi host the remote port is connected to.
3043 * @channel: Channel on shost port connected to.
3044 * @ids: The world wide names, fc address, and FC4 port
3045 * roles for the remote port.
3046 *
3047 * Allocates and creates the remoter port structure, including the
3048 * class and sysfs creation.
3049 *
3050 * Notes:
3051 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003052 */
Adrian Bunk44818ef2007-07-09 11:59:59 -07003053static struct fc_rport *
Hannes Reinecke558b88f2016-10-18 10:01:41 +02003054fc_remote_port_create(struct Scsi_Host *shost, int channel,
3055 struct fc_rport_identifiers *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
James Smartaedf3492006-04-10 10:14:05 -04003057 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 struct fc_internal *fci = to_fc_internal(shost->transportt);
3059 struct fc_rport *rport;
3060 struct device *dev;
3061 unsigned long flags;
3062 int error;
3063 size_t size;
3064
3065 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
Jes Sorensen24669f752006-01-16 10:31:18 -05003066 rport = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 if (unlikely(!rport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003068 printk(KERN_ERR "%s: allocation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 return NULL;
3070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072 rport->maxframe_size = -1;
3073 rport->supported_classes = FC_COS_UNSPECIFIED;
Mike Christie43ca9102010-09-15 16:52:32 -05003074 rport->dev_loss_tmo = fc_host->dev_loss_tmo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
3076 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
3077 rport->port_id = ids->port_id;
3078 rport->roles = ids->roles;
3079 rport->port_state = FC_PORTSTATE_ONLINE;
3080 if (fci->f->dd_fcrport_size)
3081 rport->dd_data = &rport[1];
3082 rport->channel = channel;
James Smart0f29b962006-08-18 17:33:29 -04003083 rport->fast_io_fail_tmo = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
David Howellsc4028952006-11-22 14:57:56 +00003085 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
3086 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
3087 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
3088 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
3089 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 spin_lock_irqsave(shost->host_lock, flags);
3092
3093 rport->number = fc_host->next_rport_number++;
Cathy Avery0c3ae262017-04-17 14:37:45 -04003094 if ((rport->roles & FC_PORT_ROLE_FCP_TARGET) ||
3095 (rport->roles & FC_PORT_ROLE_FCP_DUMMY_INITIATOR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 rport->scsi_target_id = fc_host->next_target_id++;
3097 else
3098 rport->scsi_target_id = -1;
James Smartaedf3492006-04-10 10:14:05 -04003099 list_add_tail(&rport->peers, &fc_host->rports);
Bart Van Assche4cd38e32015-11-20 13:33:04 -08003100 scsi_host_get(shost); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102 spin_unlock_irqrestore(shost->host_lock, flags);
3103
3104 dev = &rport->dev;
James Smart3bdad7b2006-06-26 14:19:59 -04003105 device_initialize(dev); /* takes self reference */
3106 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 dev->release = fc_rport_dev_release;
Kay Sievers71610f52008-12-03 22:41:36 +01003108 dev_set_name(dev, "rport-%d:%d-%d",
3109 shost->host_no, channel, rport->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 transport_setup_device(dev);
3111
3112 error = device_add(dev);
3113 if (error) {
3114 printk(KERN_ERR "FC Remote Port device_add failed\n");
3115 goto delete_rport;
3116 }
3117 transport_add_device(dev);
3118 transport_configure_device(dev);
3119
James Smart9e4f5e22009-03-26 13:33:19 -04003120 fc_bsg_rportadd(shost, rport);
3121 /* ignore any bsg add error - we just can't do sgio */
3122
James Smarta53eb5e2007-04-27 12:41:09 -04003123 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04003125 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04003127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 return rport;
3130
3131delete_rport:
3132 transport_destroy_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 spin_lock_irqsave(shost->host_lock, flags);
3134 list_del(&rport->peers);
Bart Van Assche4cd38e32015-11-20 13:33:04 -08003135 scsi_host_put(shost); /* for fc_host->rport list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 spin_unlock_irqrestore(shost->host_lock, flags);
3137 put_device(dev->parent);
3138 kfree(rport);
3139 return NULL;
3140}
3141
3142/**
Rob Landleyeb448202007-11-03 13:30:39 -05003143 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 * @shost: scsi host the remote port is connected to.
3145 * @channel: Channel on shost port connected to.
3146 * @ids: The world wide names, fc address, and FC4 port
3147 * roles for the remote port.
3148 *
3149 * The LLDD calls this routine to notify the transport of the existence
3150 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
3151 * of the port, it's FC address (port_id), and the FC4 roles that are
3152 * active for the port.
3153 *
3154 * For ports that are FCP targets (aka scsi targets), the FC transport
3155 * maintains consistent target id bindings on behalf of the LLDD.
3156 * A consistent target id binding is an assignment of a target id to
3157 * a remote port identifier, which persists while the scsi host is
3158 * attached. The remote port can disappear, then later reappear, and
3159 * it's target id assignment remains the same. This allows for shifts
3160 * in FC addressing (if binding by wwpn or wwnn) with no apparent
3161 * changes to the scsi subsystem which is based on scsi host number and
3162 * target id values. Bindings are only valid during the attachment of
3163 * the scsi host. If the host detaches, then later re-attaches, target
3164 * id bindings may change.
3165 *
3166 * This routine is responsible for returning a remote port structure.
3167 * The routine will search the list of remote ports it maintains
3168 * internally on behalf of consistent target id mappings. If found, the
3169 * remote port structure will be reused. Otherwise, a new remote port
3170 * structure will be allocated.
3171 *
3172 * Whenever a remote port is allocated, a new fc_remote_port class
3173 * device is created.
3174 *
3175 * Should not be called from interrupt context.
3176 *
3177 * Notes:
3178 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003179 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180struct fc_rport *
3181fc_remote_port_add(struct Scsi_Host *shost, int channel,
3182 struct fc_rport_identifiers *ids)
3183{
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003184 struct fc_internal *fci = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04003185 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 struct fc_rport *rport;
3187 unsigned long flags;
3188 int match = 0;
3189
James Smartaedf3492006-04-10 10:14:05 -04003190 /* ensure any stgt delete functions are done */
3191 fc_flush_work(shost);
3192
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003193 /*
3194 * Search the list of "active" rports, for an rport that has been
3195 * deleted, but we've held off the real delete while the target
3196 * is in a "blocked" state.
3197 */
3198 spin_lock_irqsave(shost->host_lock, flags);
3199
James Smartaedf3492006-04-10 10:14:05 -04003200 list_for_each_entry(rport, &fc_host->rports, peers) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003201
Hannes Reineckef091fb82017-09-25 13:47:23 +02003202 if ((rport->port_state == FC_PORTSTATE_BLOCKED ||
3203 rport->port_state == FC_PORTSTATE_NOTPRESENT) &&
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003204 (rport->channel == channel)) {
3205
James Smartaedf3492006-04-10 10:14:05 -04003206 switch (fc_host->tgtid_bind_type) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003207 case FC_TGTID_BIND_BY_WWPN:
3208 case FC_TGTID_BIND_NONE:
3209 if (rport->port_name == ids->port_name)
3210 match = 1;
3211 break;
3212 case FC_TGTID_BIND_BY_WWNN:
3213 if (rport->node_name == ids->node_name)
3214 match = 1;
3215 break;
3216 case FC_TGTID_BIND_BY_ID:
3217 if (rport->port_id == ids->port_id)
3218 match = 1;
3219 break;
3220 }
3221
3222 if (match) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003223
3224 memcpy(&rport->node_name, &ids->node_name,
3225 sizeof(rport->node_name));
3226 memcpy(&rport->port_name, &ids->port_name,
3227 sizeof(rport->port_name));
3228 rport->port_id = ids->port_id;
3229
3230 rport->port_state = FC_PORTSTATE_ONLINE;
3231 rport->roles = ids->roles;
3232
3233 spin_unlock_irqrestore(shost->host_lock, flags);
3234
3235 if (fci->f->dd_fcrport_size)
3236 memset(rport->dd_data, 0,
3237 fci->f->dd_fcrport_size);
3238
3239 /*
James Smart92740b22007-04-27 11:53:17 -04003240 * If we were not a target, cancel the
3241 * io terminate and rport timers, and
3242 * we're done.
3243 *
3244 * If we were a target, but our new role
3245 * doesn't indicate a target, leave the
3246 * timers running expecting the role to
3247 * change as the target fully logs in. If
3248 * it doesn't, the target will be torn down.
3249 *
3250 * If we were a target, and our role shows
3251 * we're still a target, cancel the timers
3252 * and kick off a scan.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003253 */
James Smart92740b22007-04-27 11:53:17 -04003254
3255 /* was a target, not in roles */
3256 if ((rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04003257 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003258 return rport;
3259
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003260 /*
James Smart92740b22007-04-27 11:53:17 -04003261 * Stop the fail io and dev_loss timers.
3262 * If they flush, the port_state will
3263 * be checked and will NOOP the function.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003264 */
James Smart0f29b962006-08-18 17:33:29 -04003265 if (!cancel_delayed_work(&rport->fail_io_work))
3266 fc_flush_devloss(shost);
James Smart92740b22007-04-27 11:53:17 -04003267 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04003268 fc_flush_devloss(shost);
3269
3270 spin_lock_irqsave(shost->host_lock, flags);
3271
Mike Christiefff9d402008-08-19 18:45:23 -05003272 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
James Smart4be98c02009-01-05 12:14:18 -05003273 FC_RPORT_DEVLOSS_PENDING |
3274 FC_RPORT_DEVLOSS_CALLBK_DONE);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003275
Mike Christie8fb2ef82012-04-15 21:47:50 -05003276 spin_unlock_irqrestore(shost->host_lock, flags);
3277
James Smart92740b22007-04-27 11:53:17 -04003278 /* if target, initiate a scan */
3279 if (rport->scsi_target_id != -1) {
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003280 scsi_target_unblock(&rport->dev,
3281 SDEV_RUNNING);
Mike Christie8fb2ef82012-04-15 21:47:50 -05003282 spin_lock_irqsave(shost->host_lock,
3283 flags);
James Smart92740b22007-04-27 11:53:17 -04003284 rport->flags |= FC_RPORT_SCAN_PENDING;
3285 scsi_queue_work(shost,
3286 &rport->scan_work);
3287 spin_unlock_irqrestore(shost->host_lock,
3288 flags);
Mike Christie8fb2ef82012-04-15 21:47:50 -05003289 }
James Smarta0785ed2006-05-11 13:27:09 -04003290
James Smart9e4f5e22009-03-26 13:33:19 -04003291 fc_bsg_goose_queue(rport);
3292
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003293 return rport;
3294 }
3295 }
3296 }
3297
James Smart92740b22007-04-27 11:53:17 -04003298 /*
3299 * Search the bindings array
3300 * Note: if never a FCP target, you won't be on this list
3301 */
James Smartaedf3492006-04-10 10:14:05 -04003302 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
3304 /* search for a matching consistent binding */
3305
James Smartaedf3492006-04-10 10:14:05 -04003306 list_for_each_entry(rport, &fc_host->rport_bindings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 peers) {
3308 if (rport->channel != channel)
3309 continue;
3310
James Smartaedf3492006-04-10 10:14:05 -04003311 switch (fc_host->tgtid_bind_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 case FC_TGTID_BIND_BY_WWPN:
3313 if (rport->port_name == ids->port_name)
3314 match = 1;
3315 break;
3316 case FC_TGTID_BIND_BY_WWNN:
3317 if (rport->node_name == ids->node_name)
3318 match = 1;
3319 break;
3320 case FC_TGTID_BIND_BY_ID:
3321 if (rport->port_id == ids->port_id)
3322 match = 1;
3323 break;
3324 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3325 break;
3326 }
3327
3328 if (match) {
James Smartaedf3492006-04-10 10:14:05 -04003329 list_move_tail(&rport->peers, &fc_host->rports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 break;
3331 }
3332 }
3333
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 if (match) {
3335 memcpy(&rport->node_name, &ids->node_name,
3336 sizeof(rport->node_name));
3337 memcpy(&rport->port_name, &ids->port_name,
3338 sizeof(rport->port_name));
3339 rport->port_id = ids->port_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 rport->port_state = FC_PORTSTATE_ONLINE;
Mike Christiefff9d402008-08-19 18:45:23 -05003341 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003343 if (fci->f->dd_fcrport_size)
3344 memset(rport->dd_data, 0,
3345 fci->f->dd_fcrport_size);
Mike Christie8fb2ef82012-04-15 21:47:50 -05003346 spin_unlock_irqrestore(shost->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003347
Hannes Reinecke675195d2017-09-20 08:58:53 +02003348 fc_remote_port_rolechg(rport, ids->roles);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 return rport;
3350 }
3351 }
3352
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003353 spin_unlock_irqrestore(shost->host_lock, flags);
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 /* No consistent binding found - create new remote port entry */
Hannes Reinecke558b88f2016-10-18 10:01:41 +02003356 rport = fc_remote_port_create(shost, channel, ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
3358 return rport;
3359}
3360EXPORT_SYMBOL(fc_remote_port_add);
3361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363/**
Rob Landleyeb448202007-11-03 13:30:39 -05003364 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 * @rport: The remote port that no longer exists
3366 *
3367 * The LLDD calls this routine to notify the transport that a remote
3368 * port is no longer part of the topology. Note: Although a port
3369 * may no longer be part of the topology, it may persist in the remote
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003370 * ports displayed by the fc_host. We do this under 2 conditions:
Mauro Carvalho Chehab739aca02017-05-12 10:04:14 -03003371 *
Rob Landleyeb448202007-11-03 13:30:39 -05003372 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
Mauro Carvalho Chehab739aca02017-05-12 10:04:14 -03003373 * This allows the port to temporarily disappear, then reappear without
3374 * disrupting the SCSI device tree attached to it. During the "blocked"
3375 * period the port will still exist.
3376 *
Rob Landleyeb448202007-11-03 13:30:39 -05003377 * 2) If the port was a scsi target and disappears for longer than we
Mauro Carvalho Chehab739aca02017-05-12 10:04:14 -03003378 * expect, we'll delete the port and the tear down the SCSI device tree
3379 * attached to it. However, we want to semi-persist the target id assigned
3380 * to that port if it eventually does exist. The port structure will
3381 * remain (although with minimal information) so that the target id
Linus Torvalds90311142017-07-06 12:10:33 -07003382 * bindings also remain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 *
3384 * If the remote port is not an FCP Target, it will be fully torn down
3385 * and deallocated, including the fc_remote_port class device.
3386 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003387 * If the remote port is an FCP Target, the port will be placed in a
3388 * temporary blocked state. From the LLDD's perspective, the rport no
3389 * longer exists. From the SCSI midlayer's perspective, the SCSI target
3390 * exists, but all sdevs on it are blocked from further I/O. The following
Rob Landleyeb448202007-11-03 13:30:39 -05003391 * is then expected.
3392 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003393 * If the remote port does not return (signaled by a LLDD call to
3394 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
3395 * scsi target is removed - killing all outstanding i/o and removing the
Tyrel Datwyler134699d2017-06-21 16:24:39 -04003396 * scsi devices attached to it. The port structure will be marked Not
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003397 * Present and be partially cleared, leaving only enough information to
3398 * recognize the remote port relative to the scsi target id binding if
3399 * it later appears. The port will remain as long as there is a valid
3400 * binding (e.g. until the user changes the binding type or unloads the
3401 * scsi host with the binding).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 *
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003403 * If the remote port returns within the dev_loss_tmo value (and matches
3404 * according to the target id binding type), the port structure will be
3405 * reused. If it is no longer a SCSI target, the target will be torn
3406 * down. If it continues to be a SCSI target, then the target will be
3407 * unblocked (allowing i/o to be resumed), and a scan will be activated
3408 * to ensure that all luns are detected.
3409 *
3410 * Called from normal process context only - cannot be called from interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 *
3412 * Notes:
3413 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415void
3416fc_remote_port_delete(struct fc_rport *rport)
3417{
James Smartaedf3492006-04-10 10:14:05 -04003418 struct Scsi_Host *shost = rport_to_shost(rport);
Hannes Reinecke36dd2882010-03-09 10:18:48 +01003419 unsigned long timeout = rport->dev_loss_tmo;
James Smartaedf3492006-04-10 10:14:05 -04003420 unsigned long flags;
3421
3422 /*
3423 * No need to flush the fc_host work_q's, as all adds are synchronous.
3424 *
3425 * We do need to reclaim the rport scan work element, so eventually
3426 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
3427 * there's still a scan pending.
3428 */
3429
3430 spin_lock_irqsave(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
Muneendra Kumar02c66322021-01-07 03:19:06 +05303432 if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
3433 (rport->port_state != FC_PORTSTATE_MARGINAL)) {
James Smartaedf3492006-04-10 10:14:05 -04003434 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return;
3436 }
3437
James Smart92740b22007-04-27 11:53:17 -04003438 /*
3439 * In the past, we if this was not an FCP-Target, we would
3440 * unconditionally just jump to deleting the rport.
3441 * However, rports can be used as node containers by the LLDD,
3442 * and its not appropriate to just terminate the rport at the
3443 * first sign of a loss in connectivity. The LLDD may want to
3444 * send ELS traffic to re-validate the login. If the rport is
3445 * immediately deleted, it makes it inappropriate for a node
3446 * container.
3447 * So... we now unconditionally wait dev_loss_tmo before
3448 * destroying an rport.
3449 */
3450
James Smartaedf3492006-04-10 10:14:05 -04003451 rport->port_state = FC_PORTSTATE_BLOCKED;
3452
3453 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
3454
3455 spin_unlock_irqrestore(shost->host_lock, flags);
3456
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003457 scsi_target_block(&rport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
James Smart0f29b962006-08-18 17:33:29 -04003459 /* see if we need to kill io faster than waiting for device loss */
3460 if ((rport->fast_io_fail_tmo != -1) &&
Mike Christiefff9d402008-08-19 18:45:23 -05003461 (rport->fast_io_fail_tmo < timeout))
James Smart0f29b962006-08-18 17:33:29 -04003462 fc_queue_devloss_work(shost, &rport->fail_io_work,
3463 rport->fast_io_fail_tmo * HZ);
3464
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003465 /* cap the length the devices can be blocked until they are deleted */
James Smartaedf3492006-04-10 10:14:05 -04003466 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467}
3468EXPORT_SYMBOL(fc_remote_port_delete);
3469
3470/**
Rob Landleyeb448202007-11-03 13:30:39 -05003471 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 * @rport: The remote port that changed.
Rob Landleyeb448202007-11-03 13:30:39 -05003473 * @roles: New roles for this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 *
Rob Landleyeb448202007-11-03 13:30:39 -05003475 * Description: The LLDD calls this routine to notify the transport that the
3476 * roles on a remote port may have changed. The largest effect of this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 * if a port now becomes a FCP Target, it must be allocated a
3478 * scsi target id. If the port is no longer a FCP target, any
3479 * scsi target id value assigned to it will persist in case the
3480 * role changes back to include FCP Target. No changes in the scsi
3481 * midlayer will be invoked if the role changes (in the expectation
3482 * that the role will be resumed. If it doesn't normal error processing
3483 * will take place).
3484 *
3485 * Should not be called from interrupt context.
3486 *
3487 * Notes:
3488 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003489 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490void
3491fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
3492{
3493 struct Scsi_Host *shost = rport_to_shost(rport);
James Smartaedf3492006-04-10 10:14:05 -04003494 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 unsigned long flags;
3496 int create = 0;
3497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 spin_lock_irqsave(shost->host_lock, flags);
James Smarta53eb5e2007-04-27 12:41:09 -04003499 if (roles & FC_PORT_ROLE_FCP_TARGET) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003500 if (rport->scsi_target_id == -1) {
3501 rport->scsi_target_id = fc_host->next_target_id++;
3502 create = 1;
James Smarta53eb5e2007-04-27 12:41:09 -04003503 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003504 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003507 rport->roles = roles;
3508
James Smartaedf3492006-04-10 10:14:05 -04003509 spin_unlock_irqrestore(shost->host_lock, flags);
3510
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003511 if (create) {
3512 /*
3513 * There may have been a delete timer running on the
3514 * port. Ensure that it is cancelled as we now know
3515 * the port is an FCP Target.
Tyrel Datwyler134699d2017-06-21 16:24:39 -04003516 * Note: we know the rport exists and is in an online
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003517 * state as the LLDD would not have had an rport
3518 * reference to pass us.
3519 *
3520 * Take no action on the del_timer failure as the state
3521 * machine state change will validate the
3522 * transaction.
3523 */
James Smart0f29b962006-08-18 17:33:29 -04003524 if (!cancel_delayed_work(&rport->fail_io_work))
3525 fc_flush_devloss(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003526 if (!cancel_delayed_work(&rport->dev_loss_work))
James Smartaedf3492006-04-10 10:14:05 -04003527 fc_flush_devloss(shost);
3528
3529 spin_lock_irqsave(shost->host_lock, flags);
Mike Christiefff9d402008-08-19 18:45:23 -05003530 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
James Smart0eecee42011-11-03 11:10:40 -04003531 FC_RPORT_DEVLOSS_PENDING |
3532 FC_RPORT_DEVLOSS_CALLBK_DONE);
James Smartaedf3492006-04-10 10:14:05 -04003533 spin_unlock_irqrestore(shost->host_lock, flags);
3534
3535 /* ensure any stgt delete functions are done */
3536 fc_flush_work(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003537
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003538 scsi_target_unblock(&rport->dev, SDEV_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 /* initiate a scan of the target */
James Smartaedf3492006-04-10 10:14:05 -04003540 spin_lock_irqsave(shost->host_lock, flags);
3541 rport->flags |= FC_RPORT_SCAN_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 scsi_queue_work(shost, &rport->scan_work);
James Smartaedf3492006-04-10 10:14:05 -04003543 spin_unlock_irqrestore(shost->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546EXPORT_SYMBOL(fc_remote_port_rolechg);
3547
3548/**
Rob Landleyeb448202007-11-03 13:30:39 -05003549 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
James Smart92740b22007-04-27 11:53:17 -04003550 * @work: rport target that failed to reappear in the allotted time.
Rob Landleyeb448202007-11-03 13:30:39 -05003551 *
3552 * Description: An attempt to delete a remote port blocks, and if it fails
3553 * to return in the allotted time this gets called.
3554 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555static void
David Howellsc4028952006-11-22 14:57:56 +00003556fc_timeout_deleted_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557{
David Howellsc4028952006-11-22 14:57:56 +00003558 struct fc_rport *rport =
3559 container_of(work, struct fc_rport, dev_loss_work.work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003560 struct Scsi_Host *shost = rport_to_shost(rport);
James Smart4be98c02009-01-05 12:14:18 -05003561 struct fc_internal *i = to_fc_internal(shost->transportt);
James Smartaedf3492006-04-10 10:14:05 -04003562 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003563 unsigned long flags;
Michael Reed8798a692009-10-09 14:15:59 -05003564 int do_callback = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003566 spin_lock_irqsave(shost->host_lock, flags);
3567
James Smartaedf3492006-04-10 10:14:05 -04003568 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3569
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003570 /*
James Smart92740b22007-04-27 11:53:17 -04003571 * If the port is ONLINE, then it came back. If it was a SCSI
3572 * target, validate it still is. If not, tear down the
3573 * scsi_target on it.
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003574 */
Muneendra Kumar02c66322021-01-07 03:19:06 +05303575 if (((rport->port_state == FC_PORTSTATE_ONLINE) ||
3576 (rport->port_state == FC_PORTSTATE_MARGINAL)) &&
James Smart92740b22007-04-27 11:53:17 -04003577 (rport->scsi_target_id != -1) &&
James Smarta53eb5e2007-04-27 12:41:09 -04003578 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003579 dev_printk(KERN_ERR, &rport->dev,
James Smartaedf3492006-04-10 10:14:05 -04003580 "blocked FC remote port time out: no longer"
3581 " a FCP target, removing starget\n");
James Smartaedf3492006-04-10 10:14:05 -04003582 spin_unlock_irqrestore(shost->host_lock, flags);
Mike Christie5d9fb5c2012-05-17 23:56:57 -05003583 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
James Smarta0785ed2006-05-11 13:27:09 -04003584 fc_queue_work(shost, &rport->stgt_delete_work);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003585 return;
3586 }
3587
James Smart92740b22007-04-27 11:53:17 -04003588 /* NOOP state - we're flushing workq's */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003589 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3590 spin_unlock_irqrestore(shost->host_lock, flags);
3591 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003592 "blocked FC remote port time out: leaving"
3593 " rport%s alone\n",
3594 (rport->scsi_target_id != -1) ? " and starget" : "");
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003595 return;
3596 }
3597
James Smart92740b22007-04-27 11:53:17 -04003598 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3599 (rport->scsi_target_id == -1)) {
James Smartaedf3492006-04-10 10:14:05 -04003600 list_del(&rport->peers);
3601 rport->port_state = FC_PORTSTATE_DELETED;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003602 dev_printk(KERN_ERR, &rport->dev,
James Smart92740b22007-04-27 11:53:17 -04003603 "blocked FC remote port time out: removing"
3604 " rport%s\n",
3605 (rport->scsi_target_id != -1) ? " and starget" : "");
James Smartaedf3492006-04-10 10:14:05 -04003606 fc_queue_work(shost, &rport->rport_delete_work);
3607 spin_unlock_irqrestore(shost->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003608 return;
3609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
3611 dev_printk(KERN_ERR, &rport->dev,
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003612 "blocked FC remote port time out: removing target and "
3613 "saving binding\n");
3614
James Smartaedf3492006-04-10 10:14:05 -04003615 list_move_tail(&rport->peers, &fc_host->rport_bindings);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003616
3617 /*
3618 * Note: We do not remove or clear the hostdata area. This allows
3619 * host-specific target data to persist along with the
3620 * scsi_target_id. It's up to the host to manage it's hostdata area.
3621 */
3622
3623 /*
3624 * Reinitialize port attributes that may change if the port comes back.
3625 */
3626 rport->maxframe_size = -1;
3627 rport->supported_classes = FC_COS_UNSPECIFIED;
James Smarta53eb5e2007-04-27 12:41:09 -04003628 rport->roles = FC_PORT_ROLE_UNKNOWN;
James Smartaedf3492006-04-10 10:14:05 -04003629 rport->port_state = FC_PORTSTATE_NOTPRESENT;
Mike Christiefff9d402008-08-19 18:45:23 -05003630 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003631
James Smartf78badb2008-12-05 16:29:59 -06003632 /*
3633 * Pre-emptively kill I/O rather than waiting for the work queue
3634 * item to teardown the starget. (FCOE libFC folks prefer this
3635 * and to have the rport_port_id still set when it's done).
3636 */
3637 spin_unlock_irqrestore(shost->host_lock, flags);
3638 fc_terminate_rport_io(rport);
3639
Michael Reed8798a692009-10-09 14:15:59 -05003640 spin_lock_irqsave(shost->host_lock, flags);
James Smartf78badb2008-12-05 16:29:59 -06003641
Michael Reed8798a692009-10-09 14:15:59 -05003642 if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
3643
3644 /* remove the identifiers that aren't used in the consisting binding */
3645 switch (fc_host->tgtid_bind_type) {
3646 case FC_TGTID_BIND_BY_WWPN:
3647 rport->node_name = -1;
3648 rport->port_id = -1;
3649 break;
3650 case FC_TGTID_BIND_BY_WWNN:
3651 rport->port_name = -1;
3652 rport->port_id = -1;
3653 break;
3654 case FC_TGTID_BIND_BY_ID:
3655 rport->node_name = -1;
3656 rport->port_name = -1;
3657 break;
3658 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3659 break;
3660 }
3661
3662 /*
3663 * As this only occurs if the remote port (scsi target)
3664 * went away and didn't come back - we'll remove
3665 * all attached scsi devices.
3666 */
3667 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3668 fc_queue_work(shost, &rport->stgt_delete_work);
3669
3670 do_callback = 1;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003671 }
3672
Michael Reed8798a692009-10-09 14:15:59 -05003673 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart4be98c02009-01-05 12:14:18 -05003674
3675 /*
3676 * Notify the driver that the rport is now dead. The LLDD will
3677 * also guarantee that any communication to the rport is terminated
3678 *
3679 * Note: we set the CALLBK_DONE flag above to correspond
3680 */
Michael Reed8798a692009-10-09 14:15:59 -05003681 if (do_callback && i->f->dev_loss_tmo_callbk)
James Smart4be98c02009-01-05 12:14:18 -05003682 i->f->dev_loss_tmo_callbk(rport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683}
3684
James Smart4be98c02009-01-05 12:14:18 -05003685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686/**
Rob Landleyeb448202007-11-03 13:30:39 -05003687 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
David Howellsc4028952006-11-22 14:57:56 +00003688 * @work: rport to terminate io on.
James Smart0f29b962006-08-18 17:33:29 -04003689 *
3690 * Notes: Only requests the failure of the io, not that all are flushed
3691 * prior to returning.
Rob Landleyeb448202007-11-03 13:30:39 -05003692 */
James Smart0f29b962006-08-18 17:33:29 -04003693static void
David Howellsc4028952006-11-22 14:57:56 +00003694fc_timeout_fail_rport_io(struct work_struct *work)
James Smart0f29b962006-08-18 17:33:29 -04003695{
David Howellsc4028952006-11-22 14:57:56 +00003696 struct fc_rport *rport =
3697 container_of(work, struct fc_rport, fail_io_work.work);
James Smart0f29b962006-08-18 17:33:29 -04003698
3699 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3700 return;
3701
Mike Christiefff9d402008-08-19 18:45:23 -05003702 rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3703 fc_terminate_rport_io(rport);
James Smart0f29b962006-08-18 17:33:29 -04003704}
3705
3706/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
David Howellsc4028952006-11-22 14:57:56 +00003708 * @work: remote port to be scanned.
Rob Landleyeb448202007-11-03 13:30:39 -05003709 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710static void
David Howellsc4028952006-11-22 14:57:56 +00003711fc_scsi_scan_rport(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
David Howellsc4028952006-11-22 14:57:56 +00003713 struct fc_rport *rport =
3714 container_of(work, struct fc_rport, scan_work);
James Smartaedf3492006-04-10 10:14:05 -04003715 struct Scsi_Host *shost = rport_to_shost(rport);
Christof Schmitt03f002f2007-08-28 09:31:21 +02003716 struct fc_internal *i = to_fc_internal(shost->transportt);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003717 unsigned long flags;
3718
Muneendra Kumar02c66322021-01-07 03:19:06 +05303719 if (((rport->port_state == FC_PORTSTATE_ONLINE) ||
3720 (rport->port_state == FC_PORTSTATE_MARGINAL)) &&
Christof Schmitt03f002f2007-08-28 09:31:21 +02003721 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3722 !(i->f->disable_target_scan)) {
James Smartaedf3492006-04-10 10:14:05 -04003723 scsi_scan_target(&rport->dev, rport->channel,
Hannes Reinecke1d645082016-03-17 08:39:45 +01003724 rport->scsi_target_id, SCAN_WILD_CARD,
3725 SCSI_SCAN_RESCAN);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003726 }
James Smartaedf3492006-04-10 10:14:05 -04003727
3728 spin_lock_irqsave(shost->host_lock, flags);
3729 rport->flags &= ~FC_RPORT_SCAN_PENDING;
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003730 spin_unlock_irqrestore(shost->host_lock, flags);
3731}
3732
Christof Schmitt65d430f2009-10-30 17:59:29 +01003733/**
Steffen Maier67b46522017-07-25 16:14:24 +02003734 * fc_block_rport() - Block SCSI eh thread for blocked fc_rport.
3735 * @rport: Remote port that scsi_eh is trying to recover.
Christof Schmitt65d430f2009-10-30 17:59:29 +01003736 *
3737 * This routine can be called from a FC LLD scsi_eh callback. It
3738 * blocks the scsi_eh thread until the fc_rport leaves the
Christof Schmitt2f2eb582010-03-24 16:50:30 +01003739 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3740 * necessary to avoid the scsi_eh failing recovery actions for blocked
3741 * rports which would lead to offlined SCSI devices.
3742 *
3743 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3744 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3745 * passed back to scsi_eh.
Christof Schmitt65d430f2009-10-30 17:59:29 +01003746 */
Steffen Maier67b46522017-07-25 16:14:24 +02003747int fc_block_rport(struct fc_rport *rport)
Christof Schmitt65d430f2009-10-30 17:59:29 +01003748{
Steffen Maier67b46522017-07-25 16:14:24 +02003749 struct Scsi_Host *shost = rport_to_shost(rport);
Christof Schmitt65d430f2009-10-30 17:59:29 +01003750 unsigned long flags;
3751
3752 spin_lock_irqsave(shost->host_lock, flags);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01003753 while (rport->port_state == FC_PORTSTATE_BLOCKED &&
3754 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
Christof Schmitt65d430f2009-10-30 17:59:29 +01003755 spin_unlock_irqrestore(shost->host_lock, flags);
3756 msleep(1000);
3757 spin_lock_irqsave(shost->host_lock, flags);
3758 }
3759 spin_unlock_irqrestore(shost->host_lock, flags);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01003760
3761 if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
3762 return FAST_IO_FAIL;
3763
3764 return 0;
Christof Schmitt65d430f2009-10-30 17:59:29 +01003765}
Steffen Maier67b46522017-07-25 16:14:24 +02003766EXPORT_SYMBOL(fc_block_rport);
3767
3768/**
3769 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3770 * @cmnd: SCSI command that scsi_eh is trying to recover
3771 *
3772 * This routine can be called from a FC LLD scsi_eh callback. It
3773 * blocks the scsi_eh thread until the fc_rport leaves the
3774 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3775 * necessary to avoid the scsi_eh failing recovery actions for blocked
3776 * rports which would lead to offlined SCSI devices.
3777 *
3778 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3779 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3780 * passed back to scsi_eh.
3781 */
3782int fc_block_scsi_eh(struct scsi_cmnd *cmnd)
3783{
3784 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3785
Johannes Thumshirn8d303712017-10-10 17:31:38 +02003786 if (WARN_ON_ONCE(!rport))
3787 return FAST_IO_FAIL;
3788
Steffen Maier67b46522017-07-25 16:14:24 +02003789 return fc_block_rport(rport);
3790}
Christof Schmitt65d430f2009-10-30 17:59:29 +01003791EXPORT_SYMBOL(fc_block_scsi_eh);
James.Smart@Emulex.Com42e33142005-12-15 09:56:22 -05003792
Muneendra Kumar02c66322021-01-07 03:19:06 +05303793/*
3794 * fc_eh_should_retry_cmd - Checks if the cmd should be retried or not
3795 * @scmd: The SCSI command to be checked
3796 *
3797 * This checks the rport state to decide if a cmd is
3798 * retryable.
3799 *
3800 * Returns: true if the rport state is not in marginal state.
3801 */
3802bool fc_eh_should_retry_cmd(struct scsi_cmnd *scmd)
3803{
3804 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
3805
3806 if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
3807 (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT)) {
3808 set_host_byte(scmd, DID_TRANSPORT_MARGINAL);
3809 return false;
3810 }
3811 return true;
3812}
3813EXPORT_SYMBOL_GPL(fc_eh_should_retry_cmd);
3814
James Smarta53eb5e2007-04-27 12:41:09 -04003815/**
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003816 * fc_vport_setup - allocates and creates a FC virtual port.
James Smarta53eb5e2007-04-27 12:41:09 -04003817 * @shost: scsi host the virtual port is connected to.
3818 * @channel: Channel on shost port connected to.
3819 * @pdev: parent device for vport
3820 * @ids: The world wide names, FC4 port roles, etc for
3821 * the virtual port.
3822 * @ret_vport: The pointer to the created vport.
3823 *
3824 * Allocates and creates the vport structure, calls the parent host
Tyrel Datwyler134699d2017-06-21 16:24:39 -04003825 * to instantiate the vport, this completes w/ class and sysfs creation.
James Smarta53eb5e2007-04-27 12:41:09 -04003826 *
3827 * Notes:
3828 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003829 */
James Smarta53eb5e2007-04-27 12:41:09 -04003830static int
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003831fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
James Smarta53eb5e2007-04-27 12:41:09 -04003832 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3833{
3834 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3835 struct fc_internal *fci = to_fc_internal(shost->transportt);
3836 struct fc_vport *vport;
3837 struct device *dev;
3838 unsigned long flags;
3839 size_t size;
3840 int error;
3841
3842 *ret_vport = NULL;
3843
3844 if ( ! fci->f->vport_create)
3845 return -ENOENT;
3846
3847 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3848 vport = kzalloc(size, GFP_KERNEL);
3849 if (unlikely(!vport)) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003850 printk(KERN_ERR "%s: allocation failure\n", __func__);
James Smarta53eb5e2007-04-27 12:41:09 -04003851 return -ENOMEM;
3852 }
3853
3854 vport->vport_state = FC_VPORT_UNKNOWN;
3855 vport->vport_last_state = FC_VPORT_UNKNOWN;
3856 vport->node_name = ids->node_name;
3857 vport->port_name = ids->port_name;
3858 vport->roles = ids->roles;
3859 vport->vport_type = ids->vport_type;
3860 if (fci->f->dd_fcvport_size)
3861 vport->dd_data = &vport[1];
3862 vport->shost = shost;
3863 vport->channel = channel;
3864 vport->flags = FC_VPORT_CREATING;
James Smart9ef3e4a2007-05-24 19:04:44 -04003865 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
James Smarta53eb5e2007-04-27 12:41:09 -04003866
3867 spin_lock_irqsave(shost->host_lock, flags);
3868
3869 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3870 spin_unlock_irqrestore(shost->host_lock, flags);
3871 kfree(vport);
3872 return -ENOSPC;
3873 }
3874 fc_host->npiv_vports_inuse++;
3875 vport->number = fc_host->next_vport_number++;
3876 list_add_tail(&vport->peers, &fc_host->vports);
Bart Van Assche4cd38e32015-11-20 13:33:04 -08003877 scsi_host_get(shost); /* for fc_host->vport list */
James Smarta53eb5e2007-04-27 12:41:09 -04003878
3879 spin_unlock_irqrestore(shost->host_lock, flags);
3880
3881 dev = &vport->dev;
3882 device_initialize(dev); /* takes self reference */
3883 dev->parent = get_device(pdev); /* takes parent reference */
3884 dev->release = fc_vport_dev_release;
Kay Sievers71610f52008-12-03 22:41:36 +01003885 dev_set_name(dev, "vport-%d:%d-%d",
3886 shost->host_no, channel, vport->number);
James Smarta53eb5e2007-04-27 12:41:09 -04003887 transport_setup_device(dev);
3888
3889 error = device_add(dev);
3890 if (error) {
3891 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3892 goto delete_vport;
3893 }
3894 transport_add_device(dev);
3895 transport_configure_device(dev);
3896
3897 error = fci->f->vport_create(vport, ids->disable);
3898 if (error) {
3899 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3900 goto delete_vport_all;
3901 }
3902
3903 /*
3904 * if the parent isn't the physical adapter's Scsi_Host, ensure
Tyrel Datwyler134699d2017-06-21 16:24:39 -04003905 * the Scsi_Host at least contains a symlink to the vport.
James Smarta53eb5e2007-04-27 12:41:09 -04003906 */
3907 if (pdev != &shost->shost_gendev) {
3908 error = sysfs_create_link(&shost->shost_gendev.kobj,
Kay Sievers71610f52008-12-03 22:41:36 +01003909 &dev->kobj, dev_name(dev));
James Smarta53eb5e2007-04-27 12:41:09 -04003910 if (error)
3911 printk(KERN_ERR
3912 "%s: Cannot create vport symlinks for "
3913 "%s, err=%d\n",
Kay Sievers71610f52008-12-03 22:41:36 +01003914 __func__, dev_name(dev), error);
James Smarta53eb5e2007-04-27 12:41:09 -04003915 }
3916 spin_lock_irqsave(shost->host_lock, flags);
3917 vport->flags &= ~FC_VPORT_CREATING;
3918 spin_unlock_irqrestore(shost->host_lock, flags);
3919
3920 dev_printk(KERN_NOTICE, pdev,
Kay Sievers71610f52008-12-03 22:41:36 +01003921 "%s created via shost%d channel %d\n", dev_name(dev),
James Smarta53eb5e2007-04-27 12:41:09 -04003922 shost->host_no, channel);
3923
3924 *ret_vport = vport;
3925
3926 return 0;
3927
3928delete_vport_all:
3929 transport_remove_device(dev);
3930 device_del(dev);
3931delete_vport:
3932 transport_destroy_device(dev);
3933 spin_lock_irqsave(shost->host_lock, flags);
3934 list_del(&vport->peers);
Bart Van Assche4cd38e32015-11-20 13:33:04 -08003935 scsi_host_put(shost); /* for fc_host->vport list */
James Smarta53eb5e2007-04-27 12:41:09 -04003936 fc_host->npiv_vports_inuse--;
3937 spin_unlock_irqrestore(shost->host_lock, flags);
3938 put_device(dev->parent);
3939 kfree(vport);
3940
3941 return error;
3942}
3943
Andrew Vasqueza30c3f62008-07-18 08:32:52 -07003944/**
3945 * fc_vport_create - Admin App or LLDD requests creation of a vport
3946 * @shost: scsi host the virtual port is connected to.
3947 * @channel: channel on shost port connected to.
3948 * @ids: The world wide names, FC4 port roles, etc for
3949 * the virtual port.
3950 *
3951 * Notes:
3952 * This routine assumes no locks are held on entry.
3953 */
3954struct fc_vport *
3955fc_vport_create(struct Scsi_Host *shost, int channel,
3956 struct fc_vport_identifiers *ids)
3957{
3958 int stat;
3959 struct fc_vport *vport;
3960
3961 stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3962 ids, &vport);
3963 return stat ? NULL : vport;
3964}
3965EXPORT_SYMBOL(fc_vport_create);
James Smarta53eb5e2007-04-27 12:41:09 -04003966
3967/**
3968 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3969 * @vport: fc_vport to be terminated
3970 *
3971 * Calls the LLDD vport_delete() function, then deallocates and removes
3972 * the vport from the shost and object tree.
3973 *
3974 * Notes:
3975 * This routine assumes no locks are held on entry.
Rob Landleyeb448202007-11-03 13:30:39 -05003976 */
James Smarta53eb5e2007-04-27 12:41:09 -04003977int
3978fc_vport_terminate(struct fc_vport *vport)
3979{
3980 struct Scsi_Host *shost = vport_to_shost(vport);
3981 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3982 struct fc_internal *i = to_fc_internal(shost->transportt);
3983 struct device *dev = &vport->dev;
3984 unsigned long flags;
3985 int stat;
3986
James Smarta53eb5e2007-04-27 12:41:09 -04003987 if (i->f->vport_delete)
3988 stat = i->f->vport_delete(vport);
3989 else
3990 stat = -ENOENT;
3991
3992 spin_lock_irqsave(shost->host_lock, flags);
3993 vport->flags &= ~FC_VPORT_DELETING;
3994 if (!stat) {
3995 vport->flags |= FC_VPORT_DELETED;
3996 list_del(&vport->peers);
3997 fc_host->npiv_vports_inuse--;
Bart Van Assche4cd38e32015-11-20 13:33:04 -08003998 scsi_host_put(shost); /* for fc_host->vport list */
James Smarta53eb5e2007-04-27 12:41:09 -04003999 }
4000 spin_unlock_irqrestore(shost->host_lock, flags);
4001
4002 if (stat)
4003 return stat;
4004
4005 if (dev->parent != &shost->shost_gendev)
Kay Sievers71610f52008-12-03 22:41:36 +01004006 sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
James Smarta53eb5e2007-04-27 12:41:09 -04004007 transport_remove_device(dev);
4008 device_del(dev);
4009 transport_destroy_device(dev);
4010
4011 /*
4012 * Removing our self-reference should mean our
4013 * release function gets called, which will drop the remaining
4014 * parent reference and free the data structure.
4015 */
4016 put_device(dev); /* for self-reference */
4017
4018 return 0; /* SUCCESS */
4019}
4020EXPORT_SYMBOL(fc_vport_terminate);
4021
James Smart9ef3e4a2007-05-24 19:04:44 -04004022/**
4023 * fc_vport_sched_delete - workq-based delete request for a vport
James Smart9ef3e4a2007-05-24 19:04:44 -04004024 * @work: vport to be deleted.
Rob Landleyeb448202007-11-03 13:30:39 -05004025 */
James Smart9ef3e4a2007-05-24 19:04:44 -04004026static void
4027fc_vport_sched_delete(struct work_struct *work)
4028{
4029 struct fc_vport *vport =
4030 container_of(work, struct fc_vport, vport_delete_work);
4031 int stat;
James Smarta53eb5e2007-04-27 12:41:09 -04004032
James Smart9ef3e4a2007-05-24 19:04:44 -04004033 stat = fc_vport_terminate(vport);
4034 if (stat)
4035 dev_printk(KERN_ERR, vport->dev.parent,
4036 "%s: %s could not be deleted created via "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004037 "shost%d channel %d - error %d\n", __func__,
Kay Sievers71610f52008-12-03 22:41:36 +01004038 dev_name(&vport->dev), vport->shost->host_no,
James Smart9ef3e4a2007-05-24 19:04:44 -04004039 vport->channel, stat);
4040}
4041
4042
James Smart9e4f5e22009-03-26 13:33:19 -04004043/*
4044 * BSG support
4045 */
4046
James Smart9e4f5e22009-03-26 13:33:19 -04004047/**
James Smart9e4f5e22009-03-26 13:33:19 -04004048 * fc_bsg_job_timeout - handler for when a bsg request timesout
4049 * @req: request that timed out
4050 */
4051static enum blk_eh_timer_return
4052fc_bsg_job_timeout(struct request *req)
4053{
Christoph Hellwigb468b6a2017-09-07 13:54:36 +02004054 struct bsg_job *job = blk_mq_rq_to_pdu(req);
Johannes Thumshirncd21c602016-11-17 10:31:14 +01004055 struct Scsi_Host *shost = fc_bsg_to_shost(job);
Johannes Thumshirn1d69b122016-11-17 10:31:15 +01004056 struct fc_rport *rport = fc_bsg_to_rport(job);
James Smart9e4f5e22009-03-26 13:33:19 -04004057 struct fc_internal *i = to_fc_internal(shost->transportt);
Johannes Thumshirnad7660c2016-11-17 10:31:17 +01004058 int err = 0, inflight = 0;
James Smart9e4f5e22009-03-26 13:33:19 -04004059
Johannes Thumshirn1d69b122016-11-17 10:31:15 +01004060 if (rport && rport->port_state == FC_PORTSTATE_BLOCKED)
James Smart9e4f5e22009-03-26 13:33:19 -04004061 return BLK_EH_RESET_TIMER;
4062
Johannes Thumshirnfb6f7c82016-11-17 10:31:23 +01004063 inflight = bsg_job_get(job);
James Smart9e4f5e22009-03-26 13:33:19 -04004064
Johannes Thumshirnad7660c2016-11-17 10:31:17 +01004065 if (inflight && i->f->bsg_timeout) {
James Smart9e4f5e22009-03-26 13:33:19 -04004066 /* call LLDD to abort the i/o as it has timed out */
4067 err = i->f->bsg_timeout(job);
Swen Schilligb8f08642010-01-14 17:19:00 +01004068 if (err == -EAGAIN) {
Johannes Thumshirnfb6f7c82016-11-17 10:31:23 +01004069 bsg_job_put(job);
Swen Schilligb8f08642010-01-14 17:19:00 +01004070 return BLK_EH_RESET_TIMER;
4071 } else if (err)
James Smart9e4f5e22009-03-26 13:33:19 -04004072 printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
4073 "abort failed with status %d\n", err);
4074 }
4075
James Smart9e4f5e22009-03-26 13:33:19 -04004076 /* the blk_end_sync_io() doesn't check the error */
Christoph Hellwig1fc2b622018-05-29 15:52:34 +02004077 if (inflight)
Jens Axboecd2f0762018-10-24 07:11:39 -06004078 blk_mq_end_request(req, BLK_STS_IOERR);
Christoph Hellwig1fc2b622018-05-29 15:52:34 +02004079 return BLK_EH_DONE;
James Smart9e4f5e22009-03-26 13:33:19 -04004080}
4081
James Smart9e4f5e22009-03-26 13:33:19 -04004082/**
4083 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
4084 * @shost: scsi host rport attached to
4085 * @job: bsg job to be processed
4086 */
Johannes Thumshirna0f4bd72016-11-17 10:31:24 +01004087static int fc_bsg_host_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
James Smart9e4f5e22009-03-26 13:33:19 -04004088{
4089 struct fc_internal *i = to_fc_internal(shost->transportt);
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004090 struct fc_bsg_request *bsg_request = job->request;
4091 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart9e4f5e22009-03-26 13:33:19 -04004092 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
4093 int ret;
4094
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004095 /* check if we really have all the request data needed */
4096 if (job->request_len < cmdlen) {
4097 ret = -ENOMSG;
4098 goto fail_host_msg;
4099 }
4100
James Smart9e4f5e22009-03-26 13:33:19 -04004101 /* Validate the host command */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004102 switch (bsg_request->msgcode) {
James Smart9e4f5e22009-03-26 13:33:19 -04004103 case FC_BSG_HST_ADD_RPORT:
4104 cmdlen += sizeof(struct fc_bsg_host_add_rport);
4105 break;
4106
4107 case FC_BSG_HST_DEL_RPORT:
4108 cmdlen += sizeof(struct fc_bsg_host_del_rport);
4109 break;
4110
4111 case FC_BSG_HST_ELS_NOLOGIN:
4112 cmdlen += sizeof(struct fc_bsg_host_els);
4113 /* there better be a xmt and rcv payloads */
4114 if ((!job->request_payload.payload_len) ||
4115 (!job->reply_payload.payload_len)) {
4116 ret = -EINVAL;
4117 goto fail_host_msg;
4118 }
4119 break;
4120
4121 case FC_BSG_HST_CT:
4122 cmdlen += sizeof(struct fc_bsg_host_ct);
4123 /* there better be xmt and rcv payloads */
4124 if ((!job->request_payload.payload_len) ||
4125 (!job->reply_payload.payload_len)) {
4126 ret = -EINVAL;
4127 goto fail_host_msg;
4128 }
4129 break;
4130
4131 case FC_BSG_HST_VENDOR:
4132 cmdlen += sizeof(struct fc_bsg_host_vendor);
4133 if ((shost->hostt->vendor_id == 0L) ||
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004134 (bsg_request->rqst_data.h_vendor.vendor_id !=
James Smart9e4f5e22009-03-26 13:33:19 -04004135 shost->hostt->vendor_id)) {
4136 ret = -ESRCH;
4137 goto fail_host_msg;
4138 }
4139 break;
4140
4141 default:
4142 ret = -EBADR;
4143 goto fail_host_msg;
4144 }
4145
James Smart9e4f5e22009-03-26 13:33:19 -04004146 ret = i->f->bsg_request(job);
4147 if (!ret)
Johannes Thumshirna0f4bd72016-11-17 10:31:24 +01004148 return 0;
James Smart9e4f5e22009-03-26 13:33:19 -04004149
4150fail_host_msg:
4151 /* return the errno failure code as the only status */
4152 BUG_ON(job->reply_len < sizeof(uint32_t));
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004153 bsg_reply->reply_payload_rcv_len = 0;
4154 bsg_reply->result = ret;
James Smart9e4f5e22009-03-26 13:33:19 -04004155 job->reply_len = sizeof(uint32_t);
Johannes Thumshirn06548162016-11-17 10:31:22 +01004156 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01004157 bsg_reply->reply_payload_rcv_len);
Johannes Thumshirna0f4bd72016-11-17 10:31:24 +01004158 return 0;
James Smart9e4f5e22009-03-26 13:33:19 -04004159}
4160
4161
4162/*
4163 * fc_bsg_goose_queue - restart rport queue in case it was stopped
4164 * @rport: rport to be restarted
4165 */
4166static void
4167fc_bsg_goose_queue(struct fc_rport *rport)
4168{
Bart Van Assche378eead2016-11-11 16:55:50 -08004169 struct request_queue *q = rport->rqst_q;
Bart Van Assche378eead2016-11-11 16:55:50 -08004170
Jens Axboecd2f0762018-10-24 07:11:39 -06004171 if (q)
4172 blk_mq_run_hw_queues(q, true);
James Smart9e4f5e22009-03-26 13:33:19 -04004173}
4174
James Smart9e4f5e22009-03-26 13:33:19 -04004175/**
4176 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
4177 * @shost: scsi host rport attached to
James Smart9e4f5e22009-03-26 13:33:19 -04004178 * @job: bsg job to be processed
4179 */
Johannes Thumshirna0f4bd72016-11-17 10:31:24 +01004180static int fc_bsg_rport_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
James Smart9e4f5e22009-03-26 13:33:19 -04004181{
4182 struct fc_internal *i = to_fc_internal(shost->transportt);
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004183 struct fc_bsg_request *bsg_request = job->request;
4184 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart9e4f5e22009-03-26 13:33:19 -04004185 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
4186 int ret;
4187
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004188 /* check if we really have all the request data needed */
4189 if (job->request_len < cmdlen) {
4190 ret = -ENOMSG;
4191 goto fail_rport_msg;
4192 }
4193
James Smart9e4f5e22009-03-26 13:33:19 -04004194 /* Validate the rport command */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004195 switch (bsg_request->msgcode) {
James Smart9e4f5e22009-03-26 13:33:19 -04004196 case FC_BSG_RPT_ELS:
4197 cmdlen += sizeof(struct fc_bsg_rport_els);
4198 goto check_bidi;
4199
4200 case FC_BSG_RPT_CT:
4201 cmdlen += sizeof(struct fc_bsg_rport_ct);
4202check_bidi:
4203 /* there better be xmt and rcv payloads */
4204 if ((!job->request_payload.payload_len) ||
4205 (!job->reply_payload.payload_len)) {
4206 ret = -EINVAL;
4207 goto fail_rport_msg;
4208 }
4209 break;
4210 default:
4211 ret = -EBADR;
4212 goto fail_rport_msg;
4213 }
4214
James Smart9e4f5e22009-03-26 13:33:19 -04004215 ret = i->f->bsg_request(job);
4216 if (!ret)
Johannes Thumshirna0f4bd72016-11-17 10:31:24 +01004217 return 0;
James Smart9e4f5e22009-03-26 13:33:19 -04004218
4219fail_rport_msg:
4220 /* return the errno failure code as the only status */
4221 BUG_ON(job->reply_len < sizeof(uint32_t));
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004222 bsg_reply->reply_payload_rcv_len = 0;
4223 bsg_reply->result = ret;
James Smart9e4f5e22009-03-26 13:33:19 -04004224 job->reply_len = sizeof(uint32_t);
Johannes Thumshirn06548162016-11-17 10:31:22 +01004225 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01004226 bsg_reply->reply_payload_rcv_len);
Johannes Thumshirna0f4bd72016-11-17 10:31:24 +01004227 return 0;
James Smart9e4f5e22009-03-26 13:33:19 -04004228}
4229
Johannes Thumshirna0f4bd72016-11-17 10:31:24 +01004230static int fc_bsg_dispatch(struct bsg_job *job)
James Smart9e4f5e22009-03-26 13:33:19 -04004231{
Johannes Thumshirna0f4bd72016-11-17 10:31:24 +01004232 struct Scsi_Host *shost = fc_bsg_to_shost(job);
James Smart9e4f5e22009-03-26 13:33:19 -04004233
Johannes Thumshirna0f4bd72016-11-17 10:31:24 +01004234 if (scsi_is_fc_rport(job->dev))
4235 return fc_bsg_rport_dispatch(shost, job);
4236 else
4237 return fc_bsg_host_dispatch(shost, job);
James Smart9e4f5e22009-03-26 13:33:19 -04004238}
4239
Jens Axboecd2f0762018-10-24 07:11:39 -06004240static blk_status_t fc_bsg_rport_prep(struct fc_rport *rport)
4241{
4242 if (rport->port_state == FC_PORTSTATE_BLOCKED &&
4243 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
4244 return BLK_STS_RESOURCE;
4245
Muneendra Kumar02c66322021-01-07 03:19:06 +05304246 if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
4247 (rport->port_state != FC_PORTSTATE_MARGINAL))
Jens Axboecd2f0762018-10-24 07:11:39 -06004248 return BLK_STS_IOERR;
4249
4250 return BLK_STS_OK;
4251}
4252
4253
4254static int fc_bsg_dispatch_prep(struct bsg_job *job)
4255{
4256 struct fc_rport *rport = fc_bsg_to_rport(job);
4257 blk_status_t ret;
4258
4259 ret = fc_bsg_rport_prep(rport);
4260 switch (ret) {
4261 case BLK_STS_OK:
4262 break;
4263 case BLK_STS_RESOURCE:
4264 return -EAGAIN;
4265 default:
4266 return -EIO;
4267 }
4268
4269 return fc_bsg_dispatch(job);
4270}
4271
James Smart9e4f5e22009-03-26 13:33:19 -04004272/**
4273 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
4274 * @shost: shost for fc_host
4275 * @fc_host: fc_host adding the structures to
4276 */
4277static int
4278fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
4279{
4280 struct device *dev = &shost->shost_gendev;
4281 struct fc_internal *i = to_fc_internal(shost->transportt);
4282 struct request_queue *q;
James Bottomley3c559ea2009-06-21 12:11:43 -05004283 char bsg_name[20];
James Smart9e4f5e22009-03-26 13:33:19 -04004284
4285 fc_host->rqst_q = NULL;
4286
4287 if (!i->f->bsg_request)
4288 return -ENOTSUPP;
4289
4290 snprintf(bsg_name, sizeof(bsg_name),
4291 "fc_host%d", shost->host_no);
4292
Jens Axboeaae3b062018-10-26 11:26:25 -06004293 q = bsg_setup_queue(dev, bsg_name, fc_bsg_dispatch, fc_bsg_job_timeout,
4294 i->f->dd_bsg_size);
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +03004295 if (IS_ERR(q)) {
Johannes Thumshirna0f4bd72016-11-17 10:31:24 +01004296 dev_err(dev,
4297 "fc_host%d: bsg interface failed to initialize - setup queue\n",
4298 shost->host_no);
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +03004299 return PTR_ERR(q);
James Smart9e4f5e22009-03-26 13:33:19 -04004300 }
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +03004301 __scsi_init_queue(shost, q);
Johannes Thumshirna0f4bd72016-11-17 10:31:24 +01004302 blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
James Smart9e4f5e22009-03-26 13:33:19 -04004303 fc_host->rqst_q = q;
4304 return 0;
4305}
4306
James Smart9e4f5e22009-03-26 13:33:19 -04004307/**
4308 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
4309 * @shost: shost that rport is attached to
4310 * @rport: rport that the bsg hooks are being attached to
4311 */
4312static int
4313fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
4314{
4315 struct device *dev = &rport->dev;
4316 struct fc_internal *i = to_fc_internal(shost->transportt);
4317 struct request_queue *q;
James Smart9e4f5e22009-03-26 13:33:19 -04004318
4319 rport->rqst_q = NULL;
4320
4321 if (!i->f->bsg_request)
4322 return -ENOTSUPP;
4323
Jens Axboecd2f0762018-10-24 07:11:39 -06004324 q = bsg_setup_queue(dev, dev_name(dev), fc_bsg_dispatch_prep,
Jens Axboeaae3b062018-10-26 11:26:25 -06004325 fc_bsg_job_timeout, i->f->dd_bsg_size);
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +03004326 if (IS_ERR(q)) {
Johannes Thumshirna0f4bd72016-11-17 10:31:24 +01004327 dev_err(dev, "failed to setup bsg queue\n");
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +03004328 return PTR_ERR(q);
James Smart9e4f5e22009-03-26 13:33:19 -04004329 }
Christoph Hellwig8ae94eb2017-01-03 15:25:02 +03004330 __scsi_init_queue(shost, q);
Johannes Thumshirna0f4bd72016-11-17 10:31:24 +01004331 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
James Smart9e4f5e22009-03-26 13:33:19 -04004332 rport->rqst_q = q;
4333 return 0;
4334}
4335
4336
4337/**
4338 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
4339 * @q: the request_queue that is to be torn down.
James Smart78d16342010-08-31 22:27:31 -04004340 *
4341 * Notes:
4342 * Before unregistering the queue empty any requests that are blocked
4343 *
4344 *
James Smart9e4f5e22009-03-26 13:33:19 -04004345 */
4346static void
4347fc_bsg_remove(struct request_queue *q)
4348{
Jens Axboe5e28b8d2018-10-26 11:27:02 -06004349 bsg_remove_queue(q);
James Smart9e4f5e22009-03-26 13:33:19 -04004350}
4351
4352
James Smart9ef3e4a2007-05-24 19:04:44 -04004353/* Original Author: Martin Hicks */
4354MODULE_AUTHOR("James Smart");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355MODULE_DESCRIPTION("FC Transport Attributes");
4356MODULE_LICENSE("GPL");
4357
4358module_init(fc_transport_init);
4359module_exit(fc_transport_exit);