thinkpad-acpi: explain errors from acpi_install_notify_handler
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / scsi_transport_fc.c
blobdb02e31bae391cb173f779f72055b368403ed25f
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/scsi_bsg_fc.h>
39 #include "scsi_priv.h"
40 #include "scsi_transport_fc_internal.h"
42 static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
43 static void fc_vport_sched_delete(struct work_struct *work);
44 static int fc_vport_setup(struct Scsi_Host *shost, int channel,
45 struct device *pdev, struct fc_vport_identifiers *ids,
46 struct fc_vport **vport);
47 static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
48 static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
49 static void fc_bsg_remove(struct request_queue *);
50 static void fc_bsg_goose_queue(struct fc_rport *);
53 * Redefine so that we can have same named attributes in the
54 * sdev/starget/host objects.
56 #define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
57 struct device_attribute device_attr_##_prefix##_##_name = \
58 __ATTR(_name,_mode,_show,_store)
60 #define fc_enum_name_search(title, table_type, table) \
61 static const char *get_fc_##title##_name(enum table_type table_key) \
62 { \
63 int i; \
64 char *name = NULL; \
66 for (i = 0; i < ARRAY_SIZE(table); i++) { \
67 if (table[i].value == table_key) { \
68 name = table[i].name; \
69 break; \
70 } \
71 } \
72 return name; \
75 #define fc_enum_name_match(title, table_type, table) \
76 static int get_fc_##title##_match(const char *table_key, \
77 enum table_type *value) \
78 { \
79 int i; \
81 for (i = 0; i < ARRAY_SIZE(table); i++) { \
82 if (strncmp(table_key, table[i].name, \
83 table[i].matchlen) == 0) { \
84 *value = table[i].value; \
85 return 0; /* success */ \
86 } \
87 } \
88 return 1; /* failure */ \
92 /* Convert fc_port_type values to ascii string name */
93 static struct {
94 enum fc_port_type value;
95 char *name;
96 } fc_port_type_names[] = {
97 { FC_PORTTYPE_UNKNOWN, "Unknown" },
98 { FC_PORTTYPE_OTHER, "Other" },
99 { FC_PORTTYPE_NOTPRESENT, "Not Present" },
100 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
101 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
102 { FC_PORTTYPE_LPORT, "LPort (private loop)" },
103 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" },
104 { FC_PORTTYPE_NPIV, "NPIV VPORT" },
106 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
107 #define FC_PORTTYPE_MAX_NAMELEN 50
109 /* Reuse fc_port_type enum function for vport_type */
110 #define get_fc_vport_type_name get_fc_port_type_name
113 /* Convert fc_host_event_code values to ascii string name */
114 static const struct {
115 enum fc_host_event_code value;
116 char *name;
117 } fc_host_event_code_names[] = {
118 { FCH_EVT_LIP, "lip" },
119 { FCH_EVT_LINKUP, "link_up" },
120 { FCH_EVT_LINKDOWN, "link_down" },
121 { FCH_EVT_LIPRESET, "lip_reset" },
122 { FCH_EVT_RSCN, "rscn" },
123 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
124 { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
125 { FCH_EVT_PORT_ONLINE, "port_online" },
126 { FCH_EVT_PORT_OFFLINE, "port_offline" },
127 { FCH_EVT_PORT_FABRIC, "port_fabric" },
128 { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
129 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
131 fc_enum_name_search(host_event_code, fc_host_event_code,
132 fc_host_event_code_names)
133 #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
136 /* Convert fc_port_state values to ascii string name */
137 static struct {
138 enum fc_port_state value;
139 char *name;
140 } fc_port_state_names[] = {
141 { FC_PORTSTATE_UNKNOWN, "Unknown" },
142 { FC_PORTSTATE_NOTPRESENT, "Not Present" },
143 { FC_PORTSTATE_ONLINE, "Online" },
144 { FC_PORTSTATE_OFFLINE, "Offline" },
145 { FC_PORTSTATE_BLOCKED, "Blocked" },
146 { FC_PORTSTATE_BYPASSED, "Bypassed" },
147 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
148 { FC_PORTSTATE_LINKDOWN, "Linkdown" },
149 { FC_PORTSTATE_ERROR, "Error" },
150 { FC_PORTSTATE_LOOPBACK, "Loopback" },
151 { FC_PORTSTATE_DELETED, "Deleted" },
153 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
154 #define FC_PORTSTATE_MAX_NAMELEN 20
157 /* Convert fc_vport_state values to ascii string name */
158 static struct {
159 enum fc_vport_state value;
160 char *name;
161 } fc_vport_state_names[] = {
162 { FC_VPORT_UNKNOWN, "Unknown" },
163 { FC_VPORT_ACTIVE, "Active" },
164 { FC_VPORT_DISABLED, "Disabled" },
165 { FC_VPORT_LINKDOWN, "Linkdown" },
166 { FC_VPORT_INITIALIZING, "Initializing" },
167 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
168 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
169 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
170 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
171 { FC_VPORT_FAILED, "VPort Failed" },
173 fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
174 #define FC_VPORTSTATE_MAX_NAMELEN 24
176 /* Reuse fc_vport_state enum function for vport_last_state */
177 #define get_fc_vport_last_state_name get_fc_vport_state_name
180 /* Convert fc_tgtid_binding_type values to ascii string name */
181 static const struct {
182 enum fc_tgtid_binding_type value;
183 char *name;
184 int matchlen;
185 } fc_tgtid_binding_type_names[] = {
186 { FC_TGTID_BIND_NONE, "none", 4 },
187 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
188 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
189 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
191 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
192 fc_tgtid_binding_type_names)
193 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
194 fc_tgtid_binding_type_names)
195 #define FC_BINDTYPE_MAX_NAMELEN 30
198 #define fc_bitfield_name_search(title, table) \
199 static ssize_t \
200 get_fc_##title##_names(u32 table_key, char *buf) \
202 char *prefix = ""; \
203 ssize_t len = 0; \
204 int i; \
206 for (i = 0; i < ARRAY_SIZE(table); i++) { \
207 if (table[i].value & table_key) { \
208 len += sprintf(buf + len, "%s%s", \
209 prefix, table[i].name); \
210 prefix = ", "; \
213 len += sprintf(buf + len, "\n"); \
214 return len; \
218 /* Convert FC_COS bit values to ascii string name */
219 static const struct {
220 u32 value;
221 char *name;
222 } fc_cos_names[] = {
223 { FC_COS_CLASS1, "Class 1" },
224 { FC_COS_CLASS2, "Class 2" },
225 { FC_COS_CLASS3, "Class 3" },
226 { FC_COS_CLASS4, "Class 4" },
227 { FC_COS_CLASS6, "Class 6" },
229 fc_bitfield_name_search(cos, fc_cos_names)
232 /* Convert FC_PORTSPEED bit values to ascii string name */
233 static const struct {
234 u32 value;
235 char *name;
236 } fc_port_speed_names[] = {
237 { FC_PORTSPEED_1GBIT, "1 Gbit" },
238 { FC_PORTSPEED_2GBIT, "2 Gbit" },
239 { FC_PORTSPEED_4GBIT, "4 Gbit" },
240 { FC_PORTSPEED_10GBIT, "10 Gbit" },
241 { FC_PORTSPEED_8GBIT, "8 Gbit" },
242 { FC_PORTSPEED_16GBIT, "16 Gbit" },
243 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
245 fc_bitfield_name_search(port_speed, fc_port_speed_names)
248 static int
249 show_fc_fc4s (char *buf, u8 *fc4_list)
251 int i, len=0;
253 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
254 len += sprintf(buf + len , "0x%02x ", *fc4_list);
255 len += sprintf(buf + len, "\n");
256 return len;
260 /* Convert FC_PORT_ROLE bit values to ascii string name */
261 static const struct {
262 u32 value;
263 char *name;
264 } fc_port_role_names[] = {
265 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
266 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
267 { FC_PORT_ROLE_IP_PORT, "IP Port" },
269 fc_bitfield_name_search(port_roles, fc_port_role_names)
272 * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
274 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
275 #define FC_WELLKNOWN_ROLE_MASK 0x00000f
276 #define FC_FPORT_PORTID 0x00000e
277 #define FC_FABCTLR_PORTID 0x00000d
278 #define FC_DIRSRVR_PORTID 0x00000c
279 #define FC_TIMESRVR_PORTID 0x00000b
280 #define FC_MGMTSRVR_PORTID 0x00000a
283 static void fc_timeout_deleted_rport(struct work_struct *work);
284 static void fc_timeout_fail_rport_io(struct work_struct *work);
285 static void fc_scsi_scan_rport(struct work_struct *work);
288 * Attribute counts pre object type...
289 * Increase these values if you add attributes
291 #define FC_STARGET_NUM_ATTRS 3
292 #define FC_RPORT_NUM_ATTRS 10
293 #define FC_VPORT_NUM_ATTRS 9
294 #define FC_HOST_NUM_ATTRS 22
296 struct fc_internal {
297 struct scsi_transport_template t;
298 struct fc_function_template *f;
301 * For attributes : each object has :
302 * An array of the actual attributes structures
303 * An array of null-terminated pointers to the attribute
304 * structures - used for mid-layer interaction.
306 * The attribute containers for the starget and host are are
307 * part of the midlayer. As the remote port is specific to the
308 * fc transport, we must provide the attribute container.
310 struct device_attribute private_starget_attrs[
311 FC_STARGET_NUM_ATTRS];
312 struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
314 struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
315 struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
317 struct transport_container rport_attr_cont;
318 struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
319 struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
321 struct transport_container vport_attr_cont;
322 struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
323 struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
326 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
328 static int fc_target_setup(struct transport_container *tc, struct device *dev,
329 struct device *cdev)
331 struct scsi_target *starget = to_scsi_target(dev);
332 struct fc_rport *rport = starget_to_rport(starget);
335 * if parent is remote port, use values from remote port.
336 * Otherwise, this host uses the fc_transport, but not the
337 * remote port interface. As such, initialize to known non-values.
339 if (rport) {
340 fc_starget_node_name(starget) = rport->node_name;
341 fc_starget_port_name(starget) = rport->port_name;
342 fc_starget_port_id(starget) = rport->port_id;
343 } else {
344 fc_starget_node_name(starget) = -1;
345 fc_starget_port_name(starget) = -1;
346 fc_starget_port_id(starget) = -1;
349 return 0;
352 static DECLARE_TRANSPORT_CLASS(fc_transport_class,
353 "fc_transport",
354 fc_target_setup,
355 NULL,
356 NULL);
358 static int fc_host_setup(struct transport_container *tc, struct device *dev,
359 struct device *cdev)
361 struct Scsi_Host *shost = dev_to_shost(dev);
362 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
365 * Set default values easily detected by the midlayer as
366 * failure cases. The scsi lldd is responsible for initializing
367 * all transport attributes to valid values per host.
369 fc_host->node_name = -1;
370 fc_host->port_name = -1;
371 fc_host->permanent_port_name = -1;
372 fc_host->supported_classes = FC_COS_UNSPECIFIED;
373 memset(fc_host->supported_fc4s, 0,
374 sizeof(fc_host->supported_fc4s));
375 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
376 fc_host->maxframe_size = -1;
377 fc_host->max_npiv_vports = 0;
378 memset(fc_host->serial_number, 0,
379 sizeof(fc_host->serial_number));
381 fc_host->port_id = -1;
382 fc_host->port_type = FC_PORTTYPE_UNKNOWN;
383 fc_host->port_state = FC_PORTSTATE_UNKNOWN;
384 memset(fc_host->active_fc4s, 0,
385 sizeof(fc_host->active_fc4s));
386 fc_host->speed = FC_PORTSPEED_UNKNOWN;
387 fc_host->fabric_name = -1;
388 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
389 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
391 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
393 INIT_LIST_HEAD(&fc_host->rports);
394 INIT_LIST_HEAD(&fc_host->rport_bindings);
395 INIT_LIST_HEAD(&fc_host->vports);
396 fc_host->next_rport_number = 0;
397 fc_host->next_target_id = 0;
398 fc_host->next_vport_number = 0;
399 fc_host->npiv_vports_inuse = 0;
401 snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
402 "fc_wq_%d", shost->host_no);
403 fc_host->work_q = create_singlethread_workqueue(
404 fc_host->work_q_name);
405 if (!fc_host->work_q)
406 return -ENOMEM;
408 snprintf(fc_host->devloss_work_q_name,
409 sizeof(fc_host->devloss_work_q_name),
410 "fc_dl_%d", shost->host_no);
411 fc_host->devloss_work_q = create_singlethread_workqueue(
412 fc_host->devloss_work_q_name);
413 if (!fc_host->devloss_work_q) {
414 destroy_workqueue(fc_host->work_q);
415 fc_host->work_q = NULL;
416 return -ENOMEM;
419 fc_bsg_hostadd(shost, fc_host);
420 /* ignore any bsg add error - we just can't do sgio */
422 return 0;
425 static int fc_host_remove(struct transport_container *tc, struct device *dev,
426 struct device *cdev)
428 struct Scsi_Host *shost = dev_to_shost(dev);
429 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
431 fc_bsg_remove(fc_host->rqst_q);
432 return 0;
435 static DECLARE_TRANSPORT_CLASS(fc_host_class,
436 "fc_host",
437 fc_host_setup,
438 fc_host_remove,
439 NULL);
442 * Setup and Remove actions for remote ports are handled
443 * in the service functions below.
445 static DECLARE_TRANSPORT_CLASS(fc_rport_class,
446 "fc_remote_ports",
447 NULL,
448 NULL,
449 NULL);
452 * Setup and Remove actions for virtual ports are handled
453 * in the service functions below.
455 static DECLARE_TRANSPORT_CLASS(fc_vport_class,
456 "fc_vports",
457 NULL,
458 NULL,
459 NULL);
462 * Module Parameters
466 * dev_loss_tmo: the default number of seconds that the FC transport
467 * should insulate the loss of a remote port.
468 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
470 static unsigned int fc_dev_loss_tmo = 60; /* seconds */
472 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
473 MODULE_PARM_DESC(dev_loss_tmo,
474 "Maximum number of seconds that the FC transport should"
475 " insulate the loss of a remote port. Once this value is"
476 " exceeded, the scsi target is removed. Value should be"
477 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
480 * Netlink Infrastructure
483 static atomic_t fc_event_seq;
486 * fc_get_event_number - Obtain the next sequential FC event number
488 * Notes:
489 * We could have inlined this, but it would have required fc_event_seq to
490 * be exposed. For now, live with the subroutine call.
491 * Atomic used to avoid lock/unlock...
494 fc_get_event_number(void)
496 return atomic_add_return(1, &fc_event_seq);
498 EXPORT_SYMBOL(fc_get_event_number);
502 * fc_host_post_event - called to post an even on an fc_host.
503 * @shost: host the event occurred on
504 * @event_number: fc event number obtained from get_fc_event_number()
505 * @event_code: fc_host event being posted
506 * @event_data: 32bits of data for the event being posted
508 * Notes:
509 * This routine assumes no locks are held on entry.
511 void
512 fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
513 enum fc_host_event_code event_code, u32 event_data)
515 struct sk_buff *skb;
516 struct nlmsghdr *nlh;
517 struct fc_nl_event *event;
518 const char *name;
519 u32 len, skblen;
520 int err;
522 if (!scsi_nl_sock) {
523 err = -ENOENT;
524 goto send_fail;
527 len = FC_NL_MSGALIGN(sizeof(*event));
528 skblen = NLMSG_SPACE(len);
530 skb = alloc_skb(skblen, GFP_KERNEL);
531 if (!skb) {
532 err = -ENOBUFS;
533 goto send_fail;
536 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
537 skblen - sizeof(*nlh), 0);
538 if (!nlh) {
539 err = -ENOBUFS;
540 goto send_fail_skb;
542 event = NLMSG_DATA(nlh);
544 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
545 FC_NL_ASYNC_EVENT, len);
546 event->seconds = get_seconds();
547 event->vendor_id = 0;
548 event->host_no = shost->host_no;
549 event->event_datalen = sizeof(u32); /* bytes */
550 event->event_num = event_number;
551 event->event_code = event_code;
552 event->event_data = event_data;
554 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
555 GFP_KERNEL);
556 return;
558 send_fail_skb:
559 kfree_skb(skb);
560 send_fail:
561 name = get_fc_host_event_code_name(event_code);
562 printk(KERN_WARNING
563 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
564 __func__, shost->host_no,
565 (name) ? name : "<unknown>", event_data, err);
566 return;
568 EXPORT_SYMBOL(fc_host_post_event);
572 * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
573 * @shost: host the event occurred on
574 * @event_number: fc event number obtained from get_fc_event_number()
575 * @data_len: amount, in bytes, of vendor unique data
576 * @data_buf: pointer to vendor unique data
577 * @vendor_id: Vendor id
579 * Notes:
580 * This routine assumes no locks are held on entry.
582 void
583 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
584 u32 data_len, char * data_buf, u64 vendor_id)
586 struct sk_buff *skb;
587 struct nlmsghdr *nlh;
588 struct fc_nl_event *event;
589 u32 len, skblen;
590 int err;
592 if (!scsi_nl_sock) {
593 err = -ENOENT;
594 goto send_vendor_fail;
597 len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
598 skblen = NLMSG_SPACE(len);
600 skb = alloc_skb(skblen, GFP_KERNEL);
601 if (!skb) {
602 err = -ENOBUFS;
603 goto send_vendor_fail;
606 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
607 skblen - sizeof(*nlh), 0);
608 if (!nlh) {
609 err = -ENOBUFS;
610 goto send_vendor_fail_skb;
612 event = NLMSG_DATA(nlh);
614 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
615 FC_NL_ASYNC_EVENT, len);
616 event->seconds = get_seconds();
617 event->vendor_id = vendor_id;
618 event->host_no = shost->host_no;
619 event->event_datalen = data_len; /* bytes */
620 event->event_num = event_number;
621 event->event_code = FCH_EVT_VENDOR_UNIQUE;
622 memcpy(&event->event_data, data_buf, data_len);
624 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
625 GFP_KERNEL);
626 return;
628 send_vendor_fail_skb:
629 kfree_skb(skb);
630 send_vendor_fail:
631 printk(KERN_WARNING
632 "%s: Dropped Event : host %d vendor_unique - err %d\n",
633 __func__, shost->host_no, err);
634 return;
636 EXPORT_SYMBOL(fc_host_post_vendor_event);
640 static __init int fc_transport_init(void)
642 int error;
644 atomic_set(&fc_event_seq, 0);
646 error = transport_class_register(&fc_host_class);
647 if (error)
648 return error;
649 error = transport_class_register(&fc_vport_class);
650 if (error)
651 goto unreg_host_class;
652 error = transport_class_register(&fc_rport_class);
653 if (error)
654 goto unreg_vport_class;
655 error = transport_class_register(&fc_transport_class);
656 if (error)
657 goto unreg_rport_class;
658 return 0;
660 unreg_rport_class:
661 transport_class_unregister(&fc_rport_class);
662 unreg_vport_class:
663 transport_class_unregister(&fc_vport_class);
664 unreg_host_class:
665 transport_class_unregister(&fc_host_class);
666 return error;
669 static void __exit fc_transport_exit(void)
671 transport_class_unregister(&fc_transport_class);
672 transport_class_unregister(&fc_rport_class);
673 transport_class_unregister(&fc_host_class);
674 transport_class_unregister(&fc_vport_class);
678 * FC Remote Port Attribute Management
681 #define fc_rport_show_function(field, format_string, sz, cast) \
682 static ssize_t \
683 show_fc_rport_##field (struct device *dev, \
684 struct device_attribute *attr, char *buf) \
686 struct fc_rport *rport = transport_class_to_rport(dev); \
687 struct Scsi_Host *shost = rport_to_shost(rport); \
688 struct fc_internal *i = to_fc_internal(shost->transportt); \
689 if ((i->f->get_rport_##field) && \
690 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
691 (rport->port_state == FC_PORTSTATE_DELETED) || \
692 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
693 i->f->get_rport_##field(rport); \
694 return snprintf(buf, sz, format_string, cast rport->field); \
697 #define fc_rport_store_function(field) \
698 static ssize_t \
699 store_fc_rport_##field(struct device *dev, \
700 struct device_attribute *attr, \
701 const char *buf, size_t count) \
703 int val; \
704 struct fc_rport *rport = transport_class_to_rport(dev); \
705 struct Scsi_Host *shost = rport_to_shost(rport); \
706 struct fc_internal *i = to_fc_internal(shost->transportt); \
707 char *cp; \
708 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
709 (rport->port_state == FC_PORTSTATE_DELETED) || \
710 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
711 return -EBUSY; \
712 val = simple_strtoul(buf, &cp, 0); \
713 if (*cp && (*cp != '\n')) \
714 return -EINVAL; \
715 i->f->set_rport_##field(rport, val); \
716 return count; \
719 #define fc_rport_rd_attr(field, format_string, sz) \
720 fc_rport_show_function(field, format_string, sz, ) \
721 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
722 show_fc_rport_##field, NULL)
724 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
725 fc_rport_show_function(field, format_string, sz, (cast)) \
726 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
727 show_fc_rport_##field, NULL)
729 #define fc_rport_rw_attr(field, format_string, sz) \
730 fc_rport_show_function(field, format_string, sz, ) \
731 fc_rport_store_function(field) \
732 static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
733 show_fc_rport_##field, \
734 store_fc_rport_##field)
737 #define fc_private_rport_show_function(field, format_string, sz, cast) \
738 static ssize_t \
739 show_fc_rport_##field (struct device *dev, \
740 struct device_attribute *attr, char *buf) \
742 struct fc_rport *rport = transport_class_to_rport(dev); \
743 return snprintf(buf, sz, format_string, cast rport->field); \
746 #define fc_private_rport_rd_attr(field, format_string, sz) \
747 fc_private_rport_show_function(field, format_string, sz, ) \
748 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
749 show_fc_rport_##field, NULL)
751 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
752 fc_private_rport_show_function(field, format_string, sz, (cast)) \
753 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
754 show_fc_rport_##field, NULL)
757 #define fc_private_rport_rd_enum_attr(title, maxlen) \
758 static ssize_t \
759 show_fc_rport_##title (struct device *dev, \
760 struct device_attribute *attr, char *buf) \
762 struct fc_rport *rport = transport_class_to_rport(dev); \
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_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] = 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] = 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] = 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] = 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 device *dev,
814 struct device_attribute *attr, char *buf)
816 struct fc_rport *rport = transport_class_to_rport(dev);
817 if (rport->supported_classes == FC_COS_UNSPECIFIED)
818 return snprintf(buf, 20, "unspecified\n");
819 return get_fc_cos_names(rport->supported_classes, buf);
821 static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
822 show_fc_rport_supported_classes, NULL);
824 /* Dynamic Remote Port Attributes */
827 * dev_loss_tmo attribute
829 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
830 static ssize_t
831 store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
832 const char *buf, size_t count)
834 int val;
835 struct fc_rport *rport = transport_class_to_rport(dev);
836 struct Scsi_Host *shost = rport_to_shost(rport);
837 struct fc_internal *i = to_fc_internal(shost->transportt);
838 char *cp;
839 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
840 (rport->port_state == FC_PORTSTATE_DELETED) ||
841 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
842 return -EBUSY;
843 val = simple_strtoul(buf, &cp, 0);
844 if ((*cp && (*cp != '\n')) ||
845 (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
846 return -EINVAL;
847 i->f->set_rport_dev_loss_tmo(rport, val);
848 return count;
850 static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
851 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
854 /* Private Remote Port Attributes */
856 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
857 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
858 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
860 static ssize_t
861 show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
862 char *buf)
864 struct fc_rport *rport = transport_class_to_rport(dev);
866 /* identify any roles that are port_id specific */
867 if ((rport->port_id != -1) &&
868 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
869 FC_WELLKNOWN_PORTID_MASK) {
870 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
871 case FC_FPORT_PORTID:
872 return snprintf(buf, 30, "Fabric Port\n");
873 case FC_FABCTLR_PORTID:
874 return snprintf(buf, 30, "Fabric Controller\n");
875 case FC_DIRSRVR_PORTID:
876 return snprintf(buf, 30, "Directory Server\n");
877 case FC_TIMESRVR_PORTID:
878 return snprintf(buf, 30, "Time Server\n");
879 case FC_MGMTSRVR_PORTID:
880 return snprintf(buf, 30, "Management Server\n");
881 default:
882 return snprintf(buf, 30, "Unknown Fabric Entity\n");
884 } else {
885 if (rport->roles == FC_PORT_ROLE_UNKNOWN)
886 return snprintf(buf, 20, "unknown\n");
887 return get_fc_port_roles_names(rport->roles, buf);
890 static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
891 show_fc_rport_roles, NULL);
893 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
894 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
897 * fast_io_fail_tmo attribute
899 static ssize_t
900 show_fc_rport_fast_io_fail_tmo (struct device *dev,
901 struct device_attribute *attr, char *buf)
903 struct fc_rport *rport = transport_class_to_rport(dev);
905 if (rport->fast_io_fail_tmo == -1)
906 return snprintf(buf, 5, "off\n");
907 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
910 static ssize_t
911 store_fc_rport_fast_io_fail_tmo(struct device *dev,
912 struct device_attribute *attr, const char *buf,
913 size_t count)
915 int val;
916 char *cp;
917 struct fc_rport *rport = transport_class_to_rport(dev);
919 if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
920 (rport->port_state == FC_PORTSTATE_DELETED) ||
921 (rport->port_state == FC_PORTSTATE_NOTPRESENT))
922 return -EBUSY;
923 if (strncmp(buf, "off", 3) == 0)
924 rport->fast_io_fail_tmo = -1;
925 else {
926 val = simple_strtoul(buf, &cp, 0);
927 if ((*cp && (*cp != '\n')) ||
928 (val < 0) || (val >= rport->dev_loss_tmo))
929 return -EINVAL;
930 rport->fast_io_fail_tmo = val;
932 return count;
934 static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
935 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
939 * FC SCSI Target Attribute Management
943 * Note: in the target show function we recognize when the remote
944 * port is in the heirarchy and do not allow the driver to get
945 * involved in sysfs functions. The driver only gets involved if
946 * it's the "old" style that doesn't use rports.
948 #define fc_starget_show_function(field, format_string, sz, cast) \
949 static ssize_t \
950 show_fc_starget_##field (struct device *dev, \
951 struct device_attribute *attr, char *buf) \
953 struct scsi_target *starget = transport_class_to_starget(dev); \
954 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
955 struct fc_internal *i = to_fc_internal(shost->transportt); \
956 struct fc_rport *rport = starget_to_rport(starget); \
957 if (rport) \
958 fc_starget_##field(starget) = rport->field; \
959 else if (i->f->get_starget_##field) \
960 i->f->get_starget_##field(starget); \
961 return snprintf(buf, sz, format_string, \
962 cast fc_starget_##field(starget)); \
965 #define fc_starget_rd_attr(field, format_string, sz) \
966 fc_starget_show_function(field, format_string, sz, ) \
967 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
968 show_fc_starget_##field, NULL)
970 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
971 fc_starget_show_function(field, format_string, sz, (cast)) \
972 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
973 show_fc_starget_##field, NULL)
975 #define SETUP_STARGET_ATTRIBUTE_RD(field) \
976 i->private_starget_attrs[count] = device_attr_starget_##field; \
977 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
978 i->private_starget_attrs[count].store = NULL; \
979 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
980 if (i->f->show_starget_##field) \
981 count++
983 #define SETUP_STARGET_ATTRIBUTE_RW(field) \
984 i->private_starget_attrs[count] = device_attr_starget_##field; \
985 if (!i->f->set_starget_##field) { \
986 i->private_starget_attrs[count].attr.mode = S_IRUGO; \
987 i->private_starget_attrs[count].store = NULL; \
989 i->starget_attrs[count] = &i->private_starget_attrs[count]; \
990 if (i->f->show_starget_##field) \
991 count++
993 /* The FC Transport SCSI Target Attributes: */
994 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
995 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
996 fc_starget_rd_attr(port_id, "0x%06x\n", 20);
1000 * FC Virtual Port Attribute Management
1003 #define fc_vport_show_function(field, format_string, sz, cast) \
1004 static ssize_t \
1005 show_fc_vport_##field (struct device *dev, \
1006 struct device_attribute *attr, char *buf) \
1008 struct fc_vport *vport = transport_class_to_vport(dev); \
1009 struct Scsi_Host *shost = vport_to_shost(vport); \
1010 struct fc_internal *i = to_fc_internal(shost->transportt); \
1011 if ((i->f->get_vport_##field) && \
1012 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
1013 i->f->get_vport_##field(vport); \
1014 return snprintf(buf, sz, format_string, cast vport->field); \
1017 #define fc_vport_store_function(field) \
1018 static ssize_t \
1019 store_fc_vport_##field(struct device *dev, \
1020 struct device_attribute *attr, \
1021 const char *buf, size_t count) \
1023 int val; \
1024 struct fc_vport *vport = transport_class_to_vport(dev); \
1025 struct Scsi_Host *shost = vport_to_shost(vport); \
1026 struct fc_internal *i = to_fc_internal(shost->transportt); \
1027 char *cp; \
1028 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1029 return -EBUSY; \
1030 val = simple_strtoul(buf, &cp, 0); \
1031 if (*cp && (*cp != '\n')) \
1032 return -EINVAL; \
1033 i->f->set_vport_##field(vport, val); \
1034 return count; \
1037 #define fc_vport_store_str_function(field, slen) \
1038 static ssize_t \
1039 store_fc_vport_##field(struct device *dev, \
1040 struct device_attribute *attr, \
1041 const char *buf, size_t count) \
1043 struct fc_vport *vport = transport_class_to_vport(dev); \
1044 struct Scsi_Host *shost = vport_to_shost(vport); \
1045 struct fc_internal *i = to_fc_internal(shost->transportt); \
1046 unsigned int cnt=count; \
1048 /* count may include a LF at end of string */ \
1049 if (buf[cnt-1] == '\n') \
1050 cnt--; \
1051 if (cnt > ((slen) - 1)) \
1052 return -EINVAL; \
1053 memcpy(vport->field, buf, cnt); \
1054 i->f->set_vport_##field(vport); \
1055 return count; \
1058 #define fc_vport_rd_attr(field, format_string, sz) \
1059 fc_vport_show_function(field, format_string, sz, ) \
1060 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1061 show_fc_vport_##field, NULL)
1063 #define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
1064 fc_vport_show_function(field, format_string, sz, (cast)) \
1065 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1066 show_fc_vport_##field, NULL)
1068 #define fc_vport_rw_attr(field, format_string, sz) \
1069 fc_vport_show_function(field, format_string, sz, ) \
1070 fc_vport_store_function(field) \
1071 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1072 show_fc_vport_##field, \
1073 store_fc_vport_##field)
1075 #define fc_private_vport_show_function(field, format_string, sz, cast) \
1076 static ssize_t \
1077 show_fc_vport_##field (struct device *dev, \
1078 struct device_attribute *attr, char *buf) \
1080 struct fc_vport *vport = transport_class_to_vport(dev); \
1081 return snprintf(buf, sz, format_string, cast vport->field); \
1084 #define fc_private_vport_store_u32_function(field) \
1085 static ssize_t \
1086 store_fc_vport_##field(struct device *dev, \
1087 struct device_attribute *attr, \
1088 const char *buf, size_t count) \
1090 u32 val; \
1091 struct fc_vport *vport = transport_class_to_vport(dev); \
1092 char *cp; \
1093 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
1094 return -EBUSY; \
1095 val = simple_strtoul(buf, &cp, 0); \
1096 if (*cp && (*cp != '\n')) \
1097 return -EINVAL; \
1098 vport->field = val; \
1099 return count; \
1103 #define fc_private_vport_rd_attr(field, format_string, sz) \
1104 fc_private_vport_show_function(field, format_string, sz, ) \
1105 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1106 show_fc_vport_##field, NULL)
1108 #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
1109 fc_private_vport_show_function(field, format_string, sz, (cast)) \
1110 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
1111 show_fc_vport_##field, NULL)
1113 #define fc_private_vport_rw_u32_attr(field, format_string, sz) \
1114 fc_private_vport_show_function(field, format_string, sz, ) \
1115 fc_private_vport_store_u32_function(field) \
1116 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
1117 show_fc_vport_##field, \
1118 store_fc_vport_##field)
1121 #define fc_private_vport_rd_enum_attr(title, maxlen) \
1122 static ssize_t \
1123 show_fc_vport_##title (struct device *dev, \
1124 struct device_attribute *attr, \
1125 char *buf) \
1127 struct fc_vport *vport = transport_class_to_vport(dev); \
1128 const char *name; \
1129 name = get_fc_##title##_name(vport->title); \
1130 if (!name) \
1131 return -EINVAL; \
1132 return snprintf(buf, maxlen, "%s\n", name); \
1134 static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
1135 show_fc_vport_##title, NULL)
1138 #define SETUP_VPORT_ATTRIBUTE_RD(field) \
1139 i->private_vport_attrs[count] = device_attr_vport_##field; \
1140 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1141 i->private_vport_attrs[count].store = NULL; \
1142 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1143 if (i->f->get_##field) \
1144 count++
1145 /* NOTE: Above MACRO differs: checks function not show bit */
1147 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
1148 i->private_vport_attrs[count] = device_attr_vport_##field; \
1149 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1150 i->private_vport_attrs[count].store = NULL; \
1151 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1152 count++
1154 #define SETUP_VPORT_ATTRIBUTE_WR(field) \
1155 i->private_vport_attrs[count] = device_attr_vport_##field; \
1156 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1157 if (i->f->field) \
1158 count++
1159 /* NOTE: Above MACRO differs: checks function */
1161 #define SETUP_VPORT_ATTRIBUTE_RW(field) \
1162 i->private_vport_attrs[count] = device_attr_vport_##field; \
1163 if (!i->f->set_vport_##field) { \
1164 i->private_vport_attrs[count].attr.mode = S_IRUGO; \
1165 i->private_vport_attrs[count].store = NULL; \
1167 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1168 count++
1169 /* NOTE: Above MACRO differs: does not check show bit */
1171 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
1173 i->private_vport_attrs[count] = device_attr_vport_##field; \
1174 i->vport_attrs[count] = &i->private_vport_attrs[count]; \
1175 count++; \
1179 /* The FC Transport Virtual Port Attributes: */
1181 /* Fixed Virtual Port Attributes */
1183 /* Dynamic Virtual Port Attributes */
1185 /* Private Virtual Port Attributes */
1187 fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
1188 fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
1189 fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1190 fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1192 static ssize_t
1193 show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
1194 char *buf)
1196 struct fc_vport *vport = transport_class_to_vport(dev);
1198 if (vport->roles == FC_PORT_ROLE_UNKNOWN)
1199 return snprintf(buf, 20, "unknown\n");
1200 return get_fc_port_roles_names(vport->roles, buf);
1202 static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
1204 fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
1206 fc_private_vport_show_function(symbolic_name, "%s\n",
1207 FC_VPORT_SYMBOLIC_NAMELEN + 1, )
1208 fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
1209 static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
1210 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
1212 static ssize_t
1213 store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
1214 const char *buf, size_t count)
1216 struct fc_vport *vport = transport_class_to_vport(dev);
1217 struct Scsi_Host *shost = vport_to_shost(vport);
1218 unsigned long flags;
1220 spin_lock_irqsave(shost->host_lock, flags);
1221 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
1222 spin_unlock_irqrestore(shost->host_lock, flags);
1223 return -EBUSY;
1225 vport->flags |= FC_VPORT_DELETING;
1226 spin_unlock_irqrestore(shost->host_lock, flags);
1228 fc_queue_work(shost, &vport->vport_delete_work);
1229 return count;
1231 static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
1232 NULL, store_fc_vport_delete);
1236 * Enable/Disable vport
1237 * Write "1" to disable, write "0" to enable
1239 static ssize_t
1240 store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
1241 const char *buf,
1242 size_t count)
1244 struct fc_vport *vport = transport_class_to_vport(dev);
1245 struct Scsi_Host *shost = vport_to_shost(vport);
1246 struct fc_internal *i = to_fc_internal(shost->transportt);
1247 int stat;
1249 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1250 return -EBUSY;
1252 if (*buf == '0') {
1253 if (vport->vport_state != FC_VPORT_DISABLED)
1254 return -EALREADY;
1255 } else if (*buf == '1') {
1256 if (vport->vport_state == FC_VPORT_DISABLED)
1257 return -EALREADY;
1258 } else
1259 return -EINVAL;
1261 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
1262 return stat ? stat : count;
1264 static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
1265 NULL, store_fc_vport_disable);
1269 * Host Attribute Management
1272 #define fc_host_show_function(field, format_string, sz, cast) \
1273 static ssize_t \
1274 show_fc_host_##field (struct device *dev, \
1275 struct device_attribute *attr, char *buf) \
1277 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1278 struct fc_internal *i = to_fc_internal(shost->transportt); \
1279 if (i->f->get_host_##field) \
1280 i->f->get_host_##field(shost); \
1281 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1284 #define fc_host_store_function(field) \
1285 static ssize_t \
1286 store_fc_host_##field(struct device *dev, \
1287 struct device_attribute *attr, \
1288 const char *buf, size_t count) \
1290 int val; \
1291 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1292 struct fc_internal *i = to_fc_internal(shost->transportt); \
1293 char *cp; \
1295 val = simple_strtoul(buf, &cp, 0); \
1296 if (*cp && (*cp != '\n')) \
1297 return -EINVAL; \
1298 i->f->set_host_##field(shost, val); \
1299 return count; \
1302 #define fc_host_store_str_function(field, slen) \
1303 static ssize_t \
1304 store_fc_host_##field(struct device *dev, \
1305 struct device_attribute *attr, \
1306 const char *buf, size_t count) \
1308 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1309 struct fc_internal *i = to_fc_internal(shost->transportt); \
1310 unsigned int cnt=count; \
1312 /* count may include a LF at end of string */ \
1313 if (buf[cnt-1] == '\n') \
1314 cnt--; \
1315 if (cnt > ((slen) - 1)) \
1316 return -EINVAL; \
1317 memcpy(fc_host_##field(shost), buf, cnt); \
1318 i->f->set_host_##field(shost); \
1319 return count; \
1322 #define fc_host_rd_attr(field, format_string, sz) \
1323 fc_host_show_function(field, format_string, sz, ) \
1324 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1325 show_fc_host_##field, NULL)
1327 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
1328 fc_host_show_function(field, format_string, sz, (cast)) \
1329 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1330 show_fc_host_##field, NULL)
1332 #define fc_host_rw_attr(field, format_string, sz) \
1333 fc_host_show_function(field, format_string, sz, ) \
1334 fc_host_store_function(field) \
1335 static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
1336 show_fc_host_##field, \
1337 store_fc_host_##field)
1339 #define fc_host_rd_enum_attr(title, maxlen) \
1340 static ssize_t \
1341 show_fc_host_##title (struct device *dev, \
1342 struct device_attribute *attr, char *buf) \
1344 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1345 struct fc_internal *i = to_fc_internal(shost->transportt); \
1346 const char *name; \
1347 if (i->f->get_host_##title) \
1348 i->f->get_host_##title(shost); \
1349 name = get_fc_##title##_name(fc_host_##title(shost)); \
1350 if (!name) \
1351 return -EINVAL; \
1352 return snprintf(buf, maxlen, "%s\n", name); \
1354 static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
1356 #define SETUP_HOST_ATTRIBUTE_RD(field) \
1357 i->private_host_attrs[count] = device_attr_host_##field; \
1358 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1359 i->private_host_attrs[count].store = NULL; \
1360 i->host_attrs[count] = &i->private_host_attrs[count]; \
1361 if (i->f->show_host_##field) \
1362 count++
1364 #define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
1365 i->private_host_attrs[count] = device_attr_host_##field; \
1366 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1367 i->private_host_attrs[count].store = NULL; \
1368 i->host_attrs[count] = &i->private_host_attrs[count]; \
1369 count++
1371 #define SETUP_HOST_ATTRIBUTE_RW(field) \
1372 i->private_host_attrs[count] = device_attr_host_##field; \
1373 if (!i->f->set_host_##field) { \
1374 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1375 i->private_host_attrs[count].store = NULL; \
1377 i->host_attrs[count] = &i->private_host_attrs[count]; \
1378 if (i->f->show_host_##field) \
1379 count++
1382 #define fc_private_host_show_function(field, format_string, sz, cast) \
1383 static ssize_t \
1384 show_fc_host_##field (struct device *dev, \
1385 struct device_attribute *attr, char *buf) \
1387 struct Scsi_Host *shost = transport_class_to_shost(dev); \
1388 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
1391 #define fc_private_host_rd_attr(field, format_string, sz) \
1392 fc_private_host_show_function(field, format_string, sz, ) \
1393 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1394 show_fc_host_##field, NULL)
1396 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
1397 fc_private_host_show_function(field, format_string, sz, (cast)) \
1398 static FC_DEVICE_ATTR(host, field, S_IRUGO, \
1399 show_fc_host_##field, NULL)
1401 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
1402 i->private_host_attrs[count] = device_attr_host_##field; \
1403 i->private_host_attrs[count].attr.mode = S_IRUGO; \
1404 i->private_host_attrs[count].store = NULL; \
1405 i->host_attrs[count] = &i->private_host_attrs[count]; \
1406 count++
1408 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
1410 i->private_host_attrs[count] = device_attr_host_##field; \
1411 i->host_attrs[count] = &i->private_host_attrs[count]; \
1412 count++; \
1416 /* Fixed Host Attributes */
1418 static ssize_t
1419 show_fc_host_supported_classes (struct device *dev,
1420 struct device_attribute *attr, char *buf)
1422 struct Scsi_Host *shost = transport_class_to_shost(dev);
1424 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
1425 return snprintf(buf, 20, "unspecified\n");
1427 return get_fc_cos_names(fc_host_supported_classes(shost), buf);
1429 static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
1430 show_fc_host_supported_classes, NULL);
1432 static ssize_t
1433 show_fc_host_supported_fc4s (struct device *dev,
1434 struct device_attribute *attr, char *buf)
1436 struct Scsi_Host *shost = transport_class_to_shost(dev);
1437 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
1439 static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
1440 show_fc_host_supported_fc4s, NULL);
1442 static ssize_t
1443 show_fc_host_supported_speeds (struct device *dev,
1444 struct device_attribute *attr, char *buf)
1446 struct Scsi_Host *shost = transport_class_to_shost(dev);
1448 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
1449 return snprintf(buf, 20, "unknown\n");
1451 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
1453 static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
1454 show_fc_host_supported_speeds, NULL);
1457 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
1458 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
1459 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
1460 unsigned long long);
1461 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
1462 fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
1463 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
1466 /* Dynamic Host Attributes */
1468 static ssize_t
1469 show_fc_host_active_fc4s (struct device *dev,
1470 struct device_attribute *attr, char *buf)
1472 struct Scsi_Host *shost = transport_class_to_shost(dev);
1473 struct fc_internal *i = to_fc_internal(shost->transportt);
1475 if (i->f->get_host_active_fc4s)
1476 i->f->get_host_active_fc4s(shost);
1478 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
1480 static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
1481 show_fc_host_active_fc4s, NULL);
1483 static ssize_t
1484 show_fc_host_speed (struct device *dev,
1485 struct device_attribute *attr, char *buf)
1487 struct Scsi_Host *shost = transport_class_to_shost(dev);
1488 struct fc_internal *i = to_fc_internal(shost->transportt);
1490 if (i->f->get_host_speed)
1491 i->f->get_host_speed(shost);
1493 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
1494 return snprintf(buf, 20, "unknown\n");
1496 return get_fc_port_speed_names(fc_host_speed(shost), buf);
1498 static FC_DEVICE_ATTR(host, speed, S_IRUGO,
1499 show_fc_host_speed, NULL);
1502 fc_host_rd_attr(port_id, "0x%06x\n", 20);
1503 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
1504 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
1505 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
1506 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
1508 fc_private_host_show_function(system_hostname, "%s\n",
1509 FC_SYMBOLIC_NAME_SIZE + 1, )
1510 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
1511 static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
1512 show_fc_host_system_hostname, store_fc_host_system_hostname);
1515 /* Private Host Attributes */
1517 static ssize_t
1518 show_fc_private_host_tgtid_bind_type(struct device *dev,
1519 struct device_attribute *attr, char *buf)
1521 struct Scsi_Host *shost = transport_class_to_shost(dev);
1522 const char *name;
1524 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
1525 if (!name)
1526 return -EINVAL;
1527 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
1530 #define get_list_head_entry(pos, head, member) \
1531 pos = list_entry((head)->next, typeof(*pos), member)
1533 static ssize_t
1534 store_fc_private_host_tgtid_bind_type(struct device *dev,
1535 struct device_attribute *attr, const char *buf, size_t count)
1537 struct Scsi_Host *shost = transport_class_to_shost(dev);
1538 struct fc_rport *rport;
1539 enum fc_tgtid_binding_type val;
1540 unsigned long flags;
1542 if (get_fc_tgtid_bind_type_match(buf, &val))
1543 return -EINVAL;
1545 /* if changing bind type, purge all unused consistent bindings */
1546 if (val != fc_host_tgtid_bind_type(shost)) {
1547 spin_lock_irqsave(shost->host_lock, flags);
1548 while (!list_empty(&fc_host_rport_bindings(shost))) {
1549 get_list_head_entry(rport,
1550 &fc_host_rport_bindings(shost), peers);
1551 list_del(&rport->peers);
1552 rport->port_state = FC_PORTSTATE_DELETED;
1553 fc_queue_work(shost, &rport->rport_delete_work);
1555 spin_unlock_irqrestore(shost->host_lock, flags);
1558 fc_host_tgtid_bind_type(shost) = val;
1559 return count;
1562 static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
1563 show_fc_private_host_tgtid_bind_type,
1564 store_fc_private_host_tgtid_bind_type);
1566 static ssize_t
1567 store_fc_private_host_issue_lip(struct device *dev,
1568 struct device_attribute *attr, const char *buf, size_t count)
1570 struct Scsi_Host *shost = transport_class_to_shost(dev);
1571 struct fc_internal *i = to_fc_internal(shost->transportt);
1572 int ret;
1574 /* ignore any data value written to the attribute */
1575 if (i->f->issue_fc_host_lip) {
1576 ret = i->f->issue_fc_host_lip(shost);
1577 return ret ? ret: count;
1580 return -ENOENT;
1583 static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
1584 store_fc_private_host_issue_lip);
1586 fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
1590 * Host Statistics Management
1593 /* Show a given an attribute in the statistics group */
1594 static ssize_t
1595 fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
1597 struct Scsi_Host *shost = transport_class_to_shost(dev);
1598 struct fc_internal *i = to_fc_internal(shost->transportt);
1599 struct fc_host_statistics *stats;
1600 ssize_t ret = -ENOENT;
1602 if (offset > sizeof(struct fc_host_statistics) ||
1603 offset % sizeof(u64) != 0)
1604 WARN_ON(1);
1606 if (i->f->get_fc_host_stats) {
1607 stats = (i->f->get_fc_host_stats)(shost);
1608 if (stats)
1609 ret = snprintf(buf, 20, "0x%llx\n",
1610 (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
1612 return ret;
1616 /* generate a read-only statistics attribute */
1617 #define fc_host_statistic(name) \
1618 static ssize_t show_fcstat_##name(struct device *cd, \
1619 struct device_attribute *attr, \
1620 char *buf) \
1622 return fc_stat_show(cd, buf, \
1623 offsetof(struct fc_host_statistics, name)); \
1625 static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
1627 fc_host_statistic(seconds_since_last_reset);
1628 fc_host_statistic(tx_frames);
1629 fc_host_statistic(tx_words);
1630 fc_host_statistic(rx_frames);
1631 fc_host_statistic(rx_words);
1632 fc_host_statistic(lip_count);
1633 fc_host_statistic(nos_count);
1634 fc_host_statistic(error_frames);
1635 fc_host_statistic(dumped_frames);
1636 fc_host_statistic(link_failure_count);
1637 fc_host_statistic(loss_of_sync_count);
1638 fc_host_statistic(loss_of_signal_count);
1639 fc_host_statistic(prim_seq_protocol_err_count);
1640 fc_host_statistic(invalid_tx_word_count);
1641 fc_host_statistic(invalid_crc_count);
1642 fc_host_statistic(fcp_input_requests);
1643 fc_host_statistic(fcp_output_requests);
1644 fc_host_statistic(fcp_control_requests);
1645 fc_host_statistic(fcp_input_megabytes);
1646 fc_host_statistic(fcp_output_megabytes);
1648 static ssize_t
1649 fc_reset_statistics(struct device *dev, struct device_attribute *attr,
1650 const char *buf, size_t count)
1652 struct Scsi_Host *shost = transport_class_to_shost(dev);
1653 struct fc_internal *i = to_fc_internal(shost->transportt);
1655 /* ignore any data value written to the attribute */
1656 if (i->f->reset_fc_host_stats) {
1657 i->f->reset_fc_host_stats(shost);
1658 return count;
1661 return -ENOENT;
1663 static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
1664 fc_reset_statistics);
1666 static struct attribute *fc_statistics_attrs[] = {
1667 &device_attr_host_seconds_since_last_reset.attr,
1668 &device_attr_host_tx_frames.attr,
1669 &device_attr_host_tx_words.attr,
1670 &device_attr_host_rx_frames.attr,
1671 &device_attr_host_rx_words.attr,
1672 &device_attr_host_lip_count.attr,
1673 &device_attr_host_nos_count.attr,
1674 &device_attr_host_error_frames.attr,
1675 &device_attr_host_dumped_frames.attr,
1676 &device_attr_host_link_failure_count.attr,
1677 &device_attr_host_loss_of_sync_count.attr,
1678 &device_attr_host_loss_of_signal_count.attr,
1679 &device_attr_host_prim_seq_protocol_err_count.attr,
1680 &device_attr_host_invalid_tx_word_count.attr,
1681 &device_attr_host_invalid_crc_count.attr,
1682 &device_attr_host_fcp_input_requests.attr,
1683 &device_attr_host_fcp_output_requests.attr,
1684 &device_attr_host_fcp_control_requests.attr,
1685 &device_attr_host_fcp_input_megabytes.attr,
1686 &device_attr_host_fcp_output_megabytes.attr,
1687 &device_attr_host_reset_statistics.attr,
1688 NULL
1691 static struct attribute_group fc_statistics_group = {
1692 .name = "statistics",
1693 .attrs = fc_statistics_attrs,
1697 /* Host Vport Attributes */
1699 static int
1700 fc_parse_wwn(const char *ns, u64 *nm)
1702 unsigned int i, j;
1703 u8 wwn[8];
1705 memset(wwn, 0, sizeof(wwn));
1707 /* Validate and store the new name */
1708 for (i=0, j=0; i < 16; i++) {
1709 if ((*ns >= 'a') && (*ns <= 'f'))
1710 j = ((j << 4) | ((*ns++ -'a') + 10));
1711 else if ((*ns >= 'A') && (*ns <= 'F'))
1712 j = ((j << 4) | ((*ns++ -'A') + 10));
1713 else if ((*ns >= '0') && (*ns <= '9'))
1714 j = ((j << 4) | (*ns++ -'0'));
1715 else
1716 return -EINVAL;
1717 if (i % 2) {
1718 wwn[i/2] = j & 0xff;
1719 j = 0;
1723 *nm = wwn_to_u64(wwn);
1725 return 0;
1730 * "Short-cut" sysfs variable to create a new vport on a FC Host.
1731 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
1732 * will default to a NPIV-based FCP_Initiator; The WWNs are specified
1733 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
1735 static ssize_t
1736 store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
1737 const char *buf, size_t count)
1739 struct Scsi_Host *shost = transport_class_to_shost(dev);
1740 struct fc_vport_identifiers vid;
1741 struct fc_vport *vport;
1742 unsigned int cnt=count;
1743 int stat;
1745 memset(&vid, 0, sizeof(vid));
1747 /* count may include a LF at end of string */
1748 if (buf[cnt-1] == '\n')
1749 cnt--;
1751 /* validate we have enough characters for WWPN */
1752 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1753 return -EINVAL;
1755 stat = fc_parse_wwn(&buf[0], &vid.port_name);
1756 if (stat)
1757 return stat;
1759 stat = fc_parse_wwn(&buf[17], &vid.node_name);
1760 if (stat)
1761 return stat;
1763 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1764 vid.vport_type = FC_PORTTYPE_NPIV;
1765 /* vid.symbolic_name is already zero/NULL's */
1766 vid.disable = false; /* always enabled */
1768 /* we only allow support on Channel 0 !!! */
1769 stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
1770 return stat ? stat : count;
1772 static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
1773 store_fc_host_vport_create);
1777 * "Short-cut" sysfs variable to delete a vport on a FC Host.
1778 * Vport is identified by a string containing "<WWPN>:<WWNN>".
1779 * The WWNs are specified as hex characters, and may *not* contain
1780 * any prefixes (e.g. 0x, x, etc)
1782 static ssize_t
1783 store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
1784 const char *buf, size_t count)
1786 struct Scsi_Host *shost = transport_class_to_shost(dev);
1787 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
1788 struct fc_vport *vport;
1789 u64 wwpn, wwnn;
1790 unsigned long flags;
1791 unsigned int cnt=count;
1792 int stat, match;
1794 /* count may include a LF at end of string */
1795 if (buf[cnt-1] == '\n')
1796 cnt--;
1798 /* validate we have enough characters for WWPN */
1799 if ((cnt != (16+1+16)) || (buf[16] != ':'))
1800 return -EINVAL;
1802 stat = fc_parse_wwn(&buf[0], &wwpn);
1803 if (stat)
1804 return stat;
1806 stat = fc_parse_wwn(&buf[17], &wwnn);
1807 if (stat)
1808 return stat;
1810 spin_lock_irqsave(shost->host_lock, flags);
1811 match = 0;
1812 /* we only allow support on Channel 0 !!! */
1813 list_for_each_entry(vport, &fc_host->vports, peers) {
1814 if ((vport->channel == 0) &&
1815 (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
1816 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
1817 break;
1818 vport->flags |= FC_VPORT_DELETING;
1819 match = 1;
1820 break;
1823 spin_unlock_irqrestore(shost->host_lock, flags);
1825 if (!match)
1826 return -ENODEV;
1828 stat = fc_vport_terminate(vport);
1829 return stat ? stat : count;
1831 static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
1832 store_fc_host_vport_delete);
1835 static int fc_host_match(struct attribute_container *cont,
1836 struct device *dev)
1838 struct Scsi_Host *shost;
1839 struct fc_internal *i;
1841 if (!scsi_is_host_device(dev))
1842 return 0;
1844 shost = dev_to_shost(dev);
1845 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1846 != &fc_host_class.class)
1847 return 0;
1849 i = to_fc_internal(shost->transportt);
1851 return &i->t.host_attrs.ac == cont;
1854 static int fc_target_match(struct attribute_container *cont,
1855 struct device *dev)
1857 struct Scsi_Host *shost;
1858 struct fc_internal *i;
1860 if (!scsi_is_target_device(dev))
1861 return 0;
1863 shost = dev_to_shost(dev->parent);
1864 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1865 != &fc_host_class.class)
1866 return 0;
1868 i = to_fc_internal(shost->transportt);
1870 return &i->t.target_attrs.ac == cont;
1873 static void fc_rport_dev_release(struct device *dev)
1875 struct fc_rport *rport = dev_to_rport(dev);
1876 put_device(dev->parent);
1877 kfree(rport);
1880 int scsi_is_fc_rport(const struct device *dev)
1882 return dev->release == fc_rport_dev_release;
1884 EXPORT_SYMBOL(scsi_is_fc_rport);
1886 static int fc_rport_match(struct attribute_container *cont,
1887 struct device *dev)
1889 struct Scsi_Host *shost;
1890 struct fc_internal *i;
1892 if (!scsi_is_fc_rport(dev))
1893 return 0;
1895 shost = dev_to_shost(dev->parent);
1896 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1897 != &fc_host_class.class)
1898 return 0;
1900 i = to_fc_internal(shost->transportt);
1902 return &i->rport_attr_cont.ac == cont;
1906 static void fc_vport_dev_release(struct device *dev)
1908 struct fc_vport *vport = dev_to_vport(dev);
1909 put_device(dev->parent); /* release kobj parent */
1910 kfree(vport);
1913 int scsi_is_fc_vport(const struct device *dev)
1915 return dev->release == fc_vport_dev_release;
1917 EXPORT_SYMBOL(scsi_is_fc_vport);
1919 static int fc_vport_match(struct attribute_container *cont,
1920 struct device *dev)
1922 struct fc_vport *vport;
1923 struct Scsi_Host *shost;
1924 struct fc_internal *i;
1926 if (!scsi_is_fc_vport(dev))
1927 return 0;
1928 vport = dev_to_vport(dev);
1930 shost = vport_to_shost(vport);
1931 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1932 != &fc_host_class.class)
1933 return 0;
1935 i = to_fc_internal(shost->transportt);
1936 return &i->vport_attr_cont.ac == cont;
1941 * fc_timed_out - FC Transport I/O timeout intercept handler
1942 * @scmd: The SCSI command which timed out
1944 * This routine protects against error handlers getting invoked while a
1945 * rport is in a blocked state, typically due to a temporarily loss of
1946 * connectivity. If the error handlers are allowed to proceed, requests
1947 * to abort i/o, reset the target, etc will likely fail as there is no way
1948 * to communicate with the device to perform the requested function. These
1949 * failures may result in the midlayer taking the device offline, requiring
1950 * manual intervention to restore operation.
1952 * This routine, called whenever an i/o times out, validates the state of
1953 * the underlying rport. If the rport is blocked, it returns
1954 * EH_RESET_TIMER, which will continue to reschedule the timeout.
1955 * Eventually, either the device will return, or devloss_tmo will fire,
1956 * and when the timeout then fires, it will be handled normally.
1957 * If the rport is not blocked, normal error handling continues.
1959 * Notes:
1960 * This routine assumes no locks are held on entry.
1962 static enum blk_eh_timer_return
1963 fc_timed_out(struct scsi_cmnd *scmd)
1965 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
1967 if (rport->port_state == FC_PORTSTATE_BLOCKED)
1968 return BLK_EH_RESET_TIMER;
1970 return BLK_EH_NOT_HANDLED;
1974 * Called by fc_user_scan to locate an rport on the shost that
1975 * matches the channel and target id, and invoke scsi_scan_target()
1976 * on the rport.
1978 static void
1979 fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
1981 struct fc_rport *rport;
1982 unsigned long flags;
1984 spin_lock_irqsave(shost->host_lock, flags);
1986 list_for_each_entry(rport, &fc_host_rports(shost), peers) {
1987 if (rport->scsi_target_id == -1)
1988 continue;
1990 if (rport->port_state != FC_PORTSTATE_ONLINE)
1991 continue;
1993 if ((channel == rport->channel) &&
1994 (id == rport->scsi_target_id)) {
1995 spin_unlock_irqrestore(shost->host_lock, flags);
1996 scsi_scan_target(&rport->dev, channel, id, lun, 1);
1997 return;
2001 spin_unlock_irqrestore(shost->host_lock, flags);
2005 * Called via sysfs scan routines. Necessary, as the FC transport
2006 * wants to place all target objects below the rport object. So this
2007 * routine must invoke the scsi_scan_target() routine with the rport
2008 * object as the parent.
2010 static int
2011 fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
2013 uint chlo, chhi;
2014 uint tgtlo, tgthi;
2016 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
2017 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
2018 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
2019 return -EINVAL;
2021 if (channel == SCAN_WILD_CARD) {
2022 chlo = 0;
2023 chhi = shost->max_channel + 1;
2024 } else {
2025 chlo = channel;
2026 chhi = channel + 1;
2029 if (id == SCAN_WILD_CARD) {
2030 tgtlo = 0;
2031 tgthi = shost->max_id;
2032 } else {
2033 tgtlo = id;
2034 tgthi = id + 1;
2037 for ( ; chlo < chhi; chlo++)
2038 for ( ; tgtlo < tgthi; tgtlo++)
2039 fc_user_scan_tgt(shost, chlo, tgtlo, lun);
2041 return 0;
2044 static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
2045 int result)
2047 struct fc_internal *i = to_fc_internal(shost->transportt);
2048 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
2051 static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
2053 struct fc_internal *i = to_fc_internal(shost->transportt);
2054 return i->f->it_nexus_response(shost, nexus, result);
2057 struct scsi_transport_template *
2058 fc_attach_transport(struct fc_function_template *ft)
2060 int count;
2061 struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
2062 GFP_KERNEL);
2064 if (unlikely(!i))
2065 return NULL;
2067 i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
2068 i->t.target_attrs.ac.class = &fc_transport_class.class;
2069 i->t.target_attrs.ac.match = fc_target_match;
2070 i->t.target_size = sizeof(struct fc_starget_attrs);
2071 transport_container_register(&i->t.target_attrs);
2073 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
2074 i->t.host_attrs.ac.class = &fc_host_class.class;
2075 i->t.host_attrs.ac.match = fc_host_match;
2076 i->t.host_size = sizeof(struct fc_host_attrs);
2077 if (ft->get_fc_host_stats)
2078 i->t.host_attrs.statistics = &fc_statistics_group;
2079 transport_container_register(&i->t.host_attrs);
2081 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
2082 i->rport_attr_cont.ac.class = &fc_rport_class.class;
2083 i->rport_attr_cont.ac.match = fc_rport_match;
2084 transport_container_register(&i->rport_attr_cont);
2086 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
2087 i->vport_attr_cont.ac.class = &fc_vport_class.class;
2088 i->vport_attr_cont.ac.match = fc_vport_match;
2089 transport_container_register(&i->vport_attr_cont);
2091 i->f = ft;
2093 /* Transport uses the shost workq for scsi scanning */
2094 i->t.create_work_queue = 1;
2096 i->t.eh_timed_out = fc_timed_out;
2098 i->t.user_scan = fc_user_scan;
2100 /* target-mode drivers' functions */
2101 i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
2102 i->t.it_nexus_response = fc_it_nexus_response;
2105 * Setup SCSI Target Attributes.
2107 count = 0;
2108 SETUP_STARGET_ATTRIBUTE_RD(node_name);
2109 SETUP_STARGET_ATTRIBUTE_RD(port_name);
2110 SETUP_STARGET_ATTRIBUTE_RD(port_id);
2112 BUG_ON(count > FC_STARGET_NUM_ATTRS);
2114 i->starget_attrs[count] = NULL;
2118 * Setup SCSI Host Attributes.
2120 count=0;
2121 SETUP_HOST_ATTRIBUTE_RD(node_name);
2122 SETUP_HOST_ATTRIBUTE_RD(port_name);
2123 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
2124 SETUP_HOST_ATTRIBUTE_RD(supported_classes);
2125 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
2126 SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
2127 SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
2128 if (ft->vport_create) {
2129 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
2130 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
2132 SETUP_HOST_ATTRIBUTE_RD(serial_number);
2134 SETUP_HOST_ATTRIBUTE_RD(port_id);
2135 SETUP_HOST_ATTRIBUTE_RD(port_type);
2136 SETUP_HOST_ATTRIBUTE_RD(port_state);
2137 SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
2138 SETUP_HOST_ATTRIBUTE_RD(speed);
2139 SETUP_HOST_ATTRIBUTE_RD(fabric_name);
2140 SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
2141 SETUP_HOST_ATTRIBUTE_RW(system_hostname);
2143 /* Transport-managed attributes */
2144 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
2145 if (ft->issue_fc_host_lip)
2146 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
2147 if (ft->vport_create)
2148 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
2149 if (ft->vport_delete)
2150 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
2152 BUG_ON(count > FC_HOST_NUM_ATTRS);
2154 i->host_attrs[count] = NULL;
2157 * Setup Remote Port Attributes.
2159 count=0;
2160 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
2161 SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
2162 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
2163 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
2164 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
2165 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
2166 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
2167 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
2168 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
2169 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
2171 BUG_ON(count > FC_RPORT_NUM_ATTRS);
2173 i->rport_attrs[count] = NULL;
2176 * Setup Virtual Port Attributes.
2178 count=0;
2179 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
2180 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
2181 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
2182 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
2183 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
2184 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
2185 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
2186 SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
2187 SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
2189 BUG_ON(count > FC_VPORT_NUM_ATTRS);
2191 i->vport_attrs[count] = NULL;
2193 return &i->t;
2195 EXPORT_SYMBOL(fc_attach_transport);
2197 void fc_release_transport(struct scsi_transport_template *t)
2199 struct fc_internal *i = to_fc_internal(t);
2201 transport_container_unregister(&i->t.target_attrs);
2202 transport_container_unregister(&i->t.host_attrs);
2203 transport_container_unregister(&i->rport_attr_cont);
2204 transport_container_unregister(&i->vport_attr_cont);
2206 kfree(i);
2208 EXPORT_SYMBOL(fc_release_transport);
2211 * fc_queue_work - Queue work to the fc_host workqueue.
2212 * @shost: Pointer to Scsi_Host bound to fc_host.
2213 * @work: Work to queue for execution.
2215 * Return value:
2216 * 1 - work queued for execution
2217 * 0 - work is already queued
2218 * -EINVAL - work queue doesn't exist
2220 static int
2221 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
2223 if (unlikely(!fc_host_work_q(shost))) {
2224 printk(KERN_ERR
2225 "ERROR: FC host '%s' attempted to queue work, "
2226 "when no workqueue created.\n", shost->hostt->name);
2227 dump_stack();
2229 return -EINVAL;
2232 return queue_work(fc_host_work_q(shost), work);
2236 * fc_flush_work - Flush a fc_host's workqueue.
2237 * @shost: Pointer to Scsi_Host bound to fc_host.
2239 static void
2240 fc_flush_work(struct Scsi_Host *shost)
2242 if (!fc_host_work_q(shost)) {
2243 printk(KERN_ERR
2244 "ERROR: FC host '%s' attempted to flush work, "
2245 "when no workqueue created.\n", shost->hostt->name);
2246 dump_stack();
2247 return;
2250 flush_workqueue(fc_host_work_q(shost));
2254 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
2255 * @shost: Pointer to Scsi_Host bound to fc_host.
2256 * @work: Work to queue for execution.
2257 * @delay: jiffies to delay the work queuing
2259 * Return value:
2260 * 1 on success / 0 already queued / < 0 for error
2262 static int
2263 fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
2264 unsigned long delay)
2266 if (unlikely(!fc_host_devloss_work_q(shost))) {
2267 printk(KERN_ERR
2268 "ERROR: FC host '%s' attempted to queue work, "
2269 "when no workqueue created.\n", shost->hostt->name);
2270 dump_stack();
2272 return -EINVAL;
2275 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
2279 * fc_flush_devloss - Flush a fc_host's devloss workqueue.
2280 * @shost: Pointer to Scsi_Host bound to fc_host.
2282 static void
2283 fc_flush_devloss(struct Scsi_Host *shost)
2285 if (!fc_host_devloss_work_q(shost)) {
2286 printk(KERN_ERR
2287 "ERROR: FC host '%s' attempted to flush work, "
2288 "when no workqueue created.\n", shost->hostt->name);
2289 dump_stack();
2290 return;
2293 flush_workqueue(fc_host_devloss_work_q(shost));
2298 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
2299 * @shost: Which &Scsi_Host
2301 * This routine is expected to be called immediately preceeding the
2302 * a driver's call to scsi_remove_host().
2304 * WARNING: A driver utilizing the fc_transport, which fails to call
2305 * this routine prior to scsi_remove_host(), will leave dangling
2306 * objects in /sys/class/fc_remote_ports. Access to any of these
2307 * objects can result in a system crash !!!
2309 * Notes:
2310 * This routine assumes no locks are held on entry.
2312 void
2313 fc_remove_host(struct Scsi_Host *shost)
2315 struct fc_vport *vport = NULL, *next_vport = NULL;
2316 struct fc_rport *rport = NULL, *next_rport = NULL;
2317 struct workqueue_struct *work_q;
2318 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2319 unsigned long flags;
2321 spin_lock_irqsave(shost->host_lock, flags);
2323 /* Remove any vports */
2324 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
2325 fc_queue_work(shost, &vport->vport_delete_work);
2327 /* Remove any remote ports */
2328 list_for_each_entry_safe(rport, next_rport,
2329 &fc_host->rports, peers) {
2330 list_del(&rport->peers);
2331 rport->port_state = FC_PORTSTATE_DELETED;
2332 fc_queue_work(shost, &rport->rport_delete_work);
2335 list_for_each_entry_safe(rport, next_rport,
2336 &fc_host->rport_bindings, peers) {
2337 list_del(&rport->peers);
2338 rport->port_state = FC_PORTSTATE_DELETED;
2339 fc_queue_work(shost, &rport->rport_delete_work);
2342 spin_unlock_irqrestore(shost->host_lock, flags);
2344 /* flush all scan work items */
2345 scsi_flush_work(shost);
2347 /* flush all stgt delete, and rport delete work items, then kill it */
2348 if (fc_host->work_q) {
2349 work_q = fc_host->work_q;
2350 fc_host->work_q = NULL;
2351 destroy_workqueue(work_q);
2354 /* flush all devloss work items, then kill it */
2355 if (fc_host->devloss_work_q) {
2356 work_q = fc_host->devloss_work_q;
2357 fc_host->devloss_work_q = NULL;
2358 destroy_workqueue(work_q);
2361 EXPORT_SYMBOL(fc_remove_host);
2363 static void fc_terminate_rport_io(struct fc_rport *rport)
2365 struct Scsi_Host *shost = rport_to_shost(rport);
2366 struct fc_internal *i = to_fc_internal(shost->transportt);
2368 /* Involve the LLDD if possible to terminate all io on the rport. */
2369 if (i->f->terminate_rport_io)
2370 i->f->terminate_rport_io(rport);
2373 * must unblock to flush queued IO. The caller will have set
2374 * the port_state or flags, so that fc_remote_port_chkready will
2375 * fail IO.
2377 scsi_target_unblock(&rport->dev);
2381 * fc_starget_delete - called to delete the scsi decendents of an rport
2382 * @work: remote port to be operated on.
2384 * Deletes target and all sdevs.
2386 static void
2387 fc_starget_delete(struct work_struct *work)
2389 struct fc_rport *rport =
2390 container_of(work, struct fc_rport, stgt_delete_work);
2392 fc_terminate_rport_io(rport);
2393 scsi_remove_target(&rport->dev);
2398 * fc_rport_final_delete - finish rport termination and delete it.
2399 * @work: remote port to be deleted.
2401 static void
2402 fc_rport_final_delete(struct work_struct *work)
2404 struct fc_rport *rport =
2405 container_of(work, struct fc_rport, rport_delete_work);
2406 struct device *dev = &rport->dev;
2407 struct Scsi_Host *shost = rport_to_shost(rport);
2408 struct fc_internal *i = to_fc_internal(shost->transportt);
2409 unsigned long flags;
2410 int do_callback = 0;
2413 * if a scan is pending, flush the SCSI Host work_q so that
2414 * that we can reclaim the rport scan work element.
2416 if (rport->flags & FC_RPORT_SCAN_PENDING)
2417 scsi_flush_work(shost);
2419 fc_terminate_rport_io(rport);
2422 * Cancel any outstanding timers. These should really exist
2423 * only when rmmod'ing the LLDD and we're asking for
2424 * immediate termination of the rports
2426 spin_lock_irqsave(shost->host_lock, flags);
2427 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
2428 spin_unlock_irqrestore(shost->host_lock, flags);
2429 if (!cancel_delayed_work(&rport->fail_io_work))
2430 fc_flush_devloss(shost);
2431 if (!cancel_delayed_work(&rport->dev_loss_work))
2432 fc_flush_devloss(shost);
2433 spin_lock_irqsave(shost->host_lock, flags);
2434 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
2436 spin_unlock_irqrestore(shost->host_lock, flags);
2438 /* Delete SCSI target and sdevs */
2439 if (rport->scsi_target_id != -1)
2440 fc_starget_delete(&rport->stgt_delete_work);
2443 * Notify the driver that the rport is now dead. The LLDD will
2444 * also guarantee that any communication to the rport is terminated
2446 * Avoid this call if we already called it when we preserved the
2447 * rport for the binding.
2449 spin_lock_irqsave(shost->host_lock, flags);
2450 if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
2451 (i->f->dev_loss_tmo_callbk)) {
2452 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
2453 do_callback = 1;
2455 spin_unlock_irqrestore(shost->host_lock, flags);
2457 if (do_callback)
2458 i->f->dev_loss_tmo_callbk(rport);
2460 fc_bsg_remove(rport->rqst_q);
2462 transport_remove_device(dev);
2463 device_del(dev);
2464 transport_destroy_device(dev);
2465 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2466 put_device(dev); /* for self-reference */
2471 * fc_rport_create - allocates and creates a remote FC port.
2472 * @shost: scsi host the remote port is connected to.
2473 * @channel: Channel on shost port connected to.
2474 * @ids: The world wide names, fc address, and FC4 port
2475 * roles for the remote port.
2477 * Allocates and creates the remoter port structure, including the
2478 * class and sysfs creation.
2480 * Notes:
2481 * This routine assumes no locks are held on entry.
2483 static struct fc_rport *
2484 fc_rport_create(struct Scsi_Host *shost, int channel,
2485 struct fc_rport_identifiers *ids)
2487 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2488 struct fc_internal *fci = to_fc_internal(shost->transportt);
2489 struct fc_rport *rport;
2490 struct device *dev;
2491 unsigned long flags;
2492 int error;
2493 size_t size;
2495 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
2496 rport = kzalloc(size, GFP_KERNEL);
2497 if (unlikely(!rport)) {
2498 printk(KERN_ERR "%s: allocation failure\n", __func__);
2499 return NULL;
2502 rport->maxframe_size = -1;
2503 rport->supported_classes = FC_COS_UNSPECIFIED;
2504 rport->dev_loss_tmo = fc_dev_loss_tmo;
2505 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
2506 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
2507 rport->port_id = ids->port_id;
2508 rport->roles = ids->roles;
2509 rport->port_state = FC_PORTSTATE_ONLINE;
2510 if (fci->f->dd_fcrport_size)
2511 rport->dd_data = &rport[1];
2512 rport->channel = channel;
2513 rport->fast_io_fail_tmo = -1;
2515 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
2516 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
2517 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
2518 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
2519 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
2521 spin_lock_irqsave(shost->host_lock, flags);
2523 rport->number = fc_host->next_rport_number++;
2524 if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
2525 rport->scsi_target_id = fc_host->next_target_id++;
2526 else
2527 rport->scsi_target_id = -1;
2528 list_add_tail(&rport->peers, &fc_host->rports);
2529 get_device(&shost->shost_gendev); /* for fc_host->rport list */
2531 spin_unlock_irqrestore(shost->host_lock, flags);
2533 dev = &rport->dev;
2534 device_initialize(dev); /* takes self reference */
2535 dev->parent = get_device(&shost->shost_gendev); /* parent reference */
2536 dev->release = fc_rport_dev_release;
2537 dev_set_name(dev, "rport-%d:%d-%d",
2538 shost->host_no, channel, rport->number);
2539 transport_setup_device(dev);
2541 error = device_add(dev);
2542 if (error) {
2543 printk(KERN_ERR "FC Remote Port device_add failed\n");
2544 goto delete_rport;
2546 transport_add_device(dev);
2547 transport_configure_device(dev);
2549 fc_bsg_rportadd(shost, rport);
2550 /* ignore any bsg add error - we just can't do sgio */
2552 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
2553 /* initiate a scan of the target */
2554 rport->flags |= FC_RPORT_SCAN_PENDING;
2555 scsi_queue_work(shost, &rport->scan_work);
2558 return rport;
2560 delete_rport:
2561 transport_destroy_device(dev);
2562 spin_lock_irqsave(shost->host_lock, flags);
2563 list_del(&rport->peers);
2564 put_device(&shost->shost_gendev); /* for fc_host->rport list */
2565 spin_unlock_irqrestore(shost->host_lock, flags);
2566 put_device(dev->parent);
2567 kfree(rport);
2568 return NULL;
2572 * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
2573 * @shost: scsi host the remote port is connected to.
2574 * @channel: Channel on shost port connected to.
2575 * @ids: The world wide names, fc address, and FC4 port
2576 * roles for the remote port.
2578 * The LLDD calls this routine to notify the transport of the existence
2579 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
2580 * of the port, it's FC address (port_id), and the FC4 roles that are
2581 * active for the port.
2583 * For ports that are FCP targets (aka scsi targets), the FC transport
2584 * maintains consistent target id bindings on behalf of the LLDD.
2585 * A consistent target id binding is an assignment of a target id to
2586 * a remote port identifier, which persists while the scsi host is
2587 * attached. The remote port can disappear, then later reappear, and
2588 * it's target id assignment remains the same. This allows for shifts
2589 * in FC addressing (if binding by wwpn or wwnn) with no apparent
2590 * changes to the scsi subsystem which is based on scsi host number and
2591 * target id values. Bindings are only valid during the attachment of
2592 * the scsi host. If the host detaches, then later re-attaches, target
2593 * id bindings may change.
2595 * This routine is responsible for returning a remote port structure.
2596 * The routine will search the list of remote ports it maintains
2597 * internally on behalf of consistent target id mappings. If found, the
2598 * remote port structure will be reused. Otherwise, a new remote port
2599 * structure will be allocated.
2601 * Whenever a remote port is allocated, a new fc_remote_port class
2602 * device is created.
2604 * Should not be called from interrupt context.
2606 * Notes:
2607 * This routine assumes no locks are held on entry.
2609 struct fc_rport *
2610 fc_remote_port_add(struct Scsi_Host *shost, int channel,
2611 struct fc_rport_identifiers *ids)
2613 struct fc_internal *fci = to_fc_internal(shost->transportt);
2614 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2615 struct fc_rport *rport;
2616 unsigned long flags;
2617 int match = 0;
2619 /* ensure any stgt delete functions are done */
2620 fc_flush_work(shost);
2623 * Search the list of "active" rports, for an rport that has been
2624 * deleted, but we've held off the real delete while the target
2625 * is in a "blocked" state.
2627 spin_lock_irqsave(shost->host_lock, flags);
2629 list_for_each_entry(rport, &fc_host->rports, peers) {
2631 if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
2632 (rport->channel == channel)) {
2634 switch (fc_host->tgtid_bind_type) {
2635 case FC_TGTID_BIND_BY_WWPN:
2636 case FC_TGTID_BIND_NONE:
2637 if (rport->port_name == ids->port_name)
2638 match = 1;
2639 break;
2640 case FC_TGTID_BIND_BY_WWNN:
2641 if (rport->node_name == ids->node_name)
2642 match = 1;
2643 break;
2644 case FC_TGTID_BIND_BY_ID:
2645 if (rport->port_id == ids->port_id)
2646 match = 1;
2647 break;
2650 if (match) {
2652 memcpy(&rport->node_name, &ids->node_name,
2653 sizeof(rport->node_name));
2654 memcpy(&rport->port_name, &ids->port_name,
2655 sizeof(rport->port_name));
2656 rport->port_id = ids->port_id;
2658 rport->port_state = FC_PORTSTATE_ONLINE;
2659 rport->roles = ids->roles;
2661 spin_unlock_irqrestore(shost->host_lock, flags);
2663 if (fci->f->dd_fcrport_size)
2664 memset(rport->dd_data, 0,
2665 fci->f->dd_fcrport_size);
2668 * If we were not a target, cancel the
2669 * io terminate and rport timers, and
2670 * we're done.
2672 * If we were a target, but our new role
2673 * doesn't indicate a target, leave the
2674 * timers running expecting the role to
2675 * change as the target fully logs in. If
2676 * it doesn't, the target will be torn down.
2678 * If we were a target, and our role shows
2679 * we're still a target, cancel the timers
2680 * and kick off a scan.
2683 /* was a target, not in roles */
2684 if ((rport->scsi_target_id != -1) &&
2685 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
2686 return rport;
2689 * Stop the fail io and dev_loss timers.
2690 * If they flush, the port_state will
2691 * be checked and will NOOP the function.
2693 if (!cancel_delayed_work(&rport->fail_io_work))
2694 fc_flush_devloss(shost);
2695 if (!cancel_delayed_work(&rport->dev_loss_work))
2696 fc_flush_devloss(shost);
2698 spin_lock_irqsave(shost->host_lock, flags);
2700 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2701 FC_RPORT_DEVLOSS_PENDING |
2702 FC_RPORT_DEVLOSS_CALLBK_DONE);
2704 /* if target, initiate a scan */
2705 if (rport->scsi_target_id != -1) {
2706 rport->flags |= FC_RPORT_SCAN_PENDING;
2707 scsi_queue_work(shost,
2708 &rport->scan_work);
2709 spin_unlock_irqrestore(shost->host_lock,
2710 flags);
2711 scsi_target_unblock(&rport->dev);
2712 } else
2713 spin_unlock_irqrestore(shost->host_lock,
2714 flags);
2716 fc_bsg_goose_queue(rport);
2718 return rport;
2724 * Search the bindings array
2725 * Note: if never a FCP target, you won't be on this list
2727 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
2729 /* search for a matching consistent binding */
2731 list_for_each_entry(rport, &fc_host->rport_bindings,
2732 peers) {
2733 if (rport->channel != channel)
2734 continue;
2736 switch (fc_host->tgtid_bind_type) {
2737 case FC_TGTID_BIND_BY_WWPN:
2738 if (rport->port_name == ids->port_name)
2739 match = 1;
2740 break;
2741 case FC_TGTID_BIND_BY_WWNN:
2742 if (rport->node_name == ids->node_name)
2743 match = 1;
2744 break;
2745 case FC_TGTID_BIND_BY_ID:
2746 if (rport->port_id == ids->port_id)
2747 match = 1;
2748 break;
2749 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
2750 break;
2753 if (match) {
2754 list_move_tail(&rport->peers, &fc_host->rports);
2755 break;
2759 if (match) {
2760 memcpy(&rport->node_name, &ids->node_name,
2761 sizeof(rport->node_name));
2762 memcpy(&rport->port_name, &ids->port_name,
2763 sizeof(rport->port_name));
2764 rport->port_id = ids->port_id;
2765 rport->roles = ids->roles;
2766 rport->port_state = FC_PORTSTATE_ONLINE;
2767 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
2769 if (fci->f->dd_fcrport_size)
2770 memset(rport->dd_data, 0,
2771 fci->f->dd_fcrport_size);
2773 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
2774 /* initiate a scan of the target */
2775 rport->flags |= FC_RPORT_SCAN_PENDING;
2776 scsi_queue_work(shost, &rport->scan_work);
2777 spin_unlock_irqrestore(shost->host_lock, flags);
2778 scsi_target_unblock(&rport->dev);
2779 } else
2780 spin_unlock_irqrestore(shost->host_lock, flags);
2782 return rport;
2786 spin_unlock_irqrestore(shost->host_lock, flags);
2788 /* No consistent binding found - create new remote port entry */
2789 rport = fc_rport_create(shost, channel, ids);
2791 return rport;
2793 EXPORT_SYMBOL(fc_remote_port_add);
2797 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
2798 * @rport: The remote port that no longer exists
2800 * The LLDD calls this routine to notify the transport that a remote
2801 * port is no longer part of the topology. Note: Although a port
2802 * may no longer be part of the topology, it may persist in the remote
2803 * ports displayed by the fc_host. We do this under 2 conditions:
2804 * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
2805 * This allows the port to temporarily disappear, then reappear without
2806 * disrupting the SCSI device tree attached to it. During the "blocked"
2807 * period the port will still exist.
2808 * 2) If the port was a scsi target and disappears for longer than we
2809 * expect, we'll delete the port and the tear down the SCSI device tree
2810 * attached to it. However, we want to semi-persist the target id assigned
2811 * to that port if it eventually does exist. The port structure will
2812 * remain (although with minimal information) so that the target id
2813 * bindings remails.
2815 * If the remote port is not an FCP Target, it will be fully torn down
2816 * and deallocated, including the fc_remote_port class device.
2818 * If the remote port is an FCP Target, the port will be placed in a
2819 * temporary blocked state. From the LLDD's perspective, the rport no
2820 * longer exists. From the SCSI midlayer's perspective, the SCSI target
2821 * exists, but all sdevs on it are blocked from further I/O. The following
2822 * is then expected.
2824 * If the remote port does not return (signaled by a LLDD call to
2825 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
2826 * scsi target is removed - killing all outstanding i/o and removing the
2827 * scsi devices attached ot it. The port structure will be marked Not
2828 * Present and be partially cleared, leaving only enough information to
2829 * recognize the remote port relative to the scsi target id binding if
2830 * it later appears. The port will remain as long as there is a valid
2831 * binding (e.g. until the user changes the binding type or unloads the
2832 * scsi host with the binding).
2834 * If the remote port returns within the dev_loss_tmo value (and matches
2835 * according to the target id binding type), the port structure will be
2836 * reused. If it is no longer a SCSI target, the target will be torn
2837 * down. If it continues to be a SCSI target, then the target will be
2838 * unblocked (allowing i/o to be resumed), and a scan will be activated
2839 * to ensure that all luns are detected.
2841 * Called from normal process context only - cannot be called from interrupt.
2843 * Notes:
2844 * This routine assumes no locks are held on entry.
2846 void
2847 fc_remote_port_delete(struct fc_rport *rport)
2849 struct Scsi_Host *shost = rport_to_shost(rport);
2850 int timeout = rport->dev_loss_tmo;
2851 unsigned long flags;
2854 * No need to flush the fc_host work_q's, as all adds are synchronous.
2856 * We do need to reclaim the rport scan work element, so eventually
2857 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
2858 * there's still a scan pending.
2861 spin_lock_irqsave(shost->host_lock, flags);
2863 if (rport->port_state != FC_PORTSTATE_ONLINE) {
2864 spin_unlock_irqrestore(shost->host_lock, flags);
2865 return;
2869 * In the past, we if this was not an FCP-Target, we would
2870 * unconditionally just jump to deleting the rport.
2871 * However, rports can be used as node containers by the LLDD,
2872 * and its not appropriate to just terminate the rport at the
2873 * first sign of a loss in connectivity. The LLDD may want to
2874 * send ELS traffic to re-validate the login. If the rport is
2875 * immediately deleted, it makes it inappropriate for a node
2876 * container.
2877 * So... we now unconditionally wait dev_loss_tmo before
2878 * destroying an rport.
2881 rport->port_state = FC_PORTSTATE_BLOCKED;
2883 rport->flags |= FC_RPORT_DEVLOSS_PENDING;
2885 spin_unlock_irqrestore(shost->host_lock, flags);
2887 if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
2888 shost->active_mode & MODE_TARGET)
2889 fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
2891 scsi_target_block(&rport->dev);
2893 /* see if we need to kill io faster than waiting for device loss */
2894 if ((rport->fast_io_fail_tmo != -1) &&
2895 (rport->fast_io_fail_tmo < timeout))
2896 fc_queue_devloss_work(shost, &rport->fail_io_work,
2897 rport->fast_io_fail_tmo * HZ);
2899 /* cap the length the devices can be blocked until they are deleted */
2900 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
2902 EXPORT_SYMBOL(fc_remote_port_delete);
2905 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
2906 * @rport: The remote port that changed.
2907 * @roles: New roles for this port.
2909 * Description: The LLDD calls this routine to notify the transport that the
2910 * roles on a remote port may have changed. The largest effect of this is
2911 * if a port now becomes a FCP Target, it must be allocated a
2912 * scsi target id. If the port is no longer a FCP target, any
2913 * scsi target id value assigned to it will persist in case the
2914 * role changes back to include FCP Target. No changes in the scsi
2915 * midlayer will be invoked if the role changes (in the expectation
2916 * that the role will be resumed. If it doesn't normal error processing
2917 * will take place).
2919 * Should not be called from interrupt context.
2921 * Notes:
2922 * This routine assumes no locks are held on entry.
2924 void
2925 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
2927 struct Scsi_Host *shost = rport_to_shost(rport);
2928 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
2929 unsigned long flags;
2930 int create = 0;
2931 int ret;
2933 spin_lock_irqsave(shost->host_lock, flags);
2934 if (roles & FC_PORT_ROLE_FCP_TARGET) {
2935 if (rport->scsi_target_id == -1) {
2936 rport->scsi_target_id = fc_host->next_target_id++;
2937 create = 1;
2938 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
2939 create = 1;
2940 } else if (shost->active_mode & MODE_TARGET) {
2941 ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
2942 (char *)&rport->node_name);
2943 if (ret)
2944 printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
2945 ret);
2948 rport->roles = roles;
2950 spin_unlock_irqrestore(shost->host_lock, flags);
2952 if (create) {
2954 * There may have been a delete timer running on the
2955 * port. Ensure that it is cancelled as we now know
2956 * the port is an FCP Target.
2957 * Note: we know the rport is exists and in an online
2958 * state as the LLDD would not have had an rport
2959 * reference to pass us.
2961 * Take no action on the del_timer failure as the state
2962 * machine state change will validate the
2963 * transaction.
2965 if (!cancel_delayed_work(&rport->fail_io_work))
2966 fc_flush_devloss(shost);
2967 if (!cancel_delayed_work(&rport->dev_loss_work))
2968 fc_flush_devloss(shost);
2970 spin_lock_irqsave(shost->host_lock, flags);
2971 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
2972 FC_RPORT_DEVLOSS_PENDING);
2973 spin_unlock_irqrestore(shost->host_lock, flags);
2975 /* ensure any stgt delete functions are done */
2976 fc_flush_work(shost);
2978 /* initiate a scan of the target */
2979 spin_lock_irqsave(shost->host_lock, flags);
2980 rport->flags |= FC_RPORT_SCAN_PENDING;
2981 scsi_queue_work(shost, &rport->scan_work);
2982 spin_unlock_irqrestore(shost->host_lock, flags);
2983 scsi_target_unblock(&rport->dev);
2986 EXPORT_SYMBOL(fc_remote_port_rolechg);
2989 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
2990 * @work: rport target that failed to reappear in the allotted time.
2992 * Description: An attempt to delete a remote port blocks, and if it fails
2993 * to return in the allotted time this gets called.
2995 static void
2996 fc_timeout_deleted_rport(struct work_struct *work)
2998 struct fc_rport *rport =
2999 container_of(work, struct fc_rport, dev_loss_work.work);
3000 struct Scsi_Host *shost = rport_to_shost(rport);
3001 struct fc_internal *i = to_fc_internal(shost->transportt);
3002 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3003 unsigned long flags;
3004 int do_callback = 0;
3006 spin_lock_irqsave(shost->host_lock, flags);
3008 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
3011 * If the port is ONLINE, then it came back. If it was a SCSI
3012 * target, validate it still is. If not, tear down the
3013 * scsi_target on it.
3015 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3016 (rport->scsi_target_id != -1) &&
3017 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
3018 dev_printk(KERN_ERR, &rport->dev,
3019 "blocked FC remote port time out: no longer"
3020 " a FCP target, removing starget\n");
3021 spin_unlock_irqrestore(shost->host_lock, flags);
3022 scsi_target_unblock(&rport->dev);
3023 fc_queue_work(shost, &rport->stgt_delete_work);
3024 return;
3027 /* NOOP state - we're flushing workq's */
3028 if (rport->port_state != FC_PORTSTATE_BLOCKED) {
3029 spin_unlock_irqrestore(shost->host_lock, flags);
3030 dev_printk(KERN_ERR, &rport->dev,
3031 "blocked FC remote port time out: leaving"
3032 " rport%s alone\n",
3033 (rport->scsi_target_id != -1) ? " and starget" : "");
3034 return;
3037 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
3038 (rport->scsi_target_id == -1)) {
3039 list_del(&rport->peers);
3040 rport->port_state = FC_PORTSTATE_DELETED;
3041 dev_printk(KERN_ERR, &rport->dev,
3042 "blocked FC remote port time out: removing"
3043 " rport%s\n",
3044 (rport->scsi_target_id != -1) ? " and starget" : "");
3045 fc_queue_work(shost, &rport->rport_delete_work);
3046 spin_unlock_irqrestore(shost->host_lock, flags);
3047 return;
3050 dev_printk(KERN_ERR, &rport->dev,
3051 "blocked FC remote port time out: removing target and "
3052 "saving binding\n");
3054 list_move_tail(&rport->peers, &fc_host->rport_bindings);
3057 * Note: We do not remove or clear the hostdata area. This allows
3058 * host-specific target data to persist along with the
3059 * scsi_target_id. It's up to the host to manage it's hostdata area.
3063 * Reinitialize port attributes that may change if the port comes back.
3065 rport->maxframe_size = -1;
3066 rport->supported_classes = FC_COS_UNSPECIFIED;
3067 rport->roles = FC_PORT_ROLE_UNKNOWN;
3068 rport->port_state = FC_PORTSTATE_NOTPRESENT;
3069 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
3072 * Pre-emptively kill I/O rather than waiting for the work queue
3073 * item to teardown the starget. (FCOE libFC folks prefer this
3074 * and to have the rport_port_id still set when it's done).
3076 spin_unlock_irqrestore(shost->host_lock, flags);
3077 fc_terminate_rport_io(rport);
3079 spin_lock_irqsave(shost->host_lock, flags);
3081 if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
3083 /* remove the identifiers that aren't used in the consisting binding */
3084 switch (fc_host->tgtid_bind_type) {
3085 case FC_TGTID_BIND_BY_WWPN:
3086 rport->node_name = -1;
3087 rport->port_id = -1;
3088 break;
3089 case FC_TGTID_BIND_BY_WWNN:
3090 rport->port_name = -1;
3091 rport->port_id = -1;
3092 break;
3093 case FC_TGTID_BIND_BY_ID:
3094 rport->node_name = -1;
3095 rport->port_name = -1;
3096 break;
3097 case FC_TGTID_BIND_NONE: /* to keep compiler happy */
3098 break;
3102 * As this only occurs if the remote port (scsi target)
3103 * went away and didn't come back - we'll remove
3104 * all attached scsi devices.
3106 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
3107 fc_queue_work(shost, &rport->stgt_delete_work);
3109 do_callback = 1;
3112 spin_unlock_irqrestore(shost->host_lock, flags);
3115 * Notify the driver that the rport is now dead. The LLDD will
3116 * also guarantee that any communication to the rport is terminated
3118 * Note: we set the CALLBK_DONE flag above to correspond
3120 if (do_callback && i->f->dev_loss_tmo_callbk)
3121 i->f->dev_loss_tmo_callbk(rport);
3126 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
3127 * @work: rport to terminate io on.
3129 * Notes: Only requests the failure of the io, not that all are flushed
3130 * prior to returning.
3132 static void
3133 fc_timeout_fail_rport_io(struct work_struct *work)
3135 struct fc_rport *rport =
3136 container_of(work, struct fc_rport, fail_io_work.work);
3138 if (rport->port_state != FC_PORTSTATE_BLOCKED)
3139 return;
3141 rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
3142 fc_terminate_rport_io(rport);
3146 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
3147 * @work: remote port to be scanned.
3149 static void
3150 fc_scsi_scan_rport(struct work_struct *work)
3152 struct fc_rport *rport =
3153 container_of(work, struct fc_rport, scan_work);
3154 struct Scsi_Host *shost = rport_to_shost(rport);
3155 struct fc_internal *i = to_fc_internal(shost->transportt);
3156 unsigned long flags;
3158 if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
3159 (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
3160 !(i->f->disable_target_scan)) {
3161 scsi_scan_target(&rport->dev, rport->channel,
3162 rport->scsi_target_id, SCAN_WILD_CARD, 1);
3165 spin_lock_irqsave(shost->host_lock, flags);
3166 rport->flags &= ~FC_RPORT_SCAN_PENDING;
3167 spin_unlock_irqrestore(shost->host_lock, flags);
3172 * fc_vport_setup - allocates and creates a FC virtual port.
3173 * @shost: scsi host the virtual port is connected to.
3174 * @channel: Channel on shost port connected to.
3175 * @pdev: parent device for vport
3176 * @ids: The world wide names, FC4 port roles, etc for
3177 * the virtual port.
3178 * @ret_vport: The pointer to the created vport.
3180 * Allocates and creates the vport structure, calls the parent host
3181 * to instantiate the vport, the completes w/ class and sysfs creation.
3183 * Notes:
3184 * This routine assumes no locks are held on entry.
3186 static int
3187 fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
3188 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
3190 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3191 struct fc_internal *fci = to_fc_internal(shost->transportt);
3192 struct fc_vport *vport;
3193 struct device *dev;
3194 unsigned long flags;
3195 size_t size;
3196 int error;
3198 *ret_vport = NULL;
3200 if ( ! fci->f->vport_create)
3201 return -ENOENT;
3203 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
3204 vport = kzalloc(size, GFP_KERNEL);
3205 if (unlikely(!vport)) {
3206 printk(KERN_ERR "%s: allocation failure\n", __func__);
3207 return -ENOMEM;
3210 vport->vport_state = FC_VPORT_UNKNOWN;
3211 vport->vport_last_state = FC_VPORT_UNKNOWN;
3212 vport->node_name = ids->node_name;
3213 vport->port_name = ids->port_name;
3214 vport->roles = ids->roles;
3215 vport->vport_type = ids->vport_type;
3216 if (fci->f->dd_fcvport_size)
3217 vport->dd_data = &vport[1];
3218 vport->shost = shost;
3219 vport->channel = channel;
3220 vport->flags = FC_VPORT_CREATING;
3221 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
3223 spin_lock_irqsave(shost->host_lock, flags);
3225 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
3226 spin_unlock_irqrestore(shost->host_lock, flags);
3227 kfree(vport);
3228 return -ENOSPC;
3230 fc_host->npiv_vports_inuse++;
3231 vport->number = fc_host->next_vport_number++;
3232 list_add_tail(&vport->peers, &fc_host->vports);
3233 get_device(&shost->shost_gendev); /* for fc_host->vport list */
3235 spin_unlock_irqrestore(shost->host_lock, flags);
3237 dev = &vport->dev;
3238 device_initialize(dev); /* takes self reference */
3239 dev->parent = get_device(pdev); /* takes parent reference */
3240 dev->release = fc_vport_dev_release;
3241 dev_set_name(dev, "vport-%d:%d-%d",
3242 shost->host_no, channel, vport->number);
3243 transport_setup_device(dev);
3245 error = device_add(dev);
3246 if (error) {
3247 printk(KERN_ERR "FC Virtual Port device_add failed\n");
3248 goto delete_vport;
3250 transport_add_device(dev);
3251 transport_configure_device(dev);
3253 error = fci->f->vport_create(vport, ids->disable);
3254 if (error) {
3255 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
3256 goto delete_vport_all;
3260 * if the parent isn't the physical adapter's Scsi_Host, ensure
3261 * the Scsi_Host at least contains ia symlink to the vport.
3263 if (pdev != &shost->shost_gendev) {
3264 error = sysfs_create_link(&shost->shost_gendev.kobj,
3265 &dev->kobj, dev_name(dev));
3266 if (error)
3267 printk(KERN_ERR
3268 "%s: Cannot create vport symlinks for "
3269 "%s, err=%d\n",
3270 __func__, dev_name(dev), error);
3272 spin_lock_irqsave(shost->host_lock, flags);
3273 vport->flags &= ~FC_VPORT_CREATING;
3274 spin_unlock_irqrestore(shost->host_lock, flags);
3276 dev_printk(KERN_NOTICE, pdev,
3277 "%s created via shost%d channel %d\n", dev_name(dev),
3278 shost->host_no, channel);
3280 *ret_vport = vport;
3282 return 0;
3284 delete_vport_all:
3285 transport_remove_device(dev);
3286 device_del(dev);
3287 delete_vport:
3288 transport_destroy_device(dev);
3289 spin_lock_irqsave(shost->host_lock, flags);
3290 list_del(&vport->peers);
3291 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3292 fc_host->npiv_vports_inuse--;
3293 spin_unlock_irqrestore(shost->host_lock, flags);
3294 put_device(dev->parent);
3295 kfree(vport);
3297 return error;
3301 * fc_vport_create - Admin App or LLDD requests creation of a vport
3302 * @shost: scsi host the virtual port is connected to.
3303 * @channel: channel on shost port connected to.
3304 * @ids: The world wide names, FC4 port roles, etc for
3305 * the virtual port.
3307 * Notes:
3308 * This routine assumes no locks are held on entry.
3310 struct fc_vport *
3311 fc_vport_create(struct Scsi_Host *shost, int channel,
3312 struct fc_vport_identifiers *ids)
3314 int stat;
3315 struct fc_vport *vport;
3317 stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
3318 ids, &vport);
3319 return stat ? NULL : vport;
3321 EXPORT_SYMBOL(fc_vport_create);
3324 * fc_vport_terminate - Admin App or LLDD requests termination of a vport
3325 * @vport: fc_vport to be terminated
3327 * Calls the LLDD vport_delete() function, then deallocates and removes
3328 * the vport from the shost and object tree.
3330 * Notes:
3331 * This routine assumes no locks are held on entry.
3334 fc_vport_terminate(struct fc_vport *vport)
3336 struct Scsi_Host *shost = vport_to_shost(vport);
3337 struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
3338 struct fc_internal *i = to_fc_internal(shost->transportt);
3339 struct device *dev = &vport->dev;
3340 unsigned long flags;
3341 int stat;
3343 if (i->f->vport_delete)
3344 stat = i->f->vport_delete(vport);
3345 else
3346 stat = -ENOENT;
3348 spin_lock_irqsave(shost->host_lock, flags);
3349 vport->flags &= ~FC_VPORT_DELETING;
3350 if (!stat) {
3351 vport->flags |= FC_VPORT_DELETED;
3352 list_del(&vport->peers);
3353 fc_host->npiv_vports_inuse--;
3354 put_device(&shost->shost_gendev); /* for fc_host->vport list */
3356 spin_unlock_irqrestore(shost->host_lock, flags);
3358 if (stat)
3359 return stat;
3361 if (dev->parent != &shost->shost_gendev)
3362 sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
3363 transport_remove_device(dev);
3364 device_del(dev);
3365 transport_destroy_device(dev);
3368 * Removing our self-reference should mean our
3369 * release function gets called, which will drop the remaining
3370 * parent reference and free the data structure.
3372 put_device(dev); /* for self-reference */
3374 return 0; /* SUCCESS */
3376 EXPORT_SYMBOL(fc_vport_terminate);
3379 * fc_vport_sched_delete - workq-based delete request for a vport
3380 * @work: vport to be deleted.
3382 static void
3383 fc_vport_sched_delete(struct work_struct *work)
3385 struct fc_vport *vport =
3386 container_of(work, struct fc_vport, vport_delete_work);
3387 int stat;
3389 stat = fc_vport_terminate(vport);
3390 if (stat)
3391 dev_printk(KERN_ERR, vport->dev.parent,
3392 "%s: %s could not be deleted created via "
3393 "shost%d channel %d - error %d\n", __func__,
3394 dev_name(&vport->dev), vport->shost->host_no,
3395 vport->channel, stat);
3400 * BSG support
3405 * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
3406 * @job: fc_bsg_job that is to be torn down
3408 static void
3409 fc_destroy_bsgjob(struct fc_bsg_job *job)
3411 unsigned long flags;
3413 spin_lock_irqsave(&job->job_lock, flags);
3414 if (job->ref_cnt) {
3415 spin_unlock_irqrestore(&job->job_lock, flags);
3416 return;
3418 spin_unlock_irqrestore(&job->job_lock, flags);
3420 put_device(job->dev); /* release reference for the request */
3422 kfree(job->request_payload.sg_list);
3423 kfree(job->reply_payload.sg_list);
3424 kfree(job);
3428 * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
3429 * completed
3430 * @job: fc_bsg_job that is complete
3432 static void
3433 fc_bsg_jobdone(struct fc_bsg_job *job)
3435 struct request *req = job->req;
3436 struct request *rsp = req->next_rq;
3437 int err;
3439 err = job->req->errors = job->reply->result;
3441 if (err < 0)
3442 /* we're only returning the result field in the reply */
3443 job->req->sense_len = sizeof(uint32_t);
3444 else
3445 job->req->sense_len = job->reply_len;
3447 /* we assume all request payload was transferred, residual == 0 */
3448 req->resid_len = 0;
3450 if (rsp) {
3451 WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);
3453 /* set reply (bidi) residual */
3454 rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
3455 rsp->resid_len);
3457 blk_complete_request(req);
3461 * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
3462 * @rq: BSG request that holds the job to be destroyed
3464 static void fc_bsg_softirq_done(struct request *rq)
3466 struct fc_bsg_job *job = rq->special;
3467 unsigned long flags;
3469 spin_lock_irqsave(&job->job_lock, flags);
3470 job->state_flags |= FC_RQST_STATE_DONE;
3471 job->ref_cnt--;
3472 spin_unlock_irqrestore(&job->job_lock, flags);
3474 blk_end_request_all(rq, rq->errors);
3475 fc_destroy_bsgjob(job);
3479 * fc_bsg_job_timeout - handler for when a bsg request timesout
3480 * @req: request that timed out
3482 static enum blk_eh_timer_return
3483 fc_bsg_job_timeout(struct request *req)
3485 struct fc_bsg_job *job = (void *) req->special;
3486 struct Scsi_Host *shost = job->shost;
3487 struct fc_internal *i = to_fc_internal(shost->transportt);
3488 unsigned long flags;
3489 int err = 0, done = 0;
3491 if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
3492 return BLK_EH_RESET_TIMER;
3494 spin_lock_irqsave(&job->job_lock, flags);
3495 if (job->state_flags & FC_RQST_STATE_DONE)
3496 done = 1;
3497 else
3498 job->ref_cnt++;
3499 spin_unlock_irqrestore(&job->job_lock, flags);
3501 if (!done && i->f->bsg_timeout) {
3502 /* call LLDD to abort the i/o as it has timed out */
3503 err = i->f->bsg_timeout(job);
3504 if (err)
3505 printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
3506 "abort failed with status %d\n", err);
3509 /* the blk_end_sync_io() doesn't check the error */
3510 if (done)
3511 return BLK_EH_NOT_HANDLED;
3512 else
3513 return BLK_EH_HANDLED;
3516 static int
3517 fc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
3519 size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
3521 BUG_ON(!req->nr_phys_segments);
3523 buf->sg_list = kzalloc(sz, GFP_KERNEL);
3524 if (!buf->sg_list)
3525 return -ENOMEM;
3526 sg_init_table(buf->sg_list, req->nr_phys_segments);
3527 buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
3528 buf->payload_len = blk_rq_bytes(req);
3529 return 0;
3534 * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
3535 * bsg request
3536 * @shost: SCSI Host corresponding to the bsg object
3537 * @rport: (optional) FC Remote Port corresponding to the bsg object
3538 * @req: BSG request that needs a job structure
3540 static int
3541 fc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
3542 struct request *req)
3544 struct fc_internal *i = to_fc_internal(shost->transportt);
3545 struct request *rsp = req->next_rq;
3546 struct fc_bsg_job *job;
3547 int ret;
3549 BUG_ON(req->special);
3551 job = kzalloc(sizeof(struct fc_bsg_job) + i->f->dd_bsg_size,
3552 GFP_KERNEL);
3553 if (!job)
3554 return -ENOMEM;
3557 * Note: this is a bit silly.
3558 * The request gets formatted as a SGIO v4 ioctl request, which
3559 * then gets reformatted as a blk request, which then gets
3560 * reformatted as a fc bsg request. And on completion, we have
3561 * to wrap return results such that SGIO v4 thinks it was a scsi
3562 * status. I hope this was all worth it.
3565 req->special = job;
3566 job->shost = shost;
3567 job->rport = rport;
3568 job->req = req;
3569 if (i->f->dd_bsg_size)
3570 job->dd_data = (void *)&job[1];
3571 spin_lock_init(&job->job_lock);
3572 job->request = (struct fc_bsg_request *)req->cmd;
3573 job->request_len = req->cmd_len;
3574 job->reply = req->sense;
3575 job->reply_len = SCSI_SENSE_BUFFERSIZE; /* Size of sense buffer
3576 * allocated */
3577 if (req->bio) {
3578 ret = fc_bsg_map_buffer(&job->request_payload, req);
3579 if (ret)
3580 goto failjob_rls_job;
3582 if (rsp && rsp->bio) {
3583 ret = fc_bsg_map_buffer(&job->reply_payload, rsp);
3584 if (ret)
3585 goto failjob_rls_rqst_payload;
3587 job->job_done = fc_bsg_jobdone;
3588 if (rport)
3589 job->dev = &rport->dev;
3590 else
3591 job->dev = &shost->shost_gendev;
3592 get_device(job->dev); /* take a reference for the request */
3594 job->ref_cnt = 1;
3596 return 0;
3599 failjob_rls_rqst_payload:
3600 kfree(job->request_payload.sg_list);
3601 failjob_rls_job:
3602 kfree(job);
3603 return -ENOMEM;
3607 enum fc_dispatch_result {
3608 FC_DISPATCH_BREAK, /* on return, q is locked, break from q loop */
3609 FC_DISPATCH_LOCKED, /* on return, q is locked, continue on */
3610 FC_DISPATCH_UNLOCKED, /* on return, q is unlocked, continue on */
3615 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
3616 * @q: fc host request queue
3617 * @shost: scsi host rport attached to
3618 * @job: bsg job to be processed
3620 static enum fc_dispatch_result
3621 fc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3622 struct fc_bsg_job *job)
3624 struct fc_internal *i = to_fc_internal(shost->transportt);
3625 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3626 int ret;
3628 /* Validate the host command */
3629 switch (job->request->msgcode) {
3630 case FC_BSG_HST_ADD_RPORT:
3631 cmdlen += sizeof(struct fc_bsg_host_add_rport);
3632 break;
3634 case FC_BSG_HST_DEL_RPORT:
3635 cmdlen += sizeof(struct fc_bsg_host_del_rport);
3636 break;
3638 case FC_BSG_HST_ELS_NOLOGIN:
3639 cmdlen += sizeof(struct fc_bsg_host_els);
3640 /* there better be a xmt and rcv payloads */
3641 if ((!job->request_payload.payload_len) ||
3642 (!job->reply_payload.payload_len)) {
3643 ret = -EINVAL;
3644 goto fail_host_msg;
3646 break;
3648 case FC_BSG_HST_CT:
3649 cmdlen += sizeof(struct fc_bsg_host_ct);
3650 /* there better be xmt and rcv payloads */
3651 if ((!job->request_payload.payload_len) ||
3652 (!job->reply_payload.payload_len)) {
3653 ret = -EINVAL;
3654 goto fail_host_msg;
3656 break;
3658 case FC_BSG_HST_VENDOR:
3659 cmdlen += sizeof(struct fc_bsg_host_vendor);
3660 if ((shost->hostt->vendor_id == 0L) ||
3661 (job->request->rqst_data.h_vendor.vendor_id !=
3662 shost->hostt->vendor_id)) {
3663 ret = -ESRCH;
3664 goto fail_host_msg;
3666 break;
3668 default:
3669 ret = -EBADR;
3670 goto fail_host_msg;
3673 /* check if we really have all the request data needed */
3674 if (job->request_len < cmdlen) {
3675 ret = -ENOMSG;
3676 goto fail_host_msg;
3679 ret = i->f->bsg_request(job);
3680 if (!ret)
3681 return FC_DISPATCH_UNLOCKED;
3683 fail_host_msg:
3684 /* return the errno failure code as the only status */
3685 BUG_ON(job->reply_len < sizeof(uint32_t));
3686 job->reply->reply_payload_rcv_len = 0;
3687 job->reply->result = ret;
3688 job->reply_len = sizeof(uint32_t);
3689 fc_bsg_jobdone(job);
3690 return FC_DISPATCH_UNLOCKED;
3695 * fc_bsg_goose_queue - restart rport queue in case it was stopped
3696 * @rport: rport to be restarted
3698 static void
3699 fc_bsg_goose_queue(struct fc_rport *rport)
3701 int flagset;
3702 unsigned long flags;
3704 if (!rport->rqst_q)
3705 return;
3707 get_device(&rport->dev);
3709 spin_lock_irqsave(rport->rqst_q->queue_lock, flags);
3710 flagset = test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags) &&
3711 !test_bit(QUEUE_FLAG_REENTER, &rport->rqst_q->queue_flags);
3712 if (flagset)
3713 queue_flag_set(QUEUE_FLAG_REENTER, rport->rqst_q);
3714 __blk_run_queue(rport->rqst_q);
3715 if (flagset)
3716 queue_flag_clear(QUEUE_FLAG_REENTER, rport->rqst_q);
3717 spin_unlock_irqrestore(rport->rqst_q->queue_lock, flags);
3719 put_device(&rport->dev);
3724 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
3725 * @q: rport request queue
3726 * @shost: scsi host rport attached to
3727 * @rport: rport request destined to
3728 * @job: bsg job to be processed
3730 static enum fc_dispatch_result
3731 fc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
3732 struct fc_rport *rport, struct fc_bsg_job *job)
3734 struct fc_internal *i = to_fc_internal(shost->transportt);
3735 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
3736 int ret;
3738 /* Validate the rport command */
3739 switch (job->request->msgcode) {
3740 case FC_BSG_RPT_ELS:
3741 cmdlen += sizeof(struct fc_bsg_rport_els);
3742 goto check_bidi;
3744 case FC_BSG_RPT_CT:
3745 cmdlen += sizeof(struct fc_bsg_rport_ct);
3746 check_bidi:
3747 /* there better be xmt and rcv payloads */
3748 if ((!job->request_payload.payload_len) ||
3749 (!job->reply_payload.payload_len)) {
3750 ret = -EINVAL;
3751 goto fail_rport_msg;
3753 break;
3754 default:
3755 ret = -EBADR;
3756 goto fail_rport_msg;
3759 /* check if we really have all the request data needed */
3760 if (job->request_len < cmdlen) {
3761 ret = -ENOMSG;
3762 goto fail_rport_msg;
3765 ret = i->f->bsg_request(job);
3766 if (!ret)
3767 return FC_DISPATCH_UNLOCKED;
3769 fail_rport_msg:
3770 /* return the errno failure code as the only status */
3771 BUG_ON(job->reply_len < sizeof(uint32_t));
3772 job->reply->reply_payload_rcv_len = 0;
3773 job->reply->result = ret;
3774 job->reply_len = sizeof(uint32_t);
3775 fc_bsg_jobdone(job);
3776 return FC_DISPATCH_UNLOCKED;
3781 * fc_bsg_request_handler - generic handler for bsg requests
3782 * @q: request queue to manage
3783 * @shost: Scsi_Host related to the bsg object
3784 * @rport: FC remote port related to the bsg object (optional)
3785 * @dev: device structure for bsg object
3787 static void
3788 fc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
3789 struct fc_rport *rport, struct device *dev)
3791 struct request *req;
3792 struct fc_bsg_job *job;
3793 enum fc_dispatch_result ret;
3795 if (!get_device(dev))
3796 return;
3798 while (!blk_queue_plugged(q)) {
3799 if (rport && (rport->port_state == FC_PORTSTATE_BLOCKED) &&
3800 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
3801 break;
3803 req = blk_fetch_request(q);
3804 if (!req)
3805 break;
3807 if (rport && (rport->port_state != FC_PORTSTATE_ONLINE)) {
3808 req->errors = -ENXIO;
3809 spin_unlock_irq(q->queue_lock);
3810 blk_end_request(req, -ENXIO, blk_rq_bytes(req));
3811 spin_lock_irq(q->queue_lock);
3812 continue;
3815 spin_unlock_irq(q->queue_lock);
3817 ret = fc_req_to_bsgjob(shost, rport, req);
3818 if (ret) {
3819 req->errors = ret;
3820 blk_end_request(req, ret, blk_rq_bytes(req));
3821 spin_lock_irq(q->queue_lock);
3822 continue;
3825 job = req->special;
3827 /* check if we have the msgcode value at least */
3828 if (job->request_len < sizeof(uint32_t)) {
3829 BUG_ON(job->reply_len < sizeof(uint32_t));
3830 job->reply->reply_payload_rcv_len = 0;
3831 job->reply->result = -ENOMSG;
3832 job->reply_len = sizeof(uint32_t);
3833 fc_bsg_jobdone(job);
3834 spin_lock_irq(q->queue_lock);
3835 continue;
3838 /* the dispatch routines will unlock the queue_lock */
3839 if (rport)
3840 ret = fc_bsg_rport_dispatch(q, shost, rport, job);
3841 else
3842 ret = fc_bsg_host_dispatch(q, shost, job);
3844 /* did dispatcher hit state that can't process any more */
3845 if (ret == FC_DISPATCH_BREAK)
3846 break;
3848 /* did dispatcher had released the lock */
3849 if (ret == FC_DISPATCH_UNLOCKED)
3850 spin_lock_irq(q->queue_lock);
3853 spin_unlock_irq(q->queue_lock);
3854 put_device(dev);
3855 spin_lock_irq(q->queue_lock);
3860 * fc_bsg_host_handler - handler for bsg requests for a fc host
3861 * @q: fc host request queue
3863 static void
3864 fc_bsg_host_handler(struct request_queue *q)
3866 struct Scsi_Host *shost = q->queuedata;
3868 fc_bsg_request_handler(q, shost, NULL, &shost->shost_gendev);
3873 * fc_bsg_rport_handler - handler for bsg requests for a fc rport
3874 * @q: rport request queue
3876 static void
3877 fc_bsg_rport_handler(struct request_queue *q)
3879 struct fc_rport *rport = q->queuedata;
3880 struct Scsi_Host *shost = rport_to_shost(rport);
3882 fc_bsg_request_handler(q, shost, rport, &rport->dev);
3887 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
3888 * @shost: shost for fc_host
3889 * @fc_host: fc_host adding the structures to
3891 static int
3892 fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
3894 struct device *dev = &shost->shost_gendev;
3895 struct fc_internal *i = to_fc_internal(shost->transportt);
3896 struct request_queue *q;
3897 int err;
3898 char bsg_name[20];
3900 fc_host->rqst_q = NULL;
3902 if (!i->f->bsg_request)
3903 return -ENOTSUPP;
3905 snprintf(bsg_name, sizeof(bsg_name),
3906 "fc_host%d", shost->host_no);
3908 q = __scsi_alloc_queue(shost, fc_bsg_host_handler);
3909 if (!q) {
3910 printk(KERN_ERR "fc_host%d: bsg interface failed to "
3911 "initialize - no request queue\n",
3912 shost->host_no);
3913 return -ENOMEM;
3916 q->queuedata = shost;
3917 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
3918 blk_queue_softirq_done(q, fc_bsg_softirq_done);
3919 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
3920 blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
3922 err = bsg_register_queue(q, dev, bsg_name, NULL);
3923 if (err) {
3924 printk(KERN_ERR "fc_host%d: bsg interface failed to "
3925 "initialize - register queue\n",
3926 shost->host_no);
3927 blk_cleanup_queue(q);
3928 return err;
3931 fc_host->rqst_q = q;
3932 return 0;
3937 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
3938 * @shost: shost that rport is attached to
3939 * @rport: rport that the bsg hooks are being attached to
3941 static int
3942 fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
3944 struct device *dev = &rport->dev;
3945 struct fc_internal *i = to_fc_internal(shost->transportt);
3946 struct request_queue *q;
3947 int err;
3949 rport->rqst_q = NULL;
3951 if (!i->f->bsg_request)
3952 return -ENOTSUPP;
3954 q = __scsi_alloc_queue(shost, fc_bsg_rport_handler);
3955 if (!q) {
3956 printk(KERN_ERR "%s: bsg interface failed to "
3957 "initialize - no request queue\n",
3958 dev->kobj.name);
3959 return -ENOMEM;
3962 q->queuedata = rport;
3963 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
3964 blk_queue_softirq_done(q, fc_bsg_softirq_done);
3965 blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
3966 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
3968 err = bsg_register_queue(q, dev, NULL, NULL);
3969 if (err) {
3970 printk(KERN_ERR "%s: bsg interface failed to "
3971 "initialize - register queue\n",
3972 dev->kobj.name);
3973 blk_cleanup_queue(q);
3974 return err;
3977 rport->rqst_q = q;
3978 return 0;
3983 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
3984 * @q: the request_queue that is to be torn down.
3986 static void
3987 fc_bsg_remove(struct request_queue *q)
3989 if (q) {
3990 bsg_unregister_queue(q);
3991 blk_cleanup_queue(q);
3996 /* Original Author: Martin Hicks */
3997 MODULE_AUTHOR("James Smart");
3998 MODULE_DESCRIPTION("FC Transport Attributes");
3999 MODULE_LICENSE("GPL");
4001 module_init(fc_transport_init);
4002 module_exit(fc_transport_exit);