[SCSI] fc transport: add fc_host system_hostname attribute and u64_to_wwn()
[deliverable/linux.git] / drivers / scsi / scsi_transport_fc.c
CommitLineData
1da177e4
LT
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>
4e57b681 29#include <linux/sched.h> /* workqueue stuff, HZ */
1da177e4
LT
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>
c829c394 34#include <scsi/scsi_cmnd.h>
1da177e4
LT
35#include "scsi_priv.h"
36
aedf3497
JS
37static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
38
1da177e4
LT
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) \
44struct 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) \
48static const char *get_fc_##title##_name(enum table_type table_key) \
49{ \
50 int i; \
51 char *name = NULL; \
52 \
6391a113 53 for (i = 0; i < ARRAY_SIZE(table); i++) { \
1da177e4
LT
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) \
63static int get_fc_##title##_match(const char *table_key, \
64 enum table_type *value) \
65{ \
66 int i; \
67 \
6391a113 68 for (i = 0; i < ARRAY_SIZE(table); i++) { \
1da177e4
LT
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 */
80static 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};
92fc_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 */
97static 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" },
42e33148 111 { FC_PORTSTATE_DELETED, "Deleted" },
1da177e4
LT
112};
113fc_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 */
0ad78200 118static const struct {
1da177e4
LT
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};
128fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
129 fc_tgtid_binding_type_names)
130fc_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) \
136static ssize_t \
137get_fc_##title##_names(u32 table_key, char *buf) \
138{ \
139 char *prefix = ""; \
140 ssize_t len = 0; \
141 int i; \
142 \
6391a113 143 for (i = 0; i < ARRAY_SIZE(table); i++) { \
1da177e4
LT
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 */
0ad78200 156static const struct {
1da177e4
LT
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};
166fc_bitfield_name_search(cos, fc_cos_names)
167
168
169/* Convert FC_PORTSPEED bit values to ascii string name */
0ad78200 170static const struct {
1da177e4
LT
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};
180fc_bitfield_name_search(port_speed, fc_port_speed_names)
181
182
183static int
184show_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 */
0ad78200 196static const struct {
1da177e4
LT
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};
204fc_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
19a7b4ae 218static void fc_timeout_deleted_rport(void *data);
1da177e4 219static void fc_scsi_scan_rport(void *data);
1da177e4
LT
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
ad139a2f 227#define FC_HOST_NUM_ATTRS 17
1da177e4
LT
228
229struct 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
d0a7e574
JB
257static int fc_target_setup(struct transport_container *tc, struct device *dev,
258 struct class_device *cdev)
1da177e4
LT
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
281static DECLARE_TRANSPORT_CLASS(fc_transport_class,
282 "fc_transport",
283 fc_target_setup,
284 NULL,
285 NULL);
286
d0a7e574
JB
287static int fc_host_setup(struct transport_container *tc, struct device *dev,
288 struct class_device *cdev)
1da177e4
LT
289{
290 struct Scsi_Host *shost = dev_to_shost(dev);
aedf3497 291 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1da177e4
LT
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 */
aedf3497
JS
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));
aedf3497
JS
304 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
305 fc_host->maxframe_size = -1;
306 memset(fc_host->serial_number, 0,
307 sizeof(fc_host->serial_number));
308
309 fc_host->port_id = -1;
310 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
311 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
312 memset(fc_host->active_fc4s, 0,
313 sizeof(fc_host->active_fc4s));
314 fc_host->speed = FC_PORTSPEED_UNKNOWN;
315 fc_host->fabric_name = -1;
b8d08210
JS
316 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
317 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
aedf3497
JS
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
1da177e4
LT
343 return 0;
344}
345
346static 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 */
356static 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 */
1c9e16e4 371static unsigned int fc_dev_loss_tmo = 60; /* seconds */
1da177e4
LT
372
373module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
374MODULE_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
381static __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
392static 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) \
404static ssize_t \
405show_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); \
19a7b4ae
JSEC
410 if ((i->f->get_rport_##field) && \
411 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
42e33148 412 (rport->port_state == FC_PORTSTATE_DELETED) || \
19a7b4ae 413 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
1da177e4
LT
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) \
419static ssize_t \
420store_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); \
19a7b4ae 427 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
42e33148 428 (rport->port_state == FC_PORTSTATE_DELETED) || \
19a7b4ae
JSEC
429 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
430 return -EBUSY; \
1da177e4
LT
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, ) \
438static 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)) \
443static 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) \
449static 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) \
455static ssize_t \
456show_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, ) \
464static 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)) \
469static 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) \
474static ssize_t \
475show_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} \
484static 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
518fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
519
520static ssize_t
521show_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}
528static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
529 show_fc_rport_supported_classes, NULL);
530
531/* Dynamic Remote Port Attributes */
532
19a7b4ae
JSEC
533/*
534 * dev_loss_tmo attribute
535 */
536fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
537static ssize_t
538store_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) ||
42e33148 546 (rport->port_state == FC_PORTSTATE_DELETED) ||
19a7b4ae
JSEC
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}
555static 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);
1da177e4
LT
557
558
559/* Private Remote Port Attributes */
560
561fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
562fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
563fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
564
565static ssize_t
566show_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}
594static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
595 show_fc_rport_roles, NULL);
596
597fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
598fc_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) \
613static ssize_t \
614show_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, ) \
630static 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)) \
635static 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: */
657fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
658fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
659fc_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) \
667static ssize_t \
668show_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) \
678static ssize_t \
679store_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
b8d08210
JS
691#define fc_host_store_str_function(field, slen) \
692static ssize_t \
693store_fc_host_##field(struct class_device *cdev, const char *buf, \
694 size_t count) \
695{ \
696 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
697 struct fc_internal *i = to_fc_internal(shost->transportt); \
698 unsigned int cnt=count; \
699 \
700 /* count may include a LF at end of string */ \
701 if (buf[cnt-1] == '\n') \
702 cnt--; \
703 if (cnt > ((slen) - 1)) \
704 return -EINVAL; \
705 memcpy(fc_host_##field(shost), buf, cnt); \
706 i->f->set_host_##field(shost); \
707 return count; \
708}
709
1da177e4
LT
710#define fc_host_rd_attr(field, format_string, sz) \
711 fc_host_show_function(field, format_string, sz, ) \
712static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
713 show_fc_host_##field, NULL)
714
715#define fc_host_rd_attr_cast(field, format_string, sz, cast) \
716 fc_host_show_function(field, format_string, sz, (cast)) \
717static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
718 show_fc_host_##field, NULL)
719
720#define fc_host_rw_attr(field, format_string, sz) \
721 fc_host_show_function(field, format_string, sz, ) \
722 fc_host_store_function(field) \
723static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
724 show_fc_host_##field, \
725 store_fc_host_##field)
726
727#define fc_host_rd_enum_attr(title, maxlen) \
728static ssize_t \
729show_fc_host_##title (struct class_device *cdev, char *buf) \
730{ \
731 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
732 struct fc_internal *i = to_fc_internal(shost->transportt); \
733 const char *name; \
734 if (i->f->get_host_##title) \
735 i->f->get_host_##title(shost); \
736 name = get_fc_##title##_name(fc_host_##title(shost)); \
737 if (!name) \
738 return -EINVAL; \
739 return snprintf(buf, maxlen, "%s\n", name); \
740} \
741static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
742
743#define SETUP_HOST_ATTRIBUTE_RD(field) \
744 i->private_host_attrs[count] = class_device_attr_host_##field; \
745 i->private_host_attrs[count].attr.mode = S_IRUGO; \
746 i->private_host_attrs[count].store = NULL; \
747 i->host_attrs[count] = &i->private_host_attrs[count]; \
748 if (i->f->show_host_##field) \
749 count++
750
751#define SETUP_HOST_ATTRIBUTE_RW(field) \
752 i->private_host_attrs[count] = class_device_attr_host_##field; \
753 if (!i->f->set_host_##field) { \
754 i->private_host_attrs[count].attr.mode = S_IRUGO; \
755 i->private_host_attrs[count].store = NULL; \
756 } \
757 i->host_attrs[count] = &i->private_host_attrs[count]; \
758 if (i->f->show_host_##field) \
759 count++
760
761
762#define fc_private_host_show_function(field, format_string, sz, cast) \
763static ssize_t \
764show_fc_host_##field (struct class_device *cdev, char *buf) \
765{ \
766 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
767 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
768}
769
770#define fc_private_host_rd_attr(field, format_string, sz) \
771 fc_private_host_show_function(field, format_string, sz, ) \
772static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
773 show_fc_host_##field, NULL)
774
775#define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
776 fc_private_host_show_function(field, format_string, sz, (cast)) \
777static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
778 show_fc_host_##field, NULL)
779
780#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
781 i->private_host_attrs[count] = class_device_attr_host_##field; \
782 i->private_host_attrs[count].attr.mode = S_IRUGO; \
783 i->private_host_attrs[count].store = NULL; \
784 i->host_attrs[count] = &i->private_host_attrs[count]; \
785 count++
786
787#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
91ca7b01 788{ \
1da177e4
LT
789 i->private_host_attrs[count] = class_device_attr_host_##field; \
790 i->host_attrs[count] = &i->private_host_attrs[count]; \
91ca7b01
AV
791 count++; \
792}
1da177e4
LT
793
794
795/* Fixed Host Attributes */
796
797static ssize_t
798show_fc_host_supported_classes (struct class_device *cdev, char *buf)
799{
800 struct Scsi_Host *shost = transport_class_to_shost(cdev);
801
802 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
803 return snprintf(buf, 20, "unspecified\n");
804
805 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
806}
807static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
808 show_fc_host_supported_classes, NULL);
809
810static ssize_t
811show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
812{
813 struct Scsi_Host *shost = transport_class_to_shost(cdev);
814 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
815}
816static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
817 show_fc_host_supported_fc4s, NULL);
818
819static ssize_t
820show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
821{
822 struct Scsi_Host *shost = transport_class_to_shost(cdev);
823
824 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
825 return snprintf(buf, 20, "unknown\n");
826
827 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
828}
829static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
830 show_fc_host_supported_speeds, NULL);
831
832
833fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
834fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
6b7281d0
AH
835fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
836 unsigned long long);
1da177e4
LT
837fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
838fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
839
840
841/* Dynamic Host Attributes */
842
843static ssize_t
844show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
845{
846 struct Scsi_Host *shost = transport_class_to_shost(cdev);
847 struct fc_internal *i = to_fc_internal(shost->transportt);
848
849 if (i->f->get_host_active_fc4s)
850 i->f->get_host_active_fc4s(shost);
851
852 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
853}
854static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
855 show_fc_host_active_fc4s, NULL);
856
857static ssize_t
858show_fc_host_speed (struct class_device *cdev, char *buf)
859{
860 struct Scsi_Host *shost = transport_class_to_shost(cdev);
861 struct fc_internal *i = to_fc_internal(shost->transportt);
862
863 if (i->f->get_host_speed)
864 i->f->get_host_speed(shost);
865
866 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
867 return snprintf(buf, 20, "unknown\n");
868
869 return get_fc_port_speed_names(fc_host_speed(shost), buf);
870}
871static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
872 show_fc_host_speed, NULL);
873
874
875fc_host_rd_attr(port_id, "0x%06x\n", 20);
876fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
877fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
878fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
016131b8 879fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1da177e4 880
b8d08210
JS
881fc_private_host_show_function(system_hostname, "%s\n",
882 FC_SYMBOLIC_NAME_SIZE + 1, )
883fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
884static FC_CLASS_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
885 show_fc_host_system_hostname, store_fc_host_system_hostname);
886
1da177e4
LT
887
888/* Private Host Attributes */
889
890static ssize_t
891show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
892{
893 struct Scsi_Host *shost = transport_class_to_shost(cdev);
894 const char *name;
895
896 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
897 if (!name)
898 return -EINVAL;
899 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
900}
901
d16794f6
JSEC
902#define get_list_head_entry(pos, head, member) \
903 pos = list_entry((head)->next, typeof(*pos), member)
904
1da177e4
LT
905static ssize_t
906store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
907 const char *buf, size_t count)
908{
909 struct Scsi_Host *shost = transport_class_to_shost(cdev);
d16794f6 910 struct fc_rport *rport;
1da177e4
LT
911 enum fc_tgtid_binding_type val;
912 unsigned long flags;
913
914 if (get_fc_tgtid_bind_type_match(buf, &val))
915 return -EINVAL;
916
917 /* if changing bind type, purge all unused consistent bindings */
918 if (val != fc_host_tgtid_bind_type(shost)) {
919 spin_lock_irqsave(shost->host_lock, flags);
d16794f6
JSEC
920 while (!list_empty(&fc_host_rport_bindings(shost))) {
921 get_list_head_entry(rport,
922 &fc_host_rport_bindings(shost), peers);
aedf3497
JS
923 list_del(&rport->peers);
924 rport->port_state = FC_PORTSTATE_DELETED;
925 fc_queue_work(shost, &rport->rport_delete_work);
d16794f6 926 }
1da177e4
LT
927 spin_unlock_irqrestore(shost->host_lock, flags);
928 }
929
930 fc_host_tgtid_bind_type(shost) = val;
931 return count;
932}
933
934static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
935 show_fc_private_host_tgtid_bind_type,
936 store_fc_private_host_tgtid_bind_type);
937
91ca7b01
AV
938static ssize_t
939store_fc_private_host_issue_lip(struct class_device *cdev,
940 const char *buf, size_t count)
941{
942 struct Scsi_Host *shost = transport_class_to_shost(cdev);
943 struct fc_internal *i = to_fc_internal(shost->transportt);
944 int ret;
945
946 /* ignore any data value written to the attribute */
947 if (i->f->issue_fc_host_lip) {
948 ret = i->f->issue_fc_host_lip(shost);
949 return ret ? ret: count;
950 }
951
952 return -ENOENT;
953}
954
955static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
956 store_fc_private_host_issue_lip);
957
1da177e4
LT
958/*
959 * Host Statistics Management
960 */
961
962/* Show a given an attribute in the statistics group */
963static ssize_t
964fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
965{
966 struct Scsi_Host *shost = transport_class_to_shost(cdev);
967 struct fc_internal *i = to_fc_internal(shost->transportt);
968 struct fc_host_statistics *stats;
969 ssize_t ret = -ENOENT;
970
971 if (offset > sizeof(struct fc_host_statistics) ||
972 offset % sizeof(u64) != 0)
973 WARN_ON(1);
974
975 if (i->f->get_fc_host_stats) {
976 stats = (i->f->get_fc_host_stats)(shost);
977 if (stats)
978 ret = snprintf(buf, 20, "0x%llx\n",
979 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
980 }
981 return ret;
982}
983
984
985/* generate a read-only statistics attribute */
986#define fc_host_statistic(name) \
987static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
988{ \
989 return fc_stat_show(cd, buf, \
990 offsetof(struct fc_host_statistics, name)); \
991} \
992static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
993
994fc_host_statistic(seconds_since_last_reset);
995fc_host_statistic(tx_frames);
996fc_host_statistic(tx_words);
997fc_host_statistic(rx_frames);
998fc_host_statistic(rx_words);
999fc_host_statistic(lip_count);
1000fc_host_statistic(nos_count);
1001fc_host_statistic(error_frames);
1002fc_host_statistic(dumped_frames);
1003fc_host_statistic(link_failure_count);
1004fc_host_statistic(loss_of_sync_count);
1005fc_host_statistic(loss_of_signal_count);
1006fc_host_statistic(prim_seq_protocol_err_count);
1007fc_host_statistic(invalid_tx_word_count);
1008fc_host_statistic(invalid_crc_count);
1009fc_host_statistic(fcp_input_requests);
1010fc_host_statistic(fcp_output_requests);
1011fc_host_statistic(fcp_control_requests);
1012fc_host_statistic(fcp_input_megabytes);
1013fc_host_statistic(fcp_output_megabytes);
1014
1015static ssize_t
1016fc_reset_statistics(struct class_device *cdev, const char *buf,
1017 size_t count)
1018{
1019 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1020 struct fc_internal *i = to_fc_internal(shost->transportt);
1021
1022 /* ignore any data value written to the attribute */
1023 if (i->f->reset_fc_host_stats) {
1024 i->f->reset_fc_host_stats(shost);
1025 return count;
1026 }
1027
1028 return -ENOENT;
1029}
1030static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1031 fc_reset_statistics);
1032
1033
1034static struct attribute *fc_statistics_attrs[] = {
1035 &class_device_attr_host_seconds_since_last_reset.attr,
1036 &class_device_attr_host_tx_frames.attr,
1037 &class_device_attr_host_tx_words.attr,
1038 &class_device_attr_host_rx_frames.attr,
1039 &class_device_attr_host_rx_words.attr,
1040 &class_device_attr_host_lip_count.attr,
1041 &class_device_attr_host_nos_count.attr,
1042 &class_device_attr_host_error_frames.attr,
1043 &class_device_attr_host_dumped_frames.attr,
1044 &class_device_attr_host_link_failure_count.attr,
1045 &class_device_attr_host_loss_of_sync_count.attr,
1046 &class_device_attr_host_loss_of_signal_count.attr,
1047 &class_device_attr_host_prim_seq_protocol_err_count.attr,
1048 &class_device_attr_host_invalid_tx_word_count.attr,
1049 &class_device_attr_host_invalid_crc_count.attr,
1050 &class_device_attr_host_fcp_input_requests.attr,
1051 &class_device_attr_host_fcp_output_requests.attr,
1052 &class_device_attr_host_fcp_control_requests.attr,
1053 &class_device_attr_host_fcp_input_megabytes.attr,
1054 &class_device_attr_host_fcp_output_megabytes.attr,
1055 &class_device_attr_host_reset_statistics.attr,
1056 NULL
1057};
1058
1059static struct attribute_group fc_statistics_group = {
1060 .name = "statistics",
1061 .attrs = fc_statistics_attrs,
1062};
1063
1064static int fc_host_match(struct attribute_container *cont,
1065 struct device *dev)
1066{
1067 struct Scsi_Host *shost;
1068 struct fc_internal *i;
1069
1070 if (!scsi_is_host_device(dev))
1071 return 0;
1072
1073 shost = dev_to_shost(dev);
1074 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1075 != &fc_host_class.class)
1076 return 0;
1077
1078 i = to_fc_internal(shost->transportt);
1079
1080 return &i->t.host_attrs.ac == cont;
1081}
1082
1083static int fc_target_match(struct attribute_container *cont,
1084 struct device *dev)
1085{
1086 struct Scsi_Host *shost;
1087 struct fc_internal *i;
1088
1089 if (!scsi_is_target_device(dev))
1090 return 0;
1091
1092 shost = dev_to_shost(dev->parent);
1093 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1094 != &fc_host_class.class)
1095 return 0;
1096
1097 i = to_fc_internal(shost->transportt);
1098
1099 return &i->t.target_attrs.ac == cont;
1100}
1101
1102static void fc_rport_dev_release(struct device *dev)
1103{
1104 struct fc_rport *rport = dev_to_rport(dev);
1105 put_device(dev->parent);
1106 kfree(rport);
1107}
1108
1109int scsi_is_fc_rport(const struct device *dev)
1110{
1111 return dev->release == fc_rport_dev_release;
1112}
1113EXPORT_SYMBOL(scsi_is_fc_rport);
1114
1115static int fc_rport_match(struct attribute_container *cont,
1116 struct device *dev)
1117{
1118 struct Scsi_Host *shost;
1119 struct fc_internal *i;
1120
1121 if (!scsi_is_fc_rport(dev))
1122 return 0;
1123
1124 shost = dev_to_shost(dev->parent);
1125 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1126 != &fc_host_class.class)
1127 return 0;
1128
1129 i = to_fc_internal(shost->transportt);
1130
1131 return &i->rport_attr_cont.ac == cont;
1132}
1133
5c44cd2a 1134
c829c394
JS
1135/**
1136 * fc_timed_out - FC Transport I/O timeout intercept handler
1137 *
1138 * @scmd: The SCSI command which timed out
1139 *
1140 * This routine protects against error handlers getting invoked while a
1141 * rport is in a blocked state, typically due to a temporarily loss of
1142 * connectivity. If the error handlers are allowed to proceed, requests
1143 * to abort i/o, reset the target, etc will likely fail as there is no way
1144 * to communicate with the device to perform the requested function. These
1145 * failures may result in the midlayer taking the device offline, requiring
1146 * manual intervention to restore operation.
1147 *
1148 * This routine, called whenever an i/o times out, validates the state of
1149 * the underlying rport. If the rport is blocked, it returns
1150 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1151 * Eventually, either the device will return, or devloss_tmo will fire,
1152 * and when the timeout then fires, it will be handled normally.
1153 * If the rport is not blocked, normal error handling continues.
1154 *
1155 * Notes:
1156 * This routine assumes no locks are held on entry.
1157 **/
1158static enum scsi_eh_timer_return
1159fc_timed_out(struct scsi_cmnd *scmd)
1160{
1161 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1162
1163 if (rport->port_state == FC_PORTSTATE_BLOCKED)
1164 return EH_RESET_TIMER;
1165
1166 return EH_NOT_HANDLED;
1167}
1168
5c44cd2a
JSEC
1169/*
1170 * Must be called with shost->host_lock held
1171 */
e02f3f59
CH
1172static int fc_user_scan(struct Scsi_Host *shost, uint channel,
1173 uint id, uint lun)
5c44cd2a
JSEC
1174{
1175 struct fc_rport *rport;
1176
e02f3f59
CH
1177 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1178 if (rport->scsi_target_id == -1)
1179 continue;
5c44cd2a 1180
e02f3f59
CH
1181 if ((channel == SCAN_WILD_CARD || channel == rport->channel) &&
1182 (id == SCAN_WILD_CARD || id == rport->scsi_target_id)) {
1183 scsi_scan_target(&rport->dev, rport->channel,
1184 rport->scsi_target_id, lun, 1);
1185 }
1186 }
1187
1188 return 0;
5c44cd2a
JSEC
1189}
1190
1da177e4
LT
1191struct scsi_transport_template *
1192fc_attach_transport(struct fc_function_template *ft)
1193{
1da177e4 1194 int count;
24669f75
JS
1195 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
1196 GFP_KERNEL);
1da177e4
LT
1197
1198 if (unlikely(!i))
1199 return NULL;
1200
1da177e4
LT
1201 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1202 i->t.target_attrs.ac.class = &fc_transport_class.class;
1203 i->t.target_attrs.ac.match = fc_target_match;
1204 i->t.target_size = sizeof(struct fc_starget_attrs);
1205 transport_container_register(&i->t.target_attrs);
1206
1207 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1208 i->t.host_attrs.ac.class = &fc_host_class.class;
1209 i->t.host_attrs.ac.match = fc_host_match;
1210 i->t.host_size = sizeof(struct fc_host_attrs);
1211 if (ft->get_fc_host_stats)
1212 i->t.host_attrs.statistics = &fc_statistics_group;
1213 transport_container_register(&i->t.host_attrs);
1214
1215 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1216 i->rport_attr_cont.ac.class = &fc_rport_class.class;
1217 i->rport_attr_cont.ac.match = fc_rport_match;
1218 transport_container_register(&i->rport_attr_cont);
1219
1220 i->f = ft;
1221
1222 /* Transport uses the shost workq for scsi scanning */
1223 i->t.create_work_queue = 1;
5c44cd2a 1224
c829c394
JS
1225 i->t.eh_timed_out = fc_timed_out;
1226
e02f3f59 1227 i->t.user_scan = fc_user_scan;
1da177e4
LT
1228
1229 /*
1230 * Setup SCSI Target Attributes.
1231 */
1232 count = 0;
1233 SETUP_STARGET_ATTRIBUTE_RD(node_name);
1234 SETUP_STARGET_ATTRIBUTE_RD(port_name);
1235 SETUP_STARGET_ATTRIBUTE_RD(port_id);
1236
1237 BUG_ON(count > FC_STARGET_NUM_ATTRS);
1238
1239 i->starget_attrs[count] = NULL;
1240
1241
1242 /*
1243 * Setup SCSI Host Attributes.
1244 */
1245 count=0;
1246 SETUP_HOST_ATTRIBUTE_RD(node_name);
1247 SETUP_HOST_ATTRIBUTE_RD(port_name);
6b7281d0 1248 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
1da177e4
LT
1249 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
1250 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1da177e4
LT
1251 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
1252 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
1253 SETUP_HOST_ATTRIBUTE_RD(serial_number);
1254
1255 SETUP_HOST_ATTRIBUTE_RD(port_id);
1256 SETUP_HOST_ATTRIBUTE_RD(port_type);
1257 SETUP_HOST_ATTRIBUTE_RD(port_state);
1258 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
1259 SETUP_HOST_ATTRIBUTE_RD(speed);
1260 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
016131b8 1261 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
b8d08210 1262 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
1da177e4
LT
1263
1264 /* Transport-managed attributes */
1265 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
91ca7b01
AV
1266 if (ft->issue_fc_host_lip)
1267 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
1da177e4
LT
1268
1269 BUG_ON(count > FC_HOST_NUM_ATTRS);
1270
1271 i->host_attrs[count] = NULL;
1272
1273 /*
1274 * Setup Remote Port Attributes.
1275 */
1276 count=0;
1277 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
1278 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
1279 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
1280 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
1281 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
1282 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
1283 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
1284 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
1285 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
1286
1287 BUG_ON(count > FC_RPORT_NUM_ATTRS);
1288
1289 i->rport_attrs[count] = NULL;
1290
1291 return &i->t;
1292}
1293EXPORT_SYMBOL(fc_attach_transport);
1294
1295void fc_release_transport(struct scsi_transport_template *t)
1296{
1297 struct fc_internal *i = to_fc_internal(t);
1298
1299 transport_container_unregister(&i->t.target_attrs);
1300 transport_container_unregister(&i->t.host_attrs);
1301 transport_container_unregister(&i->rport_attr_cont);
1302
1303 kfree(i);
1304}
1305EXPORT_SYMBOL(fc_release_transport);
1306
aedf3497
JS
1307/**
1308 * fc_queue_work - Queue work to the fc_host workqueue.
1309 * @shost: Pointer to Scsi_Host bound to fc_host.
1310 * @work: Work to queue for execution.
1311 *
1312 * Return value:
a0785edf
JS
1313 * 1 - work queued for execution
1314 * 0 - work is already queued
1315 * -EINVAL - work queue doesn't exist
aedf3497
JS
1316 **/
1317static int
1318fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
1319{
1320 if (unlikely(!fc_host_work_q(shost))) {
1321 printk(KERN_ERR
1322 "ERROR: FC host '%s' attempted to queue work, "
1323 "when no workqueue created.\n", shost->hostt->name);
1324 dump_stack();
1325
1326 return -EINVAL;
1327 }
1328
1329 return queue_work(fc_host_work_q(shost), work);
1330}
1331
1332/**
1333 * fc_flush_work - Flush a fc_host's workqueue.
1334 * @shost: Pointer to Scsi_Host bound to fc_host.
1335 **/
1336static void
1337fc_flush_work(struct Scsi_Host *shost)
1338{
1339 if (!fc_host_work_q(shost)) {
1340 printk(KERN_ERR
1341 "ERROR: FC host '%s' attempted to flush work, "
1342 "when no workqueue created.\n", shost->hostt->name);
1343 dump_stack();
1344 return;
1345 }
1346
1347 flush_workqueue(fc_host_work_q(shost));
1348}
1349
1350/**
1351 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
1352 * @shost: Pointer to Scsi_Host bound to fc_host.
1353 * @work: Work to queue for execution.
1354 * @delay: jiffies to delay the work queuing
1355 *
1356 * Return value:
1357 * 0 on success / != 0 for error
1358 **/
1359static int
1360fc_queue_devloss_work(struct Scsi_Host *shost, struct work_struct *work,
1361 unsigned long delay)
1362{
1363 if (unlikely(!fc_host_devloss_work_q(shost))) {
1364 printk(KERN_ERR
1365 "ERROR: FC host '%s' attempted to queue work, "
1366 "when no workqueue created.\n", shost->hostt->name);
1367 dump_stack();
1368
1369 return -EINVAL;
1370 }
1371
1372 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
1373}
1374
1375/**
1376 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
1377 * @shost: Pointer to Scsi_Host bound to fc_host.
1378 **/
1379static void
1380fc_flush_devloss(struct Scsi_Host *shost)
1381{
1382 if (!fc_host_devloss_work_q(shost)) {
1383 printk(KERN_ERR
1384 "ERROR: FC host '%s' attempted to flush work, "
1385 "when no workqueue created.\n", shost->hostt->name);
1386 dump_stack();
1387 return;
1388 }
1389
1390 flush_workqueue(fc_host_devloss_work_q(shost));
1391}
1392
1da177e4
LT
1393
1394/**
1395 * fc_remove_host - called to terminate any fc_transport-related elements
1396 * for a scsi host.
1397 * @rport: remote port to be unblocked.
1398 *
1399 * This routine is expected to be called immediately preceeding the
1400 * a driver's call to scsi_remove_host().
1401 *
1402 * WARNING: A driver utilizing the fc_transport, which fails to call
1403 * this routine prior to scsi_remote_host(), will leave dangling
1404 * objects in /sys/class/fc_remote_ports. Access to any of these
1405 * objects can result in a system crash !!!
1406 *
1407 * Notes:
1408 * This routine assumes no locks are held on entry.
1409 **/
1410void
1411fc_remove_host(struct Scsi_Host *shost)
1412{
1413 struct fc_rport *rport, *next_rport;
aedf3497
JS
1414 struct workqueue_struct *work_q;
1415 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1da177e4
LT
1416
1417 /* Remove any remote ports */
1418 list_for_each_entry_safe(rport, next_rport,
aedf3497
JS
1419 &fc_host->rports, peers) {
1420 list_del(&rport->peers);
1421 rport->port_state = FC_PORTSTATE_DELETED;
1422 fc_queue_work(shost, &rport->rport_delete_work);
1423 }
1424
1da177e4 1425 list_for_each_entry_safe(rport, next_rport,
aedf3497
JS
1426 &fc_host->rport_bindings, peers) {
1427 list_del(&rport->peers);
1428 rport->port_state = FC_PORTSTATE_DELETED;
1429 fc_queue_work(shost, &rport->rport_delete_work);
1430 }
1431
1432 /* flush all scan work items */
1433 scsi_flush_work(shost);
1434
1435 /* flush all stgt delete, and rport delete work items, then kill it */
1436 if (fc_host->work_q) {
1437 work_q = fc_host->work_q;
1438 fc_host->work_q = NULL;
1439 destroy_workqueue(work_q);
1440 }
1441
1442 /* flush all devloss work items, then kill it */
1443 if (fc_host->devloss_work_q) {
1444 work_q = fc_host->devloss_work_q;
1445 fc_host->devloss_work_q = NULL;
1446 destroy_workqueue(work_q);
1447 }
1da177e4
LT
1448}
1449EXPORT_SYMBOL(fc_remove_host);
1450
aedf3497
JS
1451
1452/**
1453 * fc_starget_delete - called to delete the scsi decendents of an rport
1454 * (target and all sdevs)
1455 *
1456 * @data: remote port to be operated on.
1457 **/
19a7b4ae 1458static void
aedf3497 1459fc_starget_delete(void *data)
19a7b4ae 1460{
aedf3497 1461 struct fc_rport *rport = (struct fc_rport *)data;
19a7b4ae 1462 struct Scsi_Host *shost = rport_to_shost(rport);
aedf3497 1463 unsigned long flags;
19a7b4ae 1464
aedf3497
JS
1465 spin_lock_irqsave(shost->host_lock, flags);
1466 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
1467 spin_unlock_irqrestore(shost->host_lock, flags);
1468 if (!cancel_delayed_work(&rport->dev_loss_work))
1469 fc_flush_devloss(shost);
1470 spin_lock_irqsave(shost->host_lock, flags);
1471 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
1472 }
1473 spin_unlock_irqrestore(shost->host_lock, flags);
19a7b4ae
JSEC
1474
1475 scsi_remove_target(&rport->dev);
1476}
1477
aedf3497
JS
1478
1479/**
1480 * fc_rport_final_delete - finish rport termination and delete it.
1481 *
1482 * @data: remote port to be deleted.
1483 **/
1484static void
1485fc_rport_final_delete(void *data)
1486{
1487 struct fc_rport *rport = (struct fc_rport *)data;
1488 struct device *dev = &rport->dev;
1489 struct Scsi_Host *shost = rport_to_shost(rport);
1490
1491 /* Delete SCSI target and sdevs */
1492 if (rport->scsi_target_id != -1)
1493 fc_starget_delete(data);
1494
1495 /*
1496 * if a scan is pending, flush the SCSI Host work_q so that
1497 * that we can reclaim the rport scan work element.
1498 */
1499 if (rport->flags & FC_RPORT_SCAN_PENDING)
1500 scsi_flush_work(shost);
1501
1502 transport_remove_device(dev);
1503 device_del(dev);
1504 transport_destroy_device(dev);
3bdad7bd
JS
1505 put_device(&shost->shost_gendev); /* for fc_host->rport list */
1506 put_device(dev); /* for self-reference */
aedf3497
JS
1507}
1508
1509
1da177e4
LT
1510/**
1511 * fc_rport_create - allocates and creates a remote FC port.
1512 * @shost: scsi host the remote port is connected to.
1513 * @channel: Channel on shost port connected to.
1514 * @ids: The world wide names, fc address, and FC4 port
1515 * roles for the remote port.
1516 *
1517 * Allocates and creates the remoter port structure, including the
1518 * class and sysfs creation.
1519 *
1520 * Notes:
1521 * This routine assumes no locks are held on entry.
1522 **/
1523struct fc_rport *
1524fc_rport_create(struct Scsi_Host *shost, int channel,
1525 struct fc_rport_identifiers *ids)
1526{
aedf3497 1527 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1da177e4
LT
1528 struct fc_internal *fci = to_fc_internal(shost->transportt);
1529 struct fc_rport *rport;
1530 struct device *dev;
1531 unsigned long flags;
1532 int error;
1533 size_t size;
1534
1535 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
24669f75 1536 rport = kzalloc(size, GFP_KERNEL);
1da177e4
LT
1537 if (unlikely(!rport)) {
1538 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
1539 return NULL;
1540 }
1da177e4
LT
1541
1542 rport->maxframe_size = -1;
1543 rport->supported_classes = FC_COS_UNSPECIFIED;
1544 rport->dev_loss_tmo = fc_dev_loss_tmo;
1545 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
1546 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
1547 rport->port_id = ids->port_id;
1548 rport->roles = ids->roles;
1549 rport->port_state = FC_PORTSTATE_ONLINE;
1550 if (fci->f->dd_fcrport_size)
1551 rport->dd_data = &rport[1];
1552 rport->channel = channel;
1553
19a7b4ae 1554 INIT_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport, rport);
1da177e4 1555 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
aedf3497
JS
1556 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete, rport);
1557 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete, rport);
1da177e4
LT
1558
1559 spin_lock_irqsave(shost->host_lock, flags);
1560
1561 rport->number = fc_host->next_rport_number++;
1562 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1563 rport->scsi_target_id = fc_host->next_target_id++;
1564 else
1565 rport->scsi_target_id = -1;
aedf3497 1566 list_add_tail(&rport->peers, &fc_host->rports);
3bdad7bd 1567 get_device(&shost->shost_gendev); /* for fc_host->rport list */
1da177e4
LT
1568
1569 spin_unlock_irqrestore(shost->host_lock, flags);
1570
1571 dev = &rport->dev;
3bdad7bd
JS
1572 device_initialize(dev); /* takes self reference */
1573 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
1da177e4
LT
1574 dev->release = fc_rport_dev_release;
1575 sprintf(dev->bus_id, "rport-%d:%d-%d",
1576 shost->host_no, channel, rport->number);
1577 transport_setup_device(dev);
1578
1579 error = device_add(dev);
1580 if (error) {
1581 printk(KERN_ERR "FC Remote Port device_add failed\n");
1582 goto delete_rport;
1583 }
1584 transport_add_device(dev);
1585 transport_configure_device(dev);
1586
aedf3497 1587 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
1da177e4 1588 /* initiate a scan of the target */
aedf3497 1589 rport->flags |= FC_RPORT_SCAN_PENDING;
1da177e4 1590 scsi_queue_work(shost, &rport->scan_work);
aedf3497 1591 }
1da177e4
LT
1592
1593 return rport;
1594
1595delete_rport:
1596 transport_destroy_device(dev);
1da177e4
LT
1597 spin_lock_irqsave(shost->host_lock, flags);
1598 list_del(&rport->peers);
3bdad7bd 1599 put_device(&shost->shost_gendev); /* for fc_host->rport list */
1da177e4
LT
1600 spin_unlock_irqrestore(shost->host_lock, flags);
1601 put_device(dev->parent);
1602 kfree(rport);
1603 return NULL;
1604}
1605
1606/**
1607 * fc_remote_port_add - notifies the fc transport of the existence
1608 * of a remote FC port.
1609 * @shost: scsi host the remote port is connected to.
1610 * @channel: Channel on shost port connected to.
1611 * @ids: The world wide names, fc address, and FC4 port
1612 * roles for the remote port.
1613 *
1614 * The LLDD calls this routine to notify the transport of the existence
1615 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1616 * of the port, it's FC address (port_id), and the FC4 roles that are
1617 * active for the port.
1618 *
1619 * For ports that are FCP targets (aka scsi targets), the FC transport
1620 * maintains consistent target id bindings on behalf of the LLDD.
1621 * A consistent target id binding is an assignment of a target id to
1622 * a remote port identifier, which persists while the scsi host is
1623 * attached. The remote port can disappear, then later reappear, and
1624 * it's target id assignment remains the same. This allows for shifts
1625 * in FC addressing (if binding by wwpn or wwnn) with no apparent
1626 * changes to the scsi subsystem which is based on scsi host number and
1627 * target id values. Bindings are only valid during the attachment of
1628 * the scsi host. If the host detaches, then later re-attaches, target
1629 * id bindings may change.
1630 *
1631 * This routine is responsible for returning a remote port structure.
1632 * The routine will search the list of remote ports it maintains
1633 * internally on behalf of consistent target id mappings. If found, the
1634 * remote port structure will be reused. Otherwise, a new remote port
1635 * structure will be allocated.
1636 *
1637 * Whenever a remote port is allocated, a new fc_remote_port class
1638 * device is created.
1639 *
1640 * Should not be called from interrupt context.
1641 *
1642 * Notes:
1643 * This routine assumes no locks are held on entry.
1644 **/
1645struct fc_rport *
1646fc_remote_port_add(struct Scsi_Host *shost, int channel,
1647 struct fc_rport_identifiers *ids)
1648{
19a7b4ae 1649 struct fc_internal *fci = to_fc_internal(shost->transportt);
aedf3497 1650 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1da177e4
LT
1651 struct fc_rport *rport;
1652 unsigned long flags;
1653 int match = 0;
1654
aedf3497
JS
1655 /* ensure any stgt delete functions are done */
1656 fc_flush_work(shost);
1657
19a7b4ae
JSEC
1658 /*
1659 * Search the list of "active" rports, for an rport that has been
1660 * deleted, but we've held off the real delete while the target
1661 * is in a "blocked" state.
1662 */
1663 spin_lock_irqsave(shost->host_lock, flags);
1664
aedf3497 1665 list_for_each_entry(rport, &fc_host->rports, peers) {
19a7b4ae
JSEC
1666
1667 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
1668 (rport->channel == channel)) {
1669
aedf3497 1670 switch (fc_host->tgtid_bind_type) {
19a7b4ae
JSEC
1671 case FC_TGTID_BIND_BY_WWPN:
1672 case FC_TGTID_BIND_NONE:
1673 if (rport->port_name == ids->port_name)
1674 match = 1;
1675 break;
1676 case FC_TGTID_BIND_BY_WWNN:
1677 if (rport->node_name == ids->node_name)
1678 match = 1;
1679 break;
1680 case FC_TGTID_BIND_BY_ID:
1681 if (rport->port_id == ids->port_id)
1682 match = 1;
1683 break;
1684 }
1685
1686 if (match) {
1687 struct work_struct *work =
1688 &rport->dev_loss_work;
1689
1690 memcpy(&rport->node_name, &ids->node_name,
1691 sizeof(rport->node_name));
1692 memcpy(&rport->port_name, &ids->port_name,
1693 sizeof(rport->port_name));
1694 rport->port_id = ids->port_id;
1695
1696 rport->port_state = FC_PORTSTATE_ONLINE;
1697 rport->roles = ids->roles;
1698
1699 spin_unlock_irqrestore(shost->host_lock, flags);
1700
1701 if (fci->f->dd_fcrport_size)
1702 memset(rport->dd_data, 0,
1703 fci->f->dd_fcrport_size);
1704
1705 /*
1706 * If we were blocked, we were a target.
1707 * If no longer a target, we leave the timer
1708 * running in case the port changes roles
1709 * prior to the timer expiring. If the timer
1710 * fires, the target will be torn down.
1711 */
1712 if (!(ids->roles & FC_RPORT_ROLE_FCP_TARGET))
1713 return rport;
1714
1715 /* restart the target */
1716
1717 /*
1718 * Stop the target timer first. Take no action
1719 * on the del_timer failure as the state
1720 * machine state change will validate the
1721 * transaction.
1722 */
1723 if (!cancel_delayed_work(work))
aedf3497
JS
1724 fc_flush_devloss(shost);
1725
1726 spin_lock_irqsave(shost->host_lock, flags);
1727
1728 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
19a7b4ae
JSEC
1729
1730 /* initiate a scan of the target */
aedf3497 1731 rport->flags |= FC_RPORT_SCAN_PENDING;
19a7b4ae
JSEC
1732 scsi_queue_work(shost, &rport->scan_work);
1733
aedf3497
JS
1734 spin_unlock_irqrestore(shost->host_lock, flags);
1735
a0785edf
JS
1736 scsi_target_unblock(&rport->dev);
1737
19a7b4ae
JSEC
1738 return rport;
1739 }
1740 }
1741 }
1742
1743 /* Search the bindings array */
aedf3497 1744 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
1da177e4
LT
1745
1746 /* search for a matching consistent binding */
1747
aedf3497 1748 list_for_each_entry(rport, &fc_host->rport_bindings,
1da177e4
LT
1749 peers) {
1750 if (rport->channel != channel)
1751 continue;
1752
aedf3497 1753 switch (fc_host->tgtid_bind_type) {
1da177e4
LT
1754 case FC_TGTID_BIND_BY_WWPN:
1755 if (rport->port_name == ids->port_name)
1756 match = 1;
1757 break;
1758 case FC_TGTID_BIND_BY_WWNN:
1759 if (rport->node_name == ids->node_name)
1760 match = 1;
1761 break;
1762 case FC_TGTID_BIND_BY_ID:
1763 if (rport->port_id == ids->port_id)
1764 match = 1;
1765 break;
1766 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1767 break;
1768 }
1769
1770 if (match) {
aedf3497 1771 list_move_tail(&rport->peers, &fc_host->rports);
1da177e4
LT
1772 break;
1773 }
1774 }
1775
1da177e4
LT
1776 if (match) {
1777 memcpy(&rport->node_name, &ids->node_name,
1778 sizeof(rport->node_name));
1779 memcpy(&rport->port_name, &ids->port_name,
1780 sizeof(rport->port_name));
1781 rport->port_id = ids->port_id;
1782 rport->roles = ids->roles;
1783 rport->port_state = FC_PORTSTATE_ONLINE;
1784
19a7b4ae
JSEC
1785 if (fci->f->dd_fcrport_size)
1786 memset(rport->dd_data, 0,
1787 fci->f->dd_fcrport_size);
1788
aedf3497 1789 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
1da177e4 1790 /* initiate a scan of the target */
aedf3497 1791 rport->flags |= FC_RPORT_SCAN_PENDING;
1da177e4 1792 scsi_queue_work(shost, &rport->scan_work);
a0785edf
JS
1793 spin_unlock_irqrestore(shost->host_lock, flags);
1794 scsi_target_unblock(&rport->dev);
1795 } else
1796 spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4
LT
1797
1798 return rport;
1799 }
1800 }
1801
19a7b4ae
JSEC
1802 spin_unlock_irqrestore(shost->host_lock, flags);
1803
1da177e4
LT
1804 /* No consistent binding found - create new remote port entry */
1805 rport = fc_rport_create(shost, channel, ids);
1806
1807 return rport;
1808}
1809EXPORT_SYMBOL(fc_remote_port_add);
1810
1da177e4
LT
1811
1812/**
1813 * fc_remote_port_delete - notifies the fc transport that a remote
1814 * port is no longer in existence.
1815 * @rport: The remote port that no longer exists
1816 *
1817 * The LLDD calls this routine to notify the transport that a remote
1818 * port is no longer part of the topology. Note: Although a port
1819 * may no longer be part of the topology, it may persist in the remote
19a7b4ae
JSEC
1820 * ports displayed by the fc_host. We do this under 2 conditions:
1821 * - If the port was a scsi target, we delay its deletion by "blocking" it.
1822 * This allows the port to temporarily disappear, then reappear without
1823 * disrupting the SCSI device tree attached to it. During the "blocked"
1824 * period the port will still exist.
1825 * - If the port was a scsi target and disappears for longer than we
1826 * expect, we'll delete the port and the tear down the SCSI device tree
1827 * attached to it. However, we want to semi-persist the target id assigned
1828 * to that port if it eventually does exist. The port structure will
1829 * remain (although with minimal information) so that the target id
1830 * bindings remails.
1da177e4
LT
1831 *
1832 * If the remote port is not an FCP Target, it will be fully torn down
1833 * and deallocated, including the fc_remote_port class device.
1834 *
19a7b4ae
JSEC
1835 * If the remote port is an FCP Target, the port will be placed in a
1836 * temporary blocked state. From the LLDD's perspective, the rport no
1837 * longer exists. From the SCSI midlayer's perspective, the SCSI target
1838 * exists, but all sdevs on it are blocked from further I/O. The following
1839 * is then expected:
1840 * If the remote port does not return (signaled by a LLDD call to
1841 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
1842 * scsi target is removed - killing all outstanding i/o and removing the
1843 * scsi devices attached ot it. The port structure will be marked Not
1844 * Present and be partially cleared, leaving only enough information to
1845 * recognize the remote port relative to the scsi target id binding if
1846 * it later appears. The port will remain as long as there is a valid
1847 * binding (e.g. until the user changes the binding type or unloads the
1848 * scsi host with the binding).
1da177e4 1849 *
19a7b4ae
JSEC
1850 * If the remote port returns within the dev_loss_tmo value (and matches
1851 * according to the target id binding type), the port structure will be
1852 * reused. If it is no longer a SCSI target, the target will be torn
1853 * down. If it continues to be a SCSI target, then the target will be
1854 * unblocked (allowing i/o to be resumed), and a scan will be activated
1855 * to ensure that all luns are detected.
1856 *
1857 * Called from normal process context only - cannot be called from interrupt.
1da177e4
LT
1858 *
1859 * Notes:
1860 * This routine assumes no locks are held on entry.
1861 **/
1862void
1863fc_remote_port_delete(struct fc_rport *rport)
1864{
aedf3497 1865 struct Scsi_Host *shost = rport_to_shost(rport);
19a7b4ae 1866 int timeout = rport->dev_loss_tmo;
aedf3497
JS
1867 unsigned long flags;
1868
1869 /*
1870 * No need to flush the fc_host work_q's, as all adds are synchronous.
1871 *
1872 * We do need to reclaim the rport scan work element, so eventually
1873 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
1874 * there's still a scan pending.
1875 */
1876
1877 spin_lock_irqsave(shost->host_lock, flags);
1da177e4 1878
19a7b4ae
JSEC
1879 /* If no scsi target id mapping, delete it */
1880 if (rport->scsi_target_id == -1) {
aedf3497
JS
1881 list_del(&rport->peers);
1882 rport->port_state = FC_PORTSTATE_DELETED;
1883 fc_queue_work(shost, &rport->rport_delete_work);
1884 spin_unlock_irqrestore(shost->host_lock, flags);
1da177e4
LT
1885 return;
1886 }
1887
aedf3497
JS
1888 rport->port_state = FC_PORTSTATE_BLOCKED;
1889
1890 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
1891
1892 spin_unlock_irqrestore(shost->host_lock, flags);
1893
19a7b4ae 1894 scsi_target_block(&rport->dev);
1da177e4 1895
19a7b4ae 1896 /* cap the length the devices can be blocked until they are deleted */
aedf3497 1897 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
1da177e4
LT
1898}
1899EXPORT_SYMBOL(fc_remote_port_delete);
1900
1901/**
1902 * fc_remote_port_rolechg - notifies the fc transport that the roles
1903 * on a remote may have changed.
1904 * @rport: The remote port that changed.
1905 *
1906 * The LLDD calls this routine to notify the transport that the roles
1907 * on a remote port may have changed. The largest effect of this is
1908 * if a port now becomes a FCP Target, it must be allocated a
1909 * scsi target id. If the port is no longer a FCP target, any
1910 * scsi target id value assigned to it will persist in case the
1911 * role changes back to include FCP Target. No changes in the scsi
1912 * midlayer will be invoked if the role changes (in the expectation
1913 * that the role will be resumed. If it doesn't normal error processing
1914 * will take place).
1915 *
1916 * Should not be called from interrupt context.
1917 *
1918 * Notes:
1919 * This routine assumes no locks are held on entry.
1920 **/
1921void
1922fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
1923{
1924 struct Scsi_Host *shost = rport_to_shost(rport);
aedf3497 1925 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1da177e4
LT
1926 unsigned long flags;
1927 int create = 0;
1928
1da177e4 1929 spin_lock_irqsave(shost->host_lock, flags);
19a7b4ae
JSEC
1930 if (roles & FC_RPORT_ROLE_FCP_TARGET) {
1931 if (rport->scsi_target_id == -1) {
1932 rport->scsi_target_id = fc_host->next_target_id++;
1933 create = 1;
1934 } else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
1935 create = 1;
1da177e4 1936 }
1da177e4 1937
19a7b4ae
JSEC
1938 rport->roles = roles;
1939
aedf3497
JS
1940 spin_unlock_irqrestore(shost->host_lock, flags);
1941
19a7b4ae
JSEC
1942 if (create) {
1943 /*
1944 * There may have been a delete timer running on the
1945 * port. Ensure that it is cancelled as we now know
1946 * the port is an FCP Target.
1947 * Note: we know the rport is exists and in an online
1948 * state as the LLDD would not have had an rport
1949 * reference to pass us.
1950 *
1951 * Take no action on the del_timer failure as the state
1952 * machine state change will validate the
1953 * transaction.
1954 */
1955 if (!cancel_delayed_work(&rport->dev_loss_work))
aedf3497
JS
1956 fc_flush_devloss(shost);
1957
1958 spin_lock_irqsave(shost->host_lock, flags);
1959 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
1960 spin_unlock_irqrestore(shost->host_lock, flags);
1961
1962 /* ensure any stgt delete functions are done */
1963 fc_flush_work(shost);
19a7b4ae 1964
1da177e4 1965 /* initiate a scan of the target */
aedf3497
JS
1966 spin_lock_irqsave(shost->host_lock, flags);
1967 rport->flags |= FC_RPORT_SCAN_PENDING;
1da177e4 1968 scsi_queue_work(shost, &rport->scan_work);
aedf3497 1969 spin_unlock_irqrestore(shost->host_lock, flags);
a0785edf 1970 scsi_target_unblock(&rport->dev);
19a7b4ae 1971 }
1da177e4
LT
1972}
1973EXPORT_SYMBOL(fc_remote_port_rolechg);
1974
1975/**
19a7b4ae
JSEC
1976 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port that
1977 * was a SCSI target (thus was blocked), and failed
1978 * to return in the alloted time.
1979 *
1980 * @data: rport target that failed to reappear in the alloted time.
1da177e4
LT
1981 **/
1982static void
19a7b4ae 1983fc_timeout_deleted_rport(void *data)
1da177e4
LT
1984{
1985 struct fc_rport *rport = (struct fc_rport *)data;
19a7b4ae 1986 struct Scsi_Host *shost = rport_to_shost(rport);
aedf3497 1987 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
19a7b4ae 1988 unsigned long flags;
1da177e4 1989
19a7b4ae 1990 spin_lock_irqsave(shost->host_lock, flags);
1da177e4 1991
aedf3497
JS
1992 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
1993
1da177e4 1994 /*
aedf3497
JS
1995 * If the port is ONLINE, then it came back. Validate it's still an
1996 * FCP target. If not, tear down the scsi_target on it.
1da177e4 1997 */
aedf3497
JS
1998 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
1999 !(rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
19a7b4ae 2000 dev_printk(KERN_ERR, &rport->dev,
aedf3497
JS
2001 "blocked FC remote port time out: no longer"
2002 " a FCP target, removing starget\n");
aedf3497 2003 spin_unlock_irqrestore(shost->host_lock, flags);
a0785edf
JS
2004 scsi_target_unblock(&rport->dev);
2005 fc_queue_work(shost, &rport->stgt_delete_work);
19a7b4ae
JSEC
2006 return;
2007 }
1da177e4 2008
19a7b4ae
JSEC
2009 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
2010 spin_unlock_irqrestore(shost->host_lock, flags);
2011 dev_printk(KERN_ERR, &rport->dev,
2012 "blocked FC remote port time out: leaving target alone\n");
2013 return;
2014 }
1da177e4 2015
aedf3497
JS
2016 if (fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) {
2017 list_del(&rport->peers);
2018 rport->port_state = FC_PORTSTATE_DELETED;
19a7b4ae
JSEC
2019 dev_printk(KERN_ERR, &rport->dev,
2020 "blocked FC remote port time out: removing target\n");
aedf3497
JS
2021 fc_queue_work(shost, &rport->rport_delete_work);
2022 spin_unlock_irqrestore(shost->host_lock, flags);
19a7b4ae
JSEC
2023 return;
2024 }
1da177e4 2025
19a7b4ae
JSEC
2026 dev_printk(KERN_ERR, &rport->dev,
2027 "blocked FC remote port time out: removing target and "
2028 "saving binding\n");
2029
aedf3497 2030 list_move_tail(&rport->peers, &fc_host->rport_bindings);
1da177e4
LT
2031
2032 /*
19a7b4ae
JSEC
2033 * Note: We do not remove or clear the hostdata area. This allows
2034 * host-specific target data to persist along with the
2035 * scsi_target_id. It's up to the host to manage it's hostdata area.
1da177e4 2036 */
1da177e4 2037
19a7b4ae
JSEC
2038 /*
2039 * Reinitialize port attributes that may change if the port comes back.
2040 */
2041 rport->maxframe_size = -1;
2042 rport->supported_classes = FC_COS_UNSPECIFIED;
2043 rport->roles = FC_RPORT_ROLE_UNKNOWN;
aedf3497 2044 rport->port_state = FC_PORTSTATE_NOTPRESENT;
1da177e4 2045
19a7b4ae 2046 /* remove the identifiers that aren't used in the consisting binding */
aedf3497 2047 switch (fc_host->tgtid_bind_type) {
19a7b4ae
JSEC
2048 case FC_TGTID_BIND_BY_WWPN:
2049 rport->node_name = -1;
2050 rport->port_id = -1;
2051 break;
2052 case FC_TGTID_BIND_BY_WWNN:
2053 rport->port_name = -1;
2054 rport->port_id = -1;
2055 break;
2056 case FC_TGTID_BIND_BY_ID:
2057 rport->node_name = -1;
2058 rport->port_name = -1;
2059 break;
2060 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2061 break;
2062 }
2063
19a7b4ae
JSEC
2064 /*
2065 * As this only occurs if the remote port (scsi target)
2066 * went away and didn't come back - we'll remove
2067 * all attached scsi devices.
2068 */
42e33148 2069 spin_unlock_irqrestore(shost->host_lock, flags);
a0785edf
JS
2070
2071 scsi_target_unblock(&rport->dev);
2072 fc_queue_work(shost, &rport->stgt_delete_work);
1da177e4 2073}
1da177e4
LT
2074
2075/**
2076 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
19a7b4ae 2077 *
1da177e4
LT
2078 * @data: remote port to be scanned.
2079 **/
2080static void
2081fc_scsi_scan_rport(void *data)
2082{
2083 struct fc_rport *rport = (struct fc_rport *)data;
aedf3497 2084 struct Scsi_Host *shost = rport_to_shost(rport);
42e33148
JSEC
2085 unsigned long flags;
2086
aedf3497
JS
2087 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
2088 (rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
aedf3497
JS
2089 scsi_scan_target(&rport->dev, rport->channel,
2090 rport->scsi_target_id, SCAN_WILD_CARD, 1);
42e33148 2091 }
aedf3497
JS
2092
2093 spin_lock_irqsave(shost->host_lock, flags);
2094 rport->flags &= ~FC_RPORT_SCAN_PENDING;
42e33148
JSEC
2095 spin_unlock_irqrestore(shost->host_lock, flags);
2096}
2097
2098
1da177e4
LT
2099MODULE_AUTHOR("Martin Hicks");
2100MODULE_DESCRIPTION("FC Transport Attributes");
2101MODULE_LICENSE("GPL");
2102
2103module_init(fc_transport_init);
2104module_exit(fc_transport_exit);
This page took 0.234209 seconds and 5 git commands to generate.