xref: /linux/drivers/scsi/scsi_transport_fc.c (revision d30aca3eeffc18452e5cc5c4e59f1a4da2bd2f12)
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_fpin_statistic(name)					\
1333 static ssize_t fc_rport_fpinstat_##name(struct device *cd,		\
1334 				  struct device_attribute *attr,	\
1335 				  char *buf)				\
1336 {									\
1337 	struct fc_rport *rport = transport_class_to_rport(cd);		\
1338 									\
1339 	return snprintf(buf, 20, "0x%llx\n", rport->fpin_stats.name);	\
1340 }									\
1341 static FC_DEVICE_ATTR(rport, fpin_##name, 0444, fc_rport_fpinstat_##name, NULL)
1342 
1343 fc_rport_fpin_statistic(dn);
1344 fc_rport_fpin_statistic(dn_unknown);
1345 fc_rport_fpin_statistic(dn_timeout);
1346 fc_rport_fpin_statistic(dn_unable_to_route);
1347 fc_rport_fpin_statistic(dn_device_specific);
1348 fc_rport_fpin_statistic(cn);
1349 fc_rport_fpin_statistic(cn_clear);
1350 fc_rport_fpin_statistic(cn_lost_credit);
1351 fc_rport_fpin_statistic(cn_credit_stall);
1352 fc_rport_fpin_statistic(cn_oversubscription);
1353 fc_rport_fpin_statistic(cn_device_specific);
1354 fc_rport_fpin_statistic(li);
1355 fc_rport_fpin_statistic(li_failure_unknown);
1356 fc_rport_fpin_statistic(li_link_failure_count);
1357 fc_rport_fpin_statistic(li_loss_of_sync_count);
1358 fc_rport_fpin_statistic(li_loss_of_signals_count);
1359 fc_rport_fpin_statistic(li_prim_seq_err_count);
1360 fc_rport_fpin_statistic(li_invalid_tx_word_count);
1361 fc_rport_fpin_statistic(li_invalid_crc_count);
1362 fc_rport_fpin_statistic(li_device_specific);
1363 
1364 static struct attribute *fc_rport_statistics_attrs[] = {
1365 	&device_attr_rport_fpin_dn.attr,
1366 	&device_attr_rport_fpin_dn_unknown.attr,
1367 	&device_attr_rport_fpin_dn_timeout.attr,
1368 	&device_attr_rport_fpin_dn_unable_to_route.attr,
1369 	&device_attr_rport_fpin_dn_device_specific.attr,
1370 	&device_attr_rport_fpin_li.attr,
1371 	&device_attr_rport_fpin_li_failure_unknown.attr,
1372 	&device_attr_rport_fpin_li_link_failure_count.attr,
1373 	&device_attr_rport_fpin_li_loss_of_sync_count.attr,
1374 	&device_attr_rport_fpin_li_loss_of_signals_count.attr,
1375 	&device_attr_rport_fpin_li_prim_seq_err_count.attr,
1376 	&device_attr_rport_fpin_li_invalid_tx_word_count.attr,
1377 	&device_attr_rport_fpin_li_invalid_crc_count.attr,
1378 	&device_attr_rport_fpin_li_device_specific.attr,
1379 	&device_attr_rport_fpin_cn.attr,
1380 	&device_attr_rport_fpin_cn_clear.attr,
1381 	&device_attr_rport_fpin_cn_lost_credit.attr,
1382 	&device_attr_rport_fpin_cn_credit_stall.attr,
1383 	&device_attr_rport_fpin_cn_oversubscription.attr,
1384 	&device_attr_rport_fpin_cn_device_specific.attr,
1385 	NULL
1386 };
1387 
1388 static struct attribute_group fc_rport_statistics_group = {
1389 	.name = "statistics",
1390 	.attrs = fc_rport_statistics_attrs,
1391 };
1392 
1393 
1394 /*
1395  * FC SCSI Target Attribute Management
1396  */
1397 
1398 /*
1399  * Note: in the target show function we recognize when the remote
1400  *  port is in the hierarchy and do not allow the driver to get
1401  *  involved in sysfs functions. The driver only gets involved if
1402  *  it's the "old" style that doesn't use rports.
1403  */
1404 #define fc_starget_show_function(field, format_string, sz, cast)	\
1405 static ssize_t								\
1406 show_fc_starget_##field (struct device *dev, 				\
1407 			 struct device_attribute *attr, char *buf)	\
1408 {									\
1409 	struct scsi_target *starget = transport_class_to_starget(dev);	\
1410 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);	\
1411 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1412 	struct fc_rport *rport = starget_to_rport(starget);		\
1413 	if (rport)							\
1414 		fc_starget_##field(starget) = rport->field;		\
1415 	else if (i->f->get_starget_##field)				\
1416 		i->f->get_starget_##field(starget);			\
1417 	return snprintf(buf, sz, format_string, 			\
1418 		cast fc_starget_##field(starget)); 			\
1419 }
1420 
1421 #define fc_starget_rd_attr(field, format_string, sz)			\
1422 	fc_starget_show_function(field, format_string, sz, )		\
1423 static FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
1424 			 show_fc_starget_##field, NULL)
1425 
1426 #define fc_starget_rd_attr_cast(field, format_string, sz, cast)		\
1427 	fc_starget_show_function(field, format_string, sz, (cast))	\
1428 static FC_DEVICE_ATTR(starget, field, S_IRUGO,			\
1429 			  show_fc_starget_##field, NULL)
1430 
1431 #define SETUP_STARGET_ATTRIBUTE_RD(field)				\
1432 	i->private_starget_attrs[count] = device_attr_starget_##field; \
1433 	i->private_starget_attrs[count].attr.mode = S_IRUGO;		\
1434 	i->private_starget_attrs[count].store = NULL;			\
1435 	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
1436 	if (i->f->show_starget_##field)					\
1437 		count++
1438 
1439 #define SETUP_STARGET_ATTRIBUTE_RW(field)				\
1440 	i->private_starget_attrs[count] = device_attr_starget_##field; \
1441 	if (!i->f->set_starget_##field) {				\
1442 		i->private_starget_attrs[count].attr.mode = S_IRUGO;	\
1443 		i->private_starget_attrs[count].store = NULL;		\
1444 	}								\
1445 	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
1446 	if (i->f->show_starget_##field)					\
1447 		count++
1448 
1449 /* The FC Transport SCSI Target Attributes: */
1450 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1451 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1452 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1453 
1454 
1455 /*
1456  * FC Virtual Port Attribute Management
1457  */
1458 
1459 #define fc_vport_show_function(field, format_string, sz, cast)		\
1460 static ssize_t								\
1461 show_fc_vport_##field (struct device *dev, 				\
1462 		       struct device_attribute *attr, char *buf)	\
1463 {									\
1464 	struct fc_vport *vport = transport_class_to_vport(dev);		\
1465 	struct Scsi_Host *shost = vport_to_shost(vport);		\
1466 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1467 	if ((i->f->get_vport_##field) &&				\
1468 	    !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)))	\
1469 		i->f->get_vport_##field(vport);				\
1470 	return snprintf(buf, sz, format_string, cast vport->field); 	\
1471 }
1472 
1473 #define fc_vport_store_function(field)					\
1474 static ssize_t								\
1475 store_fc_vport_##field(struct device *dev,				\
1476 		       struct device_attribute *attr,			\
1477 		       const char *buf,	size_t count)			\
1478 {									\
1479 	int val;							\
1480 	struct fc_vport *vport = transport_class_to_vport(dev);		\
1481 	struct Scsi_Host *shost = vport_to_shost(vport);		\
1482 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1483 	char *cp;							\
1484 	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))	\
1485 		return -EBUSY;						\
1486 	val = simple_strtoul(buf, &cp, 0);				\
1487 	if (*cp && (*cp != '\n'))					\
1488 		return -EINVAL;						\
1489 	i->f->set_vport_##field(vport, val);				\
1490 	return count;							\
1491 }
1492 
1493 #define fc_vport_store_str_function(field, slen)			\
1494 static ssize_t								\
1495 store_fc_vport_##field(struct device *dev,				\
1496 		       struct device_attribute *attr, 			\
1497 		       const char *buf,	size_t count)			\
1498 {									\
1499 	struct fc_vport *vport = transport_class_to_vport(dev);		\
1500 	struct Scsi_Host *shost = vport_to_shost(vport);		\
1501 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1502 	unsigned int cnt=count;						\
1503 									\
1504 	/* count may include a LF at end of string */			\
1505 	if (buf[cnt-1] == '\n')						\
1506 		cnt--;							\
1507 	if (cnt > ((slen) - 1))						\
1508 		return -EINVAL;						\
1509 	memcpy(vport->field, buf, cnt);					\
1510 	i->f->set_vport_##field(vport);					\
1511 	return count;							\
1512 }
1513 
1514 #define fc_vport_rd_attr(field, format_string, sz)			\
1515 	fc_vport_show_function(field, format_string, sz, )		\
1516 static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1517 			 show_fc_vport_##field, NULL)
1518 
1519 #define fc_vport_rd_attr_cast(field, format_string, sz, cast)		\
1520 	fc_vport_show_function(field, format_string, sz, (cast))	\
1521 static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1522 			  show_fc_vport_##field, NULL)
1523 
1524 #define fc_vport_rw_attr(field, format_string, sz)			\
1525 	fc_vport_show_function(field, format_string, sz, )		\
1526 	fc_vport_store_function(field)					\
1527 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1528 			show_fc_vport_##field,				\
1529 			store_fc_vport_##field)
1530 
1531 #define fc_private_vport_show_function(field, format_string, sz, cast)	\
1532 static ssize_t								\
1533 show_fc_vport_##field (struct device *dev,				\
1534 		       struct device_attribute *attr, char *buf)	\
1535 {									\
1536 	struct fc_vport *vport = transport_class_to_vport(dev);		\
1537 	return snprintf(buf, sz, format_string, cast vport->field); 	\
1538 }
1539 
1540 #define fc_private_vport_store_u32_function(field)			\
1541 static ssize_t								\
1542 store_fc_vport_##field(struct device *dev,				\
1543 		       struct device_attribute *attr,			\
1544 		       const char *buf,	size_t count)			\
1545 {									\
1546 	u32 val;							\
1547 	struct fc_vport *vport = transport_class_to_vport(dev);		\
1548 	char *cp;							\
1549 	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))		\
1550 		return -EBUSY;						\
1551 	val = simple_strtoul(buf, &cp, 0);				\
1552 	if (*cp && (*cp != '\n'))					\
1553 		return -EINVAL;						\
1554 	vport->field = val;						\
1555 	return count;							\
1556 }
1557 
1558 
1559 #define fc_private_vport_rd_attr(field, format_string, sz)		\
1560 	fc_private_vport_show_function(field, format_string, sz, )	\
1561 static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1562 			 show_fc_vport_##field, NULL)
1563 
1564 #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast)	\
1565 	fc_private_vport_show_function(field, format_string, sz, (cast)) \
1566 static FC_DEVICE_ATTR(vport, field, S_IRUGO,			\
1567 			  show_fc_vport_##field, NULL)
1568 
1569 #define fc_private_vport_rw_u32_attr(field, format_string, sz)		\
1570 	fc_private_vport_show_function(field, format_string, sz, )	\
1571 	fc_private_vport_store_u32_function(field)			\
1572 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR,		\
1573 			show_fc_vport_##field,				\
1574 			store_fc_vport_##field)
1575 
1576 
1577 #define fc_private_vport_rd_enum_attr(title, maxlen)			\
1578 static ssize_t								\
1579 show_fc_vport_##title (struct device *dev,				\
1580 		       struct device_attribute *attr,			\
1581 		       char *buf)					\
1582 {									\
1583 	struct fc_vport *vport = transport_class_to_vport(dev);		\
1584 	const char *name;						\
1585 	name = get_fc_##title##_name(vport->title);			\
1586 	if (!name)							\
1587 		return -EINVAL;						\
1588 	return snprintf(buf, maxlen, "%s\n", name);			\
1589 }									\
1590 static FC_DEVICE_ATTR(vport, title, S_IRUGO,			\
1591 			show_fc_vport_##title, NULL)
1592 
1593 
1594 #define SETUP_VPORT_ATTRIBUTE_RD(field)					\
1595 	i->private_vport_attrs[count] = device_attr_vport_##field; \
1596 	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
1597 	i->private_vport_attrs[count].store = NULL;			\
1598 	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1599 	if (i->f->get_##field)						\
1600 		count++
1601 	/* NOTE: Above MACRO differs: checks function not show bit */
1602 
1603 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field)				\
1604 	i->private_vport_attrs[count] = device_attr_vport_##field; \
1605 	i->private_vport_attrs[count].attr.mode = S_IRUGO;		\
1606 	i->private_vport_attrs[count].store = NULL;			\
1607 	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1608 	count++
1609 
1610 #define SETUP_VPORT_ATTRIBUTE_WR(field)					\
1611 	i->private_vport_attrs[count] = device_attr_vport_##field; \
1612 	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1613 	if (i->f->field)						\
1614 		count++
1615 	/* NOTE: Above MACRO differs: checks function */
1616 
1617 #define SETUP_VPORT_ATTRIBUTE_RW(field)					\
1618 	i->private_vport_attrs[count] = device_attr_vport_##field; \
1619 	if (!i->f->set_vport_##field) {					\
1620 		i->private_vport_attrs[count].attr.mode = S_IRUGO;	\
1621 		i->private_vport_attrs[count].store = NULL;		\
1622 	}								\
1623 	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1624 	count++
1625 	/* NOTE: Above MACRO differs: does not check show bit */
1626 
1627 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field)				\
1628 {									\
1629 	i->private_vport_attrs[count] = device_attr_vport_##field; \
1630 	i->vport_attrs[count] = &i->private_vport_attrs[count];		\
1631 	count++;							\
1632 }
1633 
1634 
1635 /* The FC Transport Virtual Port Attributes: */
1636 
1637 /* Fixed Virtual Port Attributes */
1638 
1639 /* Dynamic Virtual Port Attributes */
1640 
1641 /* Private Virtual Port Attributes */
1642 
1643 fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1644 fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1645 fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1646 fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1647 
1648 static ssize_t
1649 show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1650 		     char *buf)
1651 {
1652 	struct fc_vport *vport = transport_class_to_vport(dev);
1653 
1654 	if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1655 		return snprintf(buf, 20, "unknown\n");
1656 	return get_fc_port_roles_names(vport->roles, buf);
1657 }
1658 static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1659 
1660 fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1661 
1662 fc_private_vport_show_function(symbolic_name, "%s\n",
1663 		FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1664 fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1665 static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1666 		show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1667 
1668 static ssize_t
1669 store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1670 		      const char *buf, size_t count)
1671 {
1672 	struct fc_vport *vport = transport_class_to_vport(dev);
1673 	struct Scsi_Host *shost = vport_to_shost(vport);
1674 	unsigned long flags;
1675 
1676 	spin_lock_irqsave(shost->host_lock, flags);
1677 	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
1678 		spin_unlock_irqrestore(shost->host_lock, flags);
1679 		return -EBUSY;
1680 	}
1681 	vport->flags |= FC_VPORT_DELETING;
1682 	spin_unlock_irqrestore(shost->host_lock, flags);
1683 
1684 	fc_queue_work(shost, &vport->vport_delete_work);
1685 	return count;
1686 }
1687 static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1688 			NULL, store_fc_vport_delete);
1689 
1690 
1691 /*
1692  * Enable/Disable vport
1693  *  Write "1" to disable, write "0" to enable
1694  */
1695 static ssize_t
1696 store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1697 		       const char *buf,
1698 			   size_t count)
1699 {
1700 	struct fc_vport *vport = transport_class_to_vport(dev);
1701 	struct Scsi_Host *shost = vport_to_shost(vport);
1702 	struct fc_internal *i = to_fc_internal(shost->transportt);
1703 	int stat;
1704 
1705 	if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1706 		return -EBUSY;
1707 
1708 	if (*buf == '0') {
1709 		if (vport->vport_state != FC_VPORT_DISABLED)
1710 			return -EALREADY;
1711 	} else if (*buf == '1') {
1712 		if (vport->vport_state == FC_VPORT_DISABLED)
1713 			return -EALREADY;
1714 	} else
1715 		return -EINVAL;
1716 
1717 	stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1718 	return stat ? stat : count;
1719 }
1720 static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1721 			NULL, store_fc_vport_disable);
1722 
1723 
1724 /*
1725  * Host Attribute Management
1726  */
1727 
1728 #define fc_host_show_function(field, format_string, sz, cast)		\
1729 static ssize_t								\
1730 show_fc_host_##field (struct device *dev,				\
1731 		      struct device_attribute *attr, char *buf)		\
1732 {									\
1733 	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1734 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1735 	if (i->f->get_host_##field)					\
1736 		i->f->get_host_##field(shost);				\
1737 	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1738 }
1739 
1740 #define fc_host_store_function(field)					\
1741 static ssize_t								\
1742 store_fc_host_##field(struct device *dev, 				\
1743 		      struct device_attribute *attr,			\
1744 		      const char *buf,	size_t count)			\
1745 {									\
1746 	int val;							\
1747 	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1748 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1749 	char *cp;							\
1750 									\
1751 	val = simple_strtoul(buf, &cp, 0);				\
1752 	if (*cp && (*cp != '\n'))					\
1753 		return -EINVAL;						\
1754 	i->f->set_host_##field(shost, val);				\
1755 	return count;							\
1756 }
1757 
1758 #define fc_host_store_str_function(field, slen)				\
1759 static ssize_t								\
1760 store_fc_host_##field(struct device *dev,				\
1761 		      struct device_attribute *attr,			\
1762 		      const char *buf, size_t count)			\
1763 {									\
1764 	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1765 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1766 	unsigned int cnt=count;						\
1767 									\
1768 	/* count may include a LF at end of string */			\
1769 	if (buf[cnt-1] == '\n')						\
1770 		cnt--;							\
1771 	if (cnt > ((slen) - 1))						\
1772 		return -EINVAL;						\
1773 	memcpy(fc_host_##field(shost), buf, cnt);			\
1774 	i->f->set_host_##field(shost);					\
1775 	return count;							\
1776 }
1777 
1778 #define fc_host_rd_attr(field, format_string, sz)			\
1779 	fc_host_show_function(field, format_string, sz, )		\
1780 static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
1781 			 show_fc_host_##field, NULL)
1782 
1783 #define fc_host_rd_attr_cast(field, format_string, sz, cast)		\
1784 	fc_host_show_function(field, format_string, sz, (cast))		\
1785 static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
1786 			  show_fc_host_##field, NULL)
1787 
1788 #define fc_host_rw_attr(field, format_string, sz)			\
1789 	fc_host_show_function(field, format_string, sz, )		\
1790 	fc_host_store_function(field)					\
1791 static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,		\
1792 			show_fc_host_##field,				\
1793 			store_fc_host_##field)
1794 
1795 #define fc_host_rd_enum_attr(title, maxlen)				\
1796 static ssize_t								\
1797 show_fc_host_##title (struct device *dev,				\
1798 		      struct device_attribute *attr, char *buf)		\
1799 {									\
1800 	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1801 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
1802 	const char *name;						\
1803 	if (i->f->get_host_##title)					\
1804 		i->f->get_host_##title(shost);				\
1805 	name = get_fc_##title##_name(fc_host_##title(shost));		\
1806 	if (!name)							\
1807 		return -EINVAL;						\
1808 	return snprintf(buf, maxlen, "%s\n", name);			\
1809 }									\
1810 static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1811 
1812 #define SETUP_HOST_ATTRIBUTE_RD(field)					\
1813 	i->private_host_attrs[count] = device_attr_host_##field;	\
1814 	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
1815 	i->private_host_attrs[count].store = NULL;			\
1816 	i->host_attrs[count] = &i->private_host_attrs[count];		\
1817 	if (i->f->show_host_##field)					\
1818 		count++
1819 
1820 #define SETUP_HOST_ATTRIBUTE_RD_NS(field)				\
1821 	i->private_host_attrs[count] = device_attr_host_##field;	\
1822 	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
1823 	i->private_host_attrs[count].store = NULL;			\
1824 	i->host_attrs[count] = &i->private_host_attrs[count];		\
1825 	count++
1826 
1827 #define SETUP_HOST_ATTRIBUTE_RW(field)					\
1828 	i->private_host_attrs[count] = device_attr_host_##field;	\
1829 	if (!i->f->set_host_##field) {					\
1830 		i->private_host_attrs[count].attr.mode = S_IRUGO;	\
1831 		i->private_host_attrs[count].store = NULL;		\
1832 	}								\
1833 	i->host_attrs[count] = &i->private_host_attrs[count];		\
1834 	if (i->f->show_host_##field)					\
1835 		count++
1836 
1837 
1838 #define fc_private_host_show_function(field, format_string, sz, cast)	\
1839 static ssize_t								\
1840 show_fc_host_##field (struct device *dev,				\
1841 		      struct device_attribute *attr, char *buf)		\
1842 {									\
1843 	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1844 	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1845 }
1846 
1847 #define fc_private_host_rd_attr(field, format_string, sz)		\
1848 	fc_private_host_show_function(field, format_string, sz, )	\
1849 static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
1850 			 show_fc_host_##field, NULL)
1851 
1852 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast)	\
1853 	fc_private_host_show_function(field, format_string, sz, (cast)) \
1854 static FC_DEVICE_ATTR(host, field, S_IRUGO,			\
1855 			  show_fc_host_##field, NULL)
1856 
1857 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)			\
1858 	i->private_host_attrs[count] = device_attr_host_##field;	\
1859 	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
1860 	i->private_host_attrs[count].store = NULL;			\
1861 	i->host_attrs[count] = &i->private_host_attrs[count];		\
1862 	count++
1863 
1864 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)			\
1865 {									\
1866 	i->private_host_attrs[count] = device_attr_host_##field;	\
1867 	i->host_attrs[count] = &i->private_host_attrs[count];		\
1868 	count++;							\
1869 }
1870 
1871 
1872 /* Fixed Host Attributes */
1873 
1874 static ssize_t
1875 show_fc_host_supported_classes (struct device *dev,
1876 			        struct device_attribute *attr, char *buf)
1877 {
1878 	struct Scsi_Host *shost = transport_class_to_shost(dev);
1879 
1880 	if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1881 		return snprintf(buf, 20, "unspecified\n");
1882 
1883 	return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1884 }
1885 static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1886 		show_fc_host_supported_classes, NULL);
1887 
1888 static ssize_t
1889 show_fc_host_supported_fc4s (struct device *dev,
1890 			     struct device_attribute *attr, char *buf)
1891 {
1892 	struct Scsi_Host *shost = transport_class_to_shost(dev);
1893 	return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1894 }
1895 static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1896 		show_fc_host_supported_fc4s, NULL);
1897 
1898 static ssize_t
1899 show_fc_host_supported_speeds (struct device *dev,
1900 			       struct device_attribute *attr, char *buf)
1901 {
1902 	struct Scsi_Host *shost = transport_class_to_shost(dev);
1903 
1904 	if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1905 		return snprintf(buf, 20, "unknown\n");
1906 
1907 	return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1908 }
1909 static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1910 		show_fc_host_supported_speeds, NULL);
1911 
1912 
1913 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1914 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1915 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1916 			     unsigned long long);
1917 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1918 fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
1919 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1920 fc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1);
1921 fc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1922 fc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1923 fc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1924 fc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1925 fc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1926 fc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
1927 
1928 
1929 /* Dynamic Host Attributes */
1930 
1931 static ssize_t
1932 show_fc_host_active_fc4s (struct device *dev,
1933 			  struct device_attribute *attr, char *buf)
1934 {
1935 	struct Scsi_Host *shost = transport_class_to_shost(dev);
1936 	struct fc_internal *i = to_fc_internal(shost->transportt);
1937 
1938 	if (i->f->get_host_active_fc4s)
1939 		i->f->get_host_active_fc4s(shost);
1940 
1941 	return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1942 }
1943 static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1944 		show_fc_host_active_fc4s, NULL);
1945 
1946 static ssize_t
1947 show_fc_host_speed (struct device *dev,
1948 		    struct device_attribute *attr, char *buf)
1949 {
1950 	struct Scsi_Host *shost = transport_class_to_shost(dev);
1951 	struct fc_internal *i = to_fc_internal(shost->transportt);
1952 
1953 	if (i->f->get_host_speed)
1954 		i->f->get_host_speed(shost);
1955 
1956 	if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1957 		return snprintf(buf, 20, "unknown\n");
1958 
1959 	return get_fc_port_speed_names(fc_host_speed(shost), buf);
1960 }
1961 static FC_DEVICE_ATTR(host, speed, S_IRUGO,
1962 		show_fc_host_speed, NULL);
1963 
1964 
1965 fc_host_rd_attr(port_id, "0x%06x\n", 20);
1966 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1967 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1968 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1969 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1970 
1971 fc_private_host_show_function(system_hostname, "%s\n",
1972 		FC_SYMBOLIC_NAME_SIZE + 1, )
1973 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1974 static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1975 		show_fc_host_system_hostname, store_fc_host_system_hostname);
1976 
1977 
1978 /* Private Host Attributes */
1979 
1980 static ssize_t
1981 show_fc_private_host_tgtid_bind_type(struct device *dev,
1982 				     struct device_attribute *attr, char *buf)
1983 {
1984 	struct Scsi_Host *shost = transport_class_to_shost(dev);
1985 	const char *name;
1986 
1987 	name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1988 	if (!name)
1989 		return -EINVAL;
1990 	return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1991 }
1992 
1993 #define get_list_head_entry(pos, head, member) 		\
1994 	pos = list_entry((head)->next, typeof(*pos), member)
1995 
1996 static ssize_t
1997 store_fc_private_host_tgtid_bind_type(struct device *dev,
1998 	struct device_attribute *attr, const char *buf, size_t count)
1999 {
2000 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2001 	struct fc_rport *rport;
2002  	enum fc_tgtid_binding_type val;
2003 	unsigned long flags;
2004 
2005 	if (get_fc_tgtid_bind_type_match(buf, &val))
2006 		return -EINVAL;
2007 
2008 	/* if changing bind type, purge all unused consistent bindings */
2009 	if (val != fc_host_tgtid_bind_type(shost)) {
2010 		spin_lock_irqsave(shost->host_lock, flags);
2011 		while (!list_empty(&fc_host_rport_bindings(shost))) {
2012 			get_list_head_entry(rport,
2013 				&fc_host_rport_bindings(shost), peers);
2014 			list_del(&rport->peers);
2015 			rport->port_state = FC_PORTSTATE_DELETED;
2016 			fc_queue_work(shost, &rport->rport_delete_work);
2017 		}
2018 		spin_unlock_irqrestore(shost->host_lock, flags);
2019 	}
2020 
2021 	fc_host_tgtid_bind_type(shost) = val;
2022 	return count;
2023 }
2024 
2025 static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
2026 			show_fc_private_host_tgtid_bind_type,
2027 			store_fc_private_host_tgtid_bind_type);
2028 
2029 static ssize_t
2030 store_fc_private_host_issue_lip(struct device *dev,
2031 	struct device_attribute *attr, const char *buf, size_t count)
2032 {
2033 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2034 	struct fc_internal *i = to_fc_internal(shost->transportt);
2035 	int ret;
2036 
2037 	/* ignore any data value written to the attribute */
2038 	if (i->f->issue_fc_host_lip) {
2039 		ret = i->f->issue_fc_host_lip(shost);
2040 		return ret ? ret: count;
2041 	}
2042 
2043 	return -ENOENT;
2044 }
2045 
2046 static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
2047 			store_fc_private_host_issue_lip);
2048 
2049 static ssize_t
2050 store_fc_private_host_dev_loss_tmo(struct device *dev,
2051 				   struct device_attribute *attr,
2052 				   const char *buf, size_t count)
2053 {
2054 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2055 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2056 	struct fc_rport *rport;
2057 	unsigned long val, flags;
2058 	int rc;
2059 
2060 	rc = fc_str_to_dev_loss(buf, &val);
2061 	if (rc)
2062 		return rc;
2063 
2064 	fc_host_dev_loss_tmo(shost) = val;
2065 	spin_lock_irqsave(shost->host_lock, flags);
2066 	list_for_each_entry(rport, &fc_host->rports, peers)
2067 		fc_rport_set_dev_loss_tmo(rport, val);
2068 	spin_unlock_irqrestore(shost->host_lock, flags);
2069 	return count;
2070 }
2071 
2072 fc_private_host_show_function(dev_loss_tmo, "%d\n", 20, );
2073 static FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR,
2074 		      show_fc_host_dev_loss_tmo,
2075 		      store_fc_private_host_dev_loss_tmo);
2076 
2077 fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
2078 
2079 /*
2080  * Host Statistics Management
2081  */
2082 
2083 /* Show a given attribute in the statistics group */
2084 static ssize_t
2085 fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
2086 {
2087 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2088 	struct fc_internal *i = to_fc_internal(shost->transportt);
2089 	struct fc_host_statistics *stats;
2090 	ssize_t ret = -ENOENT;
2091 
2092 	if (offset > sizeof(struct fc_host_statistics) ||
2093 	    offset % sizeof(u64) != 0)
2094 		WARN_ON(1);
2095 
2096 	if (i->f->get_fc_host_stats) {
2097 		stats = (i->f->get_fc_host_stats)(shost);
2098 		if (stats)
2099 			ret = snprintf(buf, 20, "0x%llx\n",
2100 			      (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
2101 	}
2102 	return ret;
2103 }
2104 
2105 
2106 /* generate a read-only statistics attribute */
2107 #define fc_host_statistic(name)						\
2108 static ssize_t show_fcstat_##name(struct device *cd,			\
2109 				  struct device_attribute *attr,	\
2110 				  char *buf)				\
2111 {									\
2112 	return fc_stat_show(cd, buf, 					\
2113 			    offsetof(struct fc_host_statistics, name));	\
2114 }									\
2115 static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
2116 
2117 fc_host_statistic(seconds_since_last_reset);
2118 fc_host_statistic(tx_frames);
2119 fc_host_statistic(tx_words);
2120 fc_host_statistic(rx_frames);
2121 fc_host_statistic(rx_words);
2122 fc_host_statistic(lip_count);
2123 fc_host_statistic(nos_count);
2124 fc_host_statistic(error_frames);
2125 fc_host_statistic(dumped_frames);
2126 fc_host_statistic(link_failure_count);
2127 fc_host_statistic(loss_of_sync_count);
2128 fc_host_statistic(loss_of_signal_count);
2129 fc_host_statistic(prim_seq_protocol_err_count);
2130 fc_host_statistic(invalid_tx_word_count);
2131 fc_host_statistic(invalid_crc_count);
2132 fc_host_statistic(fcp_input_requests);
2133 fc_host_statistic(fcp_output_requests);
2134 fc_host_statistic(fcp_control_requests);
2135 fc_host_statistic(fcp_input_megabytes);
2136 fc_host_statistic(fcp_output_megabytes);
2137 fc_host_statistic(fcp_packet_alloc_failures);
2138 fc_host_statistic(fcp_packet_aborts);
2139 fc_host_statistic(fcp_frame_alloc_failures);
2140 fc_host_statistic(fc_no_free_exch);
2141 fc_host_statistic(fc_no_free_exch_xid);
2142 fc_host_statistic(fc_xid_not_found);
2143 fc_host_statistic(fc_xid_busy);
2144 fc_host_statistic(fc_seq_not_found);
2145 fc_host_statistic(fc_non_bls_resp);
2146 fc_host_statistic(cn_sig_warn);
2147 fc_host_statistic(cn_sig_alarm);
2148 
2149 
2150 #define fc_host_fpin_statistic(name)					\
2151 static ssize_t fc_host_fpinstat_##name(struct device *cd,		\
2152 				  struct device_attribute *attr,	\
2153 				  char *buf)				\
2154 {									\
2155 	struct Scsi_Host *shost = transport_class_to_shost(cd);		\
2156 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);	\
2157 									\
2158 	return snprintf(buf, 20, "0x%llx\n", fc_host->fpin_stats.name);	\
2159 }									\
2160 static FC_DEVICE_ATTR(host, fpin_##name, 0444, fc_host_fpinstat_##name, NULL)
2161 
2162 fc_host_fpin_statistic(dn);
2163 fc_host_fpin_statistic(dn_unknown);
2164 fc_host_fpin_statistic(dn_timeout);
2165 fc_host_fpin_statistic(dn_unable_to_route);
2166 fc_host_fpin_statistic(dn_device_specific);
2167 fc_host_fpin_statistic(cn);
2168 fc_host_fpin_statistic(cn_clear);
2169 fc_host_fpin_statistic(cn_lost_credit);
2170 fc_host_fpin_statistic(cn_credit_stall);
2171 fc_host_fpin_statistic(cn_oversubscription);
2172 fc_host_fpin_statistic(cn_device_specific);
2173 fc_host_fpin_statistic(li);
2174 fc_host_fpin_statistic(li_failure_unknown);
2175 fc_host_fpin_statistic(li_link_failure_count);
2176 fc_host_fpin_statistic(li_loss_of_sync_count);
2177 fc_host_fpin_statistic(li_loss_of_signals_count);
2178 fc_host_fpin_statistic(li_prim_seq_err_count);
2179 fc_host_fpin_statistic(li_invalid_tx_word_count);
2180 fc_host_fpin_statistic(li_invalid_crc_count);
2181 fc_host_fpin_statistic(li_device_specific);
2182 
2183 static ssize_t
2184 fc_reset_statistics(struct device *dev, struct device_attribute *attr,
2185 		    const char *buf, size_t count)
2186 {
2187 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2188 	struct fc_internal *i = to_fc_internal(shost->transportt);
2189 
2190 	/* ignore any data value written to the attribute */
2191 	if (i->f->reset_fc_host_stats) {
2192 		i->f->reset_fc_host_stats(shost);
2193 		return count;
2194 	}
2195 
2196 	return -ENOENT;
2197 }
2198 static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
2199 				fc_reset_statistics);
2200 
2201 static struct attribute *fc_statistics_attrs[] = {
2202 	&device_attr_host_seconds_since_last_reset.attr,
2203 	&device_attr_host_tx_frames.attr,
2204 	&device_attr_host_tx_words.attr,
2205 	&device_attr_host_rx_frames.attr,
2206 	&device_attr_host_rx_words.attr,
2207 	&device_attr_host_lip_count.attr,
2208 	&device_attr_host_nos_count.attr,
2209 	&device_attr_host_error_frames.attr,
2210 	&device_attr_host_dumped_frames.attr,
2211 	&device_attr_host_link_failure_count.attr,
2212 	&device_attr_host_loss_of_sync_count.attr,
2213 	&device_attr_host_loss_of_signal_count.attr,
2214 	&device_attr_host_prim_seq_protocol_err_count.attr,
2215 	&device_attr_host_invalid_tx_word_count.attr,
2216 	&device_attr_host_invalid_crc_count.attr,
2217 	&device_attr_host_fcp_input_requests.attr,
2218 	&device_attr_host_fcp_output_requests.attr,
2219 	&device_attr_host_fcp_control_requests.attr,
2220 	&device_attr_host_fcp_input_megabytes.attr,
2221 	&device_attr_host_fcp_output_megabytes.attr,
2222 	&device_attr_host_fcp_packet_alloc_failures.attr,
2223 	&device_attr_host_fcp_packet_aborts.attr,
2224 	&device_attr_host_fcp_frame_alloc_failures.attr,
2225 	&device_attr_host_fc_no_free_exch.attr,
2226 	&device_attr_host_fc_no_free_exch_xid.attr,
2227 	&device_attr_host_fc_xid_not_found.attr,
2228 	&device_attr_host_fc_xid_busy.attr,
2229 	&device_attr_host_fc_seq_not_found.attr,
2230 	&device_attr_host_fc_non_bls_resp.attr,
2231 	&device_attr_host_cn_sig_warn.attr,
2232 	&device_attr_host_cn_sig_alarm.attr,
2233 	&device_attr_host_reset_statistics.attr,
2234 	&device_attr_host_fpin_dn.attr,
2235 	&device_attr_host_fpin_dn_unknown.attr,
2236 	&device_attr_host_fpin_dn_timeout.attr,
2237 	&device_attr_host_fpin_dn_unable_to_route.attr,
2238 	&device_attr_host_fpin_dn_device_specific.attr,
2239 	&device_attr_host_fpin_li.attr,
2240 	&device_attr_host_fpin_li_failure_unknown.attr,
2241 	&device_attr_host_fpin_li_link_failure_count.attr,
2242 	&device_attr_host_fpin_li_loss_of_sync_count.attr,
2243 	&device_attr_host_fpin_li_loss_of_signals_count.attr,
2244 	&device_attr_host_fpin_li_prim_seq_err_count.attr,
2245 	&device_attr_host_fpin_li_invalid_tx_word_count.attr,
2246 	&device_attr_host_fpin_li_invalid_crc_count.attr,
2247 	&device_attr_host_fpin_li_device_specific.attr,
2248 	&device_attr_host_fpin_cn.attr,
2249 	&device_attr_host_fpin_cn_clear.attr,
2250 	&device_attr_host_fpin_cn_lost_credit.attr,
2251 	&device_attr_host_fpin_cn_credit_stall.attr,
2252 	&device_attr_host_fpin_cn_oversubscription.attr,
2253 	&device_attr_host_fpin_cn_device_specific.attr,
2254 	NULL
2255 };
2256 
2257 static struct attribute_group fc_statistics_group = {
2258 	.name = "statistics",
2259 	.attrs = fc_statistics_attrs,
2260 };
2261 
2262 
2263 /* Host Vport Attributes */
2264 
2265 static int
2266 fc_parse_wwn(const char *ns, u64 *nm)
2267 {
2268 	unsigned int i, j;
2269 	u8 wwn[8];
2270 
2271 	memset(wwn, 0, sizeof(wwn));
2272 
2273 	/* Validate and store the new name */
2274 	for (i=0, j=0; i < 16; i++) {
2275 		int value;
2276 
2277 		value = hex_to_bin(*ns++);
2278 		if (value >= 0)
2279 			j = (j << 4) | value;
2280 		else
2281 			return -EINVAL;
2282 		if (i % 2) {
2283 			wwn[i/2] = j & 0xff;
2284 			j = 0;
2285 		}
2286 	}
2287 
2288 	*nm = wwn_to_u64(wwn);
2289 
2290 	return 0;
2291 }
2292 
2293 
2294 /*
2295  * "Short-cut" sysfs variable to create a new vport on a FC Host.
2296  * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
2297  * will default to a NPIV-based FCP_Initiator; The WWNs are specified
2298  * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
2299  */
2300 static ssize_t
2301 store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
2302 			   const char *buf, size_t count)
2303 {
2304 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2305 	struct fc_vport_identifiers vid;
2306 	struct fc_vport *vport;
2307 	unsigned int cnt=count;
2308 	int stat;
2309 
2310 	memset(&vid, 0, sizeof(vid));
2311 
2312 	/* count may include a LF at end of string */
2313 	if (buf[cnt-1] == '\n')
2314 		cnt--;
2315 
2316 	/* validate we have enough characters for WWPN */
2317 	if ((cnt != (16+1+16)) || (buf[16] != ':'))
2318 		return -EINVAL;
2319 
2320 	stat = fc_parse_wwn(&buf[0], &vid.port_name);
2321 	if (stat)
2322 		return stat;
2323 
2324 	stat = fc_parse_wwn(&buf[17], &vid.node_name);
2325 	if (stat)
2326 		return stat;
2327 
2328 	vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
2329 	vid.vport_type = FC_PORTTYPE_NPIV;
2330 	/* vid.symbolic_name is already zero/NULL's */
2331 	vid.disable = false;		/* always enabled */
2332 
2333 	/* we only allow support on Channel 0 !!! */
2334 	stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
2335 	return stat ? stat : count;
2336 }
2337 static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
2338 			store_fc_host_vport_create);
2339 
2340 
2341 /*
2342  * "Short-cut" sysfs variable to delete a vport on a FC Host.
2343  * Vport is identified by a string containing "<WWPN>:<WWNN>".
2344  * The WWNs are specified as hex characters, and may *not* contain
2345  * any prefixes (e.g. 0x, x, etc)
2346  */
2347 static ssize_t
2348 store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
2349 			   const char *buf, size_t count)
2350 {
2351 	struct Scsi_Host *shost = transport_class_to_shost(dev);
2352 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2353 	struct fc_vport *vport;
2354 	u64 wwpn, wwnn;
2355 	unsigned long flags;
2356 	unsigned int cnt=count;
2357 	int stat, match;
2358 
2359 	/* count may include a LF at end of string */
2360 	if (buf[cnt-1] == '\n')
2361 		cnt--;
2362 
2363 	/* validate we have enough characters for WWPN */
2364 	if ((cnt != (16+1+16)) || (buf[16] != ':'))
2365 		return -EINVAL;
2366 
2367 	stat = fc_parse_wwn(&buf[0], &wwpn);
2368 	if (stat)
2369 		return stat;
2370 
2371 	stat = fc_parse_wwn(&buf[17], &wwnn);
2372 	if (stat)
2373 		return stat;
2374 
2375 	spin_lock_irqsave(shost->host_lock, flags);
2376 	match = 0;
2377 	/* we only allow support on Channel 0 !!! */
2378 	list_for_each_entry(vport, &fc_host->vports, peers) {
2379 		if ((vport->channel == 0) &&
2380 		    (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
2381 			if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
2382 				break;
2383 			vport->flags |= FC_VPORT_DELETING;
2384 			match = 1;
2385 			break;
2386 		}
2387 	}
2388 	spin_unlock_irqrestore(shost->host_lock, flags);
2389 
2390 	if (!match)
2391 		return -ENODEV;
2392 
2393 	stat = fc_vport_terminate(vport);
2394 	return stat ? stat : count;
2395 }
2396 static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
2397 			store_fc_host_vport_delete);
2398 
2399 
2400 static int fc_host_match(struct attribute_container *cont,
2401 			  struct device *dev)
2402 {
2403 	struct Scsi_Host *shost;
2404 	struct fc_internal *i;
2405 
2406 	if (!scsi_is_host_device(dev))
2407 		return 0;
2408 
2409 	shost = dev_to_shost(dev);
2410 	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
2411 	    != &fc_host_class.class)
2412 		return 0;
2413 
2414 	i = to_fc_internal(shost->transportt);
2415 
2416 	return &i->t.host_attrs.ac == cont;
2417 }
2418 
2419 static int fc_target_match(struct attribute_container *cont,
2420 			    struct device *dev)
2421 {
2422 	struct Scsi_Host *shost;
2423 	struct fc_internal *i;
2424 
2425 	if (!scsi_is_target_device(dev))
2426 		return 0;
2427 
2428 	shost = dev_to_shost(dev->parent);
2429 	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
2430 	    != &fc_host_class.class)
2431 		return 0;
2432 
2433 	i = to_fc_internal(shost->transportt);
2434 
2435 	return &i->t.target_attrs.ac == cont;
2436 }
2437 
2438 static void fc_rport_dev_release(struct device *dev)
2439 {
2440 	struct fc_rport *rport = dev_to_rport(dev);
2441 	put_device(dev->parent);
2442 	kfree(rport);
2443 }
2444 
2445 int scsi_is_fc_rport(const struct device *dev)
2446 {
2447 	return dev->release == fc_rport_dev_release;
2448 }
2449 EXPORT_SYMBOL(scsi_is_fc_rport);
2450 
2451 static int fc_rport_match(struct attribute_container *cont,
2452 			    struct device *dev)
2453 {
2454 	struct Scsi_Host *shost;
2455 	struct fc_internal *i;
2456 
2457 	if (!scsi_is_fc_rport(dev))
2458 		return 0;
2459 
2460 	shost = dev_to_shost(dev->parent);
2461 	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
2462 	    != &fc_host_class.class)
2463 		return 0;
2464 
2465 	i = to_fc_internal(shost->transportt);
2466 
2467 	return &i->rport_attr_cont.ac == cont;
2468 }
2469 
2470 
2471 static void fc_vport_dev_release(struct device *dev)
2472 {
2473 	struct fc_vport *vport = dev_to_vport(dev);
2474 	put_device(dev->parent);		/* release kobj parent */
2475 	kfree(vport);
2476 }
2477 
2478 static int scsi_is_fc_vport(const struct device *dev)
2479 {
2480 	return dev->release == fc_vport_dev_release;
2481 }
2482 
2483 static int fc_vport_match(struct attribute_container *cont,
2484 			    struct device *dev)
2485 {
2486 	struct fc_vport *vport;
2487 	struct Scsi_Host *shost;
2488 	struct fc_internal *i;
2489 
2490 	if (!scsi_is_fc_vport(dev))
2491 		return 0;
2492 	vport = dev_to_vport(dev);
2493 
2494 	shost = vport_to_shost(vport);
2495 	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
2496 	    != &fc_host_class.class)
2497 		return 0;
2498 
2499 	i = to_fc_internal(shost->transportt);
2500 	return &i->vport_attr_cont.ac == cont;
2501 }
2502 
2503 
2504 /**
2505  * fc_eh_timed_out - FC Transport I/O timeout intercept handler
2506  * @scmd:	The SCSI command which timed out
2507  *
2508  * This routine protects against error handlers getting invoked while a
2509  * rport is in a blocked state, typically due to a temporarily loss of
2510  * connectivity. If the error handlers are allowed to proceed, requests
2511  * to abort i/o, reset the target, etc will likely fail as there is no way
2512  * to communicate with the device to perform the requested function. These
2513  * failures may result in the midlayer taking the device offline, requiring
2514  * manual intervention to restore operation.
2515  *
2516  * This routine, called whenever an i/o times out, validates the state of
2517  * the underlying rport. If the rport is blocked, it returns
2518  * EH_RESET_TIMER, which will continue to reschedule the timeout.
2519  * Eventually, either the device will return, or devloss_tmo will fire,
2520  * and when the timeout then fires, it will be handled normally.
2521  * If the rport is not blocked, normal error handling continues.
2522  *
2523  * Notes:
2524  *	This routine assumes no locks are held on entry.
2525  */
2526 enum scsi_timeout_action fc_eh_timed_out(struct scsi_cmnd *scmd)
2527 {
2528 	struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
2529 
2530 	if (rport->port_state == FC_PORTSTATE_BLOCKED)
2531 		return SCSI_EH_RESET_TIMER;
2532 
2533 	return SCSI_EH_NOT_HANDLED;
2534 }
2535 EXPORT_SYMBOL(fc_eh_timed_out);
2536 
2537 /*
2538  * Called by fc_user_scan to locate an rport on the shost that
2539  * matches the channel and target id, and invoke scsi_scan_target()
2540  * on the rport.
2541  */
2542 static void
2543 fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
2544 {
2545 	struct fc_rport *rport;
2546 	unsigned long flags;
2547 
2548 	spin_lock_irqsave(shost->host_lock, flags);
2549 
2550 	list_for_each_entry(rport, &fc_host_rports(shost), peers) {
2551 		if (rport->scsi_target_id == -1)
2552 			continue;
2553 
2554 		if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
2555 			(rport->port_state != FC_PORTSTATE_MARGINAL))
2556 			continue;
2557 
2558 		if ((channel == rport->channel) &&
2559 		    (id == rport->scsi_target_id)) {
2560 			spin_unlock_irqrestore(shost->host_lock, flags);
2561 			scsi_scan_target(&rport->dev, channel, id, lun,
2562 					 SCSI_SCAN_MANUAL);
2563 			return;
2564 		}
2565 	}
2566 
2567 	spin_unlock_irqrestore(shost->host_lock, flags);
2568 }
2569 
2570 /*
2571  * Called via sysfs scan routines. Necessary, as the FC transport
2572  * wants to place all target objects below the rport object. So this
2573  * routine must invoke the scsi_scan_target() routine with the rport
2574  * object as the parent.
2575  */
2576 static int
2577 fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
2578 {
2579 	uint chlo, chhi;
2580 	uint tgtlo, tgthi;
2581 
2582 	if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2583 	    ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2584 	    ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2585 		return -EINVAL;
2586 
2587 	if (channel == SCAN_WILD_CARD) {
2588 		chlo = 0;
2589 		chhi = shost->max_channel + 1;
2590 	} else {
2591 		chlo = channel;
2592 		chhi = channel + 1;
2593 	}
2594 
2595 	if (id == SCAN_WILD_CARD) {
2596 		tgtlo = 0;
2597 		tgthi = shost->max_id;
2598 	} else {
2599 		tgtlo = id;
2600 		tgthi = id + 1;
2601 	}
2602 
2603 	for ( ; chlo < chhi; chlo++)
2604 		for ( ; tgtlo < tgthi; tgtlo++)
2605 			fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2606 
2607 	return 0;
2608 }
2609 
2610 struct scsi_transport_template *
2611 fc_attach_transport(struct fc_function_template *ft)
2612 {
2613 	int count;
2614 	struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2615 					GFP_KERNEL);
2616 
2617 	if (unlikely(!i))
2618 		return NULL;
2619 
2620 	i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2621 	i->t.target_attrs.ac.class = &fc_transport_class.class;
2622 	i->t.target_attrs.ac.match = fc_target_match;
2623 	i->t.target_size = sizeof(struct fc_starget_attrs);
2624 	transport_container_register(&i->t.target_attrs);
2625 
2626 	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2627 	i->t.host_attrs.ac.class = &fc_host_class.class;
2628 	i->t.host_attrs.ac.match = fc_host_match;
2629 	i->t.host_size = sizeof(struct fc_host_attrs);
2630 	if (ft->get_fc_host_stats)
2631 		i->t.host_attrs.statistics = &fc_statistics_group;
2632 	transport_container_register(&i->t.host_attrs);
2633 
2634 	i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2635 	i->rport_attr_cont.ac.class = &fc_rport_class.class;
2636 	i->rport_attr_cont.ac.match = fc_rport_match;
2637 	i->rport_attr_cont.statistics = &fc_rport_statistics_group;
2638 	transport_container_register(&i->rport_attr_cont);
2639 
2640 	i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2641 	i->vport_attr_cont.ac.class = &fc_vport_class.class;
2642 	i->vport_attr_cont.ac.match = fc_vport_match;
2643 	transport_container_register(&i->vport_attr_cont);
2644 
2645 	i->f = ft;
2646 
2647 	/* Transport uses the shost workq for scsi scanning */
2648 	i->t.create_work_queue = 1;
2649 
2650 	i->t.user_scan = fc_user_scan;
2651 
2652 	/*
2653 	 * Setup SCSI Target Attributes.
2654 	 */
2655 	count = 0;
2656 	SETUP_STARGET_ATTRIBUTE_RD(node_name);
2657 	SETUP_STARGET_ATTRIBUTE_RD(port_name);
2658 	SETUP_STARGET_ATTRIBUTE_RD(port_id);
2659 
2660 	BUG_ON(count > FC_STARGET_NUM_ATTRS);
2661 
2662 	i->starget_attrs[count] = NULL;
2663 
2664 
2665 	/*
2666 	 * Setup SCSI Host Attributes.
2667 	 */
2668 	count=0;
2669 	SETUP_HOST_ATTRIBUTE_RD(node_name);
2670 	SETUP_HOST_ATTRIBUTE_RD(port_name);
2671 	SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
2672 	SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2673 	SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
2674 	SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2675 	SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2676 	if (ft->vport_create) {
2677 		SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2678 		SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2679 	}
2680 	SETUP_HOST_ATTRIBUTE_RD(serial_number);
2681 	SETUP_HOST_ATTRIBUTE_RD(manufacturer);
2682 	SETUP_HOST_ATTRIBUTE_RD(model);
2683 	SETUP_HOST_ATTRIBUTE_RD(model_description);
2684 	SETUP_HOST_ATTRIBUTE_RD(hardware_version);
2685 	SETUP_HOST_ATTRIBUTE_RD(driver_version);
2686 	SETUP_HOST_ATTRIBUTE_RD(firmware_version);
2687 	SETUP_HOST_ATTRIBUTE_RD(optionrom_version);
2688 
2689 	SETUP_HOST_ATTRIBUTE_RD(port_id);
2690 	SETUP_HOST_ATTRIBUTE_RD(port_type);
2691 	SETUP_HOST_ATTRIBUTE_RD(port_state);
2692 	SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2693 	SETUP_HOST_ATTRIBUTE_RD(speed);
2694 	SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2695 	SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2696 	SETUP_HOST_ATTRIBUTE_RW(system_hostname);
2697 
2698 	/* Transport-managed attributes */
2699 	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(dev_loss_tmo);
2700 	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
2701 	if (ft->issue_fc_host_lip)
2702 		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2703 	if (ft->vport_create)
2704 		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2705 	if (ft->vport_delete)
2706 		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
2707 
2708 	BUG_ON(count > FC_HOST_NUM_ATTRS);
2709 
2710 	i->host_attrs[count] = NULL;
2711 
2712 	/*
2713 	 * Setup Remote Port Attributes.
2714 	 */
2715 	count=0;
2716 	SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2717 	SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2718 	SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2719 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2720 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2721 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2722 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2723 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(port_state);
2724 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2725 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
2726 
2727 	BUG_ON(count > FC_RPORT_NUM_ATTRS);
2728 
2729 	i->rport_attrs[count] = NULL;
2730 
2731 	/*
2732 	 * Setup Virtual Port Attributes.
2733 	 */
2734 	count=0;
2735 	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2736 	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2737 	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2738 	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2739 	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2740 	SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2741 	SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2742 	SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2743 	SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2744 
2745 	BUG_ON(count > FC_VPORT_NUM_ATTRS);
2746 
2747 	i->vport_attrs[count] = NULL;
2748 
2749 	return &i->t;
2750 }
2751 EXPORT_SYMBOL(fc_attach_transport);
2752 
2753 void fc_release_transport(struct scsi_transport_template *t)
2754 {
2755 	struct fc_internal *i = to_fc_internal(t);
2756 
2757 	transport_container_unregister(&i->t.target_attrs);
2758 	transport_container_unregister(&i->t.host_attrs);
2759 	transport_container_unregister(&i->rport_attr_cont);
2760 	transport_container_unregister(&i->vport_attr_cont);
2761 
2762 	kfree(i);
2763 }
2764 EXPORT_SYMBOL(fc_release_transport);
2765 
2766 /**
2767  * fc_queue_work - Queue work to the fc_host workqueue.
2768  * @shost:	Pointer to Scsi_Host bound to fc_host.
2769  * @work:	Work to queue for execution.
2770  *
2771  * Return value:
2772  * 	1 - work queued for execution
2773  *	0 - work is already queued
2774  *	-EINVAL - work queue doesn't exist
2775  */
2776 static int
2777 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2778 {
2779 	if (unlikely(!fc_host_work_q(shost))) {
2780 		printk(KERN_ERR
2781 			"ERROR: FC host '%s' attempted to queue work, "
2782 			"when no workqueue created.\n", shost->hostt->name);
2783 		dump_stack();
2784 
2785 		return -EINVAL;
2786 	}
2787 
2788 	return queue_work(fc_host_work_q(shost), work);
2789 }
2790 
2791 /**
2792  * fc_flush_work - Flush a fc_host's workqueue.
2793  * @shost:	Pointer to Scsi_Host bound to fc_host.
2794  */
2795 static void
2796 fc_flush_work(struct Scsi_Host *shost)
2797 {
2798 	if (!fc_host_work_q(shost)) {
2799 		printk(KERN_ERR
2800 			"ERROR: FC host '%s' attempted to flush work, "
2801 			"when no workqueue created.\n", shost->hostt->name);
2802 		dump_stack();
2803 		return;
2804 	}
2805 
2806 	flush_workqueue(fc_host_work_q(shost));
2807 }
2808 
2809 /**
2810  * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2811  * @shost:	Pointer to Scsi_Host bound to fc_host.
2812  * @rport:	rport associated with the devloss work
2813  * @work:	Work to queue for execution.
2814  * @delay:	jiffies to delay the work queuing
2815  *
2816  * Return value:
2817  * 	1 on success / 0 already queued / < 0 for error
2818  */
2819 static int
2820 fc_queue_devloss_work(struct Scsi_Host *shost, struct fc_rport *rport,
2821 		      struct delayed_work *work, unsigned long delay)
2822 {
2823 	if (unlikely(!rport->devloss_work_q)) {
2824 		printk(KERN_ERR
2825 			"ERROR: FC host '%s' attempted to queue work, "
2826 			"when no workqueue created.\n", shost->hostt->name);
2827 		dump_stack();
2828 
2829 		return -EINVAL;
2830 	}
2831 
2832 	return queue_delayed_work(rport->devloss_work_q, work, delay);
2833 }
2834 
2835 /**
2836  * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2837  * @shost:	Pointer to Scsi_Host bound to fc_host.
2838  * @rport:	rport associated with the devloss work
2839  */
2840 static void
2841 fc_flush_devloss(struct Scsi_Host *shost, struct fc_rport *rport)
2842 {
2843 	if (unlikely(!rport->devloss_work_q)) {
2844 		printk(KERN_ERR
2845 			"ERROR: FC host '%s' attempted to flush work, "
2846 			"when no workqueue created.\n", shost->hostt->name);
2847 		dump_stack();
2848 		return;
2849 	}
2850 
2851 	flush_workqueue(rport->devloss_work_q);
2852 }
2853 
2854 
2855 /**
2856  * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2857  * @shost:	Which &Scsi_Host
2858  *
2859  * This routine is expected to be called immediately preceding the
2860  * a driver's call to scsi_remove_host().
2861  *
2862  * WARNING: A driver utilizing the fc_transport, which fails to call
2863  *   this routine prior to scsi_remove_host(), will leave dangling
2864  *   objects in /sys/class/fc_remote_ports. Access to any of these
2865  *   objects can result in a system crash !!!
2866  *
2867  * Notes:
2868  *	This routine assumes no locks are held on entry.
2869  */
2870 void
2871 fc_remove_host(struct Scsi_Host *shost)
2872 {
2873 	struct fc_vport *vport = NULL, *next_vport = NULL;
2874 	struct fc_rport *rport = NULL, *next_rport = NULL;
2875 	struct workqueue_struct *work_q;
2876 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2877 	unsigned long flags;
2878 
2879 	spin_lock_irqsave(shost->host_lock, flags);
2880 
2881 	/* Remove any vports */
2882 	list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2883 		vport->flags |= FC_VPORT_DELETING;
2884 		fc_queue_work(shost, &vport->vport_delete_work);
2885 	}
2886 
2887 	/* Remove any remote ports */
2888 	list_for_each_entry_safe(rport, next_rport,
2889 			&fc_host->rports, peers) {
2890 		list_del(&rport->peers);
2891 		rport->port_state = FC_PORTSTATE_DELETED;
2892 		fc_queue_work(shost, &rport->rport_delete_work);
2893 	}
2894 
2895 	list_for_each_entry_safe(rport, next_rport,
2896 			&fc_host->rport_bindings, peers) {
2897 		list_del(&rport->peers);
2898 		rport->port_state = FC_PORTSTATE_DELETED;
2899 		fc_queue_work(shost, &rport->rport_delete_work);
2900 	}
2901 
2902 	spin_unlock_irqrestore(shost->host_lock, flags);
2903 
2904 	/* flush all scan work items */
2905 	scsi_flush_work(shost);
2906 
2907 	/* flush all stgt delete, and rport delete work items, then kill it  */
2908 	if (fc_host->work_q) {
2909 		work_q = fc_host->work_q;
2910 		fc_host->work_q = NULL;
2911 		destroy_workqueue(work_q);
2912 	}
2913 }
2914 EXPORT_SYMBOL(fc_remove_host);
2915 
2916 static void fc_terminate_rport_io(struct fc_rport *rport)
2917 {
2918 	struct Scsi_Host *shost = rport_to_shost(rport);
2919 	struct fc_internal *i = to_fc_internal(shost->transportt);
2920 
2921 	/* Involve the LLDD if possible to terminate all io on the rport. */
2922 	if (i->f->terminate_rport_io)
2923 		i->f->terminate_rport_io(rport);
2924 
2925 	/*
2926 	 * Must unblock to flush queued IO. scsi-ml will fail incoming reqs.
2927 	 */
2928 	scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
2929 }
2930 
2931 /**
2932  * fc_starget_delete - called to delete the scsi descendants of an rport
2933  * @work:	remote port to be operated on.
2934  *
2935  * Deletes target and all sdevs.
2936  */
2937 static void
2938 fc_starget_delete(struct work_struct *work)
2939 {
2940 	struct fc_rport *rport =
2941 		container_of(work, struct fc_rport, stgt_delete_work);
2942 
2943 	fc_terminate_rport_io(rport);
2944 	scsi_remove_target(&rport->dev);
2945 }
2946 
2947 
2948 /**
2949  * fc_rport_final_delete - finish rport termination and delete it.
2950  * @work:	remote port to be deleted.
2951  */
2952 static void
2953 fc_rport_final_delete(struct work_struct *work)
2954 {
2955 	struct fc_rport *rport =
2956 		container_of(work, struct fc_rport, rport_delete_work);
2957 	struct device *dev = &rport->dev;
2958 	struct Scsi_Host *shost = rport_to_shost(rport);
2959 	struct fc_internal *i = to_fc_internal(shost->transportt);
2960 	struct workqueue_struct *work_q;
2961 	unsigned long flags;
2962 	int do_callback = 0;
2963 
2964 	fc_terminate_rport_io(rport);
2965 
2966 	/*
2967 	 * if a scan is pending, flush the SCSI Host work_q so that
2968 	 * that we can reclaim the rport scan work element.
2969 	 */
2970 	if (rport->flags & FC_RPORT_SCAN_PENDING)
2971 		scsi_flush_work(shost);
2972 
2973 	/*
2974 	 * Cancel any outstanding timers. These should really exist
2975 	 * only when rmmod'ing the LLDD and we're asking for
2976 	 * immediate termination of the rports
2977 	 */
2978 	spin_lock_irqsave(shost->host_lock, flags);
2979 	if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2980 		spin_unlock_irqrestore(shost->host_lock, flags);
2981 		if (!cancel_delayed_work(&rport->fail_io_work))
2982 			fc_flush_devloss(shost, rport);
2983 		if (!cancel_delayed_work(&rport->dev_loss_work))
2984 			fc_flush_devloss(shost, rport);
2985 		cancel_work_sync(&rport->scan_work);
2986 		spin_lock_irqsave(shost->host_lock, flags);
2987 		rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2988 	}
2989 	spin_unlock_irqrestore(shost->host_lock, flags);
2990 
2991 	/* Delete SCSI target and sdevs */
2992 	if (rport->scsi_target_id != -1)
2993 		fc_starget_delete(&rport->stgt_delete_work);
2994 
2995 	/*
2996 	 * Notify the driver that the rport is now dead. The LLDD will
2997 	 * also guarantee that any communication to the rport is terminated
2998 	 *
2999 	 * Avoid this call if we already called it when we preserved the
3000 	 * rport for the binding.
3001 	 */
3002 	spin_lock_irqsave(shost->host_lock, flags);
3003 	if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
3004 	    (i->f->dev_loss_tmo_callbk)) {
3005 		rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3006 		do_callback = 1;
3007 	}
3008 	spin_unlock_irqrestore(shost->host_lock, flags);
3009 
3010 	if (do_callback)
3011 		i->f->dev_loss_tmo_callbk(rport);
3012 
3013 	fc_bsg_remove(rport->rqst_q);
3014 
3015 	if (rport->devloss_work_q) {
3016 		work_q = rport->devloss_work_q;
3017 		rport->devloss_work_q = NULL;
3018 		destroy_workqueue(work_q);
3019 	}
3020 
3021 	transport_remove_device(dev);
3022 	device_del(dev);
3023 	transport_destroy_device(dev);
3024 	scsi_host_put(shost);			/* for fc_host->rport list */
3025 	put_device(dev);			/* for self-reference */
3026 }
3027 
3028 
3029 /**
3030  * fc_remote_port_create - allocates and creates a remote FC port.
3031  * @shost:	scsi host the remote port is connected to.
3032  * @channel:	Channel on shost port connected to.
3033  * @ids:	The world wide names, fc address, and FC4 port
3034  *		roles for the remote port.
3035  *
3036  * Allocates and creates the remoter port structure, including the
3037  * class and sysfs creation.
3038  *
3039  * Notes:
3040  *	This routine assumes no locks are held on entry.
3041  */
3042 static struct fc_rport *
3043 fc_remote_port_create(struct Scsi_Host *shost, int channel,
3044 		      struct fc_rport_identifiers  *ids)
3045 {
3046 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3047 	struct fc_internal *fci = to_fc_internal(shost->transportt);
3048 	struct fc_rport *rport;
3049 	struct device *dev;
3050 	unsigned long flags;
3051 	int error;
3052 	size_t size;
3053 
3054 	size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
3055 	rport = kzalloc(size, GFP_KERNEL);
3056 	if (unlikely(!rport)) {
3057 		printk(KERN_ERR "%s: allocation failure\n", __func__);
3058 		return NULL;
3059 	}
3060 
3061 	rport->maxframe_size = -1;
3062 	rport->supported_classes = FC_COS_UNSPECIFIED;
3063 	rport->dev_loss_tmo = fc_host->dev_loss_tmo;
3064 	memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
3065 	memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
3066 	rport->port_id = ids->port_id;
3067 	rport->roles = ids->roles;
3068 	rport->port_state = FC_PORTSTATE_ONLINE;
3069 	if (fci->f->dd_fcrport_size)
3070 		rport->dd_data = &rport[1];
3071 	rport->channel = channel;
3072 	rport->fast_io_fail_tmo = -1;
3073 
3074 	INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
3075 	INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
3076 	INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
3077 	INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
3078 	INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
3079 
3080 	spin_lock_irqsave(shost->host_lock, flags);
3081 
3082 	rport->number = fc_host->next_rport_number++;
3083 	if ((rport->roles & FC_PORT_ROLE_FCP_TARGET) ||
3084 	    (rport->roles & FC_PORT_ROLE_FCP_DUMMY_INITIATOR))
3085 		rport->scsi_target_id = fc_host->next_target_id++;
3086 	else
3087 		rport->scsi_target_id = -1;
3088 	list_add_tail(&rport->peers, &fc_host->rports);
3089 	scsi_host_get(shost);			/* for fc_host->rport list */
3090 
3091 	spin_unlock_irqrestore(shost->host_lock, flags);
3092 
3093 	rport->devloss_work_q = alloc_workqueue("fc_dl_%d_%d", WQ_PERCPU, 0,
3094 						shost->host_no, rport->number);
3095 	if (!rport->devloss_work_q) {
3096 		printk(KERN_ERR "FC Remote Port alloc_workqueue failed\n");
3097 /*
3098  * Note that we have not yet called device_initialize() / get_device()
3099  * Cannot reclaim incremented rport->number because we released host_lock
3100  */
3101 		spin_lock_irqsave(shost->host_lock, flags);
3102 		list_del(&rport->peers);
3103 		scsi_host_put(shost);			/* for fc_host->rport list */
3104 		spin_unlock_irqrestore(shost->host_lock, flags);
3105 		kfree(rport);
3106 		return NULL;
3107 	}
3108 
3109 	dev = &rport->dev;
3110 	device_initialize(dev);			/* takes self reference */
3111 	dev->parent = get_device(&shost->shost_gendev); /* parent reference */
3112 	dev->release = fc_rport_dev_release;
3113 	dev_set_name(dev, "rport-%d:%d-%d",
3114 		     shost->host_no, channel, rport->number);
3115 	transport_setup_device(dev);
3116 
3117 	error = device_add(dev);
3118 	if (error) {
3119 		printk(KERN_ERR "FC Remote Port device_add failed\n");
3120 		goto delete_rport;
3121 	}
3122 	transport_add_device(dev);
3123 	transport_configure_device(dev);
3124 
3125 	fc_bsg_rportadd(shost, rport);
3126 	/* ignore any bsg add error - we just can't do sgio */
3127 
3128 	if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
3129 		/* initiate a scan of the target */
3130 		rport->flags |= FC_RPORT_SCAN_PENDING;
3131 		scsi_queue_work(shost, &rport->scan_work);
3132 	}
3133 
3134 	return rport;
3135 
3136 delete_rport:
3137 	transport_destroy_device(dev);
3138 	spin_lock_irqsave(shost->host_lock, flags);
3139 	list_del(&rport->peers);
3140 	scsi_host_put(shost);			/* for fc_host->rport list */
3141 	spin_unlock_irqrestore(shost->host_lock, flags);
3142 	put_device(dev->parent);
3143 	kfree(rport);
3144 	return NULL;
3145 }
3146 
3147 /**
3148  * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
3149  * @shost:	scsi host the remote port is connected to.
3150  * @channel:	Channel on shost port connected to.
3151  * @ids:	The world wide names, fc address, and FC4 port
3152  *		roles for the remote port.
3153  *
3154  * The LLDD calls this routine to notify the transport of the existence
3155  * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
3156  * of the port, it's FC address (port_id), and the FC4 roles that are
3157  * active for the port.
3158  *
3159  * For ports that are FCP targets (aka scsi targets), the FC transport
3160  * maintains consistent target id bindings on behalf of the LLDD.
3161  * A consistent target id binding is an assignment of a target id to
3162  * a remote port identifier, which persists while the scsi host is
3163  * attached. The remote port can disappear, then later reappear, and
3164  * it's target id assignment remains the same. This allows for shifts
3165  * in FC addressing (if binding by wwpn or wwnn) with no apparent
3166  * changes to the scsi subsystem which is based on scsi host number and
3167  * target id values.  Bindings are only valid during the attachment of
3168  * the scsi host. If the host detaches, then later re-attaches, target
3169  * id bindings may change.
3170  *
3171  * This routine is responsible for returning a remote port structure.
3172  * The routine will search the list of remote ports it maintains
3173  * internally on behalf of consistent target id mappings. If found, the
3174  * remote port structure will be reused. Otherwise, a new remote port
3175  * structure will be allocated.
3176  *
3177  * Whenever a remote port is allocated, a new fc_remote_port class
3178  * device is created.
3179  *
3180  * Should not be called from interrupt context.
3181  *
3182  * Notes:
3183  *	This routine assumes no locks are held on entry.
3184  */
3185 struct fc_rport *
3186 fc_remote_port_add(struct Scsi_Host *shost, int channel,
3187 	struct fc_rport_identifiers  *ids)
3188 {
3189 	struct fc_internal *fci = to_fc_internal(shost->transportt);
3190 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3191 	struct fc_rport *rport;
3192 	unsigned long flags;
3193 	int match = 0;
3194 
3195 	/* ensure any stgt delete functions are done */
3196 	fc_flush_work(shost);
3197 
3198 	/*
3199 	 * Search the list of "active" rports, for an rport that has been
3200 	 * deleted, but we've held off the real delete while the target
3201 	 * is in a "blocked" state.
3202 	 */
3203 	spin_lock_irqsave(shost->host_lock, flags);
3204 
3205 	list_for_each_entry(rport, &fc_host->rports, peers) {
3206 
3207 		if ((rport->port_state == FC_PORTSTATE_BLOCKED ||
3208 		     rport->port_state == FC_PORTSTATE_NOTPRESENT) &&
3209 			(rport->channel == channel)) {
3210 
3211 			switch (fc_host->tgtid_bind_type) {
3212 			case FC_TGTID_BIND_BY_WWPN:
3213 			case FC_TGTID_BIND_NONE:
3214 				if (rport->port_name == ids->port_name)
3215 					match = 1;
3216 				break;
3217 			case FC_TGTID_BIND_BY_WWNN:
3218 				if (rport->node_name == ids->node_name)
3219 					match = 1;
3220 				break;
3221 			case FC_TGTID_BIND_BY_ID:
3222 				if (rport->port_id == ids->port_id)
3223 					match = 1;
3224 				break;
3225 			}
3226 
3227 			if (match) {
3228 
3229 				memcpy(&rport->node_name, &ids->node_name,
3230 					sizeof(rport->node_name));
3231 				memcpy(&rport->port_name, &ids->port_name,
3232 					sizeof(rport->port_name));
3233 				rport->port_id = ids->port_id;
3234 
3235 				rport->port_state = FC_PORTSTATE_ONLINE;
3236 				rport->roles = ids->roles;
3237 
3238 				spin_unlock_irqrestore(shost->host_lock, flags);
3239 
3240 				if (fci->f->dd_fcrport_size)
3241 					memset(rport->dd_data, 0,
3242 						fci->f->dd_fcrport_size);
3243 
3244 				/*
3245 				 * If we were not a target, cancel the
3246 				 * io terminate and rport timers, and
3247 				 * we're done.
3248 				 *
3249 				 * If we were a target, but our new role
3250 				 * doesn't indicate a target, leave the
3251 				 * timers running expecting the role to
3252 				 * change as the target fully logs in. If
3253 				 * it doesn't, the target will be torn down.
3254 				 *
3255 				 * If we were a target, and our role shows
3256 				 * we're still a target, cancel the timers
3257 				 * and kick off a scan.
3258 				 */
3259 
3260 				/* was a target, not in roles */
3261 				if ((rport->scsi_target_id != -1) &&
3262 				    (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
3263 					return rport;
3264 
3265 				/*
3266 				 * Stop the fail io and dev_loss timers.
3267 				 * If they flush, the port_state will
3268 				 * be checked and will NOOP the function.
3269 				 */
3270 				if (!cancel_delayed_work(&rport->fail_io_work))
3271 					fc_flush_devloss(shost, rport);
3272 				if (!cancel_delayed_work(&rport->dev_loss_work))
3273 					fc_flush_devloss(shost, rport);
3274 
3275 				spin_lock_irqsave(shost->host_lock, flags);
3276 
3277 				rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
3278 						  FC_RPORT_DEVLOSS_PENDING |
3279 						  FC_RPORT_DEVLOSS_CALLBK_DONE);
3280 
3281 				spin_unlock_irqrestore(shost->host_lock, flags);
3282 
3283 				/* if target, initiate a scan */
3284 				if (rport->scsi_target_id != -1) {
3285 					scsi_target_unblock(&rport->dev,
3286 							    SDEV_RUNNING);
3287 					spin_lock_irqsave(shost->host_lock,
3288 							  flags);
3289 					rport->flags |= FC_RPORT_SCAN_PENDING;
3290 					scsi_queue_work(shost,
3291 							&rport->scan_work);
3292 					spin_unlock_irqrestore(shost->host_lock,
3293 							flags);
3294 				}
3295 
3296 				fc_bsg_goose_queue(rport);
3297 
3298 				return rport;
3299 			}
3300 		}
3301 	}
3302 
3303 	/*
3304 	 * Search the bindings array
3305 	 * Note: if never a FCP target, you won't be on this list
3306 	 */
3307 	if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
3308 
3309 		/* search for a matching consistent binding */
3310 
3311 		list_for_each_entry(rport, &fc_host->rport_bindings,
3312 					peers) {
3313 			if (rport->channel != channel)
3314 				continue;
3315 
3316 			switch (fc_host->tgtid_bind_type) {
3317 			case FC_TGTID_BIND_BY_WWPN:
3318 				if (rport->port_name == ids->port_name)
3319 					match = 1;
3320 				break;
3321 			case FC_TGTID_BIND_BY_WWNN:
3322 				if (rport->node_name == ids->node_name)
3323 					match = 1;
3324 				break;
3325 			case FC_TGTID_BIND_BY_ID:
3326 				if (rport->port_id == ids->port_id)
3327 					match = 1;
3328 				break;
3329 			case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3330 				break;
3331 			}
3332 
3333 			if (match) {
3334 				list_move_tail(&rport->peers, &fc_host->rports);
3335 				break;
3336 			}
3337 		}
3338 
3339 		if (match) {
3340 			memcpy(&rport->node_name, &ids->node_name,
3341 				sizeof(rport->node_name));
3342 			memcpy(&rport->port_name, &ids->port_name,
3343 				sizeof(rport->port_name));
3344 			rport->port_id = ids->port_id;
3345 			rport->port_state = FC_PORTSTATE_ONLINE;
3346 			rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
3347 
3348 			if (fci->f->dd_fcrport_size)
3349 				memset(rport->dd_data, 0,
3350 						fci->f->dd_fcrport_size);
3351 			spin_unlock_irqrestore(shost->host_lock, flags);
3352 
3353 			fc_remote_port_rolechg(rport, ids->roles);
3354 			return rport;
3355 		}
3356 	}
3357 
3358 	spin_unlock_irqrestore(shost->host_lock, flags);
3359 
3360 	/* No consistent binding found - create new remote port entry */
3361 	rport = fc_remote_port_create(shost, channel, ids);
3362 
3363 	return rport;
3364 }
3365 EXPORT_SYMBOL(fc_remote_port_add);
3366 
3367 
3368 /**
3369  * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
3370  * @rport:	The remote port that no longer exists
3371  *
3372  * The LLDD calls this routine to notify the transport that a remote
3373  * port is no longer part of the topology. Note: Although a port
3374  * may no longer be part of the topology, it may persist in the remote
3375  * ports displayed by the fc_host. We do this under 2 conditions:
3376  *
3377  * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
3378  *    This allows the port to temporarily disappear, then reappear without
3379  *    disrupting the SCSI device tree attached to it. During the "blocked"
3380  *    period the port will still exist.
3381  *
3382  * 2) If the port was a scsi target and disappears for longer than we
3383  *    expect, we'll delete the port and the tear down the SCSI device tree
3384  *    attached to it. However, we want to semi-persist the target id assigned
3385  *    to that port if it eventually does exist. The port structure will
3386  *    remain (although with minimal information) so that the target id
3387  *    bindings also remain.
3388  *
3389  * If the remote port is not an FCP Target, it will be fully torn down
3390  * and deallocated, including the fc_remote_port class device.
3391  *
3392  * If the remote port is an FCP Target, the port will be placed in a
3393  * temporary blocked state. From the LLDD's perspective, the rport no
3394  * longer exists. From the SCSI midlayer's perspective, the SCSI target
3395  * exists, but all sdevs on it are blocked from further I/O. The following
3396  * is then expected.
3397  *
3398  *   If the remote port does not return (signaled by a LLDD call to
3399  *   fc_remote_port_add()) within the dev_loss_tmo timeout, then the
3400  *   scsi target is removed - killing all outstanding i/o and removing the
3401  *   scsi devices attached to it. The port structure will be marked Not
3402  *   Present and be partially cleared, leaving only enough information to
3403  *   recognize the remote port relative to the scsi target id binding if
3404  *   it later appears.  The port will remain as long as there is a valid
3405  *   binding (e.g. until the user changes the binding type or unloads the
3406  *   scsi host with the binding).
3407  *
3408  *   If the remote port returns within the dev_loss_tmo value (and matches
3409  *   according to the target id binding type), the port structure will be
3410  *   reused. If it is no longer a SCSI target, the target will be torn
3411  *   down. If it continues to be a SCSI target, then the target will be
3412  *   unblocked (allowing i/o to be resumed), and a scan will be activated
3413  *   to ensure that all luns are detected.
3414  *
3415  * Called from normal process context only - cannot be called from interrupt.
3416  *
3417  * Notes:
3418  *	This routine assumes no locks are held on entry.
3419  */
3420 void
3421 fc_remote_port_delete(struct fc_rport  *rport)
3422 {
3423 	struct Scsi_Host *shost = rport_to_shost(rport);
3424 	unsigned long timeout = rport->dev_loss_tmo;
3425 	unsigned long flags;
3426 
3427 	/*
3428 	 * No need to flush the fc_host work_q's, as all adds are synchronous.
3429 	 *
3430 	 * We do need to reclaim the rport scan work element, so eventually
3431 	 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
3432 	 * there's still a scan pending.
3433 	 */
3434 
3435 	spin_lock_irqsave(shost->host_lock, flags);
3436 
3437 	if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
3438 		(rport->port_state != FC_PORTSTATE_MARGINAL)) {
3439 		spin_unlock_irqrestore(shost->host_lock, flags);
3440 		return;
3441 	}
3442 
3443 	/*
3444 	 * In the past, we if this was not an FCP-Target, we would
3445 	 * unconditionally just jump to deleting the rport.
3446 	 * However, rports can be used as node containers by the LLDD,
3447 	 * and its not appropriate to just terminate the rport at the
3448 	 * first sign of a loss in connectivity. The LLDD may want to
3449 	 * send ELS traffic to re-validate the login. If the rport is
3450 	 * immediately deleted, it makes it inappropriate for a node
3451 	 * container.
3452 	 * So... we now unconditionally wait dev_loss_tmo before
3453 	 * destroying an rport.
3454 	 */
3455 
3456 	rport->port_state = FC_PORTSTATE_BLOCKED;
3457 
3458 	rport->flags |= FC_RPORT_DEVLOSS_PENDING;
3459 
3460 	spin_unlock_irqrestore(shost->host_lock, flags);
3461 
3462 	scsi_block_targets(shost, &rport->dev);
3463 
3464 	/* see if we need to kill io faster than waiting for device loss */
3465 	if ((rport->fast_io_fail_tmo != -1) &&
3466 	    (rport->fast_io_fail_tmo < timeout))
3467 		fc_queue_devloss_work(shost, rport, &rport->fail_io_work,
3468 				      rport->fast_io_fail_tmo * HZ);
3469 
3470 	/* cap the length the devices can be blocked until they are deleted */
3471 	fc_queue_devloss_work(shost, rport, &rport->dev_loss_work,
3472 			      timeout * HZ);
3473 }
3474 EXPORT_SYMBOL(fc_remote_port_delete);
3475 
3476 /**
3477  * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
3478  * @rport:	The remote port that changed.
3479  * @roles:      New roles for this port.
3480  *
3481  * Description: The LLDD calls this routine to notify the transport that the
3482  * roles on a remote port may have changed. The largest effect of this is
3483  * if a port now becomes a FCP Target, it must be allocated a
3484  * scsi target id.  If the port is no longer a FCP target, any
3485  * scsi target id value assigned to it will persist in case the
3486  * role changes back to include FCP Target. No changes in the scsi
3487  * midlayer will be invoked if the role changes (in the expectation
3488  * that the role will be resumed. If it doesn't normal error processing
3489  * will take place).
3490  *
3491  * Should not be called from interrupt context.
3492  *
3493  * Notes:
3494  *	This routine assumes no locks are held on entry.
3495  */
3496 void
3497 fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
3498 {
3499 	struct Scsi_Host *shost = rport_to_shost(rport);
3500 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3501 	unsigned long flags;
3502 	int create = 0;
3503 
3504 	spin_lock_irqsave(shost->host_lock, flags);
3505 	if (roles & FC_PORT_ROLE_FCP_TARGET) {
3506 		if (rport->scsi_target_id == -1) {
3507 			rport->scsi_target_id = fc_host->next_target_id++;
3508 			create = 1;
3509 		} else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
3510 			create = 1;
3511 	}
3512 
3513 	rport->roles = roles;
3514 
3515 	spin_unlock_irqrestore(shost->host_lock, flags);
3516 
3517 	if (create) {
3518 		/*
3519 		 * There may have been a delete timer running on the
3520 		 * port. Ensure that it is cancelled as we now know
3521 		 * the port is an FCP Target.
3522 		 * Note: we know the rport exists and is in an online
3523 		 *  state as the LLDD would not have had an rport
3524 		 *  reference to pass us.
3525 		 *
3526 		 * Take no action on the timer_delete() failure as the state
3527 		 * machine state change will validate the
3528 		 * transaction.
3529 		 */
3530 		if (!cancel_delayed_work(&rport->fail_io_work))
3531 			fc_flush_devloss(shost, rport);
3532 		if (!cancel_delayed_work(&rport->dev_loss_work))
3533 			fc_flush_devloss(shost, rport);
3534 
3535 		spin_lock_irqsave(shost->host_lock, flags);
3536 		rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
3537 				  FC_RPORT_DEVLOSS_PENDING |
3538 				  FC_RPORT_DEVLOSS_CALLBK_DONE);
3539 		spin_unlock_irqrestore(shost->host_lock, flags);
3540 
3541 		/* ensure any stgt delete functions are done */
3542 		fc_flush_work(shost);
3543 
3544 		scsi_target_unblock(&rport->dev, SDEV_RUNNING);
3545 		/* initiate a scan of the target */
3546 		spin_lock_irqsave(shost->host_lock, flags);
3547 		rport->flags |= FC_RPORT_SCAN_PENDING;
3548 		scsi_queue_work(shost, &rport->scan_work);
3549 		spin_unlock_irqrestore(shost->host_lock, flags);
3550 	}
3551 }
3552 EXPORT_SYMBOL(fc_remote_port_rolechg);
3553 
3554 /**
3555  * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
3556  * @work:	rport target that failed to reappear in the allotted time.
3557  *
3558  * Description: An attempt to delete a remote port blocks, and if it fails
3559  *              to return in the allotted time this gets called.
3560  */
3561 static void
3562 fc_timeout_deleted_rport(struct work_struct *work)
3563 {
3564 	struct fc_rport *rport =
3565 		container_of(work, struct fc_rport, dev_loss_work.work);
3566 	struct Scsi_Host *shost = rport_to_shost(rport);
3567 	struct fc_internal *i = to_fc_internal(shost->transportt);
3568 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3569 	unsigned long flags;
3570 	int do_callback = 0;
3571 
3572 	spin_lock_irqsave(shost->host_lock, flags);
3573 
3574 	rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3575 
3576 	/*
3577 	 * If the port is ONLINE, then it came back. If it was a SCSI
3578 	 * target, validate it still is. If not, tear down the
3579 	 * scsi_target on it.
3580 	 */
3581 	if (((rport->port_state == FC_PORTSTATE_ONLINE) ||
3582 		(rport->port_state == FC_PORTSTATE_MARGINAL)) &&
3583 	    (rport->scsi_target_id != -1) &&
3584 	    !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
3585 		dev_printk(KERN_ERR, &rport->dev,
3586 			"blocked FC remote port time out: no longer"
3587 			" a FCP target, removing starget\n");
3588 		spin_unlock_irqrestore(shost->host_lock, flags);
3589 		scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
3590 		fc_queue_work(shost, &rport->stgt_delete_work);
3591 		return;
3592 	}
3593 
3594 	/* NOOP state - we're flushing workq's */
3595 	if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3596 		spin_unlock_irqrestore(shost->host_lock, flags);
3597 		dev_printk(KERN_ERR, &rport->dev,
3598 			"blocked FC remote port time out: leaving"
3599 			" rport%s alone\n",
3600 			(rport->scsi_target_id != -1) ?  " and starget" : "");
3601 		return;
3602 	}
3603 
3604 	if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3605 	    (rport->scsi_target_id == -1)) {
3606 		list_del(&rport->peers);
3607 		rport->port_state = FC_PORTSTATE_DELETED;
3608 		dev_printk(KERN_ERR, &rport->dev,
3609 			"blocked FC remote port time out: removing"
3610 			" rport%s\n",
3611 			(rport->scsi_target_id != -1) ?  " and starget" : "");
3612 		fc_queue_work(shost, &rport->rport_delete_work);
3613 		spin_unlock_irqrestore(shost->host_lock, flags);
3614 		return;
3615 	}
3616 
3617 	dev_printk(KERN_ERR, &rport->dev,
3618 		"blocked FC remote port time out: removing target and "
3619 		"saving binding\n");
3620 
3621 	list_move_tail(&rport->peers, &fc_host->rport_bindings);
3622 
3623 	/*
3624 	 * Note: We do not remove or clear the hostdata area. This allows
3625 	 *   host-specific target data to persist along with the
3626 	 *   scsi_target_id. It's up to the host to manage it's hostdata area.
3627 	 */
3628 
3629 	/*
3630 	 * Reinitialize port attributes that may change if the port comes back.
3631 	 */
3632 	rport->maxframe_size = -1;
3633 	rport->supported_classes = FC_COS_UNSPECIFIED;
3634 	rport->roles = FC_PORT_ROLE_UNKNOWN;
3635 	rport->port_state = FC_PORTSTATE_NOTPRESENT;
3636 	rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
3637 
3638 	/*
3639 	 * Pre-emptively kill I/O rather than waiting for the work queue
3640 	 * item to teardown the starget. (FCOE libFC folks prefer this
3641 	 * and to have the rport_port_id still set when it's done).
3642 	 */
3643 	spin_unlock_irqrestore(shost->host_lock, flags);
3644 	fc_terminate_rport_io(rport);
3645 
3646 	spin_lock_irqsave(shost->host_lock, flags);
3647 
3648 	if (rport->port_state == FC_PORTSTATE_NOTPRESENT) {	/* still missing */
3649 
3650 		/* remove the identifiers that aren't used in the consisting binding */
3651 		switch (fc_host->tgtid_bind_type) {
3652 		case FC_TGTID_BIND_BY_WWPN:
3653 			rport->node_name = -1;
3654 			rport->port_id = -1;
3655 			break;
3656 		case FC_TGTID_BIND_BY_WWNN:
3657 			rport->port_name = -1;
3658 			rport->port_id = -1;
3659 			break;
3660 		case FC_TGTID_BIND_BY_ID:
3661 			rport->node_name = -1;
3662 			rport->port_name = -1;
3663 			break;
3664 		case FC_TGTID_BIND_NONE:	/* to keep compiler happy */
3665 			break;
3666 		}
3667 
3668 		/*
3669 		 * As this only occurs if the remote port (scsi target)
3670 		 * went away and didn't come back - we'll remove
3671 		 * all attached scsi devices.
3672 		 */
3673 		rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3674 		fc_queue_work(shost, &rport->stgt_delete_work);
3675 
3676 		do_callback = 1;
3677 	}
3678 
3679 	spin_unlock_irqrestore(shost->host_lock, flags);
3680 
3681 	/*
3682 	 * Notify the driver that the rport is now dead. The LLDD will
3683 	 * also guarantee that any communication to the rport is terminated
3684 	 *
3685 	 * Note: we set the CALLBK_DONE flag above to correspond
3686 	 */
3687 	if (do_callback && i->f->dev_loss_tmo_callbk)
3688 		i->f->dev_loss_tmo_callbk(rport);
3689 }
3690 
3691 
3692 /**
3693  * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3694  * @work:	rport to terminate io on.
3695  *
3696  * Notes: Only requests the failure of the io, not that all are flushed
3697  *    prior to returning.
3698  */
3699 static void
3700 fc_timeout_fail_rport_io(struct work_struct *work)
3701 {
3702 	struct fc_rport *rport =
3703 		container_of(work, struct fc_rport, fail_io_work.work);
3704 
3705 	if (rport->port_state != FC_PORTSTATE_BLOCKED)
3706 		return;
3707 
3708 	rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3709 	fc_terminate_rport_io(rport);
3710 }
3711 
3712 /**
3713  * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3714  * @work:	remote port to be scanned.
3715  */
3716 static void
3717 fc_scsi_scan_rport(struct work_struct *work)
3718 {
3719 	struct fc_rport *rport =
3720 		container_of(work, struct fc_rport, scan_work);
3721 	struct Scsi_Host *shost = rport_to_shost(rport);
3722 	struct fc_internal *i = to_fc_internal(shost->transportt);
3723 	unsigned long flags;
3724 
3725 	if (((rport->port_state == FC_PORTSTATE_ONLINE) ||
3726 		(rport->port_state == FC_PORTSTATE_MARGINAL)) &&
3727 	    (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3728 	    !(i->f->disable_target_scan)) {
3729 		scsi_scan_target(&rport->dev, rport->channel,
3730 				 rport->scsi_target_id, SCAN_WILD_CARD,
3731 				 SCSI_SCAN_RESCAN);
3732 	}
3733 
3734 	spin_lock_irqsave(shost->host_lock, flags);
3735 	rport->flags &= ~FC_RPORT_SCAN_PENDING;
3736 	spin_unlock_irqrestore(shost->host_lock, flags);
3737 }
3738 
3739 /**
3740  * fc_block_rport() - Block SCSI eh thread for blocked fc_rport.
3741  * @rport: Remote port that scsi_eh is trying to recover.
3742  *
3743  * This routine can be called from a FC LLD scsi_eh callback. It
3744  * blocks the scsi_eh thread until the fc_rport leaves the
3745  * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3746  * necessary to avoid the scsi_eh failing recovery actions for blocked
3747  * rports which would lead to offlined SCSI devices.
3748  *
3749  * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3750  *	    FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3751  *	    passed back to scsi_eh.
3752  */
3753 int fc_block_rport(struct fc_rport *rport)
3754 {
3755 	struct Scsi_Host *shost = rport_to_shost(rport);
3756 	unsigned long flags;
3757 
3758 	spin_lock_irqsave(shost->host_lock, flags);
3759 	while (rport->port_state == FC_PORTSTATE_BLOCKED &&
3760 	       !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
3761 		spin_unlock_irqrestore(shost->host_lock, flags);
3762 		msleep(1000);
3763 		spin_lock_irqsave(shost->host_lock, flags);
3764 	}
3765 	spin_unlock_irqrestore(shost->host_lock, flags);
3766 
3767 	if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
3768 		return FAST_IO_FAIL;
3769 
3770 	return 0;
3771 }
3772 EXPORT_SYMBOL(fc_block_rport);
3773 
3774 /**
3775  * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
3776  * @cmnd: SCSI command that scsi_eh is trying to recover
3777  *
3778  * This routine can be called from a FC LLD scsi_eh callback. It
3779  * blocks the scsi_eh thread until the fc_rport leaves the
3780  * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
3781  * necessary to avoid the scsi_eh failing recovery actions for blocked
3782  * rports which would lead to offlined SCSI devices.
3783  *
3784  * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
3785  *	    FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
3786  *	    passed back to scsi_eh.
3787  */
3788 int fc_block_scsi_eh(struct scsi_cmnd *cmnd)
3789 {
3790 	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3791 
3792 	if (WARN_ON_ONCE(!rport))
3793 		return FAST_IO_FAIL;
3794 
3795 	return fc_block_rport(rport);
3796 }
3797 EXPORT_SYMBOL(fc_block_scsi_eh);
3798 
3799 /*
3800  * fc_eh_should_retry_cmd - Checks if the cmd should be retried or not
3801  * @scmd:        The SCSI command to be checked
3802  *
3803  * This checks the rport state to decide if a cmd is
3804  * retryable.
3805  *
3806  * Returns: true if the rport state is not in marginal state.
3807  */
3808 bool fc_eh_should_retry_cmd(struct scsi_cmnd *scmd)
3809 {
3810 	struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
3811 
3812 	if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
3813 		(scsi_cmd_to_rq(scmd)->cmd_flags & REQ_FAILFAST_TRANSPORT)) {
3814 		set_host_byte(scmd, DID_TRANSPORT_MARGINAL);
3815 		return false;
3816 	}
3817 	return true;
3818 }
3819 EXPORT_SYMBOL_GPL(fc_eh_should_retry_cmd);
3820 
3821 /**
3822  * fc_vport_setup - allocates and creates a FC virtual port.
3823  * @shost:	scsi host the virtual port is connected to.
3824  * @channel:	Channel on shost port connected to.
3825  * @pdev:	parent device for vport
3826  * @ids:	The world wide names, FC4 port roles, etc for
3827  *              the virtual port.
3828  * @ret_vport:	The pointer to the created vport.
3829  *
3830  * Allocates and creates the vport structure, calls the parent host
3831  * to instantiate the vport, this completes w/ class and sysfs creation.
3832  *
3833  * Notes:
3834  *	This routine assumes no locks are held on entry.
3835  */
3836 static int
3837 fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
3838 	struct fc_vport_identifiers  *ids, struct fc_vport **ret_vport)
3839 {
3840 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3841 	struct fc_internal *fci = to_fc_internal(shost->transportt);
3842 	struct fc_vport *vport;
3843 	struct device *dev;
3844 	unsigned long flags;
3845 	size_t size;
3846 	int error;
3847 
3848 	*ret_vport = NULL;
3849 
3850 	if ( ! fci->f->vport_create)
3851 		return -ENOENT;
3852 
3853 	size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3854 	vport = kzalloc(size, GFP_KERNEL);
3855 	if (unlikely(!vport)) {
3856 		printk(KERN_ERR "%s: allocation failure\n", __func__);
3857 		return -ENOMEM;
3858 	}
3859 
3860 	vport->vport_state = FC_VPORT_UNKNOWN;
3861 	vport->vport_last_state = FC_VPORT_UNKNOWN;
3862 	vport->node_name = ids->node_name;
3863 	vport->port_name = ids->port_name;
3864 	vport->roles = ids->roles;
3865 	vport->vport_type = ids->vport_type;
3866 	if (fci->f->dd_fcvport_size)
3867 		vport->dd_data = &vport[1];
3868 	vport->shost = shost;
3869 	vport->channel = channel;
3870 	vport->flags = FC_VPORT_CREATING;
3871 	INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3872 
3873 	spin_lock_irqsave(shost->host_lock, flags);
3874 
3875 	if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3876 		spin_unlock_irqrestore(shost->host_lock, flags);
3877 		kfree(vport);
3878 		return -ENOSPC;
3879 	}
3880 	fc_host->npiv_vports_inuse++;
3881 	vport->number = fc_host->next_vport_number++;
3882 	list_add_tail(&vport->peers, &fc_host->vports);
3883 	scsi_host_get(shost);			/* for fc_host->vport list */
3884 
3885 	spin_unlock_irqrestore(shost->host_lock, flags);
3886 
3887 	dev = &vport->dev;
3888 	device_initialize(dev);			/* takes self reference */
3889 	dev->parent = get_device(pdev);		/* takes parent reference */
3890 	dev->release = fc_vport_dev_release;
3891 	dev_set_name(dev, "vport-%d:%d-%d",
3892 		     shost->host_no, channel, vport->number);
3893 	transport_setup_device(dev);
3894 
3895 	error = device_add(dev);
3896 	if (error) {
3897 		printk(KERN_ERR "FC Virtual Port device_add failed\n");
3898 		goto delete_vport;
3899 	}
3900 	transport_add_device(dev);
3901 	transport_configure_device(dev);
3902 
3903 	error = fci->f->vport_create(vport, ids->disable);
3904 	if (error) {
3905 		printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3906 		goto delete_vport_all;
3907 	}
3908 
3909 	/*
3910 	 * if the parent isn't the physical adapter's Scsi_Host, ensure
3911 	 * the Scsi_Host at least contains a symlink to the vport.
3912 	 */
3913 	if (pdev != &shost->shost_gendev) {
3914 		error = sysfs_create_link(&shost->shost_gendev.kobj,
3915 				 &dev->kobj, dev_name(dev));
3916 		if (error)
3917 			printk(KERN_ERR
3918 				"%s: Cannot create vport symlinks for "
3919 				"%s, err=%d\n",
3920 				__func__, dev_name(dev), error);
3921 	}
3922 	spin_lock_irqsave(shost->host_lock, flags);
3923 	vport->flags &= ~FC_VPORT_CREATING;
3924 	spin_unlock_irqrestore(shost->host_lock, flags);
3925 
3926 	dev_printk(KERN_NOTICE, pdev,
3927 			"%s created via shost%d channel %d\n", dev_name(dev),
3928 			shost->host_no, channel);
3929 
3930 	*ret_vport = vport;
3931 
3932 	return 0;
3933 
3934 delete_vport_all:
3935 	transport_remove_device(dev);
3936 	device_del(dev);
3937 delete_vport:
3938 	transport_destroy_device(dev);
3939 	spin_lock_irqsave(shost->host_lock, flags);
3940 	list_del(&vport->peers);
3941 	scsi_host_put(shost);			/* for fc_host->vport list */
3942 	fc_host->npiv_vports_inuse--;
3943 	spin_unlock_irqrestore(shost->host_lock, flags);
3944 	put_device(dev->parent);
3945 	kfree(vport);
3946 
3947 	return error;
3948 }
3949 
3950 /**
3951  * fc_vport_create - Admin App or LLDD requests creation of a vport
3952  * @shost:	scsi host the virtual port is connected to.
3953  * @channel:	channel on shost port connected to.
3954  * @ids:	The world wide names, FC4 port roles, etc for
3955  *              the virtual port.
3956  *
3957  * Notes:
3958  *	This routine assumes no locks are held on entry.
3959  */
3960 struct fc_vport *
3961 fc_vport_create(struct Scsi_Host *shost, int channel,
3962 	struct fc_vport_identifiers *ids)
3963 {
3964 	int stat;
3965 	struct fc_vport *vport;
3966 
3967 	stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3968 		 ids, &vport);
3969 	return stat ? NULL : vport;
3970 }
3971 EXPORT_SYMBOL(fc_vport_create);
3972 
3973 /**
3974  * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3975  * @vport:	fc_vport to be terminated
3976  *
3977  * Calls the LLDD vport_delete() function, then deallocates and removes
3978  * the vport from the shost and object tree.
3979  *
3980  * Notes:
3981  *	This routine assumes no locks are held on entry.
3982  */
3983 int
3984 fc_vport_terminate(struct fc_vport *vport)
3985 {
3986 	struct Scsi_Host *shost = vport_to_shost(vport);
3987 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3988 	struct fc_internal *i = to_fc_internal(shost->transportt);
3989 	struct device *dev = &vport->dev;
3990 	unsigned long flags;
3991 	int stat;
3992 
3993 	if (i->f->vport_delete)
3994 		stat = i->f->vport_delete(vport);
3995 	else
3996 		stat = -ENOENT;
3997 
3998 	spin_lock_irqsave(shost->host_lock, flags);
3999 	vport->flags &= ~FC_VPORT_DELETING;
4000 	if (!stat) {
4001 		vport->flags |= FC_VPORT_DELETED;
4002 		list_del(&vport->peers);
4003 		fc_host->npiv_vports_inuse--;
4004 		scsi_host_put(shost);		/* for fc_host->vport list */
4005 	}
4006 	spin_unlock_irqrestore(shost->host_lock, flags);
4007 
4008 	if (stat)
4009 		return stat;
4010 
4011 	if (dev->parent != &shost->shost_gendev)
4012 		sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
4013 	transport_remove_device(dev);
4014 	device_del(dev);
4015 	transport_destroy_device(dev);
4016 
4017 	/*
4018 	 * Removing our self-reference should mean our
4019 	 * release function gets called, which will drop the remaining
4020 	 * parent reference and free the data structure.
4021 	 */
4022 	put_device(dev);			/* for self-reference */
4023 
4024 	return 0; /* SUCCESS */
4025 }
4026 EXPORT_SYMBOL(fc_vport_terminate);
4027 
4028 /**
4029  * fc_vport_sched_delete - workq-based delete request for a vport
4030  * @work:	vport to be deleted.
4031  */
4032 static void
4033 fc_vport_sched_delete(struct work_struct *work)
4034 {
4035 	struct fc_vport *vport =
4036 		container_of(work, struct fc_vport, vport_delete_work);
4037 	int stat;
4038 
4039 	stat = fc_vport_terminate(vport);
4040 	if (stat)
4041 		dev_printk(KERN_ERR, vport->dev.parent,
4042 			"%s: %s could not be deleted created via "
4043 			"shost%d channel %d - error %d\n", __func__,
4044 			dev_name(&vport->dev), vport->shost->host_no,
4045 			vport->channel, stat);
4046 }
4047 
4048 
4049 /*
4050  * BSG support
4051  */
4052 
4053 /**
4054  * fc_bsg_job_timeout - handler for when a bsg request timesout
4055  * @req:	request that timed out
4056  */
4057 static enum blk_eh_timer_return
4058 fc_bsg_job_timeout(struct request *req)
4059 {
4060 	struct bsg_job *job = blk_mq_rq_to_pdu(req);
4061 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
4062 	struct fc_rport *rport = fc_bsg_to_rport(job);
4063 	struct fc_internal *i = to_fc_internal(shost->transportt);
4064 	int err = 0, inflight = 0;
4065 
4066 	if (rport && rport->port_state == FC_PORTSTATE_BLOCKED)
4067 		return BLK_EH_RESET_TIMER;
4068 
4069 	inflight = bsg_job_get(job);
4070 
4071 	if (inflight && i->f->bsg_timeout) {
4072 		/* call LLDD to abort the i/o as it has timed out */
4073 		err = i->f->bsg_timeout(job);
4074 		if (err == -EAGAIN) {
4075 			bsg_job_put(job);
4076 			return BLK_EH_RESET_TIMER;
4077 		} else if (err)
4078 			printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
4079 				"abort failed with status %d\n", err);
4080 	}
4081 
4082 	/* the blk_end_sync_io() doesn't check the error */
4083 	if (inflight)
4084 		blk_mq_end_request(req, BLK_STS_IOERR);
4085 	return BLK_EH_DONE;
4086 }
4087 
4088 /**
4089  * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
4090  * @shost:	scsi host rport attached to
4091  * @job:	bsg job to be processed
4092  */
4093 static int fc_bsg_host_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
4094 {
4095 	struct fc_internal *i = to_fc_internal(shost->transportt);
4096 	struct fc_bsg_request *bsg_request = job->request;
4097 	struct fc_bsg_reply *bsg_reply = job->reply;
4098 	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
4099 	int ret;
4100 
4101 	/* check if we really have all the request data needed */
4102 	if (job->request_len < cmdlen) {
4103 		ret = -ENOMSG;
4104 		goto fail_host_msg;
4105 	}
4106 
4107 	/* Validate the host command */
4108 	switch (bsg_request->msgcode) {
4109 	case FC_BSG_HST_ADD_RPORT:
4110 		cmdlen += sizeof(struct fc_bsg_host_add_rport);
4111 		break;
4112 
4113 	case FC_BSG_HST_DEL_RPORT:
4114 		cmdlen += sizeof(struct fc_bsg_host_del_rport);
4115 		break;
4116 
4117 	case FC_BSG_HST_ELS_NOLOGIN:
4118 		cmdlen += sizeof(struct fc_bsg_host_els);
4119 		/* there better be a xmt and rcv payloads */
4120 		if ((!job->request_payload.payload_len) ||
4121 		    (!job->reply_payload.payload_len)) {
4122 			ret = -EINVAL;
4123 			goto fail_host_msg;
4124 		}
4125 		break;
4126 
4127 	case FC_BSG_HST_CT:
4128 		cmdlen += sizeof(struct fc_bsg_host_ct);
4129 		/* there better be xmt and rcv payloads */
4130 		if ((!job->request_payload.payload_len) ||
4131 		    (!job->reply_payload.payload_len)) {
4132 			ret = -EINVAL;
4133 			goto fail_host_msg;
4134 		}
4135 		break;
4136 
4137 	case FC_BSG_HST_VENDOR:
4138 		cmdlen += sizeof(struct fc_bsg_host_vendor);
4139 		if ((shost->hostt->vendor_id == 0L) ||
4140 		    (bsg_request->rqst_data.h_vendor.vendor_id !=
4141 			shost->hostt->vendor_id)) {
4142 			ret = -ESRCH;
4143 			goto fail_host_msg;
4144 		}
4145 		break;
4146 
4147 	default:
4148 		ret = -EBADR;
4149 		goto fail_host_msg;
4150 	}
4151 
4152 	ret = i->f->bsg_request(job);
4153 	if (!ret)
4154 		return 0;
4155 
4156 fail_host_msg:
4157 	/* return the errno failure code as the only status */
4158 	BUG_ON(job->reply_len < sizeof(uint32_t));
4159 	bsg_reply->reply_payload_rcv_len = 0;
4160 	bsg_reply->result = ret;
4161 	job->reply_len = sizeof(uint32_t);
4162 	bsg_job_done(job, bsg_reply->result,
4163 		       bsg_reply->reply_payload_rcv_len);
4164 	return 0;
4165 }
4166 
4167 
4168 /*
4169  * fc_bsg_goose_queue - restart rport queue in case it was stopped
4170  * @rport:	rport to be restarted
4171  */
4172 static void
4173 fc_bsg_goose_queue(struct fc_rport *rport)
4174 {
4175 	struct request_queue *q = rport->rqst_q;
4176 
4177 	if (q)
4178 		blk_mq_run_hw_queues(q, true);
4179 }
4180 
4181 /**
4182  * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
4183  * @shost:	scsi host rport attached to
4184  * @job:	bsg job to be processed
4185  */
4186 static int fc_bsg_rport_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
4187 {
4188 	struct fc_internal *i = to_fc_internal(shost->transportt);
4189 	struct fc_bsg_request *bsg_request = job->request;
4190 	struct fc_bsg_reply *bsg_reply = job->reply;
4191 	int cmdlen = sizeof(uint32_t);	/* start with length of msgcode */
4192 	int ret;
4193 
4194 	/* check if we really have all the request data needed */
4195 	if (job->request_len < cmdlen) {
4196 		ret = -ENOMSG;
4197 		goto fail_rport_msg;
4198 	}
4199 
4200 	/* Validate the rport command */
4201 	switch (bsg_request->msgcode) {
4202 	case FC_BSG_RPT_ELS:
4203 		cmdlen += sizeof(struct fc_bsg_rport_els);
4204 		goto check_bidi;
4205 
4206 	case FC_BSG_RPT_CT:
4207 		cmdlen += sizeof(struct fc_bsg_rport_ct);
4208 check_bidi:
4209 		/* there better be xmt and rcv payloads */
4210 		if ((!job->request_payload.payload_len) ||
4211 		    (!job->reply_payload.payload_len)) {
4212 			ret = -EINVAL;
4213 			goto fail_rport_msg;
4214 		}
4215 		break;
4216 	default:
4217 		ret = -EBADR;
4218 		goto fail_rport_msg;
4219 	}
4220 
4221 	ret = i->f->bsg_request(job);
4222 	if (!ret)
4223 		return 0;
4224 
4225 fail_rport_msg:
4226 	/* return the errno failure code as the only status */
4227 	BUG_ON(job->reply_len < sizeof(uint32_t));
4228 	bsg_reply->reply_payload_rcv_len = 0;
4229 	bsg_reply->result = ret;
4230 	job->reply_len = sizeof(uint32_t);
4231 	bsg_job_done(job, bsg_reply->result,
4232 		       bsg_reply->reply_payload_rcv_len);
4233 	return 0;
4234 }
4235 
4236 static int fc_bsg_dispatch(struct bsg_job *job)
4237 {
4238 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
4239 
4240 	if (scsi_is_fc_rport(job->dev))
4241 		return fc_bsg_rport_dispatch(shost, job);
4242 	else
4243 		return fc_bsg_host_dispatch(shost, job);
4244 }
4245 
4246 static blk_status_t fc_bsg_rport_prep(struct fc_rport *rport)
4247 {
4248 	if (rport->port_state == FC_PORTSTATE_BLOCKED &&
4249 	    !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
4250 		return BLK_STS_RESOURCE;
4251 
4252 	if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
4253 		(rport->port_state != FC_PORTSTATE_MARGINAL))
4254 		return BLK_STS_IOERR;
4255 
4256 	return BLK_STS_OK;
4257 }
4258 
4259 
4260 static int fc_bsg_dispatch_prep(struct bsg_job *job)
4261 {
4262 	struct fc_rport *rport = fc_bsg_to_rport(job);
4263 	blk_status_t ret;
4264 
4265 	ret = fc_bsg_rport_prep(rport);
4266 	switch (ret) {
4267 	case BLK_STS_OK:
4268 		break;
4269 	case BLK_STS_RESOURCE:
4270 		return -EAGAIN;
4271 	default:
4272 		return -EIO;
4273 	}
4274 
4275 	return fc_bsg_dispatch(job);
4276 }
4277 
4278 /**
4279  * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
4280  * @shost:	shost for fc_host
4281  * @fc_host:	fc_host adding the structures to
4282  */
4283 static int
4284 fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
4285 {
4286 	struct device *dev = &shost->shost_gendev;
4287 	struct fc_internal *i = to_fc_internal(shost->transportt);
4288 	struct queue_limits lim;
4289 	struct request_queue *q;
4290 	char bsg_name[20];
4291 
4292 	fc_host->rqst_q = NULL;
4293 
4294 	if (!i->f->bsg_request)
4295 		return -ENOTSUPP;
4296 
4297 	snprintf(bsg_name, sizeof(bsg_name),
4298 		 "fc_host%d", shost->host_no);
4299 	scsi_init_limits(shost, &lim);
4300 	lim.max_segments = min_not_zero(lim.max_segments, i->f->max_bsg_segments);
4301 	q = bsg_setup_queue(dev, bsg_name, &lim, fc_bsg_dispatch,
4302 			fc_bsg_job_timeout, i->f->dd_bsg_size);
4303 	if (IS_ERR(q)) {
4304 		dev_err(dev,
4305 			"fc_host%d: bsg interface failed to initialize - setup queue\n",
4306 			shost->host_no);
4307 		return PTR_ERR(q);
4308 	}
4309 	blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
4310 	fc_host->rqst_q = q;
4311 	return 0;
4312 }
4313 
4314 /**
4315  * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
4316  * @shost:	shost that rport is attached to
4317  * @rport:	rport that the bsg hooks are being attached to
4318  */
4319 static int
4320 fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
4321 {
4322 	struct device *dev = &rport->dev;
4323 	struct fc_internal *i = to_fc_internal(shost->transportt);
4324 	struct queue_limits lim;
4325 	struct request_queue *q;
4326 
4327 	rport->rqst_q = NULL;
4328 
4329 	if (!i->f->bsg_request)
4330 		return -ENOTSUPP;
4331 
4332 	scsi_init_limits(shost, &lim);
4333 	lim.max_segments = min_not_zero(lim.max_segments, i->f->max_bsg_segments);
4334 	q = bsg_setup_queue(dev, dev_name(dev), &lim, fc_bsg_dispatch_prep,
4335 				fc_bsg_job_timeout, i->f->dd_bsg_size);
4336 	if (IS_ERR(q)) {
4337 		dev_err(dev, "failed to setup bsg queue\n");
4338 		return PTR_ERR(q);
4339 	}
4340 	blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
4341 	rport->rqst_q = q;
4342 	return 0;
4343 }
4344 
4345 
4346 /**
4347  * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
4348  * @q:	the request_queue that is to be torn down.
4349  *
4350  * Notes:
4351  *   Before unregistering the queue empty any requests that are blocked
4352  *
4353  *
4354  */
4355 static void
4356 fc_bsg_remove(struct request_queue *q)
4357 {
4358 	bsg_remove_queue(q);
4359 }
4360 
4361 
4362 /* Original Author:  Martin Hicks */
4363 MODULE_AUTHOR("James Smart");
4364 MODULE_DESCRIPTION("FC Transport Attributes");
4365 MODULE_LICENSE("GPL");
4366 
4367 module_init(fc_transport_init);
4368 module_exit(fc_transport_exit);
4369