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