xref: /linux/drivers/scsi/scsi_transport_fc.c (revision d4a379a52c3c2dc44366c4f6722c063a7d0de179)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  FiberChannel transport specific attributes exported to sysfs.
4  *
5  *  Copyright (c) 2003 Silicon Graphics, Inc.  All rights reserved.
6  *  Copyright (C) 2004-2007   James Smart, Emulex Corporation
7  *    Rewrite for host, target, device, and remote port attributes,
8  *    statistics, and service functions...
9  *    Add vports, etc
10  */
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/delay.h>
15 #include <linux/hex.h>
16 #include <linux/kernel.h>
17 #include <linux/bsg-lib.h>
18 #include <scsi/scsi_device.h>
19 #include <scsi/scsi_host.h>
20 #include <scsi/scsi_transport.h>
21 #include <scsi/scsi_transport_fc.h>
22 #include <scsi/scsi_cmnd.h>
23 #include <net/netlink.h>
24 #include <scsi/scsi_netlink_fc.h>
25 #include <scsi/scsi_bsg_fc.h>
26 #include <uapi/scsi/fc/fc_els.h>
27 #include "scsi_priv.h"
28 
29 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
30 static void fc_vport_sched_delete(struct work_struct *work);
31 static int fc_vport_setup(struct Scsi_Host *shost, int channel,
32 	struct device *pdev, struct fc_vport_identifiers  *ids,
33 	struct fc_vport **vport);
34 static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
35 static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
36 static void fc_bsg_remove(struct request_queue *);
37 static void fc_bsg_goose_queue(struct fc_rport *);
38 static void fc_li_stats_update(u16 event_type,
39 			       struct fc_fpin_stats *stats);
40 static void fc_delivery_stats_update(u32 reason_code,
41 				     struct fc_fpin_stats *stats);
42 static void fc_cn_stats_update(u16 event_type, struct fc_fpin_stats *stats);
43 
44 /*
45  * Module Parameters
46  */
47 
48 /*
49  * dev_loss_tmo: the default number of seconds that the FC transport
50  *   should insulate the loss of a remote port.
51  *   The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
52  */
53 static unsigned int fc_dev_loss_tmo = 60;		/* seconds */
54 
55 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
56 MODULE_PARM_DESC(dev_loss_tmo,
57 		 "Maximum number of seconds that the FC transport should"
58 		 " insulate the loss of a remote port. Once this value is"
59 		 " exceeded, the scsi target is removed. Value should be"
60 		 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
61 		 " fast_io_fail_tmo is not set.");
62 
63 /*
64  * Redefine so that we can have same named attributes in the
65  * sdev/starget/host objects.
66  */
67 #define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store)		\
68 struct device_attribute device_attr_##_prefix##_##_name = 	\
69 	__ATTR(_name,_mode,_show,_store)
70 
71 #define fc_enum_name_search(title, table_type, table)			\
72 static const char *get_fc_##title##_name(enum table_type table_key)	\
73 {									\
74 	int i;								\
75 	char *name = NULL;						\
76 									\
77 	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
78 		if (table[i].value == table_key) {			\
79 			name = table[i].name;				\
80 			break;						\
81 		}							\
82 	}								\
83 	return name;							\
84 }
85 
86 #define fc_enum_name_match(title, table_type, table)			\
87 static int get_fc_##title##_match(const char *table_key,		\
88 		enum table_type *value)					\
89 {									\
90 	int i;								\
91 									\
92 	for (i = 0; i < ARRAY_SIZE(table); i++) {			\
93 		if (strncmp(table_key, table[i].name,			\
94 				table[i].matchlen) == 0) {		\
95 			*value = table[i].value;			\
96 			return 0; /* success */				\
97 		}							\
98 	}								\
99 	return 1; /* failure */						\
100 }
101 
102 
103 /* Convert fc_port_type values to ascii string name */
104 static struct {
105 	enum fc_port_type	value;
106 	char			*name;
107 } fc_port_type_names[] = {
108 	{ FC_PORTTYPE_UNKNOWN,		"Unknown" },
109 	{ FC_PORTTYPE_OTHER,		"Other" },
110 	{ FC_PORTTYPE_NOTPRESENT,	"Not Present" },
111 	{ FC_PORTTYPE_NPORT,	"NPort (fabric via point-to-point)" },
112 	{ FC_PORTTYPE_NLPORT,	"NLPort (fabric via loop)" },
113 	{ FC_PORTTYPE_LPORT,	"LPort (private loop)" },
114 	{ FC_PORTTYPE_PTP,	"Point-To-Point (direct nport connection)" },
115 	{ FC_PORTTYPE_NPIV,		"NPIV VPORT" },
116 };
117 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
118 #define FC_PORTTYPE_MAX_NAMELEN		50
119 
120 /* Reuse fc_port_type enum function for vport_type */
121 #define get_fc_vport_type_name get_fc_port_type_name
122 
123 
124 /* Convert fc_host_event_code values to ascii string name */
125 static const struct {
126 	enum fc_host_event_code		value;
127 	char				*name;
128 } fc_host_event_code_names[] = {
129 	{ FCH_EVT_LIP,			"lip" },
130 	{ FCH_EVT_LINKUP,		"link_up" },
131 	{ FCH_EVT_LINKDOWN,		"link_down" },
132 	{ FCH_EVT_LIPRESET,		"lip_reset" },
133 	{ FCH_EVT_RSCN,			"rscn" },
134 	{ FCH_EVT_ADAPTER_CHANGE,	"adapter_chg" },
135 	{ FCH_EVT_PORT_UNKNOWN,		"port_unknown" },
136 	{ FCH_EVT_PORT_ONLINE,		"port_online" },
137 	{ FCH_EVT_PORT_OFFLINE,		"port_offline" },
138 	{ FCH_EVT_PORT_FABRIC,		"port_fabric" },
139 	{ FCH_EVT_LINK_UNKNOWN,		"link_unknown" },
140 	{ FCH_EVT_LINK_FPIN,		"link_FPIN" },
141 	{ FCH_EVT_LINK_FPIN_ACK,	"link_FPIN_ACK" },
142 	{ FCH_EVT_VENDOR_UNIQUE,	"vendor_unique" },
143 };
144 fc_enum_name_search(host_event_code, fc_host_event_code,
145 		fc_host_event_code_names)
146 #define FC_HOST_EVENT_CODE_MAX_NAMELEN	30
147 
148 
149 /* Convert fc_port_state values to ascii string name */
150 static struct {
151 	enum fc_port_state	value;
152 	char			*name;
153 	int			matchlen;
154 } fc_port_state_names[] = {
155 	{ FC_PORTSTATE_UNKNOWN,		"Unknown", 7},
156 	{ FC_PORTSTATE_NOTPRESENT,	"Not Present", 11 },
157 	{ FC_PORTSTATE_ONLINE,		"Online", 6 },
158 	{ FC_PORTSTATE_OFFLINE,		"Offline", 7 },
159 	{ FC_PORTSTATE_BLOCKED,		"Blocked", 7 },
160 	{ FC_PORTSTATE_BYPASSED,	"Bypassed", 8 },
161 	{ FC_PORTSTATE_DIAGNOSTICS,	"Diagnostics", 11 },
162 	{ FC_PORTSTATE_LINKDOWN,	"Linkdown", 8 },
163 	{ FC_PORTSTATE_ERROR,		"Error", 5 },
164 	{ FC_PORTSTATE_LOOPBACK,	"Loopback", 8 },
165 	{ FC_PORTSTATE_DELETED,		"Deleted", 7 },
166 	{ FC_PORTSTATE_MARGINAL,	"Marginal", 8 },
167 };
168 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
169 fc_enum_name_match(port_state, fc_port_state, fc_port_state_names)
170 #define FC_PORTSTATE_MAX_NAMELEN	20
171 
172 
173 /* Convert fc_vport_state values to ascii string name */
174 static struct {
175 	enum fc_vport_state	value;
176 	char			*name;
177 } fc_vport_state_names[] = {
178 	{ FC_VPORT_UNKNOWN,		"Unknown" },
179 	{ FC_VPORT_ACTIVE,		"Active" },
180 	{ FC_VPORT_DISABLED,		"Disabled" },
181 	{ FC_VPORT_LINKDOWN,		"Linkdown" },
182 	{ FC_VPORT_INITIALIZING,	"Initializing" },
183 	{ FC_VPORT_NO_FABRIC_SUPP,	"No Fabric Support" },
184 	{ FC_VPORT_NO_FABRIC_RSCS,	"No Fabric Resources" },
185 	{ FC_VPORT_FABRIC_LOGOUT,	"Fabric Logout" },
186 	{ FC_VPORT_FABRIC_REJ_WWN,	"Fabric Rejected WWN" },
187 	{ FC_VPORT_FAILED,		"VPort Failed" },
188 };
189 fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
190 #define FC_VPORTSTATE_MAX_NAMELEN	24
191 
192 /* Reuse fc_vport_state enum function for vport_last_state */
193 #define get_fc_vport_last_state_name get_fc_vport_state_name
194 
195 
196 /* Convert fc_tgtid_binding_type values to ascii string name */
197 static const struct {
198 	enum fc_tgtid_binding_type	value;
199 	char				*name;
200 	int				matchlen;
201 } fc_tgtid_binding_type_names[] = {
202 	{ FC_TGTID_BIND_NONE, "none", 4 },
203 	{ FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
204 	{ FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
205 	{ FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
206 };
207 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
208 		fc_tgtid_binding_type_names)
209 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
210 		fc_tgtid_binding_type_names)
211 #define FC_BINDTYPE_MAX_NAMELEN	30
212 
213 
214 #define fc_bitfield_name_search(title, table)			\
215 static ssize_t							\
216 get_fc_##title##_names(u32 table_key, char *buf)		\
217 {								\
218 	char *prefix = "";					\
219 	ssize_t len = 0;					\
220 	int i;							\
221 								\
222 	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
223 		if (table[i].value & table_key) {		\
224 			len += sprintf(buf + len, "%s%s",	\
225 				prefix, table[i].name);		\
226 			prefix = ", ";				\
227 		}						\
228 	}							\
229 	len += sprintf(buf + len, "\n");			\
230 	return len;						\
231 }
232 
233 
234 /* Convert FC_COS bit values to ascii string name */
235 static const struct {
236 	u32 			value;
237 	char			*name;
238 } fc_cos_names[] = {
239 	{ FC_COS_CLASS1,	"Class 1" },
240 	{ FC_COS_CLASS2,	"Class 2" },
241 	{ FC_COS_CLASS3,	"Class 3" },
242 	{ FC_COS_CLASS4,	"Class 4" },
243 	{ FC_COS_CLASS6,	"Class 6" },
244 };
245 fc_bitfield_name_search(cos, fc_cos_names)
246 
247 
248 /* Convert FC_PORTSPEED bit values to ascii string name */
249 static const struct {
250 	u32 			value;
251 	char			*name;
252 } fc_port_speed_names[] = {
253 	{ FC_PORTSPEED_1GBIT,		"1 Gbit" },
254 	{ FC_PORTSPEED_2GBIT,		"2 Gbit" },
255 	{ FC_PORTSPEED_4GBIT,		"4 Gbit" },
256 	{ FC_PORTSPEED_10GBIT,		"10 Gbit" },
257 	{ FC_PORTSPEED_8GBIT,		"8 Gbit" },
258 	{ FC_PORTSPEED_16GBIT,		"16 Gbit" },
259 	{ FC_PORTSPEED_32GBIT,		"32 Gbit" },
260 	{ FC_PORTSPEED_20GBIT,		"20 Gbit" },
261 	{ FC_PORTSPEED_40GBIT,		"40 Gbit" },
262 	{ FC_PORTSPEED_50GBIT,		"50 Gbit" },
263 	{ FC_PORTSPEED_100GBIT,		"100 Gbit" },
264 	{ FC_PORTSPEED_25GBIT,		"25 Gbit" },
265 	{ FC_PORTSPEED_64GBIT,		"64 Gbit" },
266 	{ FC_PORTSPEED_128GBIT,		"128 Gbit" },
267 	{ FC_PORTSPEED_256GBIT,		"256 Gbit" },
268 	{ FC_PORTSPEED_NOT_NEGOTIATED,	"Not Negotiated" },
269 };
270 fc_bitfield_name_search(port_speed, fc_port_speed_names)
271 
272 
273 static int
274 show_fc_fc4s (char *buf, u8 *fc4_list)
275 {
276 	int i, len=0;
277 
278 	for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
279 		len += sprintf(buf + len , "0x%02x ", *fc4_list);
280 	len += sprintf(buf + len, "\n");
281 	return len;
282 }
283 
284 
285 /* Convert FC_PORT_ROLE bit values to ascii string name */
286 static const struct {
287 	u32 			value;
288 	char			*name;
289 } fc_port_role_names[] = {
290 	{ FC_PORT_ROLE_FCP_TARGET,		"FCP Target" },
291 	{ FC_PORT_ROLE_FCP_INITIATOR,		"FCP Initiator" },
292 	{ FC_PORT_ROLE_IP_PORT,			"IP Port" },
293 	{ FC_PORT_ROLE_FCP_DUMMY_INITIATOR,	"FCP Dummy Initiator" },
294 	{ FC_PORT_ROLE_NVME_INITIATOR,		"NVMe Initiator" },
295 	{ FC_PORT_ROLE_NVME_TARGET,		"NVMe Target" },
296 	{ FC_PORT_ROLE_NVME_DISCOVERY,		"NVMe Discovery" },
297 };
298 fc_bitfield_name_search(port_roles, fc_port_role_names)
299 
300 /*
301  * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
302  */
303 #define FC_WELLKNOWN_PORTID_MASK	0xfffff0
304 #define FC_WELLKNOWN_ROLE_MASK  	0x00000f
305 #define FC_FPORT_PORTID			0x00000e
306 #define FC_FABCTLR_PORTID		0x00000d
307 #define FC_DIRSRVR_PORTID		0x00000c
308 #define FC_TIMESRVR_PORTID		0x00000b
309 #define FC_MGMTSRVR_PORTID		0x00000a
310 
311 
312 static void fc_timeout_deleted_rport(struct work_struct *work);
313 static void fc_timeout_fail_rport_io(struct work_struct *work);
314 static void fc_scsi_scan_rport(struct work_struct *work);
315 
316 /*
317  * Attribute counts pre object type...
318  * Increase these values if you add attributes
319  */
320 #define FC_STARGET_NUM_ATTRS 	3
321 #define FC_RPORT_NUM_ATTRS	10
322 #define FC_VPORT_NUM_ATTRS	9
323 #define FC_HOST_NUM_ATTRS	29
324 
325 struct fc_internal {
326 	struct scsi_transport_template t;
327 	struct fc_function_template *f;
328 
329 	/*
330 	 * For attributes : each object has :
331 	 *   An array of the actual attributes structures
332 	 *   An array of null-terminated pointers to the attribute
333 	 *     structures - used for mid-layer interaction.
334 	 *
335 	 * The attribute containers for the starget and host are are
336 	 * part of the midlayer. As the remote port is specific to the
337 	 * fc transport, we must provide the attribute container.
338 	 */
339 	struct device_attribute private_starget_attrs[
340 							FC_STARGET_NUM_ATTRS];
341 	struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
342 
343 	struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
344 	struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
345 
346 	struct transport_container rport_attr_cont;
347 	struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
348 	struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
349 
350 	struct transport_container vport_attr_cont;
351 	struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
352 	struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
353 };
354 
355 #define to_fc_internal(tmpl)	container_of(tmpl, struct fc_internal, t)
356 
357 static int fc_target_setup(struct transport_container *tc, struct device *dev,
358 			   struct device *cdev)
359 {
360 	struct scsi_target *starget = to_scsi_target(dev);
361 	struct fc_rport *rport = starget_to_rport(starget);
362 
363 	/*
364 	 * if parent is remote port, use values from remote port.
365 	 * Otherwise, this host uses the fc_transport, but not the
366 	 * remote port interface. As such, initialize to known non-values.
367 	 */
368 	if (rport) {
369 		fc_starget_node_name(starget) = rport->node_name;
370 		fc_starget_port_name(starget) = rport->port_name;
371 		fc_starget_port_id(starget) = rport->port_id;
372 	} else {
373 		fc_starget_node_name(starget) = -1;
374 		fc_starget_port_name(starget) = -1;
375 		fc_starget_port_id(starget) = -1;
376 	}
377 
378 	return 0;
379 }
380 
381 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
382 			       "fc_transport",
383 			       fc_target_setup,
384 			       NULL,
385 			       NULL);
386 
387 static int fc_host_setup(struct transport_container *tc, struct device *dev,
388 			 struct device *cdev)
389 {
390 	struct Scsi_Host *shost = dev_to_shost(dev);
391 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
392 
393 	/*
394 	 * Set default values easily detected by the midlayer as
395 	 * failure cases.  The scsi lldd is responsible for initializing
396 	 * all transport attributes to valid values per host.
397 	 */
398 	fc_host->node_name = -1;
399 	fc_host->port_name = -1;
400 	fc_host->permanent_port_name = -1;
401 	fc_host->supported_classes = FC_COS_UNSPECIFIED;
402 	memset(fc_host->supported_fc4s, 0,
403 		sizeof(fc_host->supported_fc4s));
404 	fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
405 	fc_host->maxframe_size = -1;
406 	fc_host->max_npiv_vports = 0;
407 	memset(fc_host->serial_number, 0,
408 		sizeof(fc_host->serial_number));
409 	memset(fc_host->manufacturer, 0,
410 		sizeof(fc_host->manufacturer));
411 	memset(fc_host->model, 0,
412 		sizeof(fc_host->model));
413 	memset(fc_host->model_description, 0,
414 		sizeof(fc_host->model_description));
415 	memset(fc_host->hardware_version, 0,
416 		sizeof(fc_host->hardware_version));
417 	memset(fc_host->driver_version, 0,
418 		sizeof(fc_host->driver_version));
419 	memset(fc_host->firmware_version, 0,
420 		sizeof(fc_host->firmware_version));
421 	memset(fc_host->optionrom_version, 0,
422 		sizeof(fc_host->optionrom_version));
423 
424 	fc_host->port_id = -1;
425 	fc_host->port_type = FC_PORTTYPE_UNKNOWN;
426 	fc_host->port_state = FC_PORTSTATE_UNKNOWN;
427 	memset(fc_host->active_fc4s, 0,
428 		sizeof(fc_host->active_fc4s));
429 	fc_host->speed = FC_PORTSPEED_UNKNOWN;
430 	fc_host->fabric_name = -1;
431 	memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
432 	memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
433 	memset(&fc_host->fpin_stats, 0, sizeof(fc_host->fpin_stats));
434 
435 	fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
436 
437 	INIT_LIST_HEAD(&fc_host->rports);
438 	INIT_LIST_HEAD(&fc_host->rport_bindings);
439 	INIT_LIST_HEAD(&fc_host->vports);
440 	fc_host->next_rport_number = 0;
441 	fc_host->next_target_id = 0;
442 	fc_host->next_vport_number = 0;
443 	fc_host->npiv_vports_inuse = 0;
444 
445 	fc_host->work_q = alloc_workqueue("fc_wq_%d", WQ_PERCPU, 0,
446 					  shost->host_no);
447 	if (!fc_host->work_q)
448 		return -ENOMEM;
449 
450 	fc_host->dev_loss_tmo = fc_dev_loss_tmo;
451 
452 	fc_bsg_hostadd(shost, fc_host);
453 	/* ignore any bsg add error - we just can't do sgio */
454 
455 	return 0;
456 }
457 
458 static int fc_host_remove(struct transport_container *tc, struct device *dev,
459 			 struct device *cdev)
460 {
461 	struct Scsi_Host *shost = dev_to_shost(dev);
462 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
463 
464 	fc_bsg_remove(fc_host->rqst_q);
465 	return 0;
466 }
467 
468 static DECLARE_TRANSPORT_CLASS(fc_host_class,
469 			       "fc_host",
470 			       fc_host_setup,
471 			       fc_host_remove,
472 			       NULL);
473 
474 /*
475  * Setup and Remove actions for remote ports are handled
476  * in the service functions below.
477  */
478 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
479 			       "fc_remote_ports",
480 			       NULL,
481 			       NULL,
482 			       NULL);
483 
484 /*
485  * Setup and Remove actions for virtual ports are handled
486  * in the service functions below.
487  */
488 static DECLARE_TRANSPORT_CLASS(fc_vport_class,
489 			       "fc_vports",
490 			       NULL,
491 			       NULL,
492 			       NULL);
493 
494 /*
495  * Netlink Infrastructure
496  */
497 
498 static atomic_t fc_event_seq;
499 
500 /**
501  * fc_get_event_number - Obtain the next sequential FC event number
502  *
503  * Notes:
504  *   We could have inlined this, but it would have required fc_event_seq to
505  *   be exposed. For now, live with the subroutine call.
506  *   Atomic used to avoid lock/unlock...
507  */
508 u32
509 fc_get_event_number(void)
510 {
511 	return atomic_add_return(1, &fc_event_seq);
512 }
513 EXPORT_SYMBOL(fc_get_event_number);
514 
515 /**
516  * fc_host_post_fc_event - routine to do the work of posting an event
517  *                      on an fc_host.
518  * @shost:		host the event occurred on
519  * @event_number:	fc event number obtained from get_fc_event_number()
520  * @event_code:		fc_host event being posted
521  * @data_len:		amount, in bytes, of event data
522  * @data_buf:		pointer to event data
523  * @vendor_id:          value for Vendor id
524  *
525  * Notes:
526  *	This routine assumes no locks are held on entry.
527  */
528 void
529 fc_host_post_fc_event(struct Scsi_Host *shost, u32 event_number,
530 		enum fc_host_event_code event_code,
531 		u32 data_len, char *data_buf, u64 vendor_id)
532 {
533 	struct sk_buff *skb;
534 	struct nlmsghdr	*nlh;
535 	struct fc_nl_event *event;
536 	const char *name;
537 	size_t len, padding;
538 	int err;
539 
540 	if (!data_buf || data_len < 4)
541 		data_len = 0;
542 
543 	if (!scsi_nl_sock) {
544 		err = -ENOENT;
545 		goto send_fail;
546 	}
547 
548 	len = FC_NL_MSGALIGN(sizeof(*event) - sizeof(event->event_data) + data_len);
549 
550 	skb = nlmsg_new(len, GFP_KERNEL);
551 	if (!skb) {
552 		err = -ENOBUFS;
553 		goto send_fail;
554 	}
555 
556 	nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0);
557 	if (!nlh) {
558 		err = -ENOBUFS;
559 		goto send_fail_skb;
560 	}
561 	event = nlmsg_data(nlh);
562 
563 	INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
564 				FC_NL_ASYNC_EVENT, len);
565 	event->seconds = ktime_get_real_seconds();
566 	event->vendor_id = vendor_id;
567 	event->host_no = shost->host_no;
568 	event->event_datalen = data_len;	/* bytes */
569 	event->event_num = event_number;
570 	event->event_code = event_code;
571 	if (data_len)
572 		memcpy(event->event_data_flex, data_buf, data_len);
573 	padding = len - offsetof(typeof(*event), event_data_flex) - data_len;
574 	memset(event->event_data_flex + data_len, 0, padding);
575 
576 	nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
577 			GFP_KERNEL);
578 	return;
579 
580 send_fail_skb:
581 	kfree_skb(skb);
582 send_fail:
583 	name = get_fc_host_event_code_name(event_code);
584 	printk(KERN_WARNING
585 		"%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
586 		__func__, shost->host_no,
587 		(name) ? name : "<unknown>",
588 		(data_len) ? *((u32 *)data_buf) : 0xFFFFFFFF, err);
589 	return;
590 }
591 EXPORT_SYMBOL(fc_host_post_fc_event);
592 
593 /**
594  * fc_host_post_event - called to post an even on an fc_host.
595  * @shost:		host the event occurred on
596  * @event_number:	fc event number obtained from get_fc_event_number()
597  * @event_code:		fc_host event being posted
598  * @event_data:		32bits of data for the event being posted
599  *
600  * Notes:
601  *	This routine assumes no locks are held on entry.
602  */
603 void
604 fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
605 		enum fc_host_event_code event_code, u32 event_data)
606 {
607 	fc_host_post_fc_event(shost, event_number, event_code,
608 		(u32)sizeof(u32), (char *)&event_data, 0);
609 }
610 EXPORT_SYMBOL(fc_host_post_event);
611 
612 
613 /**
614  * fc_host_post_vendor_event - called to post a vendor unique event
615  *                      on an fc_host
616  * @shost:		host the event occurred on
617  * @event_number:	fc event number obtained from get_fc_event_number()
618  * @data_len:		amount, in bytes, of vendor unique data
619  * @data_buf:		pointer to vendor unique data
620  * @vendor_id:          Vendor id
621  *
622  * Notes:
623  *	This routine assumes no locks are held on entry.
624  */
625 void
626 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
627 		u32 data_len, char * data_buf, u64 vendor_id)
628 {
629 	fc_host_post_fc_event(shost, event_number, FCH_EVT_VENDOR_UNIQUE,
630 		data_len, data_buf, vendor_id);
631 }
632 EXPORT_SYMBOL(fc_host_post_vendor_event);
633 
634 /**
635  * fc_find_rport_by_wwpn - find the fc_rport pointer for a given wwpn
636  * @shost:		host the fc_rport is associated with
637  * @wwpn:		wwpn of the fc_rport device
638  *
639  * Notes:
640  *	This routine assumes no locks are held on entry.
641  */
642 struct fc_rport *
643 fc_find_rport_by_wwpn(struct Scsi_Host *shost, u64 wwpn)
644 {
645 	struct fc_rport *rport;
646 	unsigned long flags;
647 
648 	spin_lock_irqsave(shost->host_lock, flags);
649 
650 	list_for_each_entry(rport, &fc_host_rports(shost), peers) {
651 		if (rport->port_state != FC_PORTSTATE_ONLINE)
652 			continue;
653 
654 		if (rport->port_name == wwpn) {
655 			spin_unlock_irqrestore(shost->host_lock, flags);
656 			return rport;
657 		}
658 	}
659 
660 	spin_unlock_irqrestore(shost->host_lock, flags);
661 	return NULL;
662 }
663 EXPORT_SYMBOL(fc_find_rport_by_wwpn);
664 
665 static void
666 fc_li_stats_update(u16 event_type,
667 		   struct fc_fpin_stats *stats)
668 {
669 	stats->li++;
670 	switch (event_type) {
671 	case FPIN_LI_UNKNOWN:
672 		stats->li_failure_unknown++;
673 		break;
674 	case FPIN_LI_LINK_FAILURE:
675 		stats->li_link_failure_count++;
676 		break;
677 	case FPIN_LI_LOSS_OF_SYNC:
678 		stats->li_loss_of_sync_count++;
679 		break;
680 	case FPIN_LI_LOSS_OF_SIG:
681 		stats->li_loss_of_signals_count++;
682 		break;
683 	case FPIN_LI_PRIM_SEQ_ERR:
684 		stats->li_prim_seq_err_count++;
685 		break;
686 	case FPIN_LI_INVALID_TX_WD:
687 		stats->li_invalid_tx_word_count++;
688 		break;
689 	case FPIN_LI_INVALID_CRC:
690 		stats->li_invalid_crc_count++;
691 		break;
692 	case FPIN_LI_DEVICE_SPEC:
693 		stats->li_device_specific++;
694 		break;
695 	}
696 }
697 
698 static void
699 fc_delivery_stats_update(u32 reason_code, struct fc_fpin_stats *stats)
700 {
701 	stats->dn++;
702 	switch (reason_code) {
703 	case FPIN_DELI_UNKNOWN:
704 		stats->dn_unknown++;
705 		break;
706 	case FPIN_DELI_TIMEOUT:
707 		stats->dn_timeout++;
708 		break;
709 	case FPIN_DELI_UNABLE_TO_ROUTE:
710 		stats->dn_unable_to_route++;
711 		break;
712 	case FPIN_DELI_DEVICE_SPEC:
713 		stats->dn_device_specific++;
714 		break;
715 	}
716 }
717 
718 static void
719 fc_cn_stats_update(u16 event_type, struct fc_fpin_stats *stats)
720 {
721 	stats->cn++;
722 	switch (event_type) {
723 	case FPIN_CONGN_CLEAR:
724 		stats->cn_clear++;
725 		break;
726 	case FPIN_CONGN_LOST_CREDIT:
727 		stats->cn_lost_credit++;
728 		break;
729 	case FPIN_CONGN_CREDIT_STALL:
730 		stats->cn_credit_stall++;
731 		break;
732 	case FPIN_CONGN_OVERSUBSCRIPTION:
733 		stats->cn_oversubscription++;
734 		break;
735 	case FPIN_CONGN_DEVICE_SPEC:
736 		stats->cn_device_specific++;
737 	}
738 }
739 
740 /*
741  * fc_fpin_li_stats_update - routine to update Link Integrity
742  * event statistics.
743  * @shost:		host the FPIN was received on
744  * @tlv:		pointer to link integrity descriptor
745  *
746  */
747 static void
748 fc_fpin_li_stats_update(struct Scsi_Host *shost, struct fc_tlv_desc *tlv)
749 {
750 	u8 i;
751 	struct fc_rport *rport = NULL;
752 	struct fc_rport *attach_rport = NULL;
753 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
754 	struct fc_fn_li_desc *li_desc = (struct fc_fn_li_desc *)tlv;
755 	u16 event_type = be16_to_cpu(li_desc->event_type);
756 	u64 wwpn;
757 
758 	rport = fc_find_rport_by_wwpn(shost,
759 				      be64_to_cpu(li_desc->attached_wwpn));
760 	if (rport &&
761 	    (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
762 	     rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
763 		attach_rport = rport;
764 		fc_li_stats_update(event_type, &attach_rport->fpin_stats);
765 	}
766 
767 	if (be32_to_cpu(li_desc->pname_count) > 0) {
768 		for (i = 0;
769 		    i < be32_to_cpu(li_desc->pname_count);
770 		    i++) {
771 			wwpn = be64_to_cpu(li_desc->pname_list[i]);
772 			rport = fc_find_rport_by_wwpn(shost, wwpn);
773 			if (rport &&
774 			    (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
775 			    rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
776 				if (rport == attach_rport)
777 					continue;
778 				fc_li_stats_update(event_type,
779 						   &rport->fpin_stats);
780 			}
781 		}
782 	}
783 
784 	if (fc_host->port_name == be64_to_cpu(li_desc->attached_wwpn))
785 		fc_li_stats_update(event_type, &fc_host->fpin_stats);
786 }
787 
788 /*
789  * fc_fpin_delivery_stats_update - routine to update Delivery Notification
790  * event statistics.
791  * @shost:		host the FPIN was received on
792  * @tlv:		pointer to delivery descriptor
793  *
794  */
795 static void
796 fc_fpin_delivery_stats_update(struct Scsi_Host *shost,
797 			      struct fc_tlv_desc *tlv)
798 {
799 	struct fc_rport *rport = NULL;
800 	struct fc_rport *attach_rport = NULL;
801 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
802 	struct fc_fn_deli_desc *dn_desc = (struct fc_fn_deli_desc *)tlv;
803 	u32 reason_code = be32_to_cpu(dn_desc->deli_reason_code);
804 
805 	rport = fc_find_rport_by_wwpn(shost,
806 				      be64_to_cpu(dn_desc->attached_wwpn));
807 	if (rport &&
808 	    (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
809 	     rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
810 		attach_rport = rport;
811 		fc_delivery_stats_update(reason_code,
812 					 &attach_rport->fpin_stats);
813 	}
814 
815 	if (fc_host->port_name == be64_to_cpu(dn_desc->attached_wwpn))
816 		fc_delivery_stats_update(reason_code, &fc_host->fpin_stats);
817 }
818 
819 /*
820  * fc_fpin_peer_congn_stats_update - routine to update Peer Congestion
821  * event statistics.
822  * @shost:		host the FPIN was received on
823  * @tlv:		pointer to peer congestion descriptor
824  *
825  */
826 static void
827 fc_fpin_peer_congn_stats_update(struct Scsi_Host *shost,
828 				struct fc_tlv_desc *tlv)
829 {
830 	u8 i;
831 	struct fc_rport *rport = NULL;
832 	struct fc_rport *attach_rport = NULL;
833 	struct fc_fn_peer_congn_desc *pc_desc =
834 	    (struct fc_fn_peer_congn_desc *)tlv;
835 	u16 event_type = be16_to_cpu(pc_desc->event_type);
836 	u64 wwpn;
837 
838 	rport = fc_find_rport_by_wwpn(shost,
839 				      be64_to_cpu(pc_desc->attached_wwpn));
840 	if (rport &&
841 	    (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
842 	     rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
843 		attach_rport = rport;
844 		fc_cn_stats_update(event_type, &attach_rport->fpin_stats);
845 	}
846 
847 	if (be32_to_cpu(pc_desc->pname_count) > 0) {
848 		for (i = 0;
849 		    i < be32_to_cpu(pc_desc->pname_count);
850 		    i++) {
851 			wwpn = be64_to_cpu(pc_desc->pname_list[i]);
852 			rport = fc_find_rport_by_wwpn(shost, wwpn);
853 			if (rport &&
854 			    (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
855 			     rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
856 				if (rport == attach_rport)
857 					continue;
858 				fc_cn_stats_update(event_type,
859 						   &rport->fpin_stats);
860 			}
861 		}
862 	}
863 }
864 
865 /*
866  * fc_fpin_congn_stats_update - routine to update Congestion
867  * event statistics.
868  * @shost:		host the FPIN was received on
869  * @tlv:		pointer to congestion descriptor
870  *
871  */
872 static void
873 fc_fpin_congn_stats_update(struct Scsi_Host *shost,
874 			   struct fc_tlv_desc *tlv)
875 {
876 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
877 	struct fc_fn_congn_desc *congn = (struct fc_fn_congn_desc *)tlv;
878 
879 	fc_cn_stats_update(be16_to_cpu(congn->event_type),
880 			   &fc_host->fpin_stats);
881 }
882 
883 /**
884  * fc_host_fpin_rcv - routine to process a received FPIN.
885  * @shost:		host the FPIN was received on
886  * @fpin_len:		length of FPIN payload, in bytes
887  * @fpin_buf:		pointer to FPIN payload
888  * @event_acknowledge:	1, if LLDD handles this event.
889  * Notes:
890  *	This routine assumes no locks are held on entry.
891  */
892 void
893 fc_host_fpin_rcv(struct Scsi_Host *shost, u32 fpin_len, char *fpin_buf,
894 		u8 event_acknowledge)
895 {
896 	struct fc_els_fpin *fpin = (struct fc_els_fpin *)fpin_buf;
897 	struct fc_tlv_desc *tlv;
898 	u32 bytes_remain;
899 	u32 dtag;
900 	enum fc_host_event_code event_code =
901 		event_acknowledge ? FCH_EVT_LINK_FPIN_ACK : FCH_EVT_LINK_FPIN;
902 
903 	/* Update Statistics */
904 	tlv = (struct fc_tlv_desc *)&fpin->fpin_desc[0];
905 	bytes_remain = fpin_len - offsetof(struct fc_els_fpin, fpin_desc);
906 	bytes_remain = min_t(u32, bytes_remain, be32_to_cpu(fpin->desc_len));
907 
908 	while (bytes_remain >= FC_TLV_DESC_HDR_SZ &&
909 	       bytes_remain >= FC_TLV_DESC_SZ_FROM_LENGTH(tlv)) {
910 		dtag = be32_to_cpu(tlv->desc_tag);
911 		switch (dtag) {
912 		case ELS_DTAG_LNK_INTEGRITY:
913 			fc_fpin_li_stats_update(shost, tlv);
914 			break;
915 		case ELS_DTAG_DELIVERY:
916 			fc_fpin_delivery_stats_update(shost, tlv);
917 			break;
918 		case ELS_DTAG_PEER_CONGEST:
919 			fc_fpin_peer_congn_stats_update(shost, tlv);
920 			break;
921 		case ELS_DTAG_CONGESTION:
922 			fc_fpin_congn_stats_update(shost, tlv);
923 		}
924 
925 		bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
926 		tlv = fc_tlv_next_desc(tlv);
927 	}
928 
929 	fc_host_post_fc_event(shost, fc_get_event_number(),
930 				event_code, fpin_len, fpin_buf, 0);
931 }
932 EXPORT_SYMBOL(fc_host_fpin_rcv);
933 
934 
935 static __init int fc_transport_init(void)
936 {
937 	int error;
938 
939 	atomic_set(&fc_event_seq, 0);
940 
941 	error = transport_class_register(&fc_host_class);
942 	if (error)
943 		return error;
944 	error = transport_class_register(&fc_vport_class);
945 	if (error)
946 		goto unreg_host_class;
947 	error = transport_class_register(&fc_rport_class);
948 	if (error)
949 		goto unreg_vport_class;
950 	error = transport_class_register(&fc_transport_class);
951 	if (error)
952 		goto unreg_rport_class;
953 	return 0;
954 
955 unreg_rport_class:
956 	transport_class_unregister(&fc_rport_class);
957 unreg_vport_class:
958 	transport_class_unregister(&fc_vport_class);
959 unreg_host_class:
960 	transport_class_unregister(&fc_host_class);
961 	return error;
962 }
963 
964 static void __exit fc_transport_exit(void)
965 {
966 	transport_class_unregister(&fc_transport_class);
967 	transport_class_unregister(&fc_rport_class);
968 	transport_class_unregister(&fc_host_class);
969 	transport_class_unregister(&fc_vport_class);
970 }
971 
972 /*
973  * FC Remote Port Attribute Management
974  */
975 
976 #define fc_rport_show_function(field, format_string, sz, cast)		\
977 static ssize_t								\
978 show_fc_rport_##field (struct device *dev, 				\
979 		       struct device_attribute *attr, char *buf)	\
980 {									\
981 	struct fc_rport *rport = transport_class_to_rport(dev);		\
982 	struct Scsi_Host *shost = rport_to_shost(rport);		\
983 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
984 	if ((i->f->get_rport_##field) &&				\
985 	    !((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
986 	      (rport->port_state == FC_PORTSTATE_DELETED) ||		\
987 	      (rport->port_state == FC_PORTSTATE_NOTPRESENT)))		\
988 		i->f->get_rport_##field(rport);				\
989 	return snprintf(buf, sz, format_string, cast rport->field); 	\
990 }
991 
992 #define fc_rport_store_function(field)					\
993 static ssize_t								\
994 store_fc_rport_##field(struct device *dev,				\
995 		       struct device_attribute *attr,			\
996 		       const char *buf,	size_t count)			\
997 {									\
998 	int val;							\
999 	struct fc_rport *rport = transport_class_to_rport(dev);		\
1000 	struct Scsi_Host *shost = rport_to_shost(rport);		\
1001 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1002 	char *cp;							\
1003 	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
1004 	    (rport->port_state == FC_PORTSTATE_DELETED) ||		\
1005 	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))		\
1006 		return -EBUSY;						\
1007 	val = simple_strtoul(buf, &cp, 0);				\
1008 	if (*cp && (*cp != '\n'))					\
1009 		return -EINVAL;						\
1010 	i->f->set_rport_##field(rport, val);				\
1011 	return count;							\
1012 }
1013 
1014 #define fc_rport_rd_attr(field, format_string, sz)			\
1015 	fc_rport_show_function(field, format_string, sz, )		\
1016 static FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
1017 			 show_fc_rport_##field, NULL)
1018 
1019 #define fc_rport_rd_attr_cast(field, format_string, sz, cast)		\
1020 	fc_rport_show_function(field, format_string, sz, (cast))	\
1021 static FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
1022 			  show_fc_rport_##field, NULL)
1023 
1024 #define fc_rport_rw_attr(field, format_string, sz)			\
1025 	fc_rport_show_function(field, format_string, sz, )		\
1026 	fc_rport_store_function(field)					\
1027 static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR,		\
1028 			show_fc_rport_##field,				\
1029 			store_fc_rport_##field)
1030 
1031 
1032 #define fc_private_rport_show_function(field, format_string, sz, cast)	\
1033 static ssize_t								\
1034 show_fc_rport_##field (struct device *dev, 				\
1035 		       struct device_attribute *attr, char *buf)	\
1036 {									\
1037 	struct fc_rport *rport = transport_class_to_rport(dev);		\
1038 	return snprintf(buf, sz, format_string, cast rport->field); 	\
1039 }
1040 
1041 #define fc_private_rport_rd_attr(field, format_string, sz)		\
1042 	fc_private_rport_show_function(field, format_string, sz, )	\
1043 static FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
1044 			 show_fc_rport_##field, NULL)
1045 
1046 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast)	\
1047 	fc_private_rport_show_function(field, format_string, sz, (cast)) \
1048 static FC_DEVICE_ATTR(rport, field, S_IRUGO,			\
1049 			  show_fc_rport_##field, NULL)
1050 
1051 
1052 #define fc_private_rport_rd_enum_attr(title, maxlen)			\
1053 static ssize_t								\
1054 show_fc_rport_##title (struct device *dev,				\
1055 		       struct device_attribute *attr, char *buf)	\
1056 {									\
1057 	struct fc_rport *rport = transport_class_to_rport(dev);		\
1058 	const char *name;						\
1059 	name = get_fc_##title##_name(rport->title);			\
1060 	if (!name)							\
1061 		return -EINVAL;						\
1062 	return snprintf(buf, maxlen, "%s\n", name);			\
1063 }									\
1064 static FC_DEVICE_ATTR(rport, title, S_IRUGO,			\
1065 			show_fc_rport_##title, NULL)
1066 
1067 
1068 #define SETUP_RPORT_ATTRIBUTE_RD(field)					\
1069 	i->private_rport_attrs[count] = device_attr_rport_##field; \
1070 	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
1071 	i->private_rport_attrs[count].store = NULL;			\
1072 	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
1073 	if (i->f->show_rport_##field)					\
1074 		count++
1075 
1076 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field)				\
1077 	i->private_rport_attrs[count] = device_attr_rport_##field; \
1078 	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
1079 	i->private_rport_attrs[count].store = NULL;			\
1080 	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
1081 	count++
1082 
1083 #define SETUP_RPORT_ATTRIBUTE_RW(field)					\
1084 	i->private_rport_attrs[count] = device_attr_rport_##field; \
1085 	if (!i->f->set_rport_##field) {					\
1086 		i->private_rport_attrs[count].attr.mode = S_IRUGO;	\
1087 		i->private_rport_attrs[count].store = NULL;		\
1088 	}								\
1089 	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
1090 	if (i->f->show_rport_##field)					\
1091 		count++
1092 
1093 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field)				\
1094 {									\
1095 	i->private_rport_attrs[count] = device_attr_rport_##field; \
1096 	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
1097 	count++;							\
1098 }
1099 
1100 
1101 /* The FC Transport Remote Port Attributes: */
1102 
1103 /* Fixed Remote Port Attributes */
1104 
1105 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
1106 
1107 static ssize_t
1108 show_fc_rport_supported_classes (struct device *dev,
1109 				 struct device_attribute *attr, char *buf)
1110 {
1111 	struct fc_rport *rport = transport_class_to_rport(dev);
1112 	if (rport->supported_classes == FC_COS_UNSPECIFIED)
1113 		return snprintf(buf, 20, "unspecified\n");
1114 	return get_fc_cos_names(rport->supported_classes, buf);
1115 }
1116 static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
1117 		show_fc_rport_supported_classes, NULL);
1118 
1119 /* Dynamic Remote Port Attributes */
1120 
1121 /*
1122  * dev_loss_tmo attribute
1123  */
1124 static int fc_str_to_dev_loss(const char *buf, unsigned long *val)
1125 {
1126 	char *cp;
1127 
1128 	*val = simple_strtoul(buf, &cp, 0);
1129 	if (*cp && (*cp != '\n'))
1130 		return -EINVAL;
1131 	/*
1132 	 * Check for overflow; dev_loss_tmo is u32
1133 	 */
1134 	if (*val > UINT_MAX)
1135 		return -EINVAL;
1136 
1137 	return 0;
1138 }
1139 
1140 static int fc_rport_set_dev_loss_tmo(struct fc_rport *rport,
1141 				     unsigned long val)
1142 {
1143 	struct Scsi_Host *shost = rport_to_shost(rport);
1144 	struct fc_internal *i = to_fc_internal(shost->transportt);
1145 
1146 	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
1147 	    (rport->port_state == FC_PORTSTATE_DELETED) ||
1148 	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
1149 		return -EBUSY;
1150 	/*
1151 	 * Check for overflow; dev_loss_tmo is u32
1152 	 */
1153 	if (val > UINT_MAX)
1154 		return -EINVAL;
1155 
1156 	/*
1157 	 * If fast_io_fail is off we have to cap
1158 	 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
1159 	 */
1160 	if (rport->fast_io_fail_tmo == -1 &&
1161 	    val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
1162 		return -EINVAL;
1163 
1164 	i->f->set_rport_dev_loss_tmo(rport, val);
1165 	return 0;
1166 }
1167 
1168 fc_rport_show_function(dev_loss_tmo, "%u\n", 20, )
1169 static ssize_t
1170 store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
1171 			    const char *buf, size_t count)
1172 {
1173 	struct fc_rport *rport = transport_class_to_rport(dev);
1174 	unsigned long val;
1175 	int rc;
1176 
1177 	rc = fc_str_to_dev_loss(buf, &val);
1178 	if (rc)
1179 		return rc;
1180 
1181 	rc = fc_rport_set_dev_loss_tmo(rport, val);
1182 	if (rc)
1183 		return rc;
1184 	return count;
1185 }
1186 static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
1187 		show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
1188 
1189 
1190 /* Private Remote Port Attributes */
1191 
1192 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1193 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1194 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
1195 
1196 static ssize_t
1197 show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
1198 		     char *buf)
1199 {
1200 	struct fc_rport *rport = transport_class_to_rport(dev);
1201 
1202 	/* identify any roles that are port_id specific */
1203 	if ((rport->port_id != -1) &&
1204 	    (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
1205 					FC_WELLKNOWN_PORTID_MASK) {
1206 		switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
1207 		case FC_FPORT_PORTID:
1208 			return snprintf(buf, 30, "Fabric Port\n");
1209 		case FC_FABCTLR_PORTID:
1210 			return snprintf(buf, 30, "Fabric Controller\n");
1211 		case FC_DIRSRVR_PORTID:
1212 			return snprintf(buf, 30, "Directory Server\n");
1213 		case FC_TIMESRVR_PORTID:
1214 			return snprintf(buf, 30, "Time Server\n");
1215 		case FC_MGMTSRVR_PORTID:
1216 			return snprintf(buf, 30, "Management Server\n");
1217 		default:
1218 			return snprintf(buf, 30, "Unknown Fabric Entity\n");
1219 		}
1220 	} else {
1221 		if (rport->roles == FC_PORT_ROLE_UNKNOWN)
1222 			return snprintf(buf, 20, "unknown\n");
1223 		return get_fc_port_roles_names(rport->roles, buf);
1224 	}
1225 }
1226 static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
1227 		show_fc_rport_roles, NULL);
1228 
1229 static ssize_t fc_rport_set_marginal_state(struct device *dev,
1230 						struct device_attribute *attr,
1231 						const char *buf, size_t count)
1232 {
1233 	struct fc_rport *rport = transport_class_to_rport(dev);
1234 	enum fc_port_state port_state;
1235 	int ret = 0;
1236 
1237 	ret = get_fc_port_state_match(buf, &port_state);
1238 	if (ret)
1239 		return -EINVAL;
1240 	if (port_state == FC_PORTSTATE_MARGINAL) {
1241 		/*
1242 		 * Change the state to Marginal only if the
1243 		 * current rport state is Online
1244 		 * Allow only Online->Marginal
1245 		 */
1246 		if (rport->port_state == FC_PORTSTATE_ONLINE)
1247 			rport->port_state = port_state;
1248 		else if (port_state != rport->port_state)
1249 			return -EINVAL;
1250 	} else if (port_state == FC_PORTSTATE_ONLINE) {
1251 		/*
1252 		 * Change the state to Online only if the
1253 		 * current rport state is Marginal
1254 		 * Allow only Marginal->Online
1255 		 */
1256 		if (rport->port_state == FC_PORTSTATE_MARGINAL)
1257 			rport->port_state = port_state;
1258 		else if (port_state != rport->port_state)
1259 			return -EINVAL;
1260 	} else
1261 		return -EINVAL;
1262 	return count;
1263 }
1264 
1265 static ssize_t
1266 show_fc_rport_port_state(struct device *dev,
1267 				struct device_attribute *attr, char *buf)
1268 {
1269 	const char *name;
1270 	struct fc_rport *rport = transport_class_to_rport(dev);
1271 
1272 	name = get_fc_port_state_name(rport->port_state);
1273 	if (!name)
1274 		return -EINVAL;
1275 
1276 	return snprintf(buf, 20, "%s\n", name);
1277 }
1278 
1279 static FC_DEVICE_ATTR(rport, port_state, 0444 | 0200,
1280 			show_fc_rport_port_state, fc_rport_set_marginal_state);
1281 
1282 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
1283 
1284 /*
1285  * fast_io_fail_tmo attribute
1286  */
1287 static ssize_t
1288 show_fc_rport_fast_io_fail_tmo (struct device *dev,
1289 				struct device_attribute *attr, char *buf)
1290 {
1291 	struct fc_rport *rport = transport_class_to_rport(dev);
1292 
1293 	if (rport->fast_io_fail_tmo == -1)
1294 		return snprintf(buf, 5, "off\n");
1295 	return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
1296 }
1297 
1298 static ssize_t
1299 store_fc_rport_fast_io_fail_tmo(struct device *dev,
1300 				struct device_attribute *attr, const char *buf,
1301 				size_t count)
1302 {
1303 	int val;
1304 	char *cp;
1305 	struct fc_rport *rport = transport_class_to_rport(dev);
1306 
1307 	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
1308 	    (rport->port_state == FC_PORTSTATE_DELETED) ||
1309 	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
1310 		return -EBUSY;
1311 	if (strncmp(buf, "off", 3) == 0)
1312 		rport->fast_io_fail_tmo = -1;
1313 	else {
1314 		val = simple_strtoul(buf, &cp, 0);
1315 		if ((*cp && (*cp != '\n')) || (val < 0))
1316 			return -EINVAL;
1317 		/*
1318 		 * Cap fast_io_fail by dev_loss_tmo or
1319 		 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
1320 		 */
1321 		if ((val >= rport->dev_loss_tmo) ||
1322 		    (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
1323 			return -EINVAL;
1324 
1325 		rport->fast_io_fail_tmo = val;
1326 	}
1327 	return count;
1328 }
1329 static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
1330 	show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
1331 
1332 #define fc_rport_encryption(name)							\
1333 static ssize_t fc_rport_encinfo_##name(struct device *cd,				\
1334 				       struct device_attribute *attr,			\
1335 				       char *buf)					\
1336 {											\
1337 	struct fc_rport *rport = transport_class_to_rport(cd);				\
1338 	struct Scsi_Host *shost = rport_to_shost(rport);				\
1339 	struct fc_internal *i = to_fc_internal(shost->transportt);			\
1340 	struct fc_encryption_info *info;						\
1341 	ssize_t ret = -ENOENT;								\
1342 	u32 data;									\
1343 											\
1344 	if (i->f->get_fc_rport_enc_info) {						\
1345 		info = (i->f->get_fc_rport_enc_info)(rport);				\
1346 		if (info) {								\
1347 			data = info->name;						\
1348 			if (!strcmp(#name, "status")) {					\
1349 				ret = scnprintf(buf,					\
1350 						FC_RPORT_ENCRYPTION_STATUS_MAX_LEN,	\
1351 						"%s\n",					\
1352 						data ? "Encrypted" : "Unencrypted");	\
1353 			}								\
1354 		}									\
1355 	}										\
1356 	return ret;									\
1357 }											\
1358 static FC_DEVICE_ATTR(rport, encryption_##name, 0444, fc_rport_encinfo_##name, NULL)	\
1359 
1360 fc_rport_encryption(status);
1361 
1362 static struct attribute *fc_rport_encryption_attrs[] = {
1363 	&device_attr_rport_encryption_status.attr,
1364 	NULL
1365 };
1366 
1367 static struct attribute_group fc_rport_encryption_group = {
1368 	.name = "encryption",
1369 	.attrs = fc_rport_encryption_attrs,
1370 };
1371 
1372 #define fc_rport_fpin_statistic(name)					\
1373 static ssize_t fc_rport_fpinstat_##name(struct device *cd,		\
1374 				  struct device_attribute *attr,	\
1375 				  char *buf)				\
1376 {									\
1377 	struct fc_rport *rport = transport_class_to_rport(cd);		\
1378 									\
1379 	return snprintf(buf, 20, "0x%llx\n", rport->fpin_stats.name);	\
1380 }									\
1381 static FC_DEVICE_ATTR(rport, fpin_##name, 0444, fc_rport_fpinstat_##name, NULL)
1382 
1383 fc_rport_fpin_statistic(dn);
1384 fc_rport_fpin_statistic(dn_unknown);
1385 fc_rport_fpin_statistic(dn_timeout);
1386 fc_rport_fpin_statistic(dn_unable_to_route);
1387 fc_rport_fpin_statistic(dn_device_specific);
1388 fc_rport_fpin_statistic(cn);
1389 fc_rport_fpin_statistic(cn_clear);
1390 fc_rport_fpin_statistic(cn_lost_credit);
1391 fc_rport_fpin_statistic(cn_credit_stall);
1392 fc_rport_fpin_statistic(cn_oversubscription);
1393 fc_rport_fpin_statistic(cn_device_specific);
1394 fc_rport_fpin_statistic(li);
1395 fc_rport_fpin_statistic(li_failure_unknown);
1396 fc_rport_fpin_statistic(li_link_failure_count);
1397 fc_rport_fpin_statistic(li_loss_of_sync_count);
1398 fc_rport_fpin_statistic(li_loss_of_signals_count);
1399 fc_rport_fpin_statistic(li_prim_seq_err_count);
1400 fc_rport_fpin_statistic(li_invalid_tx_word_count);
1401 fc_rport_fpin_statistic(li_invalid_crc_count);
1402 fc_rport_fpin_statistic(li_device_specific);
1403 
1404 static struct attribute *fc_rport_statistics_attrs[] = {
1405 	&device_attr_rport_fpin_dn.attr,
1406 	&device_attr_rport_fpin_dn_unknown.attr,
1407 	&device_attr_rport_fpin_dn_timeout.attr,
1408 	&device_attr_rport_fpin_dn_unable_to_route.attr,
1409 	&device_attr_rport_fpin_dn_device_specific.attr,
1410 	&device_attr_rport_fpin_li.attr,
1411 	&device_attr_rport_fpin_li_failure_unknown.attr,
1412 	&device_attr_rport_fpin_li_link_failure_count.attr,
1413 	&device_attr_rport_fpin_li_loss_of_sync_count.attr,
1414 	&device_attr_rport_fpin_li_loss_of_signals_count.attr,
1415 	&device_attr_rport_fpin_li_prim_seq_err_count.attr,
1416 	&device_attr_rport_fpin_li_invalid_tx_word_count.attr,
1417 	&device_attr_rport_fpin_li_invalid_crc_count.attr,
1418 	&device_attr_rport_fpin_li_device_specific.attr,
1419 	&device_attr_rport_fpin_cn.attr,
1420 	&device_attr_rport_fpin_cn_clear.attr,
1421 	&device_attr_rport_fpin_cn_lost_credit.attr,
1422 	&device_attr_rport_fpin_cn_credit_stall.attr,
1423 	&device_attr_rport_fpin_cn_oversubscription.attr,
1424 	&device_attr_rport_fpin_cn_device_specific.attr,
1425 	NULL
1426 };
1427 
1428 static struct attribute_group fc_rport_statistics_group = {
1429 	.name = "statistics",
1430 	.attrs = fc_rport_statistics_attrs,
1431 };
1432 
1433 
1434 /*
1435  * FC SCSI Target Attribute Management
1436  */
1437 
1438 /*
1439  * Note: in the target show function we recognize when the remote
1440  *  port is in the hierarchy and do not allow the driver to get
1441  *  involved in sysfs functions. The driver only gets involved if
1442  *  it's the "old" style that doesn't use rports.
1443  */
1444 #define fc_starget_show_function(field, format_string, sz, cast)	\
1445 static ssize_t								\
1446 show_fc_starget_##field (struct device *dev, 				\
1447 			 struct device_attribute *attr, char *buf)	\
1448 {									\
1449 	struct scsi_target *starget = transport_class_to_starget(dev);	\
1450 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);	\
1451 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1452 	struct fc_rport *rport = starget_to_rport(starget);		\
1453 	if (rport)							\
1454 		fc_starget_##field(starget) = rport->field;		\
1455 	else if (i->f->get_starget_##field)				\
1456 		i->f->get_starget_##field(starget);			\
1457 	return snprintf(buf, sz, format_string, 			\
1458 		cast fc_starget_##field(starget)); 			\
1459 }
1460 
1461 #define fc_starget_rd_attr(field, format_string, sz)			\
1462 	fc_starget_show_function(field, format_string, sz, )		\
1463 static FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
1464 			 show_fc_starget_##field, NULL)
1465 
1466 #define fc_starget_rd_attr_cast(field, format_string, sz, cast)		\
1467 	fc_starget_show_function(field, format_string, sz, (cast))	\
1468 static FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
1469 			  show_fc_starget_##field, NULL)
1470 
1471 #define SETUP_STARGET_ATTRIBUTE_RD(field)				\
1472 	i->private_starget_attrs[count] = device_attr_starget_##field; \
1473 	i->private_starget_attrs[count].attr.mode = S_IRUGO;		\
1474 	i->private_starget_attrs[count].store = NULL;			\
1475 	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
1476 	if (i->f->show_starget_##field)					\
1477 		count++
1478 
1479 #define SETUP_STARGET_ATTRIBUTE_RW(field)				\
1480 	i->private_starget_attrs[count] = device_attr_starget_##field; \
1481 	if (!i->f->set_starget_##field) {				\
1482 		i->private_starget_attrs[count].attr.mode = S_IRUGO;	\
1483 		i->private_starget_attrs[count].store = NULL;		\
1484 	}								\
1485 	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
1486 	if (i->f->show_starget_##field)					\
1487 		count++
1488 
1489 /* The FC Transport SCSI Target Attributes: */
1490 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1491 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1492 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1493 
1494 
1495 /*
1496  * FC Virtual Port Attribute Management
1497  */
1498 
1499 #define fc_vport_show_function(field, format_string, sz, cast)		\
1500 static ssize_t								\
1501 show_fc_vport_##field (struct device *dev, 				\
1502 		       struct device_attribute *attr, char *buf)	\
1503 {									\
1504 	struct fc_vport *vport = transport_class_to_vport(dev);		\
1505 	struct Scsi_Host *shost = vport_to_shost(vport);		\
1506 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1507 	if ((i->f->get_vport_##field) &&				\
1508 	    !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)))	\
1509 		i->f->get_vport_##field(vport);				\
1510 	return snprintf(buf, sz, format_string, cast vport->field); 	\
1511 }
1512 
1513 #define fc_vport_store_function(field)					\
1514 static ssize_t								\
1515 store_fc_vport_##field(struct device *dev,				\
1516 		       struct device_attribute *attr,			\
1517 		       const char *buf,	size_t count)			\
1518 {									\
1519 	int val;							\
1520 	struct fc_vport *vport = transport_class_to_vport(dev);		\
1521 	struct Scsi_Host *shost = vport_to_shost(vport);		\
1522 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1523 	char *cp;							\
1524 	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))	\
1525 		return -EBUSY;						\
1526 	val = simple_strtoul(buf, &cp, 0);				\
1527 	if (*cp && (*cp != '\n'))					\
1528 		return -EINVAL;						\
1529 	i->f->set_vport_##field(vport, val);				\
1530 	return count;							\
1531 }
1532 
1533 #define fc_vport_store_str_function(field, slen)			\
1534 static ssize_t								\
1535 store_fc_vport_##field(struct device *dev,				\
1536 		       struct device_attribute *attr, 			\
1537 		       const char *buf,	size_t count)			\
1538 {									\
1539 	struct fc_vport *vport = transport_class_to_vport(dev);		\
1540 	struct Scsi_Host *shost = vport_to_shost(vport);		\
1541 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1542 	unsigned int cnt=count;						\
1543 									\
1544 	/* count may include a LF at end of string */			\
1545 	if (buf[cnt-1] == '\n')						\
1546 		cnt--;							\
1547 	if (cnt > ((slen) - 1))						\
1548 		return -EINVAL;						\
1549 	memcpy(vport->field, buf, cnt);					\
1550 	i->f->set_vport_##field(vport);					\
1551 	return count;							\
1552 }
1553 
1554 #define fc_vport_rd_attr(field, format_string, sz)			\
1555 	fc_vport_show_function(field, format_string, sz, )		\
1556 static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1557 			 show_fc_vport_##field, NULL)
1558 
1559 #define fc_vport_rd_attr_cast(field, format_string, sz, cast)		\
1560 	fc_vport_show_function(field, format_string, sz, (cast))	\
1561 static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1562 			  show_fc_vport_##field, NULL)
1563 
1564 #define fc_vport_rw_attr(field, format_string, sz)			\
1565 	fc_vport_show_function(field, format_string, sz, )		\
1566 	fc_vport_store_function(field)					\
1567 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1568 			show_fc_vport_##field,				\
1569 			store_fc_vport_##field)
1570 
1571 #define fc_private_vport_show_function(field, format_string, sz, cast)	\
1572 static ssize_t								\
1573 show_fc_vport_##field (struct device *dev,				\
1574 		       struct device_attribute *attr, char *buf)	\
1575 {									\
1576 	struct fc_vport *vport = transport_class_to_vport(dev);		\
1577 	return snprintf(buf, sz, format_string, cast vport->field); 	\
1578 }
1579 
1580 #define fc_private_vport_store_u32_function(field)			\
1581 static ssize_t								\
1582 store_fc_vport_##field(struct device *dev,				\
1583 		       struct device_attribute *attr,			\
1584 		       const char *buf,	size_t count)			\
1585 {									\
1586 	u32 val;							\
1587 	struct fc_vport *vport = transport_class_to_vport(dev);		\
1588 	char *cp;							\
1589 	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))		\
1590 		return -EBUSY;						\
1591 	val = simple_strtoul(buf, &cp, 0);				\
1592 	if (*cp && (*cp != '\n'))					\
1593 		return -EINVAL;						\
1594 	vport->field = val;						\
1595 	return count;							\
1596 }
1597 
1598 
1599 #define fc_private_vport_rd_attr(field, format_string, sz)		\
1600 	fc_private_vport_show_function(field, format_string, sz, )	\
1601 static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1602 			 show_fc_vport_##field, NULL)
1603 
1604 #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast)	\
1605 	fc_private_vport_show_function(field, format_string, sz, (cast)) \
1606 static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1607 			  show_fc_vport_##field, NULL)
1608 
1609 #define fc_private_vport_rw_u32_attr(field, format_string, sz)		\
1610 	fc_private_vport_show_function(field, format_string, sz, )	\
1611 	fc_private_vport_store_u32_function(field)			\
1612 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1613 			show_fc_vport_##field,				\
1614 			store_fc_vport_##field)
1615 
1616 
1617 #define fc_private_vport_rd_enum_attr(title, maxlen)			\
1618 static ssize_t								\
1619 show_fc_vport_##title (struct device *dev,				\
1620 		       struct device_attribute *attr,			\
1621 		       char *buf)					\
1622 {									\
1623 	struct fc_vport *vport = transport_class_to_vport(dev);		\
1624 	const char *name;						\
1625 	name = get_fc_##title##_name(vport->title);			\
1626 	if (!name)							\
1627 		return -EINVAL;						\
1628 	return snprintf(buf, maxlen, "%s\n", name);			\
1629 }									\
1630 static FC_DEVICE_ATTR(vport, title, S_IRUGO,			\
1631 			show_fc_vport_##title, NULL)
1632 
1633 
1634 #define SETUP_VPORT_ATTRIBUTE_RD(field)					\
1635 	i->private_vport_attrs[count] = device_attr_vport_##field; \
1636 	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
1637 	i->private_vport_attrs[count].store = NULL;			\
1638 	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1639 	if (i->f->get_##field)						\
1640 		count++
1641 	/* NOTE: Above MACRO differs: checks function not show bit */
1642 
1643 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field)				\
1644 	i->private_vport_attrs[count] = device_attr_vport_##field; \
1645 	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
1646 	i->private_vport_attrs[count].store = NULL;			\
1647 	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1648 	count++
1649 
1650 #define SETUP_VPORT_ATTRIBUTE_WR(field)					\
1651 	i->private_vport_attrs[count] = device_attr_vport_##field; \
1652 	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1653 	if (i->f->field)						\
1654 		count++
1655 	/* NOTE: Above MACRO differs: checks function */
1656 
1657 #define SETUP_VPORT_ATTRIBUTE_RW(field)					\
1658 	i->private_vport_attrs[count] = device_attr_vport_##field; \
1659 	if (!i->f->set_vport_##field) {					\
1660 		i->private_vport_attrs[count].attr.mode = S_IRUGO;	\
1661 		i->private_vport_attrs[count].store = NULL;		\
1662 	}								\
1663 	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1664 	count++
1665 	/* NOTE: Above MACRO differs: does not check show bit */
1666 
1667 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field)				\
1668 {									\
1669 	i->private_vport_attrs[count] = device_attr_vport_##field; \
1670 	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1671 	count++;							\
1672 }
1673 
1674 
1675 /* The FC Transport Virtual Port Attributes: */
1676 
1677 /* Fixed Virtual Port Attributes */
1678 
1679 /* Dynamic Virtual Port Attributes */
1680 
1681 /* Private Virtual Port Attributes */
1682 
1683 fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1684 fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1685 fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1686 fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1687 
1688 static ssize_t
1689 show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1690 		     char *buf)
1691 {
1692 	struct fc_vport *vport = transport_class_to_vport(dev);
1693 
1694 	if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1695 		return snprintf(buf, 20, "unknown\n");
1696 	return get_fc_port_roles_names(vport->roles, buf);
1697 }
1698 static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1699 
1700 fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1701 
1702 fc_private_vport_show_function(symbolic_name, "%s\n",
1703 		FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1704 fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1705 static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1706 		show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1707 
1708 static ssize_t
1709 store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1710 		      const char *buf, size_t count)
1711 {
1712 	struct fc_vport *vport = transport_class_to_vport(dev);
1713 	struct Scsi_Host *shost = vport_to_shost(vport);
1714 	unsigned long flags;
1715 
1716 	spin_lock_irqsave(shost->host_lock, flags);
1717 	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
1718 		spin_unlock_irqrestore(shost->host_lock, flags);
1719 		return -EBUSY;
1720 	}
1721 	vport->flags |= FC_VPORT_DELETING;
1722 	spin_unlock_irqrestore(shost->host_lock, flags);
1723 
1724 	fc_queue_work(shost, &vport->vport_delete_work);
1725 	return count;
1726 }
1727 static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1728 			NULL, store_fc_vport_delete);
1729 
1730 
1731 /*
1732  * Enable/Disable vport
1733  *  Write "1" to disable, write "0" to enable
1734  */
1735 static ssize_t
1736 store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1737 		       const char *buf,
1738 			   size_t count)
1739 {
1740 	struct fc_vport *vport = transport_class_to_vport(dev);
1741 	struct Scsi_Host *shost = vport_to_shost(vport);
1742 	struct fc_internal *i = to_fc_internal(shost->transportt);
1743 	int stat;
1744 
1745 	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1746 		return -EBUSY;
1747 
1748 	if (*buf == '0') {
1749 		if (vport->vport_state != FC_VPORT_DISABLED)
1750 			return -EALREADY;
1751 	} else if (*buf == '1') {
1752 		if (vport->vport_state == FC_VPORT_DISABLED)
1753 			return -EALREADY;
1754 	} else
1755 		return -EINVAL;
1756 
1757 	stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1758 	return stat ? stat : count;
1759 }
1760 static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1761 			NULL, store_fc_vport_disable);
1762 
1763 
1764 /*
1765  * Host Attribute Management
1766  */
1767 
1768 #define fc_host_show_function(field, format_string, sz, cast)		\
1769 static ssize_t								\
1770 show_fc_host_##field (struct device *dev,				\
1771 		      struct device_attribute *attr, char *buf)		\
1772 {									\
1773 	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1774 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1775 	if (i->f->get_host_##field)					\
1776 		i->f->get_host_##field(shost);				\
1777 	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1778 }
1779 
1780 #define fc_host_store_function(field)					\
1781 static ssize_t								\
1782 store_fc_host_##field(struct device *dev, 				\
1783 		      struct device_attribute *attr,			\
1784 		      const char *buf,	size_t count)			\
1785 {									\
1786 	int val;							\
1787 	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1788 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1789 	char *cp;							\
1790 									\
1791 	val = simple_strtoul(buf, &cp, 0);				\
1792 	if (*cp && (*cp != '\n'))					\
1793 		return -EINVAL;						\
1794 	i->f->set_host_##field(shost, val);				\
1795 	return count;							\
1796 }
1797 
1798 #define fc_host_store_str_function(field, slen)				\
1799 static ssize_t								\
1800 store_fc_host_##field(struct device *dev,				\
1801 		      struct device_attribute *attr,			\
1802 		      const char *buf, size_t count)			\
1803 {									\
1804 	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1805 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1806 	unsigned int cnt=count;						\
1807 									\
1808 	/* count may include a LF at end of string */			\
1809 	if (buf[cnt-1] == '\n')						\
1810 		cnt--;							\
1811 	if (cnt > ((slen) - 1))						\
1812 		return -EINVAL;						\
1813 	memcpy(fc_host_##field(shost), buf, cnt);			\
1814 	i->f->set_host_##field(shost);					\
1815 	return count;							\
1816 }
1817 
1818 #define fc_host_rd_attr(field, format_string, sz)			\
1819 	fc_host_show_function(field, format_string, sz, )		\
1820 static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
1821 			 show_fc_host_##field, NULL)
1822 
1823 #define fc_host_rd_attr_cast(field, format_string, sz, cast)		\
1824 	fc_host_show_function(field, format_string, sz, (cast))		\
1825 static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
1826 			  show_fc_host_##field, NULL)
1827 
1828 #define fc_host_rw_attr(field, format_string, sz)			\
1829 	fc_host_show_function(field, format_string, sz, )		\
1830 	fc_host_store_function(field)					\
1831 static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,		\
1832 			show_fc_host_##field,				\
1833 			store_fc_host_##field)
1834 
1835 #define fc_host_rd_enum_attr(title, maxlen)				\
1836 static ssize_t								\
1837 show_fc_host_##title (struct device *dev,				\
1838 		      struct device_attribute *attr, char *buf)		\
1839 {									\
1840 	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1841 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1842 	const char *name;						\
1843 	if (i->f->get_host_##title)					\
1844 		i->f->get_host_##title(shost);				\
1845 	name = get_fc_##title##_name(fc_host_##title(shost));		\
1846 	if (!name)							\
1847 		return -EINVAL;						\
1848 	return snprintf(buf, maxlen, "%s\n", name);			\
1849 }									\
1850 static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1851 
1852 #define SETUP_HOST_ATTRIBUTE_RD(field)					\
1853 	i->private_host_attrs[count] = device_attr_host_##field;	\
1854 	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
1855 	i->private_host_attrs[count].store = NULL;			\
1856 	i->host_attrs[count] = &i->private_host_attrs[count];		\
1857 	if (i->f->show_host_##field)					\
1858 		count++
1859 
1860 #define SETUP_HOST_ATTRIBUTE_RD_NS(field)				\
1861 	i->private_host_attrs[count] = device_attr_host_##field;	\
1862 	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
1863 	i->private_host_attrs[count].store = NULL;			\
1864 	i->host_attrs[count] = &i->private_host_attrs[count];		\
1865 	count++
1866 
1867 #define SETUP_HOST_ATTRIBUTE_RW(field)					\
1868 	i->private_host_attrs[count] = device_attr_host_##field;	\
1869 	if (!i->f->set_host_##field) {					\
1870 		i->private_host_attrs[count].attr.mode = S_IRUGO;	\
1871 		i->private_host_attrs[count].store = NULL;		\
1872 	}								\
1873 	i->host_attrs[count] = &i->private_host_attrs[count];		\
1874 	if (i->f->show_host_##field)					\
1875 		count++
1876 
1877 
1878 #define fc_private_host_show_function(field, format_string, sz, cast)	\
1879 static ssize_t								\
1880 show_fc_host_##field (struct device *dev,				\
1881 		      struct device_attribute *attr, char *buf)		\
1882 {									\
1883 	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1884 	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1885 }
1886 
1887 #define fc_private_host_rd_attr(field, format_string, sz)		\
1888 	fc_private_host_show_function(field, format_string, sz, )	\
1889 static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
1890 			 show_fc_host_##field, NULL)
1891 
1892 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast)	\
1893 	fc_private_host_show_function(field, format_string, sz, (cast)) \
1894 static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
1895 			  show_fc_host_##field, NULL)
1896 
1897 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)			\
1898 	i->private_host_attrs[count] = device_attr_host_##field;	\
1899 	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
1900 	i->private_host_attrs[count].store = NULL;			\
1901 	i->host_attrs[count] = &i->private_host_attrs[count];		\
1902 	count++
1903 
1904 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)			\
1905 {									\
1906 	i->private_host_attrs[count] = device_attr_host_##field;	\
1907 	i->host_attrs[count] = &i->private_host_attrs[count];		\
1908 	count++;							\
1909 }
1910 
1911 
1912 /* Fixed Host Attributes */
1913 
1914 static ssize_t
1915 show_fc_host_supported_classes (struct device *dev,
1916 			        struct device_attribute *attr, char *buf)
1917 {
1918 	struct Scsi_Host *shost = transport_class_to_shost(dev);
1919 
1920 	if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1921 		return snprintf(buf, 20, "unspecified\n");
1922 
1923 	return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1924 }
1925 static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1926 		show_fc_host_supported_classes, NULL);
1927 
1928 static ssize_t
1929 show_fc_host_supported_fc4s (struct device *dev,
1930 			     struct device_attribute *attr, char *buf)
1931 {
1932 	struct Scsi_Host *shost = transport_class_to_shost(dev);
1933 	return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1934 }
1935 static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1936 		show_fc_host_supported_fc4s, NULL);
1937 
1938 static ssize_t
1939 show_fc_host_supported_speeds (struct device *dev,
1940 			       struct device_attribute *attr, char *buf)
1941 {
1942 	struct Scsi_Host *shost = transport_class_to_shost(dev);
1943 
1944 	if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1945 		return snprintf(buf, 20, "unknown\n");
1946 
1947 	return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1948 }
1949 static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1950 		show_fc_host_supported_speeds, NULL);
1951 
1952 
1953 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1954 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1955 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1956 			     unsigned long long);
1957 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1958 fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
1959 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1960 fc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1);
1961 fc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1962 fc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1963 fc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1964 fc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1965 fc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1966 fc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1967 
1968 
1969 /* Dynamic Host Attributes */
1970 
1971 static ssize_t
1972 show_fc_host_active_fc4s (struct device *dev,
1973 			  struct device_attribute *attr, char *buf)
1974 {
1975 	struct Scsi_Host *shost = transport_class_to_shost(dev);
1976 	struct fc_internal *i = to_fc_internal(shost->transportt);
1977 
1978 	if (i->f->get_host_active_fc4s)
1979 		i->f->get_host_active_fc4s(shost);
1980 
1981 	return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1982 }
1983 static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1984 		show_fc_host_active_fc4s, NULL);
1985 
1986 static ssize_t
1987 show_fc_host_speed (struct device *dev,
1988 		    struct device_attribute *attr, char *buf)
1989 {
1990 	struct Scsi_Host *shost = transport_class_to_shost(dev);
1991 	struct fc_internal *i = to_fc_internal(shost->transportt);
1992 
1993 	if (i->f->get_host_speed)
1994 		i->f->get_host_speed(shost);
1995 
1996 	if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1997 		return snprintf(buf, 20, "unknown\n");
1998 
1999 	return get_fc_port_speed_names(fc_host_speed(shost), buf);
2000 }
2001 static FC_DEVICE_ATTR(host, speed, S_IRUGO,
2002 		show_fc_host_speed, NULL);
2003 
2004 
2005 fc_host_rd_attr(port_id, "0x%06x\n", 20);
2006 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
2007 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
2008 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
2009 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
2010 
2011 fc_private_host_show_function(system_hostname, "%s\n",
2012 		FC_SYMBOLIC_NAME_SIZE + 1, )
2013 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
2014 static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
2015 		show_fc_host_system_hostname, store_fc_host_system_hostname);
2016 
2017 
2018 /* Private Host Attributes */
2019 
2020 static ssize_t
2021 show_fc_private_host_tgtid_bind_type(struct device *dev,
2022 				     struct device_attribute *attr, char *buf)
2023 {
2024 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2025 	const char *name;
2026 
2027 	name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
2028 	if (!name)
2029 		return -EINVAL;
2030 	return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
2031 }
2032 
2033 #define get_list_head_entry(pos, head, member) 		\
2034 	pos = list_entry((head)->next, typeof(*pos), member)
2035 
2036 static ssize_t
2037 store_fc_private_host_tgtid_bind_type(struct device *dev,
2038 	struct device_attribute *attr, const char *buf, size_t count)
2039 {
2040 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2041 	struct fc_rport *rport;
2042  	enum fc_tgtid_binding_type val;
2043 	unsigned long flags;
2044 
2045 	if (get_fc_tgtid_bind_type_match(buf, &val))
2046 		return -EINVAL;
2047 
2048 	/* if changing bind type, purge all unused consistent bindings */
2049 	if (val != fc_host_tgtid_bind_type(shost)) {
2050 		spin_lock_irqsave(shost->host_lock, flags);
2051 		while (!list_empty(&fc_host_rport_bindings(shost))) {
2052 			get_list_head_entry(rport,
2053 				&fc_host_rport_bindings(shost), peers);
2054 			list_del(&rport->peers);
2055 			rport->port_state = FC_PORTSTATE_DELETED;
2056 			fc_queue_work(shost, &rport->rport_delete_work);
2057 		}
2058 		spin_unlock_irqrestore(shost->host_lock, flags);
2059 	}
2060 
2061 	fc_host_tgtid_bind_type(shost) = val;
2062 	return count;
2063 }
2064 
2065 static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
2066 			show_fc_private_host_tgtid_bind_type,
2067 			store_fc_private_host_tgtid_bind_type);
2068 
2069 static ssize_t
2070 store_fc_private_host_issue_lip(struct device *dev,
2071 	struct device_attribute *attr, const char *buf, size_t count)
2072 {
2073 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2074 	struct fc_internal *i = to_fc_internal(shost->transportt);
2075 	int ret;
2076 
2077 	/* ignore any data value written to the attribute */
2078 	if (i->f->issue_fc_host_lip) {
2079 		ret = i->f->issue_fc_host_lip(shost);
2080 		return ret ? ret: count;
2081 	}
2082 
2083 	return -ENOENT;
2084 }
2085 
2086 static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
2087 			store_fc_private_host_issue_lip);
2088 
2089 static ssize_t
2090 store_fc_private_host_dev_loss_tmo(struct device *dev,
2091 				   struct device_attribute *attr,
2092 				   const char *buf, size_t count)
2093 {
2094 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2095 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2096 	struct fc_rport *rport;
2097 	unsigned long val, flags;
2098 	int rc;
2099 
2100 	rc = fc_str_to_dev_loss(buf, &val);
2101 	if (rc)
2102 		return rc;
2103 
2104 	fc_host_dev_loss_tmo(shost) = val;
2105 	spin_lock_irqsave(shost->host_lock, flags);
2106 	list_for_each_entry(rport, &fc_host->rports, peers)
2107 		fc_rport_set_dev_loss_tmo(rport, val);
2108 	spin_unlock_irqrestore(shost->host_lock, flags);
2109 	return count;
2110 }
2111 
2112 fc_private_host_show_function(dev_loss_tmo, "%d\n", 20, );
2113 static FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR,
2114 		      show_fc_host_dev_loss_tmo,
2115 		      store_fc_private_host_dev_loss_tmo);
2116 
2117 fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
2118 
2119 /*
2120  * Host Statistics Management
2121  */
2122 
2123 /* Show a given attribute in the statistics group */
2124 static ssize_t
2125 fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
2126 {
2127 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2128 	struct fc_internal *i = to_fc_internal(shost->transportt);
2129 	struct fc_host_statistics *stats;
2130 	ssize_t ret = -ENOENT;
2131 
2132 	if (offset > sizeof(struct fc_host_statistics) ||
2133 	    offset % sizeof(u64) != 0)
2134 		WARN_ON(1);
2135 
2136 	if (i->f->get_fc_host_stats) {
2137 		stats = (i->f->get_fc_host_stats)(shost);
2138 		if (stats)
2139 			ret = snprintf(buf, 20, "0x%llx\n",
2140 			      (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
2141 	}
2142 	return ret;
2143 }
2144 
2145 
2146 /* generate a read-only statistics attribute */
2147 #define fc_host_statistic(name)						\
2148 static ssize_t show_fcstat_##name(struct device *cd,			\
2149 				  struct device_attribute *attr,	\
2150 				  char *buf)				\
2151 {									\
2152 	return fc_stat_show(cd, buf, 					\
2153 			    offsetof(struct fc_host_statistics, name));	\
2154 }									\
2155 static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
2156 
2157 fc_host_statistic(seconds_since_last_reset);
2158 fc_host_statistic(tx_frames);
2159 fc_host_statistic(tx_words);
2160 fc_host_statistic(rx_frames);
2161 fc_host_statistic(rx_words);
2162 fc_host_statistic(lip_count);
2163 fc_host_statistic(nos_count);
2164 fc_host_statistic(error_frames);
2165 fc_host_statistic(dumped_frames);
2166 fc_host_statistic(link_failure_count);
2167 fc_host_statistic(loss_of_sync_count);
2168 fc_host_statistic(loss_of_signal_count);
2169 fc_host_statistic(prim_seq_protocol_err_count);
2170 fc_host_statistic(invalid_tx_word_count);
2171 fc_host_statistic(invalid_crc_count);
2172 fc_host_statistic(fcp_input_requests);
2173 fc_host_statistic(fcp_output_requests);
2174 fc_host_statistic(fcp_control_requests);
2175 fc_host_statistic(fcp_input_megabytes);
2176 fc_host_statistic(fcp_output_megabytes);
2177 fc_host_statistic(fcp_packet_alloc_failures);
2178 fc_host_statistic(fcp_packet_aborts);
2179 fc_host_statistic(fcp_frame_alloc_failures);
2180 fc_host_statistic(fc_no_free_exch);
2181 fc_host_statistic(fc_no_free_exch_xid);
2182 fc_host_statistic(fc_xid_not_found);
2183 fc_host_statistic(fc_xid_busy);
2184 fc_host_statistic(fc_seq_not_found);
2185 fc_host_statistic(fc_non_bls_resp);
2186 fc_host_statistic(cn_sig_warn);
2187 fc_host_statistic(cn_sig_alarm);
2188 
2189 
2190 #define fc_host_fpin_statistic(name)					\
2191 static ssize_t fc_host_fpinstat_##name(struct device *cd,		\
2192 				  struct device_attribute *attr,	\
2193 				  char *buf)				\
2194 {									\
2195 	struct Scsi_Host *shost = transport_class_to_shost(cd);		\
2196 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);	\
2197 									\
2198 	return snprintf(buf, 20, "0x%llx\n", fc_host->fpin_stats.name);	\
2199 }									\
2200 static FC_DEVICE_ATTR(host, fpin_##name, 0444, fc_host_fpinstat_##name, NULL)
2201 
2202 fc_host_fpin_statistic(dn);
2203 fc_host_fpin_statistic(dn_unknown);
2204 fc_host_fpin_statistic(dn_timeout);
2205 fc_host_fpin_statistic(dn_unable_to_route);
2206 fc_host_fpin_statistic(dn_device_specific);
2207 fc_host_fpin_statistic(cn);
2208 fc_host_fpin_statistic(cn_clear);
2209 fc_host_fpin_statistic(cn_lost_credit);
2210 fc_host_fpin_statistic(cn_credit_stall);
2211 fc_host_fpin_statistic(cn_oversubscription);
2212 fc_host_fpin_statistic(cn_device_specific);
2213 fc_host_fpin_statistic(li);
2214 fc_host_fpin_statistic(li_failure_unknown);
2215 fc_host_fpin_statistic(li_link_failure_count);
2216 fc_host_fpin_statistic(li_loss_of_sync_count);
2217 fc_host_fpin_statistic(li_loss_of_signals_count);
2218 fc_host_fpin_statistic(li_prim_seq_err_count);
2219 fc_host_fpin_statistic(li_invalid_tx_word_count);
2220 fc_host_fpin_statistic(li_invalid_crc_count);
2221 fc_host_fpin_statistic(li_device_specific);
2222 
2223 static ssize_t
2224 fc_reset_statistics(struct device *dev, struct device_attribute *attr,
2225 		    const char *buf, size_t count)
2226 {
2227 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2228 	struct fc_internal *i = to_fc_internal(shost->transportt);
2229 
2230 	/* ignore any data value written to the attribute */
2231 	if (i->f->reset_fc_host_stats) {
2232 		i->f->reset_fc_host_stats(shost);
2233 		return count;
2234 	}
2235 
2236 	return -ENOENT;
2237 }
2238 static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
2239 				fc_reset_statistics);
2240 
2241 static struct attribute *fc_statistics_attrs[] = {
2242 	&device_attr_host_seconds_since_last_reset.attr,
2243 	&device_attr_host_tx_frames.attr,
2244 	&device_attr_host_tx_words.attr,
2245 	&device_attr_host_rx_frames.attr,
2246 	&device_attr_host_rx_words.attr,
2247 	&device_attr_host_lip_count.attr,
2248 	&device_attr_host_nos_count.attr,
2249 	&device_attr_host_error_frames.attr,
2250 	&device_attr_host_dumped_frames.attr,
2251 	&device_attr_host_link_failure_count.attr,
2252 	&device_attr_host_loss_of_sync_count.attr,
2253 	&device_attr_host_loss_of_signal_count.attr,
2254 	&device_attr_host_prim_seq_protocol_err_count.attr,
2255 	&device_attr_host_invalid_tx_word_count.attr,
2256 	&device_attr_host_invalid_crc_count.attr,
2257 	&device_attr_host_fcp_input_requests.attr,
2258 	&device_attr_host_fcp_output_requests.attr,
2259 	&device_attr_host_fcp_control_requests.attr,
2260 	&device_attr_host_fcp_input_megabytes.attr,
2261 	&device_attr_host_fcp_output_megabytes.attr,
2262 	&device_attr_host_fcp_packet_alloc_failures.attr,
2263 	&device_attr_host_fcp_packet_aborts.attr,
2264 	&device_attr_host_fcp_frame_alloc_failures.attr,
2265 	&device_attr_host_fc_no_free_exch.attr,
2266 	&device_attr_host_fc_no_free_exch_xid.attr,
2267 	&device_attr_host_fc_xid_not_found.attr,
2268 	&device_attr_host_fc_xid_busy.attr,
2269 	&device_attr_host_fc_seq_not_found.attr,
2270 	&device_attr_host_fc_non_bls_resp.attr,
2271 	&device_attr_host_cn_sig_warn.attr,
2272 	&device_attr_host_cn_sig_alarm.attr,
2273 	&device_attr_host_reset_statistics.attr,
2274 	&device_attr_host_fpin_dn.attr,
2275 	&device_attr_host_fpin_dn_unknown.attr,
2276 	&device_attr_host_fpin_dn_timeout.attr,
2277 	&device_attr_host_fpin_dn_unable_to_route.attr,
2278 	&device_attr_host_fpin_dn_device_specific.attr,
2279 	&device_attr_host_fpin_li.attr,
2280 	&device_attr_host_fpin_li_failure_unknown.attr,
2281 	&device_attr_host_fpin_li_link_failure_count.attr,
2282 	&device_attr_host_fpin_li_loss_of_sync_count.attr,
2283 	&device_attr_host_fpin_li_loss_of_signals_count.attr,
2284 	&device_attr_host_fpin_li_prim_seq_err_count.attr,
2285 	&device_attr_host_fpin_li_invalid_tx_word_count.attr,
2286 	&device_attr_host_fpin_li_invalid_crc_count.attr,
2287 	&device_attr_host_fpin_li_device_specific.attr,
2288 	&device_attr_host_fpin_cn.attr,
2289 	&device_attr_host_fpin_cn_clear.attr,
2290 	&device_attr_host_fpin_cn_lost_credit.attr,
2291 	&device_attr_host_fpin_cn_credit_stall.attr,
2292 	&device_attr_host_fpin_cn_oversubscription.attr,
2293 	&device_attr_host_fpin_cn_device_specific.attr,
2294 	NULL
2295 };
2296 
2297 static struct attribute_group fc_statistics_group = {
2298 	.name = "statistics",
2299 	.attrs = fc_statistics_attrs,
2300 };
2301 
2302 
2303 /* Host Vport Attributes */
2304 
2305 static int
2306 fc_parse_wwn(const char *ns, u64 *nm)
2307 {
2308 	unsigned int i, j;
2309 	u8 wwn[8];
2310 
2311 	memset(wwn, 0, sizeof(wwn));
2312 
2313 	/* Validate and store the new name */
2314 	for (i=0, j=0; i < 16; i++) {
2315 		int value;
2316 
2317 		value = hex_to_bin(*ns++);
2318 		if (value >= 0)
2319 			j = (j << 4) | value;
2320 		else
2321 			return -EINVAL;
2322 		if (i % 2) {
2323 			wwn[i/2] = j & 0xff;
2324 			j = 0;
2325 		}
2326 	}
2327 
2328 	*nm = wwn_to_u64(wwn);
2329 
2330 	return 0;
2331 }
2332 
2333 
2334 /*
2335  * "Short-cut" sysfs variable to create a new vport on a FC Host.
2336  * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
2337  * will default to a NPIV-based FCP_Initiator; The WWNs are specified
2338  * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
2339  */
2340 static ssize_t
2341 store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
2342 			   const char *buf, size_t count)
2343 {
2344 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2345 	struct fc_vport_identifiers vid;
2346 	struct fc_vport *vport;
2347 	unsigned int cnt=count;
2348 	int stat;
2349 
2350 	memset(&vid, 0, sizeof(vid));
2351 
2352 	/* count may include a LF at end of string */
2353 	if (buf[cnt-1] == '\n')
2354 		cnt--;
2355 
2356 	/* validate we have enough characters for WWPN */
2357 	if ((cnt != (16+1+16)) || (buf[16] != ':'))
2358 		return -EINVAL;
2359 
2360 	stat = fc_parse_wwn(&buf[0], &vid.port_name);
2361 	if (stat)
2362 		return stat;
2363 
2364 	stat = fc_parse_wwn(&buf[17], &vid.node_name);
2365 	if (stat)
2366 		return stat;
2367 
2368 	vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
2369 	vid.vport_type = FC_PORTTYPE_NPIV;
2370 	/* vid.symbolic_name is already zero/NULL's */
2371 	vid.disable = false;		/* always enabled */
2372 
2373 	/* we only allow support on Channel 0 !!! */
2374 	stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
2375 	return stat ? stat : count;
2376 }
2377 static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
2378 			store_fc_host_vport_create);
2379 
2380 
2381 /*
2382  * "Short-cut" sysfs variable to delete a vport on a FC Host.
2383  * Vport is identified by a string containing "<WWPN>:<WWNN>".
2384  * The WWNs are specified as hex characters, and may *not* contain
2385  * any prefixes (e.g. 0x, x, etc)
2386  */
2387 static ssize_t
2388 store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
2389 			   const char *buf, size_t count)
2390 {
2391 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2392 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2393 	struct fc_vport *vport;
2394 	u64 wwpn, wwnn;
2395 	unsigned long flags;
2396 	unsigned int cnt=count;
2397 	int stat, match;
2398 
2399 	/* count may include a LF at end of string */
2400 	if (buf[cnt-1] == '\n')
2401 		cnt--;
2402 
2403 	/* validate we have enough characters for WWPN */
2404 	if ((cnt != (16+1+16)) || (buf[16] != ':'))
2405 		return -EINVAL;
2406 
2407 	stat = fc_parse_wwn(&buf[0], &wwpn);
2408 	if (stat)
2409 		return stat;
2410 
2411 	stat = fc_parse_wwn(&buf[17], &wwnn);
2412 	if (stat)
2413 		return stat;
2414 
2415 	spin_lock_irqsave(shost->host_lock, flags);
2416 	match = 0;
2417 	/* we only allow support on Channel 0 !!! */
2418 	list_for_each_entry(vport, &fc_host->vports, peers) {
2419 		if ((vport->channel == 0) &&
2420 		    (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
2421 			if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
2422 				break;
2423 			vport->flags |= FC_VPORT_DELETING;
2424 			match = 1;
2425 			break;
2426 		}
2427 	}
2428 	spin_unlock_irqrestore(shost->host_lock, flags);
2429 
2430 	if (!match)
2431 		return -ENODEV;
2432 
2433 	stat = fc_vport_terminate(vport);
2434 	return stat ? stat : count;
2435 }
2436 static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
2437 			store_fc_host_vport_delete);
2438 
2439 
2440 static int fc_host_match(struct attribute_container *cont,
2441 			  struct device *dev)
2442 {
2443 	struct Scsi_Host *shost;
2444 	struct fc_internal *i;
2445 
2446 	if (!scsi_is_host_device(dev))
2447 		return 0;
2448 
2449 	shost = dev_to_shost(dev);
2450 	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
2451 	    != &fc_host_class.class)
2452 		return 0;
2453 
2454 	i = to_fc_internal(shost->transportt);
2455 
2456 	return &i->t.host_attrs.ac == cont;
2457 }
2458 
2459 static int fc_target_match(struct attribute_container *cont,
2460 			    struct device *dev)
2461 {
2462 	struct Scsi_Host *shost;
2463 	struct fc_internal *i;
2464 
2465 	if (!scsi_is_target_device(dev))
2466 		return 0;
2467 
2468 	shost = dev_to_shost(dev->parent);
2469 	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
2470 	    != &fc_host_class.class)
2471 		return 0;
2472 
2473 	i = to_fc_internal(shost->transportt);
2474 
2475 	return &i->t.target_attrs.ac == cont;
2476 }
2477 
2478 static void fc_rport_dev_release(struct device *dev)
2479 {
2480 	struct fc_rport *rport = dev_to_rport(dev);
2481 	put_device(dev->parent);
2482 	kfree(rport);
2483 }
2484 
2485 int scsi_is_fc_rport(const struct device *dev)
2486 {
2487 	return dev->release == fc_rport_dev_release;
2488 }
2489 EXPORT_SYMBOL(scsi_is_fc_rport);
2490 
2491 static int fc_rport_match(struct attribute_container *cont,
2492 			    struct device *dev)
2493 {
2494 	struct Scsi_Host *shost;
2495 	struct fc_internal *i;
2496 
2497 	if (!scsi_is_fc_rport(dev))
2498 		return 0;
2499 
2500 	shost = dev_to_shost(dev->parent);
2501 	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
2502 	    != &fc_host_class.class)
2503 		return 0;
2504 
2505 	i = to_fc_internal(shost->transportt);
2506 
2507 	return &i->rport_attr_cont.ac == cont;
2508 }
2509 
2510 
2511 static void fc_vport_dev_release(struct device *dev)
2512 {
2513 	struct fc_vport *vport = dev_to_vport(dev);
2514 	put_device(dev->parent);		/* release kobj parent */
2515 	kfree(vport);
2516 }
2517 
2518 static int scsi_is_fc_vport(const struct device *dev)
2519 {
2520 	return dev->release == fc_vport_dev_release;
2521 }
2522 
2523 static int fc_vport_match(struct attribute_container *cont,
2524 			    struct device *dev)
2525 {
2526 	struct fc_vport *vport;
2527 	struct Scsi_Host *shost;
2528 	struct fc_internal *i;
2529 
2530 	if (!scsi_is_fc_vport(dev))
2531 		return 0;
2532 	vport = dev_to_vport(dev);
2533 
2534 	shost = vport_to_shost(vport);
2535 	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
2536 	    != &fc_host_class.class)
2537 		return 0;
2538 
2539 	i = to_fc_internal(shost->transportt);
2540 	return &i->vport_attr_cont.ac == cont;
2541 }
2542 
2543 
2544 /**
2545  * fc_eh_timed_out - FC Transport I/O timeout intercept handler
2546  * @scmd:	The SCSI command which timed out
2547  *
2548  * This routine protects against error handlers getting invoked while a
2549  * rport is in a blocked state, typically due to a temporarily loss of
2550  * connectivity. If the error handlers are allowed to proceed, requests
2551  * to abort i/o, reset the target, etc will likely fail as there is no way
2552  * to communicate with the device to perform the requested function. These
2553  * failures may result in the midlayer taking the device offline, requiring
2554  * manual intervention to restore operation.
2555  *
2556  * This routine, called whenever an i/o times out, validates the state of
2557  * the underlying rport. If the rport is blocked, it returns
2558  * EH_RESET_TIMER, which will continue to reschedule the timeout.
2559  * Eventually, either the device will return, or devloss_tmo will fire,
2560  * and when the timeout then fires, it will be handled normally.
2561  * If the rport is not blocked, normal error handling continues.
2562  *
2563  * Notes:
2564  *	This routine assumes no locks are held on entry.
2565  */
2566 enum scsi_timeout_action fc_eh_timed_out(struct scsi_cmnd *scmd)
2567 {
2568 	struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
2569 
2570 	if (rport->port_state == FC_PORTSTATE_BLOCKED)
2571 		return SCSI_EH_RESET_TIMER;
2572 
2573 	return SCSI_EH_NOT_HANDLED;
2574 }
2575 EXPORT_SYMBOL(fc_eh_timed_out);
2576 
2577 /*
2578  * Called by fc_user_scan to locate an rport on the shost that
2579  * matches the channel and target id, and invoke scsi_scan_target()
2580  * on the rport.
2581  */
2582 static void
2583 fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
2584 {
2585 	struct fc_rport *rport;
2586 	unsigned long flags;
2587 
2588 	spin_lock_irqsave(shost->host_lock, flags);
2589 
2590 	list_for_each_entry(rport, &fc_host_rports(shost), peers) {
2591 		if (rport->scsi_target_id == -1)
2592 			continue;
2593 
2594 		if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
2595 			(rport->port_state != FC_PORTSTATE_MARGINAL))
2596 			continue;
2597 
2598 		if ((channel == rport->channel) &&
2599 		    (id == rport->scsi_target_id)) {
2600 			spin_unlock_irqrestore(shost->host_lock, flags);
2601 			scsi_scan_target(&rport->dev, channel, id, lun,
2602 					 SCSI_SCAN_MANUAL);
2603 			return;
2604 		}
2605 	}
2606 
2607 	spin_unlock_irqrestore(shost->host_lock, flags);
2608 }
2609 
2610 /*
2611  * Called via sysfs scan routines. Necessary, as the FC transport
2612  * wants to place all target objects below the rport object. So this
2613  * routine must invoke the scsi_scan_target() routine with the rport
2614  * object as the parent.
2615  */
2616 static int
2617 fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
2618 {
2619 	uint chlo, chhi;
2620 	uint tgtlo, tgthi;
2621 
2622 	if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2623 	    ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2624 	    ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2625 		return -EINVAL;
2626 
2627 	if (channel == SCAN_WILD_CARD) {
2628 		chlo = 0;
2629 		chhi = shost->max_channel + 1;
2630 	} else {
2631 		chlo = channel;
2632 		chhi = channel + 1;
2633 	}
2634 
2635 	if (id == SCAN_WILD_CARD) {
2636 		tgtlo = 0;
2637 		tgthi = shost->max_id;
2638 	} else {
2639 		tgtlo = id;
2640 		tgthi = id + 1;
2641 	}
2642 
2643 	for ( ; chlo < chhi; chlo++)
2644 		for ( ; tgtlo < tgthi; tgtlo++)
2645 			fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2646 
2647 	return 0;
2648 }
2649 
2650 struct scsi_transport_template *
2651 fc_attach_transport(struct fc_function_template *ft)
2652 {
2653 	int count;
2654 	struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2655 					GFP_KERNEL);
2656 
2657 	if (unlikely(!i))
2658 		return NULL;
2659 
2660 	i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2661 	i->t.target_attrs.ac.class = &fc_transport_class.class;
2662 	i->t.target_attrs.ac.match = fc_target_match;
2663 	i->t.target_size = sizeof(struct fc_starget_attrs);
2664 	transport_container_register(&i->t.target_attrs);
2665 
2666 	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2667 	i->t.host_attrs.ac.class = &fc_host_class.class;
2668 	i->t.host_attrs.ac.match = fc_host_match;
2669 	i->t.host_size = sizeof(struct fc_host_attrs);
2670 	if (ft->get_fc_host_stats)
2671 		i->t.host_attrs.statistics = &fc_statistics_group;
2672 	transport_container_register(&i->t.host_attrs);
2673 
2674 	i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2675 	i->rport_attr_cont.ac.class = &fc_rport_class.class;
2676 	i->rport_attr_cont.ac.match = fc_rport_match;
2677 	if (ft->get_fc_rport_enc_info)
2678 		i->rport_attr_cont.encryption = &fc_rport_encryption_group;
2679 	i->rport_attr_cont.statistics = &fc_rport_statistics_group;
2680 	transport_container_register(&i->rport_attr_cont);
2681 
2682 	i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2683 	i->vport_attr_cont.ac.class = &fc_vport_class.class;
2684 	i->vport_attr_cont.ac.match = fc_vport_match;
2685 	transport_container_register(&i->vport_attr_cont);
2686 
2687 	i->f = ft;
2688 
2689 	/* Transport uses the shost workq for scsi scanning */
2690 	i->t.create_work_queue = 1;
2691 
2692 	i->t.user_scan = fc_user_scan;
2693 
2694 	/*
2695 	 * Setup SCSI Target Attributes.
2696 	 */
2697 	count = 0;
2698 	SETUP_STARGET_ATTRIBUTE_RD(node_name);
2699 	SETUP_STARGET_ATTRIBUTE_RD(port_name);
2700 	SETUP_STARGET_ATTRIBUTE_RD(port_id);
2701 
2702 	BUG_ON(count > FC_STARGET_NUM_ATTRS);
2703 
2704 	i->starget_attrs[count] = NULL;
2705 
2706 
2707 	/*
2708 	 * Setup SCSI Host Attributes.
2709 	 */
2710 	count=0;
2711 	SETUP_HOST_ATTRIBUTE_RD(node_name);
2712 	SETUP_HOST_ATTRIBUTE_RD(port_name);
2713 	SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
2714 	SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2715 	SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
2716 	SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2717 	SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2718 	if (ft->vport_create) {
2719 		SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2720 		SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2721 	}
2722 	SETUP_HOST_ATTRIBUTE_RD(serial_number);
2723 	SETUP_HOST_ATTRIBUTE_RD(manufacturer);
2724 	SETUP_HOST_ATTRIBUTE_RD(model);
2725 	SETUP_HOST_ATTRIBUTE_RD(model_description);
2726 	SETUP_HOST_ATTRIBUTE_RD(hardware_version);
2727 	SETUP_HOST_ATTRIBUTE_RD(driver_version);
2728 	SETUP_HOST_ATTRIBUTE_RD(firmware_version);
2729 	SETUP_HOST_ATTRIBUTE_RD(optionrom_version);
2730 
2731 	SETUP_HOST_ATTRIBUTE_RD(port_id);
2732 	SETUP_HOST_ATTRIBUTE_RD(port_type);
2733 	SETUP_HOST_ATTRIBUTE_RD(port_state);
2734 	SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2735 	SETUP_HOST_ATTRIBUTE_RD(speed);
2736 	SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2737 	SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2738 	SETUP_HOST_ATTRIBUTE_RW(system_hostname);
2739 
2740 	/* Transport-managed attributes */
2741 	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(dev_loss_tmo);
2742 	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
2743 	if (ft->issue_fc_host_lip)
2744 		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2745 	if (ft->vport_create)
2746 		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2747 	if (ft->vport_delete)
2748 		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
2749 
2750 	BUG_ON(count > FC_HOST_NUM_ATTRS);
2751 
2752 	i->host_attrs[count] = NULL;
2753 
2754 	/*
2755 	 * Setup Remote Port Attributes.
2756 	 */
2757 	count=0;
2758 	SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2759 	SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2760 	SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2761 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2762 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2763 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2764 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2765 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(port_state);
2766 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2767 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
2768 
2769 	BUG_ON(count > FC_RPORT_NUM_ATTRS);
2770 
2771 	i->rport_attrs[count] = NULL;
2772 
2773 	/*
2774 	 * Setup Virtual Port Attributes.
2775 	 */
2776 	count=0;
2777 	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2778 	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2779 	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2780 	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2781 	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2782 	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2783 	SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2784 	SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2785 	SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2786 
2787 	BUG_ON(count > FC_VPORT_NUM_ATTRS);
2788 
2789 	i->vport_attrs[count] = NULL;
2790 
2791 	return &i->t;
2792 }
2793 EXPORT_SYMBOL(fc_attach_transport);
2794 
2795 void fc_release_transport(struct scsi_transport_template *t)
2796 {
2797 	struct fc_internal *i = to_fc_internal(t);
2798 
2799 	transport_container_unregister(&i->t.target_attrs);
2800 	transport_container_unregister(&i->t.host_attrs);
2801 	transport_container_unregister(&i->rport_attr_cont);
2802 	transport_container_unregister(&i->vport_attr_cont);
2803 
2804 	kfree(i);
2805 }
2806 EXPORT_SYMBOL(fc_release_transport);
2807 
2808 /**
2809  * fc_queue_work - Queue work to the fc_host workqueue.
2810  * @shost:	Pointer to Scsi_Host bound to fc_host.
2811  * @work:	Work to queue for execution.
2812  *
2813  * Return value:
2814  * 	1 - work queued for execution
2815  *	0 - work is already queued
2816  *	-EINVAL - work queue doesn't exist
2817  */
2818 static int
2819 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2820 {
2821 	if (unlikely(!fc_host_work_q(shost))) {
2822 		printk(KERN_ERR
2823 			"ERROR: FC host '%s' attempted to queue work, "
2824 			"when no workqueue created.\n", shost->hostt->name);
2825 		dump_stack();
2826 
2827 		return -EINVAL;
2828 	}
2829 
2830 	return queue_work(fc_host_work_q(shost), work);
2831 }
2832 
2833 /**
2834  * fc_flush_work - Flush a fc_host's workqueue.
2835  * @shost:	Pointer to Scsi_Host bound to fc_host.
2836  */
2837 static void
2838 fc_flush_work(struct Scsi_Host *shost)
2839 {
2840 	if (!fc_host_work_q(shost)) {
2841 		printk(KERN_ERR
2842 			"ERROR: FC host '%s' attempted to flush work, "
2843 			"when no workqueue created.\n", shost->hostt->name);
2844 		dump_stack();
2845 		return;
2846 	}
2847 
2848 	flush_workqueue(fc_host_work_q(shost));
2849 }
2850 
2851 /**
2852  * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2853  * @shost:	Pointer to Scsi_Host bound to fc_host.
2854  * @rport:	rport associated with the devloss work
2855  * @work:	Work to queue for execution.
2856  * @delay:	jiffies to delay the work queuing
2857  *
2858  * Return value:
2859  * 	1 on success / 0 already queued / < 0 for error
2860  */
2861 static int
2862 fc_queue_devloss_work(struct Scsi_Host *shost, struct fc_rport *rport,
2863 		      struct delayed_work *work, unsigned long delay)
2864 {
2865 	if (unlikely(!rport->devloss_work_q)) {
2866 		printk(KERN_ERR
2867 			"ERROR: FC host '%s' attempted to queue work, "
2868 			"when no workqueue created.\n", shost->hostt->name);
2869 		dump_stack();
2870 
2871 		return -EINVAL;
2872 	}
2873 
2874 	return queue_delayed_work(rport->devloss_work_q, work, delay);
2875 }
2876 
2877 /**
2878  * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2879  * @shost:	Pointer to Scsi_Host bound to fc_host.
2880  * @rport:	rport associated with the devloss work
2881  */
2882 static void
2883 fc_flush_devloss(struct Scsi_Host *shost, struct fc_rport *rport)
2884 {
2885 	if (unlikely(!rport->devloss_work_q)) {
2886 		printk(KERN_ERR
2887 			"ERROR: FC host '%s' attempted to flush work, "
2888 			"when no workqueue created.\n", shost->hostt->name);
2889 		dump_stack();
2890 		return;
2891 	}
2892 
2893 	flush_workqueue(rport->devloss_work_q);
2894 }
2895 
2896 
2897 /**
2898  * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2899  * @shost:	Which &Scsi_Host
2900  *
2901  * This routine is expected to be called immediately preceding the
2902  * a driver's call to scsi_remove_host().
2903  *
2904  * WARNING: A driver utilizing the fc_transport, which fails to call
2905  *   this routine prior to scsi_remove_host(), will leave dangling
2906  *   objects in /sys/class/fc_remote_ports. Access to any of these
2907  *   objects can result in a system crash !!!
2908  *
2909  * Notes:
2910  *	This routine assumes no locks are held on entry.
2911  */
2912 void
2913 fc_remove_host(struct Scsi_Host *shost)
2914 {
2915 	struct fc_vport *vport = NULL, *next_vport = NULL;
2916 	struct fc_rport *rport = NULL, *next_rport = NULL;
2917 	struct workqueue_struct *work_q;
2918 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2919 	unsigned long flags;
2920 
2921 	spin_lock_irqsave(shost->host_lock, flags);
2922 
2923 	/* Remove any vports */
2924 	list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2925 		vport->flags |= FC_VPORT_DELETING;
2926 		fc_queue_work(shost, &vport->vport_delete_work);
2927 	}
2928 
2929 	/* Remove any remote ports */
2930 	list_for_each_entry_safe(rport, next_rport,
2931 			&fc_host->rports, peers) {
2932 		list_del(&rport->peers);
2933 		rport->port_state = FC_PORTSTATE_DELETED;
2934 		fc_queue_work(shost, &rport->rport_delete_work);
2935 	}
2936 
2937 	list_for_each_entry_safe(rport, next_rport,
2938 			&fc_host->rport_bindings, peers) {
2939 		list_del(&rport->peers);
2940 		rport->port_state = FC_PORTSTATE_DELETED;
2941 		fc_queue_work(shost, &rport->rport_delete_work);
2942 	}
2943 
2944 	spin_unlock_irqrestore(shost->host_lock, flags);
2945 
2946 	/* flush all scan work items */
2947 	scsi_flush_work(shost);
2948 
2949 	/* flush all stgt delete, and rport delete work items, then kill it  */
2950 	if (fc_host->work_q) {
2951 		work_q = fc_host->work_q;
2952 		fc_host->work_q = NULL;
2953 		destroy_workqueue(work_q);
2954 	}
2955 }
2956 EXPORT_SYMBOL(fc_remove_host);
2957 
2958 static void fc_terminate_rport_io(struct fc_rport *rport)
2959 {
2960 	struct Scsi_Host *shost = rport_to_shost(rport);
2961 	struct fc_internal *i = to_fc_internal(shost->transportt);
2962 
2963 	/* Involve the LLDD if possible to terminate all io on the rport. */
2964 	if (i->f->terminate_rport_io)
2965 		i->f->terminate_rport_io(rport);
2966 
2967 	/*
2968 	 * Must unblock to flush queued IO. scsi-ml will fail incoming reqs.
2969 	 */
2970 	scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
2971 }
2972 
2973 /**
2974  * fc_starget_delete - called to delete the scsi descendants of an rport
2975  * @work:	remote port to be operated on.
2976  *
2977  * Deletes target and all sdevs.
2978  */
2979 static void
2980 fc_starget_delete(struct work_struct *work)
2981 {
2982 	struct fc_rport *rport =
2983 		container_of(work, struct fc_rport, stgt_delete_work);
2984 
2985 	fc_terminate_rport_io(rport);
2986 	scsi_remove_target(&rport->dev);
2987 }
2988 
2989 
2990 /**
2991  * fc_rport_final_delete - finish rport termination and delete it.
2992  * @work:	remote port to be deleted.
2993  */
2994 static void
2995 fc_rport_final_delete(struct work_struct *work)
2996 {
2997 	struct fc_rport *rport =
2998 		container_of(work, struct fc_rport, rport_delete_work);
2999 	struct device *dev = &rport->dev;
3000 	struct Scsi_Host *shost = rport_to_shost(rport);
3001 	struct fc_internal *i = to_fc_internal(shost->transportt);
3002 	struct workqueue_struct *work_q;
3003 	unsigned long flags;
3004 	int do_callback = 0;
3005 
3006 	fc_terminate_rport_io(rport);
3007 
3008 	/*
3009 	 * if a scan is pending, flush the SCSI Host work_q so that
3010 	 * that we can reclaim the rport scan work element.
3011 	 */
3012 	if (rport->flags & FC_RPORT_SCAN_PENDING)
3013 		scsi_flush_work(shost);
3014 
3015 	/*
3016 	 * Cancel any outstanding timers. These should really exist
3017 	 * only when rmmod'ing the LLDD and we're asking for
3018 	 * immediate termination of the rports
3019 	 */
3020 	spin_lock_irqsave(shost->host_lock, flags);
3021 	if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
3022 		spin_unlock_irqrestore(shost->host_lock, flags);
3023 		if (!cancel_delayed_work(&rport->fail_io_work))
3024 			fc_flush_devloss(shost, rport);
3025 		if (!cancel_delayed_work(&rport->dev_loss_work))
3026 			fc_flush_devloss(shost, rport);
3027 		cancel_work_sync(&rport->scan_work);
3028 		spin_lock_irqsave(shost->host_lock, flags);
3029 		rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3030 	}
3031 	spin_unlock_irqrestore(shost->host_lock, flags);
3032 
3033 	/* Delete SCSI target and sdevs */
3034 	if (rport->scsi_target_id != -1)
3035 		fc_starget_delete(&rport->stgt_delete_work);
3036 
3037 	/*
3038 	 * Notify the driver that the rport is now dead. The LLDD will
3039 	 * also guarantee that any communication to the rport is terminated
3040 	 *
3041 	 * Avoid this call if we already called it when we preserved the
3042 	 * rport for the binding.
3043 	 */
3044 	spin_lock_irqsave(shost->host_lock, flags);
3045 	if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
3046 	    (i->f->dev_loss_tmo_callbk)) {
3047 		rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3048 		do_callback = 1;
3049 	}
3050 	spin_unlock_irqrestore(shost->host_lock, flags);
3051 
3052 	if (do_callback)
3053 		i->f->dev_loss_tmo_callbk(rport);
3054 
3055 	fc_bsg_remove(rport->rqst_q);
3056 
3057 	if (rport->devloss_work_q) {
3058 		work_q = rport->devloss_work_q;
3059 		rport->devloss_work_q = NULL;
3060 		destroy_workqueue(work_q);
3061 	}
3062 
3063 	transport_remove_device(dev);
3064 	device_del(dev);
3065 	transport_destroy_device(dev);
3066 	scsi_host_put(shost);			/* for fc_host->rport list */
3067 	put_device(dev);			/* for self-reference */
3068 }
3069 
3070 
3071 /**
3072  * fc_remote_port_create - allocates and creates a remote FC port.
3073  * @shost:	scsi host the remote port is connected to.
3074  * @channel:	Channel on shost port connected to.
3075  * @ids:	The world wide names, fc address, and FC4 port
3076  *		roles for the remote port.
3077  *
3078  * Allocates and creates the remoter port structure, including the
3079  * class and sysfs creation.
3080  *
3081  * Notes:
3082  *	This routine assumes no locks are held on entry.
3083  */
3084 static struct fc_rport *
3085 fc_remote_port_create(struct Scsi_Host *shost, int channel,
3086 		      struct fc_rport_identifiers  *ids)
3087 {
3088 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3089 	struct fc_internal *fci = to_fc_internal(shost->transportt);
3090 	struct fc_rport *rport;
3091 	struct device *dev;
3092 	unsigned long flags;
3093 	int error;
3094 	size_t size;
3095 
3096 	size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
3097 	rport = kzalloc(size, GFP_KERNEL);
3098 	if (unlikely(!rport)) {
3099 		printk(KERN_ERR "%s: allocation failure\n", __func__);
3100 		return NULL;
3101 	}
3102 
3103 	rport->maxframe_size = -1;
3104 	rport->supported_classes = FC_COS_UNSPECIFIED;
3105 	rport->dev_loss_tmo = fc_host->dev_loss_tmo;
3106 	memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
3107 	memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
3108 	rport->port_id = ids->port_id;
3109 	rport->roles = ids->roles;
3110 	rport->port_state = FC_PORTSTATE_ONLINE;
3111 	if (fci->f->dd_fcrport_size)
3112 		rport->dd_data = &rport[1];
3113 	rport->channel = channel;
3114 	rport->fast_io_fail_tmo = -1;
3115 
3116 	INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
3117 	INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
3118 	INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
3119 	INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
3120 	INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
3121 
3122 	spin_lock_irqsave(shost->host_lock, flags);
3123 
3124 	rport->number = fc_host->next_rport_number++;
3125 	if ((rport->roles & FC_PORT_ROLE_FCP_TARGET) ||
3126 	    (rport->roles & FC_PORT_ROLE_FCP_DUMMY_INITIATOR))
3127 		rport->scsi_target_id = fc_host->next_target_id++;
3128 	else
3129 		rport->scsi_target_id = -1;
3130 	list_add_tail(&rport->peers, &fc_host->rports);
3131 	scsi_host_get(shost);			/* for fc_host->rport list */
3132 
3133 	spin_unlock_irqrestore(shost->host_lock, flags);
3134 
3135 	rport->devloss_work_q = alloc_workqueue("fc_dl_%d_%d", WQ_PERCPU, 0,
3136 						shost->host_no, rport->number);
3137 	if (!rport->devloss_work_q) {
3138 		printk(KERN_ERR "FC Remote Port alloc_workqueue failed\n");
3139 /*
3140  * Note that we have not yet called device_initialize() / get_device()
3141  * Cannot reclaim incremented rport->number because we released host_lock
3142  */
3143 		spin_lock_irqsave(shost->host_lock, flags);
3144 		list_del(&rport->peers);
3145 		scsi_host_put(shost);			/* for fc_host->rport list */
3146 		spin_unlock_irqrestore(shost->host_lock, flags);
3147 		kfree(rport);
3148 		return NULL;
3149 	}
3150 
3151 	dev = &rport->dev;
3152 	device_initialize(dev);			/* takes self reference */
3153 	dev->parent = get_device(&shost->shost_gendev); /* parent reference */
3154 	dev->release = fc_rport_dev_release;
3155 	dev_set_name(dev, "rport-%d:%d-%d",
3156 		     shost->host_no, channel, rport->number);
3157 	transport_setup_device(dev);
3158 
3159 	error = device_add(dev);
3160 	if (error) {
3161 		printk(KERN_ERR "FC Remote Port device_add failed\n");
3162 		goto delete_rport;
3163 	}
3164 	transport_add_device(dev);
3165 	transport_configure_device(dev);
3166 
3167 	fc_bsg_rportadd(shost, rport);
3168 	/* ignore any bsg add error - we just can't do sgio */
3169 
3170 	if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
3171 		/* initiate a scan of the target */
3172 		rport->flags |= FC_RPORT_SCAN_PENDING;
3173 		scsi_queue_work(shost, &rport->scan_work);
3174 	}
3175 
3176 	return rport;
3177 
3178 delete_rport:
3179 	transport_destroy_device(dev);
3180 	spin_lock_irqsave(shost->host_lock, flags);
3181 	list_del(&rport->peers);
3182 	scsi_host_put(shost);			/* for fc_host->rport list */
3183 	spin_unlock_irqrestore(shost->host_lock, flags);
3184 	put_device(dev->parent);
3185 	kfree(rport);
3186 	return NULL;
3187 }
3188 
3189 /**
3190  * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
3191  * @shost:	scsi host the remote port is connected to.
3192  * @channel:	Channel on shost port connected to.
3193  * @ids:	The world wide names, fc address, and FC4 port
3194  *		roles for the remote port.
3195  *
3196  * The LLDD calls this routine to notify the transport of the existence
3197  * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
3198  * of the port, it's FC address (port_id), and the FC4 roles that are
3199  * active for the port.
3200  *
3201  * For ports that are FCP targets (aka scsi targets), the FC transport
3202  * maintains consistent target id bindings on behalf of the LLDD.
3203  * A consistent target id binding is an assignment of a target id to
3204  * a remote port identifier, which persists while the scsi host is
3205  * attached. The remote port can disappear, then later reappear, and
3206  * it's target id assignment remains the same. This allows for shifts
3207  * in FC addressing (if binding by wwpn or wwnn) with no apparent
3208  * changes to the scsi subsystem which is based on scsi host number and
3209  * target id values.  Bindings are only valid during the attachment of
3210  * the scsi host. If the host detaches, then later re-attaches, target
3211  * id bindings may change.
3212  *
3213  * This routine is responsible for returning a remote port structure.
3214  * The routine will search the list of remote ports it maintains
3215  * internally on behalf of consistent target id mappings. If found, the
3216  * remote port structure will be reused. Otherwise, a new remote port
3217  * structure will be allocated.
3218  *
3219  * Whenever a remote port is allocated, a new fc_remote_port class
3220  * device is created.
3221  *
3222  * Should not be called from interrupt context.
3223  *
3224  * Notes:
3225  *	This routine assumes no locks are held on entry.
3226  */
3227 struct fc_rport *
3228 fc_remote_port_add(struct Scsi_Host *shost, int channel,
3229 	struct fc_rport_identifiers  *ids)
3230 {
3231 	struct fc_internal *fci = to_fc_internal(shost->transportt);
3232 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3233 	struct fc_rport *rport;
3234 	unsigned long flags;
3235 	int match = 0;
3236 
3237 	/* ensure any stgt delete functions are done */
3238 	fc_flush_work(shost);
3239 
3240 	/*
3241 	 * Search the list of "active" rports, for an rport that has been
3242 	 * deleted, but we've held off the real delete while the target
3243 	 * is in a "blocked" state.
3244 	 */
3245 	spin_lock_irqsave(shost->host_lock, flags);
3246 
3247 	list_for_each_entry(rport, &fc_host->rports, peers) {
3248 
3249 		if ((rport->port_state == FC_PORTSTATE_BLOCKED ||
3250 		     rport->port_state == FC_PORTSTATE_NOTPRESENT) &&
3251 			(rport->channel == channel)) {
3252 
3253 			switch (fc_host->tgtid_bind_type) {
3254 			case FC_TGTID_BIND_BY_WWPN:
3255 			case FC_TGTID_BIND_NONE:
3256 				if (rport->port_name == ids->port_name)
3257 					match = 1;
3258 				break;
3259 			case FC_TGTID_BIND_BY_WWNN:
3260 				if (rport->node_name == ids->node_name)
3261 					match = 1;
3262 				break;
3263 			case FC_TGTID_BIND_BY_ID:
3264 				if (rport->port_id == ids->port_id)
3265 					match = 1;
3266 				break;
3267 			}
3268 
3269 			if (match) {
3270 
3271 				memcpy(&rport->node_name, &ids->node_name,
3272 					sizeof(rport->node_name));
3273 				memcpy(&rport->port_name, &ids->port_name,
3274 					sizeof(rport->port_name));
3275 				rport->port_id = ids->port_id;
3276 
3277 				rport->port_state = FC_PORTSTATE_ONLINE;
3278 				rport->roles = ids->roles;
3279 
3280 				spin_unlock_irqrestore(shost->host_lock, flags);
3281 
3282 				if (fci->f->dd_fcrport_size)
3283 					memset(rport->dd_data, 0,
3284 						fci->f->dd_fcrport_size);
3285 
3286 				/*
3287 				 * If we were not a target, cancel the
3288 				 * io terminate and rport timers, and
3289 				 * we're done.
3290 				 *
3291 				 * If we were a target, but our new role
3292 				 * doesn't indicate a target, leave the
3293 				 * timers running expecting the role to
3294 				 * change as the target fully logs in. If
3295 				 * it doesn't, the target will be torn down.
3296 				 *
3297 				 * If we were a target, and our role shows
3298 				 * we're still a target, cancel the timers
3299 				 * and kick off a scan.
3300 				 */
3301 
3302 				/* was a target, not in roles */
3303 				if ((rport->scsi_target_id != -1) &&
3304 				    (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
3305 					return rport;
3306 
3307 				/*
3308 				 * Stop the fail io and dev_loss timers.
3309 				 * If they flush, the port_state will
3310 				 * be checked and will NOOP the function.
3311 				 */
3312 				if (!cancel_delayed_work(&rport->fail_io_work))
3313 					fc_flush_devloss(shost, rport);
3314 				if (!cancel_delayed_work(&rport->dev_loss_work))
3315 					fc_flush_devloss(shost, rport);
3316 
3317 				spin_lock_irqsave(shost->host_lock, flags);
3318 
3319 				rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
3320 						  FC_RPORT_DEVLOSS_PENDING |
3321 						  FC_RPORT_DEVLOSS_CALLBK_DONE);
3322 
3323 				spin_unlock_irqrestore(shost->host_lock, flags);
3324 
3325 				/* if target, initiate a scan */
3326 				if (rport->scsi_target_id != -1) {
3327 					scsi_target_unblock(&rport->dev,
3328 							    SDEV_RUNNING);
3329 					spin_lock_irqsave(shost->host_lock,
3330 							  flags);
3331 					rport->flags |= FC_RPORT_SCAN_PENDING;
3332 					scsi_queue_work(shost,
3333 							&rport->scan_work);
3334 					spin_unlock_irqrestore(shost->host_lock,
3335 							flags);
3336 				}
3337 
3338 				fc_bsg_goose_queue(rport);
3339 
3340 				return rport;
3341 			}
3342 		}
3343 	}
3344 
3345 	/*
3346 	 * Search the bindings array
3347 	 * Note: if never a FCP target, you won't be on this list
3348 	 */
3349 	if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
3350 
3351 		/* search for a matching consistent binding */
3352 
3353 		list_for_each_entry(rport, &fc_host->rport_bindings,
3354 					peers) {
3355 			if (rport->channel != channel)
3356 				continue;
3357 
3358 			switch (fc_host->tgtid_bind_type) {
3359 			case FC_TGTID_BIND_BY_WWPN:
3360 				if (rport->port_name == ids->port_name)
3361 					match = 1;
3362 				break;
3363 			case FC_TGTID_BIND_BY_WWNN:
3364 				if (rport->node_name == ids->node_name)
3365 					match = 1;
3366 				break;
3367 			case FC_TGTID_BIND_BY_ID:
3368 				if (rport->port_id == ids->port_id)
3369 					match = 1;
3370 				break;
3371 			case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3372 				break;
3373 			}
3374 
3375 			if (match) {
3376 				list_move_tail(&rport->peers, &fc_host->rports);
3377 				break;
3378 			}
3379 		}
3380 
3381 		if (match) {
3382 			memcpy(&rport->node_name, &ids->node_name,
3383 				sizeof(rport->node_name));
3384 			memcpy(&rport->port_name, &ids->port_name,
3385 				sizeof(rport->port_name));
3386 			rport->port_id = ids->port_id;
3387 			rport->port_state = FC_PORTSTATE_ONLINE;
3388 			rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
3389 
3390 			if (fci->f->dd_fcrport_size)
3391 				memset(rport->dd_data, 0,
3392 						fci->f->dd_fcrport_size);
3393 			spin_unlock_irqrestore(shost->host_lock, flags);
3394 
3395 			fc_remote_port_rolechg(rport, ids->roles);
3396 			return rport;
3397 		}
3398 	}
3399 
3400 	spin_unlock_irqrestore(shost->host_lock, flags);
3401 
3402 	/* No consistent binding found - create new remote port entry */
3403 	rport = fc_remote_port_create(shost, channel, ids);
3404 
3405 	return rport;
3406 }
3407 EXPORT_SYMBOL(fc_remote_port_add);
3408 
3409 
3410 /**
3411  * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
3412  * @rport:	The remote port that no longer exists
3413  *
3414  * The LLDD calls this routine to notify the transport that a remote
3415  * port is no longer part of the topology. Note: Although a port
3416  * may no longer be part of the topology, it may persist in the remote
3417  * ports displayed by the fc_host. We do this under 2 conditions:
3418  *
3419  * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
3420  *    This allows the port to temporarily disappear, then reappear without
3421  *    disrupting the SCSI device tree attached to it. During the "blocked"
3422  *    period the port will still exist.
3423  *
3424  * 2) If the port was a scsi target and disappears for longer than we
3425  *    expect, we'll delete the port and the tear down the SCSI device tree
3426  *    attached to it. However, we want to semi-persist the target id assigned
3427  *    to that port if it eventually does exist. The port structure will
3428  *    remain (although with minimal information) so that the target id
3429  *    bindings also remain.
3430  *
3431  * If the remote port is not an FCP Target, it will be fully torn down
3432  * and deallocated, including the fc_remote_port class device.
3433  *
3434  * If the remote port is an FCP Target, the port will be placed in a
3435  * temporary blocked state. From the LLDD's perspective, the rport no
3436  * longer exists. From the SCSI midlayer's perspective, the SCSI target
3437  * exists, but all sdevs on it are blocked from further I/O. The following
3438  * is then expected.
3439  *
3440  *   If the remote port does not return (signaled by a LLDD call to
3441  *   fc_remote_port_add()) within the dev_loss_tmo timeout, then the
3442  *   scsi target is removed - killing all outstanding i/o and removing the
3443  *   scsi devices attached to it. The port structure will be marked Not
3444  *   Present and be partially cleared, leaving only enough information to
3445  *   recognize the remote port relative to the scsi target id binding if
3446  *   it later appears.  The port will remain as long as there is a valid
3447  *   binding (e.g. until the user changes the binding type or unloads the
3448  *   scsi host with the binding).
3449  *
3450  *   If the remote port returns within the dev_loss_tmo value (and matches
3451  *   according to the target id binding type), the port structure will be
3452  *   reused. If it is no longer a SCSI target, the target will be torn
3453  *   down. If it continues to be a SCSI target, then the target will be
3454  *   unblocked (allowing i/o to be resumed), and a scan will be activated
3455  *   to ensure that all luns are detected.
3456  *
3457  * Called from normal process context only - cannot be called from interrupt.
3458  *
3459  * Notes:
3460  *	This routine assumes no locks are held on entry.
3461  */
3462 void
3463 fc_remote_port_delete(struct fc_rport  *rport)
3464 {
3465 	struct Scsi_Host *shost = rport_to_shost(rport);
3466 	unsigned long timeout = rport->dev_loss_tmo;
3467 	unsigned long flags;
3468 
3469 	/*
3470 	 * No need to flush the fc_host work_q's, as all adds are synchronous.
3471 	 *
3472 	 * We do need to reclaim the rport scan work element, so eventually
3473 	 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
3474 	 * there's still a scan pending.
3475 	 */
3476 
3477 	spin_lock_irqsave(shost->host_lock, flags);
3478 
3479 	if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
3480 		(rport->port_state != FC_PORTSTATE_MARGINAL)) {
3481 		spin_unlock_irqrestore(shost->host_lock, flags);
3482 		return;
3483 	}
3484 
3485 	/*
3486 	 * In the past, we if this was not an FCP-Target, we would
3487 	 * unconditionally just jump to deleting the rport.
3488 	 * However, rports can be used as node containers by the LLDD,
3489 	 * and its not appropriate to just terminate the rport at the
3490 	 * first sign of a loss in connectivity. The LLDD may want to
3491 	 * send ELS traffic to re-validate the login. If the rport is
3492 	 * immediately deleted, it makes it inappropriate for a node
3493 	 * container.
3494 	 * So... we now unconditionally wait dev_loss_tmo before
3495 	 * destroying an rport.
3496 	 */
3497 
3498 	rport->port_state = FC_PORTSTATE_BLOCKED;
3499 
3500 	rport->flags |= FC_RPORT_DEVLOSS_PENDING;
3501 
3502 	spin_unlock_irqrestore(shost->host_lock, flags);
3503 
3504 	scsi_block_targets(shost, &rport->dev);
3505 
3506 	/* see if we need to kill io faster than waiting for device loss */
3507 	if ((rport->fast_io_fail_tmo != -1) &&
3508 	    (rport->fast_io_fail_tmo < timeout))
3509 		fc_queue_devloss_work(shost, rport, &rport->fail_io_work,
3510 				      rport->fast_io_fail_tmo * HZ);
3511 
3512 	/* cap the length the devices can be blocked until they are deleted */
3513 	fc_queue_devloss_work(shost, rport, &rport->dev_loss_work,
3514 			      timeout * HZ);
3515 }
3516 EXPORT_SYMBOL(fc_remote_port_delete);
3517 
3518 /**
3519  * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
3520  * @rport:	The remote port that changed.
3521  * @roles:      New roles for this port.
3522  *
3523  * Description: The LLDD calls this routine to notify the transport that the
3524  * roles on a remote port may have changed. The largest effect of this is
3525  * if a port now becomes a FCP Target, it must be allocated a
3526  * scsi target id.  If the port is no longer a FCP target, any
3527  * scsi target id value assigned to it will persist in case the
3528  * role changes back to include FCP Target. No changes in the scsi
3529  * midlayer will be invoked if the role changes (in the expectation
3530  * that the role will be resumed. If it doesn't normal error processing
3531  * will take place).
3532  *
3533  * Should not be called from interrupt context.
3534  *
3535  * Notes:
3536  *	This routine assumes no locks are held on entry.
3537  */
3538 void
3539 fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
3540 {
3541 	struct Scsi_Host *shost = rport_to_shost(rport);
3542 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3543 	unsigned long flags;
3544 	int create = 0;
3545 
3546 	spin_lock_irqsave(shost->host_lock, flags);
3547 	if (roles & FC_PORT_ROLE_FCP_TARGET) {
3548 		if (rport->scsi_target_id == -1) {
3549 			rport->scsi_target_id = fc_host->next_target_id++;
3550 			create = 1;
3551 		} else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
3552 			create = 1;
3553 	}
3554 
3555 	rport->roles = roles;
3556 
3557 	spin_unlock_irqrestore(shost->host_lock, flags);
3558 
3559 	if (create) {
3560 		/*
3561 		 * There may have been a delete timer running on the
3562 		 * port. Ensure that it is cancelled as we now know
3563 		 * the port is an FCP Target.
3564 		 * Note: we know the rport exists and is in an online
3565 		 *  state as the LLDD would not have had an rport
3566 		 *  reference to pass us.
3567 		 *
3568 		 * Take no action on the timer_delete() failure as the state
3569 		 * machine state change will validate the
3570 		 * transaction.
3571 		 */
3572 		if (!cancel_delayed_work(&rport->fail_io_work))
3573 			fc_flush_devloss(shost, rport);
3574 		if (!cancel_delayed_work(&rport->dev_loss_work))
3575 			fc_flush_devloss(shost, rport);
3576 
3577 		spin_lock_irqsave(shost->host_lock, flags);
3578 		rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
3579 				  FC_RPORT_DEVLOSS_PENDING |
3580 				  FC_RPORT_DEVLOSS_CALLBK_DONE);
3581 		spin_unlock_irqrestore(shost->host_lock, flags);
3582 
3583 		/* ensure any stgt delete functions are done */
3584 		fc_flush_work(shost);
3585 
3586 		scsi_target_unblock(&rport->dev, SDEV_RUNNING);
3587 		/* initiate a scan of the target */
3588 		spin_lock_irqsave(shost->host_lock, flags);
3589 		rport->flags |= FC_RPORT_SCAN_PENDING;
3590 		scsi_queue_work(shost, &rport->scan_work);
3591 		spin_unlock_irqrestore(shost->host_lock, flags);
3592 	}
3593 }
3594 EXPORT_SYMBOL(fc_remote_port_rolechg);
3595 
3596 /**
3597  * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
3598  * @work:	rport target that failed to reappear in the allotted time.
3599  *
3600  * Description: An attempt to delete a remote port blocks, and if it fails
3601  *              to return in the allotted time this gets called.
3602  */
3603 static void
3604 fc_timeout_deleted_rport(struct work_struct *work)
3605 {
3606 	struct fc_rport *rport =
3607 		container_of(work, struct fc_rport, dev_loss_work.work);
3608 	struct Scsi_Host *shost = rport_to_shost(rport);
3609 	struct fc_internal *i = to_fc_internal(shost->transportt);
3610 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3611 	unsigned long flags;
3612 	int do_callback = 0;
3613 
3614 	spin_lock_irqsave(shost->host_lock, flags);
3615 
3616 	rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3617 
3618 	/*
3619 	 * If the port is ONLINE, then it came back. If it was a SCSI
3620 	 * target, validate it still is. If not, tear down the
3621 	 * scsi_target on it.
3622 	 */
3623 	if (((rport->port_state == FC_PORTSTATE_ONLINE) ||
3624 		(rport->port_state == FC_PORTSTATE_MARGINAL)) &&
3625 	    (rport->scsi_target_id != -1) &&
3626 	    !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
3627 		dev_printk(KERN_ERR, &rport->dev,
3628 			"blocked FC remote port time out: no longer"
3629 			" a FCP target, removing starget\n");
3630 		spin_unlock_irqrestore(shost->host_lock, flags);
3631 		scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
3632 		fc_queue_work(shost, &rport->stgt_delete_work);
3633 		return;
3634 	}
3635 
3636 	/* NOOP state - we're flushing workq's */
3637 	if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3638 		spin_unlock_irqrestore(shost->host_lock, flags);
3639 		dev_printk(KERN_ERR, &rport->dev,
3640 			"blocked FC remote port time out: leaving"
3641 			" rport%s alone\n",
3642 			(rport->scsi_target_id != -1) ?  " and starget" : "");
3643 		return;
3644 	}
3645 
3646 	if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3647 	    (rport->scsi_target_id == -1)) {
3648 		list_del(&rport->peers);
3649 		rport->port_state = FC_PORTSTATE_DELETED;
3650 		dev_printk(KERN_ERR, &rport->dev,
3651 			"blocked FC remote port time out: removing"
3652 			" rport%s\n",
3653 			(rport->scsi_target_id != -1) ?  " and starget" : "");
3654 		fc_queue_work(shost, &rport->rport_delete_work);
3655 		spin_unlock_irqrestore(shost->host_lock, flags);
3656 		return;
3657 	}
3658 
3659 	dev_printk(KERN_ERR, &rport->dev,
3660 		"blocked FC remote port time out: removing target and "
3661 		"saving binding\n");
3662 
3663 	list_move_tail(&rport->peers, &fc_host->rport_bindings);
3664 
3665 	/*
3666 	 * Note: We do not remove or clear the hostdata area. This allows
3667 	 *   host-specific target data to persist along with the
3668 	 *   scsi_target_id. It's up to the host to manage it's hostdata area.
3669 	 */
3670 
3671 	/*
3672 	 * Reinitialize port attributes that may change if the port comes back.
3673 	 */
3674 	rport->maxframe_size = -1;
3675 	rport->supported_classes = FC_COS_UNSPECIFIED;
3676 	rport->roles = FC_PORT_ROLE_UNKNOWN;
3677 	rport->port_state = FC_PORTSTATE_NOTPRESENT;
3678 	rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
3679 
3680 	/*
3681 	 * Pre-emptively kill I/O rather than waiting for the work queue
3682 	 * item to teardown the starget. (FCOE libFC folks prefer this
3683 	 * and to have the rport_port_id still set when it's done).
3684 	 */
3685 	spin_unlock_irqrestore(shost->host_lock, flags);
3686 	fc_terminate_rport_io(rport);
3687 
3688 	spin_lock_irqsave(shost->host_lock, flags);
3689 
3690 	if (rport->port_state == FC_PORTSTATE_NOTPRESENT) {	/* still missing */
3691 
3692 		/* remove the identifiers that aren't used in the consisting binding */
3693 		switch (fc_host->tgtid_bind_type) {
3694 		case FC_TGTID_BIND_BY_WWPN:
3695 			rport->node_name = -1;
3696 			rport->port_id = -1;
3697 			break;
3698 		case FC_TGTID_BIND_BY_WWNN:
3699 			rport->port_name = -1;
3700 			rport->port_id = -1;
3701 			break;
3702 		case FC_TGTID_BIND_BY_ID:
3703 			rport->node_name = -1;
3704 			rport->port_name = -1;
3705 			break;
3706 		case FC_TGTID_BIND_NONE:	/* to keep compiler happy */
3707 			break;
3708 		}
3709 
3710 		/*
3711 		 * As this only occurs if the remote port (scsi target)
3712 		 * went away and didn't come back - we'll remove
3713 		 * all attached scsi devices.
3714 		 */
3715 		rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3716 		fc_queue_work(shost, &rport->stgt_delete_work);
3717 
3718 		do_callback = 1;
3719 	}
3720 
3721 	spin_unlock_irqrestore(shost->host_lock, flags);
3722 
3723 	/*
3724 	 * Notify the driver that the rport is now dead. The LLDD will
3725 	 * also guarantee that any communication to the rport is terminated
3726 	 *
3727 	 * Note: we set the CALLBK_DONE flag above to correspond
3728 	 */
3729 	if (do_callback && i->f->dev_loss_tmo_callbk)
3730 		i->f->dev_loss_tmo_callbk(rport);
3731 }
3732 
3733 
3734 /**
3735  * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3736  * @work:	rport to terminate io on.
3737  *
3738  * Notes: Only requests the failure of the io, not that all are flushed
3739  *    prior to returning.
3740  */
3741 static void
3742 fc_timeout_fail_rport_io(struct work_struct *work)
3743 {
3744 	struct fc_rport *rport =
3745 		container_of(work, struct fc_rport, fail_io_work.work);
3746 
3747 	if (rport->port_state != FC_PORTSTATE_BLOCKED)
3748 		return;
3749 
3750 	rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3751 	fc_terminate_rport_io(rport);
3752 }
3753 
3754 /**
3755  * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3756  * @work:	remote port to be scanned.
3757  */
3758 static void
3759 fc_scsi_scan_rport(struct work_struct *work)
3760 {
3761 	struct fc_rport *rport =
3762 		container_of(work, struct fc_rport, scan_work);
3763 	struct Scsi_Host *shost = rport_to_shost(rport);
3764 	struct fc_internal *i = to_fc_internal(shost->transportt);
3765 	unsigned long flags;
3766 
3767 	if (((rport->port_state == FC_PORTSTATE_ONLINE) ||
3768 		(rport->port_state == FC_PORTSTATE_MARGINAL)) &&
3769 	    (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3770 	    !(i->f->disable_target_scan)) {
3771 		scsi_scan_target(&rport->dev, rport->channel,
3772 				 rport->scsi_target_id, SCAN_WILD_CARD,
3773 				 SCSI_SCAN_RESCAN);
3774 	}
3775 
3776 	spin_lock_irqsave(shost->host_lock, flags);
3777 	rport->flags &= ~FC_RPORT_SCAN_PENDING;
3778 	spin_unlock_irqrestore(shost->host_lock, flags);
3779 }
3780 
3781 /**
3782  * fc_block_rport() - Block SCSI eh thread for blocked fc_rport.
3783  * @rport: Remote port that scsi_eh is trying to recover.
3784  *
3785  * This routine can be called from a FC LLD scsi_eh callback. It
3786  * blocks the scsi_eh thread until the fc_rport leaves the
3787  * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3788  * necessary to avoid the scsi_eh failing recovery actions for blocked
3789  * rports which would lead to offlined SCSI devices.
3790  *
3791  * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3792  *	    FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3793  *	    passed back to scsi_eh.
3794  */
3795 int fc_block_rport(struct fc_rport *rport)
3796 {
3797 	struct Scsi_Host *shost = rport_to_shost(rport);
3798 	unsigned long flags;
3799 
3800 	spin_lock_irqsave(shost->host_lock, flags);
3801 	while (rport->port_state == FC_PORTSTATE_BLOCKED &&
3802 	       !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
3803 		spin_unlock_irqrestore(shost->host_lock, flags);
3804 		msleep(1000);
3805 		spin_lock_irqsave(shost->host_lock, flags);
3806 	}
3807 	spin_unlock_irqrestore(shost->host_lock, flags);
3808 
3809 	if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
3810 		return FAST_IO_FAIL;
3811 
3812 	return 0;
3813 }
3814 EXPORT_SYMBOL(fc_block_rport);
3815 
3816 /**
3817  * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3818  * @cmnd: SCSI command that scsi_eh is trying to recover
3819  *
3820  * This routine can be called from a FC LLD scsi_eh callback. It
3821  * blocks the scsi_eh thread until the fc_rport leaves the
3822  * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3823  * necessary to avoid the scsi_eh failing recovery actions for blocked
3824  * rports which would lead to offlined SCSI devices.
3825  *
3826  * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3827  *	    FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3828  *	    passed back to scsi_eh.
3829  */
3830 int fc_block_scsi_eh(struct scsi_cmnd *cmnd)
3831 {
3832 	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3833 
3834 	if (WARN_ON_ONCE(!rport))
3835 		return FAST_IO_FAIL;
3836 
3837 	return fc_block_rport(rport);
3838 }
3839 EXPORT_SYMBOL(fc_block_scsi_eh);
3840 
3841 /*
3842  * fc_eh_should_retry_cmd - Checks if the cmd should be retried or not
3843  * @scmd:        The SCSI command to be checked
3844  *
3845  * This checks the rport state to decide if a cmd is
3846  * retryable.
3847  *
3848  * Returns: true if the rport state is not in marginal state.
3849  */
3850 bool fc_eh_should_retry_cmd(struct scsi_cmnd *scmd)
3851 {
3852 	struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
3853 
3854 	if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
3855 		(scsi_cmd_to_rq(scmd)->cmd_flags & REQ_FAILFAST_TRANSPORT)) {
3856 		set_host_byte(scmd, DID_TRANSPORT_MARGINAL);
3857 		return false;
3858 	}
3859 	return true;
3860 }
3861 EXPORT_SYMBOL_GPL(fc_eh_should_retry_cmd);
3862 
3863 /**
3864  * fc_vport_setup - allocates and creates a FC virtual port.
3865  * @shost:	scsi host the virtual port is connected to.
3866  * @channel:	Channel on shost port connected to.
3867  * @pdev:	parent device for vport
3868  * @ids:	The world wide names, FC4 port roles, etc for
3869  *              the virtual port.
3870  * @ret_vport:	The pointer to the created vport.
3871  *
3872  * Allocates and creates the vport structure, calls the parent host
3873  * to instantiate the vport, this completes w/ class and sysfs creation.
3874  *
3875  * Notes:
3876  *	This routine assumes no locks are held on entry.
3877  */
3878 static int
3879 fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
3880 	struct fc_vport_identifiers  *ids, struct fc_vport **ret_vport)
3881 {
3882 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3883 	struct fc_internal *fci = to_fc_internal(shost->transportt);
3884 	struct fc_vport *vport;
3885 	struct device *dev;
3886 	unsigned long flags;
3887 	size_t size;
3888 	int error;
3889 
3890 	*ret_vport = NULL;
3891 
3892 	if ( ! fci->f->vport_create)
3893 		return -ENOENT;
3894 
3895 	size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3896 	vport = kzalloc(size, GFP_KERNEL);
3897 	if (unlikely(!vport)) {
3898 		printk(KERN_ERR "%s: allocation failure\n", __func__);
3899 		return -ENOMEM;
3900 	}
3901 
3902 	vport->vport_state = FC_VPORT_UNKNOWN;
3903 	vport->vport_last_state = FC_VPORT_UNKNOWN;
3904 	vport->node_name = ids->node_name;
3905 	vport->port_name = ids->port_name;
3906 	vport->roles = ids->roles;
3907 	vport->vport_type = ids->vport_type;
3908 	if (fci->f->dd_fcvport_size)
3909 		vport->dd_data = &vport[1];
3910 	vport->shost = shost;
3911 	vport->channel = channel;
3912 	vport->flags = FC_VPORT_CREATING;
3913 	INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3914 
3915 	spin_lock_irqsave(shost->host_lock, flags);
3916 
3917 	if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3918 		spin_unlock_irqrestore(shost->host_lock, flags);
3919 		kfree(vport);
3920 		return -ENOSPC;
3921 	}
3922 	fc_host->npiv_vports_inuse++;
3923 	vport->number = fc_host->next_vport_number++;
3924 	list_add_tail(&vport->peers, &fc_host->vports);
3925 	scsi_host_get(shost);			/* for fc_host->vport list */
3926 
3927 	spin_unlock_irqrestore(shost->host_lock, flags);
3928 
3929 	dev = &vport->dev;
3930 	device_initialize(dev);			/* takes self reference */
3931 	dev->parent = get_device(pdev);		/* takes parent reference */
3932 	dev->release = fc_vport_dev_release;
3933 	dev_set_name(dev, "vport-%d:%d-%d",
3934 		     shost->host_no, channel, vport->number);
3935 	transport_setup_device(dev);
3936 
3937 	error = device_add(dev);
3938 	if (error) {
3939 		printk(KERN_ERR "FC Virtual Port device_add failed\n");
3940 		goto delete_vport;
3941 	}
3942 	transport_add_device(dev);
3943 	transport_configure_device(dev);
3944 
3945 	error = fci->f->vport_create(vport, ids->disable);
3946 	if (error) {
3947 		printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3948 		goto delete_vport_all;
3949 	}
3950 
3951 	/*
3952 	 * if the parent isn't the physical adapter's Scsi_Host, ensure
3953 	 * the Scsi_Host at least contains a symlink to the vport.
3954 	 */
3955 	if (pdev != &shost->shost_gendev) {
3956 		error = sysfs_create_link(&shost->shost_gendev.kobj,
3957 				 &dev->kobj, dev_name(dev));
3958 		if (error)
3959 			printk(KERN_ERR
3960 				"%s: Cannot create vport symlinks for "
3961 				"%s, err=%d\n",
3962 				__func__, dev_name(dev), error);
3963 	}
3964 	spin_lock_irqsave(shost->host_lock, flags);
3965 	vport->flags &= ~FC_VPORT_CREATING;
3966 	spin_unlock_irqrestore(shost->host_lock, flags);
3967 
3968 	dev_printk(KERN_NOTICE, pdev,
3969 			"%s created via shost%d channel %d\n", dev_name(dev),
3970 			shost->host_no, channel);
3971 
3972 	*ret_vport = vport;
3973 
3974 	return 0;
3975 
3976 delete_vport_all:
3977 	transport_remove_device(dev);
3978 	device_del(dev);
3979 delete_vport:
3980 	transport_destroy_device(dev);
3981 	spin_lock_irqsave(shost->host_lock, flags);
3982 	list_del(&vport->peers);
3983 	scsi_host_put(shost);			/* for fc_host->vport list */
3984 	fc_host->npiv_vports_inuse--;
3985 	spin_unlock_irqrestore(shost->host_lock, flags);
3986 	put_device(dev->parent);
3987 	kfree(vport);
3988 
3989 	return error;
3990 }
3991 
3992 /**
3993  * fc_vport_create - Admin App or LLDD requests creation of a vport
3994  * @shost:	scsi host the virtual port is connected to.
3995  * @channel:	channel on shost port connected to.
3996  * @ids:	The world wide names, FC4 port roles, etc for
3997  *              the virtual port.
3998  *
3999  * Notes:
4000  *	This routine assumes no locks are held on entry.
4001  */
4002 struct fc_vport *
4003 fc_vport_create(struct Scsi_Host *shost, int channel,
4004 	struct fc_vport_identifiers *ids)
4005 {
4006 	int stat;
4007 	struct fc_vport *vport;
4008 
4009 	stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
4010 		 ids, &vport);
4011 	return stat ? NULL : vport;
4012 }
4013 EXPORT_SYMBOL(fc_vport_create);
4014 
4015 /**
4016  * fc_vport_terminate - Admin App or LLDD requests termination of a vport
4017  * @vport:	fc_vport to be terminated
4018  *
4019  * Calls the LLDD vport_delete() function, then deallocates and removes
4020  * the vport from the shost and object tree.
4021  *
4022  * Notes:
4023  *	This routine assumes no locks are held on entry.
4024  */
4025 int
4026 fc_vport_terminate(struct fc_vport *vport)
4027 {
4028 	struct Scsi_Host *shost = vport_to_shost(vport);
4029 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
4030 	struct fc_internal *i = to_fc_internal(shost->transportt);
4031 	struct device *dev = &vport->dev;
4032 	unsigned long flags;
4033 	int stat;
4034 
4035 	if (i->f->vport_delete)
4036 		stat = i->f->vport_delete(vport);
4037 	else
4038 		stat = -ENOENT;
4039 
4040 	spin_lock_irqsave(shost->host_lock, flags);
4041 	vport->flags &= ~FC_VPORT_DELETING;
4042 	if (!stat) {
4043 		vport->flags |= FC_VPORT_DELETED;
4044 		list_del(&vport->peers);
4045 		fc_host->npiv_vports_inuse--;
4046 		scsi_host_put(shost);		/* for fc_host->vport list */
4047 	}
4048 	spin_unlock_irqrestore(shost->host_lock, flags);
4049 
4050 	if (stat)
4051 		return stat;
4052 
4053 	if (dev->parent != &shost->shost_gendev)
4054 		sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
4055 	transport_remove_device(dev);
4056 	device_del(dev);
4057 	transport_destroy_device(dev);
4058 
4059 	/*
4060 	 * Removing our self-reference should mean our
4061 	 * release function gets called, which will drop the remaining
4062 	 * parent reference and free the data structure.
4063 	 */
4064 	put_device(dev);			/* for self-reference */
4065 
4066 	return 0; /* SUCCESS */
4067 }
4068 EXPORT_SYMBOL(fc_vport_terminate);
4069 
4070 /**
4071  * fc_vport_sched_delete - workq-based delete request for a vport
4072  * @work:	vport to be deleted.
4073  */
4074 static void
4075 fc_vport_sched_delete(struct work_struct *work)
4076 {
4077 	struct fc_vport *vport =
4078 		container_of(work, struct fc_vport, vport_delete_work);
4079 	int stat;
4080 
4081 	stat = fc_vport_terminate(vport);
4082 	if (stat)
4083 		dev_printk(KERN_ERR, vport->dev.parent,
4084 			"%s: %s could not be deleted created via "
4085 			"shost%d channel %d - error %d\n", __func__,
4086 			dev_name(&vport->dev), vport->shost->host_no,
4087 			vport->channel, stat);
4088 }
4089 
4090 
4091 /*
4092  * BSG support
4093  */
4094 
4095 /**
4096  * fc_bsg_job_timeout - handler for when a bsg request timesout
4097  * @req:	request that timed out
4098  */
4099 static enum blk_eh_timer_return
4100 fc_bsg_job_timeout(struct request *req)
4101 {
4102 	struct bsg_job *job = blk_mq_rq_to_pdu(req);
4103 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
4104 	struct fc_rport *rport = fc_bsg_to_rport(job);
4105 	struct fc_internal *i = to_fc_internal(shost->transportt);
4106 	int err = 0, inflight = 0;
4107 
4108 	if (rport && rport->port_state == FC_PORTSTATE_BLOCKED)
4109 		return BLK_EH_RESET_TIMER;
4110 
4111 	inflight = bsg_job_get(job);
4112 
4113 	if (inflight && i->f->bsg_timeout) {
4114 		/* call LLDD to abort the i/o as it has timed out */
4115 		err = i->f->bsg_timeout(job);
4116 		if (err == -EAGAIN) {
4117 			bsg_job_put(job);
4118 			return BLK_EH_RESET_TIMER;
4119 		} else if (err)
4120 			printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
4121 				"abort failed with status %d\n", err);
4122 	}
4123 
4124 	/* the blk_end_sync_io() doesn't check the error */
4125 	if (inflight)
4126 		blk_mq_end_request(req, BLK_STS_IOERR);
4127 	return BLK_EH_DONE;
4128 }
4129 
4130 /**
4131  * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
4132  * @shost:	scsi host rport attached to
4133  * @job:	bsg job to be processed
4134  */
4135 static int fc_bsg_host_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
4136 {
4137 	struct fc_internal *i = to_fc_internal(shost->transportt);
4138 	struct fc_bsg_request *bsg_request = job->request;
4139 	struct fc_bsg_reply *bsg_reply = job->reply;
4140 	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
4141 	int ret;
4142 
4143 	/* check if we really have all the request data needed */
4144 	if (job->request_len < cmdlen) {
4145 		ret = -ENOMSG;
4146 		goto fail_host_msg;
4147 	}
4148 
4149 	/* Validate the host command */
4150 	switch (bsg_request->msgcode) {
4151 	case FC_BSG_HST_ADD_RPORT:
4152 		cmdlen += sizeof(struct fc_bsg_host_add_rport);
4153 		break;
4154 
4155 	case FC_BSG_HST_DEL_RPORT:
4156 		cmdlen += sizeof(struct fc_bsg_host_del_rport);
4157 		break;
4158 
4159 	case FC_BSG_HST_ELS_NOLOGIN:
4160 		cmdlen += sizeof(struct fc_bsg_host_els);
4161 		/* there better be a xmt and rcv payloads */
4162 		if ((!job->request_payload.payload_len) ||
4163 		    (!job->reply_payload.payload_len)) {
4164 			ret = -EINVAL;
4165 			goto fail_host_msg;
4166 		}
4167 		break;
4168 
4169 	case FC_BSG_HST_CT:
4170 		cmdlen += sizeof(struct fc_bsg_host_ct);
4171 		/* there better be xmt and rcv payloads */
4172 		if ((!job->request_payload.payload_len) ||
4173 		    (!job->reply_payload.payload_len)) {
4174 			ret = -EINVAL;
4175 			goto fail_host_msg;
4176 		}
4177 		break;
4178 
4179 	case FC_BSG_HST_VENDOR:
4180 		cmdlen += sizeof(struct fc_bsg_host_vendor);
4181 		if ((shost->hostt->vendor_id == 0L) ||
4182 		    (bsg_request->rqst_data.h_vendor.vendor_id !=
4183 			shost->hostt->vendor_id)) {
4184 			ret = -ESRCH;
4185 			goto fail_host_msg;
4186 		}
4187 		break;
4188 
4189 	default:
4190 		ret = -EBADR;
4191 		goto fail_host_msg;
4192 	}
4193 
4194 	ret = i->f->bsg_request(job);
4195 	if (!ret)
4196 		return 0;
4197 
4198 fail_host_msg:
4199 	/* return the errno failure code as the only status */
4200 	BUG_ON(job->reply_len < sizeof(uint32_t));
4201 	bsg_reply->reply_payload_rcv_len = 0;
4202 	bsg_reply->result = ret;
4203 	job->reply_len = sizeof(uint32_t);
4204 	bsg_job_done(job, bsg_reply->result,
4205 		       bsg_reply->reply_payload_rcv_len);
4206 	return 0;
4207 }
4208 
4209 
4210 /*
4211  * fc_bsg_goose_queue - restart rport queue in case it was stopped
4212  * @rport:	rport to be restarted
4213  */
4214 static void
4215 fc_bsg_goose_queue(struct fc_rport *rport)
4216 {
4217 	struct request_queue *q = rport->rqst_q;
4218 
4219 	if (q)
4220 		blk_mq_run_hw_queues(q, true);
4221 }
4222 
4223 /**
4224  * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
4225  * @shost:	scsi host rport attached to
4226  * @job:	bsg job to be processed
4227  */
4228 static int fc_bsg_rport_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
4229 {
4230 	struct fc_internal *i = to_fc_internal(shost->transportt);
4231 	struct fc_bsg_request *bsg_request = job->request;
4232 	struct fc_bsg_reply *bsg_reply = job->reply;
4233 	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
4234 	int ret;
4235 
4236 	/* check if we really have all the request data needed */
4237 	if (job->request_len < cmdlen) {
4238 		ret = -ENOMSG;
4239 		goto fail_rport_msg;
4240 	}
4241 
4242 	/* Validate the rport command */
4243 	switch (bsg_request->msgcode) {
4244 	case FC_BSG_RPT_ELS:
4245 		cmdlen += sizeof(struct fc_bsg_rport_els);
4246 		goto check_bidi;
4247 
4248 	case FC_BSG_RPT_CT:
4249 		cmdlen += sizeof(struct fc_bsg_rport_ct);
4250 check_bidi:
4251 		/* there better be xmt and rcv payloads */
4252 		if ((!job->request_payload.payload_len) ||
4253 		    (!job->reply_payload.payload_len)) {
4254 			ret = -EINVAL;
4255 			goto fail_rport_msg;
4256 		}
4257 		break;
4258 	default:
4259 		ret = -EBADR;
4260 		goto fail_rport_msg;
4261 	}
4262 
4263 	ret = i->f->bsg_request(job);
4264 	if (!ret)
4265 		return 0;
4266 
4267 fail_rport_msg:
4268 	/* return the errno failure code as the only status */
4269 	BUG_ON(job->reply_len < sizeof(uint32_t));
4270 	bsg_reply->reply_payload_rcv_len = 0;
4271 	bsg_reply->result = ret;
4272 	job->reply_len = sizeof(uint32_t);
4273 	bsg_job_done(job, bsg_reply->result,
4274 		       bsg_reply->reply_payload_rcv_len);
4275 	return 0;
4276 }
4277 
4278 static int fc_bsg_dispatch(struct bsg_job *job)
4279 {
4280 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
4281 
4282 	if (scsi_is_fc_rport(job->dev))
4283 		return fc_bsg_rport_dispatch(shost, job);
4284 	else
4285 		return fc_bsg_host_dispatch(shost, job);
4286 }
4287 
4288 static blk_status_t fc_bsg_rport_prep(struct fc_rport *rport)
4289 {
4290 	if (rport->port_state == FC_PORTSTATE_BLOCKED &&
4291 	    !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
4292 		return BLK_STS_RESOURCE;
4293 
4294 	if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
4295 		(rport->port_state != FC_PORTSTATE_MARGINAL))
4296 		return BLK_STS_IOERR;
4297 
4298 	return BLK_STS_OK;
4299 }
4300 
4301 
4302 static int fc_bsg_dispatch_prep(struct bsg_job *job)
4303 {
4304 	struct fc_rport *rport = fc_bsg_to_rport(job);
4305 	blk_status_t ret;
4306 
4307 	ret = fc_bsg_rport_prep(rport);
4308 	switch (ret) {
4309 	case BLK_STS_OK:
4310 		break;
4311 	case BLK_STS_RESOURCE:
4312 		return -EAGAIN;
4313 	default:
4314 		return -EIO;
4315 	}
4316 
4317 	return fc_bsg_dispatch(job);
4318 }
4319 
4320 /**
4321  * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
4322  * @shost:	shost for fc_host
4323  * @fc_host:	fc_host adding the structures to
4324  */
4325 static int
4326 fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
4327 {
4328 	struct device *dev = &shost->shost_gendev;
4329 	struct fc_internal *i = to_fc_internal(shost->transportt);
4330 	struct queue_limits lim;
4331 	struct request_queue *q;
4332 	char bsg_name[20];
4333 
4334 	fc_host->rqst_q = NULL;
4335 
4336 	if (!i->f->bsg_request)
4337 		return -ENOTSUPP;
4338 
4339 	snprintf(bsg_name, sizeof(bsg_name),
4340 		 "fc_host%d", shost->host_no);
4341 	scsi_init_limits(shost, &lim);
4342 	lim.max_segments = min_not_zero(lim.max_segments, i->f->max_bsg_segments);
4343 	q = bsg_setup_queue(dev, bsg_name, &lim, fc_bsg_dispatch,
4344 			fc_bsg_job_timeout, i->f->dd_bsg_size);
4345 	if (IS_ERR(q)) {
4346 		dev_err(dev,
4347 			"fc_host%d: bsg interface failed to initialize - setup queue\n",
4348 			shost->host_no);
4349 		return PTR_ERR(q);
4350 	}
4351 	blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
4352 	fc_host->rqst_q = q;
4353 	return 0;
4354 }
4355 
4356 /**
4357  * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
4358  * @shost:	shost that rport is attached to
4359  * @rport:	rport that the bsg hooks are being attached to
4360  */
4361 static int
4362 fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
4363 {
4364 	struct device *dev = &rport->dev;
4365 	struct fc_internal *i = to_fc_internal(shost->transportt);
4366 	struct queue_limits lim;
4367 	struct request_queue *q;
4368 
4369 	rport->rqst_q = NULL;
4370 
4371 	if (!i->f->bsg_request)
4372 		return -ENOTSUPP;
4373 
4374 	scsi_init_limits(shost, &lim);
4375 	lim.max_segments = min_not_zero(lim.max_segments, i->f->max_bsg_segments);
4376 	q = bsg_setup_queue(dev, dev_name(dev), &lim, fc_bsg_dispatch_prep,
4377 				fc_bsg_job_timeout, i->f->dd_bsg_size);
4378 	if (IS_ERR(q)) {
4379 		dev_err(dev, "failed to setup bsg queue\n");
4380 		return PTR_ERR(q);
4381 	}
4382 	blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
4383 	rport->rqst_q = q;
4384 	return 0;
4385 }
4386 
4387 
4388 /**
4389  * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
4390  * @q:	the request_queue that is to be torn down.
4391  *
4392  * Notes:
4393  *   Before unregistering the queue empty any requests that are blocked
4394  *
4395  *
4396  */
4397 static void
4398 fc_bsg_remove(struct request_queue *q)
4399 {
4400 	bsg_remove_queue(q);
4401 }
4402 
4403 
4404 /* Original Author:  Martin Hicks */
4405 MODULE_AUTHOR("James Smart");
4406 MODULE_DESCRIPTION("FC Transport Attributes");
4407 MODULE_LICENSE("GPL");
4408 
4409 module_init(fc_transport_init);
4410 module_exit(fc_transport_exit);
4411