xref: /linux/drivers/scsi/scsi_transport_fc.c (revision 7b12b9137930eb821b68e1bfa11e9de692208620)
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-2005   James Smart, Emulex Corporation
23  *    Rewrite for host, target, device, and remote port attributes,
24  *    statistics, and service functions...
25  *
26  */
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/sched.h>	/* workqueue stuff, HZ */
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport.h>
33 #include <scsi/scsi_transport_fc.h>
34 #include <scsi/scsi_cmnd.h>
35 #include "scsi_priv.h"
36 
37 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
38 
39 /*
40  * Redefine so that we can have same named attributes in the
41  * sdev/starget/host objects.
42  */
43 #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store)		\
44 struct class_device_attribute class_device_attr_##_prefix##_##_name = 	\
45 	__ATTR(_name,_mode,_show,_store)
46 
47 #define fc_enum_name_search(title, table_type, table)			\
48 static const char *get_fc_##title##_name(enum table_type table_key)	\
49 {									\
50 	int i;								\
51 	char *name = NULL;						\
52 									\
53 	for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {		\
54 		if (table[i].value == table_key) {			\
55 			name = table[i].name;				\
56 			break;						\
57 		}							\
58 	}								\
59 	return name;							\
60 }
61 
62 #define fc_enum_name_match(title, table_type, table)			\
63 static int get_fc_##title##_match(const char *table_key,		\
64 		enum table_type *value)					\
65 {									\
66 	int i;								\
67 									\
68 	for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {		\
69 		if (strncmp(table_key, table[i].name,			\
70 				table[i].matchlen) == 0) {		\
71 			*value = table[i].value;			\
72 			return 0; /* success */				\
73 		}							\
74 	}								\
75 	return 1; /* failure */						\
76 }
77 
78 
79 /* Convert fc_port_type values to ascii string name */
80 static struct {
81 	enum fc_port_type	value;
82 	char			*name;
83 } fc_port_type_names[] = {
84 	{ FC_PORTTYPE_UNKNOWN,		"Unknown" },
85 	{ FC_PORTTYPE_OTHER,		"Other" },
86 	{ FC_PORTTYPE_NOTPRESENT,	"Not Present" },
87 	{ FC_PORTTYPE_NPORT,	"NPort (fabric via point-to-point)" },
88 	{ FC_PORTTYPE_NLPORT,	"NLPort (fabric via loop)" },
89 	{ FC_PORTTYPE_LPORT,	"LPort (private loop)" },
90 	{ FC_PORTTYPE_PTP,	"Point-To-Point (direct nport connection" },
91 };
92 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
93 #define FC_PORTTYPE_MAX_NAMELEN		50
94 
95 
96 /* Convert fc_port_state values to ascii string name */
97 static struct {
98 	enum fc_port_state	value;
99 	char			*name;
100 } fc_port_state_names[] = {
101 	{ FC_PORTSTATE_UNKNOWN,		"Unknown" },
102 	{ FC_PORTSTATE_NOTPRESENT,	"Not Present" },
103 	{ FC_PORTSTATE_ONLINE,		"Online" },
104 	{ FC_PORTSTATE_OFFLINE,		"Offline" },
105 	{ FC_PORTSTATE_BLOCKED,		"Blocked" },
106 	{ FC_PORTSTATE_BYPASSED,	"Bypassed" },
107 	{ FC_PORTSTATE_DIAGNOSTICS,	"Diagnostics" },
108 	{ FC_PORTSTATE_LINKDOWN,	"Linkdown" },
109 	{ FC_PORTSTATE_ERROR,		"Error" },
110 	{ FC_PORTSTATE_LOOPBACK,	"Loopback" },
111 	{ FC_PORTSTATE_DELETED,		"Deleted" },
112 };
113 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
114 #define FC_PORTSTATE_MAX_NAMELEN	20
115 
116 
117 /* Convert fc_tgtid_binding_type values to ascii string name */
118 static const struct {
119 	enum fc_tgtid_binding_type	value;
120 	char				*name;
121 	int				matchlen;
122 } fc_tgtid_binding_type_names[] = {
123 	{ FC_TGTID_BIND_NONE, "none", 4 },
124 	{ FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
125 	{ FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
126 	{ FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
127 };
128 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
129 		fc_tgtid_binding_type_names)
130 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
131 		fc_tgtid_binding_type_names)
132 #define FC_BINDTYPE_MAX_NAMELEN	30
133 
134 
135 #define fc_bitfield_name_search(title, table)			\
136 static ssize_t							\
137 get_fc_##title##_names(u32 table_key, char *buf)		\
138 {								\
139 	char *prefix = "";					\
140 	ssize_t len = 0;					\
141 	int i;							\
142 								\
143 	for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) {	\
144 		if (table[i].value & table_key) {		\
145 			len += sprintf(buf + len, "%s%s",	\
146 				prefix, table[i].name);		\
147 			prefix = ", ";				\
148 		}						\
149 	}							\
150 	len += sprintf(buf + len, "\n");			\
151 	return len;						\
152 }
153 
154 
155 /* Convert FC_COS bit values to ascii string name */
156 static const struct {
157 	u32 			value;
158 	char			*name;
159 } fc_cos_names[] = {
160 	{ FC_COS_CLASS1,	"Class 1" },
161 	{ FC_COS_CLASS2,	"Class 2" },
162 	{ FC_COS_CLASS3,	"Class 3" },
163 	{ FC_COS_CLASS4,	"Class 4" },
164 	{ FC_COS_CLASS6,	"Class 6" },
165 };
166 fc_bitfield_name_search(cos, fc_cos_names)
167 
168 
169 /* Convert FC_PORTSPEED bit values to ascii string name */
170 static const struct {
171 	u32 			value;
172 	char			*name;
173 } fc_port_speed_names[] = {
174 	{ FC_PORTSPEED_1GBIT,		"1 Gbit" },
175 	{ FC_PORTSPEED_2GBIT,		"2 Gbit" },
176 	{ FC_PORTSPEED_4GBIT,		"4 Gbit" },
177 	{ FC_PORTSPEED_10GBIT,		"10 Gbit" },
178 	{ FC_PORTSPEED_NOT_NEGOTIATED,	"Not Negotiated" },
179 };
180 fc_bitfield_name_search(port_speed, fc_port_speed_names)
181 
182 
183 static int
184 show_fc_fc4s (char *buf, u8 *fc4_list)
185 {
186 	int i, len=0;
187 
188 	for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
189 		len += sprintf(buf + len , "0x%02x ", *fc4_list);
190 	len += sprintf(buf + len, "\n");
191 	return len;
192 }
193 
194 
195 /* Convert FC_RPORT_ROLE bit values to ascii string name */
196 static const struct {
197 	u32 			value;
198 	char			*name;
199 } fc_remote_port_role_names[] = {
200 	{ FC_RPORT_ROLE_FCP_TARGET,	"FCP Target" },
201 	{ FC_RPORT_ROLE_FCP_INITIATOR,	"FCP Initiator" },
202 	{ FC_RPORT_ROLE_IP_PORT,	"IP Port" },
203 };
204 fc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
205 
206 /*
207  * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
208  */
209 #define FC_WELLKNOWN_PORTID_MASK	0xfffff0
210 #define FC_WELLKNOWN_ROLE_MASK  	0x00000f
211 #define FC_FPORT_PORTID			0x00000e
212 #define FC_FABCTLR_PORTID		0x00000d
213 #define FC_DIRSRVR_PORTID		0x00000c
214 #define FC_TIMESRVR_PORTID		0x00000b
215 #define FC_MGMTSRVR_PORTID		0x00000a
216 
217 
218 static void fc_timeout_deleted_rport(void *data);
219 static void fc_scsi_scan_rport(void *data);
220 
221 /*
222  * Attribute counts pre object type...
223  * Increase these values if you add attributes
224  */
225 #define FC_STARGET_NUM_ATTRS 	3
226 #define FC_RPORT_NUM_ATTRS	9
227 #define FC_HOST_NUM_ATTRS	17
228 
229 struct fc_internal {
230 	struct scsi_transport_template t;
231 	struct fc_function_template *f;
232 
233 	/*
234 	 * For attributes : each object has :
235 	 *   An array of the actual attributes structures
236 	 *   An array of null-terminated pointers to the attribute
237 	 *     structures - used for mid-layer interaction.
238 	 *
239 	 * The attribute containers for the starget and host are are
240 	 * part of the midlayer. As the remote port is specific to the
241 	 * fc transport, we must provide the attribute container.
242 	 */
243 	struct class_device_attribute private_starget_attrs[
244 							FC_STARGET_NUM_ATTRS];
245 	struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
246 
247 	struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
248 	struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
249 
250 	struct transport_container rport_attr_cont;
251 	struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
252 	struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
253 };
254 
255 #define to_fc_internal(tmpl)	container_of(tmpl, struct fc_internal, t)
256 
257 static int fc_target_setup(struct transport_container *tc, struct device *dev,
258 			   struct class_device *cdev)
259 {
260 	struct scsi_target *starget = to_scsi_target(dev);
261 	struct fc_rport *rport = starget_to_rport(starget);
262 
263 	/*
264 	 * if parent is remote port, use values from remote port.
265 	 * Otherwise, this host uses the fc_transport, but not the
266 	 * remote port interface. As such, initialize to known non-values.
267 	 */
268 	if (rport) {
269 		fc_starget_node_name(starget) = rport->node_name;
270 		fc_starget_port_name(starget) = rport->port_name;
271 		fc_starget_port_id(starget) = rport->port_id;
272 	} else {
273 		fc_starget_node_name(starget) = -1;
274 		fc_starget_port_name(starget) = -1;
275 		fc_starget_port_id(starget) = -1;
276 	}
277 
278 	return 0;
279 }
280 
281 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
282 			       "fc_transport",
283 			       fc_target_setup,
284 			       NULL,
285 			       NULL);
286 
287 static int fc_host_setup(struct transport_container *tc, struct device *dev,
288 			 struct class_device *cdev)
289 {
290 	struct Scsi_Host *shost = dev_to_shost(dev);
291 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
292 
293 	/*
294 	 * Set default values easily detected by the midlayer as
295 	 * failure cases.  The scsi lldd is responsible for initializing
296 	 * all transport attributes to valid values per host.
297 	 */
298 	fc_host->node_name = -1;
299 	fc_host->port_name = -1;
300 	fc_host->permanent_port_name = -1;
301 	fc_host->supported_classes = FC_COS_UNSPECIFIED;
302 	memset(fc_host->supported_fc4s, 0,
303 		sizeof(fc_host->supported_fc4s));
304 	memset(fc_host->symbolic_name, 0,
305 		sizeof(fc_host->symbolic_name));
306 	fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
307 	fc_host->maxframe_size = -1;
308 	memset(fc_host->serial_number, 0,
309 		sizeof(fc_host->serial_number));
310 
311 	fc_host->port_id = -1;
312 	fc_host->port_type = FC_PORTTYPE_UNKNOWN;
313 	fc_host->port_state = FC_PORTSTATE_UNKNOWN;
314 	memset(fc_host->active_fc4s, 0,
315 		sizeof(fc_host->active_fc4s));
316 	fc_host->speed = FC_PORTSPEED_UNKNOWN;
317 	fc_host->fabric_name = -1;
318 
319 	fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
320 
321 	INIT_LIST_HEAD(&fc_host->rports);
322 	INIT_LIST_HEAD(&fc_host->rport_bindings);
323 	fc_host->next_rport_number = 0;
324 	fc_host->next_target_id = 0;
325 
326 	snprintf(fc_host->work_q_name, KOBJ_NAME_LEN, "fc_wq_%d",
327 		shost->host_no);
328 	fc_host->work_q = create_singlethread_workqueue(
329 					fc_host->work_q_name);
330 	if (!fc_host->work_q)
331 		return -ENOMEM;
332 
333 	snprintf(fc_host->devloss_work_q_name, KOBJ_NAME_LEN, "fc_dl_%d",
334 		shost->host_no);
335 	fc_host->devloss_work_q = create_singlethread_workqueue(
336 					fc_host->devloss_work_q_name);
337 	if (!fc_host->devloss_work_q) {
338 		destroy_workqueue(fc_host->work_q);
339 		fc_host->work_q = NULL;
340 		return -ENOMEM;
341 	}
342 
343 	return 0;
344 }
345 
346 static DECLARE_TRANSPORT_CLASS(fc_host_class,
347 			       "fc_host",
348 			       fc_host_setup,
349 			       NULL,
350 			       NULL);
351 
352 /*
353  * Setup and Remove actions for remote ports are handled
354  * in the service functions below.
355  */
356 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
357 			       "fc_remote_ports",
358 			       NULL,
359 			       NULL,
360 			       NULL);
361 
362 /*
363  * Module Parameters
364  */
365 
366 /*
367  * dev_loss_tmo: the default number of seconds that the FC transport
368  *   should insulate the loss of a remote port.
369  *   The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
370  */
371 static unsigned int fc_dev_loss_tmo = SCSI_DEVICE_BLOCK_MAX_TIMEOUT;
372 
373 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
374 MODULE_PARM_DESC(dev_loss_tmo,
375 		 "Maximum number of seconds that the FC transport should"
376 		 " insulate the loss of a remote port. Once this value is"
377 		 " exceeded, the scsi target is removed. Value should be"
378 		 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
379 
380 
381 static __init int fc_transport_init(void)
382 {
383 	int error = transport_class_register(&fc_host_class);
384 	if (error)
385 		return error;
386 	error = transport_class_register(&fc_rport_class);
387 	if (error)
388 		return error;
389 	return transport_class_register(&fc_transport_class);
390 }
391 
392 static void __exit fc_transport_exit(void)
393 {
394 	transport_class_unregister(&fc_transport_class);
395 	transport_class_unregister(&fc_rport_class);
396 	transport_class_unregister(&fc_host_class);
397 }
398 
399 /*
400  * FC Remote Port Attribute Management
401  */
402 
403 #define fc_rport_show_function(field, format_string, sz, cast)		\
404 static ssize_t								\
405 show_fc_rport_##field (struct class_device *cdev, char *buf)		\
406 {									\
407 	struct fc_rport *rport = transport_class_to_rport(cdev);	\
408 	struct Scsi_Host *shost = rport_to_shost(rport);		\
409 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
410 	if ((i->f->get_rport_##field) &&				\
411 	    !((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
412 	      (rport->port_state == FC_PORTSTATE_DELETED) ||		\
413 	      (rport->port_state == FC_PORTSTATE_NOTPRESENT)))		\
414 		i->f->get_rport_##field(rport);				\
415 	return snprintf(buf, sz, format_string, cast rport->field); 	\
416 }
417 
418 #define fc_rport_store_function(field)					\
419 static ssize_t								\
420 store_fc_rport_##field(struct class_device *cdev, const char *buf,	\
421 			   size_t count)				\
422 {									\
423 	int val;							\
424 	struct fc_rport *rport = transport_class_to_rport(cdev);	\
425 	struct Scsi_Host *shost = rport_to_shost(rport);		\
426 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
427 	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||		\
428 	    (rport->port_state == FC_PORTSTATE_DELETED) ||		\
429 	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))		\
430 		return -EBUSY;						\
431 	val = simple_strtoul(buf, NULL, 0);				\
432 	i->f->set_rport_##field(rport, val);				\
433 	return count;							\
434 }
435 
436 #define fc_rport_rd_attr(field, format_string, sz)			\
437 	fc_rport_show_function(field, format_string, sz, )		\
438 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,			\
439 			 show_fc_rport_##field, NULL)
440 
441 #define fc_rport_rd_attr_cast(field, format_string, sz, cast)		\
442 	fc_rport_show_function(field, format_string, sz, (cast))	\
443 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,			\
444 			  show_fc_rport_##field, NULL)
445 
446 #define fc_rport_rw_attr(field, format_string, sz)			\
447 	fc_rport_show_function(field, format_string, sz, )		\
448 	fc_rport_store_function(field)					\
449 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR,		\
450 			show_fc_rport_##field,				\
451 			store_fc_rport_##field)
452 
453 
454 #define fc_private_rport_show_function(field, format_string, sz, cast)	\
455 static ssize_t								\
456 show_fc_rport_##field (struct class_device *cdev, char *buf)		\
457 {									\
458 	struct fc_rport *rport = transport_class_to_rport(cdev);	\
459 	return snprintf(buf, sz, format_string, cast rport->field); 	\
460 }
461 
462 #define fc_private_rport_rd_attr(field, format_string, sz)		\
463 	fc_private_rport_show_function(field, format_string, sz, )	\
464 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,			\
465 			 show_fc_rport_##field, NULL)
466 
467 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast)	\
468 	fc_private_rport_show_function(field, format_string, sz, (cast)) \
469 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO,			\
470 			  show_fc_rport_##field, NULL)
471 
472 
473 #define fc_private_rport_rd_enum_attr(title, maxlen)			\
474 static ssize_t								\
475 show_fc_rport_##title (struct class_device *cdev, char *buf)		\
476 {									\
477 	struct fc_rport *rport = transport_class_to_rport(cdev);	\
478 	const char *name;						\
479 	name = get_fc_##title##_name(rport->title);			\
480 	if (!name)							\
481 		return -EINVAL;						\
482 	return snprintf(buf, maxlen, "%s\n", name);			\
483 }									\
484 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO,			\
485 			show_fc_rport_##title, NULL)
486 
487 
488 #define SETUP_RPORT_ATTRIBUTE_RD(field)					\
489 	i->private_rport_attrs[count] = class_device_attr_rport_##field; \
490 	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
491 	i->private_rport_attrs[count].store = NULL;			\
492 	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
493 	if (i->f->show_rport_##field)					\
494 		count++
495 
496 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field)				\
497 	i->private_rport_attrs[count] = class_device_attr_rport_##field; \
498 	i->private_rport_attrs[count].attr.mode = S_IRUGO;		\
499 	i->private_rport_attrs[count].store = NULL;			\
500 	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
501 	count++
502 
503 #define SETUP_RPORT_ATTRIBUTE_RW(field)					\
504 	i->private_rport_attrs[count] = class_device_attr_rport_##field; \
505 	if (!i->f->set_rport_##field) {					\
506 		i->private_rport_attrs[count].attr.mode = S_IRUGO;	\
507 		i->private_rport_attrs[count].store = NULL;		\
508 	}								\
509 	i->rport_attrs[count] = &i->private_rport_attrs[count];		\
510 	if (i->f->show_rport_##field)					\
511 		count++
512 
513 
514 /* The FC Transport Remote Port Attributes: */
515 
516 /* Fixed Remote Port Attributes */
517 
518 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
519 
520 static ssize_t
521 show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
522 {
523 	struct fc_rport *rport = transport_class_to_rport(cdev);
524 	if (rport->supported_classes == FC_COS_UNSPECIFIED)
525 		return snprintf(buf, 20, "unspecified\n");
526 	return get_fc_cos_names(rport->supported_classes, buf);
527 }
528 static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
529 		show_fc_rport_supported_classes, NULL);
530 
531 /* Dynamic Remote Port Attributes */
532 
533 /*
534  * dev_loss_tmo attribute
535  */
536 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
537 static ssize_t
538 store_fc_rport_dev_loss_tmo(struct class_device *cdev, const char *buf,
539 			   size_t count)
540 {
541 	int val;
542 	struct fc_rport *rport = transport_class_to_rport(cdev);
543 	struct Scsi_Host *shost = rport_to_shost(rport);
544 	struct fc_internal *i = to_fc_internal(shost->transportt);
545 	if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
546 	    (rport->port_state == FC_PORTSTATE_DELETED) ||
547 	    (rport->port_state == FC_PORTSTATE_NOTPRESENT))
548 		return -EBUSY;
549 	val = simple_strtoul(buf, NULL, 0);
550 	if ((val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
551 		return -EINVAL;
552 	i->f->set_rport_dev_loss_tmo(rport, val);
553 	return count;
554 }
555 static FC_CLASS_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
556 		show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
557 
558 
559 /* Private Remote Port Attributes */
560 
561 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
562 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
563 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
564 
565 static ssize_t
566 show_fc_rport_roles (struct class_device *cdev, char *buf)
567 {
568 	struct fc_rport *rport = transport_class_to_rport(cdev);
569 
570 	/* identify any roles that are port_id specific */
571 	if ((rport->port_id != -1) &&
572 	    (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
573 					FC_WELLKNOWN_PORTID_MASK) {
574 		switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
575 		case FC_FPORT_PORTID:
576 			return snprintf(buf, 30, "Fabric Port\n");
577 		case FC_FABCTLR_PORTID:
578 			return snprintf(buf, 30, "Fabric Controller\n");
579 		case FC_DIRSRVR_PORTID:
580 			return snprintf(buf, 30, "Directory Server\n");
581 		case FC_TIMESRVR_PORTID:
582 			return snprintf(buf, 30, "Time Server\n");
583 		case FC_MGMTSRVR_PORTID:
584 			return snprintf(buf, 30, "Management Server\n");
585 		default:
586 			return snprintf(buf, 30, "Unknown Fabric Entity\n");
587 		}
588 	} else {
589 		if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
590 			return snprintf(buf, 20, "unknown\n");
591 		return get_fc_remote_port_roles_names(rport->roles, buf);
592 	}
593 }
594 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
595 		show_fc_rport_roles, NULL);
596 
597 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
598 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
599 
600 
601 
602 /*
603  * FC SCSI Target Attribute Management
604  */
605 
606 /*
607  * Note: in the target show function we recognize when the remote
608  *  port is in the heirarchy and do not allow the driver to get
609  *  involved in sysfs functions. The driver only gets involved if
610  *  it's the "old" style that doesn't use rports.
611  */
612 #define fc_starget_show_function(field, format_string, sz, cast)	\
613 static ssize_t								\
614 show_fc_starget_##field (struct class_device *cdev, char *buf)		\
615 {									\
616 	struct scsi_target *starget = transport_class_to_starget(cdev);	\
617 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);	\
618 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
619 	struct fc_rport *rport = starget_to_rport(starget);		\
620 	if (rport)							\
621 		fc_starget_##field(starget) = rport->field;		\
622 	else if (i->f->get_starget_##field)				\
623 		i->f->get_starget_##field(starget);			\
624 	return snprintf(buf, sz, format_string, 			\
625 		cast fc_starget_##field(starget)); 			\
626 }
627 
628 #define fc_starget_rd_attr(field, format_string, sz)			\
629 	fc_starget_show_function(field, format_string, sz, )		\
630 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,			\
631 			 show_fc_starget_##field, NULL)
632 
633 #define fc_starget_rd_attr_cast(field, format_string, sz, cast)		\
634 	fc_starget_show_function(field, format_string, sz, (cast))	\
635 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO,			\
636 			  show_fc_starget_##field, NULL)
637 
638 #define SETUP_STARGET_ATTRIBUTE_RD(field)				\
639 	i->private_starget_attrs[count] = class_device_attr_starget_##field; \
640 	i->private_starget_attrs[count].attr.mode = S_IRUGO;		\
641 	i->private_starget_attrs[count].store = NULL;			\
642 	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
643 	if (i->f->show_starget_##field)					\
644 		count++
645 
646 #define SETUP_STARGET_ATTRIBUTE_RW(field)				\
647 	i->private_starget_attrs[count] = class_device_attr_starget_##field; \
648 	if (!i->f->set_starget_##field) {				\
649 		i->private_starget_attrs[count].attr.mode = S_IRUGO;	\
650 		i->private_starget_attrs[count].store = NULL;		\
651 	}								\
652 	i->starget_attrs[count] = &i->private_starget_attrs[count];	\
653 	if (i->f->show_starget_##field)					\
654 		count++
655 
656 /* The FC Transport SCSI Target Attributes: */
657 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
658 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
659 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
660 
661 
662 /*
663  * Host Attribute Management
664  */
665 
666 #define fc_host_show_function(field, format_string, sz, cast)		\
667 static ssize_t								\
668 show_fc_host_##field (struct class_device *cdev, char *buf)		\
669 {									\
670 	struct Scsi_Host *shost = transport_class_to_shost(cdev);	\
671 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
672 	if (i->f->get_host_##field)					\
673 		i->f->get_host_##field(shost);				\
674 	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
675 }
676 
677 #define fc_host_store_function(field)					\
678 static ssize_t								\
679 store_fc_host_##field(struct class_device *cdev, const char *buf,	\
680 			   size_t count)				\
681 {									\
682 	int val;							\
683 	struct Scsi_Host *shost = transport_class_to_shost(cdev);	\
684 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
685 									\
686 	val = simple_strtoul(buf, NULL, 0);				\
687 	i->f->set_host_##field(shost, val);				\
688 	return count;							\
689 }
690 
691 #define fc_host_rd_attr(field, format_string, sz)			\
692 	fc_host_show_function(field, format_string, sz, )		\
693 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,			\
694 			 show_fc_host_##field, NULL)
695 
696 #define fc_host_rd_attr_cast(field, format_string, sz, cast)		\
697 	fc_host_show_function(field, format_string, sz, (cast))		\
698 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,			\
699 			  show_fc_host_##field, NULL)
700 
701 #define fc_host_rw_attr(field, format_string, sz)			\
702 	fc_host_show_function(field, format_string, sz, )		\
703 	fc_host_store_function(field)					\
704 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR,		\
705 			show_fc_host_##field,				\
706 			store_fc_host_##field)
707 
708 #define fc_host_rd_enum_attr(title, maxlen)				\
709 static ssize_t								\
710 show_fc_host_##title (struct class_device *cdev, char *buf)		\
711 {									\
712 	struct Scsi_Host *shost = transport_class_to_shost(cdev);	\
713 	struct fc_internal *i = to_fc_internal(shost->transportt);	\
714 	const char *name;						\
715 	if (i->f->get_host_##title)					\
716 		i->f->get_host_##title(shost);				\
717 	name = get_fc_##title##_name(fc_host_##title(shost));		\
718 	if (!name)							\
719 		return -EINVAL;						\
720 	return snprintf(buf, maxlen, "%s\n", name);			\
721 }									\
722 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
723 
724 #define SETUP_HOST_ATTRIBUTE_RD(field)					\
725 	i->private_host_attrs[count] = class_device_attr_host_##field;	\
726 	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
727 	i->private_host_attrs[count].store = NULL;			\
728 	i->host_attrs[count] = &i->private_host_attrs[count];		\
729 	if (i->f->show_host_##field)					\
730 		count++
731 
732 #define SETUP_HOST_ATTRIBUTE_RW(field)					\
733 	i->private_host_attrs[count] = class_device_attr_host_##field;	\
734 	if (!i->f->set_host_##field) {					\
735 		i->private_host_attrs[count].attr.mode = S_IRUGO;	\
736 		i->private_host_attrs[count].store = NULL;		\
737 	}								\
738 	i->host_attrs[count] = &i->private_host_attrs[count];		\
739 	if (i->f->show_host_##field)					\
740 		count++
741 
742 
743 #define fc_private_host_show_function(field, format_string, sz, cast)	\
744 static ssize_t								\
745 show_fc_host_##field (struct class_device *cdev, char *buf)		\
746 {									\
747 	struct Scsi_Host *shost = transport_class_to_shost(cdev);	\
748 	return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
749 }
750 
751 #define fc_private_host_rd_attr(field, format_string, sz)		\
752 	fc_private_host_show_function(field, format_string, sz, )	\
753 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,			\
754 			 show_fc_host_##field, NULL)
755 
756 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast)	\
757 	fc_private_host_show_function(field, format_string, sz, (cast)) \
758 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO,			\
759 			  show_fc_host_##field, NULL)
760 
761 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field)			\
762 	i->private_host_attrs[count] = class_device_attr_host_##field;	\
763 	i->private_host_attrs[count].attr.mode = S_IRUGO;		\
764 	i->private_host_attrs[count].store = NULL;			\
765 	i->host_attrs[count] = &i->private_host_attrs[count];		\
766 	count++
767 
768 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field)			\
769 {									\
770 	i->private_host_attrs[count] = class_device_attr_host_##field;	\
771 	i->host_attrs[count] = &i->private_host_attrs[count];		\
772 	count++;							\
773 }
774 
775 
776 /* Fixed Host Attributes */
777 
778 static ssize_t
779 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
780 {
781 	struct Scsi_Host *shost = transport_class_to_shost(cdev);
782 
783 	if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
784 		return snprintf(buf, 20, "unspecified\n");
785 
786 	return get_fc_cos_names(fc_host_supported_classes(shost), buf);
787 }
788 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
789 		show_fc_host_supported_classes, NULL);
790 
791 static ssize_t
792 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
793 {
794 	struct Scsi_Host *shost = transport_class_to_shost(cdev);
795 	return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
796 }
797 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
798 		show_fc_host_supported_fc4s, NULL);
799 
800 static ssize_t
801 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
802 {
803 	struct Scsi_Host *shost = transport_class_to_shost(cdev);
804 
805 	if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
806 		return snprintf(buf, 20, "unknown\n");
807 
808 	return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
809 }
810 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
811 		show_fc_host_supported_speeds, NULL);
812 
813 
814 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
815 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
816 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
817 			     unsigned long long);
818 fc_private_host_rd_attr(symbolic_name, "%s\n", (FC_SYMBOLIC_NAME_SIZE +1));
819 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
820 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
821 
822 
823 /* Dynamic Host Attributes */
824 
825 static ssize_t
826 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
827 {
828 	struct Scsi_Host *shost = transport_class_to_shost(cdev);
829 	struct fc_internal *i = to_fc_internal(shost->transportt);
830 
831 	if (i->f->get_host_active_fc4s)
832 		i->f->get_host_active_fc4s(shost);
833 
834 	return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
835 }
836 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
837 		show_fc_host_active_fc4s, NULL);
838 
839 static ssize_t
840 show_fc_host_speed (struct class_device *cdev, char *buf)
841 {
842 	struct Scsi_Host *shost = transport_class_to_shost(cdev);
843 	struct fc_internal *i = to_fc_internal(shost->transportt);
844 
845 	if (i->f->get_host_speed)
846 		i->f->get_host_speed(shost);
847 
848 	if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
849 		return snprintf(buf, 20, "unknown\n");
850 
851 	return get_fc_port_speed_names(fc_host_speed(shost), buf);
852 }
853 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
854 		show_fc_host_speed, NULL);
855 
856 
857 fc_host_rd_attr(port_id, "0x%06x\n", 20);
858 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
859 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
860 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
861 
862 
863 /* Private Host Attributes */
864 
865 static ssize_t
866 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
867 {
868 	struct Scsi_Host *shost = transport_class_to_shost(cdev);
869 	const char *name;
870 
871 	name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
872 	if (!name)
873 		return -EINVAL;
874 	return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
875 }
876 
877 #define get_list_head_entry(pos, head, member) 		\
878 	pos = list_entry((head)->next, typeof(*pos), member)
879 
880 static ssize_t
881 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
882 	const char *buf, size_t count)
883 {
884 	struct Scsi_Host *shost = transport_class_to_shost(cdev);
885 	struct fc_rport *rport;
886  	enum fc_tgtid_binding_type val;
887 	unsigned long flags;
888 
889 	if (get_fc_tgtid_bind_type_match(buf, &val))
890 		return -EINVAL;
891 
892 	/* if changing bind type, purge all unused consistent bindings */
893 	if (val != fc_host_tgtid_bind_type(shost)) {
894 		spin_lock_irqsave(shost->host_lock, flags);
895 		while (!list_empty(&fc_host_rport_bindings(shost))) {
896 			get_list_head_entry(rport,
897 				&fc_host_rport_bindings(shost), peers);
898 			list_del(&rport->peers);
899 			rport->port_state = FC_PORTSTATE_DELETED;
900 			fc_queue_work(shost, &rport->rport_delete_work);
901 		}
902 		spin_unlock_irqrestore(shost->host_lock, flags);
903 	}
904 
905 	fc_host_tgtid_bind_type(shost) = val;
906 	return count;
907 }
908 
909 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
910 			show_fc_private_host_tgtid_bind_type,
911 			store_fc_private_host_tgtid_bind_type);
912 
913 static ssize_t
914 store_fc_private_host_issue_lip(struct class_device *cdev,
915 	const char *buf, size_t count)
916 {
917 	struct Scsi_Host *shost = transport_class_to_shost(cdev);
918 	struct fc_internal *i = to_fc_internal(shost->transportt);
919 	int ret;
920 
921 	/* ignore any data value written to the attribute */
922 	if (i->f->issue_fc_host_lip) {
923 		ret = i->f->issue_fc_host_lip(shost);
924 		return ret ? ret: count;
925 	}
926 
927 	return -ENOENT;
928 }
929 
930 static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
931 			store_fc_private_host_issue_lip);
932 
933 /*
934  * Host Statistics Management
935  */
936 
937 /* Show a given an attribute in the statistics group */
938 static ssize_t
939 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
940 {
941 	struct Scsi_Host *shost = transport_class_to_shost(cdev);
942 	struct fc_internal *i = to_fc_internal(shost->transportt);
943 	struct fc_host_statistics *stats;
944 	ssize_t ret = -ENOENT;
945 
946 	if (offset > sizeof(struct fc_host_statistics) ||
947 	    offset % sizeof(u64) != 0)
948 		WARN_ON(1);
949 
950 	if (i->f->get_fc_host_stats) {
951 		stats = (i->f->get_fc_host_stats)(shost);
952 		if (stats)
953 			ret = snprintf(buf, 20, "0x%llx\n",
954 			      (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
955 	}
956 	return ret;
957 }
958 
959 
960 /* generate a read-only statistics attribute */
961 #define fc_host_statistic(name)						\
962 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) 	\
963 {									\
964 	return fc_stat_show(cd, buf, 					\
965 			    offsetof(struct fc_host_statistics, name));	\
966 }									\
967 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
968 
969 fc_host_statistic(seconds_since_last_reset);
970 fc_host_statistic(tx_frames);
971 fc_host_statistic(tx_words);
972 fc_host_statistic(rx_frames);
973 fc_host_statistic(rx_words);
974 fc_host_statistic(lip_count);
975 fc_host_statistic(nos_count);
976 fc_host_statistic(error_frames);
977 fc_host_statistic(dumped_frames);
978 fc_host_statistic(link_failure_count);
979 fc_host_statistic(loss_of_sync_count);
980 fc_host_statistic(loss_of_signal_count);
981 fc_host_statistic(prim_seq_protocol_err_count);
982 fc_host_statistic(invalid_tx_word_count);
983 fc_host_statistic(invalid_crc_count);
984 fc_host_statistic(fcp_input_requests);
985 fc_host_statistic(fcp_output_requests);
986 fc_host_statistic(fcp_control_requests);
987 fc_host_statistic(fcp_input_megabytes);
988 fc_host_statistic(fcp_output_megabytes);
989 
990 static ssize_t
991 fc_reset_statistics(struct class_device *cdev, const char *buf,
992 			   size_t count)
993 {
994 	struct Scsi_Host *shost = transport_class_to_shost(cdev);
995 	struct fc_internal *i = to_fc_internal(shost->transportt);
996 
997 	/* ignore any data value written to the attribute */
998 	if (i->f->reset_fc_host_stats) {
999 		i->f->reset_fc_host_stats(shost);
1000 		return count;
1001 	}
1002 
1003 	return -ENOENT;
1004 }
1005 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1006 				fc_reset_statistics);
1007 
1008 
1009 static struct attribute *fc_statistics_attrs[] = {
1010 	&class_device_attr_host_seconds_since_last_reset.attr,
1011 	&class_device_attr_host_tx_frames.attr,
1012 	&class_device_attr_host_tx_words.attr,
1013 	&class_device_attr_host_rx_frames.attr,
1014 	&class_device_attr_host_rx_words.attr,
1015 	&class_device_attr_host_lip_count.attr,
1016 	&class_device_attr_host_nos_count.attr,
1017 	&class_device_attr_host_error_frames.attr,
1018 	&class_device_attr_host_dumped_frames.attr,
1019 	&class_device_attr_host_link_failure_count.attr,
1020 	&class_device_attr_host_loss_of_sync_count.attr,
1021 	&class_device_attr_host_loss_of_signal_count.attr,
1022 	&class_device_attr_host_prim_seq_protocol_err_count.attr,
1023 	&class_device_attr_host_invalid_tx_word_count.attr,
1024 	&class_device_attr_host_invalid_crc_count.attr,
1025 	&class_device_attr_host_fcp_input_requests.attr,
1026 	&class_device_attr_host_fcp_output_requests.attr,
1027 	&class_device_attr_host_fcp_control_requests.attr,
1028 	&class_device_attr_host_fcp_input_megabytes.attr,
1029 	&class_device_attr_host_fcp_output_megabytes.attr,
1030 	&class_device_attr_host_reset_statistics.attr,
1031 	NULL
1032 };
1033 
1034 static struct attribute_group fc_statistics_group = {
1035 	.name = "statistics",
1036 	.attrs = fc_statistics_attrs,
1037 };
1038 
1039 static int fc_host_match(struct attribute_container *cont,
1040 			  struct device *dev)
1041 {
1042 	struct Scsi_Host *shost;
1043 	struct fc_internal *i;
1044 
1045 	if (!scsi_is_host_device(dev))
1046 		return 0;
1047 
1048 	shost = dev_to_shost(dev);
1049 	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1050 	    != &fc_host_class.class)
1051 		return 0;
1052 
1053 	i = to_fc_internal(shost->transportt);
1054 
1055 	return &i->t.host_attrs.ac == cont;
1056 }
1057 
1058 static int fc_target_match(struct attribute_container *cont,
1059 			    struct device *dev)
1060 {
1061 	struct Scsi_Host *shost;
1062 	struct fc_internal *i;
1063 
1064 	if (!scsi_is_target_device(dev))
1065 		return 0;
1066 
1067 	shost = dev_to_shost(dev->parent);
1068 	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1069 	    != &fc_host_class.class)
1070 		return 0;
1071 
1072 	i = to_fc_internal(shost->transportt);
1073 
1074 	return &i->t.target_attrs.ac == cont;
1075 }
1076 
1077 static void fc_rport_dev_release(struct device *dev)
1078 {
1079 	struct fc_rport *rport = dev_to_rport(dev);
1080 	put_device(dev->parent);
1081 	kfree(rport);
1082 }
1083 
1084 int scsi_is_fc_rport(const struct device *dev)
1085 {
1086 	return dev->release == fc_rport_dev_release;
1087 }
1088 EXPORT_SYMBOL(scsi_is_fc_rport);
1089 
1090 static int fc_rport_match(struct attribute_container *cont,
1091 			    struct device *dev)
1092 {
1093 	struct Scsi_Host *shost;
1094 	struct fc_internal *i;
1095 
1096 	if (!scsi_is_fc_rport(dev))
1097 		return 0;
1098 
1099 	shost = dev_to_shost(dev->parent);
1100 	if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1101 	    != &fc_host_class.class)
1102 		return 0;
1103 
1104 	i = to_fc_internal(shost->transportt);
1105 
1106 	return &i->rport_attr_cont.ac == cont;
1107 }
1108 
1109 
1110 /**
1111  * fc_timed_out - FC Transport I/O timeout intercept handler
1112  *
1113  * @scmd:	The SCSI command which timed out
1114  *
1115  * This routine protects against error handlers getting invoked while a
1116  * rport is in a blocked state, typically due to a temporarily loss of
1117  * connectivity. If the error handlers are allowed to proceed, requests
1118  * to abort i/o, reset the target, etc will likely fail as there is no way
1119  * to communicate with the device to perform the requested function. These
1120  * failures may result in the midlayer taking the device offline, requiring
1121  * manual intervention to restore operation.
1122  *
1123  * This routine, called whenever an i/o times out, validates the state of
1124  * the underlying rport. If the rport is blocked, it returns
1125  * EH_RESET_TIMER, which will continue to reschedule the timeout.
1126  * Eventually, either the device will return, or devloss_tmo will fire,
1127  * and when the timeout then fires, it will be handled normally.
1128  * If the rport is not blocked, normal error handling continues.
1129  *
1130  * Notes:
1131  *	This routine assumes no locks are held on entry.
1132  **/
1133 static enum scsi_eh_timer_return
1134 fc_timed_out(struct scsi_cmnd *scmd)
1135 {
1136 	struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1137 
1138 	if (rport->port_state == FC_PORTSTATE_BLOCKED)
1139 		return EH_RESET_TIMER;
1140 
1141 	return EH_NOT_HANDLED;
1142 }
1143 
1144 /*
1145  * Must be called with shost->host_lock held
1146  */
1147 static int fc_user_scan(struct Scsi_Host *shost, uint channel,
1148 		uint id, uint lun)
1149 {
1150 	struct fc_rport *rport;
1151 
1152 	list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1153 		if (rport->scsi_target_id == -1)
1154 			continue;
1155 
1156 		if ((channel == SCAN_WILD_CARD || channel == rport->channel) &&
1157 		    (id == SCAN_WILD_CARD || id == rport->scsi_target_id)) {
1158 			scsi_scan_target(&rport->dev, rport->channel,
1159 					 rport->scsi_target_id, lun, 1);
1160 		}
1161 	}
1162 
1163 	return 0;
1164 }
1165 
1166 struct scsi_transport_template *
1167 fc_attach_transport(struct fc_function_template *ft)
1168 {
1169 	int count;
1170 	struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
1171 					GFP_KERNEL);
1172 
1173 	if (unlikely(!i))
1174 		return NULL;
1175 
1176 	i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1177 	i->t.target_attrs.ac.class = &fc_transport_class.class;
1178 	i->t.target_attrs.ac.match = fc_target_match;
1179 	i->t.target_size = sizeof(struct fc_starget_attrs);
1180 	transport_container_register(&i->t.target_attrs);
1181 
1182 	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1183 	i->t.host_attrs.ac.class = &fc_host_class.class;
1184 	i->t.host_attrs.ac.match = fc_host_match;
1185 	i->t.host_size = sizeof(struct fc_host_attrs);
1186 	if (ft->get_fc_host_stats)
1187 		i->t.host_attrs.statistics = &fc_statistics_group;
1188 	transport_container_register(&i->t.host_attrs);
1189 
1190 	i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1191 	i->rport_attr_cont.ac.class = &fc_rport_class.class;
1192 	i->rport_attr_cont.ac.match = fc_rport_match;
1193 	transport_container_register(&i->rport_attr_cont);
1194 
1195 	i->f = ft;
1196 
1197 	/* Transport uses the shost workq for scsi scanning */
1198 	i->t.create_work_queue = 1;
1199 
1200 	i->t.eh_timed_out = fc_timed_out;
1201 
1202 	i->t.user_scan = fc_user_scan;
1203 
1204 	/*
1205 	 * Setup SCSI Target Attributes.
1206 	 */
1207 	count = 0;
1208 	SETUP_STARGET_ATTRIBUTE_RD(node_name);
1209 	SETUP_STARGET_ATTRIBUTE_RD(port_name);
1210 	SETUP_STARGET_ATTRIBUTE_RD(port_id);
1211 
1212 	BUG_ON(count > FC_STARGET_NUM_ATTRS);
1213 
1214 	i->starget_attrs[count] = NULL;
1215 
1216 
1217 	/*
1218 	 * Setup SCSI Host Attributes.
1219 	 */
1220 	count=0;
1221 	SETUP_HOST_ATTRIBUTE_RD(node_name);
1222 	SETUP_HOST_ATTRIBUTE_RD(port_name);
1223 	SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
1224 	SETUP_HOST_ATTRIBUTE_RD(supported_classes);
1225 	SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1226 	SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
1227 	SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
1228 	SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
1229 	SETUP_HOST_ATTRIBUTE_RD(serial_number);
1230 
1231 	SETUP_HOST_ATTRIBUTE_RD(port_id);
1232 	SETUP_HOST_ATTRIBUTE_RD(port_type);
1233 	SETUP_HOST_ATTRIBUTE_RD(port_state);
1234 	SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
1235 	SETUP_HOST_ATTRIBUTE_RD(speed);
1236 	SETUP_HOST_ATTRIBUTE_RD(fabric_name);
1237 
1238 	/* Transport-managed attributes */
1239 	SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
1240 	if (ft->issue_fc_host_lip)
1241 		SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
1242 
1243 	BUG_ON(count > FC_HOST_NUM_ATTRS);
1244 
1245 	i->host_attrs[count] = NULL;
1246 
1247 	/*
1248 	 * Setup Remote Port Attributes.
1249 	 */
1250 	count=0;
1251 	SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
1252 	SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
1253 	SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
1254 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
1255 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
1256 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
1257 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
1258 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
1259 	SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
1260 
1261 	BUG_ON(count > FC_RPORT_NUM_ATTRS);
1262 
1263 	i->rport_attrs[count] = NULL;
1264 
1265 	return &i->t;
1266 }
1267 EXPORT_SYMBOL(fc_attach_transport);
1268 
1269 void fc_release_transport(struct scsi_transport_template *t)
1270 {
1271 	struct fc_internal *i = to_fc_internal(t);
1272 
1273 	transport_container_unregister(&i->t.target_attrs);
1274 	transport_container_unregister(&i->t.host_attrs);
1275 	transport_container_unregister(&i->rport_attr_cont);
1276 
1277 	kfree(i);
1278 }
1279 EXPORT_SYMBOL(fc_release_transport);
1280 
1281 /**
1282  * fc_queue_work - Queue work to the fc_host workqueue.
1283  * @shost:	Pointer to Scsi_Host bound to fc_host.
1284  * @work:	Work to queue for execution.
1285  *
1286  * Return value:
1287  * 	0 on success / != 0 for error
1288  **/
1289 static int
1290 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
1291 {
1292 	if (unlikely(!fc_host_work_q(shost))) {
1293 		printk(KERN_ERR
1294 			"ERROR: FC host '%s' attempted to queue work, "
1295 			"when no workqueue created.\n", shost->hostt->name);
1296 		dump_stack();
1297 
1298 		return -EINVAL;
1299 	}
1300 
1301 	return queue_work(fc_host_work_q(shost), work);
1302 }
1303 
1304 /**
1305  * fc_flush_work - Flush a fc_host's workqueue.
1306  * @shost:	Pointer to Scsi_Host bound to fc_host.
1307  **/
1308 static void
1309 fc_flush_work(struct Scsi_Host *shost)
1310 {
1311 	if (!fc_host_work_q(shost)) {
1312 		printk(KERN_ERR
1313 			"ERROR: FC host '%s' attempted to flush work, "
1314 			"when no workqueue created.\n", shost->hostt->name);
1315 		dump_stack();
1316 		return;
1317 	}
1318 
1319 	flush_workqueue(fc_host_work_q(shost));
1320 }
1321 
1322 /**
1323  * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
1324  * @shost:	Pointer to Scsi_Host bound to fc_host.
1325  * @work:	Work to queue for execution.
1326  * @delay:	jiffies to delay the work queuing
1327  *
1328  * Return value:
1329  * 	0 on success / != 0 for error
1330  **/
1331 static int
1332 fc_queue_devloss_work(struct Scsi_Host *shost, struct work_struct *work,
1333 				unsigned long delay)
1334 {
1335 	if (unlikely(!fc_host_devloss_work_q(shost))) {
1336 		printk(KERN_ERR
1337 			"ERROR: FC host '%s' attempted to queue work, "
1338 			"when no workqueue created.\n", shost->hostt->name);
1339 		dump_stack();
1340 
1341 		return -EINVAL;
1342 	}
1343 
1344 	return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
1345 }
1346 
1347 /**
1348  * fc_flush_devloss - Flush a fc_host's devloss workqueue.
1349  * @shost:	Pointer to Scsi_Host bound to fc_host.
1350  **/
1351 static void
1352 fc_flush_devloss(struct Scsi_Host *shost)
1353 {
1354 	if (!fc_host_devloss_work_q(shost)) {
1355 		printk(KERN_ERR
1356 			"ERROR: FC host '%s' attempted to flush work, "
1357 			"when no workqueue created.\n", shost->hostt->name);
1358 		dump_stack();
1359 		return;
1360 	}
1361 
1362 	flush_workqueue(fc_host_devloss_work_q(shost));
1363 }
1364 
1365 
1366 /**
1367  * fc_remove_host - called to terminate any fc_transport-related elements
1368  *                  for a scsi host.
1369  * @rport:	remote port to be unblocked.
1370  *
1371  * This routine is expected to be called immediately preceeding the
1372  * a driver's call to scsi_remove_host().
1373  *
1374  * WARNING: A driver utilizing the fc_transport, which fails to call
1375  *   this routine prior to scsi_remote_host(), will leave dangling
1376  *   objects in /sys/class/fc_remote_ports. Access to any of these
1377  *   objects can result in a system crash !!!
1378  *
1379  * Notes:
1380  *	This routine assumes no locks are held on entry.
1381  **/
1382 void
1383 fc_remove_host(struct Scsi_Host *shost)
1384 {
1385 	struct fc_rport *rport, *next_rport;
1386 	struct workqueue_struct *work_q;
1387 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1388 
1389 	/* Remove any remote ports */
1390 	list_for_each_entry_safe(rport, next_rport,
1391 			&fc_host->rports, peers) {
1392 		list_del(&rport->peers);
1393 		rport->port_state = FC_PORTSTATE_DELETED;
1394 		fc_queue_work(shost, &rport->rport_delete_work);
1395 	}
1396 
1397 	list_for_each_entry_safe(rport, next_rport,
1398 			&fc_host->rport_bindings, peers) {
1399 		list_del(&rport->peers);
1400 		rport->port_state = FC_PORTSTATE_DELETED;
1401 		fc_queue_work(shost, &rport->rport_delete_work);
1402 	}
1403 
1404 	/* flush all scan work items */
1405 	scsi_flush_work(shost);
1406 
1407 	/* flush all stgt delete, and rport delete work items, then kill it  */
1408 	if (fc_host->work_q) {
1409 		work_q = fc_host->work_q;
1410 		fc_host->work_q = NULL;
1411 		destroy_workqueue(work_q);
1412 	}
1413 
1414 	/* flush all devloss work items, then kill it  */
1415 	if (fc_host->devloss_work_q) {
1416 		work_q = fc_host->devloss_work_q;
1417 		fc_host->devloss_work_q = NULL;
1418 		destroy_workqueue(work_q);
1419 	}
1420 }
1421 EXPORT_SYMBOL(fc_remove_host);
1422 
1423 
1424 /**
1425  * fc_starget_delete - called to delete the scsi decendents of an rport
1426  *                  (target and all sdevs)
1427  *
1428  * @data:	remote port to be operated on.
1429  **/
1430 static void
1431 fc_starget_delete(void *data)
1432 {
1433 	struct fc_rport *rport = (struct fc_rport *)data;
1434 	struct Scsi_Host *shost = rport_to_shost(rport);
1435 	unsigned long flags;
1436 
1437 	scsi_target_unblock(&rport->dev);
1438 
1439 	spin_lock_irqsave(shost->host_lock, flags);
1440 	if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
1441 		spin_unlock_irqrestore(shost->host_lock, flags);
1442 		if (!cancel_delayed_work(&rport->dev_loss_work))
1443 			fc_flush_devloss(shost);
1444 		spin_lock_irqsave(shost->host_lock, flags);
1445 		rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
1446 	}
1447 	spin_unlock_irqrestore(shost->host_lock, flags);
1448 
1449 	scsi_remove_target(&rport->dev);
1450 }
1451 
1452 
1453 /**
1454  * fc_rport_final_delete - finish rport termination and delete it.
1455  *
1456  * @data:	remote port to be deleted.
1457  **/
1458 static void
1459 fc_rport_final_delete(void *data)
1460 {
1461 	struct fc_rport *rport = (struct fc_rport *)data;
1462 	struct device *dev = &rport->dev;
1463 	struct Scsi_Host *shost = rport_to_shost(rport);
1464 
1465 	/* Delete SCSI target and sdevs */
1466 	if (rport->scsi_target_id != -1)
1467 		fc_starget_delete(data);
1468 
1469 	/*
1470 	 * if a scan is pending, flush the SCSI Host work_q so that
1471 	 * that we can reclaim the rport scan work element.
1472 	 */
1473 	if (rport->flags & FC_RPORT_SCAN_PENDING)
1474 		scsi_flush_work(shost);
1475 
1476 	transport_remove_device(dev);
1477 	device_del(dev);
1478 	transport_destroy_device(dev);
1479 	put_device(&shost->shost_gendev);
1480 }
1481 
1482 
1483 /**
1484  * fc_rport_create - allocates and creates a remote FC port.
1485  * @shost:	scsi host the remote port is connected to.
1486  * @channel:	Channel on shost port connected to.
1487  * @ids:	The world wide names, fc address, and FC4 port
1488  *		roles for the remote port.
1489  *
1490  * Allocates and creates the remoter port structure, including the
1491  * class and sysfs creation.
1492  *
1493  * Notes:
1494  *	This routine assumes no locks are held on entry.
1495  **/
1496 struct fc_rport *
1497 fc_rport_create(struct Scsi_Host *shost, int channel,
1498 	struct fc_rport_identifiers  *ids)
1499 {
1500 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1501 	struct fc_internal *fci = to_fc_internal(shost->transportt);
1502 	struct fc_rport *rport;
1503 	struct device *dev;
1504 	unsigned long flags;
1505 	int error;
1506 	size_t size;
1507 
1508 	size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
1509 	rport = kzalloc(size, GFP_KERNEL);
1510 	if (unlikely(!rport)) {
1511 		printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
1512 		return NULL;
1513 	}
1514 
1515 	rport->maxframe_size = -1;
1516 	rport->supported_classes = FC_COS_UNSPECIFIED;
1517 	rport->dev_loss_tmo = fc_dev_loss_tmo;
1518 	memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
1519 	memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
1520 	rport->port_id = ids->port_id;
1521 	rport->roles = ids->roles;
1522 	rport->port_state = FC_PORTSTATE_ONLINE;
1523 	if (fci->f->dd_fcrport_size)
1524 		rport->dd_data = &rport[1];
1525 	rport->channel = channel;
1526 
1527 	INIT_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport, rport);
1528 	INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
1529 	INIT_WORK(&rport->stgt_delete_work, fc_starget_delete, rport);
1530 	INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete, rport);
1531 
1532 	spin_lock_irqsave(shost->host_lock, flags);
1533 
1534 	rport->number = fc_host->next_rport_number++;
1535 	if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1536 		rport->scsi_target_id = fc_host->next_target_id++;
1537 	else
1538 		rport->scsi_target_id = -1;
1539 	list_add_tail(&rport->peers, &fc_host->rports);
1540 	get_device(&shost->shost_gendev);
1541 
1542 	spin_unlock_irqrestore(shost->host_lock, flags);
1543 
1544 	dev = &rport->dev;
1545 	device_initialize(dev);
1546 	dev->parent = get_device(&shost->shost_gendev);
1547 	dev->release = fc_rport_dev_release;
1548 	sprintf(dev->bus_id, "rport-%d:%d-%d",
1549 		shost->host_no, channel, rport->number);
1550 	transport_setup_device(dev);
1551 
1552 	error = device_add(dev);
1553 	if (error) {
1554 		printk(KERN_ERR "FC Remote Port device_add failed\n");
1555 		goto delete_rport;
1556 	}
1557 	transport_add_device(dev);
1558 	transport_configure_device(dev);
1559 
1560 	if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
1561 		/* initiate a scan of the target */
1562 		rport->flags |= FC_RPORT_SCAN_PENDING;
1563 		scsi_queue_work(shost, &rport->scan_work);
1564 	}
1565 
1566 	return rport;
1567 
1568 delete_rport:
1569 	transport_destroy_device(dev);
1570 	put_device(dev->parent);
1571 	spin_lock_irqsave(shost->host_lock, flags);
1572 	list_del(&rport->peers);
1573 	put_device(&shost->shost_gendev);
1574 	spin_unlock_irqrestore(shost->host_lock, flags);
1575 	put_device(dev->parent);
1576 	kfree(rport);
1577 	return NULL;
1578 }
1579 
1580 /**
1581  * fc_remote_port_add - notifies the fc transport of the existence
1582  *		of a remote FC port.
1583  * @shost:	scsi host the remote port is connected to.
1584  * @channel:	Channel on shost port connected to.
1585  * @ids:	The world wide names, fc address, and FC4 port
1586  *		roles for the remote port.
1587  *
1588  * The LLDD calls this routine to notify the transport of the existence
1589  * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1590  * of the port, it's FC address (port_id), and the FC4 roles that are
1591  * active for the port.
1592  *
1593  * For ports that are FCP targets (aka scsi targets), the FC transport
1594  * maintains consistent target id bindings on behalf of the LLDD.
1595  * A consistent target id binding is an assignment of a target id to
1596  * a remote port identifier, which persists while the scsi host is
1597  * attached. The remote port can disappear, then later reappear, and
1598  * it's target id assignment remains the same. This allows for shifts
1599  * in FC addressing (if binding by wwpn or wwnn) with no apparent
1600  * changes to the scsi subsystem which is based on scsi host number and
1601  * target id values.  Bindings are only valid during the attachment of
1602  * the scsi host. If the host detaches, then later re-attaches, target
1603  * id bindings may change.
1604  *
1605  * This routine is responsible for returning a remote port structure.
1606  * The routine will search the list of remote ports it maintains
1607  * internally on behalf of consistent target id mappings. If found, the
1608  * remote port structure will be reused. Otherwise, a new remote port
1609  * structure will be allocated.
1610  *
1611  * Whenever a remote port is allocated, a new fc_remote_port class
1612  * device is created.
1613  *
1614  * Should not be called from interrupt context.
1615  *
1616  * Notes:
1617  *	This routine assumes no locks are held on entry.
1618  **/
1619 struct fc_rport *
1620 fc_remote_port_add(struct Scsi_Host *shost, int channel,
1621 	struct fc_rport_identifiers  *ids)
1622 {
1623 	struct fc_internal *fci = to_fc_internal(shost->transportt);
1624 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1625 	struct fc_rport *rport;
1626 	unsigned long flags;
1627 	int match = 0;
1628 
1629 	/* ensure any stgt delete functions are done */
1630 	fc_flush_work(shost);
1631 
1632 	/*
1633 	 * Search the list of "active" rports, for an rport that has been
1634 	 * deleted, but we've held off the real delete while the target
1635 	 * is in a "blocked" state.
1636 	 */
1637 	spin_lock_irqsave(shost->host_lock, flags);
1638 
1639 	list_for_each_entry(rport, &fc_host->rports, peers) {
1640 
1641 		if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
1642 			(rport->channel == channel)) {
1643 
1644 			switch (fc_host->tgtid_bind_type) {
1645 			case FC_TGTID_BIND_BY_WWPN:
1646 			case FC_TGTID_BIND_NONE:
1647 				if (rport->port_name == ids->port_name)
1648 					match = 1;
1649 				break;
1650 			case FC_TGTID_BIND_BY_WWNN:
1651 				if (rport->node_name == ids->node_name)
1652 					match = 1;
1653 				break;
1654 			case FC_TGTID_BIND_BY_ID:
1655 				if (rport->port_id == ids->port_id)
1656 					match = 1;
1657 				break;
1658 			}
1659 
1660 			if (match) {
1661 				struct work_struct *work =
1662 							&rport->dev_loss_work;
1663 
1664 				memcpy(&rport->node_name, &ids->node_name,
1665 					sizeof(rport->node_name));
1666 				memcpy(&rport->port_name, &ids->port_name,
1667 					sizeof(rport->port_name));
1668 				rport->port_id = ids->port_id;
1669 
1670 				rport->port_state = FC_PORTSTATE_ONLINE;
1671 				rport->roles = ids->roles;
1672 
1673 				spin_unlock_irqrestore(shost->host_lock, flags);
1674 
1675 				if (fci->f->dd_fcrport_size)
1676 					memset(rport->dd_data, 0,
1677 						fci->f->dd_fcrport_size);
1678 
1679 				/*
1680 				 * If we were blocked, we were a target.
1681 				 * If no longer a target, we leave the timer
1682 				 * running in case the port changes roles
1683 				 * prior to the timer expiring. If the timer
1684 				 * fires, the target will be torn down.
1685 				 */
1686 				if (!(ids->roles & FC_RPORT_ROLE_FCP_TARGET))
1687 					return rport;
1688 
1689 				/* restart the target */
1690 
1691 				/*
1692 				 * Stop the target timer first. Take no action
1693 				 * on the del_timer failure as the state
1694 				 * machine state change will validate the
1695 				 * transaction.
1696 				 */
1697 				if (!cancel_delayed_work(work))
1698 					fc_flush_devloss(shost);
1699 
1700 				spin_lock_irqsave(shost->host_lock, flags);
1701 
1702 				rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
1703 
1704 				/* initiate a scan of the target */
1705 				rport->flags |= FC_RPORT_SCAN_PENDING;
1706 				scsi_queue_work(shost, &rport->scan_work);
1707 
1708 				spin_unlock_irqrestore(shost->host_lock, flags);
1709 
1710 				return rport;
1711 			}
1712 		}
1713 	}
1714 
1715 	/* Search the bindings array */
1716 	if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
1717 
1718 		/* search for a matching consistent binding */
1719 
1720 		list_for_each_entry(rport, &fc_host->rport_bindings,
1721 					peers) {
1722 			if (rport->channel != channel)
1723 				continue;
1724 
1725 			switch (fc_host->tgtid_bind_type) {
1726 			case FC_TGTID_BIND_BY_WWPN:
1727 				if (rport->port_name == ids->port_name)
1728 					match = 1;
1729 				break;
1730 			case FC_TGTID_BIND_BY_WWNN:
1731 				if (rport->node_name == ids->node_name)
1732 					match = 1;
1733 				break;
1734 			case FC_TGTID_BIND_BY_ID:
1735 				if (rport->port_id == ids->port_id)
1736 					match = 1;
1737 				break;
1738 			case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1739 				break;
1740 			}
1741 
1742 			if (match) {
1743 				list_move_tail(&rport->peers, &fc_host->rports);
1744 				break;
1745 			}
1746 		}
1747 
1748 		if (match) {
1749 			memcpy(&rport->node_name, &ids->node_name,
1750 				sizeof(rport->node_name));
1751 			memcpy(&rport->port_name, &ids->port_name,
1752 				sizeof(rport->port_name));
1753 			rport->port_id = ids->port_id;
1754 			rport->roles = ids->roles;
1755 			rport->port_state = FC_PORTSTATE_ONLINE;
1756 
1757 			if (fci->f->dd_fcrport_size)
1758 				memset(rport->dd_data, 0,
1759 						fci->f->dd_fcrport_size);
1760 
1761 			if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
1762 				/* initiate a scan of the target */
1763 				rport->flags |= FC_RPORT_SCAN_PENDING;
1764 				scsi_queue_work(shost, &rport->scan_work);
1765 			}
1766 
1767 			spin_unlock_irqrestore(shost->host_lock, flags);
1768 
1769 			return rport;
1770 		}
1771 	}
1772 
1773 	spin_unlock_irqrestore(shost->host_lock, flags);
1774 
1775 	/* No consistent binding found - create new remote port entry */
1776 	rport = fc_rport_create(shost, channel, ids);
1777 
1778 	return rport;
1779 }
1780 EXPORT_SYMBOL(fc_remote_port_add);
1781 
1782 
1783 /**
1784  * fc_remote_port_delete - notifies the fc transport that a remote
1785  *		port is no longer in existence.
1786  * @rport:	The remote port that no longer exists
1787  *
1788  * The LLDD calls this routine to notify the transport that a remote
1789  * port is no longer part of the topology. Note: Although a port
1790  * may no longer be part of the topology, it may persist in the remote
1791  * ports displayed by the fc_host. We do this under 2 conditions:
1792  * - If the port was a scsi target, we delay its deletion by "blocking" it.
1793  *   This allows the port to temporarily disappear, then reappear without
1794  *   disrupting the SCSI device tree attached to it. During the "blocked"
1795  *   period the port will still exist.
1796  * - If the port was a scsi target and disappears for longer than we
1797  *   expect, we'll delete the port and the tear down the SCSI device tree
1798  *   attached to it. However, we want to semi-persist the target id assigned
1799  *   to that port if it eventually does exist. The port structure will
1800  *   remain (although with minimal information) so that the target id
1801  *   bindings remails.
1802  *
1803  * If the remote port is not an FCP Target, it will be fully torn down
1804  * and deallocated, including the fc_remote_port class device.
1805  *
1806  * If the remote port is an FCP Target, the port will be placed in a
1807  * temporary blocked state. From the LLDD's perspective, the rport no
1808  * longer exists. From the SCSI midlayer's perspective, the SCSI target
1809  * exists, but all sdevs on it are blocked from further I/O. The following
1810  * is then expected:
1811  *   If the remote port does not return (signaled by a LLDD call to
1812  *   fc_remote_port_add()) within the dev_loss_tmo timeout, then the
1813  *   scsi target is removed - killing all outstanding i/o and removing the
1814  *   scsi devices attached ot it. The port structure will be marked Not
1815  *   Present and be partially cleared, leaving only enough information to
1816  *   recognize the remote port relative to the scsi target id binding if
1817  *   it later appears.  The port will remain as long as there is a valid
1818  *   binding (e.g. until the user changes the binding type or unloads the
1819  *   scsi host with the binding).
1820  *
1821  *   If the remote port returns within the dev_loss_tmo value (and matches
1822  *   according to the target id binding type), the port structure will be
1823  *   reused. If it is no longer a SCSI target, the target will be torn
1824  *   down. If it continues to be a SCSI target, then the target will be
1825  *   unblocked (allowing i/o to be resumed), and a scan will be activated
1826  *   to ensure that all luns are detected.
1827  *
1828  * Called from normal process context only - cannot be called from interrupt.
1829  *
1830  * Notes:
1831  *	This routine assumes no locks are held on entry.
1832  **/
1833 void
1834 fc_remote_port_delete(struct fc_rport  *rport)
1835 {
1836 	struct Scsi_Host *shost = rport_to_shost(rport);
1837 	int timeout = rport->dev_loss_tmo;
1838 	unsigned long flags;
1839 
1840 	/*
1841 	 * No need to flush the fc_host work_q's, as all adds are synchronous.
1842 	 *
1843 	 * We do need to reclaim the rport scan work element, so eventually
1844 	 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
1845 	 * there's still a scan pending.
1846 	 */
1847 
1848 	spin_lock_irqsave(shost->host_lock, flags);
1849 
1850 	/* If no scsi target id mapping, delete it */
1851 	if (rport->scsi_target_id == -1) {
1852 		list_del(&rport->peers);
1853 		rport->port_state = FC_PORTSTATE_DELETED;
1854 		fc_queue_work(shost, &rport->rport_delete_work);
1855 		spin_unlock_irqrestore(shost->host_lock, flags);
1856 		return;
1857 	}
1858 
1859 	rport->port_state = FC_PORTSTATE_BLOCKED;
1860 
1861 	rport->flags |= FC_RPORT_DEVLOSS_PENDING;
1862 
1863 	spin_unlock_irqrestore(shost->host_lock, flags);
1864 
1865 	scsi_target_block(&rport->dev);
1866 
1867 	/* cap the length the devices can be blocked until they are deleted */
1868 	fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
1869 }
1870 EXPORT_SYMBOL(fc_remote_port_delete);
1871 
1872 /**
1873  * fc_remote_port_rolechg - notifies the fc transport that the roles
1874  *		on a remote may have changed.
1875  * @rport:	The remote port that changed.
1876  *
1877  * The LLDD calls this routine to notify the transport that the roles
1878  * on a remote port may have changed. The largest effect of this is
1879  * if a port now becomes a FCP Target, it must be allocated a
1880  * scsi target id.  If the port is no longer a FCP target, any
1881  * scsi target id value assigned to it will persist in case the
1882  * role changes back to include FCP Target. No changes in the scsi
1883  * midlayer will be invoked if the role changes (in the expectation
1884  * that the role will be resumed. If it doesn't normal error processing
1885  * will take place).
1886  *
1887  * Should not be called from interrupt context.
1888  *
1889  * Notes:
1890  *	This routine assumes no locks are held on entry.
1891  **/
1892 void
1893 fc_remote_port_rolechg(struct fc_rport  *rport, u32 roles)
1894 {
1895 	struct Scsi_Host *shost = rport_to_shost(rport);
1896 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1897 	unsigned long flags;
1898 	int create = 0;
1899 
1900 	spin_lock_irqsave(shost->host_lock, flags);
1901 	if (roles & FC_RPORT_ROLE_FCP_TARGET) {
1902 		if (rport->scsi_target_id == -1) {
1903 			rport->scsi_target_id = fc_host->next_target_id++;
1904 			create = 1;
1905 		} else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
1906 			create = 1;
1907 	}
1908 
1909 	rport->roles = roles;
1910 
1911 	spin_unlock_irqrestore(shost->host_lock, flags);
1912 
1913 	if (create) {
1914 		/*
1915 		 * There may have been a delete timer running on the
1916 		 * port. Ensure that it is cancelled as we now know
1917 		 * the port is an FCP Target.
1918 		 * Note: we know the rport is exists and in an online
1919 		 *  state as the LLDD would not have had an rport
1920 		 *  reference to pass us.
1921 		 *
1922 		 * Take no action on the del_timer failure as the state
1923 		 * machine state change will validate the
1924 		 * transaction.
1925 		 */
1926 		if (!cancel_delayed_work(&rport->dev_loss_work))
1927 			fc_flush_devloss(shost);
1928 
1929 		spin_lock_irqsave(shost->host_lock, flags);
1930 		rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
1931 		spin_unlock_irqrestore(shost->host_lock, flags);
1932 
1933 		/* ensure any stgt delete functions are done */
1934 		fc_flush_work(shost);
1935 
1936 		/* initiate a scan of the target */
1937 		spin_lock_irqsave(shost->host_lock, flags);
1938 		rport->flags |= FC_RPORT_SCAN_PENDING;
1939 		scsi_queue_work(shost, &rport->scan_work);
1940 		spin_unlock_irqrestore(shost->host_lock, flags);
1941 	}
1942 }
1943 EXPORT_SYMBOL(fc_remote_port_rolechg);
1944 
1945 /**
1946  * fc_timeout_deleted_rport - Timeout handler for a deleted remote port that
1947  *                       was a SCSI target (thus was blocked), and failed
1948  *                       to return in the alloted time.
1949  *
1950  * @data:	rport target that failed to reappear in the alloted time.
1951  **/
1952 static void
1953 fc_timeout_deleted_rport(void  *data)
1954 {
1955 	struct fc_rport *rport = (struct fc_rport *)data;
1956 	struct Scsi_Host *shost = rport_to_shost(rport);
1957 	struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1958 	unsigned long flags;
1959 
1960 	spin_lock_irqsave(shost->host_lock, flags);
1961 
1962 	rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
1963 
1964 	/*
1965 	 * If the port is ONLINE, then it came back. Validate it's still an
1966 	 * FCP target. If not, tear down the scsi_target on it.
1967 	 */
1968 	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
1969 	    !(rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
1970 		dev_printk(KERN_ERR, &rport->dev,
1971 			"blocked FC remote port time out: no longer"
1972 			" a FCP target, removing starget\n");
1973 		fc_queue_work(shost, &rport->stgt_delete_work);
1974 		spin_unlock_irqrestore(shost->host_lock, flags);
1975 		return;
1976 	}
1977 
1978 	if (rport->port_state != FC_PORTSTATE_BLOCKED) {
1979 		spin_unlock_irqrestore(shost->host_lock, flags);
1980 		dev_printk(KERN_ERR, &rport->dev,
1981 			"blocked FC remote port time out: leaving target alone\n");
1982 		return;
1983 	}
1984 
1985 	if (fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) {
1986 		list_del(&rport->peers);
1987 		rport->port_state = FC_PORTSTATE_DELETED;
1988 		dev_printk(KERN_ERR, &rport->dev,
1989 			"blocked FC remote port time out: removing target\n");
1990 		fc_queue_work(shost, &rport->rport_delete_work);
1991 		spin_unlock_irqrestore(shost->host_lock, flags);
1992 		return;
1993 	}
1994 
1995 	dev_printk(KERN_ERR, &rport->dev,
1996 		"blocked FC remote port time out: removing target and "
1997 		"saving binding\n");
1998 
1999 	list_move_tail(&rport->peers, &fc_host->rport_bindings);
2000 
2001 	/*
2002 	 * Note: We do not remove or clear the hostdata area. This allows
2003 	 *   host-specific target data to persist along with the
2004 	 *   scsi_target_id. It's up to the host to manage it's hostdata area.
2005 	 */
2006 
2007 	/*
2008 	 * Reinitialize port attributes that may change if the port comes back.
2009 	 */
2010 	rport->maxframe_size = -1;
2011 	rport->supported_classes = FC_COS_UNSPECIFIED;
2012 	rport->roles = FC_RPORT_ROLE_UNKNOWN;
2013 	rport->port_state = FC_PORTSTATE_NOTPRESENT;
2014 
2015 	/* remove the identifiers that aren't used in the consisting binding */
2016 	switch (fc_host->tgtid_bind_type) {
2017 	case FC_TGTID_BIND_BY_WWPN:
2018 		rport->node_name = -1;
2019 		rport->port_id = -1;
2020 		break;
2021 	case FC_TGTID_BIND_BY_WWNN:
2022 		rport->port_name = -1;
2023 		rport->port_id = -1;
2024 		break;
2025 	case FC_TGTID_BIND_BY_ID:
2026 		rport->node_name = -1;
2027 		rport->port_name = -1;
2028 		break;
2029 	case FC_TGTID_BIND_NONE:	/* to keep compiler happy */
2030 		break;
2031 	}
2032 
2033 	/*
2034 	 * As this only occurs if the remote port (scsi target)
2035 	 * went away and didn't come back - we'll remove
2036 	 * all attached scsi devices.
2037 	 */
2038 	fc_queue_work(shost, &rport->stgt_delete_work);
2039 
2040 	spin_unlock_irqrestore(shost->host_lock, flags);
2041 }
2042 
2043 /**
2044  * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
2045  *
2046  * Will unblock the target (in case it went away and has now come back),
2047  * then invoke a scan.
2048  *
2049  * @data:	remote port to be scanned.
2050  **/
2051 static void
2052 fc_scsi_scan_rport(void *data)
2053 {
2054 	struct fc_rport *rport = (struct fc_rport *)data;
2055 	struct Scsi_Host *shost = rport_to_shost(rport);
2056 	unsigned long flags;
2057 
2058 	if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
2059 	    (rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
2060 		scsi_target_unblock(&rport->dev);
2061 		scsi_scan_target(&rport->dev, rport->channel,
2062 			rport->scsi_target_id, SCAN_WILD_CARD, 1);
2063 	}
2064 
2065 	spin_lock_irqsave(shost->host_lock, flags);
2066 	rport->flags &= ~FC_RPORT_SCAN_PENDING;
2067 	spin_unlock_irqrestore(shost->host_lock, flags);
2068 }
2069 
2070 
2071 MODULE_AUTHOR("Martin Hicks");
2072 MODULE_DESCRIPTION("FC Transport Attributes");
2073 MODULE_LICENSE("GPL");
2074 
2075 module_init(fc_transport_init);
2076 module_exit(fc_transport_exit);
2077