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