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