fs: use kmem_cache_zalloc instead
[pv_ops_mirror.git] / drivers / scsi / scsi_transport_fc.c
blob7a7cfe583b2ab46171bb51929465aca2abad2ae5
1 /*
2 * FiberChannel transport specific attributes exported to sysfs.
4 * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
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.
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.
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
20 * ========
22 * Copyright (C) 2004-2007 James Smart, Emulex Corporation
23 * Rewrite for host, target, device, and remote port attributes,
24 * statistics, and service functions...
25 * Add vports, etc
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport.h>
33 #include <scsi/scsi_transport_fc.h>
34 #include <scsi/scsi_cmnd.h>
35 #include <linux/netlink.h>
36 #include <net/netlink.h>
37 #include <scsi/scsi_netlink_fc.h>
38 #include "scsi_priv.h"
39 #include "scsi_transport_fc_internal.h"
41 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
42 static void fc_vport_sched_delete(struct work_struct *work);
45 * This is a temporary carrier for creating a vport. It will eventually
46 * be replaced by a real message definition for sgio or netlink.
48 * fc_vport_identifiers: This set of data contains all elements
49 * to uniquely identify and instantiate a FC virtual port.
51 * Notes:
52 * symbolic_name: The driver is to append the symbolic_name string data
53 * to the symbolic_node_name data that it generates by default.
54 * the resulting combination should then be registered with the switch.
55 * It is expected that things like Xen may stuff a VM title into
56 * this field.
58 struct fc_vport_identifiers {
59 u64 node_name;
60 u64 port_name;
61 u32 roles;
62 bool disable;
63 enum fc_port_type vport_type; /* only FC_PORTTYPE_NPIV allowed */
64 char symbolic_name[FC_VPORT_SYMBOLIC_NAMELEN];
67 static int fc_vport_create(struct Scsi_Host *shost, int channel,
68 struct device *pdev, struct fc_vport_identifiers *ids,
69 struct fc_vport **vport);
72 * Redefine so that we can have same named attributes in the
73 * sdev/starget/host objects.
75 #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
76 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
77 __ATTR(_name,_mode,_show,_store)
79 #define fc_enum_name_search(title, table_type, table) \
80 static const char *get_fc_##title##_name(enum table_type table_key) \
81 { \
82 int i; \
83 char *name = NULL; \
85 for (i = 0; i < ARRAY_SIZE(table); i++) { \
86 if (table[i].value == table_key) { \
87 name = table[i].name; \
88 break; \
89 } \
90 } \
91 return name; \
94 #define fc_enum_name_match(title, table_type, table) \
95 static int get_fc_##title##_match(const char *table_key, \
96 enum table_type *value) \
97 { \
98 int i; \
100 for (i = 0; i < ARRAY_SIZE(table); i++) { \
101 if (strncmp(table_key, table[i].name, \
102 table[i].matchlen) == 0) { \
103 *value = table[i].value; \
104 return 0; /* success */ \
107 return 1; /* failure */ \
111 /* Convert fc_port_type values to ascii string name */
112 static struct {
113 enum fc_port_type value;
114 char *name;
115 } fc_port_type_names[] = {
116 { FC_PORTTYPE_UNKNOWN, "Unknown" },
117 { FC_PORTTYPE_OTHER, "Other" },
118 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
119 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
120 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
121 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
122 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
123 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
125 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
126 #define FC_PORTTYPE_MAX_NAMELEN 50
128 /* Reuse fc_port_type enum function for vport_type */
129 #define get_fc_vport_type_name get_fc_port_type_name
132 /* Convert fc_host_event_code values to ascii string name */
133 static const struct {
134 enum fc_host_event_code value;
135 char *name;
136 } fc_host_event_code_names[] = {
137 { FCH_EVT_LIP, "lip" },
138 { FCH_EVT_LINKUP, "link_up" },
139 { FCH_EVT_LINKDOWN, "link_down" },
140 { FCH_EVT_LIPRESET, "lip_reset" },
141 { FCH_EVT_RSCN, "rscn" },
142 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
143 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
144 { FCH_EVT_PORT_ONLINE, "port_online" },
145 { FCH_EVT_PORT_OFFLINE, "port_offline" },
146 { FCH_EVT_PORT_FABRIC, "port_fabric" },
147 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
148 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
150 fc_enum_name_search(host_event_code, fc_host_event_code,
151 fc_host_event_code_names)
152 #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
155 /* Convert fc_port_state values to ascii string name */
156 static struct {
157 enum fc_port_state value;
158 char *name;
159 } fc_port_state_names[] = {
160 { FC_PORTSTATE_UNKNOWN, "Unknown" },
161 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
162 { FC_PORTSTATE_ONLINE, "Online" },
163 { FC_PORTSTATE_OFFLINE, "Offline" },
164 { FC_PORTSTATE_BLOCKED, "Blocked" },
165 { FC_PORTSTATE_BYPASSED, "Bypassed" },
166 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
167 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
168 { FC_PORTSTATE_ERROR, "Error" },
169 { FC_PORTSTATE_LOOPBACK, "Loopback" },
170 { FC_PORTSTATE_DELETED, "Deleted" },
172 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
173 #define FC_PORTSTATE_MAX_NAMELEN 20
176 /* Convert fc_vport_state values to ascii string name */
177 static struct {
178 enum fc_vport_state value;
179 char *name;
180 } fc_vport_state_names[] = {
181 { FC_VPORT_UNKNOWN, "Unknown" },
182 { FC_VPORT_ACTIVE, "Active" },
183 { FC_VPORT_DISABLED, "Disabled" },
184 { FC_VPORT_LINKDOWN, "Linkdown" },
185 { FC_VPORT_INITIALIZING, "Initializing" },
186 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
187 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
188 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
189 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
190 { FC_VPORT_FAILED, "VPort Failed" },
192 fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
193 #define FC_VPORTSTATE_MAX_NAMELEN 24
195 /* Reuse fc_vport_state enum function for vport_last_state */
196 #define get_fc_vport_last_state_name get_fc_vport_state_name
199 /* Convert fc_tgtid_binding_type values to ascii string name */
200 static const struct {
201 enum fc_tgtid_binding_type value;
202 char *name;
203 int matchlen;
204 } fc_tgtid_binding_type_names[] = {
205 { FC_TGTID_BIND_NONE, "none", 4 },
206 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
207 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
208 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
210 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
211 fc_tgtid_binding_type_names)
212 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
213 fc_tgtid_binding_type_names)
214 #define FC_BINDTYPE_MAX_NAMELEN 30
217 #define fc_bitfield_name_search(title, table) \
218 static ssize_t \
219 get_fc_##title##_names(u32 table_key, char *buf) \
221 char *prefix = ""; \
222 ssize_t len = 0; \
223 int i; \
225 for (i = 0; i < ARRAY_SIZE(table); i++) { \
226 if (table[i].value & table_key) { \
227 len += sprintf(buf + len, "%s%s", \
228 prefix, table[i].name); \
229 prefix = ", "; \
232 len += sprintf(buf + len, "\n"); \
233 return len; \
237 /* Convert FC_COS bit values to ascii string name */
238 static const struct {
239 u32 value;
240 char *name;
241 } fc_cos_names[] = {
242 { FC_COS_CLASS1, "Class 1" },
243 { FC_COS_CLASS2, "Class 2" },
244 { FC_COS_CLASS3, "Class 3" },
245 { FC_COS_CLASS4, "Class 4" },
246 { FC_COS_CLASS6, "Class 6" },
248 fc_bitfield_name_search(cos, fc_cos_names)
251 /* Convert FC_PORTSPEED bit values to ascii string name */
252 static const struct {
253 u32 value;
254 char *name;
255 } fc_port_speed_names[] = {
256 { FC_PORTSPEED_1GBIT, "1 Gbit" },
257 { FC_PORTSPEED_2GBIT, "2 Gbit" },
258 { FC_PORTSPEED_4GBIT, "4 Gbit" },
259 { FC_PORTSPEED_10GBIT, "10 Gbit" },
260 { FC_PORTSPEED_8GBIT, "8 Gbit" },
261 { FC_PORTSPEED_16GBIT, "16 Gbit" },
262 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
264 fc_bitfield_name_search(port_speed, fc_port_speed_names)
267 static int
268 show_fc_fc4s (char *buf, u8 *fc4_list)
270 int i, len=0;
272 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
273 len += sprintf(buf + len , "0x%02x ", *fc4_list);
274 len += sprintf(buf + len, "\n");
275 return len;
279 /* Convert FC_PORT_ROLE bit values to ascii string name */
280 static const struct {
281 u32 value;
282 char *name;
283 } fc_port_role_names[] = {
284 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
285 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
286 { FC_PORT_ROLE_IP_PORT, "IP Port" },
288 fc_bitfield_name_search(port_roles, fc_port_role_names)
291 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
293 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
294 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
295 #define FC_FPORT_PORTID 0x00000e
296 #define FC_FABCTLR_PORTID 0x00000d
297 #define FC_DIRSRVR_PORTID 0x00000c
298 #define FC_TIMESRVR_PORTID 0x00000b
299 #define FC_MGMTSRVR_PORTID 0x00000a
302 static void fc_timeout_deleted_rport(struct work_struct *work);
303 static void fc_timeout_fail_rport_io(struct work_struct *work);
304 static void fc_scsi_scan_rport(struct work_struct *work);
307 * Attribute counts pre object type...
308 * Increase these values if you add attributes
310 #define FC_STARGET_NUM_ATTRS 3
311 #define FC_RPORT_NUM_ATTRS 10
312 #define FC_VPORT_NUM_ATTRS 9
313 #define FC_HOST_NUM_ATTRS 21
315 struct fc_internal {
316 struct scsi_transport_template t;
317 struct fc_function_template *f;
320 * For attributes : each object has :
321 * An array of the actual attributes structures
322 * An array of null-terminated pointers to the attribute
323 * structures - used for mid-layer interaction.
325 * The attribute containers for the starget and host are are
326 * part of the midlayer. As the remote port is specific to the
327 * fc transport, we must provide the attribute container.
329 struct class_device_attribute private_starget_attrs[
330 FC_STARGET_NUM_ATTRS];
331 struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
333 struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
334 struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
336 struct transport_container rport_attr_cont;
337 struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
338 struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
340 struct transport_container vport_attr_cont;
341 struct class_device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
342 struct class_device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
345 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
347 static int fc_target_setup(struct transport_container *tc, struct device *dev,
348 struct class_device *cdev)
350 struct scsi_target *starget = to_scsi_target(dev);
351 struct fc_rport *rport = starget_to_rport(starget);
354 * if parent is remote port, use values from remote port.
355 * Otherwise, this host uses the fc_transport, but not the
356 * remote port interface. As such, initialize to known non-values.
358 if (rport) {
359 fc_starget_node_name(starget) = rport->node_name;
360 fc_starget_port_name(starget) = rport->port_name;
361 fc_starget_port_id(starget) = rport->port_id;
362 } else {
363 fc_starget_node_name(starget) = -1;
364 fc_starget_port_name(starget) = -1;
365 fc_starget_port_id(starget) = -1;
368 return 0;
371 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
372 "fc_transport",
373 fc_target_setup,
374 NULL,
375 NULL);
377 static int fc_host_setup(struct transport_container *tc, struct device *dev,
378 struct class_device *cdev)
380 struct Scsi_Host *shost = dev_to_shost(dev);
381 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
384 * Set default values easily detected by the midlayer as
385 * failure cases. The scsi lldd is responsible for initializing
386 * all transport attributes to valid values per host.
388 fc_host->node_name = -1;
389 fc_host->port_name = -1;
390 fc_host->permanent_port_name = -1;
391 fc_host->supported_classes = FC_COS_UNSPECIFIED;
392 memset(fc_host->supported_fc4s, 0,
393 sizeof(fc_host->supported_fc4s));
394 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
395 fc_host->maxframe_size = -1;
396 fc_host->max_npiv_vports = 0;
397 memset(fc_host->serial_number, 0,
398 sizeof(fc_host->serial_number));
400 fc_host->port_id = -1;
401 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
402 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
403 memset(fc_host->active_fc4s, 0,
404 sizeof(fc_host->active_fc4s));
405 fc_host->speed = FC_PORTSPEED_UNKNOWN;
406 fc_host->fabric_name = -1;
407 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
408 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
410 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
412 INIT_LIST_HEAD(&fc_host->rports);
413 INIT_LIST_HEAD(&fc_host->rport_bindings);
414 INIT_LIST_HEAD(&fc_host->vports);
415 fc_host->next_rport_number = 0;
416 fc_host->next_target_id = 0;
417 fc_host->next_vport_number = 0;
418 fc_host->npiv_vports_inuse = 0;
420 snprintf(fc_host->work_q_name, KOBJ_NAME_LEN, "fc_wq_%d",
421 shost->host_no);
422 fc_host->work_q = create_singlethread_workqueue(
423 fc_host->work_q_name);
424 if (!fc_host->work_q)
425 return -ENOMEM;
427 snprintf(fc_host->devloss_work_q_name, KOBJ_NAME_LEN, "fc_dl_%d",
428 shost->host_no);
429 fc_host->devloss_work_q = create_singlethread_workqueue(
430 fc_host->devloss_work_q_name);
431 if (!fc_host->devloss_work_q) {
432 destroy_workqueue(fc_host->work_q);
433 fc_host->work_q = NULL;
434 return -ENOMEM;
437 return 0;
440 static DECLARE_TRANSPORT_CLASS(fc_host_class,
441 "fc_host",
442 fc_host_setup,
443 NULL,
444 NULL);
447 * Setup and Remove actions for remote ports are handled
448 * in the service functions below.
450 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
451 "fc_remote_ports",
452 NULL,
453 NULL,
454 NULL);
457 * Setup and Remove actions for virtual ports are handled
458 * in the service functions below.
460 static DECLARE_TRANSPORT_CLASS(fc_vport_class,
461 "fc_vports",
462 NULL,
463 NULL,
464 NULL);
467 * Module Parameters
471 * dev_loss_tmo: the default number of seconds that the FC transport
472 * should insulate the loss of a remote port.
473 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
475 static unsigned int fc_dev_loss_tmo = 60; /* seconds */
477 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
478 MODULE_PARM_DESC(dev_loss_tmo,
479 "Maximum number of seconds that the FC transport should"
480 " insulate the loss of a remote port. Once this value is"
481 " exceeded, the scsi target is removed. Value should be"
482 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
485 * Netlink Infrastructure
488 static atomic_t fc_event_seq;
491 * fc_get_event_number - Obtain the next sequential FC event number
493 * Notes:
494 * We could have inline'd this, but it would have required fc_event_seq to
495 * be exposed. For now, live with the subroutine call.
496 * Atomic used to avoid lock/unlock...
499 fc_get_event_number(void)
501 return atomic_add_return(1, &fc_event_seq);
503 EXPORT_SYMBOL(fc_get_event_number);
507 * fc_host_post_event - called to post an even on an fc_host.
509 * @shost: host the event occurred on
510 * @event_number: fc event number obtained from get_fc_event_number()
511 * @event_code: fc_host event being posted
512 * @event_data: 32bits of data for the event being posted
514 * Notes:
515 * This routine assumes no locks are held on entry.
517 void
518 fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
519 enum fc_host_event_code event_code, u32 event_data)
521 struct sk_buff *skb;
522 struct nlmsghdr *nlh;
523 struct fc_nl_event *event;
524 const char *name;
525 u32 len, skblen;
526 int err;
528 if (!scsi_nl_sock) {
529 err = -ENOENT;
530 goto send_fail;
533 len = FC_NL_MSGALIGN(sizeof(*event));
534 skblen = NLMSG_SPACE(len);
536 skb = alloc_skb(skblen, GFP_KERNEL);
537 if (!skb) {
538 err = -ENOBUFS;
539 goto send_fail;
542 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
543 skblen - sizeof(*nlh), 0);
544 if (!nlh) {
545 err = -ENOBUFS;
546 goto send_fail_skb;
548 event = NLMSG_DATA(nlh);
550 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
551 FC_NL_ASYNC_EVENT, len);
552 event->seconds = get_seconds();
553 event->vendor_id = 0;
554 event->host_no = shost->host_no;
555 event->event_datalen = sizeof(u32); /* bytes */
556 event->event_num = event_number;
557 event->event_code = event_code;
558 event->event_data = event_data;
560 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
561 GFP_KERNEL);
562 if (err && (err != -ESRCH)) /* filter no recipient errors */
563 /* nlmsg_multicast already kfree_skb'd */
564 goto send_fail;
566 return;
568 send_fail_skb:
569 kfree_skb(skb);
570 send_fail:
571 name = get_fc_host_event_code_name(event_code);
572 printk(KERN_WARNING
573 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
574 __FUNCTION__, shost->host_no,
575 (name) ? name : "<unknown>", event_data, err);
576 return;
578 EXPORT_SYMBOL(fc_host_post_event);
582 * fc_host_post_vendor_event - called to post a vendor unique event on
583 * a fc_host
585 * @shost: host the event occurred on
586 * @event_number: fc event number obtained from get_fc_event_number()
587 * @data_len: amount, in bytes, of vendor unique data
588 * @data_buf: pointer to vendor unique data
590 * Notes:
591 * This routine assumes no locks are held on entry.
593 void
594 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
595 u32 data_len, char * data_buf, u64 vendor_id)
597 struct sk_buff *skb;
598 struct nlmsghdr *nlh;
599 struct fc_nl_event *event;
600 u32 len, skblen;
601 int err;
603 if (!scsi_nl_sock) {
604 err = -ENOENT;
605 goto send_vendor_fail;
608 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
609 skblen = NLMSG_SPACE(len);
611 skb = alloc_skb(skblen, GFP_KERNEL);
612 if (!skb) {
613 err = -ENOBUFS;
614 goto send_vendor_fail;
617 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
618 skblen - sizeof(*nlh), 0);
619 if (!nlh) {
620 err = -ENOBUFS;
621 goto send_vendor_fail_skb;
623 event = NLMSG_DATA(nlh);
625 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
626 FC_NL_ASYNC_EVENT, len);
627 event->seconds = get_seconds();
628 event->vendor_id = vendor_id;
629 event->host_no = shost->host_no;
630 event->event_datalen = data_len; /* bytes */
631 event->event_num = event_number;
632 event->event_code = FCH_EVT_VENDOR_UNIQUE;
633 memcpy(&event->event_data, data_buf, data_len);
635 err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
636 GFP_KERNEL);
637 if (err && (err != -ESRCH)) /* filter no recipient errors */
638 /* nlmsg_multicast already kfree_skb'd */
639 goto send_vendor_fail;
641 return;
643 send_vendor_fail_skb:
644 kfree_skb(skb);
645 send_vendor_fail:
646 printk(KERN_WARNING
647 "%s: Dropped Event : host %d vendor_unique - err %d\n",
648 __FUNCTION__, shost->host_no, err);
649 return;
651 EXPORT_SYMBOL(fc_host_post_vendor_event);
655 static __init int fc_transport_init(void)
657 int error;
659 atomic_set(&fc_event_seq, 0);
661 error = transport_class_register(&fc_host_class);
662 if (error)
663 return error;
664 error = transport_class_register(&fc_vport_class);
665 if (error)
666 return error;
667 error = transport_class_register(&fc_rport_class);
668 if (error)
669 return error;
670 return transport_class_register(&fc_transport_class);
673 static void __exit fc_transport_exit(void)
675 transport_class_unregister(&fc_transport_class);
676 transport_class_unregister(&fc_rport_class);
677 transport_class_unregister(&fc_host_class);
678 transport_class_unregister(&fc_vport_class);
682 * FC Remote Port Attribute Management
685 #define fc_rport_show_function(field, format_string, sz, cast) \
686 static ssize_t \
687 show_fc_rport_##field (struct class_device *cdev, char *buf) \
689 struct fc_rport *rport = transport_class_to_rport(cdev); \
690 struct Scsi_Host *shost = rport_to_shost(rport); \
691 struct fc_internal *i = to_fc_internal(shost->transportt); \
692 if ((i->f->get_rport_##field) && \
693 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
694 (rport->port_state == FC_PORTSTATE_DELETED) || \
695 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
696 i->f->get_rport_##field(rport); \
697 return snprintf(buf, sz, format_string, cast rport->field); \
700 #define fc_rport_store_function(field) \
701 static ssize_t \
702 store_fc_rport_##field(struct class_device *cdev, const char *buf, \
703 size_t count) \
705 int val; \
706 struct fc_rport *rport = transport_class_to_rport(cdev); \
707 struct Scsi_Host *shost = rport_to_shost(rport); \
708 struct fc_internal *i = to_fc_internal(shost->transportt); \
709 char *cp; \
710 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
711 (rport->port_state == FC_PORTSTATE_DELETED) || \
712 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
713 return -EBUSY; \
714 val = simple_strtoul(buf, &cp, 0); \
715 if (*cp && (*cp != '\n')) \
716 return -EINVAL; \
717 i->f->set_rport_##field(rport, val); \
718 return count; \
721 #define fc_rport_rd_attr(field, format_string, sz) \
722 fc_rport_show_function(field, format_string, sz, ) \
723 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
724 show_fc_rport_##field, NULL)
726 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
727 fc_rport_show_function(field, format_string, sz, (cast)) \
728 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
729 show_fc_rport_##field, NULL)
731 #define fc_rport_rw_attr(field, format_string, sz) \
732 fc_rport_show_function(field, format_string, sz, ) \
733 fc_rport_store_function(field) \
734 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
735 show_fc_rport_##field, \
736 store_fc_rport_##field)
739 #define fc_private_rport_show_function(field, format_string, sz, cast) \
740 static ssize_t \
741 show_fc_rport_##field (struct class_device *cdev, char *buf) \
743 struct fc_rport *rport = transport_class_to_rport(cdev); \
744 return snprintf(buf, sz, format_string, cast rport->field); \
747 #define fc_private_rport_rd_attr(field, format_string, sz) \
748 fc_private_rport_show_function(field, format_string, sz, ) \
749 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
750 show_fc_rport_##field, NULL)
752 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
753 fc_private_rport_show_function(field, format_string, sz, (cast)) \
754 static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
755 show_fc_rport_##field, NULL)
758 #define fc_private_rport_rd_enum_attr(title, maxlen) \
759 static ssize_t \
760 show_fc_rport_##title (struct class_device *cdev, char *buf) \
762 struct fc_rport *rport = transport_class_to_rport(cdev); \
763 const char *name; \
764 name = get_fc_##title##_name(rport->title); \
765 if (!name) \
766 return -EINVAL; \
767 return snprintf(buf, maxlen, "%s\n", name); \
769 static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
770 show_fc_rport_##title, NULL)
773 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
774 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
775 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
776 i->private_rport_attrs[count].store = NULL; \
777 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
778 if (i->f->show_rport_##field) \
779 count++
781 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
782 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
783 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
784 i->private_rport_attrs[count].store = NULL; \
785 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
786 count++
788 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
789 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
790 if (!i->f->set_rport_##field) { \
791 i->private_rport_attrs[count].attr.mode = S_IRUGO; \
792 i->private_rport_attrs[count].store = NULL; \
794 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
795 if (i->f->show_rport_##field) \
796 count++
798 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
800 i->private_rport_attrs[count] = class_device_attr_rport_##field; \
801 i->rport_attrs[count] = &i->private_rport_attrs[count]; \
802 count++; \
806 /* The FC Transport Remote Port Attributes: */
808 /* Fixed Remote Port Attributes */
810 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
812 static ssize_t
813 show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
815 struct fc_rport *rport = transport_class_to_rport(cdev);
816 if (rport->supported_classes == FC_COS_UNSPECIFIED)
817 return snprintf(buf, 20, "unspecified\n");
818 return get_fc_cos_names(rport->supported_classes, buf);
820 static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
821 show_fc_rport_supported_classes, NULL);
823 /* Dynamic Remote Port Attributes */
826 * dev_loss_tmo attribute
828 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
829 static ssize_t
830 store_fc_rport_dev_loss_tmo(struct class_device *cdev, const char *buf,
831 size_t count)
833 int val;
834 struct fc_rport *rport = transport_class_to_rport(cdev);
835 struct Scsi_Host *shost = rport_to_shost(rport);
836 struct fc_internal *i = to_fc_internal(shost->transportt);
837 char *cp;
838 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
839 (rport->port_state == FC_PORTSTATE_DELETED) ||
840 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
841 return -EBUSY;
842 val = simple_strtoul(buf, &cp, 0);
843 if ((*cp && (*cp != '\n')) ||
844 (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
845 return -EINVAL;
846 i->f->set_rport_dev_loss_tmo(rport, val);
847 return count;
849 static FC_CLASS_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
850 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
853 /* Private Remote Port Attributes */
855 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
856 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
857 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
859 static ssize_t
860 show_fc_rport_roles (struct class_device *cdev, char *buf)
862 struct fc_rport *rport = transport_class_to_rport(cdev);
864 /* identify any roles that are port_id specific */
865 if ((rport->port_id != -1) &&
866 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
867 FC_WELLKNOWN_PORTID_MASK) {
868 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
869 case FC_FPORT_PORTID:
870 return snprintf(buf, 30, "Fabric Port\n");
871 case FC_FABCTLR_PORTID:
872 return snprintf(buf, 30, "Fabric Controller\n");
873 case FC_DIRSRVR_PORTID:
874 return snprintf(buf, 30, "Directory Server\n");
875 case FC_TIMESRVR_PORTID:
876 return snprintf(buf, 30, "Time Server\n");
877 case FC_MGMTSRVR_PORTID:
878 return snprintf(buf, 30, "Management Server\n");
879 default:
880 return snprintf(buf, 30, "Unknown Fabric Entity\n");
882 } else {
883 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
884 return snprintf(buf, 20, "unknown\n");
885 return get_fc_port_roles_names(rport->roles, buf);
888 static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
889 show_fc_rport_roles, NULL);
891 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
892 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
895 * fast_io_fail_tmo attribute
897 static ssize_t
898 show_fc_rport_fast_io_fail_tmo (struct class_device *cdev, char *buf)
900 struct fc_rport *rport = transport_class_to_rport(cdev);
902 if (rport->fast_io_fail_tmo == -1)
903 return snprintf(buf, 5, "off\n");
904 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
907 static ssize_t
908 store_fc_rport_fast_io_fail_tmo(struct class_device *cdev, const char *buf,
909 size_t count)
911 int val;
912 char *cp;
913 struct fc_rport *rport = transport_class_to_rport(cdev);
915 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
916 (rport->port_state == FC_PORTSTATE_DELETED) ||
917 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
918 return -EBUSY;
919 if (strncmp(buf, "off", 3) == 0)
920 rport->fast_io_fail_tmo = -1;
921 else {
922 val = simple_strtoul(buf, &cp, 0);
923 if ((*cp && (*cp != '\n')) ||
924 (val < 0) || (val >= rport->dev_loss_tmo))
925 return -EINVAL;
926 rport->fast_io_fail_tmo = val;
928 return count;
930 static FC_CLASS_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
931 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
935 * FC SCSI Target Attribute Management
939 * Note: in the target show function we recognize when the remote
940 * port is in the heirarchy and do not allow the driver to get
941 * involved in sysfs functions. The driver only gets involved if
942 * it's the "old" style that doesn't use rports.
944 #define fc_starget_show_function(field, format_string, sz, cast) \
945 static ssize_t \
946 show_fc_starget_##field (struct class_device *cdev, char *buf) \
948 struct scsi_target *starget = transport_class_to_starget(cdev); \
949 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
950 struct fc_internal *i = to_fc_internal(shost->transportt); \
951 struct fc_rport *rport = starget_to_rport(starget); \
952 if (rport) \
953 fc_starget_##field(starget) = rport->field; \
954 else if (i->f->get_starget_##field) \
955 i->f->get_starget_##field(starget); \
956 return snprintf(buf, sz, format_string, \
957 cast fc_starget_##field(starget)); \
960 #define fc_starget_rd_attr(field, format_string, sz) \
961 fc_starget_show_function(field, format_string, sz, ) \
962 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
963 show_fc_starget_##field, NULL)
965 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
966 fc_starget_show_function(field, format_string, sz, (cast)) \
967 static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
968 show_fc_starget_##field, NULL)
970 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
971 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
972 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
973 i->private_starget_attrs[count].store = NULL; \
974 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
975 if (i->f->show_starget_##field) \
976 count++
978 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
979 i->private_starget_attrs[count] = class_device_attr_starget_##field; \
980 if (!i->f->set_starget_##field) { \
981 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
982 i->private_starget_attrs[count].store = NULL; \
984 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
985 if (i->f->show_starget_##field) \
986 count++
988 /* The FC Transport SCSI Target Attributes: */
989 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
990 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
991 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
995 * FC Virtual Port Attribute Management
998 #define fc_vport_show_function(field, format_string, sz, cast) \
999 static ssize_t \
1000 show_fc_vport_##field (struct class_device *cdev, char *buf) \
1002 struct fc_vport *vport = transport_class_to_vport(cdev); \
1003 struct Scsi_Host *shost = vport_to_shost(vport); \
1004 struct fc_internal *i = to_fc_internal(shost->transportt); \
1005 if ((i->f->get_vport_##field) && \
1006 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1007 i->f->get_vport_##field(vport); \
1008 return snprintf(buf, sz, format_string, cast vport->field); \
1011 #define fc_vport_store_function(field) \
1012 static ssize_t \
1013 store_fc_vport_##field(struct class_device *cdev, const char *buf, \
1014 size_t count) \
1016 int val; \
1017 struct fc_vport *vport = transport_class_to_vport(cdev); \
1018 struct Scsi_Host *shost = vport_to_shost(vport); \
1019 struct fc_internal *i = to_fc_internal(shost->transportt); \
1020 char *cp; \
1021 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1022 return -EBUSY; \
1023 val = simple_strtoul(buf, &cp, 0); \
1024 if (*cp && (*cp != '\n')) \
1025 return -EINVAL; \
1026 i->f->set_vport_##field(vport, val); \
1027 return count; \
1030 #define fc_vport_store_str_function(field, slen) \
1031 static ssize_t \
1032 store_fc_vport_##field(struct class_device *cdev, const char *buf, \
1033 size_t count) \
1035 struct fc_vport *vport = transport_class_to_vport(cdev); \
1036 struct Scsi_Host *shost = vport_to_shost(vport); \
1037 struct fc_internal *i = to_fc_internal(shost->transportt); \
1038 unsigned int cnt=count; \
1040 /* count may include a LF at end of string */ \
1041 if (buf[cnt-1] == '\n') \
1042 cnt--; \
1043 if (cnt > ((slen) - 1)) \
1044 return -EINVAL; \
1045 memcpy(vport->field, buf, cnt); \
1046 i->f->set_vport_##field(vport); \
1047 return count; \
1050 #define fc_vport_rd_attr(field, format_string, sz) \
1051 fc_vport_show_function(field, format_string, sz, ) \
1052 static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO, \
1053 show_fc_vport_##field, NULL)
1055 #define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1056 fc_vport_show_function(field, format_string, sz, (cast)) \
1057 static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO, \
1058 show_fc_vport_##field, NULL)
1060 #define fc_vport_rw_attr(field, format_string, sz) \
1061 fc_vport_show_function(field, format_string, sz, ) \
1062 fc_vport_store_function(field) \
1063 static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1064 show_fc_vport_##field, \
1065 store_fc_vport_##field)
1067 #define fc_private_vport_show_function(field, format_string, sz, cast) \
1068 static ssize_t \
1069 show_fc_vport_##field (struct class_device *cdev, char *buf) \
1071 struct fc_vport *vport = transport_class_to_vport(cdev); \
1072 return snprintf(buf, sz, format_string, cast vport->field); \
1075 #define fc_private_vport_store_u32_function(field) \
1076 static ssize_t \
1077 store_fc_vport_##field(struct class_device *cdev, const char *buf, \
1078 size_t count) \
1080 u32 val; \
1081 struct fc_vport *vport = transport_class_to_vport(cdev); \
1082 char *cp; \
1083 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1084 return -EBUSY; \
1085 val = simple_strtoul(buf, &cp, 0); \
1086 if (*cp && (*cp != '\n')) \
1087 return -EINVAL; \
1088 vport->field = val; \
1089 return count; \
1093 #define fc_private_vport_rd_attr(field, format_string, sz) \
1094 fc_private_vport_show_function(field, format_string, sz, ) \
1095 static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO, \
1096 show_fc_vport_##field, NULL)
1098 #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1099 fc_private_vport_show_function(field, format_string, sz, (cast)) \
1100 static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO, \
1101 show_fc_vport_##field, NULL)
1103 #define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1104 fc_private_vport_show_function(field, format_string, sz, ) \
1105 fc_private_vport_store_u32_function(field) \
1106 static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1107 show_fc_vport_##field, \
1108 store_fc_vport_##field)
1111 #define fc_private_vport_rd_enum_attr(title, maxlen) \
1112 static ssize_t \
1113 show_fc_vport_##title (struct class_device *cdev, char *buf) \
1115 struct fc_vport *vport = transport_class_to_vport(cdev); \
1116 const char *name; \
1117 name = get_fc_##title##_name(vport->title); \
1118 if (!name) \
1119 return -EINVAL; \
1120 return snprintf(buf, maxlen, "%s\n", name); \
1122 static FC_CLASS_DEVICE_ATTR(vport, title, S_IRUGO, \
1123 show_fc_vport_##title, NULL)
1126 #define SETUP_VPORT_ATTRIBUTE_RD(field) \
1127 i->private_vport_attrs[count] = class_device_attr_vport_##field; \
1128 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1129 i->private_vport_attrs[count].store = NULL; \
1130 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1131 if (i->f->get_##field) \
1132 count++
1133 /* NOTE: Above MACRO differs: checks function not show bit */
1135 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
1136 i->private_vport_attrs[count] = class_device_attr_vport_##field; \
1137 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1138 i->private_vport_attrs[count].store = NULL; \
1139 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1140 count++
1142 #define SETUP_VPORT_ATTRIBUTE_WR(field) \
1143 i->private_vport_attrs[count] = class_device_attr_vport_##field; \
1144 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1145 if (i->f->field) \
1146 count++
1147 /* NOTE: Above MACRO differs: checks function */
1149 #define SETUP_VPORT_ATTRIBUTE_RW(field) \
1150 i->private_vport_attrs[count] = class_device_attr_vport_##field; \
1151 if (!i->f->set_vport_##field) { \
1152 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1153 i->private_vport_attrs[count].store = NULL; \
1155 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1156 count++
1157 /* NOTE: Above MACRO differs: does not check show bit */
1159 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1161 i->private_vport_attrs[count] = class_device_attr_vport_##field; \
1162 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1163 count++; \
1167 /* The FC Transport Virtual Port Attributes: */
1169 /* Fixed Virtual Port Attributes */
1171 /* Dynamic Virtual Port Attributes */
1173 /* Private Virtual Port Attributes */
1175 fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1176 fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1177 fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1178 fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1180 static ssize_t
1181 show_fc_vport_roles (struct class_device *cdev, char *buf)
1183 struct fc_vport *vport = transport_class_to_vport(cdev);
1185 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1186 return snprintf(buf, 20, "unknown\n");
1187 return get_fc_port_roles_names(vport->roles, buf);
1189 static FC_CLASS_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1191 fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1193 fc_private_vport_show_function(symbolic_name, "%s\n",
1194 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1195 fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1196 static FC_CLASS_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1197 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1199 static ssize_t
1200 store_fc_vport_delete(struct class_device *cdev, const char *buf,
1201 size_t count)
1203 struct fc_vport *vport = transport_class_to_vport(cdev);
1204 struct Scsi_Host *shost = vport_to_shost(vport);
1206 fc_queue_work(shost, &vport->vport_delete_work);
1207 return count;
1209 static FC_CLASS_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1210 NULL, store_fc_vport_delete);
1214 * Enable/Disable vport
1215 * Write "1" to disable, write "0" to enable
1217 static ssize_t
1218 store_fc_vport_disable(struct class_device *cdev, const char *buf,
1219 size_t count)
1221 struct fc_vport *vport = transport_class_to_vport(cdev);
1222 struct Scsi_Host *shost = vport_to_shost(vport);
1223 struct fc_internal *i = to_fc_internal(shost->transportt);
1224 int stat;
1226 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1227 return -EBUSY;
1229 if (*buf == '0') {
1230 if (vport->vport_state != FC_VPORT_DISABLED)
1231 return -EALREADY;
1232 } else if (*buf == '1') {
1233 if (vport->vport_state == FC_VPORT_DISABLED)
1234 return -EALREADY;
1235 } else
1236 return -EINVAL;
1238 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1239 return stat ? stat : count;
1241 static FC_CLASS_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1242 NULL, store_fc_vport_disable);
1246 * Host Attribute Management
1249 #define fc_host_show_function(field, format_string, sz, cast) \
1250 static ssize_t \
1251 show_fc_host_##field (struct class_device *cdev, char *buf) \
1253 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1254 struct fc_internal *i = to_fc_internal(shost->transportt); \
1255 if (i->f->get_host_##field) \
1256 i->f->get_host_##field(shost); \
1257 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1260 #define fc_host_store_function(field) \
1261 static ssize_t \
1262 store_fc_host_##field(struct class_device *cdev, const char *buf, \
1263 size_t count) \
1265 int val; \
1266 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1267 struct fc_internal *i = to_fc_internal(shost->transportt); \
1268 char *cp; \
1270 val = simple_strtoul(buf, &cp, 0); \
1271 if (*cp && (*cp != '\n')) \
1272 return -EINVAL; \
1273 i->f->set_host_##field(shost, val); \
1274 return count; \
1277 #define fc_host_store_str_function(field, slen) \
1278 static ssize_t \
1279 store_fc_host_##field(struct class_device *cdev, const char *buf, \
1280 size_t count) \
1282 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1283 struct fc_internal *i = to_fc_internal(shost->transportt); \
1284 unsigned int cnt=count; \
1286 /* count may include a LF at end of string */ \
1287 if (buf[cnt-1] == '\n') \
1288 cnt--; \
1289 if (cnt > ((slen) - 1)) \
1290 return -EINVAL; \
1291 memcpy(fc_host_##field(shost), buf, cnt); \
1292 i->f->set_host_##field(shost); \
1293 return count; \
1296 #define fc_host_rd_attr(field, format_string, sz) \
1297 fc_host_show_function(field, format_string, sz, ) \
1298 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1299 show_fc_host_##field, NULL)
1301 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1302 fc_host_show_function(field, format_string, sz, (cast)) \
1303 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1304 show_fc_host_##field, NULL)
1306 #define fc_host_rw_attr(field, format_string, sz) \
1307 fc_host_show_function(field, format_string, sz, ) \
1308 fc_host_store_function(field) \
1309 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
1310 show_fc_host_##field, \
1311 store_fc_host_##field)
1313 #define fc_host_rd_enum_attr(title, maxlen) \
1314 static ssize_t \
1315 show_fc_host_##title (struct class_device *cdev, char *buf) \
1317 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1318 struct fc_internal *i = to_fc_internal(shost->transportt); \
1319 const char *name; \
1320 if (i->f->get_host_##title) \
1321 i->f->get_host_##title(shost); \
1322 name = get_fc_##title##_name(fc_host_##title(shost)); \
1323 if (!name) \
1324 return -EINVAL; \
1325 return snprintf(buf, maxlen, "%s\n", name); \
1327 static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1329 #define SETUP_HOST_ATTRIBUTE_RD(field) \
1330 i->private_host_attrs[count] = class_device_attr_host_##field; \
1331 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1332 i->private_host_attrs[count].store = NULL; \
1333 i->host_attrs[count] = &i->private_host_attrs[count]; \
1334 if (i->f->show_host_##field) \
1335 count++
1337 #define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
1338 i->private_host_attrs[count] = class_device_attr_host_##field; \
1339 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1340 i->private_host_attrs[count].store = NULL; \
1341 i->host_attrs[count] = &i->private_host_attrs[count]; \
1342 count++
1344 #define SETUP_HOST_ATTRIBUTE_RW(field) \
1345 i->private_host_attrs[count] = class_device_attr_host_##field; \
1346 if (!i->f->set_host_##field) { \
1347 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1348 i->private_host_attrs[count].store = NULL; \
1350 i->host_attrs[count] = &i->private_host_attrs[count]; \
1351 if (i->f->show_host_##field) \
1352 count++
1355 #define fc_private_host_show_function(field, format_string, sz, cast) \
1356 static ssize_t \
1357 show_fc_host_##field (struct class_device *cdev, char *buf) \
1359 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1360 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1363 #define fc_private_host_rd_attr(field, format_string, sz) \
1364 fc_private_host_show_function(field, format_string, sz, ) \
1365 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1366 show_fc_host_##field, NULL)
1368 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1369 fc_private_host_show_function(field, format_string, sz, (cast)) \
1370 static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
1371 show_fc_host_##field, NULL)
1373 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
1374 i->private_host_attrs[count] = class_device_attr_host_##field; \
1375 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1376 i->private_host_attrs[count].store = NULL; \
1377 i->host_attrs[count] = &i->private_host_attrs[count]; \
1378 count++
1380 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
1382 i->private_host_attrs[count] = class_device_attr_host_##field; \
1383 i->host_attrs[count] = &i->private_host_attrs[count]; \
1384 count++; \
1388 /* Fixed Host Attributes */
1390 static ssize_t
1391 show_fc_host_supported_classes (struct class_device *cdev, char *buf)
1393 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1395 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1396 return snprintf(buf, 20, "unspecified\n");
1398 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1400 static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1401 show_fc_host_supported_classes, NULL);
1403 static ssize_t
1404 show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
1406 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1407 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1409 static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1410 show_fc_host_supported_fc4s, NULL);
1412 static ssize_t
1413 show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
1415 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1417 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1418 return snprintf(buf, 20, "unknown\n");
1420 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1422 static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1423 show_fc_host_supported_speeds, NULL);
1426 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1427 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1428 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1429 unsigned long long);
1430 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1431 fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
1432 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1435 /* Dynamic Host Attributes */
1437 static ssize_t
1438 show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
1440 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1441 struct fc_internal *i = to_fc_internal(shost->transportt);
1443 if (i->f->get_host_active_fc4s)
1444 i->f->get_host_active_fc4s(shost);
1446 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1448 static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1449 show_fc_host_active_fc4s, NULL);
1451 static ssize_t
1452 show_fc_host_speed (struct class_device *cdev, char *buf)
1454 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1455 struct fc_internal *i = to_fc_internal(shost->transportt);
1457 if (i->f->get_host_speed)
1458 i->f->get_host_speed(shost);
1460 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1461 return snprintf(buf, 20, "unknown\n");
1463 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1465 static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
1466 show_fc_host_speed, NULL);
1469 fc_host_rd_attr(port_id, "0x%06x\n", 20);
1470 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1471 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1472 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1473 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1475 fc_private_host_show_function(system_hostname, "%s\n",
1476 FC_SYMBOLIC_NAME_SIZE + 1, )
1477 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1478 static FC_CLASS_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1479 show_fc_host_system_hostname, store_fc_host_system_hostname);
1482 /* Private Host Attributes */
1484 static ssize_t
1485 show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
1487 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1488 const char *name;
1490 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1491 if (!name)
1492 return -EINVAL;
1493 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1496 #define get_list_head_entry(pos, head, member) \
1497 pos = list_entry((head)->next, typeof(*pos), member)
1499 static ssize_t
1500 store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
1501 const char *buf, size_t count)
1503 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1504 struct fc_rport *rport;
1505 enum fc_tgtid_binding_type val;
1506 unsigned long flags;
1508 if (get_fc_tgtid_bind_type_match(buf, &val))
1509 return -EINVAL;
1511 /* if changing bind type, purge all unused consistent bindings */
1512 if (val != fc_host_tgtid_bind_type(shost)) {
1513 spin_lock_irqsave(shost->host_lock, flags);
1514 while (!list_empty(&fc_host_rport_bindings(shost))) {
1515 get_list_head_entry(rport,
1516 &fc_host_rport_bindings(shost), peers);
1517 list_del(&rport->peers);
1518 rport->port_state = FC_PORTSTATE_DELETED;
1519 fc_queue_work(shost, &rport->rport_delete_work);
1521 spin_unlock_irqrestore(shost->host_lock, flags);
1524 fc_host_tgtid_bind_type(shost) = val;
1525 return count;
1528 static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1529 show_fc_private_host_tgtid_bind_type,
1530 store_fc_private_host_tgtid_bind_type);
1532 static ssize_t
1533 store_fc_private_host_issue_lip(struct class_device *cdev,
1534 const char *buf, size_t count)
1536 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1537 struct fc_internal *i = to_fc_internal(shost->transportt);
1538 int ret;
1540 /* ignore any data value written to the attribute */
1541 if (i->f->issue_fc_host_lip) {
1542 ret = i->f->issue_fc_host_lip(shost);
1543 return ret ? ret: count;
1546 return -ENOENT;
1549 static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
1550 store_fc_private_host_issue_lip);
1552 fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1556 * Host Statistics Management
1559 /* Show a given an attribute in the statistics group */
1560 static ssize_t
1561 fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
1563 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1564 struct fc_internal *i = to_fc_internal(shost->transportt);
1565 struct fc_host_statistics *stats;
1566 ssize_t ret = -ENOENT;
1568 if (offset > sizeof(struct fc_host_statistics) ||
1569 offset % sizeof(u64) != 0)
1570 WARN_ON(1);
1572 if (i->f->get_fc_host_stats) {
1573 stats = (i->f->get_fc_host_stats)(shost);
1574 if (stats)
1575 ret = snprintf(buf, 20, "0x%llx\n",
1576 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1578 return ret;
1582 /* generate a read-only statistics attribute */
1583 #define fc_host_statistic(name) \
1584 static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
1586 return fc_stat_show(cd, buf, \
1587 offsetof(struct fc_host_statistics, name)); \
1589 static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1591 fc_host_statistic(seconds_since_last_reset);
1592 fc_host_statistic(tx_frames);
1593 fc_host_statistic(tx_words);
1594 fc_host_statistic(rx_frames);
1595 fc_host_statistic(rx_words);
1596 fc_host_statistic(lip_count);
1597 fc_host_statistic(nos_count);
1598 fc_host_statistic(error_frames);
1599 fc_host_statistic(dumped_frames);
1600 fc_host_statistic(link_failure_count);
1601 fc_host_statistic(loss_of_sync_count);
1602 fc_host_statistic(loss_of_signal_count);
1603 fc_host_statistic(prim_seq_protocol_err_count);
1604 fc_host_statistic(invalid_tx_word_count);
1605 fc_host_statistic(invalid_crc_count);
1606 fc_host_statistic(fcp_input_requests);
1607 fc_host_statistic(fcp_output_requests);
1608 fc_host_statistic(fcp_control_requests);
1609 fc_host_statistic(fcp_input_megabytes);
1610 fc_host_statistic(fcp_output_megabytes);
1612 static ssize_t
1613 fc_reset_statistics(struct class_device *cdev, const char *buf,
1614 size_t count)
1616 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1617 struct fc_internal *i = to_fc_internal(shost->transportt);
1619 /* ignore any data value written to the attribute */
1620 if (i->f->reset_fc_host_stats) {
1621 i->f->reset_fc_host_stats(shost);
1622 return count;
1625 return -ENOENT;
1627 static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1628 fc_reset_statistics);
1630 static struct attribute *fc_statistics_attrs[] = {
1631 &class_device_attr_host_seconds_since_last_reset.attr,
1632 &class_device_attr_host_tx_frames.attr,
1633 &class_device_attr_host_tx_words.attr,
1634 &class_device_attr_host_rx_frames.attr,
1635 &class_device_attr_host_rx_words.attr,
1636 &class_device_attr_host_lip_count.attr,
1637 &class_device_attr_host_nos_count.attr,
1638 &class_device_attr_host_error_frames.attr,
1639 &class_device_attr_host_dumped_frames.attr,
1640 &class_device_attr_host_link_failure_count.attr,
1641 &class_device_attr_host_loss_of_sync_count.attr,
1642 &class_device_attr_host_loss_of_signal_count.attr,
1643 &class_device_attr_host_prim_seq_protocol_err_count.attr,
1644 &class_device_attr_host_invalid_tx_word_count.attr,
1645 &class_device_attr_host_invalid_crc_count.attr,
1646 &class_device_attr_host_fcp_input_requests.attr,
1647 &class_device_attr_host_fcp_output_requests.attr,
1648 &class_device_attr_host_fcp_control_requests.attr,
1649 &class_device_attr_host_fcp_input_megabytes.attr,
1650 &class_device_attr_host_fcp_output_megabytes.attr,
1651 &class_device_attr_host_reset_statistics.attr,
1652 NULL
1655 static struct attribute_group fc_statistics_group = {
1656 .name = "statistics",
1657 .attrs = fc_statistics_attrs,
1661 /* Host Vport Attributes */
1663 static int
1664 fc_parse_wwn(const char *ns, u64 *nm)
1666 unsigned int i, j;
1667 u8 wwn[8];
1669 memset(wwn, 0, sizeof(wwn));
1671 /* Validate and store the new name */
1672 for (i=0, j=0; i < 16; i++) {
1673 if ((*ns >= 'a') && (*ns <= 'f'))
1674 j = ((j << 4) | ((*ns++ -'a') + 10));
1675 else if ((*ns >= 'A') && (*ns <= 'F'))
1676 j = ((j << 4) | ((*ns++ -'A') + 10));
1677 else if ((*ns >= '0') && (*ns <= '9'))
1678 j = ((j << 4) | (*ns++ -'0'));
1679 else
1680 return -EINVAL;
1681 if (i % 2) {
1682 wwn[i/2] = j & 0xff;
1683 j = 0;
1687 *nm = wwn_to_u64(wwn);
1689 return 0;
1694 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1695 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1696 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1697 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1699 static ssize_t
1700 store_fc_host_vport_create(struct class_device *cdev, const char *buf,
1701 size_t count)
1703 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1704 struct fc_vport_identifiers vid;
1705 struct fc_vport *vport;
1706 unsigned int cnt=count;
1707 int stat;
1709 memset(&vid, 0, sizeof(vid));
1711 /* count may include a LF at end of string */
1712 if (buf[cnt-1] == '\n')
1713 cnt--;
1715 /* validate we have enough characters for WWPN */
1716 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1717 return -EINVAL;
1719 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1720 if (stat)
1721 return stat;
1723 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1724 if (stat)
1725 return stat;
1727 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1728 vid.vport_type = FC_PORTTYPE_NPIV;
1729 /* vid.symbolic_name is already zero/NULL's */
1730 vid.disable = false; /* always enabled */
1732 /* we only allow support on Channel 0 !!! */
1733 stat = fc_vport_create(shost, 0, &shost->shost_gendev, &vid, &vport);
1734 return stat ? stat : count;
1736 static FC_CLASS_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
1737 store_fc_host_vport_create);
1741 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1742 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1743 * The WWNs are specified as hex characters, and may *not* contain
1744 * any prefixes (e.g. 0x, x, etc)
1746 static ssize_t
1747 store_fc_host_vport_delete(struct class_device *cdev, const char *buf,
1748 size_t count)
1750 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1751 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1752 struct fc_vport *vport;
1753 u64 wwpn, wwnn;
1754 unsigned long flags;
1755 unsigned int cnt=count;
1756 int stat, match;
1758 /* count may include a LF at end of string */
1759 if (buf[cnt-1] == '\n')
1760 cnt--;
1762 /* validate we have enough characters for WWPN */
1763 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1764 return -EINVAL;
1766 stat = fc_parse_wwn(&buf[0], &wwpn);
1767 if (stat)
1768 return stat;
1770 stat = fc_parse_wwn(&buf[17], &wwnn);
1771 if (stat)
1772 return stat;
1774 spin_lock_irqsave(shost->host_lock, flags);
1775 match = 0;
1776 /* we only allow support on Channel 0 !!! */
1777 list_for_each_entry(vport, &fc_host->vports, peers) {
1778 if ((vport->channel == 0) &&
1779 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1780 match = 1;
1781 break;
1784 spin_unlock_irqrestore(shost->host_lock, flags);
1786 if (!match)
1787 return -ENODEV;
1789 stat = fc_vport_terminate(vport);
1790 return stat ? stat : count;
1792 static FC_CLASS_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
1793 store_fc_host_vport_delete);
1796 static int fc_host_match(struct attribute_container *cont,
1797 struct device *dev)
1799 struct Scsi_Host *shost;
1800 struct fc_internal *i;
1802 if (!scsi_is_host_device(dev))
1803 return 0;
1805 shost = dev_to_shost(dev);
1806 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1807 != &fc_host_class.class)
1808 return 0;
1810 i = to_fc_internal(shost->transportt);
1812 return &i->t.host_attrs.ac == cont;
1815 static int fc_target_match(struct attribute_container *cont,
1816 struct device *dev)
1818 struct Scsi_Host *shost;
1819 struct fc_internal *i;
1821 if (!scsi_is_target_device(dev))
1822 return 0;
1824 shost = dev_to_shost(dev->parent);
1825 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1826 != &fc_host_class.class)
1827 return 0;
1829 i = to_fc_internal(shost->transportt);
1831 return &i->t.target_attrs.ac == cont;
1834 static void fc_rport_dev_release(struct device *dev)
1836 struct fc_rport *rport = dev_to_rport(dev);
1837 put_device(dev->parent);
1838 kfree(rport);
1841 int scsi_is_fc_rport(const struct device *dev)
1843 return dev->release == fc_rport_dev_release;
1845 EXPORT_SYMBOL(scsi_is_fc_rport);
1847 static int fc_rport_match(struct attribute_container *cont,
1848 struct device *dev)
1850 struct Scsi_Host *shost;
1851 struct fc_internal *i;
1853 if (!scsi_is_fc_rport(dev))
1854 return 0;
1856 shost = dev_to_shost(dev->parent);
1857 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1858 != &fc_host_class.class)
1859 return 0;
1861 i = to_fc_internal(shost->transportt);
1863 return &i->rport_attr_cont.ac == cont;
1867 static void fc_vport_dev_release(struct device *dev)
1869 struct fc_vport *vport = dev_to_vport(dev);
1870 put_device(dev->parent); /* release kobj parent */
1871 kfree(vport);
1874 int scsi_is_fc_vport(const struct device *dev)
1876 return dev->release == fc_vport_dev_release;
1878 EXPORT_SYMBOL(scsi_is_fc_vport);
1880 static int fc_vport_match(struct attribute_container *cont,
1881 struct device *dev)
1883 struct fc_vport *vport;
1884 struct Scsi_Host *shost;
1885 struct fc_internal *i;
1887 if (!scsi_is_fc_vport(dev))
1888 return 0;
1889 vport = dev_to_vport(dev);
1891 shost = vport_to_shost(vport);
1892 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1893 != &fc_host_class.class)
1894 return 0;
1896 i = to_fc_internal(shost->transportt);
1897 return &i->vport_attr_cont.ac == cont;
1902 * fc_timed_out - FC Transport I/O timeout intercept handler
1904 * @scmd: The SCSI command which timed out
1906 * This routine protects against error handlers getting invoked while a
1907 * rport is in a blocked state, typically due to a temporarily loss of
1908 * connectivity. If the error handlers are allowed to proceed, requests
1909 * to abort i/o, reset the target, etc will likely fail as there is no way
1910 * to communicate with the device to perform the requested function. These
1911 * failures may result in the midlayer taking the device offline, requiring
1912 * manual intervention to restore operation.
1914 * This routine, called whenever an i/o times out, validates the state of
1915 * the underlying rport. If the rport is blocked, it returns
1916 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1917 * Eventually, either the device will return, or devloss_tmo will fire,
1918 * and when the timeout then fires, it will be handled normally.
1919 * If the rport is not blocked, normal error handling continues.
1921 * Notes:
1922 * This routine assumes no locks are held on entry.
1924 static enum scsi_eh_timer_return
1925 fc_timed_out(struct scsi_cmnd *scmd)
1927 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1929 if (rport->port_state == FC_PORTSTATE_BLOCKED)
1930 return EH_RESET_TIMER;
1932 return EH_NOT_HANDLED;
1936 * Must be called with shost->host_lock held
1938 static int fc_user_scan(struct Scsi_Host *shost, uint channel,
1939 uint id, uint lun)
1941 struct fc_rport *rport;
1943 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1944 if (rport->scsi_target_id == -1)
1945 continue;
1947 if (rport->port_state != FC_PORTSTATE_ONLINE)
1948 continue;
1950 if ((channel == SCAN_WILD_CARD || channel == rport->channel) &&
1951 (id == SCAN_WILD_CARD || id == rport->scsi_target_id)) {
1952 scsi_scan_target(&rport->dev, rport->channel,
1953 rport->scsi_target_id, lun, 1);
1957 return 0;
1960 static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
1961 int result)
1963 struct fc_internal *i = to_fc_internal(shost->transportt);
1964 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
1967 static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
1969 struct fc_internal *i = to_fc_internal(shost->transportt);
1970 return i->f->it_nexus_response(shost, nexus, result);
1973 struct scsi_transport_template *
1974 fc_attach_transport(struct fc_function_template *ft)
1976 int count;
1977 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
1978 GFP_KERNEL);
1980 if (unlikely(!i))
1981 return NULL;
1983 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
1984 i->t.target_attrs.ac.class = &fc_transport_class.class;
1985 i->t.target_attrs.ac.match = fc_target_match;
1986 i->t.target_size = sizeof(struct fc_starget_attrs);
1987 transport_container_register(&i->t.target_attrs);
1989 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1990 i->t.host_attrs.ac.class = &fc_host_class.class;
1991 i->t.host_attrs.ac.match = fc_host_match;
1992 i->t.host_size = sizeof(struct fc_host_attrs);
1993 if (ft->get_fc_host_stats)
1994 i->t.host_attrs.statistics = &fc_statistics_group;
1995 transport_container_register(&i->t.host_attrs);
1997 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
1998 i->rport_attr_cont.ac.class = &fc_rport_class.class;
1999 i->rport_attr_cont.ac.match = fc_rport_match;
2000 transport_container_register(&i->rport_attr_cont);
2002 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2003 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2004 i->vport_attr_cont.ac.match = fc_vport_match;
2005 transport_container_register(&i->vport_attr_cont);
2007 i->f = ft;
2009 /* Transport uses the shost workq for scsi scanning */
2010 i->t.create_work_queue = 1;
2012 i->t.eh_timed_out = fc_timed_out;
2014 i->t.user_scan = fc_user_scan;
2016 /* target-mode drivers' functions */
2017 i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
2018 i->t.it_nexus_response = fc_it_nexus_response;
2021 * Setup SCSI Target Attributes.
2023 count = 0;
2024 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2025 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2026 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2028 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2030 i->starget_attrs[count] = NULL;
2034 * Setup SCSI Host Attributes.
2036 count=0;
2037 SETUP_HOST_ATTRIBUTE_RD(node_name);
2038 SETUP_HOST_ATTRIBUTE_RD(port_name);
2039 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
2040 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2041 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
2042 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2043 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2044 if (ft->vport_create) {
2045 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2046 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2048 SETUP_HOST_ATTRIBUTE_RD(serial_number);
2050 SETUP_HOST_ATTRIBUTE_RD(port_id);
2051 SETUP_HOST_ATTRIBUTE_RD(port_type);
2052 SETUP_HOST_ATTRIBUTE_RD(port_state);
2053 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2054 SETUP_HOST_ATTRIBUTE_RD(speed);
2055 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2056 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2057 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
2059 /* Transport-managed attributes */
2060 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
2061 if (ft->issue_fc_host_lip)
2062 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2063 if (ft->vport_create)
2064 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2065 if (ft->vport_delete)
2066 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
2068 BUG_ON(count > FC_HOST_NUM_ATTRS);
2070 i->host_attrs[count] = NULL;
2073 * Setup Remote Port Attributes.
2075 count=0;
2076 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2077 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2078 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2079 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2080 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2081 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2082 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2083 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2084 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2085 if (ft->terminate_rport_io)
2086 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
2088 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2090 i->rport_attrs[count] = NULL;
2093 * Setup Virtual Port Attributes.
2095 count=0;
2096 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2097 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2098 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2099 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2100 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2101 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2102 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2103 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2104 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2106 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2108 i->vport_attrs[count] = NULL;
2110 return &i->t;
2112 EXPORT_SYMBOL(fc_attach_transport);
2114 void fc_release_transport(struct scsi_transport_template *t)
2116 struct fc_internal *i = to_fc_internal(t);
2118 transport_container_unregister(&i->t.target_attrs);
2119 transport_container_unregister(&i->t.host_attrs);
2120 transport_container_unregister(&i->rport_attr_cont);
2121 transport_container_unregister(&i->vport_attr_cont);
2123 kfree(i);
2125 EXPORT_SYMBOL(fc_release_transport);
2128 * fc_queue_work - Queue work to the fc_host workqueue.
2129 * @shost: Pointer to Scsi_Host bound to fc_host.
2130 * @work: Work to queue for execution.
2132 * Return value:
2133 * 1 - work queued for execution
2134 * 0 - work is already queued
2135 * -EINVAL - work queue doesn't exist
2137 static int
2138 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2140 if (unlikely(!fc_host_work_q(shost))) {
2141 printk(KERN_ERR
2142 "ERROR: FC host '%s' attempted to queue work, "
2143 "when no workqueue created.\n", shost->hostt->name);
2144 dump_stack();
2146 return -EINVAL;
2149 return queue_work(fc_host_work_q(shost), work);
2153 * fc_flush_work - Flush a fc_host's workqueue.
2154 * @shost: Pointer to Scsi_Host bound to fc_host.
2156 static void
2157 fc_flush_work(struct Scsi_Host *shost)
2159 if (!fc_host_work_q(shost)) {
2160 printk(KERN_ERR
2161 "ERROR: FC host '%s' attempted to flush work, "
2162 "when no workqueue created.\n", shost->hostt->name);
2163 dump_stack();
2164 return;
2167 flush_workqueue(fc_host_work_q(shost));
2171 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2172 * @shost: Pointer to Scsi_Host bound to fc_host.
2173 * @work: Work to queue for execution.
2174 * @delay: jiffies to delay the work queuing
2176 * Return value:
2177 * 1 on success / 0 already queued / < 0 for error
2179 static int
2180 fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
2181 unsigned long delay)
2183 if (unlikely(!fc_host_devloss_work_q(shost))) {
2184 printk(KERN_ERR
2185 "ERROR: FC host '%s' attempted to queue work, "
2186 "when no workqueue created.\n", shost->hostt->name);
2187 dump_stack();
2189 return -EINVAL;
2192 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2196 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2197 * @shost: Pointer to Scsi_Host bound to fc_host.
2199 static void
2200 fc_flush_devloss(struct Scsi_Host *shost)
2202 if (!fc_host_devloss_work_q(shost)) {
2203 printk(KERN_ERR
2204 "ERROR: FC host '%s' attempted to flush work, "
2205 "when no workqueue created.\n", shost->hostt->name);
2206 dump_stack();
2207 return;
2210 flush_workqueue(fc_host_devloss_work_q(shost));
2215 * fc_remove_host - called to terminate any fc_transport-related elements
2216 * for a scsi host.
2217 * @rport: remote port to be unblocked.
2219 * This routine is expected to be called immediately preceeding the
2220 * a driver's call to scsi_remove_host().
2222 * WARNING: A driver utilizing the fc_transport, which fails to call
2223 * this routine prior to scsi_remote_host(), will leave dangling
2224 * objects in /sys/class/fc_remote_ports. Access to any of these
2225 * objects can result in a system crash !!!
2227 * Notes:
2228 * This routine assumes no locks are held on entry.
2230 void
2231 fc_remove_host(struct Scsi_Host *shost)
2233 struct fc_vport *vport = NULL, *next_vport = NULL;
2234 struct fc_rport *rport = NULL, *next_rport = NULL;
2235 struct workqueue_struct *work_q;
2236 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2237 unsigned long flags;
2239 spin_lock_irqsave(shost->host_lock, flags);
2241 /* Remove any vports */
2242 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
2243 fc_queue_work(shost, &vport->vport_delete_work);
2245 /* Remove any remote ports */
2246 list_for_each_entry_safe(rport, next_rport,
2247 &fc_host->rports, peers) {
2248 list_del(&rport->peers);
2249 rport->port_state = FC_PORTSTATE_DELETED;
2250 fc_queue_work(shost, &rport->rport_delete_work);
2253 list_for_each_entry_safe(rport, next_rport,
2254 &fc_host->rport_bindings, peers) {
2255 list_del(&rport->peers);
2256 rport->port_state = FC_PORTSTATE_DELETED;
2257 fc_queue_work(shost, &rport->rport_delete_work);
2260 spin_unlock_irqrestore(shost->host_lock, flags);
2262 /* flush all scan work items */
2263 scsi_flush_work(shost);
2265 /* flush all stgt delete, and rport delete work items, then kill it */
2266 if (fc_host->work_q) {
2267 work_q = fc_host->work_q;
2268 fc_host->work_q = NULL;
2269 destroy_workqueue(work_q);
2272 /* flush all devloss work items, then kill it */
2273 if (fc_host->devloss_work_q) {
2274 work_q = fc_host->devloss_work_q;
2275 fc_host->devloss_work_q = NULL;
2276 destroy_workqueue(work_q);
2279 EXPORT_SYMBOL(fc_remove_host);
2283 * fc_starget_delete - called to delete the scsi decendents of an rport
2284 * (target and all sdevs)
2286 * @work: remote port to be operated on.
2288 static void
2289 fc_starget_delete(struct work_struct *work)
2291 struct fc_rport *rport =
2292 container_of(work, struct fc_rport, stgt_delete_work);
2293 struct Scsi_Host *shost = rport_to_shost(rport);
2294 struct fc_internal *i = to_fc_internal(shost->transportt);
2296 /* Involve the LLDD if possible to terminate all io on the rport. */
2297 if (i->f->terminate_rport_io)
2298 i->f->terminate_rport_io(rport);
2300 scsi_remove_target(&rport->dev);
2305 * fc_rport_final_delete - finish rport termination and delete it.
2307 * @work: remote port to be deleted.
2309 static void
2310 fc_rport_final_delete(struct work_struct *work)
2312 struct fc_rport *rport =
2313 container_of(work, struct fc_rport, rport_delete_work);
2314 struct device *dev = &rport->dev;
2315 struct Scsi_Host *shost = rport_to_shost(rport);
2316 struct fc_internal *i = to_fc_internal(shost->transportt);
2317 unsigned long flags;
2320 * if a scan is pending, flush the SCSI Host work_q so that
2321 * that we can reclaim the rport scan work element.
2323 if (rport->flags & FC_RPORT_SCAN_PENDING)
2324 scsi_flush_work(shost);
2326 /* involve the LLDD to terminate all pending i/o */
2327 if (i->f->terminate_rport_io)
2328 i->f->terminate_rport_io(rport);
2331 * Cancel any outstanding timers. These should really exist
2332 * only when rmmod'ing the LLDD and we're asking for
2333 * immediate termination of the rports
2335 spin_lock_irqsave(shost->host_lock, flags);
2336 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2337 spin_unlock_irqrestore(shost->host_lock, flags);
2338 if (!cancel_delayed_work(&rport->fail_io_work))
2339 fc_flush_devloss(shost);
2340 if (!cancel_delayed_work(&rport->dev_loss_work))
2341 fc_flush_devloss(shost);
2342 spin_lock_irqsave(shost->host_lock, flags);
2343 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2345 spin_unlock_irqrestore(shost->host_lock, flags);
2347 /* Delete SCSI target and sdevs */
2348 if (rport->scsi_target_id != -1)
2349 fc_starget_delete(&rport->stgt_delete_work);
2352 * Notify the driver that the rport is now dead. The LLDD will
2353 * also guarantee that any communication to the rport is terminated
2355 if (i->f->dev_loss_tmo_callbk)
2356 i->f->dev_loss_tmo_callbk(rport);
2358 transport_remove_device(dev);
2359 device_del(dev);
2360 transport_destroy_device(dev);
2361 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2362 put_device(dev); /* for self-reference */
2367 * fc_rport_create - allocates and creates a remote FC port.
2368 * @shost: scsi host the remote port is connected to.
2369 * @channel: Channel on shost port connected to.
2370 * @ids: The world wide names, fc address, and FC4 port
2371 * roles for the remote port.
2373 * Allocates and creates the remoter port structure, including the
2374 * class and sysfs creation.
2376 * Notes:
2377 * This routine assumes no locks are held on entry.
2379 static struct fc_rport *
2380 fc_rport_create(struct Scsi_Host *shost, int channel,
2381 struct fc_rport_identifiers *ids)
2383 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2384 struct fc_internal *fci = to_fc_internal(shost->transportt);
2385 struct fc_rport *rport;
2386 struct device *dev;
2387 unsigned long flags;
2388 int error;
2389 size_t size;
2391 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
2392 rport = kzalloc(size, GFP_KERNEL);
2393 if (unlikely(!rport)) {
2394 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
2395 return NULL;
2398 rport->maxframe_size = -1;
2399 rport->supported_classes = FC_COS_UNSPECIFIED;
2400 rport->dev_loss_tmo = fc_dev_loss_tmo;
2401 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2402 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2403 rport->port_id = ids->port_id;
2404 rport->roles = ids->roles;
2405 rport->port_state = FC_PORTSTATE_ONLINE;
2406 if (fci->f->dd_fcrport_size)
2407 rport->dd_data = &rport[1];
2408 rport->channel = channel;
2409 rport->fast_io_fail_tmo = -1;
2411 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2412 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2413 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2414 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2415 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
2417 spin_lock_irqsave(shost->host_lock, flags);
2419 rport->number = fc_host->next_rport_number++;
2420 if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
2421 rport->scsi_target_id = fc_host->next_target_id++;
2422 else
2423 rport->scsi_target_id = -1;
2424 list_add_tail(&rport->peers, &fc_host->rports);
2425 get_device(&shost->shost_gendev); /* for fc_host->rport list */
2427 spin_unlock_irqrestore(shost->host_lock, flags);
2429 dev = &rport->dev;
2430 device_initialize(dev); /* takes self reference */
2431 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
2432 dev->release = fc_rport_dev_release;
2433 sprintf(dev->bus_id, "rport-%d:%d-%d",
2434 shost->host_no, channel, rport->number);
2435 transport_setup_device(dev);
2437 error = device_add(dev);
2438 if (error) {
2439 printk(KERN_ERR "FC Remote Port device_add failed\n");
2440 goto delete_rport;
2442 transport_add_device(dev);
2443 transport_configure_device(dev);
2445 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
2446 /* initiate a scan of the target */
2447 rport->flags |= FC_RPORT_SCAN_PENDING;
2448 scsi_queue_work(shost, &rport->scan_work);
2451 return rport;
2453 delete_rport:
2454 transport_destroy_device(dev);
2455 spin_lock_irqsave(shost->host_lock, flags);
2456 list_del(&rport->peers);
2457 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2458 spin_unlock_irqrestore(shost->host_lock, flags);
2459 put_device(dev->parent);
2460 kfree(rport);
2461 return NULL;
2465 * fc_remote_port_add - notifies the fc transport of the existence
2466 * of a remote FC port.
2467 * @shost: scsi host the remote port is connected to.
2468 * @channel: Channel on shost port connected to.
2469 * @ids: The world wide names, fc address, and FC4 port
2470 * roles for the remote port.
2472 * The LLDD calls this routine to notify the transport of the existence
2473 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2474 * of the port, it's FC address (port_id), and the FC4 roles that are
2475 * active for the port.
2477 * For ports that are FCP targets (aka scsi targets), the FC transport
2478 * maintains consistent target id bindings on behalf of the LLDD.
2479 * A consistent target id binding is an assignment of a target id to
2480 * a remote port identifier, which persists while the scsi host is
2481 * attached. The remote port can disappear, then later reappear, and
2482 * it's target id assignment remains the same. This allows for shifts
2483 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2484 * changes to the scsi subsystem which is based on scsi host number and
2485 * target id values. Bindings are only valid during the attachment of
2486 * the scsi host. If the host detaches, then later re-attaches, target
2487 * id bindings may change.
2489 * This routine is responsible for returning a remote port structure.
2490 * The routine will search the list of remote ports it maintains
2491 * internally on behalf of consistent target id mappings. If found, the
2492 * remote port structure will be reused. Otherwise, a new remote port
2493 * structure will be allocated.
2495 * Whenever a remote port is allocated, a new fc_remote_port class
2496 * device is created.
2498 * Should not be called from interrupt context.
2500 * Notes:
2501 * This routine assumes no locks are held on entry.
2503 struct fc_rport *
2504 fc_remote_port_add(struct Scsi_Host *shost, int channel,
2505 struct fc_rport_identifiers *ids)
2507 struct fc_internal *fci = to_fc_internal(shost->transportt);
2508 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2509 struct fc_rport *rport;
2510 unsigned long flags;
2511 int match = 0;
2513 /* ensure any stgt delete functions are done */
2514 fc_flush_work(shost);
2517 * Search the list of "active" rports, for an rport that has been
2518 * deleted, but we've held off the real delete while the target
2519 * is in a "blocked" state.
2521 spin_lock_irqsave(shost->host_lock, flags);
2523 list_for_each_entry(rport, &fc_host->rports, peers) {
2525 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
2526 (rport->channel == channel)) {
2528 switch (fc_host->tgtid_bind_type) {
2529 case FC_TGTID_BIND_BY_WWPN:
2530 case FC_TGTID_BIND_NONE:
2531 if (rport->port_name == ids->port_name)
2532 match = 1;
2533 break;
2534 case FC_TGTID_BIND_BY_WWNN:
2535 if (rport->node_name == ids->node_name)
2536 match = 1;
2537 break;
2538 case FC_TGTID_BIND_BY_ID:
2539 if (rport->port_id == ids->port_id)
2540 match = 1;
2541 break;
2544 if (match) {
2546 memcpy(&rport->node_name, &ids->node_name,
2547 sizeof(rport->node_name));
2548 memcpy(&rport->port_name, &ids->port_name,
2549 sizeof(rport->port_name));
2550 rport->port_id = ids->port_id;
2552 rport->port_state = FC_PORTSTATE_ONLINE;
2553 rport->roles = ids->roles;
2555 spin_unlock_irqrestore(shost->host_lock, flags);
2557 if (fci->f->dd_fcrport_size)
2558 memset(rport->dd_data, 0,
2559 fci->f->dd_fcrport_size);
2562 * If we were not a target, cancel the
2563 * io terminate and rport timers, and
2564 * we're done.
2566 * If we were a target, but our new role
2567 * doesn't indicate a target, leave the
2568 * timers running expecting the role to
2569 * change as the target fully logs in. If
2570 * it doesn't, the target will be torn down.
2572 * If we were a target, and our role shows
2573 * we're still a target, cancel the timers
2574 * and kick off a scan.
2577 /* was a target, not in roles */
2578 if ((rport->scsi_target_id != -1) &&
2579 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
2580 return rport;
2583 * Stop the fail io and dev_loss timers.
2584 * If they flush, the port_state will
2585 * be checked and will NOOP the function.
2587 if (!cancel_delayed_work(&rport->fail_io_work))
2588 fc_flush_devloss(shost);
2589 if (!cancel_delayed_work(&rport->dev_loss_work))
2590 fc_flush_devloss(shost);
2592 spin_lock_irqsave(shost->host_lock, flags);
2594 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2596 /* if target, initiate a scan */
2597 if (rport->scsi_target_id != -1) {
2598 rport->flags |= FC_RPORT_SCAN_PENDING;
2599 scsi_queue_work(shost,
2600 &rport->scan_work);
2601 spin_unlock_irqrestore(shost->host_lock,
2602 flags);
2603 scsi_target_unblock(&rport->dev);
2604 } else
2605 spin_unlock_irqrestore(shost->host_lock,
2606 flags);
2608 return rport;
2614 * Search the bindings array
2615 * Note: if never a FCP target, you won't be on this list
2617 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
2619 /* search for a matching consistent binding */
2621 list_for_each_entry(rport, &fc_host->rport_bindings,
2622 peers) {
2623 if (rport->channel != channel)
2624 continue;
2626 switch (fc_host->tgtid_bind_type) {
2627 case FC_TGTID_BIND_BY_WWPN:
2628 if (rport->port_name == ids->port_name)
2629 match = 1;
2630 break;
2631 case FC_TGTID_BIND_BY_WWNN:
2632 if (rport->node_name == ids->node_name)
2633 match = 1;
2634 break;
2635 case FC_TGTID_BIND_BY_ID:
2636 if (rport->port_id == ids->port_id)
2637 match = 1;
2638 break;
2639 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2640 break;
2643 if (match) {
2644 list_move_tail(&rport->peers, &fc_host->rports);
2645 break;
2649 if (match) {
2650 memcpy(&rport->node_name, &ids->node_name,
2651 sizeof(rport->node_name));
2652 memcpy(&rport->port_name, &ids->port_name,
2653 sizeof(rport->port_name));
2654 rport->port_id = ids->port_id;
2655 rport->roles = ids->roles;
2656 rport->port_state = FC_PORTSTATE_ONLINE;
2658 if (fci->f->dd_fcrport_size)
2659 memset(rport->dd_data, 0,
2660 fci->f->dd_fcrport_size);
2662 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
2663 /* initiate a scan of the target */
2664 rport->flags |= FC_RPORT_SCAN_PENDING;
2665 scsi_queue_work(shost, &rport->scan_work);
2666 spin_unlock_irqrestore(shost->host_lock, flags);
2667 scsi_target_unblock(&rport->dev);
2668 } else
2669 spin_unlock_irqrestore(shost->host_lock, flags);
2671 return rport;
2675 spin_unlock_irqrestore(shost->host_lock, flags);
2677 /* No consistent binding found - create new remote port entry */
2678 rport = fc_rport_create(shost, channel, ids);
2680 return rport;
2682 EXPORT_SYMBOL(fc_remote_port_add);
2686 * fc_remote_port_delete - notifies the fc transport that a remote
2687 * port is no longer in existence.
2688 * @rport: The remote port that no longer exists
2690 * The LLDD calls this routine to notify the transport that a remote
2691 * port is no longer part of the topology. Note: Although a port
2692 * may no longer be part of the topology, it may persist in the remote
2693 * ports displayed by the fc_host. We do this under 2 conditions:
2694 * - If the port was a scsi target, we delay its deletion by "blocking" it.
2695 * This allows the port to temporarily disappear, then reappear without
2696 * disrupting the SCSI device tree attached to it. During the "blocked"
2697 * period the port will still exist.
2698 * - If the port was a scsi target and disappears for longer than we
2699 * expect, we'll delete the port and the tear down the SCSI device tree
2700 * attached to it. However, we want to semi-persist the target id assigned
2701 * to that port if it eventually does exist. The port structure will
2702 * remain (although with minimal information) so that the target id
2703 * bindings remails.
2705 * If the remote port is not an FCP Target, it will be fully torn down
2706 * and deallocated, including the fc_remote_port class device.
2708 * If the remote port is an FCP Target, the port will be placed in a
2709 * temporary blocked state. From the LLDD's perspective, the rport no
2710 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2711 * exists, but all sdevs on it are blocked from further I/O. The following
2712 * is then expected:
2713 * If the remote port does not return (signaled by a LLDD call to
2714 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2715 * scsi target is removed - killing all outstanding i/o and removing the
2716 * scsi devices attached ot it. The port structure will be marked Not
2717 * Present and be partially cleared, leaving only enough information to
2718 * recognize the remote port relative to the scsi target id binding if
2719 * it later appears. The port will remain as long as there is a valid
2720 * binding (e.g. until the user changes the binding type or unloads the
2721 * scsi host with the binding).
2723 * If the remote port returns within the dev_loss_tmo value (and matches
2724 * according to the target id binding type), the port structure will be
2725 * reused. If it is no longer a SCSI target, the target will be torn
2726 * down. If it continues to be a SCSI target, then the target will be
2727 * unblocked (allowing i/o to be resumed), and a scan will be activated
2728 * to ensure that all luns are detected.
2730 * Called from normal process context only - cannot be called from interrupt.
2732 * Notes:
2733 * This routine assumes no locks are held on entry.
2735 void
2736 fc_remote_port_delete(struct fc_rport *rport)
2738 struct Scsi_Host *shost = rport_to_shost(rport);
2739 struct fc_internal *i = to_fc_internal(shost->transportt);
2740 int timeout = rport->dev_loss_tmo;
2741 unsigned long flags;
2744 * No need to flush the fc_host work_q's, as all adds are synchronous.
2746 * We do need to reclaim the rport scan work element, so eventually
2747 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2748 * there's still a scan pending.
2751 spin_lock_irqsave(shost->host_lock, flags);
2753 if (rport->port_state != FC_PORTSTATE_ONLINE) {
2754 spin_unlock_irqrestore(shost->host_lock, flags);
2755 return;
2759 * In the past, we if this was not an FCP-Target, we would
2760 * unconditionally just jump to deleting the rport.
2761 * However, rports can be used as node containers by the LLDD,
2762 * and its not appropriate to just terminate the rport at the
2763 * first sign of a loss in connectivity. The LLDD may want to
2764 * send ELS traffic to re-validate the login. If the rport is
2765 * immediately deleted, it makes it inappropriate for a node
2766 * container.
2767 * So... we now unconditionally wait dev_loss_tmo before
2768 * destroying an rport.
2771 rport->port_state = FC_PORTSTATE_BLOCKED;
2773 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2775 spin_unlock_irqrestore(shost->host_lock, flags);
2777 if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
2778 shost->active_mode & MODE_TARGET)
2779 fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
2781 scsi_target_block(&rport->dev);
2783 /* see if we need to kill io faster than waiting for device loss */
2784 if ((rport->fast_io_fail_tmo != -1) &&
2785 (rport->fast_io_fail_tmo < timeout) && (i->f->terminate_rport_io))
2786 fc_queue_devloss_work(shost, &rport->fail_io_work,
2787 rport->fast_io_fail_tmo * HZ);
2789 /* cap the length the devices can be blocked until they are deleted */
2790 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
2792 EXPORT_SYMBOL(fc_remote_port_delete);
2795 * fc_remote_port_rolechg - notifies the fc transport that the roles
2796 * on a remote may have changed.
2797 * @rport: The remote port that changed.
2799 * The LLDD calls this routine to notify the transport that the roles
2800 * on a remote port may have changed. The largest effect of this is
2801 * if a port now becomes a FCP Target, it must be allocated a
2802 * scsi target id. If the port is no longer a FCP target, any
2803 * scsi target id value assigned to it will persist in case the
2804 * role changes back to include FCP Target. No changes in the scsi
2805 * midlayer will be invoked if the role changes (in the expectation
2806 * that the role will be resumed. If it doesn't normal error processing
2807 * will take place).
2809 * Should not be called from interrupt context.
2811 * Notes:
2812 * This routine assumes no locks are held on entry.
2814 void
2815 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
2817 struct Scsi_Host *shost = rport_to_shost(rport);
2818 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2819 unsigned long flags;
2820 int create = 0;
2821 int ret;
2823 spin_lock_irqsave(shost->host_lock, flags);
2824 if (roles & FC_PORT_ROLE_FCP_TARGET) {
2825 if (rport->scsi_target_id == -1) {
2826 rport->scsi_target_id = fc_host->next_target_id++;
2827 create = 1;
2828 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
2829 create = 1;
2830 } else if (shost->active_mode & MODE_TARGET) {
2831 ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
2832 (char *)&rport->node_name);
2833 if (ret)
2834 printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
2835 ret);
2838 rport->roles = roles;
2840 spin_unlock_irqrestore(shost->host_lock, flags);
2842 if (create) {
2844 * There may have been a delete timer running on the
2845 * port. Ensure that it is cancelled as we now know
2846 * the port is an FCP Target.
2847 * Note: we know the rport is exists and in an online
2848 * state as the LLDD would not have had an rport
2849 * reference to pass us.
2851 * Take no action on the del_timer failure as the state
2852 * machine state change will validate the
2853 * transaction.
2855 if (!cancel_delayed_work(&rport->fail_io_work))
2856 fc_flush_devloss(shost);
2857 if (!cancel_delayed_work(&rport->dev_loss_work))
2858 fc_flush_devloss(shost);
2860 spin_lock_irqsave(shost->host_lock, flags);
2861 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2862 spin_unlock_irqrestore(shost->host_lock, flags);
2864 /* ensure any stgt delete functions are done */
2865 fc_flush_work(shost);
2867 /* initiate a scan of the target */
2868 spin_lock_irqsave(shost->host_lock, flags);
2869 rport->flags |= FC_RPORT_SCAN_PENDING;
2870 scsi_queue_work(shost, &rport->scan_work);
2871 spin_unlock_irqrestore(shost->host_lock, flags);
2872 scsi_target_unblock(&rport->dev);
2875 EXPORT_SYMBOL(fc_remote_port_rolechg);
2878 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port,
2879 * which we blocked, and has now failed to return
2880 * in the allotted time.
2882 * @work: rport target that failed to reappear in the allotted time.
2884 static void
2885 fc_timeout_deleted_rport(struct work_struct *work)
2887 struct fc_rport *rport =
2888 container_of(work, struct fc_rport, dev_loss_work.work);
2889 struct Scsi_Host *shost = rport_to_shost(rport);
2890 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2891 unsigned long flags;
2893 spin_lock_irqsave(shost->host_lock, flags);
2895 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2898 * If the port is ONLINE, then it came back. If it was a SCSI
2899 * target, validate it still is. If not, tear down the
2900 * scsi_target on it.
2902 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
2903 (rport->scsi_target_id != -1) &&
2904 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
2905 dev_printk(KERN_ERR, &rport->dev,
2906 "blocked FC remote port time out: no longer"
2907 " a FCP target, removing starget\n");
2908 spin_unlock_irqrestore(shost->host_lock, flags);
2909 scsi_target_unblock(&rport->dev);
2910 fc_queue_work(shost, &rport->stgt_delete_work);
2911 return;
2914 /* NOOP state - we're flushing workq's */
2915 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
2916 spin_unlock_irqrestore(shost->host_lock, flags);
2917 dev_printk(KERN_ERR, &rport->dev,
2918 "blocked FC remote port time out: leaving"
2919 " rport%s alone\n",
2920 (rport->scsi_target_id != -1) ? " and starget" : "");
2921 return;
2924 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
2925 (rport->scsi_target_id == -1)) {
2926 list_del(&rport->peers);
2927 rport->port_state = FC_PORTSTATE_DELETED;
2928 dev_printk(KERN_ERR, &rport->dev,
2929 "blocked FC remote port time out: removing"
2930 " rport%s\n",
2931 (rport->scsi_target_id != -1) ? " and starget" : "");
2932 fc_queue_work(shost, &rport->rport_delete_work);
2933 spin_unlock_irqrestore(shost->host_lock, flags);
2934 return;
2937 dev_printk(KERN_ERR, &rport->dev,
2938 "blocked FC remote port time out: removing target and "
2939 "saving binding\n");
2941 list_move_tail(&rport->peers, &fc_host->rport_bindings);
2944 * Note: We do not remove or clear the hostdata area. This allows
2945 * host-specific target data to persist along with the
2946 * scsi_target_id. It's up to the host to manage it's hostdata area.
2950 * Reinitialize port attributes that may change if the port comes back.
2952 rport->maxframe_size = -1;
2953 rport->supported_classes = FC_COS_UNSPECIFIED;
2954 rport->roles = FC_PORT_ROLE_UNKNOWN;
2955 rport->port_state = FC_PORTSTATE_NOTPRESENT;
2957 /* remove the identifiers that aren't used in the consisting binding */
2958 switch (fc_host->tgtid_bind_type) {
2959 case FC_TGTID_BIND_BY_WWPN:
2960 rport->node_name = -1;
2961 rport->port_id = -1;
2962 break;
2963 case FC_TGTID_BIND_BY_WWNN:
2964 rport->port_name = -1;
2965 rport->port_id = -1;
2966 break;
2967 case FC_TGTID_BIND_BY_ID:
2968 rport->node_name = -1;
2969 rport->port_name = -1;
2970 break;
2971 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2972 break;
2976 * As this only occurs if the remote port (scsi target)
2977 * went away and didn't come back - we'll remove
2978 * all attached scsi devices.
2980 spin_unlock_irqrestore(shost->host_lock, flags);
2982 scsi_target_unblock(&rport->dev);
2983 fc_queue_work(shost, &rport->stgt_delete_work);
2987 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a
2988 * disconnected SCSI target.
2990 * @work: rport to terminate io on.
2992 * Notes: Only requests the failure of the io, not that all are flushed
2993 * prior to returning.
2995 static void
2996 fc_timeout_fail_rport_io(struct work_struct *work)
2998 struct fc_rport *rport =
2999 container_of(work, struct fc_rport, fail_io_work.work);
3000 struct Scsi_Host *shost = rport_to_shost(rport);
3001 struct fc_internal *i = to_fc_internal(shost->transportt);
3003 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3004 return;
3006 i->f->terminate_rport_io(rport);
3010 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3012 * @work: remote port to be scanned.
3014 static void
3015 fc_scsi_scan_rport(struct work_struct *work)
3017 struct fc_rport *rport =
3018 container_of(work, struct fc_rport, scan_work);
3019 struct Scsi_Host *shost = rport_to_shost(rport);
3020 struct fc_internal *i = to_fc_internal(shost->transportt);
3021 unsigned long flags;
3023 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3024 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3025 !(i->f->disable_target_scan)) {
3026 scsi_scan_target(&rport->dev, rport->channel,
3027 rport->scsi_target_id, SCAN_WILD_CARD, 1);
3030 spin_lock_irqsave(shost->host_lock, flags);
3031 rport->flags &= ~FC_RPORT_SCAN_PENDING;
3032 spin_unlock_irqrestore(shost->host_lock, flags);
3037 * fc_vport_create - allocates and creates a FC virtual port.
3038 * @shost: scsi host the virtual port is connected to.
3039 * @channel: Channel on shost port connected to.
3040 * @pdev: parent device for vport
3041 * @ids: The world wide names, FC4 port roles, etc for
3042 * the virtual port.
3043 * @ret_vport: The pointer to the created vport.
3045 * Allocates and creates the vport structure, calls the parent host
3046 * to instantiate the vport, the completes w/ class and sysfs creation.
3048 * Notes:
3049 * This routine assumes no locks are held on entry.
3051 static int
3052 fc_vport_create(struct Scsi_Host *shost, int channel, struct device *pdev,
3053 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3055 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3056 struct fc_internal *fci = to_fc_internal(shost->transportt);
3057 struct fc_vport *vport;
3058 struct device *dev;
3059 unsigned long flags;
3060 size_t size;
3061 int error;
3063 *ret_vport = NULL;
3065 if ( ! fci->f->vport_create)
3066 return -ENOENT;
3068 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3069 vport = kzalloc(size, GFP_KERNEL);
3070 if (unlikely(!vport)) {
3071 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
3072 return -ENOMEM;
3075 vport->vport_state = FC_VPORT_UNKNOWN;
3076 vport->vport_last_state = FC_VPORT_UNKNOWN;
3077 vport->node_name = ids->node_name;
3078 vport->port_name = ids->port_name;
3079 vport->roles = ids->roles;
3080 vport->vport_type = ids->vport_type;
3081 if (fci->f->dd_fcvport_size)
3082 vport->dd_data = &vport[1];
3083 vport->shost = shost;
3084 vport->channel = channel;
3085 vport->flags = FC_VPORT_CREATING;
3086 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3088 spin_lock_irqsave(shost->host_lock, flags);
3090 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3091 spin_unlock_irqrestore(shost->host_lock, flags);
3092 kfree(vport);
3093 return -ENOSPC;
3095 fc_host->npiv_vports_inuse++;
3096 vport->number = fc_host->next_vport_number++;
3097 list_add_tail(&vport->peers, &fc_host->vports);
3098 get_device(&shost->shost_gendev); /* for fc_host->vport list */
3100 spin_unlock_irqrestore(shost->host_lock, flags);
3102 dev = &vport->dev;
3103 device_initialize(dev); /* takes self reference */
3104 dev->parent = get_device(pdev); /* takes parent reference */
3105 dev->release = fc_vport_dev_release;
3106 sprintf(dev->bus_id, "vport-%d:%d-%d",
3107 shost->host_no, channel, vport->number);
3108 transport_setup_device(dev);
3110 error = device_add(dev);
3111 if (error) {
3112 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3113 goto delete_vport;
3115 transport_add_device(dev);
3116 transport_configure_device(dev);
3118 error = fci->f->vport_create(vport, ids->disable);
3119 if (error) {
3120 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3121 goto delete_vport_all;
3125 * if the parent isn't the physical adapter's Scsi_Host, ensure
3126 * the Scsi_Host at least contains ia symlink to the vport.
3128 if (pdev != &shost->shost_gendev) {
3129 error = sysfs_create_link(&shost->shost_gendev.kobj,
3130 &dev->kobj, dev->bus_id);
3131 if (error)
3132 printk(KERN_ERR
3133 "%s: Cannot create vport symlinks for "
3134 "%s, err=%d\n",
3135 __FUNCTION__, dev->bus_id, error);
3137 spin_lock_irqsave(shost->host_lock, flags);
3138 vport->flags &= ~FC_VPORT_CREATING;
3139 spin_unlock_irqrestore(shost->host_lock, flags);
3141 dev_printk(KERN_NOTICE, pdev,
3142 "%s created via shost%d channel %d\n", dev->bus_id,
3143 shost->host_no, channel);
3145 *ret_vport = vport;
3147 return 0;
3149 delete_vport_all:
3150 transport_remove_device(dev);
3151 device_del(dev);
3152 delete_vport:
3153 transport_destroy_device(dev);
3154 spin_lock_irqsave(shost->host_lock, flags);
3155 list_del(&vport->peers);
3156 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3157 fc_host->npiv_vports_inuse--;
3158 spin_unlock_irqrestore(shost->host_lock, flags);
3159 put_device(dev->parent);
3160 kfree(vport);
3162 return error;
3167 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3168 * @vport: fc_vport to be terminated
3170 * Calls the LLDD vport_delete() function, then deallocates and removes
3171 * the vport from the shost and object tree.
3173 * Notes:
3174 * This routine assumes no locks are held on entry.
3177 fc_vport_terminate(struct fc_vport *vport)
3179 struct Scsi_Host *shost = vport_to_shost(vport);
3180 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3181 struct fc_internal *i = to_fc_internal(shost->transportt);
3182 struct device *dev = &vport->dev;
3183 unsigned long flags;
3184 int stat;
3186 spin_lock_irqsave(shost->host_lock, flags);
3187 if (vport->flags & FC_VPORT_CREATING) {
3188 spin_unlock_irqrestore(shost->host_lock, flags);
3189 return -EBUSY;
3191 if (vport->flags & (FC_VPORT_DEL)) {
3192 spin_unlock_irqrestore(shost->host_lock, flags);
3193 return -EALREADY;
3195 vport->flags |= FC_VPORT_DELETING;
3196 spin_unlock_irqrestore(shost->host_lock, flags);
3198 if (i->f->vport_delete)
3199 stat = i->f->vport_delete(vport);
3200 else
3201 stat = -ENOENT;
3203 spin_lock_irqsave(shost->host_lock, flags);
3204 vport->flags &= ~FC_VPORT_DELETING;
3205 if (!stat) {
3206 vport->flags |= FC_VPORT_DELETED;
3207 list_del(&vport->peers);
3208 fc_host->npiv_vports_inuse--;
3209 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3211 spin_unlock_irqrestore(shost->host_lock, flags);
3213 if (stat)
3214 return stat;
3216 if (dev->parent != &shost->shost_gendev)
3217 sysfs_remove_link(&shost->shost_gendev.kobj, dev->bus_id);
3218 transport_remove_device(dev);
3219 device_del(dev);
3220 transport_destroy_device(dev);
3223 * Removing our self-reference should mean our
3224 * release function gets called, which will drop the remaining
3225 * parent reference and free the data structure.
3227 put_device(dev); /* for self-reference */
3229 return 0; /* SUCCESS */
3231 EXPORT_SYMBOL(fc_vport_terminate);
3234 * fc_vport_sched_delete - workq-based delete request for a vport
3236 * @work: vport to be deleted.
3238 static void
3239 fc_vport_sched_delete(struct work_struct *work)
3241 struct fc_vport *vport =
3242 container_of(work, struct fc_vport, vport_delete_work);
3243 int stat;
3245 stat = fc_vport_terminate(vport);
3246 if (stat)
3247 dev_printk(KERN_ERR, vport->dev.parent,
3248 "%s: %s could not be deleted created via "
3249 "shost%d channel %d - error %d\n", __FUNCTION__,
3250 vport->dev.bus_id, vport->shost->host_no,
3251 vport->channel, stat);
3255 /* Original Author: Martin Hicks */
3256 MODULE_AUTHOR("James Smart");
3257 MODULE_DESCRIPTION("FC Transport Attributes");
3258 MODULE_LICENSE("GPL");
3260 module_init(fc_transport_init);
3261 module_exit(fc_transport_exit);