1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * FiberChannel transport specific attributes exported to sysfs. 4 * 5 * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved. 6 * 7 * ======== 8 * 9 * Copyright (C) 2004-2007 James Smart, Emulex Corporation 10 * Rewrite for host, target, device, and remote port attributes, 11 * statistics, and service functions... 12 * Add vports, etc 13 */ 14 #include <linux/module.h> 15 #include <linux/init.h> 16 #include <linux/slab.h> 17 #include <linux/delay.h> 18 #include <linux/kernel.h> 19 #include <linux/bsg-lib.h> 20 #include <scsi/scsi_device.h> 21 #include <scsi/scsi_host.h> 22 #include <scsi/scsi_transport.h> 23 #include <scsi/scsi_transport_fc.h> 24 #include <scsi/scsi_cmnd.h> 25 #include <net/netlink.h> 26 #include <scsi/scsi_netlink_fc.h> 27 #include <scsi/scsi_bsg_fc.h> 28 #include "scsi_priv.h" 29 30 static int fc_queue_work(struct Scsi_Host *, struct work_struct *); 31 static void fc_vport_sched_delete(struct work_struct *work); 32 static int fc_vport_setup(struct Scsi_Host *shost, int channel, 33 struct device *pdev, struct fc_vport_identifiers *ids, 34 struct fc_vport **vport); 35 static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *); 36 static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *); 37 static void fc_bsg_remove(struct request_queue *); 38 static void fc_bsg_goose_queue(struct fc_rport *); 39 40 /* 41 * Module Parameters 42 */ 43 44 /* 45 * dev_loss_tmo: the default number of seconds that the FC transport 46 * should insulate the loss of a remote port. 47 * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT. 48 */ 49 static unsigned int fc_dev_loss_tmo = 60; /* seconds */ 50 51 module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR); 52 MODULE_PARM_DESC(dev_loss_tmo, 53 "Maximum number of seconds that the FC transport should" 54 " insulate the loss of a remote port. Once this value is" 55 " exceeded, the scsi target is removed. Value should be" 56 " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if" 57 " fast_io_fail_tmo is not set."); 58 59 /* 60 * Redefine so that we can have same named attributes in the 61 * sdev/starget/host objects. 62 */ 63 #define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \ 64 struct device_attribute device_attr_##_prefix##_##_name = \ 65 __ATTR(_name,_mode,_show,_store) 66 67 #define fc_enum_name_search(title, table_type, table) \ 68 static const char *get_fc_##title##_name(enum table_type table_key) \ 69 { \ 70 int i; \ 71 char *name = NULL; \ 72 \ 73 for (i = 0; i < ARRAY_SIZE(table); i++) { \ 74 if (table[i].value == table_key) { \ 75 name = table[i].name; \ 76 break; \ 77 } \ 78 } \ 79 return name; \ 80 } 81 82 #define fc_enum_name_match(title, table_type, table) \ 83 static int get_fc_##title##_match(const char *table_key, \ 84 enum table_type *value) \ 85 { \ 86 int i; \ 87 \ 88 for (i = 0; i < ARRAY_SIZE(table); i++) { \ 89 if (strncmp(table_key, table[i].name, \ 90 table[i].matchlen) == 0) { \ 91 *value = table[i].value; \ 92 return 0; /* success */ \ 93 } \ 94 } \ 95 return 1; /* failure */ \ 96 } 97 98 99 /* Convert fc_port_type values to ascii string name */ 100 static struct { 101 enum fc_port_type value; 102 char *name; 103 } fc_port_type_names[] = { 104 { FC_PORTTYPE_UNKNOWN, "Unknown" }, 105 { FC_PORTTYPE_OTHER, "Other" }, 106 { FC_PORTTYPE_NOTPRESENT, "Not Present" }, 107 { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" }, 108 { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" }, 109 { FC_PORTTYPE_LPORT, "LPort (private loop)" }, 110 { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" }, 111 { FC_PORTTYPE_NPIV, "NPIV VPORT" }, 112 }; 113 fc_enum_name_search(port_type, fc_port_type, fc_port_type_names) 114 #define FC_PORTTYPE_MAX_NAMELEN 50 115 116 /* Reuse fc_port_type enum function for vport_type */ 117 #define get_fc_vport_type_name get_fc_port_type_name 118 119 120 /* Convert fc_host_event_code values to ascii string name */ 121 static const struct { 122 enum fc_host_event_code value; 123 char *name; 124 } fc_host_event_code_names[] = { 125 { FCH_EVT_LIP, "lip" }, 126 { FCH_EVT_LINKUP, "link_up" }, 127 { FCH_EVT_LINKDOWN, "link_down" }, 128 { FCH_EVT_LIPRESET, "lip_reset" }, 129 { FCH_EVT_RSCN, "rscn" }, 130 { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" }, 131 { FCH_EVT_PORT_UNKNOWN, "port_unknown" }, 132 { FCH_EVT_PORT_ONLINE, "port_online" }, 133 { FCH_EVT_PORT_OFFLINE, "port_offline" }, 134 { FCH_EVT_PORT_FABRIC, "port_fabric" }, 135 { FCH_EVT_LINK_UNKNOWN, "link_unknown" }, 136 { FCH_EVT_LINK_FPIN, "link_FPIN" }, 137 { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" }, 138 }; 139 fc_enum_name_search(host_event_code, fc_host_event_code, 140 fc_host_event_code_names) 141 #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30 142 143 144 /* Convert fc_port_state values to ascii string name */ 145 static struct { 146 enum fc_port_state value; 147 char *name; 148 } fc_port_state_names[] = { 149 { FC_PORTSTATE_UNKNOWN, "Unknown" }, 150 { FC_PORTSTATE_NOTPRESENT, "Not Present" }, 151 { FC_PORTSTATE_ONLINE, "Online" }, 152 { FC_PORTSTATE_OFFLINE, "Offline" }, 153 { FC_PORTSTATE_BLOCKED, "Blocked" }, 154 { FC_PORTSTATE_BYPASSED, "Bypassed" }, 155 { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" }, 156 { FC_PORTSTATE_LINKDOWN, "Linkdown" }, 157 { FC_PORTSTATE_ERROR, "Error" }, 158 { FC_PORTSTATE_LOOPBACK, "Loopback" }, 159 { FC_PORTSTATE_DELETED, "Deleted" }, 160 }; 161 fc_enum_name_search(port_state, fc_port_state, fc_port_state_names) 162 #define FC_PORTSTATE_MAX_NAMELEN 20 163 164 165 /* Convert fc_vport_state values to ascii string name */ 166 static struct { 167 enum fc_vport_state value; 168 char *name; 169 } fc_vport_state_names[] = { 170 { FC_VPORT_UNKNOWN, "Unknown" }, 171 { FC_VPORT_ACTIVE, "Active" }, 172 { FC_VPORT_DISABLED, "Disabled" }, 173 { FC_VPORT_LINKDOWN, "Linkdown" }, 174 { FC_VPORT_INITIALIZING, "Initializing" }, 175 { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" }, 176 { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" }, 177 { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" }, 178 { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" }, 179 { FC_VPORT_FAILED, "VPort Failed" }, 180 }; 181 fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names) 182 #define FC_VPORTSTATE_MAX_NAMELEN 24 183 184 /* Reuse fc_vport_state enum function for vport_last_state */ 185 #define get_fc_vport_last_state_name get_fc_vport_state_name 186 187 188 /* Convert fc_tgtid_binding_type values to ascii string name */ 189 static const struct { 190 enum fc_tgtid_binding_type value; 191 char *name; 192 int matchlen; 193 } fc_tgtid_binding_type_names[] = { 194 { FC_TGTID_BIND_NONE, "none", 4 }, 195 { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 }, 196 { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 }, 197 { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 }, 198 }; 199 fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type, 200 fc_tgtid_binding_type_names) 201 fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type, 202 fc_tgtid_binding_type_names) 203 #define FC_BINDTYPE_MAX_NAMELEN 30 204 205 206 #define fc_bitfield_name_search(title, table) \ 207 static ssize_t \ 208 get_fc_##title##_names(u32 table_key, char *buf) \ 209 { \ 210 char *prefix = ""; \ 211 ssize_t len = 0; \ 212 int i; \ 213 \ 214 for (i = 0; i < ARRAY_SIZE(table); i++) { \ 215 if (table[i].value & table_key) { \ 216 len += sprintf(buf + len, "%s%s", \ 217 prefix, table[i].name); \ 218 prefix = ", "; \ 219 } \ 220 } \ 221 len += sprintf(buf + len, "\n"); \ 222 return len; \ 223 } 224 225 226 /* Convert FC_COS bit values to ascii string name */ 227 static const struct { 228 u32 value; 229 char *name; 230 } fc_cos_names[] = { 231 { FC_COS_CLASS1, "Class 1" }, 232 { FC_COS_CLASS2, "Class 2" }, 233 { FC_COS_CLASS3, "Class 3" }, 234 { FC_COS_CLASS4, "Class 4" }, 235 { FC_COS_CLASS6, "Class 6" }, 236 }; 237 fc_bitfield_name_search(cos, fc_cos_names) 238 239 240 /* Convert FC_PORTSPEED bit values to ascii string name */ 241 static const struct { 242 u32 value; 243 char *name; 244 } fc_port_speed_names[] = { 245 { FC_PORTSPEED_1GBIT, "1 Gbit" }, 246 { FC_PORTSPEED_2GBIT, "2 Gbit" }, 247 { FC_PORTSPEED_4GBIT, "4 Gbit" }, 248 { FC_PORTSPEED_10GBIT, "10 Gbit" }, 249 { FC_PORTSPEED_8GBIT, "8 Gbit" }, 250 { FC_PORTSPEED_16GBIT, "16 Gbit" }, 251 { FC_PORTSPEED_32GBIT, "32 Gbit" }, 252 { FC_PORTSPEED_20GBIT, "20 Gbit" }, 253 { FC_PORTSPEED_40GBIT, "40 Gbit" }, 254 { FC_PORTSPEED_50GBIT, "50 Gbit" }, 255 { FC_PORTSPEED_100GBIT, "100 Gbit" }, 256 { FC_PORTSPEED_25GBIT, "25 Gbit" }, 257 { FC_PORTSPEED_64GBIT, "64 Gbit" }, 258 { FC_PORTSPEED_128GBIT, "128 Gbit" }, 259 { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" }, 260 }; 261 fc_bitfield_name_search(port_speed, fc_port_speed_names) 262 263 264 static int 265 show_fc_fc4s (char *buf, u8 *fc4_list) 266 { 267 int i, len=0; 268 269 for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++) 270 len += sprintf(buf + len , "0x%02x ", *fc4_list); 271 len += sprintf(buf + len, "\n"); 272 return len; 273 } 274 275 276 /* Convert FC_PORT_ROLE bit values to ascii string name */ 277 static const struct { 278 u32 value; 279 char *name; 280 } fc_port_role_names[] = { 281 { FC_PORT_ROLE_FCP_TARGET, "FCP Target" }, 282 { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" }, 283 { FC_PORT_ROLE_IP_PORT, "IP Port" }, 284 { FC_PORT_ROLE_FCP_DUMMY_INITIATOR, "FCP Dummy Initiator" }, 285 { FC_PORT_ROLE_NVME_INITIATOR, "NVMe Initiator" }, 286 { FC_PORT_ROLE_NVME_TARGET, "NVMe Target" }, 287 { FC_PORT_ROLE_NVME_DISCOVERY, "NVMe Discovery" }, 288 }; 289 fc_bitfield_name_search(port_roles, fc_port_role_names) 290 291 /* 292 * Define roles that are specific to port_id. Values are relative to ROLE_MASK. 293 */ 294 #define FC_WELLKNOWN_PORTID_MASK 0xfffff0 295 #define FC_WELLKNOWN_ROLE_MASK 0x00000f 296 #define FC_FPORT_PORTID 0x00000e 297 #define FC_FABCTLR_PORTID 0x00000d 298 #define FC_DIRSRVR_PORTID 0x00000c 299 #define FC_TIMESRVR_PORTID 0x00000b 300 #define FC_MGMTSRVR_PORTID 0x00000a 301 302 303 static void fc_timeout_deleted_rport(struct work_struct *work); 304 static void fc_timeout_fail_rport_io(struct work_struct *work); 305 static void fc_scsi_scan_rport(struct work_struct *work); 306 307 /* 308 * Attribute counts pre object type... 309 * Increase these values if you add attributes 310 */ 311 #define FC_STARGET_NUM_ATTRS 3 312 #define FC_RPORT_NUM_ATTRS 10 313 #define FC_VPORT_NUM_ATTRS 9 314 #define FC_HOST_NUM_ATTRS 29 315 316 struct fc_internal { 317 struct scsi_transport_template t; 318 struct fc_function_template *f; 319 320 /* 321 * For attributes : each object has : 322 * An array of the actual attributes structures 323 * An array of null-terminated pointers to the attribute 324 * structures - used for mid-layer interaction. 325 * 326 * The attribute containers for the starget and host are are 327 * part of the midlayer. As the remote port is specific to the 328 * fc transport, we must provide the attribute container. 329 */ 330 struct device_attribute private_starget_attrs[ 331 FC_STARGET_NUM_ATTRS]; 332 struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1]; 333 334 struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS]; 335 struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1]; 336 337 struct transport_container rport_attr_cont; 338 struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS]; 339 struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1]; 340 341 struct transport_container vport_attr_cont; 342 struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS]; 343 struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1]; 344 }; 345 346 #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t) 347 348 static int fc_target_setup(struct transport_container *tc, struct device *dev, 349 struct device *cdev) 350 { 351 struct scsi_target *starget = to_scsi_target(dev); 352 struct fc_rport *rport = starget_to_rport(starget); 353 354 /* 355 * if parent is remote port, use values from remote port. 356 * Otherwise, this host uses the fc_transport, but not the 357 * remote port interface. As such, initialize to known non-values. 358 */ 359 if (rport) { 360 fc_starget_node_name(starget) = rport->node_name; 361 fc_starget_port_name(starget) = rport->port_name; 362 fc_starget_port_id(starget) = rport->port_id; 363 } else { 364 fc_starget_node_name(starget) = -1; 365 fc_starget_port_name(starget) = -1; 366 fc_starget_port_id(starget) = -1; 367 } 368 369 return 0; 370 } 371 372 static DECLARE_TRANSPORT_CLASS(fc_transport_class, 373 "fc_transport", 374 fc_target_setup, 375 NULL, 376 NULL); 377 378 static int fc_host_setup(struct transport_container *tc, struct device *dev, 379 struct device *cdev) 380 { 381 struct Scsi_Host *shost = dev_to_shost(dev); 382 struct fc_host_attrs *fc_host = shost_to_fc_host(shost); 383 384 /* 385 * Set default values easily detected by the midlayer as 386 * failure cases. The scsi lldd is responsible for initializing 387 * all transport attributes to valid values per host. 388 */ 389 fc_host->node_name = -1; 390 fc_host->port_name = -1; 391 fc_host->permanent_port_name = -1; 392 fc_host->supported_classes = FC_COS_UNSPECIFIED; 393 memset(fc_host->supported_fc4s, 0, 394 sizeof(fc_host->supported_fc4s)); 395 fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN; 396 fc_host->maxframe_size = -1; 397 fc_host->max_npiv_vports = 0; 398 memset(fc_host->serial_number, 0, 399 sizeof(fc_host->serial_number)); 400 memset(fc_host->manufacturer, 0, 401 sizeof(fc_host->manufacturer)); 402 memset(fc_host->model, 0, 403 sizeof(fc_host->model)); 404 memset(fc_host->model_description, 0, 405 sizeof(fc_host->model_description)); 406 memset(fc_host->hardware_version, 0, 407 sizeof(fc_host->hardware_version)); 408 memset(fc_host->driver_version, 0, 409 sizeof(fc_host->driver_version)); 410 memset(fc_host->firmware_version, 0, 411 sizeof(fc_host->firmware_version)); 412 memset(fc_host->optionrom_version, 0, 413 sizeof(fc_host->optionrom_version)); 414 415 fc_host->port_id = -1; 416 fc_host->port_type = FC_PORTTYPE_UNKNOWN; 417 fc_host->port_state = FC_PORTSTATE_UNKNOWN; 418 memset(fc_host->active_fc4s, 0, 419 sizeof(fc_host->active_fc4s)); 420 fc_host->speed = FC_PORTSPEED_UNKNOWN; 421 fc_host->fabric_name = -1; 422 memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name)); 423 memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname)); 424 425 fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN; 426 427 INIT_LIST_HEAD(&fc_host->rports); 428 INIT_LIST_HEAD(&fc_host->rport_bindings); 429 INIT_LIST_HEAD(&fc_host->vports); 430 fc_host->next_rport_number = 0; 431 fc_host->next_target_id = 0; 432 fc_host->next_vport_number = 0; 433 fc_host->npiv_vports_inuse = 0; 434 435 snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name), 436 "fc_wq_%d", shost->host_no); 437 fc_host->work_q = alloc_workqueue("%s", 0, 0, fc_host->work_q_name); 438 if (!fc_host->work_q) 439 return -ENOMEM; 440 441 fc_host->dev_loss_tmo = fc_dev_loss_tmo; 442 snprintf(fc_host->devloss_work_q_name, 443 sizeof(fc_host->devloss_work_q_name), 444 "fc_dl_%d", shost->host_no); 445 fc_host->devloss_work_q = alloc_workqueue("%s", 0, 0, 446 fc_host->devloss_work_q_name); 447 if (!fc_host->devloss_work_q) { 448 destroy_workqueue(fc_host->work_q); 449 fc_host->work_q = NULL; 450 return -ENOMEM; 451 } 452 453 fc_bsg_hostadd(shost, fc_host); 454 /* ignore any bsg add error - we just can't do sgio */ 455 456 return 0; 457 } 458 459 static int fc_host_remove(struct transport_container *tc, struct device *dev, 460 struct device *cdev) 461 { 462 struct Scsi_Host *shost = dev_to_shost(dev); 463 struct fc_host_attrs *fc_host = shost_to_fc_host(shost); 464 465 fc_bsg_remove(fc_host->rqst_q); 466 return 0; 467 } 468 469 static DECLARE_TRANSPORT_CLASS(fc_host_class, 470 "fc_host", 471 fc_host_setup, 472 fc_host_remove, 473 NULL); 474 475 /* 476 * Setup and Remove actions for remote ports are handled 477 * in the service functions below. 478 */ 479 static DECLARE_TRANSPORT_CLASS(fc_rport_class, 480 "fc_remote_ports", 481 NULL, 482 NULL, 483 NULL); 484 485 /* 486 * Setup and Remove actions for virtual ports are handled 487 * in the service functions below. 488 */ 489 static DECLARE_TRANSPORT_CLASS(fc_vport_class, 490 "fc_vports", 491 NULL, 492 NULL, 493 NULL); 494 495 /* 496 * Netlink Infrastructure 497 */ 498 499 static atomic_t fc_event_seq; 500 501 /** 502 * fc_get_event_number - Obtain the next sequential FC event number 503 * 504 * Notes: 505 * We could have inlined this, but it would have required fc_event_seq to 506 * be exposed. For now, live with the subroutine call. 507 * Atomic used to avoid lock/unlock... 508 */ 509 u32 510 fc_get_event_number(void) 511 { 512 return atomic_add_return(1, &fc_event_seq); 513 } 514 EXPORT_SYMBOL(fc_get_event_number); 515 516 /** 517 * fc_host_post_fc_event - routine to do the work of posting an event 518 * on an fc_host. 519 * @shost: host the event occurred on 520 * @event_number: fc event number obtained from get_fc_event_number() 521 * @event_code: fc_host event being posted 522 * @data_len: amount, in bytes, of event data 523 * @data_buf: pointer to event data 524 * @vendor_id: value for Vendor id 525 * 526 * Notes: 527 * This routine assumes no locks are held on entry. 528 */ 529 void 530 fc_host_post_fc_event(struct Scsi_Host *shost, u32 event_number, 531 enum fc_host_event_code event_code, 532 u32 data_len, char *data_buf, u64 vendor_id) 533 { 534 struct sk_buff *skb; 535 struct nlmsghdr *nlh; 536 struct fc_nl_event *event; 537 const char *name; 538 u32 len; 539 int err; 540 541 if (!data_buf || data_len < 4) 542 data_len = 0; 543 544 if (!scsi_nl_sock) { 545 err = -ENOENT; 546 goto send_fail; 547 } 548 549 len = FC_NL_MSGALIGN(sizeof(*event) + data_len); 550 551 skb = nlmsg_new(len, GFP_KERNEL); 552 if (!skb) { 553 err = -ENOBUFS; 554 goto send_fail; 555 } 556 557 nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0); 558 if (!nlh) { 559 err = -ENOBUFS; 560 goto send_fail_skb; 561 } 562 event = nlmsg_data(nlh); 563 564 INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC, 565 FC_NL_ASYNC_EVENT, len); 566 event->seconds = ktime_get_real_seconds(); 567 event->vendor_id = vendor_id; 568 event->host_no = shost->host_no; 569 event->event_datalen = data_len; /* bytes */ 570 event->event_num = event_number; 571 event->event_code = event_code; 572 if (data_len) 573 memcpy(&event->event_data, data_buf, data_len); 574 575 nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS, 576 GFP_KERNEL); 577 return; 578 579 send_fail_skb: 580 kfree_skb(skb); 581 send_fail: 582 name = get_fc_host_event_code_name(event_code); 583 printk(KERN_WARNING 584 "%s: Dropped Event : host %d %s data 0x%08x - err %d\n", 585 __func__, shost->host_no, 586 (name) ? name : "<unknown>", 587 (data_len) ? *((u32 *)data_buf) : 0xFFFFFFFF, err); 588 return; 589 } 590 EXPORT_SYMBOL(fc_host_post_fc_event); 591 592 /** 593 * fc_host_post_event - called to post an even on an fc_host. 594 * @shost: host the event occurred on 595 * @event_number: fc event number obtained from get_fc_event_number() 596 * @event_code: fc_host event being posted 597 * @event_data: 32bits of data for the event being posted 598 * 599 * Notes: 600 * This routine assumes no locks are held on entry. 601 */ 602 void 603 fc_host_post_event(struct Scsi_Host *shost, u32 event_number, 604 enum fc_host_event_code event_code, u32 event_data) 605 { 606 fc_host_post_fc_event(shost, event_number, event_code, 607 (u32)sizeof(u32), (char *)&event_data, 0); 608 } 609 EXPORT_SYMBOL(fc_host_post_event); 610 611 612 /** 613 * fc_host_post_vendor_event - called to post a vendor unique event 614 * on an fc_host 615 * @shost: host the event occurred on 616 * @event_number: fc event number obtained from get_fc_event_number() 617 * @data_len: amount, in bytes, of vendor unique data 618 * @data_buf: pointer to vendor unique data 619 * @vendor_id: Vendor id 620 * 621 * Notes: 622 * This routine assumes no locks are held on entry. 623 */ 624 void 625 fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number, 626 u32 data_len, char * data_buf, u64 vendor_id) 627 { 628 fc_host_post_fc_event(shost, event_number, FCH_EVT_VENDOR_UNIQUE, 629 data_len, data_buf, vendor_id); 630 } 631 EXPORT_SYMBOL(fc_host_post_vendor_event); 632 633 /** 634 * fc_host_rcv_fpin - routine to process a received FPIN. 635 * @shost: host the FPIN was received on 636 * @fpin_len: length of FPIN payload, in bytes 637 * @fpin_buf: pointer to FPIN payload 638 * 639 * Notes: 640 * This routine assumes no locks are held on entry. 641 */ 642 void 643 fc_host_fpin_rcv(struct Scsi_Host *shost, u32 fpin_len, char *fpin_buf) 644 { 645 fc_host_post_fc_event(shost, fc_get_event_number(), 646 FCH_EVT_LINK_FPIN, fpin_len, fpin_buf, 0); 647 } 648 EXPORT_SYMBOL(fc_host_fpin_rcv); 649 650 651 static __init int fc_transport_init(void) 652 { 653 int error; 654 655 atomic_set(&fc_event_seq, 0); 656 657 error = transport_class_register(&fc_host_class); 658 if (error) 659 return error; 660 error = transport_class_register(&fc_vport_class); 661 if (error) 662 goto unreg_host_class; 663 error = transport_class_register(&fc_rport_class); 664 if (error) 665 goto unreg_vport_class; 666 error = transport_class_register(&fc_transport_class); 667 if (error) 668 goto unreg_rport_class; 669 return 0; 670 671 unreg_rport_class: 672 transport_class_unregister(&fc_rport_class); 673 unreg_vport_class: 674 transport_class_unregister(&fc_vport_class); 675 unreg_host_class: 676 transport_class_unregister(&fc_host_class); 677 return error; 678 } 679 680 static void __exit fc_transport_exit(void) 681 { 682 transport_class_unregister(&fc_transport_class); 683 transport_class_unregister(&fc_rport_class); 684 transport_class_unregister(&fc_host_class); 685 transport_class_unregister(&fc_vport_class); 686 } 687 688 /* 689 * FC Remote Port Attribute Management 690 */ 691 692 #define fc_rport_show_function(field, format_string, sz, cast) \ 693 static ssize_t \ 694 show_fc_rport_##field (struct device *dev, \ 695 struct device_attribute *attr, char *buf) \ 696 { \ 697 struct fc_rport *rport = transport_class_to_rport(dev); \ 698 struct Scsi_Host *shost = rport_to_shost(rport); \ 699 struct fc_internal *i = to_fc_internal(shost->transportt); \ 700 if ((i->f->get_rport_##field) && \ 701 !((rport->port_state == FC_PORTSTATE_BLOCKED) || \ 702 (rport->port_state == FC_PORTSTATE_DELETED) || \ 703 (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \ 704 i->f->get_rport_##field(rport); \ 705 return snprintf(buf, sz, format_string, cast rport->field); \ 706 } 707 708 #define fc_rport_store_function(field) \ 709 static ssize_t \ 710 store_fc_rport_##field(struct device *dev, \ 711 struct device_attribute *attr, \ 712 const char *buf, size_t count) \ 713 { \ 714 int val; \ 715 struct fc_rport *rport = transport_class_to_rport(dev); \ 716 struct Scsi_Host *shost = rport_to_shost(rport); \ 717 struct fc_internal *i = to_fc_internal(shost->transportt); \ 718 char *cp; \ 719 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \ 720 (rport->port_state == FC_PORTSTATE_DELETED) || \ 721 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \ 722 return -EBUSY; \ 723 val = simple_strtoul(buf, &cp, 0); \ 724 if (*cp && (*cp != '\n')) \ 725 return -EINVAL; \ 726 i->f->set_rport_##field(rport, val); \ 727 return count; \ 728 } 729 730 #define fc_rport_rd_attr(field, format_string, sz) \ 731 fc_rport_show_function(field, format_string, sz, ) \ 732 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \ 733 show_fc_rport_##field, NULL) 734 735 #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \ 736 fc_rport_show_function(field, format_string, sz, (cast)) \ 737 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \ 738 show_fc_rport_##field, NULL) 739 740 #define fc_rport_rw_attr(field, format_string, sz) \ 741 fc_rport_show_function(field, format_string, sz, ) \ 742 fc_rport_store_function(field) \ 743 static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \ 744 show_fc_rport_##field, \ 745 store_fc_rport_##field) 746 747 748 #define fc_private_rport_show_function(field, format_string, sz, cast) \ 749 static ssize_t \ 750 show_fc_rport_##field (struct device *dev, \ 751 struct device_attribute *attr, char *buf) \ 752 { \ 753 struct fc_rport *rport = transport_class_to_rport(dev); \ 754 return snprintf(buf, sz, format_string, cast rport->field); \ 755 } 756 757 #define fc_private_rport_rd_attr(field, format_string, sz) \ 758 fc_private_rport_show_function(field, format_string, sz, ) \ 759 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \ 760 show_fc_rport_##field, NULL) 761 762 #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \ 763 fc_private_rport_show_function(field, format_string, sz, (cast)) \ 764 static FC_DEVICE_ATTR(rport, field, S_IRUGO, \ 765 show_fc_rport_##field, NULL) 766 767 768 #define fc_private_rport_rd_enum_attr(title, maxlen) \ 769 static ssize_t \ 770 show_fc_rport_##title (struct device *dev, \ 771 struct device_attribute *attr, char *buf) \ 772 { \ 773 struct fc_rport *rport = transport_class_to_rport(dev); \ 774 const char *name; \ 775 name = get_fc_##title##_name(rport->title); \ 776 if (!name) \ 777 return -EINVAL; \ 778 return snprintf(buf, maxlen, "%s\n", name); \ 779 } \ 780 static FC_DEVICE_ATTR(rport, title, S_IRUGO, \ 781 show_fc_rport_##title, NULL) 782 783 784 #define SETUP_RPORT_ATTRIBUTE_RD(field) \ 785 i->private_rport_attrs[count] = device_attr_rport_##field; \ 786 i->private_rport_attrs[count].attr.mode = S_IRUGO; \ 787 i->private_rport_attrs[count].store = NULL; \ 788 i->rport_attrs[count] = &i->private_rport_attrs[count]; \ 789 if (i->f->show_rport_##field) \ 790 count++ 791 792 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \ 793 i->private_rport_attrs[count] = device_attr_rport_##field; \ 794 i->private_rport_attrs[count].attr.mode = S_IRUGO; \ 795 i->private_rport_attrs[count].store = NULL; \ 796 i->rport_attrs[count] = &i->private_rport_attrs[count]; \ 797 count++ 798 799 #define SETUP_RPORT_ATTRIBUTE_RW(field) \ 800 i->private_rport_attrs[count] = device_attr_rport_##field; \ 801 if (!i->f->set_rport_##field) { \ 802 i->private_rport_attrs[count].attr.mode = S_IRUGO; \ 803 i->private_rport_attrs[count].store = NULL; \ 804 } \ 805 i->rport_attrs[count] = &i->private_rport_attrs[count]; \ 806 if (i->f->show_rport_##field) \ 807 count++ 808 809 #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \ 810 { \ 811 i->private_rport_attrs[count] = device_attr_rport_##field; \ 812 i->rport_attrs[count] = &i->private_rport_attrs[count]; \ 813 count++; \ 814 } 815 816 817 /* The FC Transport Remote Port Attributes: */ 818 819 /* Fixed Remote Port Attributes */ 820 821 fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20); 822 823 static ssize_t 824 show_fc_rport_supported_classes (struct device *dev, 825 struct device_attribute *attr, char *buf) 826 { 827 struct fc_rport *rport = transport_class_to_rport(dev); 828 if (rport->supported_classes == FC_COS_UNSPECIFIED) 829 return snprintf(buf, 20, "unspecified\n"); 830 return get_fc_cos_names(rport->supported_classes, buf); 831 } 832 static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO, 833 show_fc_rport_supported_classes, NULL); 834 835 /* Dynamic Remote Port Attributes */ 836 837 /* 838 * dev_loss_tmo attribute 839 */ 840 static int fc_str_to_dev_loss(const char *buf, unsigned long *val) 841 { 842 char *cp; 843 844 *val = simple_strtoul(buf, &cp, 0); 845 if (*cp && (*cp != '\n')) 846 return -EINVAL; 847 /* 848 * Check for overflow; dev_loss_tmo is u32 849 */ 850 if (*val > UINT_MAX) 851 return -EINVAL; 852 853 return 0; 854 } 855 856 static int fc_rport_set_dev_loss_tmo(struct fc_rport *rport, 857 unsigned long val) 858 { 859 struct Scsi_Host *shost = rport_to_shost(rport); 860 struct fc_internal *i = to_fc_internal(shost->transportt); 861 862 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || 863 (rport->port_state == FC_PORTSTATE_DELETED) || 864 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) 865 return -EBUSY; 866 /* 867 * Check for overflow; dev_loss_tmo is u32 868 */ 869 if (val > UINT_MAX) 870 return -EINVAL; 871 872 /* 873 * If fast_io_fail is off we have to cap 874 * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT 875 */ 876 if (rport->fast_io_fail_tmo == -1 && 877 val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT) 878 return -EINVAL; 879 880 i->f->set_rport_dev_loss_tmo(rport, val); 881 return 0; 882 } 883 884 fc_rport_show_function(dev_loss_tmo, "%d\n", 20, ) 885 static ssize_t 886 store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr, 887 const char *buf, size_t count) 888 { 889 struct fc_rport *rport = transport_class_to_rport(dev); 890 unsigned long val; 891 int rc; 892 893 rc = fc_str_to_dev_loss(buf, &val); 894 if (rc) 895 return rc; 896 897 rc = fc_rport_set_dev_loss_tmo(rport, val); 898 if (rc) 899 return rc; 900 return count; 901 } 902 static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR, 903 show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo); 904 905 906 /* Private Remote Port Attributes */ 907 908 fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long); 909 fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long); 910 fc_private_rport_rd_attr(port_id, "0x%06x\n", 20); 911 912 static ssize_t 913 show_fc_rport_roles (struct device *dev, struct device_attribute *attr, 914 char *buf) 915 { 916 struct fc_rport *rport = transport_class_to_rport(dev); 917 918 /* identify any roles that are port_id specific */ 919 if ((rport->port_id != -1) && 920 (rport->port_id & FC_WELLKNOWN_PORTID_MASK) == 921 FC_WELLKNOWN_PORTID_MASK) { 922 switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) { 923 case FC_FPORT_PORTID: 924 return snprintf(buf, 30, "Fabric Port\n"); 925 case FC_FABCTLR_PORTID: 926 return snprintf(buf, 30, "Fabric Controller\n"); 927 case FC_DIRSRVR_PORTID: 928 return snprintf(buf, 30, "Directory Server\n"); 929 case FC_TIMESRVR_PORTID: 930 return snprintf(buf, 30, "Time Server\n"); 931 case FC_MGMTSRVR_PORTID: 932 return snprintf(buf, 30, "Management Server\n"); 933 default: 934 return snprintf(buf, 30, "Unknown Fabric Entity\n"); 935 } 936 } else { 937 if (rport->roles == FC_PORT_ROLE_UNKNOWN) 938 return snprintf(buf, 20, "unknown\n"); 939 return get_fc_port_roles_names(rport->roles, buf); 940 } 941 } 942 static FC_DEVICE_ATTR(rport, roles, S_IRUGO, 943 show_fc_rport_roles, NULL); 944 945 fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN); 946 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20); 947 948 /* 949 * fast_io_fail_tmo attribute 950 */ 951 static ssize_t 952 show_fc_rport_fast_io_fail_tmo (struct device *dev, 953 struct device_attribute *attr, char *buf) 954 { 955 struct fc_rport *rport = transport_class_to_rport(dev); 956 957 if (rport->fast_io_fail_tmo == -1) 958 return snprintf(buf, 5, "off\n"); 959 return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo); 960 } 961 962 static ssize_t 963 store_fc_rport_fast_io_fail_tmo(struct device *dev, 964 struct device_attribute *attr, const char *buf, 965 size_t count) 966 { 967 int val; 968 char *cp; 969 struct fc_rport *rport = transport_class_to_rport(dev); 970 971 if ((rport->port_state == FC_PORTSTATE_BLOCKED) || 972 (rport->port_state == FC_PORTSTATE_DELETED) || 973 (rport->port_state == FC_PORTSTATE_NOTPRESENT)) 974 return -EBUSY; 975 if (strncmp(buf, "off", 3) == 0) 976 rport->fast_io_fail_tmo = -1; 977 else { 978 val = simple_strtoul(buf, &cp, 0); 979 if ((*cp && (*cp != '\n')) || (val < 0)) 980 return -EINVAL; 981 /* 982 * Cap fast_io_fail by dev_loss_tmo or 983 * SCSI_DEVICE_BLOCK_MAX_TIMEOUT. 984 */ 985 if ((val >= rport->dev_loss_tmo) || 986 (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)) 987 return -EINVAL; 988 989 rport->fast_io_fail_tmo = val; 990 } 991 return count; 992 } 993 static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR, 994 show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo); 995 996 997 /* 998 * FC SCSI Target Attribute Management 999 */ 1000 1001 /* 1002 * Note: in the target show function we recognize when the remote 1003 * port is in the hierarchy and do not allow the driver to get 1004 * involved in sysfs functions. The driver only gets involved if 1005 * it's the "old" style that doesn't use rports. 1006 */ 1007 #define fc_starget_show_function(field, format_string, sz, cast) \ 1008 static ssize_t \ 1009 show_fc_starget_##field (struct device *dev, \ 1010 struct device_attribute *attr, char *buf) \ 1011 { \ 1012 struct scsi_target *starget = transport_class_to_starget(dev); \ 1013 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \ 1014 struct fc_internal *i = to_fc_internal(shost->transportt); \ 1015 struct fc_rport *rport = starget_to_rport(starget); \ 1016 if (rport) \ 1017 fc_starget_##field(starget) = rport->field; \ 1018 else if (i->f->get_starget_##field) \ 1019 i->f->get_starget_##field(starget); \ 1020 return snprintf(buf, sz, format_string, \ 1021 cast fc_starget_##field(starget)); \ 1022 } 1023 1024 #define fc_starget_rd_attr(field, format_string, sz) \ 1025 fc_starget_show_function(field, format_string, sz, ) \ 1026 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \ 1027 show_fc_starget_##field, NULL) 1028 1029 #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \ 1030 fc_starget_show_function(field, format_string, sz, (cast)) \ 1031 static FC_DEVICE_ATTR(starget, field, S_IRUGO, \ 1032 show_fc_starget_##field, NULL) 1033 1034 #define SETUP_STARGET_ATTRIBUTE_RD(field) \ 1035 i->private_starget_attrs[count] = device_attr_starget_##field; \ 1036 i->private_starget_attrs[count].attr.mode = S_IRUGO; \ 1037 i->private_starget_attrs[count].store = NULL; \ 1038 i->starget_attrs[count] = &i->private_starget_attrs[count]; \ 1039 if (i->f->show_starget_##field) \ 1040 count++ 1041 1042 #define SETUP_STARGET_ATTRIBUTE_RW(field) \ 1043 i->private_starget_attrs[count] = device_attr_starget_##field; \ 1044 if (!i->f->set_starget_##field) { \ 1045 i->private_starget_attrs[count].attr.mode = S_IRUGO; \ 1046 i->private_starget_attrs[count].store = NULL; \ 1047 } \ 1048 i->starget_attrs[count] = &i->private_starget_attrs[count]; \ 1049 if (i->f->show_starget_##field) \ 1050 count++ 1051 1052 /* The FC Transport SCSI Target Attributes: */ 1053 fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long); 1054 fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long); 1055 fc_starget_rd_attr(port_id, "0x%06x\n", 20); 1056 1057 1058 /* 1059 * FC Virtual Port Attribute Management 1060 */ 1061 1062 #define fc_vport_show_function(field, format_string, sz, cast) \ 1063 static ssize_t \ 1064 show_fc_vport_##field (struct device *dev, \ 1065 struct device_attribute *attr, char *buf) \ 1066 { \ 1067 struct fc_vport *vport = transport_class_to_vport(dev); \ 1068 struct Scsi_Host *shost = vport_to_shost(vport); \ 1069 struct fc_internal *i = to_fc_internal(shost->transportt); \ 1070 if ((i->f->get_vport_##field) && \ 1071 !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \ 1072 i->f->get_vport_##field(vport); \ 1073 return snprintf(buf, sz, format_string, cast vport->field); \ 1074 } 1075 1076 #define fc_vport_store_function(field) \ 1077 static ssize_t \ 1078 store_fc_vport_##field(struct device *dev, \ 1079 struct device_attribute *attr, \ 1080 const char *buf, size_t count) \ 1081 { \ 1082 int val; \ 1083 struct fc_vport *vport = transport_class_to_vport(dev); \ 1084 struct Scsi_Host *shost = vport_to_shost(vport); \ 1085 struct fc_internal *i = to_fc_internal(shost->transportt); \ 1086 char *cp; \ 1087 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \ 1088 return -EBUSY; \ 1089 val = simple_strtoul(buf, &cp, 0); \ 1090 if (*cp && (*cp != '\n')) \ 1091 return -EINVAL; \ 1092 i->f->set_vport_##field(vport, val); \ 1093 return count; \ 1094 } 1095 1096 #define fc_vport_store_str_function(field, slen) \ 1097 static ssize_t \ 1098 store_fc_vport_##field(struct device *dev, \ 1099 struct device_attribute *attr, \ 1100 const char *buf, size_t count) \ 1101 { \ 1102 struct fc_vport *vport = transport_class_to_vport(dev); \ 1103 struct Scsi_Host *shost = vport_to_shost(vport); \ 1104 struct fc_internal *i = to_fc_internal(shost->transportt); \ 1105 unsigned int cnt=count; \ 1106 \ 1107 /* count may include a LF at end of string */ \ 1108 if (buf[cnt-1] == '\n') \ 1109 cnt--; \ 1110 if (cnt > ((slen) - 1)) \ 1111 return -EINVAL; \ 1112 memcpy(vport->field, buf, cnt); \ 1113 i->f->set_vport_##field(vport); \ 1114 return count; \ 1115 } 1116 1117 #define fc_vport_rd_attr(field, format_string, sz) \ 1118 fc_vport_show_function(field, format_string, sz, ) \ 1119 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \ 1120 show_fc_vport_##field, NULL) 1121 1122 #define fc_vport_rd_attr_cast(field, format_string, sz, cast) \ 1123 fc_vport_show_function(field, format_string, sz, (cast)) \ 1124 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \ 1125 show_fc_vport_##field, NULL) 1126 1127 #define fc_vport_rw_attr(field, format_string, sz) \ 1128 fc_vport_show_function(field, format_string, sz, ) \ 1129 fc_vport_store_function(field) \ 1130 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \ 1131 show_fc_vport_##field, \ 1132 store_fc_vport_##field) 1133 1134 #define fc_private_vport_show_function(field, format_string, sz, cast) \ 1135 static ssize_t \ 1136 show_fc_vport_##field (struct device *dev, \ 1137 struct device_attribute *attr, char *buf) \ 1138 { \ 1139 struct fc_vport *vport = transport_class_to_vport(dev); \ 1140 return snprintf(buf, sz, format_string, cast vport->field); \ 1141 } 1142 1143 #define fc_private_vport_store_u32_function(field) \ 1144 static ssize_t \ 1145 store_fc_vport_##field(struct device *dev, \ 1146 struct device_attribute *attr, \ 1147 const char *buf, size_t count) \ 1148 { \ 1149 u32 val; \ 1150 struct fc_vport *vport = transport_class_to_vport(dev); \ 1151 char *cp; \ 1152 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \ 1153 return -EBUSY; \ 1154 val = simple_strtoul(buf, &cp, 0); \ 1155 if (*cp && (*cp != '\n')) \ 1156 return -EINVAL; \ 1157 vport->field = val; \ 1158 return count; \ 1159 } 1160 1161 1162 #define fc_private_vport_rd_attr(field, format_string, sz) \ 1163 fc_private_vport_show_function(field, format_string, sz, ) \ 1164 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \ 1165 show_fc_vport_##field, NULL) 1166 1167 #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \ 1168 fc_private_vport_show_function(field, format_string, sz, (cast)) \ 1169 static FC_DEVICE_ATTR(vport, field, S_IRUGO, \ 1170 show_fc_vport_##field, NULL) 1171 1172 #define fc_private_vport_rw_u32_attr(field, format_string, sz) \ 1173 fc_private_vport_show_function(field, format_string, sz, ) \ 1174 fc_private_vport_store_u32_function(field) \ 1175 static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \ 1176 show_fc_vport_##field, \ 1177 store_fc_vport_##field) 1178 1179 1180 #define fc_private_vport_rd_enum_attr(title, maxlen) \ 1181 static ssize_t \ 1182 show_fc_vport_##title (struct device *dev, \ 1183 struct device_attribute *attr, \ 1184 char *buf) \ 1185 { \ 1186 struct fc_vport *vport = transport_class_to_vport(dev); \ 1187 const char *name; \ 1188 name = get_fc_##title##_name(vport->title); \ 1189 if (!name) \ 1190 return -EINVAL; \ 1191 return snprintf(buf, maxlen, "%s\n", name); \ 1192 } \ 1193 static FC_DEVICE_ATTR(vport, title, S_IRUGO, \ 1194 show_fc_vport_##title, NULL) 1195 1196 1197 #define SETUP_VPORT_ATTRIBUTE_RD(field) \ 1198 i->private_vport_attrs[count] = device_attr_vport_##field; \ 1199 i->private_vport_attrs[count].attr.mode = S_IRUGO; \ 1200 i->private_vport_attrs[count].store = NULL; \ 1201 i->vport_attrs[count] = &i->private_vport_attrs[count]; \ 1202 if (i->f->get_##field) \ 1203 count++ 1204 /* NOTE: Above MACRO differs: checks function not show bit */ 1205 1206 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \ 1207 i->private_vport_attrs[count] = device_attr_vport_##field; \ 1208 i->private_vport_attrs[count].attr.mode = S_IRUGO; \ 1209 i->private_vport_attrs[count].store = NULL; \ 1210 i->vport_attrs[count] = &i->private_vport_attrs[count]; \ 1211 count++ 1212 1213 #define SETUP_VPORT_ATTRIBUTE_WR(field) \ 1214 i->private_vport_attrs[count] = device_attr_vport_##field; \ 1215 i->vport_attrs[count] = &i->private_vport_attrs[count]; \ 1216 if (i->f->field) \ 1217 count++ 1218 /* NOTE: Above MACRO differs: checks function */ 1219 1220 #define SETUP_VPORT_ATTRIBUTE_RW(field) \ 1221 i->private_vport_attrs[count] = device_attr_vport_##field; \ 1222 if (!i->f->set_vport_##field) { \ 1223 i->private_vport_attrs[count].attr.mode = S_IRUGO; \ 1224 i->private_vport_attrs[count].store = NULL; \ 1225 } \ 1226 i->vport_attrs[count] = &i->private_vport_attrs[count]; \ 1227 count++ 1228 /* NOTE: Above MACRO differs: does not check show bit */ 1229 1230 #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \ 1231 { \ 1232 i->private_vport_attrs[count] = device_attr_vport_##field; \ 1233 i->vport_attrs[count] = &i->private_vport_attrs[count]; \ 1234 count++; \ 1235 } 1236 1237 1238 /* The FC Transport Virtual Port Attributes: */ 1239 1240 /* Fixed Virtual Port Attributes */ 1241 1242 /* Dynamic Virtual Port Attributes */ 1243 1244 /* Private Virtual Port Attributes */ 1245 1246 fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN); 1247 fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN); 1248 fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long); 1249 fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long); 1250 1251 static ssize_t 1252 show_fc_vport_roles (struct device *dev, struct device_attribute *attr, 1253 char *buf) 1254 { 1255 struct fc_vport *vport = transport_class_to_vport(dev); 1256 1257 if (vport->roles == FC_PORT_ROLE_UNKNOWN) 1258 return snprintf(buf, 20, "unknown\n"); 1259 return get_fc_port_roles_names(vport->roles, buf); 1260 } 1261 static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL); 1262 1263 fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN); 1264 1265 fc_private_vport_show_function(symbolic_name, "%s\n", 1266 FC_VPORT_SYMBOLIC_NAMELEN + 1, ) 1267 fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN) 1268 static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR, 1269 show_fc_vport_symbolic_name, store_fc_vport_symbolic_name); 1270 1271 static ssize_t 1272 store_fc_vport_delete(struct device *dev, struct device_attribute *attr, 1273 const char *buf, size_t count) 1274 { 1275 struct fc_vport *vport = transport_class_to_vport(dev); 1276 struct Scsi_Host *shost = vport_to_shost(vport); 1277 unsigned long flags; 1278 1279 spin_lock_irqsave(shost->host_lock, flags); 1280 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING | FC_VPORT_DELETING)) { 1281 spin_unlock_irqrestore(shost->host_lock, flags); 1282 return -EBUSY; 1283 } 1284 vport->flags |= FC_VPORT_DELETING; 1285 spin_unlock_irqrestore(shost->host_lock, flags); 1286 1287 fc_queue_work(shost, &vport->vport_delete_work); 1288 return count; 1289 } 1290 static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR, 1291 NULL, store_fc_vport_delete); 1292 1293 1294 /* 1295 * Enable/Disable vport 1296 * Write "1" to disable, write "0" to enable 1297 */ 1298 static ssize_t 1299 store_fc_vport_disable(struct device *dev, struct device_attribute *attr, 1300 const char *buf, 1301 size_t count) 1302 { 1303 struct fc_vport *vport = transport_class_to_vport(dev); 1304 struct Scsi_Host *shost = vport_to_shost(vport); 1305 struct fc_internal *i = to_fc_internal(shost->transportt); 1306 int stat; 1307 1308 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) 1309 return -EBUSY; 1310 1311 if (*buf == '0') { 1312 if (vport->vport_state != FC_VPORT_DISABLED) 1313 return -EALREADY; 1314 } else if (*buf == '1') { 1315 if (vport->vport_state == FC_VPORT_DISABLED) 1316 return -EALREADY; 1317 } else 1318 return -EINVAL; 1319 1320 stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true)); 1321 return stat ? stat : count; 1322 } 1323 static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR, 1324 NULL, store_fc_vport_disable); 1325 1326 1327 /* 1328 * Host Attribute Management 1329 */ 1330 1331 #define fc_host_show_function(field, format_string, sz, cast) \ 1332 static ssize_t \ 1333 show_fc_host_##field (struct device *dev, \ 1334 struct device_attribute *attr, char *buf) \ 1335 { \ 1336 struct Scsi_Host *shost = transport_class_to_shost(dev); \ 1337 struct fc_internal *i = to_fc_internal(shost->transportt); \ 1338 if (i->f->get_host_##field) \ 1339 i->f->get_host_##field(shost); \ 1340 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \ 1341 } 1342 1343 #define fc_host_store_function(field) \ 1344 static ssize_t \ 1345 store_fc_host_##field(struct device *dev, \ 1346 struct device_attribute *attr, \ 1347 const char *buf, size_t count) \ 1348 { \ 1349 int val; \ 1350 struct Scsi_Host *shost = transport_class_to_shost(dev); \ 1351 struct fc_internal *i = to_fc_internal(shost->transportt); \ 1352 char *cp; \ 1353 \ 1354 val = simple_strtoul(buf, &cp, 0); \ 1355 if (*cp && (*cp != '\n')) \ 1356 return -EINVAL; \ 1357 i->f->set_host_##field(shost, val); \ 1358 return count; \ 1359 } 1360 1361 #define fc_host_store_str_function(field, slen) \ 1362 static ssize_t \ 1363 store_fc_host_##field(struct device *dev, \ 1364 struct device_attribute *attr, \ 1365 const char *buf, size_t count) \ 1366 { \ 1367 struct Scsi_Host *shost = transport_class_to_shost(dev); \ 1368 struct fc_internal *i = to_fc_internal(shost->transportt); \ 1369 unsigned int cnt=count; \ 1370 \ 1371 /* count may include a LF at end of string */ \ 1372 if (buf[cnt-1] == '\n') \ 1373 cnt--; \ 1374 if (cnt > ((slen) - 1)) \ 1375 return -EINVAL; \ 1376 memcpy(fc_host_##field(shost), buf, cnt); \ 1377 i->f->set_host_##field(shost); \ 1378 return count; \ 1379 } 1380 1381 #define fc_host_rd_attr(field, format_string, sz) \ 1382 fc_host_show_function(field, format_string, sz, ) \ 1383 static FC_DEVICE_ATTR(host, field, S_IRUGO, \ 1384 show_fc_host_##field, NULL) 1385 1386 #define fc_host_rd_attr_cast(field, format_string, sz, cast) \ 1387 fc_host_show_function(field, format_string, sz, (cast)) \ 1388 static FC_DEVICE_ATTR(host, field, S_IRUGO, \ 1389 show_fc_host_##field, NULL) 1390 1391 #define fc_host_rw_attr(field, format_string, sz) \ 1392 fc_host_show_function(field, format_string, sz, ) \ 1393 fc_host_store_function(field) \ 1394 static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \ 1395 show_fc_host_##field, \ 1396 store_fc_host_##field) 1397 1398 #define fc_host_rd_enum_attr(title, maxlen) \ 1399 static ssize_t \ 1400 show_fc_host_##title (struct device *dev, \ 1401 struct device_attribute *attr, char *buf) \ 1402 { \ 1403 struct Scsi_Host *shost = transport_class_to_shost(dev); \ 1404 struct fc_internal *i = to_fc_internal(shost->transportt); \ 1405 const char *name; \ 1406 if (i->f->get_host_##title) \ 1407 i->f->get_host_##title(shost); \ 1408 name = get_fc_##title##_name(fc_host_##title(shost)); \ 1409 if (!name) \ 1410 return -EINVAL; \ 1411 return snprintf(buf, maxlen, "%s\n", name); \ 1412 } \ 1413 static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL) 1414 1415 #define SETUP_HOST_ATTRIBUTE_RD(field) \ 1416 i->private_host_attrs[count] = device_attr_host_##field; \ 1417 i->private_host_attrs[count].attr.mode = S_IRUGO; \ 1418 i->private_host_attrs[count].store = NULL; \ 1419 i->host_attrs[count] = &i->private_host_attrs[count]; \ 1420 if (i->f->show_host_##field) \ 1421 count++ 1422 1423 #define SETUP_HOST_ATTRIBUTE_RD_NS(field) \ 1424 i->private_host_attrs[count] = device_attr_host_##field; \ 1425 i->private_host_attrs[count].attr.mode = S_IRUGO; \ 1426 i->private_host_attrs[count].store = NULL; \ 1427 i->host_attrs[count] = &i->private_host_attrs[count]; \ 1428 count++ 1429 1430 #define SETUP_HOST_ATTRIBUTE_RW(field) \ 1431 i->private_host_attrs[count] = device_attr_host_##field; \ 1432 if (!i->f->set_host_##field) { \ 1433 i->private_host_attrs[count].attr.mode = S_IRUGO; \ 1434 i->private_host_attrs[count].store = NULL; \ 1435 } \ 1436 i->host_attrs[count] = &i->private_host_attrs[count]; \ 1437 if (i->f->show_host_##field) \ 1438 count++ 1439 1440 1441 #define fc_private_host_show_function(field, format_string, sz, cast) \ 1442 static ssize_t \ 1443 show_fc_host_##field (struct device *dev, \ 1444 struct device_attribute *attr, char *buf) \ 1445 { \ 1446 struct Scsi_Host *shost = transport_class_to_shost(dev); \ 1447 return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \ 1448 } 1449 1450 #define fc_private_host_rd_attr(field, format_string, sz) \ 1451 fc_private_host_show_function(field, format_string, sz, ) \ 1452 static FC_DEVICE_ATTR(host, field, S_IRUGO, \ 1453 show_fc_host_##field, NULL) 1454 1455 #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \ 1456 fc_private_host_show_function(field, format_string, sz, (cast)) \ 1457 static FC_DEVICE_ATTR(host, field, S_IRUGO, \ 1458 show_fc_host_##field, NULL) 1459 1460 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \ 1461 i->private_host_attrs[count] = device_attr_host_##field; \ 1462 i->private_host_attrs[count].attr.mode = S_IRUGO; \ 1463 i->private_host_attrs[count].store = NULL; \ 1464 i->host_attrs[count] = &i->private_host_attrs[count]; \ 1465 count++ 1466 1467 #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \ 1468 { \ 1469 i->private_host_attrs[count] = device_attr_host_##field; \ 1470 i->host_attrs[count] = &i->private_host_attrs[count]; \ 1471 count++; \ 1472 } 1473 1474 1475 /* Fixed Host Attributes */ 1476 1477 static ssize_t 1478 show_fc_host_supported_classes (struct device *dev, 1479 struct device_attribute *attr, char *buf) 1480 { 1481 struct Scsi_Host *shost = transport_class_to_shost(dev); 1482 1483 if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED) 1484 return snprintf(buf, 20, "unspecified\n"); 1485 1486 return get_fc_cos_names(fc_host_supported_classes(shost), buf); 1487 } 1488 static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO, 1489 show_fc_host_supported_classes, NULL); 1490 1491 static ssize_t 1492 show_fc_host_supported_fc4s (struct device *dev, 1493 struct device_attribute *attr, char *buf) 1494 { 1495 struct Scsi_Host *shost = transport_class_to_shost(dev); 1496 return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost)); 1497 } 1498 static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO, 1499 show_fc_host_supported_fc4s, NULL); 1500 1501 static ssize_t 1502 show_fc_host_supported_speeds (struct device *dev, 1503 struct device_attribute *attr, char *buf) 1504 { 1505 struct Scsi_Host *shost = transport_class_to_shost(dev); 1506 1507 if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN) 1508 return snprintf(buf, 20, "unknown\n"); 1509 1510 return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf); 1511 } 1512 static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO, 1513 show_fc_host_supported_speeds, NULL); 1514 1515 1516 fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long); 1517 fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long); 1518 fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20, 1519 unsigned long long); 1520 fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20); 1521 fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20); 1522 fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1)); 1523 fc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1); 1524 fc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1); 1525 fc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1); 1526 fc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1); 1527 fc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1); 1528 fc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1); 1529 fc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1); 1530 1531 1532 /* Dynamic Host Attributes */ 1533 1534 static ssize_t 1535 show_fc_host_active_fc4s (struct device *dev, 1536 struct device_attribute *attr, char *buf) 1537 { 1538 struct Scsi_Host *shost = transport_class_to_shost(dev); 1539 struct fc_internal *i = to_fc_internal(shost->transportt); 1540 1541 if (i->f->get_host_active_fc4s) 1542 i->f->get_host_active_fc4s(shost); 1543 1544 return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost)); 1545 } 1546 static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO, 1547 show_fc_host_active_fc4s, NULL); 1548 1549 static ssize_t 1550 show_fc_host_speed (struct device *dev, 1551 struct device_attribute *attr, char *buf) 1552 { 1553 struct Scsi_Host *shost = transport_class_to_shost(dev); 1554 struct fc_internal *i = to_fc_internal(shost->transportt); 1555 1556 if (i->f->get_host_speed) 1557 i->f->get_host_speed(shost); 1558 1559 if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN) 1560 return snprintf(buf, 20, "unknown\n"); 1561 1562 return get_fc_port_speed_names(fc_host_speed(shost), buf); 1563 } 1564 static FC_DEVICE_ATTR(host, speed, S_IRUGO, 1565 show_fc_host_speed, NULL); 1566 1567 1568 fc_host_rd_attr(port_id, "0x%06x\n", 20); 1569 fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN); 1570 fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN); 1571 fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long); 1572 fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1); 1573 1574 fc_private_host_show_function(system_hostname, "%s\n", 1575 FC_SYMBOLIC_NAME_SIZE + 1, ) 1576 fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE) 1577 static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR, 1578 show_fc_host_system_hostname, store_fc_host_system_hostname); 1579 1580 1581 /* Private Host Attributes */ 1582 1583 static ssize_t 1584 show_fc_private_host_tgtid_bind_type(struct device *dev, 1585 struct device_attribute *attr, char *buf) 1586 { 1587 struct Scsi_Host *shost = transport_class_to_shost(dev); 1588 const char *name; 1589 1590 name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost)); 1591 if (!name) 1592 return -EINVAL; 1593 return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name); 1594 } 1595 1596 #define get_list_head_entry(pos, head, member) \ 1597 pos = list_entry((head)->next, typeof(*pos), member) 1598 1599 static ssize_t 1600 store_fc_private_host_tgtid_bind_type(struct device *dev, 1601 struct device_attribute *attr, const char *buf, size_t count) 1602 { 1603 struct Scsi_Host *shost = transport_class_to_shost(dev); 1604 struct fc_rport *rport; 1605 enum fc_tgtid_binding_type val; 1606 unsigned long flags; 1607 1608 if (get_fc_tgtid_bind_type_match(buf, &val)) 1609 return -EINVAL; 1610 1611 /* if changing bind type, purge all unused consistent bindings */ 1612 if (val != fc_host_tgtid_bind_type(shost)) { 1613 spin_lock_irqsave(shost->host_lock, flags); 1614 while (!list_empty(&fc_host_rport_bindings(shost))) { 1615 get_list_head_entry(rport, 1616 &fc_host_rport_bindings(shost), peers); 1617 list_del(&rport->peers); 1618 rport->port_state = FC_PORTSTATE_DELETED; 1619 fc_queue_work(shost, &rport->rport_delete_work); 1620 } 1621 spin_unlock_irqrestore(shost->host_lock, flags); 1622 } 1623 1624 fc_host_tgtid_bind_type(shost) = val; 1625 return count; 1626 } 1627 1628 static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR, 1629 show_fc_private_host_tgtid_bind_type, 1630 store_fc_private_host_tgtid_bind_type); 1631 1632 static ssize_t 1633 store_fc_private_host_issue_lip(struct device *dev, 1634 struct device_attribute *attr, const char *buf, size_t count) 1635 { 1636 struct Scsi_Host *shost = transport_class_to_shost(dev); 1637 struct fc_internal *i = to_fc_internal(shost->transportt); 1638 int ret; 1639 1640 /* ignore any data value written to the attribute */ 1641 if (i->f->issue_fc_host_lip) { 1642 ret = i->f->issue_fc_host_lip(shost); 1643 return ret ? ret: count; 1644 } 1645 1646 return -ENOENT; 1647 } 1648 1649 static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL, 1650 store_fc_private_host_issue_lip); 1651 1652 static ssize_t 1653 store_fc_private_host_dev_loss_tmo(struct device *dev, 1654 struct device_attribute *attr, 1655 const char *buf, size_t count) 1656 { 1657 struct Scsi_Host *shost = transport_class_to_shost(dev); 1658 struct fc_host_attrs *fc_host = shost_to_fc_host(shost); 1659 struct fc_rport *rport; 1660 unsigned long val, flags; 1661 int rc; 1662 1663 rc = fc_str_to_dev_loss(buf, &val); 1664 if (rc) 1665 return rc; 1666 1667 fc_host_dev_loss_tmo(shost) = val; 1668 spin_lock_irqsave(shost->host_lock, flags); 1669 list_for_each_entry(rport, &fc_host->rports, peers) 1670 fc_rport_set_dev_loss_tmo(rport, val); 1671 spin_unlock_irqrestore(shost->host_lock, flags); 1672 return count; 1673 } 1674 1675 fc_private_host_show_function(dev_loss_tmo, "%d\n", 20, ); 1676 static FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR, 1677 show_fc_host_dev_loss_tmo, 1678 store_fc_private_host_dev_loss_tmo); 1679 1680 fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20); 1681 1682 /* 1683 * Host Statistics Management 1684 */ 1685 1686 /* Show a given attribute in the statistics group */ 1687 static ssize_t 1688 fc_stat_show(const struct device *dev, char *buf, unsigned long offset) 1689 { 1690 struct Scsi_Host *shost = transport_class_to_shost(dev); 1691 struct fc_internal *i = to_fc_internal(shost->transportt); 1692 struct fc_host_statistics *stats; 1693 ssize_t ret = -ENOENT; 1694 1695 if (offset > sizeof(struct fc_host_statistics) || 1696 offset % sizeof(u64) != 0) 1697 WARN_ON(1); 1698 1699 if (i->f->get_fc_host_stats) { 1700 stats = (i->f->get_fc_host_stats)(shost); 1701 if (stats) 1702 ret = snprintf(buf, 20, "0x%llx\n", 1703 (unsigned long long)*(u64 *)(((u8 *) stats) + offset)); 1704 } 1705 return ret; 1706 } 1707 1708 1709 /* generate a read-only statistics attribute */ 1710 #define fc_host_statistic(name) \ 1711 static ssize_t show_fcstat_##name(struct device *cd, \ 1712 struct device_attribute *attr, \ 1713 char *buf) \ 1714 { \ 1715 return fc_stat_show(cd, buf, \ 1716 offsetof(struct fc_host_statistics, name)); \ 1717 } \ 1718 static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL) 1719 1720 fc_host_statistic(seconds_since_last_reset); 1721 fc_host_statistic(tx_frames); 1722 fc_host_statistic(tx_words); 1723 fc_host_statistic(rx_frames); 1724 fc_host_statistic(rx_words); 1725 fc_host_statistic(lip_count); 1726 fc_host_statistic(nos_count); 1727 fc_host_statistic(error_frames); 1728 fc_host_statistic(dumped_frames); 1729 fc_host_statistic(link_failure_count); 1730 fc_host_statistic(loss_of_sync_count); 1731 fc_host_statistic(loss_of_signal_count); 1732 fc_host_statistic(prim_seq_protocol_err_count); 1733 fc_host_statistic(invalid_tx_word_count); 1734 fc_host_statistic(invalid_crc_count); 1735 fc_host_statistic(fcp_input_requests); 1736 fc_host_statistic(fcp_output_requests); 1737 fc_host_statistic(fcp_control_requests); 1738 fc_host_statistic(fcp_input_megabytes); 1739 fc_host_statistic(fcp_output_megabytes); 1740 fc_host_statistic(fcp_packet_alloc_failures); 1741 fc_host_statistic(fcp_packet_aborts); 1742 fc_host_statistic(fcp_frame_alloc_failures); 1743 fc_host_statistic(fc_no_free_exch); 1744 fc_host_statistic(fc_no_free_exch_xid); 1745 fc_host_statistic(fc_xid_not_found); 1746 fc_host_statistic(fc_xid_busy); 1747 fc_host_statistic(fc_seq_not_found); 1748 fc_host_statistic(fc_non_bls_resp); 1749 1750 static ssize_t 1751 fc_reset_statistics(struct device *dev, struct device_attribute *attr, 1752 const char *buf, size_t count) 1753 { 1754 struct Scsi_Host *shost = transport_class_to_shost(dev); 1755 struct fc_internal *i = to_fc_internal(shost->transportt); 1756 1757 /* ignore any data value written to the attribute */ 1758 if (i->f->reset_fc_host_stats) { 1759 i->f->reset_fc_host_stats(shost); 1760 return count; 1761 } 1762 1763 return -ENOENT; 1764 } 1765 static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL, 1766 fc_reset_statistics); 1767 1768 static struct attribute *fc_statistics_attrs[] = { 1769 &device_attr_host_seconds_since_last_reset.attr, 1770 &device_attr_host_tx_frames.attr, 1771 &device_attr_host_tx_words.attr, 1772 &device_attr_host_rx_frames.attr, 1773 &device_attr_host_rx_words.attr, 1774 &device_attr_host_lip_count.attr, 1775 &device_attr_host_nos_count.attr, 1776 &device_attr_host_error_frames.attr, 1777 &device_attr_host_dumped_frames.attr, 1778 &device_attr_host_link_failure_count.attr, 1779 &device_attr_host_loss_of_sync_count.attr, 1780 &device_attr_host_loss_of_signal_count.attr, 1781 &device_attr_host_prim_seq_protocol_err_count.attr, 1782 &device_attr_host_invalid_tx_word_count.attr, 1783 &device_attr_host_invalid_crc_count.attr, 1784 &device_attr_host_fcp_input_requests.attr, 1785 &device_attr_host_fcp_output_requests.attr, 1786 &device_attr_host_fcp_control_requests.attr, 1787 &device_attr_host_fcp_input_megabytes.attr, 1788 &device_attr_host_fcp_output_megabytes.attr, 1789 &device_attr_host_fcp_packet_alloc_failures.attr, 1790 &device_attr_host_fcp_packet_aborts.attr, 1791 &device_attr_host_fcp_frame_alloc_failures.attr, 1792 &device_attr_host_fc_no_free_exch.attr, 1793 &device_attr_host_fc_no_free_exch_xid.attr, 1794 &device_attr_host_fc_xid_not_found.attr, 1795 &device_attr_host_fc_xid_busy.attr, 1796 &device_attr_host_fc_seq_not_found.attr, 1797 &device_attr_host_fc_non_bls_resp.attr, 1798 &device_attr_host_reset_statistics.attr, 1799 NULL 1800 }; 1801 1802 static struct attribute_group fc_statistics_group = { 1803 .name = "statistics", 1804 .attrs = fc_statistics_attrs, 1805 }; 1806 1807 1808 /* Host Vport Attributes */ 1809 1810 static int 1811 fc_parse_wwn(const char *ns, u64 *nm) 1812 { 1813 unsigned int i, j; 1814 u8 wwn[8]; 1815 1816 memset(wwn, 0, sizeof(wwn)); 1817 1818 /* Validate and store the new name */ 1819 for (i=0, j=0; i < 16; i++) { 1820 int value; 1821 1822 value = hex_to_bin(*ns++); 1823 if (value >= 0) 1824 j = (j << 4) | value; 1825 else 1826 return -EINVAL; 1827 if (i % 2) { 1828 wwn[i/2] = j & 0xff; 1829 j = 0; 1830 } 1831 } 1832 1833 *nm = wwn_to_u64(wwn); 1834 1835 return 0; 1836 } 1837 1838 1839 /* 1840 * "Short-cut" sysfs variable to create a new vport on a FC Host. 1841 * Input is a string of the form "<WWPN>:<WWNN>". Other attributes 1842 * will default to a NPIV-based FCP_Initiator; The WWNs are specified 1843 * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc) 1844 */ 1845 static ssize_t 1846 store_fc_host_vport_create(struct device *dev, struct device_attribute *attr, 1847 const char *buf, size_t count) 1848 { 1849 struct Scsi_Host *shost = transport_class_to_shost(dev); 1850 struct fc_vport_identifiers vid; 1851 struct fc_vport *vport; 1852 unsigned int cnt=count; 1853 int stat; 1854 1855 memset(&vid, 0, sizeof(vid)); 1856 1857 /* count may include a LF at end of string */ 1858 if (buf[cnt-1] == '\n') 1859 cnt--; 1860 1861 /* validate we have enough characters for WWPN */ 1862 if ((cnt != (16+1+16)) || (buf[16] != ':')) 1863 return -EINVAL; 1864 1865 stat = fc_parse_wwn(&buf[0], &vid.port_name); 1866 if (stat) 1867 return stat; 1868 1869 stat = fc_parse_wwn(&buf[17], &vid.node_name); 1870 if (stat) 1871 return stat; 1872 1873 vid.roles = FC_PORT_ROLE_FCP_INITIATOR; 1874 vid.vport_type = FC_PORTTYPE_NPIV; 1875 /* vid.symbolic_name is already zero/NULL's */ 1876 vid.disable = false; /* always enabled */ 1877 1878 /* we only allow support on Channel 0 !!! */ 1879 stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport); 1880 return stat ? stat : count; 1881 } 1882 static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL, 1883 store_fc_host_vport_create); 1884 1885 1886 /* 1887 * "Short-cut" sysfs variable to delete a vport on a FC Host. 1888 * Vport is identified by a string containing "<WWPN>:<WWNN>". 1889 * The WWNs are specified as hex characters, and may *not* contain 1890 * any prefixes (e.g. 0x, x, etc) 1891 */ 1892 static ssize_t 1893 store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr, 1894 const char *buf, size_t count) 1895 { 1896 struct Scsi_Host *shost = transport_class_to_shost(dev); 1897 struct fc_host_attrs *fc_host = shost_to_fc_host(shost); 1898 struct fc_vport *vport; 1899 u64 wwpn, wwnn; 1900 unsigned long flags; 1901 unsigned int cnt=count; 1902 int stat, match; 1903 1904 /* count may include a LF at end of string */ 1905 if (buf[cnt-1] == '\n') 1906 cnt--; 1907 1908 /* validate we have enough characters for WWPN */ 1909 if ((cnt != (16+1+16)) || (buf[16] != ':')) 1910 return -EINVAL; 1911 1912 stat = fc_parse_wwn(&buf[0], &wwpn); 1913 if (stat) 1914 return stat; 1915 1916 stat = fc_parse_wwn(&buf[17], &wwnn); 1917 if (stat) 1918 return stat; 1919 1920 spin_lock_irqsave(shost->host_lock, flags); 1921 match = 0; 1922 /* we only allow support on Channel 0 !!! */ 1923 list_for_each_entry(vport, &fc_host->vports, peers) { 1924 if ((vport->channel == 0) && 1925 (vport->port_name == wwpn) && (vport->node_name == wwnn)) { 1926 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) 1927 break; 1928 vport->flags |= FC_VPORT_DELETING; 1929 match = 1; 1930 break; 1931 } 1932 } 1933 spin_unlock_irqrestore(shost->host_lock, flags); 1934 1935 if (!match) 1936 return -ENODEV; 1937 1938 stat = fc_vport_terminate(vport); 1939 return stat ? stat : count; 1940 } 1941 static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL, 1942 store_fc_host_vport_delete); 1943 1944 1945 static int fc_host_match(struct attribute_container *cont, 1946 struct device *dev) 1947 { 1948 struct Scsi_Host *shost; 1949 struct fc_internal *i; 1950 1951 if (!scsi_is_host_device(dev)) 1952 return 0; 1953 1954 shost = dev_to_shost(dev); 1955 if (!shost->transportt || shost->transportt->host_attrs.ac.class 1956 != &fc_host_class.class) 1957 return 0; 1958 1959 i = to_fc_internal(shost->transportt); 1960 1961 return &i->t.host_attrs.ac == cont; 1962 } 1963 1964 static int fc_target_match(struct attribute_container *cont, 1965 struct device *dev) 1966 { 1967 struct Scsi_Host *shost; 1968 struct fc_internal *i; 1969 1970 if (!scsi_is_target_device(dev)) 1971 return 0; 1972 1973 shost = dev_to_shost(dev->parent); 1974 if (!shost->transportt || shost->transportt->host_attrs.ac.class 1975 != &fc_host_class.class) 1976 return 0; 1977 1978 i = to_fc_internal(shost->transportt); 1979 1980 return &i->t.target_attrs.ac == cont; 1981 } 1982 1983 static void fc_rport_dev_release(struct device *dev) 1984 { 1985 struct fc_rport *rport = dev_to_rport(dev); 1986 put_device(dev->parent); 1987 kfree(rport); 1988 } 1989 1990 int scsi_is_fc_rport(const struct device *dev) 1991 { 1992 return dev->release == fc_rport_dev_release; 1993 } 1994 EXPORT_SYMBOL(scsi_is_fc_rport); 1995 1996 static int fc_rport_match(struct attribute_container *cont, 1997 struct device *dev) 1998 { 1999 struct Scsi_Host *shost; 2000 struct fc_internal *i; 2001 2002 if (!scsi_is_fc_rport(dev)) 2003 return 0; 2004 2005 shost = dev_to_shost(dev->parent); 2006 if (!shost->transportt || shost->transportt->host_attrs.ac.class 2007 != &fc_host_class.class) 2008 return 0; 2009 2010 i = to_fc_internal(shost->transportt); 2011 2012 return &i->rport_attr_cont.ac == cont; 2013 } 2014 2015 2016 static void fc_vport_dev_release(struct device *dev) 2017 { 2018 struct fc_vport *vport = dev_to_vport(dev); 2019 put_device(dev->parent); /* release kobj parent */ 2020 kfree(vport); 2021 } 2022 2023 static int scsi_is_fc_vport(const struct device *dev) 2024 { 2025 return dev->release == fc_vport_dev_release; 2026 } 2027 2028 static int fc_vport_match(struct attribute_container *cont, 2029 struct device *dev) 2030 { 2031 struct fc_vport *vport; 2032 struct Scsi_Host *shost; 2033 struct fc_internal *i; 2034 2035 if (!scsi_is_fc_vport(dev)) 2036 return 0; 2037 vport = dev_to_vport(dev); 2038 2039 shost = vport_to_shost(vport); 2040 if (!shost->transportt || shost->transportt->host_attrs.ac.class 2041 != &fc_host_class.class) 2042 return 0; 2043 2044 i = to_fc_internal(shost->transportt); 2045 return &i->vport_attr_cont.ac == cont; 2046 } 2047 2048 2049 /** 2050 * fc_eh_timed_out - FC Transport I/O timeout intercept handler 2051 * @scmd: The SCSI command which timed out 2052 * 2053 * This routine protects against error handlers getting invoked while a 2054 * rport is in a blocked state, typically due to a temporarily loss of 2055 * connectivity. If the error handlers are allowed to proceed, requests 2056 * to abort i/o, reset the target, etc will likely fail as there is no way 2057 * to communicate with the device to perform the requested function. These 2058 * failures may result in the midlayer taking the device offline, requiring 2059 * manual intervention to restore operation. 2060 * 2061 * This routine, called whenever an i/o times out, validates the state of 2062 * the underlying rport. If the rport is blocked, it returns 2063 * EH_RESET_TIMER, which will continue to reschedule the timeout. 2064 * Eventually, either the device will return, or devloss_tmo will fire, 2065 * and when the timeout then fires, it will be handled normally. 2066 * If the rport is not blocked, normal error handling continues. 2067 * 2068 * Notes: 2069 * This routine assumes no locks are held on entry. 2070 */ 2071 enum blk_eh_timer_return 2072 fc_eh_timed_out(struct scsi_cmnd *scmd) 2073 { 2074 struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device)); 2075 2076 if (rport->port_state == FC_PORTSTATE_BLOCKED) 2077 return BLK_EH_RESET_TIMER; 2078 2079 return BLK_EH_DONE; 2080 } 2081 EXPORT_SYMBOL(fc_eh_timed_out); 2082 2083 /* 2084 * Called by fc_user_scan to locate an rport on the shost that 2085 * matches the channel and target id, and invoke scsi_scan_target() 2086 * on the rport. 2087 */ 2088 static void 2089 fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, u64 lun) 2090 { 2091 struct fc_rport *rport; 2092 unsigned long flags; 2093 2094 spin_lock_irqsave(shost->host_lock, flags); 2095 2096 list_for_each_entry(rport, &fc_host_rports(shost), peers) { 2097 if (rport->scsi_target_id == -1) 2098 continue; 2099 2100 if (rport->port_state != FC_PORTSTATE_ONLINE) 2101 continue; 2102 2103 if ((channel == rport->channel) && 2104 (id == rport->scsi_target_id)) { 2105 spin_unlock_irqrestore(shost->host_lock, flags); 2106 scsi_scan_target(&rport->dev, channel, id, lun, 2107 SCSI_SCAN_MANUAL); 2108 return; 2109 } 2110 } 2111 2112 spin_unlock_irqrestore(shost->host_lock, flags); 2113 } 2114 2115 /* 2116 * Called via sysfs scan routines. Necessary, as the FC transport 2117 * wants to place all target objects below the rport object. So this 2118 * routine must invoke the scsi_scan_target() routine with the rport 2119 * object as the parent. 2120 */ 2121 static int 2122 fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, u64 lun) 2123 { 2124 uint chlo, chhi; 2125 uint tgtlo, tgthi; 2126 2127 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) || 2128 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) || 2129 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun))) 2130 return -EINVAL; 2131 2132 if (channel == SCAN_WILD_CARD) { 2133 chlo = 0; 2134 chhi = shost->max_channel + 1; 2135 } else { 2136 chlo = channel; 2137 chhi = channel + 1; 2138 } 2139 2140 if (id == SCAN_WILD_CARD) { 2141 tgtlo = 0; 2142 tgthi = shost->max_id; 2143 } else { 2144 tgtlo = id; 2145 tgthi = id + 1; 2146 } 2147 2148 for ( ; chlo < chhi; chlo++) 2149 for ( ; tgtlo < tgthi; tgtlo++) 2150 fc_user_scan_tgt(shost, chlo, tgtlo, lun); 2151 2152 return 0; 2153 } 2154 2155 struct scsi_transport_template * 2156 fc_attach_transport(struct fc_function_template *ft) 2157 { 2158 int count; 2159 struct fc_internal *i = kzalloc(sizeof(struct fc_internal), 2160 GFP_KERNEL); 2161 2162 if (unlikely(!i)) 2163 return NULL; 2164 2165 i->t.target_attrs.ac.attrs = &i->starget_attrs[0]; 2166 i->t.target_attrs.ac.class = &fc_transport_class.class; 2167 i->t.target_attrs.ac.match = fc_target_match; 2168 i->t.target_size = sizeof(struct fc_starget_attrs); 2169 transport_container_register(&i->t.target_attrs); 2170 2171 i->t.host_attrs.ac.attrs = &i->host_attrs[0]; 2172 i->t.host_attrs.ac.class = &fc_host_class.class; 2173 i->t.host_attrs.ac.match = fc_host_match; 2174 i->t.host_size = sizeof(struct fc_host_attrs); 2175 if (ft->get_fc_host_stats) 2176 i->t.host_attrs.statistics = &fc_statistics_group; 2177 transport_container_register(&i->t.host_attrs); 2178 2179 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0]; 2180 i->rport_attr_cont.ac.class = &fc_rport_class.class; 2181 i->rport_attr_cont.ac.match = fc_rport_match; 2182 transport_container_register(&i->rport_attr_cont); 2183 2184 i->vport_attr_cont.ac.attrs = &i->vport_attrs[0]; 2185 i->vport_attr_cont.ac.class = &fc_vport_class.class; 2186 i->vport_attr_cont.ac.match = fc_vport_match; 2187 transport_container_register(&i->vport_attr_cont); 2188 2189 i->f = ft; 2190 2191 /* Transport uses the shost workq for scsi scanning */ 2192 i->t.create_work_queue = 1; 2193 2194 i->t.user_scan = fc_user_scan; 2195 2196 /* 2197 * Setup SCSI Target Attributes. 2198 */ 2199 count = 0; 2200 SETUP_STARGET_ATTRIBUTE_RD(node_name); 2201 SETUP_STARGET_ATTRIBUTE_RD(port_name); 2202 SETUP_STARGET_ATTRIBUTE_RD(port_id); 2203 2204 BUG_ON(count > FC_STARGET_NUM_ATTRS); 2205 2206 i->starget_attrs[count] = NULL; 2207 2208 2209 /* 2210 * Setup SCSI Host Attributes. 2211 */ 2212 count=0; 2213 SETUP_HOST_ATTRIBUTE_RD(node_name); 2214 SETUP_HOST_ATTRIBUTE_RD(port_name); 2215 SETUP_HOST_ATTRIBUTE_RD(permanent_port_name); 2216 SETUP_HOST_ATTRIBUTE_RD(supported_classes); 2217 SETUP_HOST_ATTRIBUTE_RD(supported_fc4s); 2218 SETUP_HOST_ATTRIBUTE_RD(supported_speeds); 2219 SETUP_HOST_ATTRIBUTE_RD(maxframe_size); 2220 if (ft->vport_create) { 2221 SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports); 2222 SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse); 2223 } 2224 SETUP_HOST_ATTRIBUTE_RD(serial_number); 2225 SETUP_HOST_ATTRIBUTE_RD(manufacturer); 2226 SETUP_HOST_ATTRIBUTE_RD(model); 2227 SETUP_HOST_ATTRIBUTE_RD(model_description); 2228 SETUP_HOST_ATTRIBUTE_RD(hardware_version); 2229 SETUP_HOST_ATTRIBUTE_RD(driver_version); 2230 SETUP_HOST_ATTRIBUTE_RD(firmware_version); 2231 SETUP_HOST_ATTRIBUTE_RD(optionrom_version); 2232 2233 SETUP_HOST_ATTRIBUTE_RD(port_id); 2234 SETUP_HOST_ATTRIBUTE_RD(port_type); 2235 SETUP_HOST_ATTRIBUTE_RD(port_state); 2236 SETUP_HOST_ATTRIBUTE_RD(active_fc4s); 2237 SETUP_HOST_ATTRIBUTE_RD(speed); 2238 SETUP_HOST_ATTRIBUTE_RD(fabric_name); 2239 SETUP_HOST_ATTRIBUTE_RD(symbolic_name); 2240 SETUP_HOST_ATTRIBUTE_RW(system_hostname); 2241 2242 /* Transport-managed attributes */ 2243 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(dev_loss_tmo); 2244 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type); 2245 if (ft->issue_fc_host_lip) 2246 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip); 2247 if (ft->vport_create) 2248 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create); 2249 if (ft->vport_delete) 2250 SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete); 2251 2252 BUG_ON(count > FC_HOST_NUM_ATTRS); 2253 2254 i->host_attrs[count] = NULL; 2255 2256 /* 2257 * Setup Remote Port Attributes. 2258 */ 2259 count=0; 2260 SETUP_RPORT_ATTRIBUTE_RD(maxframe_size); 2261 SETUP_RPORT_ATTRIBUTE_RD(supported_classes); 2262 SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo); 2263 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name); 2264 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name); 2265 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id); 2266 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles); 2267 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state); 2268 SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id); 2269 SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo); 2270 2271 BUG_ON(count > FC_RPORT_NUM_ATTRS); 2272 2273 i->rport_attrs[count] = NULL; 2274 2275 /* 2276 * Setup Virtual Port Attributes. 2277 */ 2278 count=0; 2279 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state); 2280 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state); 2281 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name); 2282 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name); 2283 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles); 2284 SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type); 2285 SETUP_VPORT_ATTRIBUTE_RW(symbolic_name); 2286 SETUP_VPORT_ATTRIBUTE_WR(vport_delete); 2287 SETUP_VPORT_ATTRIBUTE_WR(vport_disable); 2288 2289 BUG_ON(count > FC_VPORT_NUM_ATTRS); 2290 2291 i->vport_attrs[count] = NULL; 2292 2293 return &i->t; 2294 } 2295 EXPORT_SYMBOL(fc_attach_transport); 2296 2297 void fc_release_transport(struct scsi_transport_template *t) 2298 { 2299 struct fc_internal *i = to_fc_internal(t); 2300 2301 transport_container_unregister(&i->t.target_attrs); 2302 transport_container_unregister(&i->t.host_attrs); 2303 transport_container_unregister(&i->rport_attr_cont); 2304 transport_container_unregister(&i->vport_attr_cont); 2305 2306 kfree(i); 2307 } 2308 EXPORT_SYMBOL(fc_release_transport); 2309 2310 /** 2311 * fc_queue_work - Queue work to the fc_host workqueue. 2312 * @shost: Pointer to Scsi_Host bound to fc_host. 2313 * @work: Work to queue for execution. 2314 * 2315 * Return value: 2316 * 1 - work queued for execution 2317 * 0 - work is already queued 2318 * -EINVAL - work queue doesn't exist 2319 */ 2320 static int 2321 fc_queue_work(struct Scsi_Host *shost, struct work_struct *work) 2322 { 2323 if (unlikely(!fc_host_work_q(shost))) { 2324 printk(KERN_ERR 2325 "ERROR: FC host '%s' attempted to queue work, " 2326 "when no workqueue created.\n", shost->hostt->name); 2327 dump_stack(); 2328 2329 return -EINVAL; 2330 } 2331 2332 return queue_work(fc_host_work_q(shost), work); 2333 } 2334 2335 /** 2336 * fc_flush_work - Flush a fc_host's workqueue. 2337 * @shost: Pointer to Scsi_Host bound to fc_host. 2338 */ 2339 static void 2340 fc_flush_work(struct Scsi_Host *shost) 2341 { 2342 if (!fc_host_work_q(shost)) { 2343 printk(KERN_ERR 2344 "ERROR: FC host '%s' attempted to flush work, " 2345 "when no workqueue created.\n", shost->hostt->name); 2346 dump_stack(); 2347 return; 2348 } 2349 2350 flush_workqueue(fc_host_work_q(shost)); 2351 } 2352 2353 /** 2354 * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue. 2355 * @shost: Pointer to Scsi_Host bound to fc_host. 2356 * @work: Work to queue for execution. 2357 * @delay: jiffies to delay the work queuing 2358 * 2359 * Return value: 2360 * 1 on success / 0 already queued / < 0 for error 2361 */ 2362 static int 2363 fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work, 2364 unsigned long delay) 2365 { 2366 if (unlikely(!fc_host_devloss_work_q(shost))) { 2367 printk(KERN_ERR 2368 "ERROR: FC host '%s' attempted to queue work, " 2369 "when no workqueue created.\n", shost->hostt->name); 2370 dump_stack(); 2371 2372 return -EINVAL; 2373 } 2374 2375 return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay); 2376 } 2377 2378 /** 2379 * fc_flush_devloss - Flush a fc_host's devloss workqueue. 2380 * @shost: Pointer to Scsi_Host bound to fc_host. 2381 */ 2382 static void 2383 fc_flush_devloss(struct Scsi_Host *shost) 2384 { 2385 if (!fc_host_devloss_work_q(shost)) { 2386 printk(KERN_ERR 2387 "ERROR: FC host '%s' attempted to flush work, " 2388 "when no workqueue created.\n", shost->hostt->name); 2389 dump_stack(); 2390 return; 2391 } 2392 2393 flush_workqueue(fc_host_devloss_work_q(shost)); 2394 } 2395 2396 2397 /** 2398 * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host. 2399 * @shost: Which &Scsi_Host 2400 * 2401 * This routine is expected to be called immediately preceding the 2402 * a driver's call to scsi_remove_host(). 2403 * 2404 * WARNING: A driver utilizing the fc_transport, which fails to call 2405 * this routine prior to scsi_remove_host(), will leave dangling 2406 * objects in /sys/class/fc_remote_ports. Access to any of these 2407 * objects can result in a system crash !!! 2408 * 2409 * Notes: 2410 * This routine assumes no locks are held on entry. 2411 */ 2412 void 2413 fc_remove_host(struct Scsi_Host *shost) 2414 { 2415 struct fc_vport *vport = NULL, *next_vport = NULL; 2416 struct fc_rport *rport = NULL, *next_rport = NULL; 2417 struct workqueue_struct *work_q; 2418 struct fc_host_attrs *fc_host = shost_to_fc_host(shost); 2419 unsigned long flags; 2420 2421 spin_lock_irqsave(shost->host_lock, flags); 2422 2423 /* Remove any vports */ 2424 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) { 2425 vport->flags |= FC_VPORT_DELETING; 2426 fc_queue_work(shost, &vport->vport_delete_work); 2427 } 2428 2429 /* Remove any remote ports */ 2430 list_for_each_entry_safe(rport, next_rport, 2431 &fc_host->rports, peers) { 2432 list_del(&rport->peers); 2433 rport->port_state = FC_PORTSTATE_DELETED; 2434 fc_queue_work(shost, &rport->rport_delete_work); 2435 } 2436 2437 list_for_each_entry_safe(rport, next_rport, 2438 &fc_host->rport_bindings, peers) { 2439 list_del(&rport->peers); 2440 rport->port_state = FC_PORTSTATE_DELETED; 2441 fc_queue_work(shost, &rport->rport_delete_work); 2442 } 2443 2444 spin_unlock_irqrestore(shost->host_lock, flags); 2445 2446 /* flush all scan work items */ 2447 scsi_flush_work(shost); 2448 2449 /* flush all stgt delete, and rport delete work items, then kill it */ 2450 if (fc_host->work_q) { 2451 work_q = fc_host->work_q; 2452 fc_host->work_q = NULL; 2453 destroy_workqueue(work_q); 2454 } 2455 2456 /* flush all devloss work items, then kill it */ 2457 if (fc_host->devloss_work_q) { 2458 work_q = fc_host->devloss_work_q; 2459 fc_host->devloss_work_q = NULL; 2460 destroy_workqueue(work_q); 2461 } 2462 } 2463 EXPORT_SYMBOL(fc_remove_host); 2464 2465 static void fc_terminate_rport_io(struct fc_rport *rport) 2466 { 2467 struct Scsi_Host *shost = rport_to_shost(rport); 2468 struct fc_internal *i = to_fc_internal(shost->transportt); 2469 2470 /* Involve the LLDD if possible to terminate all io on the rport. */ 2471 if (i->f->terminate_rport_io) 2472 i->f->terminate_rport_io(rport); 2473 2474 /* 2475 * Must unblock to flush queued IO. scsi-ml will fail incoming reqs. 2476 */ 2477 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE); 2478 } 2479 2480 /** 2481 * fc_starget_delete - called to delete the scsi descendants of an rport 2482 * @work: remote port to be operated on. 2483 * 2484 * Deletes target and all sdevs. 2485 */ 2486 static void 2487 fc_starget_delete(struct work_struct *work) 2488 { 2489 struct fc_rport *rport = 2490 container_of(work, struct fc_rport, stgt_delete_work); 2491 2492 fc_terminate_rport_io(rport); 2493 scsi_remove_target(&rport->dev); 2494 } 2495 2496 2497 /** 2498 * fc_rport_final_delete - finish rport termination and delete it. 2499 * @work: remote port to be deleted. 2500 */ 2501 static void 2502 fc_rport_final_delete(struct work_struct *work) 2503 { 2504 struct fc_rport *rport = 2505 container_of(work, struct fc_rport, rport_delete_work); 2506 struct device *dev = &rport->dev; 2507 struct Scsi_Host *shost = rport_to_shost(rport); 2508 struct fc_internal *i = to_fc_internal(shost->transportt); 2509 unsigned long flags; 2510 int do_callback = 0; 2511 2512 fc_terminate_rport_io(rport); 2513 2514 /* 2515 * if a scan is pending, flush the SCSI Host work_q so that 2516 * that we can reclaim the rport scan work element. 2517 */ 2518 if (rport->flags & FC_RPORT_SCAN_PENDING) 2519 scsi_flush_work(shost); 2520 2521 /* 2522 * Cancel any outstanding timers. These should really exist 2523 * only when rmmod'ing the LLDD and we're asking for 2524 * immediate termination of the rports 2525 */ 2526 spin_lock_irqsave(shost->host_lock, flags); 2527 if (rport->flags & FC_RPORT_DEVLOSS_PENDING) { 2528 spin_unlock_irqrestore(shost->host_lock, flags); 2529 if (!cancel_delayed_work(&rport->fail_io_work)) 2530 fc_flush_devloss(shost); 2531 if (!cancel_delayed_work(&rport->dev_loss_work)) 2532 fc_flush_devloss(shost); 2533 cancel_work_sync(&rport->scan_work); 2534 spin_lock_irqsave(shost->host_lock, flags); 2535 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING; 2536 } 2537 spin_unlock_irqrestore(shost->host_lock, flags); 2538 2539 /* Delete SCSI target and sdevs */ 2540 if (rport->scsi_target_id != -1) 2541 fc_starget_delete(&rport->stgt_delete_work); 2542 2543 /* 2544 * Notify the driver that the rport is now dead. The LLDD will 2545 * also guarantee that any communication to the rport is terminated 2546 * 2547 * Avoid this call if we already called it when we preserved the 2548 * rport for the binding. 2549 */ 2550 spin_lock_irqsave(shost->host_lock, flags); 2551 if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) && 2552 (i->f->dev_loss_tmo_callbk)) { 2553 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE; 2554 do_callback = 1; 2555 } 2556 spin_unlock_irqrestore(shost->host_lock, flags); 2557 2558 if (do_callback) 2559 i->f->dev_loss_tmo_callbk(rport); 2560 2561 fc_bsg_remove(rport->rqst_q); 2562 2563 transport_remove_device(dev); 2564 device_del(dev); 2565 transport_destroy_device(dev); 2566 scsi_host_put(shost); /* for fc_host->rport list */ 2567 put_device(dev); /* for self-reference */ 2568 } 2569 2570 2571 /** 2572 * fc_remote_port_create - allocates and creates 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. 2577 * 2578 * Allocates and creates the remoter port structure, including the 2579 * class and sysfs creation. 2580 * 2581 * Notes: 2582 * This routine assumes no locks are held on entry. 2583 */ 2584 static struct fc_rport * 2585 fc_remote_port_create(struct Scsi_Host *shost, int channel, 2586 struct fc_rport_identifiers *ids) 2587 { 2588 struct fc_host_attrs *fc_host = shost_to_fc_host(shost); 2589 struct fc_internal *fci = to_fc_internal(shost->transportt); 2590 struct fc_rport *rport; 2591 struct device *dev; 2592 unsigned long flags; 2593 int error; 2594 size_t size; 2595 2596 size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size); 2597 rport = kzalloc(size, GFP_KERNEL); 2598 if (unlikely(!rport)) { 2599 printk(KERN_ERR "%s: allocation failure\n", __func__); 2600 return NULL; 2601 } 2602 2603 rport->maxframe_size = -1; 2604 rport->supported_classes = FC_COS_UNSPECIFIED; 2605 rport->dev_loss_tmo = fc_host->dev_loss_tmo; 2606 memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name)); 2607 memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name)); 2608 rport->port_id = ids->port_id; 2609 rport->roles = ids->roles; 2610 rport->port_state = FC_PORTSTATE_ONLINE; 2611 if (fci->f->dd_fcrport_size) 2612 rport->dd_data = &rport[1]; 2613 rport->channel = channel; 2614 rport->fast_io_fail_tmo = -1; 2615 2616 INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport); 2617 INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io); 2618 INIT_WORK(&rport->scan_work, fc_scsi_scan_rport); 2619 INIT_WORK(&rport->stgt_delete_work, fc_starget_delete); 2620 INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete); 2621 2622 spin_lock_irqsave(shost->host_lock, flags); 2623 2624 rport->number = fc_host->next_rport_number++; 2625 if ((rport->roles & FC_PORT_ROLE_FCP_TARGET) || 2626 (rport->roles & FC_PORT_ROLE_FCP_DUMMY_INITIATOR)) 2627 rport->scsi_target_id = fc_host->next_target_id++; 2628 else 2629 rport->scsi_target_id = -1; 2630 list_add_tail(&rport->peers, &fc_host->rports); 2631 scsi_host_get(shost); /* for fc_host->rport list */ 2632 2633 spin_unlock_irqrestore(shost->host_lock, flags); 2634 2635 dev = &rport->dev; 2636 device_initialize(dev); /* takes self reference */ 2637 dev->parent = get_device(&shost->shost_gendev); /* parent reference */ 2638 dev->release = fc_rport_dev_release; 2639 dev_set_name(dev, "rport-%d:%d-%d", 2640 shost->host_no, channel, rport->number); 2641 transport_setup_device(dev); 2642 2643 error = device_add(dev); 2644 if (error) { 2645 printk(KERN_ERR "FC Remote Port device_add failed\n"); 2646 goto delete_rport; 2647 } 2648 transport_add_device(dev); 2649 transport_configure_device(dev); 2650 2651 fc_bsg_rportadd(shost, rport); 2652 /* ignore any bsg add error - we just can't do sgio */ 2653 2654 if (rport->roles & FC_PORT_ROLE_FCP_TARGET) { 2655 /* initiate a scan of the target */ 2656 rport->flags |= FC_RPORT_SCAN_PENDING; 2657 scsi_queue_work(shost, &rport->scan_work); 2658 } 2659 2660 return rport; 2661 2662 delete_rport: 2663 transport_destroy_device(dev); 2664 spin_lock_irqsave(shost->host_lock, flags); 2665 list_del(&rport->peers); 2666 scsi_host_put(shost); /* for fc_host->rport list */ 2667 spin_unlock_irqrestore(shost->host_lock, flags); 2668 put_device(dev->parent); 2669 kfree(rport); 2670 return NULL; 2671 } 2672 2673 /** 2674 * fc_remote_port_add - notify fc transport of the existence of a remote FC port. 2675 * @shost: scsi host the remote port is connected to. 2676 * @channel: Channel on shost port connected to. 2677 * @ids: The world wide names, fc address, and FC4 port 2678 * roles for the remote port. 2679 * 2680 * The LLDD calls this routine to notify the transport of the existence 2681 * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn) 2682 * of the port, it's FC address (port_id), and the FC4 roles that are 2683 * active for the port. 2684 * 2685 * For ports that are FCP targets (aka scsi targets), the FC transport 2686 * maintains consistent target id bindings on behalf of the LLDD. 2687 * A consistent target id binding is an assignment of a target id to 2688 * a remote port identifier, which persists while the scsi host is 2689 * attached. The remote port can disappear, then later reappear, and 2690 * it's target id assignment remains the same. This allows for shifts 2691 * in FC addressing (if binding by wwpn or wwnn) with no apparent 2692 * changes to the scsi subsystem which is based on scsi host number and 2693 * target id values. Bindings are only valid during the attachment of 2694 * the scsi host. If the host detaches, then later re-attaches, target 2695 * id bindings may change. 2696 * 2697 * This routine is responsible for returning a remote port structure. 2698 * The routine will search the list of remote ports it maintains 2699 * internally on behalf of consistent target id mappings. If found, the 2700 * remote port structure will be reused. Otherwise, a new remote port 2701 * structure will be allocated. 2702 * 2703 * Whenever a remote port is allocated, a new fc_remote_port class 2704 * device is created. 2705 * 2706 * Should not be called from interrupt context. 2707 * 2708 * Notes: 2709 * This routine assumes no locks are held on entry. 2710 */ 2711 struct fc_rport * 2712 fc_remote_port_add(struct Scsi_Host *shost, int channel, 2713 struct fc_rport_identifiers *ids) 2714 { 2715 struct fc_internal *fci = to_fc_internal(shost->transportt); 2716 struct fc_host_attrs *fc_host = shost_to_fc_host(shost); 2717 struct fc_rport *rport; 2718 unsigned long flags; 2719 int match = 0; 2720 2721 /* ensure any stgt delete functions are done */ 2722 fc_flush_work(shost); 2723 2724 /* 2725 * Search the list of "active" rports, for an rport that has been 2726 * deleted, but we've held off the real delete while the target 2727 * is in a "blocked" state. 2728 */ 2729 spin_lock_irqsave(shost->host_lock, flags); 2730 2731 list_for_each_entry(rport, &fc_host->rports, peers) { 2732 2733 if ((rport->port_state == FC_PORTSTATE_BLOCKED || 2734 rport->port_state == FC_PORTSTATE_NOTPRESENT) && 2735 (rport->channel == channel)) { 2736 2737 switch (fc_host->tgtid_bind_type) { 2738 case FC_TGTID_BIND_BY_WWPN: 2739 case FC_TGTID_BIND_NONE: 2740 if (rport->port_name == ids->port_name) 2741 match = 1; 2742 break; 2743 case FC_TGTID_BIND_BY_WWNN: 2744 if (rport->node_name == ids->node_name) 2745 match = 1; 2746 break; 2747 case FC_TGTID_BIND_BY_ID: 2748 if (rport->port_id == ids->port_id) 2749 match = 1; 2750 break; 2751 } 2752 2753 if (match) { 2754 2755 memcpy(&rport->node_name, &ids->node_name, 2756 sizeof(rport->node_name)); 2757 memcpy(&rport->port_name, &ids->port_name, 2758 sizeof(rport->port_name)); 2759 rport->port_id = ids->port_id; 2760 2761 rport->port_state = FC_PORTSTATE_ONLINE; 2762 rport->roles = ids->roles; 2763 2764 spin_unlock_irqrestore(shost->host_lock, flags); 2765 2766 if (fci->f->dd_fcrport_size) 2767 memset(rport->dd_data, 0, 2768 fci->f->dd_fcrport_size); 2769 2770 /* 2771 * If we were not a target, cancel the 2772 * io terminate and rport timers, and 2773 * we're done. 2774 * 2775 * If we were a target, but our new role 2776 * doesn't indicate a target, leave the 2777 * timers running expecting the role to 2778 * change as the target fully logs in. If 2779 * it doesn't, the target will be torn down. 2780 * 2781 * If we were a target, and our role shows 2782 * we're still a target, cancel the timers 2783 * and kick off a scan. 2784 */ 2785 2786 /* was a target, not in roles */ 2787 if ((rport->scsi_target_id != -1) && 2788 (!(ids->roles & FC_PORT_ROLE_FCP_TARGET))) 2789 return rport; 2790 2791 /* 2792 * Stop the fail io and dev_loss timers. 2793 * If they flush, the port_state will 2794 * be checked and will NOOP the function. 2795 */ 2796 if (!cancel_delayed_work(&rport->fail_io_work)) 2797 fc_flush_devloss(shost); 2798 if (!cancel_delayed_work(&rport->dev_loss_work)) 2799 fc_flush_devloss(shost); 2800 2801 spin_lock_irqsave(shost->host_lock, flags); 2802 2803 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT | 2804 FC_RPORT_DEVLOSS_PENDING | 2805 FC_RPORT_DEVLOSS_CALLBK_DONE); 2806 2807 spin_unlock_irqrestore(shost->host_lock, flags); 2808 2809 /* if target, initiate a scan */ 2810 if (rport->scsi_target_id != -1) { 2811 scsi_target_unblock(&rport->dev, 2812 SDEV_RUNNING); 2813 spin_lock_irqsave(shost->host_lock, 2814 flags); 2815 rport->flags |= FC_RPORT_SCAN_PENDING; 2816 scsi_queue_work(shost, 2817 &rport->scan_work); 2818 spin_unlock_irqrestore(shost->host_lock, 2819 flags); 2820 } 2821 2822 fc_bsg_goose_queue(rport); 2823 2824 return rport; 2825 } 2826 } 2827 } 2828 2829 /* 2830 * Search the bindings array 2831 * Note: if never a FCP target, you won't be on this list 2832 */ 2833 if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) { 2834 2835 /* search for a matching consistent binding */ 2836 2837 list_for_each_entry(rport, &fc_host->rport_bindings, 2838 peers) { 2839 if (rport->channel != channel) 2840 continue; 2841 2842 switch (fc_host->tgtid_bind_type) { 2843 case FC_TGTID_BIND_BY_WWPN: 2844 if (rport->port_name == ids->port_name) 2845 match = 1; 2846 break; 2847 case FC_TGTID_BIND_BY_WWNN: 2848 if (rport->node_name == ids->node_name) 2849 match = 1; 2850 break; 2851 case FC_TGTID_BIND_BY_ID: 2852 if (rport->port_id == ids->port_id) 2853 match = 1; 2854 break; 2855 case FC_TGTID_BIND_NONE: /* to keep compiler happy */ 2856 break; 2857 } 2858 2859 if (match) { 2860 list_move_tail(&rport->peers, &fc_host->rports); 2861 break; 2862 } 2863 } 2864 2865 if (match) { 2866 memcpy(&rport->node_name, &ids->node_name, 2867 sizeof(rport->node_name)); 2868 memcpy(&rport->port_name, &ids->port_name, 2869 sizeof(rport->port_name)); 2870 rport->port_id = ids->port_id; 2871 rport->port_state = FC_PORTSTATE_ONLINE; 2872 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT; 2873 2874 if (fci->f->dd_fcrport_size) 2875 memset(rport->dd_data, 0, 2876 fci->f->dd_fcrport_size); 2877 spin_unlock_irqrestore(shost->host_lock, flags); 2878 2879 fc_remote_port_rolechg(rport, ids->roles); 2880 return rport; 2881 } 2882 } 2883 2884 spin_unlock_irqrestore(shost->host_lock, flags); 2885 2886 /* No consistent binding found - create new remote port entry */ 2887 rport = fc_remote_port_create(shost, channel, ids); 2888 2889 return rport; 2890 } 2891 EXPORT_SYMBOL(fc_remote_port_add); 2892 2893 2894 /** 2895 * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence. 2896 * @rport: The remote port that no longer exists 2897 * 2898 * The LLDD calls this routine to notify the transport that a remote 2899 * port is no longer part of the topology. Note: Although a port 2900 * may no longer be part of the topology, it may persist in the remote 2901 * ports displayed by the fc_host. We do this under 2 conditions: 2902 * 2903 * 1) If the port was a scsi target, we delay its deletion by "blocking" it. 2904 * This allows the port to temporarily disappear, then reappear without 2905 * disrupting the SCSI device tree attached to it. During the "blocked" 2906 * period the port will still exist. 2907 * 2908 * 2) If the port was a scsi target and disappears for longer than we 2909 * expect, we'll delete the port and the tear down the SCSI device tree 2910 * attached to it. However, we want to semi-persist the target id assigned 2911 * to that port if it eventually does exist. The port structure will 2912 * remain (although with minimal information) so that the target id 2913 * bindings also remain. 2914 * 2915 * If the remote port is not an FCP Target, it will be fully torn down 2916 * and deallocated, including the fc_remote_port class device. 2917 * 2918 * If the remote port is an FCP Target, the port will be placed in a 2919 * temporary blocked state. From the LLDD's perspective, the rport no 2920 * longer exists. From the SCSI midlayer's perspective, the SCSI target 2921 * exists, but all sdevs on it are blocked from further I/O. The following 2922 * is then expected. 2923 * 2924 * If the remote port does not return (signaled by a LLDD call to 2925 * fc_remote_port_add()) within the dev_loss_tmo timeout, then the 2926 * scsi target is removed - killing all outstanding i/o and removing the 2927 * scsi devices attached to it. The port structure will be marked Not 2928 * Present and be partially cleared, leaving only enough information to 2929 * recognize the remote port relative to the scsi target id binding if 2930 * it later appears. The port will remain as long as there is a valid 2931 * binding (e.g. until the user changes the binding type or unloads the 2932 * scsi host with the binding). 2933 * 2934 * If the remote port returns within the dev_loss_tmo value (and matches 2935 * according to the target id binding type), the port structure will be 2936 * reused. If it is no longer a SCSI target, the target will be torn 2937 * down. If it continues to be a SCSI target, then the target will be 2938 * unblocked (allowing i/o to be resumed), and a scan will be activated 2939 * to ensure that all luns are detected. 2940 * 2941 * Called from normal process context only - cannot be called from interrupt. 2942 * 2943 * Notes: 2944 * This routine assumes no locks are held on entry. 2945 */ 2946 void 2947 fc_remote_port_delete(struct fc_rport *rport) 2948 { 2949 struct Scsi_Host *shost = rport_to_shost(rport); 2950 unsigned long timeout = rport->dev_loss_tmo; 2951 unsigned long flags; 2952 2953 /* 2954 * No need to flush the fc_host work_q's, as all adds are synchronous. 2955 * 2956 * We do need to reclaim the rport scan work element, so eventually 2957 * (in fc_rport_final_delete()) we'll flush the scsi host work_q if 2958 * there's still a scan pending. 2959 */ 2960 2961 spin_lock_irqsave(shost->host_lock, flags); 2962 2963 if (rport->port_state != FC_PORTSTATE_ONLINE) { 2964 spin_unlock_irqrestore(shost->host_lock, flags); 2965 return; 2966 } 2967 2968 /* 2969 * In the past, we if this was not an FCP-Target, we would 2970 * unconditionally just jump to deleting the rport. 2971 * However, rports can be used as node containers by the LLDD, 2972 * and its not appropriate to just terminate the rport at the 2973 * first sign of a loss in connectivity. The LLDD may want to 2974 * send ELS traffic to re-validate the login. If the rport is 2975 * immediately deleted, it makes it inappropriate for a node 2976 * container. 2977 * So... we now unconditionally wait dev_loss_tmo before 2978 * destroying an rport. 2979 */ 2980 2981 rport->port_state = FC_PORTSTATE_BLOCKED; 2982 2983 rport->flags |= FC_RPORT_DEVLOSS_PENDING; 2984 2985 spin_unlock_irqrestore(shost->host_lock, flags); 2986 2987 scsi_target_block(&rport->dev); 2988 2989 /* see if we need to kill io faster than waiting for device loss */ 2990 if ((rport->fast_io_fail_tmo != -1) && 2991 (rport->fast_io_fail_tmo < timeout)) 2992 fc_queue_devloss_work(shost, &rport->fail_io_work, 2993 rport->fast_io_fail_tmo * HZ); 2994 2995 /* cap the length the devices can be blocked until they are deleted */ 2996 fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ); 2997 } 2998 EXPORT_SYMBOL(fc_remote_port_delete); 2999 3000 /** 3001 * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed. 3002 * @rport: The remote port that changed. 3003 * @roles: New roles for this port. 3004 * 3005 * Description: The LLDD calls this routine to notify the transport that the 3006 * roles on a remote port may have changed. The largest effect of this is 3007 * if a port now becomes a FCP Target, it must be allocated a 3008 * scsi target id. If the port is no longer a FCP target, any 3009 * scsi target id value assigned to it will persist in case the 3010 * role changes back to include FCP Target. No changes in the scsi 3011 * midlayer will be invoked if the role changes (in the expectation 3012 * that the role will be resumed. If it doesn't normal error processing 3013 * will take place). 3014 * 3015 * Should not be called from interrupt context. 3016 * 3017 * Notes: 3018 * This routine assumes no locks are held on entry. 3019 */ 3020 void 3021 fc_remote_port_rolechg(struct fc_rport *rport, u32 roles) 3022 { 3023 struct Scsi_Host *shost = rport_to_shost(rport); 3024 struct fc_host_attrs *fc_host = shost_to_fc_host(shost); 3025 unsigned long flags; 3026 int create = 0; 3027 3028 spin_lock_irqsave(shost->host_lock, flags); 3029 if (roles & FC_PORT_ROLE_FCP_TARGET) { 3030 if (rport->scsi_target_id == -1) { 3031 rport->scsi_target_id = fc_host->next_target_id++; 3032 create = 1; 3033 } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET)) 3034 create = 1; 3035 } 3036 3037 rport->roles = roles; 3038 3039 spin_unlock_irqrestore(shost->host_lock, flags); 3040 3041 if (create) { 3042 /* 3043 * There may have been a delete timer running on the 3044 * port. Ensure that it is cancelled as we now know 3045 * the port is an FCP Target. 3046 * Note: we know the rport exists and is in an online 3047 * state as the LLDD would not have had an rport 3048 * reference to pass us. 3049 * 3050 * Take no action on the del_timer failure as the state 3051 * machine state change will validate the 3052 * transaction. 3053 */ 3054 if (!cancel_delayed_work(&rport->fail_io_work)) 3055 fc_flush_devloss(shost); 3056 if (!cancel_delayed_work(&rport->dev_loss_work)) 3057 fc_flush_devloss(shost); 3058 3059 spin_lock_irqsave(shost->host_lock, flags); 3060 rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT | 3061 FC_RPORT_DEVLOSS_PENDING | 3062 FC_RPORT_DEVLOSS_CALLBK_DONE); 3063 spin_unlock_irqrestore(shost->host_lock, flags); 3064 3065 /* ensure any stgt delete functions are done */ 3066 fc_flush_work(shost); 3067 3068 scsi_target_unblock(&rport->dev, SDEV_RUNNING); 3069 /* initiate a scan of the target */ 3070 spin_lock_irqsave(shost->host_lock, flags); 3071 rport->flags |= FC_RPORT_SCAN_PENDING; 3072 scsi_queue_work(shost, &rport->scan_work); 3073 spin_unlock_irqrestore(shost->host_lock, flags); 3074 } 3075 } 3076 EXPORT_SYMBOL(fc_remote_port_rolechg); 3077 3078 /** 3079 * fc_timeout_deleted_rport - Timeout handler for a deleted remote port. 3080 * @work: rport target that failed to reappear in the allotted time. 3081 * 3082 * Description: An attempt to delete a remote port blocks, and if it fails 3083 * to return in the allotted time this gets called. 3084 */ 3085 static void 3086 fc_timeout_deleted_rport(struct work_struct *work) 3087 { 3088 struct fc_rport *rport = 3089 container_of(work, struct fc_rport, dev_loss_work.work); 3090 struct Scsi_Host *shost = rport_to_shost(rport); 3091 struct fc_internal *i = to_fc_internal(shost->transportt); 3092 struct fc_host_attrs *fc_host = shost_to_fc_host(shost); 3093 unsigned long flags; 3094 int do_callback = 0; 3095 3096 spin_lock_irqsave(shost->host_lock, flags); 3097 3098 rport->flags &= ~FC_RPORT_DEVLOSS_PENDING; 3099 3100 /* 3101 * If the port is ONLINE, then it came back. If it was a SCSI 3102 * target, validate it still is. If not, tear down the 3103 * scsi_target on it. 3104 */ 3105 if ((rport->port_state == FC_PORTSTATE_ONLINE) && 3106 (rport->scsi_target_id != -1) && 3107 !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) { 3108 dev_printk(KERN_ERR, &rport->dev, 3109 "blocked FC remote port time out: no longer" 3110 " a FCP target, removing starget\n"); 3111 spin_unlock_irqrestore(shost->host_lock, flags); 3112 scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE); 3113 fc_queue_work(shost, &rport->stgt_delete_work); 3114 return; 3115 } 3116 3117 /* NOOP state - we're flushing workq's */ 3118 if (rport->port_state != FC_PORTSTATE_BLOCKED) { 3119 spin_unlock_irqrestore(shost->host_lock, flags); 3120 dev_printk(KERN_ERR, &rport->dev, 3121 "blocked FC remote port time out: leaving" 3122 " rport%s alone\n", 3123 (rport->scsi_target_id != -1) ? " and starget" : ""); 3124 return; 3125 } 3126 3127 if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) || 3128 (rport->scsi_target_id == -1)) { 3129 list_del(&rport->peers); 3130 rport->port_state = FC_PORTSTATE_DELETED; 3131 dev_printk(KERN_ERR, &rport->dev, 3132 "blocked FC remote port time out: removing" 3133 " rport%s\n", 3134 (rport->scsi_target_id != -1) ? " and starget" : ""); 3135 fc_queue_work(shost, &rport->rport_delete_work); 3136 spin_unlock_irqrestore(shost->host_lock, flags); 3137 return; 3138 } 3139 3140 dev_printk(KERN_ERR, &rport->dev, 3141 "blocked FC remote port time out: removing target and " 3142 "saving binding\n"); 3143 3144 list_move_tail(&rport->peers, &fc_host->rport_bindings); 3145 3146 /* 3147 * Note: We do not remove or clear the hostdata area. This allows 3148 * host-specific target data to persist along with the 3149 * scsi_target_id. It's up to the host to manage it's hostdata area. 3150 */ 3151 3152 /* 3153 * Reinitialize port attributes that may change if the port comes back. 3154 */ 3155 rport->maxframe_size = -1; 3156 rport->supported_classes = FC_COS_UNSPECIFIED; 3157 rport->roles = FC_PORT_ROLE_UNKNOWN; 3158 rport->port_state = FC_PORTSTATE_NOTPRESENT; 3159 rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT; 3160 3161 /* 3162 * Pre-emptively kill I/O rather than waiting for the work queue 3163 * item to teardown the starget. (FCOE libFC folks prefer this 3164 * and to have the rport_port_id still set when it's done). 3165 */ 3166 spin_unlock_irqrestore(shost->host_lock, flags); 3167 fc_terminate_rport_io(rport); 3168 3169 spin_lock_irqsave(shost->host_lock, flags); 3170 3171 if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */ 3172 3173 /* remove the identifiers that aren't used in the consisting binding */ 3174 switch (fc_host->tgtid_bind_type) { 3175 case FC_TGTID_BIND_BY_WWPN: 3176 rport->node_name = -1; 3177 rport->port_id = -1; 3178 break; 3179 case FC_TGTID_BIND_BY_WWNN: 3180 rport->port_name = -1; 3181 rport->port_id = -1; 3182 break; 3183 case FC_TGTID_BIND_BY_ID: 3184 rport->node_name = -1; 3185 rport->port_name = -1; 3186 break; 3187 case FC_TGTID_BIND_NONE: /* to keep compiler happy */ 3188 break; 3189 } 3190 3191 /* 3192 * As this only occurs if the remote port (scsi target) 3193 * went away and didn't come back - we'll remove 3194 * all attached scsi devices. 3195 */ 3196 rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE; 3197 fc_queue_work(shost, &rport->stgt_delete_work); 3198 3199 do_callback = 1; 3200 } 3201 3202 spin_unlock_irqrestore(shost->host_lock, flags); 3203 3204 /* 3205 * Notify the driver that the rport is now dead. The LLDD will 3206 * also guarantee that any communication to the rport is terminated 3207 * 3208 * Note: we set the CALLBK_DONE flag above to correspond 3209 */ 3210 if (do_callback && i->f->dev_loss_tmo_callbk) 3211 i->f->dev_loss_tmo_callbk(rport); 3212 } 3213 3214 3215 /** 3216 * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target. 3217 * @work: rport to terminate io on. 3218 * 3219 * Notes: Only requests the failure of the io, not that all are flushed 3220 * prior to returning. 3221 */ 3222 static void 3223 fc_timeout_fail_rport_io(struct work_struct *work) 3224 { 3225 struct fc_rport *rport = 3226 container_of(work, struct fc_rport, fail_io_work.work); 3227 3228 if (rport->port_state != FC_PORTSTATE_BLOCKED) 3229 return; 3230 3231 rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT; 3232 fc_terminate_rport_io(rport); 3233 } 3234 3235 /** 3236 * fc_scsi_scan_rport - called to perform a scsi scan on a remote port. 3237 * @work: remote port to be scanned. 3238 */ 3239 static void 3240 fc_scsi_scan_rport(struct work_struct *work) 3241 { 3242 struct fc_rport *rport = 3243 container_of(work, struct fc_rport, scan_work); 3244 struct Scsi_Host *shost = rport_to_shost(rport); 3245 struct fc_internal *i = to_fc_internal(shost->transportt); 3246 unsigned long flags; 3247 3248 if ((rport->port_state == FC_PORTSTATE_ONLINE) && 3249 (rport->roles & FC_PORT_ROLE_FCP_TARGET) && 3250 !(i->f->disable_target_scan)) { 3251 scsi_scan_target(&rport->dev, rport->channel, 3252 rport->scsi_target_id, SCAN_WILD_CARD, 3253 SCSI_SCAN_RESCAN); 3254 } 3255 3256 spin_lock_irqsave(shost->host_lock, flags); 3257 rport->flags &= ~FC_RPORT_SCAN_PENDING; 3258 spin_unlock_irqrestore(shost->host_lock, flags); 3259 } 3260 3261 /** 3262 * fc_block_rport() - Block SCSI eh thread for blocked fc_rport. 3263 * @rport: Remote port that scsi_eh is trying to recover. 3264 * 3265 * This routine can be called from a FC LLD scsi_eh callback. It 3266 * blocks the scsi_eh thread until the fc_rport leaves the 3267 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is 3268 * necessary to avoid the scsi_eh failing recovery actions for blocked 3269 * rports which would lead to offlined SCSI devices. 3270 * 3271 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED. 3272 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be 3273 * passed back to scsi_eh. 3274 */ 3275 int fc_block_rport(struct fc_rport *rport) 3276 { 3277 struct Scsi_Host *shost = rport_to_shost(rport); 3278 unsigned long flags; 3279 3280 spin_lock_irqsave(shost->host_lock, flags); 3281 while (rport->port_state == FC_PORTSTATE_BLOCKED && 3282 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) { 3283 spin_unlock_irqrestore(shost->host_lock, flags); 3284 msleep(1000); 3285 spin_lock_irqsave(shost->host_lock, flags); 3286 } 3287 spin_unlock_irqrestore(shost->host_lock, flags); 3288 3289 if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT) 3290 return FAST_IO_FAIL; 3291 3292 return 0; 3293 } 3294 EXPORT_SYMBOL(fc_block_rport); 3295 3296 /** 3297 * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport 3298 * @cmnd: SCSI command that scsi_eh is trying to recover 3299 * 3300 * This routine can be called from a FC LLD scsi_eh callback. It 3301 * blocks the scsi_eh thread until the fc_rport leaves the 3302 * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is 3303 * necessary to avoid the scsi_eh failing recovery actions for blocked 3304 * rports which would lead to offlined SCSI devices. 3305 * 3306 * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED. 3307 * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be 3308 * passed back to scsi_eh. 3309 */ 3310 int fc_block_scsi_eh(struct scsi_cmnd *cmnd) 3311 { 3312 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device)); 3313 3314 if (WARN_ON_ONCE(!rport)) 3315 return FAST_IO_FAIL; 3316 3317 return fc_block_rport(rport); 3318 } 3319 EXPORT_SYMBOL(fc_block_scsi_eh); 3320 3321 /** 3322 * fc_vport_setup - allocates and creates a FC virtual port. 3323 * @shost: scsi host the virtual port is connected to. 3324 * @channel: Channel on shost port connected to. 3325 * @pdev: parent device for vport 3326 * @ids: The world wide names, FC4 port roles, etc for 3327 * the virtual port. 3328 * @ret_vport: The pointer to the created vport. 3329 * 3330 * Allocates and creates the vport structure, calls the parent host 3331 * to instantiate the vport, this completes w/ class and sysfs creation. 3332 * 3333 * Notes: 3334 * This routine assumes no locks are held on entry. 3335 */ 3336 static int 3337 fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev, 3338 struct fc_vport_identifiers *ids, struct fc_vport **ret_vport) 3339 { 3340 struct fc_host_attrs *fc_host = shost_to_fc_host(shost); 3341 struct fc_internal *fci = to_fc_internal(shost->transportt); 3342 struct fc_vport *vport; 3343 struct device *dev; 3344 unsigned long flags; 3345 size_t size; 3346 int error; 3347 3348 *ret_vport = NULL; 3349 3350 if ( ! fci->f->vport_create) 3351 return -ENOENT; 3352 3353 size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size); 3354 vport = kzalloc(size, GFP_KERNEL); 3355 if (unlikely(!vport)) { 3356 printk(KERN_ERR "%s: allocation failure\n", __func__); 3357 return -ENOMEM; 3358 } 3359 3360 vport->vport_state = FC_VPORT_UNKNOWN; 3361 vport->vport_last_state = FC_VPORT_UNKNOWN; 3362 vport->node_name = ids->node_name; 3363 vport->port_name = ids->port_name; 3364 vport->roles = ids->roles; 3365 vport->vport_type = ids->vport_type; 3366 if (fci->f->dd_fcvport_size) 3367 vport->dd_data = &vport[1]; 3368 vport->shost = shost; 3369 vport->channel = channel; 3370 vport->flags = FC_VPORT_CREATING; 3371 INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete); 3372 3373 spin_lock_irqsave(shost->host_lock, flags); 3374 3375 if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) { 3376 spin_unlock_irqrestore(shost->host_lock, flags); 3377 kfree(vport); 3378 return -ENOSPC; 3379 } 3380 fc_host->npiv_vports_inuse++; 3381 vport->number = fc_host->next_vport_number++; 3382 list_add_tail(&vport->peers, &fc_host->vports); 3383 scsi_host_get(shost); /* for fc_host->vport list */ 3384 3385 spin_unlock_irqrestore(shost->host_lock, flags); 3386 3387 dev = &vport->dev; 3388 device_initialize(dev); /* takes self reference */ 3389 dev->parent = get_device(pdev); /* takes parent reference */ 3390 dev->release = fc_vport_dev_release; 3391 dev_set_name(dev, "vport-%d:%d-%d", 3392 shost->host_no, channel, vport->number); 3393 transport_setup_device(dev); 3394 3395 error = device_add(dev); 3396 if (error) { 3397 printk(KERN_ERR "FC Virtual Port device_add failed\n"); 3398 goto delete_vport; 3399 } 3400 transport_add_device(dev); 3401 transport_configure_device(dev); 3402 3403 error = fci->f->vport_create(vport, ids->disable); 3404 if (error) { 3405 printk(KERN_ERR "FC Virtual Port LLDD Create failed\n"); 3406 goto delete_vport_all; 3407 } 3408 3409 /* 3410 * if the parent isn't the physical adapter's Scsi_Host, ensure 3411 * the Scsi_Host at least contains a symlink to the vport. 3412 */ 3413 if (pdev != &shost->shost_gendev) { 3414 error = sysfs_create_link(&shost->shost_gendev.kobj, 3415 &dev->kobj, dev_name(dev)); 3416 if (error) 3417 printk(KERN_ERR 3418 "%s: Cannot create vport symlinks for " 3419 "%s, err=%d\n", 3420 __func__, dev_name(dev), error); 3421 } 3422 spin_lock_irqsave(shost->host_lock, flags); 3423 vport->flags &= ~FC_VPORT_CREATING; 3424 spin_unlock_irqrestore(shost->host_lock, flags); 3425 3426 dev_printk(KERN_NOTICE, pdev, 3427 "%s created via shost%d channel %d\n", dev_name(dev), 3428 shost->host_no, channel); 3429 3430 *ret_vport = vport; 3431 3432 return 0; 3433 3434 delete_vport_all: 3435 transport_remove_device(dev); 3436 device_del(dev); 3437 delete_vport: 3438 transport_destroy_device(dev); 3439 spin_lock_irqsave(shost->host_lock, flags); 3440 list_del(&vport->peers); 3441 scsi_host_put(shost); /* for fc_host->vport list */ 3442 fc_host->npiv_vports_inuse--; 3443 spin_unlock_irqrestore(shost->host_lock, flags); 3444 put_device(dev->parent); 3445 kfree(vport); 3446 3447 return error; 3448 } 3449 3450 /** 3451 * fc_vport_create - Admin App or LLDD requests creation of a vport 3452 * @shost: scsi host the virtual port is connected to. 3453 * @channel: channel on shost port connected to. 3454 * @ids: The world wide names, FC4 port roles, etc for 3455 * the virtual port. 3456 * 3457 * Notes: 3458 * This routine assumes no locks are held on entry. 3459 */ 3460 struct fc_vport * 3461 fc_vport_create(struct Scsi_Host *shost, int channel, 3462 struct fc_vport_identifiers *ids) 3463 { 3464 int stat; 3465 struct fc_vport *vport; 3466 3467 stat = fc_vport_setup(shost, channel, &shost->shost_gendev, 3468 ids, &vport); 3469 return stat ? NULL : vport; 3470 } 3471 EXPORT_SYMBOL(fc_vport_create); 3472 3473 /** 3474 * fc_vport_terminate - Admin App or LLDD requests termination of a vport 3475 * @vport: fc_vport to be terminated 3476 * 3477 * Calls the LLDD vport_delete() function, then deallocates and removes 3478 * the vport from the shost and object tree. 3479 * 3480 * Notes: 3481 * This routine assumes no locks are held on entry. 3482 */ 3483 int 3484 fc_vport_terminate(struct fc_vport *vport) 3485 { 3486 struct Scsi_Host *shost = vport_to_shost(vport); 3487 struct fc_host_attrs *fc_host = shost_to_fc_host(shost); 3488 struct fc_internal *i = to_fc_internal(shost->transportt); 3489 struct device *dev = &vport->dev; 3490 unsigned long flags; 3491 int stat; 3492 3493 if (i->f->vport_delete) 3494 stat = i->f->vport_delete(vport); 3495 else 3496 stat = -ENOENT; 3497 3498 spin_lock_irqsave(shost->host_lock, flags); 3499 vport->flags &= ~FC_VPORT_DELETING; 3500 if (!stat) { 3501 vport->flags |= FC_VPORT_DELETED; 3502 list_del(&vport->peers); 3503 fc_host->npiv_vports_inuse--; 3504 scsi_host_put(shost); /* for fc_host->vport list */ 3505 } 3506 spin_unlock_irqrestore(shost->host_lock, flags); 3507 3508 if (stat) 3509 return stat; 3510 3511 if (dev->parent != &shost->shost_gendev) 3512 sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev)); 3513 transport_remove_device(dev); 3514 device_del(dev); 3515 transport_destroy_device(dev); 3516 3517 /* 3518 * Removing our self-reference should mean our 3519 * release function gets called, which will drop the remaining 3520 * parent reference and free the data structure. 3521 */ 3522 put_device(dev); /* for self-reference */ 3523 3524 return 0; /* SUCCESS */ 3525 } 3526 EXPORT_SYMBOL(fc_vport_terminate); 3527 3528 /** 3529 * fc_vport_sched_delete - workq-based delete request for a vport 3530 * @work: vport to be deleted. 3531 */ 3532 static void 3533 fc_vport_sched_delete(struct work_struct *work) 3534 { 3535 struct fc_vport *vport = 3536 container_of(work, struct fc_vport, vport_delete_work); 3537 int stat; 3538 3539 stat = fc_vport_terminate(vport); 3540 if (stat) 3541 dev_printk(KERN_ERR, vport->dev.parent, 3542 "%s: %s could not be deleted created via " 3543 "shost%d channel %d - error %d\n", __func__, 3544 dev_name(&vport->dev), vport->shost->host_no, 3545 vport->channel, stat); 3546 } 3547 3548 3549 /* 3550 * BSG support 3551 */ 3552 3553 /** 3554 * fc_bsg_job_timeout - handler for when a bsg request timesout 3555 * @req: request that timed out 3556 */ 3557 static enum blk_eh_timer_return 3558 fc_bsg_job_timeout(struct request *req) 3559 { 3560 struct bsg_job *job = blk_mq_rq_to_pdu(req); 3561 struct Scsi_Host *shost = fc_bsg_to_shost(job); 3562 struct fc_rport *rport = fc_bsg_to_rport(job); 3563 struct fc_internal *i = to_fc_internal(shost->transportt); 3564 int err = 0, inflight = 0; 3565 3566 if (rport && rport->port_state == FC_PORTSTATE_BLOCKED) 3567 return BLK_EH_RESET_TIMER; 3568 3569 inflight = bsg_job_get(job); 3570 3571 if (inflight && i->f->bsg_timeout) { 3572 /* call LLDD to abort the i/o as it has timed out */ 3573 err = i->f->bsg_timeout(job); 3574 if (err == -EAGAIN) { 3575 bsg_job_put(job); 3576 return BLK_EH_RESET_TIMER; 3577 } else if (err) 3578 printk(KERN_ERR "ERROR: FC BSG request timeout - LLD " 3579 "abort failed with status %d\n", err); 3580 } 3581 3582 /* the blk_end_sync_io() doesn't check the error */ 3583 if (inflight) 3584 blk_mq_end_request(req, BLK_STS_IOERR); 3585 return BLK_EH_DONE; 3586 } 3587 3588 /** 3589 * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD 3590 * @shost: scsi host rport attached to 3591 * @job: bsg job to be processed 3592 */ 3593 static int fc_bsg_host_dispatch(struct Scsi_Host *shost, struct bsg_job *job) 3594 { 3595 struct fc_internal *i = to_fc_internal(shost->transportt); 3596 struct fc_bsg_request *bsg_request = job->request; 3597 struct fc_bsg_reply *bsg_reply = job->reply; 3598 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */ 3599 int ret; 3600 3601 /* check if we really have all the request data needed */ 3602 if (job->request_len < cmdlen) { 3603 ret = -ENOMSG; 3604 goto fail_host_msg; 3605 } 3606 3607 /* Validate the host command */ 3608 switch (bsg_request->msgcode) { 3609 case FC_BSG_HST_ADD_RPORT: 3610 cmdlen += sizeof(struct fc_bsg_host_add_rport); 3611 break; 3612 3613 case FC_BSG_HST_DEL_RPORT: 3614 cmdlen += sizeof(struct fc_bsg_host_del_rport); 3615 break; 3616 3617 case FC_BSG_HST_ELS_NOLOGIN: 3618 cmdlen += sizeof(struct fc_bsg_host_els); 3619 /* there better be a xmt and rcv payloads */ 3620 if ((!job->request_payload.payload_len) || 3621 (!job->reply_payload.payload_len)) { 3622 ret = -EINVAL; 3623 goto fail_host_msg; 3624 } 3625 break; 3626 3627 case FC_BSG_HST_CT: 3628 cmdlen += sizeof(struct fc_bsg_host_ct); 3629 /* there better be xmt and rcv payloads */ 3630 if ((!job->request_payload.payload_len) || 3631 (!job->reply_payload.payload_len)) { 3632 ret = -EINVAL; 3633 goto fail_host_msg; 3634 } 3635 break; 3636 3637 case FC_BSG_HST_VENDOR: 3638 cmdlen += sizeof(struct fc_bsg_host_vendor); 3639 if ((shost->hostt->vendor_id == 0L) || 3640 (bsg_request->rqst_data.h_vendor.vendor_id != 3641 shost->hostt->vendor_id)) { 3642 ret = -ESRCH; 3643 goto fail_host_msg; 3644 } 3645 break; 3646 3647 default: 3648 ret = -EBADR; 3649 goto fail_host_msg; 3650 } 3651 3652 ret = i->f->bsg_request(job); 3653 if (!ret) 3654 return 0; 3655 3656 fail_host_msg: 3657 /* return the errno failure code as the only status */ 3658 BUG_ON(job->reply_len < sizeof(uint32_t)); 3659 bsg_reply->reply_payload_rcv_len = 0; 3660 bsg_reply->result = ret; 3661 job->reply_len = sizeof(uint32_t); 3662 bsg_job_done(job, bsg_reply->result, 3663 bsg_reply->reply_payload_rcv_len); 3664 return 0; 3665 } 3666 3667 3668 /* 3669 * fc_bsg_goose_queue - restart rport queue in case it was stopped 3670 * @rport: rport to be restarted 3671 */ 3672 static void 3673 fc_bsg_goose_queue(struct fc_rport *rport) 3674 { 3675 struct request_queue *q = rport->rqst_q; 3676 3677 if (q) 3678 blk_mq_run_hw_queues(q, true); 3679 } 3680 3681 /** 3682 * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD 3683 * @shost: scsi host rport attached to 3684 * @job: bsg job to be processed 3685 */ 3686 static int fc_bsg_rport_dispatch(struct Scsi_Host *shost, struct bsg_job *job) 3687 { 3688 struct fc_internal *i = to_fc_internal(shost->transportt); 3689 struct fc_bsg_request *bsg_request = job->request; 3690 struct fc_bsg_reply *bsg_reply = job->reply; 3691 int cmdlen = sizeof(uint32_t); /* start with length of msgcode */ 3692 int ret; 3693 3694 /* check if we really have all the request data needed */ 3695 if (job->request_len < cmdlen) { 3696 ret = -ENOMSG; 3697 goto fail_rport_msg; 3698 } 3699 3700 /* Validate the rport command */ 3701 switch (bsg_request->msgcode) { 3702 case FC_BSG_RPT_ELS: 3703 cmdlen += sizeof(struct fc_bsg_rport_els); 3704 goto check_bidi; 3705 3706 case FC_BSG_RPT_CT: 3707 cmdlen += sizeof(struct fc_bsg_rport_ct); 3708 check_bidi: 3709 /* there better be xmt and rcv payloads */ 3710 if ((!job->request_payload.payload_len) || 3711 (!job->reply_payload.payload_len)) { 3712 ret = -EINVAL; 3713 goto fail_rport_msg; 3714 } 3715 break; 3716 default: 3717 ret = -EBADR; 3718 goto fail_rport_msg; 3719 } 3720 3721 ret = i->f->bsg_request(job); 3722 if (!ret) 3723 return 0; 3724 3725 fail_rport_msg: 3726 /* return the errno failure code as the only status */ 3727 BUG_ON(job->reply_len < sizeof(uint32_t)); 3728 bsg_reply->reply_payload_rcv_len = 0; 3729 bsg_reply->result = ret; 3730 job->reply_len = sizeof(uint32_t); 3731 bsg_job_done(job, bsg_reply->result, 3732 bsg_reply->reply_payload_rcv_len); 3733 return 0; 3734 } 3735 3736 static int fc_bsg_dispatch(struct bsg_job *job) 3737 { 3738 struct Scsi_Host *shost = fc_bsg_to_shost(job); 3739 3740 if (scsi_is_fc_rport(job->dev)) 3741 return fc_bsg_rport_dispatch(shost, job); 3742 else 3743 return fc_bsg_host_dispatch(shost, job); 3744 } 3745 3746 static blk_status_t fc_bsg_rport_prep(struct fc_rport *rport) 3747 { 3748 if (rport->port_state == FC_PORTSTATE_BLOCKED && 3749 !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) 3750 return BLK_STS_RESOURCE; 3751 3752 if (rport->port_state != FC_PORTSTATE_ONLINE) 3753 return BLK_STS_IOERR; 3754 3755 return BLK_STS_OK; 3756 } 3757 3758 3759 static int fc_bsg_dispatch_prep(struct bsg_job *job) 3760 { 3761 struct fc_rport *rport = fc_bsg_to_rport(job); 3762 blk_status_t ret; 3763 3764 ret = fc_bsg_rport_prep(rport); 3765 switch (ret) { 3766 case BLK_STS_OK: 3767 break; 3768 case BLK_STS_RESOURCE: 3769 return -EAGAIN; 3770 default: 3771 return -EIO; 3772 } 3773 3774 return fc_bsg_dispatch(job); 3775 } 3776 3777 /** 3778 * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests 3779 * @shost: shost for fc_host 3780 * @fc_host: fc_host adding the structures to 3781 */ 3782 static int 3783 fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host) 3784 { 3785 struct device *dev = &shost->shost_gendev; 3786 struct fc_internal *i = to_fc_internal(shost->transportt); 3787 struct request_queue *q; 3788 char bsg_name[20]; 3789 3790 fc_host->rqst_q = NULL; 3791 3792 if (!i->f->bsg_request) 3793 return -ENOTSUPP; 3794 3795 snprintf(bsg_name, sizeof(bsg_name), 3796 "fc_host%d", shost->host_no); 3797 3798 q = bsg_setup_queue(dev, bsg_name, fc_bsg_dispatch, fc_bsg_job_timeout, 3799 i->f->dd_bsg_size); 3800 if (IS_ERR(q)) { 3801 dev_err(dev, 3802 "fc_host%d: bsg interface failed to initialize - setup queue\n", 3803 shost->host_no); 3804 return PTR_ERR(q); 3805 } 3806 __scsi_init_queue(shost, q); 3807 blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT); 3808 fc_host->rqst_q = q; 3809 return 0; 3810 } 3811 3812 /** 3813 * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests 3814 * @shost: shost that rport is attached to 3815 * @rport: rport that the bsg hooks are being attached to 3816 */ 3817 static int 3818 fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport) 3819 { 3820 struct device *dev = &rport->dev; 3821 struct fc_internal *i = to_fc_internal(shost->transportt); 3822 struct request_queue *q; 3823 3824 rport->rqst_q = NULL; 3825 3826 if (!i->f->bsg_request) 3827 return -ENOTSUPP; 3828 3829 q = bsg_setup_queue(dev, dev_name(dev), fc_bsg_dispatch_prep, 3830 fc_bsg_job_timeout, i->f->dd_bsg_size); 3831 if (IS_ERR(q)) { 3832 dev_err(dev, "failed to setup bsg queue\n"); 3833 return PTR_ERR(q); 3834 } 3835 __scsi_init_queue(shost, q); 3836 blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT); 3837 rport->rqst_q = q; 3838 return 0; 3839 } 3840 3841 3842 /** 3843 * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports 3844 * @q: the request_queue that is to be torn down. 3845 * 3846 * Notes: 3847 * Before unregistering the queue empty any requests that are blocked 3848 * 3849 * 3850 */ 3851 static void 3852 fc_bsg_remove(struct request_queue *q) 3853 { 3854 bsg_remove_queue(q); 3855 } 3856 3857 3858 /* Original Author: Martin Hicks */ 3859 MODULE_AUTHOR("James Smart"); 3860 MODULE_DESCRIPTION("FC Transport Attributes"); 3861 MODULE_LICENSE("GPL"); 3862 3863 module_init(fc_transport_init); 3864 module_exit(fc_transport_exit); 3865