blob: b856e140e65f9a1ba441fa2d6ef4f9e1b3a3aa6c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080029#include <linux/sched.h> /* workqueue stuff, HZ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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_priv.h"
35
36#define FC_PRINTK(x, l, f, a...) printk(l "scsi(%d:%d:%d:%d): " f, (x)->host->host_no, (x)->channel, (x)->id, (x)->lun , ##a)
37
38/*
39 * Redefine so that we can have same named attributes in the
40 * sdev/starget/host objects.
41 */
42#define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
43struct class_device_attribute class_device_attr_##_prefix##_##_name = \
44 __ATTR(_name,_mode,_show,_store)
45
46#define fc_enum_name_search(title, table_type, table) \
47static const char *get_fc_##title##_name(enum table_type table_key) \
48{ \
49 int i; \
50 char *name = NULL; \
51 \
52 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
53 if (table[i].value == table_key) { \
54 name = table[i].name; \
55 break; \
56 } \
57 } \
58 return name; \
59}
60
61#define fc_enum_name_match(title, table_type, table) \
62static int get_fc_##title##_match(const char *table_key, \
63 enum table_type *value) \
64{ \
65 int i; \
66 \
67 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
68 if (strncmp(table_key, table[i].name, \
69 table[i].matchlen) == 0) { \
70 *value = table[i].value; \
71 return 0; /* success */ \
72 } \
73 } \
74 return 1; /* failure */ \
75}
76
77
78/* Convert fc_port_type values to ascii string name */
79static struct {
80 enum fc_port_type value;
81 char *name;
82} fc_port_type_names[] = {
83 { FC_PORTTYPE_UNKNOWN, "Unknown" },
84 { FC_PORTTYPE_OTHER, "Other" },
85 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
86 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
87 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
88 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
89 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
90};
91fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
92#define FC_PORTTYPE_MAX_NAMELEN 50
93
94
95/* Convert fc_port_state values to ascii string name */
96static struct {
97 enum fc_port_state value;
98 char *name;
99} fc_port_state_names[] = {
100 { FC_PORTSTATE_UNKNOWN, "Unknown" },
101 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
102 { FC_PORTSTATE_ONLINE, "Online" },
103 { FC_PORTSTATE_OFFLINE, "Offline" },
104 { FC_PORTSTATE_BLOCKED, "Blocked" },
105 { FC_PORTSTATE_BYPASSED, "Bypassed" },
106 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
107 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
108 { FC_PORTSTATE_ERROR, "Error" },
109 { FC_PORTSTATE_LOOPBACK, "Loopback" },
110};
111fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
112#define FC_PORTSTATE_MAX_NAMELEN 20
113
114
115/* Convert fc_tgtid_binding_type values to ascii string name */
116static struct {
117 enum fc_tgtid_binding_type value;
118 char *name;
119 int matchlen;
120} fc_tgtid_binding_type_names[] = {
121 { FC_TGTID_BIND_NONE, "none", 4 },
122 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
123 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
124 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
125};
126fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
127 fc_tgtid_binding_type_names)
128fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
129 fc_tgtid_binding_type_names)
130#define FC_BINDTYPE_MAX_NAMELEN 30
131
132
133#define fc_bitfield_name_search(title, table) \
134static ssize_t \
135get_fc_##title##_names(u32 table_key, char *buf) \
136{ \
137 char *prefix = ""; \
138 ssize_t len = 0; \
139 int i; \
140 \
141 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
142 if (table[i].value & table_key) { \
143 len += sprintf(buf + len, "%s%s", \
144 prefix, table[i].name); \
145 prefix = ", "; \
146 } \
147 } \
148 len += sprintf(buf + len, "\n"); \
149 return len; \
150}
151
152
153/* Convert FC_COS bit values to ascii string name */
154static struct {
155 u32 value;
156 char *name;
157} fc_cos_names[] = {
158 { FC_COS_CLASS1, "Class 1" },
159 { FC_COS_CLASS2, "Class 2" },
160 { FC_COS_CLASS3, "Class 3" },
161 { FC_COS_CLASS4, "Class 4" },
162 { FC_COS_CLASS6, "Class 6" },
163};
164fc_bitfield_name_search(cos, fc_cos_names)
165
166
167/* Convert FC_PORTSPEED bit values to ascii string name */
168static struct {
169 u32 value;
170 char *name;
171} fc_port_speed_names[] = {
172 { FC_PORTSPEED_1GBIT, "1 Gbit" },
173 { FC_PORTSPEED_2GBIT, "2 Gbit" },
174 { FC_PORTSPEED_4GBIT, "4 Gbit" },
175 { FC_PORTSPEED_10GBIT, "10 Gbit" },
176 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
177};
178fc_bitfield_name_search(port_speed, fc_port_speed_names)
179
180
181static int
182show_fc_fc4s (char *buf, u8 *fc4_list)
183{
184 int i, len=0;
185
186 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
187 len += sprintf(buf + len , "0x%02x ", *fc4_list);
188 len += sprintf(buf + len, "\n");
189 return len;
190}
191
192
193/* Convert FC_RPORT_ROLE bit values to ascii string name */
194static struct {
195 u32 value;
196 char *name;
197} fc_remote_port_role_names[] = {
198 { FC_RPORT_ROLE_FCP_TARGET, "FCP Target" },
199 { FC_RPORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
200 { FC_RPORT_ROLE_IP_PORT, "IP Port" },
201};
202fc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
203
204/*
205 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
206 */
207#define FC_WELLKNOWN_PORTID_MASK 0xfffff0
208#define FC_WELLKNOWN_ROLE_MASK 0x00000f
209#define FC_FPORT_PORTID 0x00000e
210#define FC_FABCTLR_PORTID 0x00000d
211#define FC_DIRSRVR_PORTID 0x00000c
212#define FC_TIMESRVR_PORTID 0x00000b
213#define FC_MGMTSRVR_PORTID 0x00000a
214
215
216static void fc_timeout_blocked_rport(void *data);
217static void fc_scsi_scan_rport(void *data);
218static void fc_rport_terminate(struct fc_rport *rport);
219
220/*
221 * Attribute counts pre object type...
222 * Increase these values if you add attributes
223 */
224#define FC_STARGET_NUM_ATTRS 3
225#define FC_RPORT_NUM_ATTRS 9
226#define FC_HOST_NUM_ATTRS 15
227
228struct fc_internal {
229 struct scsi_transport_template t;
230 struct fc_function_template *f;
231
232 /*
233 * For attributes : each object has :
234 * An array of the actual attributes structures
235 * An array of null-terminated pointers to the attribute
236 * structures - used for mid-layer interaction.
237 *
238 * The attribute containers for the starget and host are are
239 * part of the midlayer. As the remote port is specific to the
240 * fc transport, we must provide the attribute container.
241 */
242 struct class_device_attribute private_starget_attrs[
243 FC_STARGET_NUM_ATTRS];
244 struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
245
246 struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
247 struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
248
249 struct transport_container rport_attr_cont;
250 struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
251 struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
252};
253
254#define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
255
James Bottomleyd0a7e572005-08-14 17:09:01 -0500256static int fc_target_setup(struct transport_container *tc, struct device *dev,
257 struct class_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct scsi_target *starget = to_scsi_target(dev);
260 struct fc_rport *rport = starget_to_rport(starget);
261
262 /*
263 * if parent is remote port, use values from remote port.
264 * Otherwise, this host uses the fc_transport, but not the
265 * remote port interface. As such, initialize to known non-values.
266 */
267 if (rport) {
268 fc_starget_node_name(starget) = rport->node_name;
269 fc_starget_port_name(starget) = rport->port_name;
270 fc_starget_port_id(starget) = rport->port_id;
271 } else {
272 fc_starget_node_name(starget) = -1;
273 fc_starget_port_name(starget) = -1;
274 fc_starget_port_id(starget) = -1;
275 }
276
277 return 0;
278}
279
280static DECLARE_TRANSPORT_CLASS(fc_transport_class,
281 "fc_transport",
282 fc_target_setup,
283 NULL,
284 NULL);
285
James Bottomleyd0a7e572005-08-14 17:09:01 -0500286static int fc_host_setup(struct transport_container *tc, struct device *dev,
287 struct class_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct Scsi_Host *shost = dev_to_shost(dev);
290
291 /*
292 * Set default values easily detected by the midlayer as
293 * failure cases. The scsi lldd is responsible for initializing
294 * all transport attributes to valid values per host.
295 */
296 fc_host_node_name(shost) = -1;
297 fc_host_port_name(shost) = -1;
298 fc_host_supported_classes(shost) = FC_COS_UNSPECIFIED;
299 memset(fc_host_supported_fc4s(shost), 0,
300 sizeof(fc_host_supported_fc4s(shost)));
301 memset(fc_host_symbolic_name(shost), 0,
302 sizeof(fc_host_symbolic_name(shost)));
303 fc_host_supported_speeds(shost) = FC_PORTSPEED_UNKNOWN;
304 fc_host_maxframe_size(shost) = -1;
305 memset(fc_host_serial_number(shost), 0,
306 sizeof(fc_host_serial_number(shost)));
307
308 fc_host_port_id(shost) = -1;
309 fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
310 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
311 memset(fc_host_active_fc4s(shost), 0,
312 sizeof(fc_host_active_fc4s(shost)));
313 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
314 fc_host_fabric_name(shost) = -1;
315
316 fc_host_tgtid_bind_type(shost) = FC_TGTID_BIND_BY_WWPN;
317
318 INIT_LIST_HEAD(&fc_host_rports(shost));
319 INIT_LIST_HEAD(&fc_host_rport_bindings(shost));
320 fc_host_next_rport_number(shost) = 0;
321 fc_host_next_target_id(shost) = 0;
322
323 return 0;
324}
325
326static DECLARE_TRANSPORT_CLASS(fc_host_class,
327 "fc_host",
328 fc_host_setup,
329 NULL,
330 NULL);
331
332/*
333 * Setup and Remove actions for remote ports are handled
334 * in the service functions below.
335 */
336static DECLARE_TRANSPORT_CLASS(fc_rport_class,
337 "fc_remote_ports",
338 NULL,
339 NULL,
340 NULL);
341
342/*
343 * Module Parameters
344 */
345
346/*
347 * dev_loss_tmo: the default number of seconds that the FC transport
348 * should insulate the loss of a remote port.
349 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
350 */
351static unsigned int fc_dev_loss_tmo = SCSI_DEVICE_BLOCK_MAX_TIMEOUT;
352
353module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
354MODULE_PARM_DESC(dev_loss_tmo,
355 "Maximum number of seconds that the FC transport should"
356 " insulate the loss of a remote port. Once this value is"
357 " exceeded, the scsi target is removed. Value should be"
358 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
359
360
361static __init int fc_transport_init(void)
362{
363 int error = transport_class_register(&fc_host_class);
364 if (error)
365 return error;
366 error = transport_class_register(&fc_rport_class);
367 if (error)
368 return error;
369 return transport_class_register(&fc_transport_class);
370}
371
372static void __exit fc_transport_exit(void)
373{
374 transport_class_unregister(&fc_transport_class);
375 transport_class_unregister(&fc_rport_class);
376 transport_class_unregister(&fc_host_class);
377}
378
379/*
380 * FC Remote Port Attribute Management
381 */
382
383#define fc_rport_show_function(field, format_string, sz, cast) \
384static ssize_t \
385show_fc_rport_##field (struct class_device *cdev, char *buf) \
386{ \
387 struct fc_rport *rport = transport_class_to_rport(cdev); \
388 struct Scsi_Host *shost = rport_to_shost(rport); \
389 struct fc_internal *i = to_fc_internal(shost->transportt); \
390 if (i->f->get_rport_##field) \
391 i->f->get_rport_##field(rport); \
392 return snprintf(buf, sz, format_string, cast rport->field); \
393}
394
395#define fc_rport_store_function(field) \
396static ssize_t \
397store_fc_rport_##field(struct class_device *cdev, const char *buf, \
398 size_t count) \
399{ \
400 int val; \
401 struct fc_rport *rport = transport_class_to_rport(cdev); \
402 struct Scsi_Host *shost = rport_to_shost(rport); \
403 struct fc_internal *i = to_fc_internal(shost->transportt); \
404 val = simple_strtoul(buf, NULL, 0); \
405 i->f->set_rport_##field(rport, val); \
406 return count; \
407}
408
409#define fc_rport_rd_attr(field, format_string, sz) \
410 fc_rport_show_function(field, format_string, sz, ) \
411static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
412 show_fc_rport_##field, NULL)
413
414#define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
415 fc_rport_show_function(field, format_string, sz, (cast)) \
416static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
417 show_fc_rport_##field, NULL)
418
419#define fc_rport_rw_attr(field, format_string, sz) \
420 fc_rport_show_function(field, format_string, sz, ) \
421 fc_rport_store_function(field) \
422static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
423 show_fc_rport_##field, \
424 store_fc_rport_##field)
425
426
427#define fc_private_rport_show_function(field, format_string, sz, cast) \
428static ssize_t \
429show_fc_rport_##field (struct class_device *cdev, char *buf) \
430{ \
431 struct fc_rport *rport = transport_class_to_rport(cdev); \
432 return snprintf(buf, sz, format_string, cast rport->field); \
433}
434
435#define fc_private_rport_rd_attr(field, format_string, sz) \
436 fc_private_rport_show_function(field, format_string, sz, ) \
437static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
438 show_fc_rport_##field, NULL)
439
440#define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
441 fc_private_rport_show_function(field, format_string, sz, (cast)) \
442static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
443 show_fc_rport_##field, NULL)
444
445
446#define fc_private_rport_rd_enum_attr(title, maxlen) \
447static ssize_t \
448show_fc_rport_##title (struct class_device *cdev, char *buf) \
449{ \
450 struct fc_rport *rport = transport_class_to_rport(cdev); \
451 const char *name; \
452 name = get_fc_##title##_name(rport->title); \
453 if (!name) \
454 return -EINVAL; \
455 return snprintf(buf, maxlen, "%s\n", name); \
456} \
457static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
458 show_fc_rport_##title, NULL)
459
460
461#define SETUP_RPORT_ATTRIBUTE_RD(field) \
462 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
463 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
464 i->private_rport_attrs[count].store = NULL; \
465 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
466 if (i->f->show_rport_##field) \
467 count++
468
469#define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
470 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
471 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
472 i->private_rport_attrs[count].store = NULL; \
473 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
474 count++
475
476#define SETUP_RPORT_ATTRIBUTE_RW(field) \
477 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
478 if (!i->f->set_rport_##field) { \
479 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
480 i->private_rport_attrs[count].store = NULL; \
481 } \
482 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
483 if (i->f->show_rport_##field) \
484 count++
485
486
487/* The FC Transport Remote Port Attributes: */
488
489/* Fixed Remote Port Attributes */
490
491fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
492
493static ssize_t
494show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
495{
496 struct fc_rport *rport = transport_class_to_rport(cdev);
497 if (rport->supported_classes == FC_COS_UNSPECIFIED)
498 return snprintf(buf, 20, "unspecified\n");
499 return get_fc_cos_names(rport->supported_classes, buf);
500}
501static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
502 show_fc_rport_supported_classes, NULL);
503
504/* Dynamic Remote Port Attributes */
505
506fc_rport_rw_attr(dev_loss_tmo, "%d\n", 20);
507
508
509/* Private Remote Port Attributes */
510
511fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
512fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
513fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
514
515static ssize_t
516show_fc_rport_roles (struct class_device *cdev, char *buf)
517{
518 struct fc_rport *rport = transport_class_to_rport(cdev);
519
520 /* identify any roles that are port_id specific */
521 if ((rport->port_id != -1) &&
522 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
523 FC_WELLKNOWN_PORTID_MASK) {
524 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
525 case FC_FPORT_PORTID:
526 return snprintf(buf, 30, "Fabric Port\n");
527 case FC_FABCTLR_PORTID:
528 return snprintf(buf, 30, "Fabric Controller\n");
529 case FC_DIRSRVR_PORTID:
530 return snprintf(buf, 30, "Directory Server\n");
531 case FC_TIMESRVR_PORTID:
532 return snprintf(buf, 30, "Time Server\n");
533 case FC_MGMTSRVR_PORTID:
534 return snprintf(buf, 30, "Management Server\n");
535 default:
536 return snprintf(buf, 30, "Unknown Fabric Entity\n");
537 }
538 } else {
539 if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
540 return snprintf(buf, 20, "unknown\n");
541 return get_fc_remote_port_roles_names(rport->roles, buf);
542 }
543}
544static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
545 show_fc_rport_roles, NULL);
546
547fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
548fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
549
550
551
552/*
553 * FC SCSI Target Attribute Management
554 */
555
556/*
557 * Note: in the target show function we recognize when the remote
558 * port is in the heirarchy and do not allow the driver to get
559 * involved in sysfs functions. The driver only gets involved if
560 * it's the "old" style that doesn't use rports.
561 */
562#define fc_starget_show_function(field, format_string, sz, cast) \
563static ssize_t \
564show_fc_starget_##field (struct class_device *cdev, char *buf) \
565{ \
566 struct scsi_target *starget = transport_class_to_starget(cdev); \
567 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
568 struct fc_internal *i = to_fc_internal(shost->transportt); \
569 struct fc_rport *rport = starget_to_rport(starget); \
570 if (rport) \
571 fc_starget_##field(starget) = rport->field; \
572 else if (i->f->get_starget_##field) \
573 i->f->get_starget_##field(starget); \
574 return snprintf(buf, sz, format_string, \
575 cast fc_starget_##field(starget)); \
576}
577
578#define fc_starget_rd_attr(field, format_string, sz) \
579 fc_starget_show_function(field, format_string, sz, ) \
580static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
581 show_fc_starget_##field, NULL)
582
583#define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
584 fc_starget_show_function(field, format_string, sz, (cast)) \
585static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
586 show_fc_starget_##field, NULL)
587
588#define SETUP_STARGET_ATTRIBUTE_RD(field) \
589 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
590 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
591 i->private_starget_attrs[count].store = NULL; \
592 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
593 if (i->f->show_starget_##field) \
594 count++
595
596#define SETUP_STARGET_ATTRIBUTE_RW(field) \
597 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
598 if (!i->f->set_starget_##field) { \
599 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
600 i->private_starget_attrs[count].store = NULL; \
601 } \
602 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
603 if (i->f->show_starget_##field) \
604 count++
605
606/* The FC Transport SCSI Target Attributes: */
607fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
608fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
609fc_starget_rd_attr(port_id, "0x%06x\n", 20);
610
611
612/*
613 * Host Attribute Management
614 */
615
616#define fc_host_show_function(field, format_string, sz, cast) \
617static ssize_t \
618show_fc_host_##field (struct class_device *cdev, char *buf) \
619{ \
620 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
621 struct fc_internal *i = to_fc_internal(shost->transportt); \
622 if (i->f->get_host_##field) \
623 i->f->get_host_##field(shost); \
624 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
625}
626
627#define fc_host_store_function(field) \
628static ssize_t \
629store_fc_host_##field(struct class_device *cdev, const char *buf, \
630 size_t count) \
631{ \
632 int val; \
633 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
634 struct fc_internal *i = to_fc_internal(shost->transportt); \
635 \
636 val = simple_strtoul(buf, NULL, 0); \
637 i->f->set_host_##field(shost, val); \
638 return count; \
639}
640
641#define fc_host_rd_attr(field, format_string, sz) \
642 fc_host_show_function(field, format_string, sz, ) \
643static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
644 show_fc_host_##field, NULL)
645
646#define fc_host_rd_attr_cast(field, format_string, sz, cast) \
647 fc_host_show_function(field, format_string, sz, (cast)) \
648static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
649 show_fc_host_##field, NULL)
650
651#define fc_host_rw_attr(field, format_string, sz) \
652 fc_host_show_function(field, format_string, sz, ) \
653 fc_host_store_function(field) \
654static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
655 show_fc_host_##field, \
656 store_fc_host_##field)
657
658#define fc_host_rd_enum_attr(title, maxlen) \
659static ssize_t \
660show_fc_host_##title (struct class_device *cdev, char *buf) \
661{ \
662 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
663 struct fc_internal *i = to_fc_internal(shost->transportt); \
664 const char *name; \
665 if (i->f->get_host_##title) \
666 i->f->get_host_##title(shost); \
667 name = get_fc_##title##_name(fc_host_##title(shost)); \
668 if (!name) \
669 return -EINVAL; \
670 return snprintf(buf, maxlen, "%s\n", name); \
671} \
672static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
673
674#define SETUP_HOST_ATTRIBUTE_RD(field) \
675 i->private_host_attrs[count] = class_device_attr_host_##field; \
676 i->private_host_attrs[count].attr.mode = S_IRUGO; \
677 i->private_host_attrs[count].store = NULL; \
678 i->host_attrs[count] = &i->private_host_attrs[count]; \
679 if (i->f->show_host_##field) \
680 count++
681
682#define SETUP_HOST_ATTRIBUTE_RW(field) \
683 i->private_host_attrs[count] = class_device_attr_host_##field; \
684 if (!i->f->set_host_##field) { \
685 i->private_host_attrs[count].attr.mode = S_IRUGO; \
686 i->private_host_attrs[count].store = NULL; \
687 } \
688 i->host_attrs[count] = &i->private_host_attrs[count]; \
689 if (i->f->show_host_##field) \
690 count++
691
692
693#define fc_private_host_show_function(field, format_string, sz, cast) \
694static ssize_t \
695show_fc_host_##field (struct class_device *cdev, char *buf) \
696{ \
697 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
698 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
699}
700
701#define fc_private_host_rd_attr(field, format_string, sz) \
702 fc_private_host_show_function(field, format_string, sz, ) \
703static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
704 show_fc_host_##field, NULL)
705
706#define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
707 fc_private_host_show_function(field, format_string, sz, (cast)) \
708static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
709 show_fc_host_##field, NULL)
710
711#define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
712 i->private_host_attrs[count] = class_device_attr_host_##field; \
713 i->private_host_attrs[count].attr.mode = S_IRUGO; \
714 i->private_host_attrs[count].store = NULL; \
715 i->host_attrs[count] = &i->private_host_attrs[count]; \
716 count++
717
718#define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
719 i->private_host_attrs[count] = class_device_attr_host_##field; \
720 i->host_attrs[count] = &i->private_host_attrs[count]; \
721 count++
722
723
724/* Fixed Host Attributes */
725
726static ssize_t
727show_fc_host_supported_classes (struct class_device *cdev, char *buf)
728{
729 struct Scsi_Host *shost = transport_class_to_shost(cdev);
730
731 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
732 return snprintf(buf, 20, "unspecified\n");
733
734 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
735}
736static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
737 show_fc_host_supported_classes, NULL);
738
739static ssize_t
740show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
741{
742 struct Scsi_Host *shost = transport_class_to_shost(cdev);
743 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
744}
745static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
746 show_fc_host_supported_fc4s, NULL);
747
748static ssize_t
749show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
750{
751 struct Scsi_Host *shost = transport_class_to_shost(cdev);
752
753 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
754 return snprintf(buf, 20, "unknown\n");
755
756 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
757}
758static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
759 show_fc_host_supported_speeds, NULL);
760
761
762fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
763fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
764fc_private_host_rd_attr(symbolic_name, "%s\n", (FC_SYMBOLIC_NAME_SIZE +1));
765fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
766fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
767
768
769/* Dynamic Host Attributes */
770
771static ssize_t
772show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
773{
774 struct Scsi_Host *shost = transport_class_to_shost(cdev);
775 struct fc_internal *i = to_fc_internal(shost->transportt);
776
777 if (i->f->get_host_active_fc4s)
778 i->f->get_host_active_fc4s(shost);
779
780 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
781}
782static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
783 show_fc_host_active_fc4s, NULL);
784
785static ssize_t
786show_fc_host_speed (struct class_device *cdev, char *buf)
787{
788 struct Scsi_Host *shost = transport_class_to_shost(cdev);
789 struct fc_internal *i = to_fc_internal(shost->transportt);
790
791 if (i->f->get_host_speed)
792 i->f->get_host_speed(shost);
793
794 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
795 return snprintf(buf, 20, "unknown\n");
796
797 return get_fc_port_speed_names(fc_host_speed(shost), buf);
798}
799static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
800 show_fc_host_speed, NULL);
801
802
803fc_host_rd_attr(port_id, "0x%06x\n", 20);
804fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
805fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
806fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
807
808
809/* Private Host Attributes */
810
811static ssize_t
812show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
813{
814 struct Scsi_Host *shost = transport_class_to_shost(cdev);
815 const char *name;
816
817 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
818 if (!name)
819 return -EINVAL;
820 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
821}
822
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -0400823#define get_list_head_entry(pos, head, member) \
824 pos = list_entry((head)->next, typeof(*pos), member)
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826static ssize_t
827store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
828 const char *buf, size_t count)
829{
830 struct Scsi_Host *shost = transport_class_to_shost(cdev);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -0400831 struct fc_rport *rport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 enum fc_tgtid_binding_type val;
833 unsigned long flags;
834
835 if (get_fc_tgtid_bind_type_match(buf, &val))
836 return -EINVAL;
837
838 /* if changing bind type, purge all unused consistent bindings */
839 if (val != fc_host_tgtid_bind_type(shost)) {
840 spin_lock_irqsave(shost->host_lock, flags);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -0400841 while (!list_empty(&fc_host_rport_bindings(shost))) {
842 get_list_head_entry(rport,
843 &fc_host_rport_bindings(shost), peers);
844 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 fc_rport_terminate(rport);
James.Smart@Emulex.Comd16794f6a2005-10-05 13:50:08 -0400846 spin_lock_irqsave(shost->host_lock, flags);
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 spin_unlock_irqrestore(shost->host_lock, flags);
849 }
850
851 fc_host_tgtid_bind_type(shost) = val;
852 return count;
853}
854
855static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
856 show_fc_private_host_tgtid_bind_type,
857 store_fc_private_host_tgtid_bind_type);
858
859/*
860 * Host Statistics Management
861 */
862
863/* Show a given an attribute in the statistics group */
864static ssize_t
865fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
866{
867 struct Scsi_Host *shost = transport_class_to_shost(cdev);
868 struct fc_internal *i = to_fc_internal(shost->transportt);
869 struct fc_host_statistics *stats;
870 ssize_t ret = -ENOENT;
871
872 if (offset > sizeof(struct fc_host_statistics) ||
873 offset % sizeof(u64) != 0)
874 WARN_ON(1);
875
876 if (i->f->get_fc_host_stats) {
877 stats = (i->f->get_fc_host_stats)(shost);
878 if (stats)
879 ret = snprintf(buf, 20, "0x%llx\n",
880 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
881 }
882 return ret;
883}
884
885
886/* generate a read-only statistics attribute */
887#define fc_host_statistic(name) \
888static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
889{ \
890 return fc_stat_show(cd, buf, \
891 offsetof(struct fc_host_statistics, name)); \
892} \
893static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
894
895fc_host_statistic(seconds_since_last_reset);
896fc_host_statistic(tx_frames);
897fc_host_statistic(tx_words);
898fc_host_statistic(rx_frames);
899fc_host_statistic(rx_words);
900fc_host_statistic(lip_count);
901fc_host_statistic(nos_count);
902fc_host_statistic(error_frames);
903fc_host_statistic(dumped_frames);
904fc_host_statistic(link_failure_count);
905fc_host_statistic(loss_of_sync_count);
906fc_host_statistic(loss_of_signal_count);
907fc_host_statistic(prim_seq_protocol_err_count);
908fc_host_statistic(invalid_tx_word_count);
909fc_host_statistic(invalid_crc_count);
910fc_host_statistic(fcp_input_requests);
911fc_host_statistic(fcp_output_requests);
912fc_host_statistic(fcp_control_requests);
913fc_host_statistic(fcp_input_megabytes);
914fc_host_statistic(fcp_output_megabytes);
915
916static ssize_t
917fc_reset_statistics(struct class_device *cdev, const char *buf,
918 size_t count)
919{
920 struct Scsi_Host *shost = transport_class_to_shost(cdev);
921 struct fc_internal *i = to_fc_internal(shost->transportt);
922
923 /* ignore any data value written to the attribute */
924 if (i->f->reset_fc_host_stats) {
925 i->f->reset_fc_host_stats(shost);
926 return count;
927 }
928
929 return -ENOENT;
930}
931static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
932 fc_reset_statistics);
933
934
935static struct attribute *fc_statistics_attrs[] = {
936 &class_device_attr_host_seconds_since_last_reset.attr,
937 &class_device_attr_host_tx_frames.attr,
938 &class_device_attr_host_tx_words.attr,
939 &class_device_attr_host_rx_frames.attr,
940 &class_device_attr_host_rx_words.attr,
941 &class_device_attr_host_lip_count.attr,
942 &class_device_attr_host_nos_count.attr,
943 &class_device_attr_host_error_frames.attr,
944 &class_device_attr_host_dumped_frames.attr,
945 &class_device_attr_host_link_failure_count.attr,
946 &class_device_attr_host_loss_of_sync_count.attr,
947 &class_device_attr_host_loss_of_signal_count.attr,
948 &class_device_attr_host_prim_seq_protocol_err_count.attr,
949 &class_device_attr_host_invalid_tx_word_count.attr,
950 &class_device_attr_host_invalid_crc_count.attr,
951 &class_device_attr_host_fcp_input_requests.attr,
952 &class_device_attr_host_fcp_output_requests.attr,
953 &class_device_attr_host_fcp_control_requests.attr,
954 &class_device_attr_host_fcp_input_megabytes.attr,
955 &class_device_attr_host_fcp_output_megabytes.attr,
956 &class_device_attr_host_reset_statistics.attr,
957 NULL
958};
959
960static struct attribute_group fc_statistics_group = {
961 .name = "statistics",
962 .attrs = fc_statistics_attrs,
963};
964
965static int fc_host_match(struct attribute_container *cont,
966 struct device *dev)
967{
968 struct Scsi_Host *shost;
969 struct fc_internal *i;
970
971 if (!scsi_is_host_device(dev))
972 return 0;
973
974 shost = dev_to_shost(dev);
975 if (!shost->transportt || shost->transportt->host_attrs.ac.class
976 != &fc_host_class.class)
977 return 0;
978
979 i = to_fc_internal(shost->transportt);
980
981 return &i->t.host_attrs.ac == cont;
982}
983
984static int fc_target_match(struct attribute_container *cont,
985 struct device *dev)
986{
987 struct Scsi_Host *shost;
988 struct fc_internal *i;
989
990 if (!scsi_is_target_device(dev))
991 return 0;
992
993 shost = dev_to_shost(dev->parent);
994 if (!shost->transportt || shost->transportt->host_attrs.ac.class
995 != &fc_host_class.class)
996 return 0;
997
998 i = to_fc_internal(shost->transportt);
999
1000 return &i->t.target_attrs.ac == cont;
1001}
1002
1003static void fc_rport_dev_release(struct device *dev)
1004{
1005 struct fc_rport *rport = dev_to_rport(dev);
1006 put_device(dev->parent);
1007 kfree(rport);
1008}
1009
1010int scsi_is_fc_rport(const struct device *dev)
1011{
1012 return dev->release == fc_rport_dev_release;
1013}
1014EXPORT_SYMBOL(scsi_is_fc_rport);
1015
1016static int fc_rport_match(struct attribute_container *cont,
1017 struct device *dev)
1018{
1019 struct Scsi_Host *shost;
1020 struct fc_internal *i;
1021
1022 if (!scsi_is_fc_rport(dev))
1023 return 0;
1024
1025 shost = dev_to_shost(dev->parent);
1026 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1027 != &fc_host_class.class)
1028 return 0;
1029
1030 i = to_fc_internal(shost->transportt);
1031
1032 return &i->rport_attr_cont.ac == cont;
1033}
1034
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001035
1036/*
1037 * Must be called with shost->host_lock held
1038 */
1039static struct device *fc_target_parent(struct Scsi_Host *shost,
1040 int channel, uint id)
1041{
1042 struct fc_rport *rport;
1043
1044 list_for_each_entry(rport, &fc_host_rports(shost), peers)
1045 if ((rport->channel == channel) &&
1046 (rport->scsi_target_id == id))
1047 return &rport->dev;
1048
1049 return NULL;
1050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052struct scsi_transport_template *
1053fc_attach_transport(struct fc_function_template *ft)
1054{
1055 struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
1056 GFP_KERNEL);
1057 int count;
1058
1059 if (unlikely(!i))
1060 return NULL;
1061
1062 memset(i, 0, sizeof(struct fc_internal));
1063
1064 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1065 i->t.target_attrs.ac.class = &fc_transport_class.class;
1066 i->t.target_attrs.ac.match = fc_target_match;
1067 i->t.target_size = sizeof(struct fc_starget_attrs);
1068 transport_container_register(&i->t.target_attrs);
1069
1070 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1071 i->t.host_attrs.ac.class = &fc_host_class.class;
1072 i->t.host_attrs.ac.match = fc_host_match;
1073 i->t.host_size = sizeof(struct fc_host_attrs);
1074 if (ft->get_fc_host_stats)
1075 i->t.host_attrs.statistics = &fc_statistics_group;
1076 transport_container_register(&i->t.host_attrs);
1077
1078 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1079 i->rport_attr_cont.ac.class = &fc_rport_class.class;
1080 i->rport_attr_cont.ac.match = fc_rport_match;
1081 transport_container_register(&i->rport_attr_cont);
1082
1083 i->f = ft;
1084
1085 /* Transport uses the shost workq for scsi scanning */
1086 i->t.create_work_queue = 1;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -04001087
1088 i->t.target_parent = fc_target_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 /*
1091 * Setup SCSI Target Attributes.
1092 */
1093 count = 0;
1094 SETUP_STARGET_ATTRIBUTE_RD(node_name);
1095 SETUP_STARGET_ATTRIBUTE_RD(port_name);
1096 SETUP_STARGET_ATTRIBUTE_RD(port_id);
1097
1098 BUG_ON(count > FC_STARGET_NUM_ATTRS);
1099
1100 i->starget_attrs[count] = NULL;
1101
1102
1103 /*
1104 * Setup SCSI Host Attributes.
1105 */
1106 count=0;
1107 SETUP_HOST_ATTRIBUTE_RD(node_name);
1108 SETUP_HOST_ATTRIBUTE_RD(port_name);
1109 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
1110 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
1111 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
1112 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
1113 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
1114 SETUP_HOST_ATTRIBUTE_RD(serial_number);
1115
1116 SETUP_HOST_ATTRIBUTE_RD(port_id);
1117 SETUP_HOST_ATTRIBUTE_RD(port_type);
1118 SETUP_HOST_ATTRIBUTE_RD(port_state);
1119 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
1120 SETUP_HOST_ATTRIBUTE_RD(speed);
1121 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
1122
1123 /* Transport-managed attributes */
1124 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
1125
1126 BUG_ON(count > FC_HOST_NUM_ATTRS);
1127
1128 i->host_attrs[count] = NULL;
1129
1130 /*
1131 * Setup Remote Port Attributes.
1132 */
1133 count=0;
1134 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
1135 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
1136 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
1137 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
1138 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
1139 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
1140 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
1141 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
1142 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
1143
1144 BUG_ON(count > FC_RPORT_NUM_ATTRS);
1145
1146 i->rport_attrs[count] = NULL;
1147
1148 return &i->t;
1149}
1150EXPORT_SYMBOL(fc_attach_transport);
1151
1152void fc_release_transport(struct scsi_transport_template *t)
1153{
1154 struct fc_internal *i = to_fc_internal(t);
1155
1156 transport_container_unregister(&i->t.target_attrs);
1157 transport_container_unregister(&i->t.host_attrs);
1158 transport_container_unregister(&i->rport_attr_cont);
1159
1160 kfree(i);
1161}
1162EXPORT_SYMBOL(fc_release_transport);
1163
1164
1165/**
1166 * fc_remove_host - called to terminate any fc_transport-related elements
1167 * for a scsi host.
1168 * @rport: remote port to be unblocked.
1169 *
1170 * This routine is expected to be called immediately preceeding the
1171 * a driver's call to scsi_remove_host().
1172 *
1173 * WARNING: A driver utilizing the fc_transport, which fails to call
1174 * this routine prior to scsi_remote_host(), will leave dangling
1175 * objects in /sys/class/fc_remote_ports. Access to any of these
1176 * objects can result in a system crash !!!
1177 *
1178 * Notes:
1179 * This routine assumes no locks are held on entry.
1180 **/
1181void
1182fc_remove_host(struct Scsi_Host *shost)
1183{
1184 struct fc_rport *rport, *next_rport;
1185
1186 /* Remove any remote ports */
1187 list_for_each_entry_safe(rport, next_rport,
1188 &fc_host_rports(shost), peers)
1189 fc_rport_terminate(rport);
1190 list_for_each_entry_safe(rport, next_rport,
1191 &fc_host_rport_bindings(shost), peers)
1192 fc_rport_terminate(rport);
1193}
1194EXPORT_SYMBOL(fc_remove_host);
1195
1196/**
1197 * fc_rport_create - allocates and creates a remote FC port.
1198 * @shost: scsi host the remote port is connected to.
1199 * @channel: Channel on shost port connected to.
1200 * @ids: The world wide names, fc address, and FC4 port
1201 * roles for the remote port.
1202 *
1203 * Allocates and creates the remoter port structure, including the
1204 * class and sysfs creation.
1205 *
1206 * Notes:
1207 * This routine assumes no locks are held on entry.
1208 **/
1209struct fc_rport *
1210fc_rport_create(struct Scsi_Host *shost, int channel,
1211 struct fc_rport_identifiers *ids)
1212{
1213 struct fc_host_attrs *fc_host =
1214 (struct fc_host_attrs *)shost->shost_data;
1215 struct fc_internal *fci = to_fc_internal(shost->transportt);
1216 struct fc_rport *rport;
1217 struct device *dev;
1218 unsigned long flags;
1219 int error;
1220 size_t size;
1221
1222 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
1223 rport = kmalloc(size, GFP_KERNEL);
1224 if (unlikely(!rport)) {
1225 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
1226 return NULL;
1227 }
1228 memset(rport, 0, size);
1229
1230 rport->maxframe_size = -1;
1231 rport->supported_classes = FC_COS_UNSPECIFIED;
1232 rport->dev_loss_tmo = fc_dev_loss_tmo;
1233 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
1234 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
1235 rport->port_id = ids->port_id;
1236 rport->roles = ids->roles;
1237 rport->port_state = FC_PORTSTATE_ONLINE;
1238 if (fci->f->dd_fcrport_size)
1239 rport->dd_data = &rport[1];
1240 rport->channel = channel;
1241
1242 INIT_WORK(&rport->dev_loss_work, fc_timeout_blocked_rport, rport);
1243 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
1244
1245 spin_lock_irqsave(shost->host_lock, flags);
1246
1247 rport->number = fc_host->next_rport_number++;
1248 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1249 rport->scsi_target_id = fc_host->next_target_id++;
1250 else
1251 rport->scsi_target_id = -1;
1252 list_add_tail(&rport->peers, &fc_host_rports(shost));
1253 get_device(&shost->shost_gendev);
1254
1255 spin_unlock_irqrestore(shost->host_lock, flags);
1256
1257 dev = &rport->dev;
1258 device_initialize(dev);
1259 dev->parent = get_device(&shost->shost_gendev);
1260 dev->release = fc_rport_dev_release;
1261 sprintf(dev->bus_id, "rport-%d:%d-%d",
1262 shost->host_no, channel, rport->number);
1263 transport_setup_device(dev);
1264
1265 error = device_add(dev);
1266 if (error) {
1267 printk(KERN_ERR "FC Remote Port device_add failed\n");
1268 goto delete_rport;
1269 }
1270 transport_add_device(dev);
1271 transport_configure_device(dev);
1272
1273 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1274 /* initiate a scan of the target */
1275 scsi_queue_work(shost, &rport->scan_work);
1276
1277 return rport;
1278
1279delete_rport:
1280 transport_destroy_device(dev);
1281 put_device(dev->parent);
1282 spin_lock_irqsave(shost->host_lock, flags);
1283 list_del(&rport->peers);
1284 put_device(&shost->shost_gendev);
1285 spin_unlock_irqrestore(shost->host_lock, flags);
1286 put_device(dev->parent);
1287 kfree(rport);
1288 return NULL;
1289}
1290
1291/**
1292 * fc_remote_port_add - notifies the fc transport of the existence
1293 * of a remote FC port.
1294 * @shost: scsi host the remote port is connected to.
1295 * @channel: Channel on shost port connected to.
1296 * @ids: The world wide names, fc address, and FC4 port
1297 * roles for the remote port.
1298 *
1299 * The LLDD calls this routine to notify the transport of the existence
1300 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
1301 * of the port, it's FC address (port_id), and the FC4 roles that are
1302 * active for the port.
1303 *
1304 * For ports that are FCP targets (aka scsi targets), the FC transport
1305 * maintains consistent target id bindings on behalf of the LLDD.
1306 * A consistent target id binding is an assignment of a target id to
1307 * a remote port identifier, which persists while the scsi host is
1308 * attached. The remote port can disappear, then later reappear, and
1309 * it's target id assignment remains the same. This allows for shifts
1310 * in FC addressing (if binding by wwpn or wwnn) with no apparent
1311 * changes to the scsi subsystem which is based on scsi host number and
1312 * target id values. Bindings are only valid during the attachment of
1313 * the scsi host. If the host detaches, then later re-attaches, target
1314 * id bindings may change.
1315 *
1316 * This routine is responsible for returning a remote port structure.
1317 * The routine will search the list of remote ports it maintains
1318 * internally on behalf of consistent target id mappings. If found, the
1319 * remote port structure will be reused. Otherwise, a new remote port
1320 * structure will be allocated.
1321 *
1322 * Whenever a remote port is allocated, a new fc_remote_port class
1323 * device is created.
1324 *
1325 * Should not be called from interrupt context.
1326 *
1327 * Notes:
1328 * This routine assumes no locks are held on entry.
1329 **/
1330struct fc_rport *
1331fc_remote_port_add(struct Scsi_Host *shost, int channel,
1332 struct fc_rport_identifiers *ids)
1333{
1334 struct fc_rport *rport;
1335 unsigned long flags;
1336 int match = 0;
1337
1338 if (likely((ids->roles & FC_RPORT_ROLE_FCP_TARGET) &&
1339 (fc_host_tgtid_bind_type(shost) != FC_TGTID_BIND_NONE))) {
1340
1341 /* search for a matching consistent binding */
1342
1343 spin_lock_irqsave(shost->host_lock, flags);
1344
1345 list_for_each_entry(rport, &fc_host_rport_bindings(shost),
1346 peers) {
1347 if (rport->channel != channel)
1348 continue;
1349
1350 switch (fc_host_tgtid_bind_type(shost)) {
1351 case FC_TGTID_BIND_BY_WWPN:
1352 if (rport->port_name == ids->port_name)
1353 match = 1;
1354 break;
1355 case FC_TGTID_BIND_BY_WWNN:
1356 if (rport->node_name == ids->node_name)
1357 match = 1;
1358 break;
1359 case FC_TGTID_BIND_BY_ID:
1360 if (rport->port_id == ids->port_id)
1361 match = 1;
1362 break;
1363 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1364 break;
1365 }
1366
1367 if (match) {
1368 list_move_tail(&rport->peers,
1369 &fc_host_rports(shost));
1370 break;
1371 }
1372 }
1373
1374 spin_unlock_irqrestore(shost->host_lock, flags);
1375
1376 if (match) {
1377 memcpy(&rport->node_name, &ids->node_name,
1378 sizeof(rport->node_name));
1379 memcpy(&rport->port_name, &ids->port_name,
1380 sizeof(rport->port_name));
1381 rport->port_id = ids->port_id;
1382 rport->roles = ids->roles;
1383 rport->port_state = FC_PORTSTATE_ONLINE;
1384
1385 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
1386 /* initiate a scan of the target */
1387 scsi_queue_work(shost, &rport->scan_work);
1388
1389 return rport;
1390 }
1391 }
1392
1393 /* No consistent binding found - create new remote port entry */
1394 rport = fc_rport_create(shost, channel, ids);
1395
1396 return rport;
1397}
1398EXPORT_SYMBOL(fc_remote_port_add);
1399
1400/*
1401 * fc_rport_tgt_remove - Removes the scsi target on the remote port
1402 * @rport: The remote port to be operated on
1403 */
1404static void
1405fc_rport_tgt_remove(struct fc_rport *rport)
1406{
1407 struct Scsi_Host *shost = rport_to_shost(rport);
1408
1409 scsi_target_unblock(&rport->dev);
1410
1411 /* Stop anything on the workq */
1412 if (!cancel_delayed_work(&rport->dev_loss_work))
1413 flush_scheduled_work();
1414 scsi_flush_work(shost);
1415
1416 scsi_remove_target(&rport->dev);
1417}
1418
1419/*
1420 * fc_rport_terminate - this routine tears down and deallocates a remote port.
1421 * @rport: The remote port to be terminated
1422 *
1423 * Notes:
1424 * This routine assumes no locks are held on entry.
1425 */
1426static void
1427fc_rport_terminate(struct fc_rport *rport)
1428{
1429 struct Scsi_Host *shost = rport_to_shost(rport);
1430 struct device *dev = &rport->dev;
1431 unsigned long flags;
1432
1433 fc_rport_tgt_remove(rport);
1434
1435 transport_remove_device(dev);
1436 device_del(dev);
1437 transport_destroy_device(dev);
1438 spin_lock_irqsave(shost->host_lock, flags);
1439 list_del(&rport->peers);
1440 spin_unlock_irqrestore(shost->host_lock, flags);
1441 put_device(&shost->shost_gendev);
1442}
1443
1444/**
1445 * fc_remote_port_delete - notifies the fc transport that a remote
1446 * port is no longer in existence.
1447 * @rport: The remote port that no longer exists
1448 *
1449 * The LLDD calls this routine to notify the transport that a remote
1450 * port is no longer part of the topology. Note: Although a port
1451 * may no longer be part of the topology, it may persist in the remote
1452 * ports displayed by the fc_host. This is done so that target id
1453 * mappings (managed via the remote port structures), are always visible
1454 * as long as the mapping is valid, regardless of port state,
1455 *
1456 * If the remote port is not an FCP Target, it will be fully torn down
1457 * and deallocated, including the fc_remote_port class device.
1458 *
1459 * If the remote port is an FCP Target, the port structure will be
1460 * marked as Not Present, but will remain as long as there is a valid
1461 * SCSI target id mapping associated with the port structure. Validity
1462 * is determined by the binding type. If binding by wwpn, then the port
1463 * structure is always valid and will not be deallocated until the host
1464 * is removed. If binding by wwnn, then the port structure is valid
1465 * until another port with the same node name is found in the topology.
1466 * If binding by port id (fc address), then the port structure is valid
1467 * valid until another port with the same address is identified.
1468 *
1469 * Called from interrupt or normal process context.
1470 *
1471 * Notes:
1472 * This routine assumes no locks are held on entry.
1473 **/
1474void
1475fc_remote_port_delete(struct fc_rport *rport)
1476{
1477 struct Scsi_Host *shost = rport_to_shost(rport);
1478 unsigned long flags;
1479
1480 /* If no scsi target id mapping or consistent binding type, delete it */
1481 if ((rport->scsi_target_id == -1) ||
1482 (fc_host_tgtid_bind_type(shost) == FC_TGTID_BIND_NONE)) {
1483 fc_rport_terminate(rport);
1484 return;
1485 }
1486
1487 fc_rport_tgt_remove(rport);
1488
1489 spin_lock_irqsave(shost->host_lock, flags);
1490 list_move_tail(&rport->peers, &fc_host_rport_bindings(shost));
1491 spin_unlock_irqrestore(shost->host_lock, flags);
1492
1493 /*
1494 * Note: We do not remove or clear the hostdata area. This allows
1495 * host-specific target data to persist along with the
1496 * scsi_target_id. It's up to the host to manage it's hostdata area.
1497 */
1498
1499 /*
1500 * Reinitialize port attributes that may change if the port comes back.
1501 */
1502 rport->maxframe_size = -1;
1503 rport->supported_classes = FC_COS_UNSPECIFIED;
1504 rport->roles = FC_RPORT_ROLE_UNKNOWN;
1505 rport->port_state = FC_PORTSTATE_NOTPRESENT;
1506
1507 /* remove the identifiers that aren't used in the consisting binding */
1508 switch (fc_host_tgtid_bind_type(shost)) {
1509 case FC_TGTID_BIND_BY_WWPN:
1510 rport->node_name = -1;
1511 rport->port_id = -1;
1512 break;
1513 case FC_TGTID_BIND_BY_WWNN:
1514 rport->port_name = -1;
1515 rport->port_id = -1;
1516 break;
1517 case FC_TGTID_BIND_BY_ID:
1518 rport->node_name = -1;
1519 rport->port_name = -1;
1520 break;
1521 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
1522 break;
1523 }
1524}
1525EXPORT_SYMBOL(fc_remote_port_delete);
1526
1527/**
1528 * fc_remote_port_rolechg - notifies the fc transport that the roles
1529 * on a remote may have changed.
1530 * @rport: The remote port that changed.
1531 *
1532 * The LLDD calls this routine to notify the transport that the roles
1533 * on a remote port may have changed. The largest effect of this is
1534 * if a port now becomes a FCP Target, it must be allocated a
1535 * scsi target id. If the port is no longer a FCP target, any
1536 * scsi target id value assigned to it will persist in case the
1537 * role changes back to include FCP Target. No changes in the scsi
1538 * midlayer will be invoked if the role changes (in the expectation
1539 * that the role will be resumed. If it doesn't normal error processing
1540 * will take place).
1541 *
1542 * Should not be called from interrupt context.
1543 *
1544 * Notes:
1545 * This routine assumes no locks are held on entry.
1546 **/
1547void
1548fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
1549{
1550 struct Scsi_Host *shost = rport_to_shost(rport);
1551 struct fc_host_attrs *fc_host =
1552 (struct fc_host_attrs *)shost->shost_data;
1553 unsigned long flags;
1554 int create = 0;
1555
1556 rport->roles = roles;
1557
1558 spin_lock_irqsave(shost->host_lock, flags);
1559 if ((rport->scsi_target_id == -1) &&
1560 (rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
1561 rport->scsi_target_id = fc_host->next_target_id++;
1562 create = 1;
1563 }
1564 spin_unlock_irqrestore(shost->host_lock, flags);
1565
1566 if (create)
1567 /* initiate a scan of the target */
1568 scsi_queue_work(shost, &rport->scan_work);
1569}
1570EXPORT_SYMBOL(fc_remote_port_rolechg);
1571
1572/**
1573 * fc_timeout_blocked_rport - Timeout handler for blocked remote port
1574 * that fails to return in the alloted time.
1575 * @data: scsi target that failed to reappear in the alloted time.
1576 **/
1577static void
1578fc_timeout_blocked_rport(void *data)
1579{
1580 struct fc_rport *rport = (struct fc_rport *)data;
1581
1582 rport->port_state = FC_PORTSTATE_OFFLINE;
1583
1584 dev_printk(KERN_ERR, &rport->dev,
1585 "blocked FC remote port time out: removing target\n");
1586
1587 /*
1588 * As this only occurs if the remote port (scsi target)
1589 * went away and didn't come back - we'll remove
1590 * all attached scsi devices.
1591 */
1592 scsi_target_unblock(&rport->dev);
1593 scsi_remove_target(&rport->dev);
1594}
1595
1596/**
1597 * fc_remote_port_block - temporarily block any scsi traffic to a remote port.
1598 * @rport: remote port to be blocked.
1599 *
1600 * scsi lldd's with a FC transport call this routine to temporarily stop
1601 * all scsi traffic to a remote port. If the port is not a SCSI target,
1602 * no action is taken. If the port is a SCSI target, all attached devices
1603 * are placed into a SDEV_BLOCK state and a timer is started. The timer is
1604 * represents the maximum amount of time the port may be blocked. If the
1605 * timer expires, the port is considered non-existent and the attached
1606 * scsi devices will be removed.
1607 *
1608 * Called from interrupt or normal process context.
1609 *
1610 * Returns zero if successful or error if not
1611 *
1612 * Notes:
1613 * This routine assumes no locks are held on entry.
1614 *
1615 * The timeout and timer types are extracted from the fc transport
1616 * attributes from the caller's rport pointer.
1617 **/
1618int
1619fc_remote_port_block(struct fc_rport *rport)
1620{
1621 int timeout = rport->dev_loss_tmo;
1622 struct work_struct *work = &rport->dev_loss_work;
1623
1624 if (timeout < 0 || timeout > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
1625 return -EINVAL;
1626
1627 scsi_target_block(&rport->dev);
1628
1629 /* cap the length the devices can be blocked */
1630 schedule_delayed_work(work, timeout * HZ);
1631
1632 rport->port_state = FC_PORTSTATE_BLOCKED;
1633 return 0;
1634}
1635EXPORT_SYMBOL(fc_remote_port_block);
1636
1637/**
1638 * fc_remote_port_unblock - restart any blocked scsi traffic to a remote port.
1639 * @rport: remote port to be unblocked.
1640 *
1641 * scsi lld's with a FC transport call this routine to restart IO to all
1642 * devices associated with the caller's scsi target following a fc_target_block
1643 * request. Called from interrupt or normal process context.
1644 *
1645 * Notes:
1646 * This routine assumes no locks are held on entry.
1647 **/
1648 void
1649fc_remote_port_unblock(struct fc_rport *rport)
1650{
1651 struct work_struct *work = &rport->dev_loss_work;
1652 struct Scsi_Host *shost = rport_to_shost(rport);
1653
1654 /*
1655 * Stop the target timer first. Take no action on the del_timer
1656 * failure as the state machine state change will validate the
1657 * transaction.
1658 */
1659 if (!cancel_delayed_work(work))
1660 flush_scheduled_work();
1661
1662 if (rport->port_state == FC_PORTSTATE_OFFLINE)
1663 /*
1664 * initiate a scan of the target as the target has
1665 * been torn down.
1666 */
1667 scsi_queue_work(shost, &rport->scan_work);
1668 else
1669 scsi_target_unblock(&rport->dev);
1670
1671 rport->port_state = FC_PORTSTATE_ONLINE;
1672}
1673EXPORT_SYMBOL(fc_remote_port_unblock);
1674
1675/**
1676 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
1677 * @data: remote port to be scanned.
1678 **/
1679static void
1680fc_scsi_scan_rport(void *data)
1681{
1682 struct fc_rport *rport = (struct fc_rport *)data;
1683
1684 scsi_scan_target(&rport->dev, rport->channel, rport->scsi_target_id,
1685 SCAN_WILD_CARD, 1);
1686}
1687
1688
1689MODULE_AUTHOR("Martin Hicks");
1690MODULE_DESCRIPTION("FC Transport Attributes");
1691MODULE_LICENSE("GPL");
1692
1693module_init(fc_transport_init);
1694module_exit(fc_transport_exit);