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