scsi_transport_sas.c revision 79cb1819e231f811211133a09a5382cb89d7ec67
1/* 2 * Copyright (C) 2005-2006 Dell Inc. 3 * Released under GPL v2. 4 * 5 * Serial Attached SCSI (SAS) transport class. 6 * 7 * The SAS transport class contains common code to deal with SAS HBAs, 8 * an aproximated representation of SAS topologies in the driver model, 9 * and various sysfs attributes to expose these topologies and managment 10 * interfaces to userspace. 11 * 12 * In addition to the basic SCSI core objects this transport class 13 * introduces two additional intermediate objects: The SAS PHY 14 * as represented by struct sas_phy defines an "outgoing" PHY on 15 * a SAS HBA or Expander, and the SAS remote PHY represented by 16 * struct sas_rphy defines an "incoming" PHY on a SAS Expander or 17 * end device. Note that this is purely a software concept, the 18 * underlying hardware for a PHY and a remote PHY is the exactly 19 * the same. 20 * 21 * There is no concept of a SAS port in this code, users can see 22 * what PHYs form a wide port based on the port_identifier attribute, 23 * which is the same for all PHYs in a port. 24 */ 25 26#include <linux/init.h> 27#include <linux/module.h> 28#include <linux/err.h> 29#include <linux/slab.h> 30#include <linux/string.h> 31 32#include <scsi/scsi.h> 33#include <scsi/scsi_device.h> 34#include <scsi/scsi_host.h> 35#include <scsi/scsi_transport.h> 36#include <scsi/scsi_transport_sas.h> 37 38 39#define SAS_HOST_ATTRS 0 40#define SAS_PORT_ATTRS 17 41#define SAS_RPORT_ATTRS 7 42#define SAS_END_DEV_ATTRS 3 43#define SAS_EXPANDER_ATTRS 7 44 45struct sas_internal { 46 struct scsi_transport_template t; 47 struct sas_function_template *f; 48 49 struct class_device_attribute private_host_attrs[SAS_HOST_ATTRS]; 50 struct class_device_attribute private_phy_attrs[SAS_PORT_ATTRS]; 51 struct class_device_attribute private_rphy_attrs[SAS_RPORT_ATTRS]; 52 struct class_device_attribute private_end_dev_attrs[SAS_END_DEV_ATTRS]; 53 struct class_device_attribute private_expander_attrs[SAS_EXPANDER_ATTRS]; 54 55 struct transport_container phy_attr_cont; 56 struct transport_container rphy_attr_cont; 57 struct transport_container end_dev_attr_cont; 58 struct transport_container expander_attr_cont; 59 60 /* 61 * The array of null terminated pointers to attributes 62 * needed by scsi_sysfs.c 63 */ 64 struct class_device_attribute *host_attrs[SAS_HOST_ATTRS + 1]; 65 struct class_device_attribute *phy_attrs[SAS_PORT_ATTRS + 1]; 66 struct class_device_attribute *rphy_attrs[SAS_RPORT_ATTRS + 1]; 67 struct class_device_attribute *end_dev_attrs[SAS_END_DEV_ATTRS + 1]; 68 struct class_device_attribute *expander_attrs[SAS_EXPANDER_ATTRS + 1]; 69}; 70#define to_sas_internal(tmpl) container_of(tmpl, struct sas_internal, t) 71 72struct sas_host_attrs { 73 struct list_head rphy_list; 74 struct mutex lock; 75 u32 next_target_id; 76 u32 next_expander_id; 77}; 78#define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data) 79 80 81/* 82 * Hack to allow attributes of the same name in different objects. 83 */ 84#define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \ 85 struct class_device_attribute class_device_attr_##_prefix##_##_name = \ 86 __ATTR(_name,_mode,_show,_store) 87 88 89/* 90 * Pretty printing helpers 91 */ 92 93#define sas_bitfield_name_match(title, table) \ 94static ssize_t \ 95get_sas_##title##_names(u32 table_key, char *buf) \ 96{ \ 97 char *prefix = ""; \ 98 ssize_t len = 0; \ 99 int i; \ 100 \ 101 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \ 102 if (table[i].value & table_key) { \ 103 len += sprintf(buf + len, "%s%s", \ 104 prefix, table[i].name); \ 105 prefix = ", "; \ 106 } \ 107 } \ 108 len += sprintf(buf + len, "\n"); \ 109 return len; \ 110} 111 112#define sas_bitfield_name_search(title, table) \ 113static ssize_t \ 114get_sas_##title##_names(u32 table_key, char *buf) \ 115{ \ 116 ssize_t len = 0; \ 117 int i; \ 118 \ 119 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \ 120 if (table[i].value == table_key) { \ 121 len += sprintf(buf + len, "%s", \ 122 table[i].name); \ 123 break; \ 124 } \ 125 } \ 126 len += sprintf(buf + len, "\n"); \ 127 return len; \ 128} 129 130static struct { 131 u32 value; 132 char *name; 133} sas_device_type_names[] = { 134 { SAS_PHY_UNUSED, "unused" }, 135 { SAS_END_DEVICE, "end device" }, 136 { SAS_EDGE_EXPANDER_DEVICE, "edge expander" }, 137 { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" }, 138}; 139sas_bitfield_name_search(device_type, sas_device_type_names) 140 141 142static struct { 143 u32 value; 144 char *name; 145} sas_protocol_names[] = { 146 { SAS_PROTOCOL_SATA, "sata" }, 147 { SAS_PROTOCOL_SMP, "smp" }, 148 { SAS_PROTOCOL_STP, "stp" }, 149 { SAS_PROTOCOL_SSP, "ssp" }, 150}; 151sas_bitfield_name_match(protocol, sas_protocol_names) 152 153static struct { 154 u32 value; 155 char *name; 156} sas_linkspeed_names[] = { 157 { SAS_LINK_RATE_UNKNOWN, "Unknown" }, 158 { SAS_PHY_DISABLED, "Phy disabled" }, 159 { SAS_LINK_RATE_FAILED, "Link Rate failed" }, 160 { SAS_SATA_SPINUP_HOLD, "Spin-up hold" }, 161 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" }, 162 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" }, 163 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" }, 164}; 165sas_bitfield_name_search(linkspeed, sas_linkspeed_names) 166 167 168/* 169 * SAS host attributes 170 */ 171 172static int sas_host_setup(struct transport_container *tc, struct device *dev, 173 struct class_device *cdev) 174{ 175 struct Scsi_Host *shost = dev_to_shost(dev); 176 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); 177 178 INIT_LIST_HEAD(&sas_host->rphy_list); 179 mutex_init(&sas_host->lock); 180 sas_host->next_target_id = 0; 181 sas_host->next_expander_id = 0; 182 return 0; 183} 184 185static DECLARE_TRANSPORT_CLASS(sas_host_class, 186 "sas_host", sas_host_setup, NULL, NULL); 187 188static int sas_host_match(struct attribute_container *cont, 189 struct device *dev) 190{ 191 struct Scsi_Host *shost; 192 struct sas_internal *i; 193 194 if (!scsi_is_host_device(dev)) 195 return 0; 196 shost = dev_to_shost(dev); 197 198 if (!shost->transportt) 199 return 0; 200 if (shost->transportt->host_attrs.ac.class != 201 &sas_host_class.class) 202 return 0; 203 204 i = to_sas_internal(shost->transportt); 205 return &i->t.host_attrs.ac == cont; 206} 207 208static int do_sas_phy_delete(struct device *dev, void *data) 209{ 210 if (scsi_is_sas_phy(dev)) 211 sas_phy_delete(dev_to_phy(dev)); 212 return 0; 213} 214 215/** 216 * sas_remove_host -- tear down a Scsi_Host's SAS data structures 217 * @shost: Scsi Host that is torn down 218 * 219 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host. 220 * Must be called just before scsi_remove_host for SAS HBAs. 221 */ 222void sas_remove_host(struct Scsi_Host *shost) 223{ 224 device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete); 225} 226EXPORT_SYMBOL(sas_remove_host); 227 228 229/* 230 * SAS Port attributes 231 */ 232 233#define sas_phy_show_simple(field, name, format_string, cast) \ 234static ssize_t \ 235show_sas_phy_##name(struct class_device *cdev, char *buf) \ 236{ \ 237 struct sas_phy *phy = transport_class_to_phy(cdev); \ 238 \ 239 return snprintf(buf, 20, format_string, cast phy->field); \ 240} 241 242#define sas_phy_simple_attr(field, name, format_string, type) \ 243 sas_phy_show_simple(field, name, format_string, (type)) \ 244static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL) 245 246#define sas_phy_show_protocol(field, name) \ 247static ssize_t \ 248show_sas_phy_##name(struct class_device *cdev, char *buf) \ 249{ \ 250 struct sas_phy *phy = transport_class_to_phy(cdev); \ 251 \ 252 if (!phy->field) \ 253 return snprintf(buf, 20, "none\n"); \ 254 return get_sas_protocol_names(phy->field, buf); \ 255} 256 257#define sas_phy_protocol_attr(field, name) \ 258 sas_phy_show_protocol(field, name) \ 259static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL) 260 261#define sas_phy_show_linkspeed(field) \ 262static ssize_t \ 263show_sas_phy_##field(struct class_device *cdev, char *buf) \ 264{ \ 265 struct sas_phy *phy = transport_class_to_phy(cdev); \ 266 \ 267 return get_sas_linkspeed_names(phy->field, buf); \ 268} 269 270#define sas_phy_linkspeed_attr(field) \ 271 sas_phy_show_linkspeed(field) \ 272static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL) 273 274#define sas_phy_show_linkerror(field) \ 275static ssize_t \ 276show_sas_phy_##field(struct class_device *cdev, char *buf) \ 277{ \ 278 struct sas_phy *phy = transport_class_to_phy(cdev); \ 279 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \ 280 struct sas_internal *i = to_sas_internal(shost->transportt); \ 281 int error; \ 282 \ 283 if (!phy->local_attached) \ 284 return -EINVAL; \ 285 \ 286 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \ 287 if (error) \ 288 return error; \ 289 return snprintf(buf, 20, "%u\n", phy->field); \ 290} 291 292#define sas_phy_linkerror_attr(field) \ 293 sas_phy_show_linkerror(field) \ 294static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL) 295 296 297static ssize_t 298show_sas_device_type(struct class_device *cdev, char *buf) 299{ 300 struct sas_phy *phy = transport_class_to_phy(cdev); 301 302 if (!phy->identify.device_type) 303 return snprintf(buf, 20, "none\n"); 304 return get_sas_device_type_names(phy->identify.device_type, buf); 305} 306static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL); 307 308static ssize_t do_sas_phy_reset(struct class_device *cdev, 309 size_t count, int hard_reset) 310{ 311 struct sas_phy *phy = transport_class_to_phy(cdev); 312 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 313 struct sas_internal *i = to_sas_internal(shost->transportt); 314 int error; 315 316 if (!phy->local_attached) 317 return -EINVAL; 318 319 error = i->f->phy_reset(phy, hard_reset); 320 if (error) 321 return error; 322 return count; 323}; 324 325static ssize_t store_sas_link_reset(struct class_device *cdev, 326 const char *buf, size_t count) 327{ 328 return do_sas_phy_reset(cdev, count, 0); 329} 330static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset); 331 332static ssize_t store_sas_hard_reset(struct class_device *cdev, 333 const char *buf, size_t count) 334{ 335 return do_sas_phy_reset(cdev, count, 1); 336} 337static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset); 338 339sas_phy_protocol_attr(identify.initiator_port_protocols, 340 initiator_port_protocols); 341sas_phy_protocol_attr(identify.target_port_protocols, 342 target_port_protocols); 343sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n", 344 unsigned long long); 345sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8); 346sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8); 347sas_phy_linkspeed_attr(negotiated_linkrate); 348sas_phy_linkspeed_attr(minimum_linkrate_hw); 349sas_phy_linkspeed_attr(minimum_linkrate); 350sas_phy_linkspeed_attr(maximum_linkrate_hw); 351sas_phy_linkspeed_attr(maximum_linkrate); 352sas_phy_linkerror_attr(invalid_dword_count); 353sas_phy_linkerror_attr(running_disparity_error_count); 354sas_phy_linkerror_attr(loss_of_dword_sync_count); 355sas_phy_linkerror_attr(phy_reset_problem_count); 356 357 358static DECLARE_TRANSPORT_CLASS(sas_phy_class, 359 "sas_phy", NULL, NULL, NULL); 360 361static int sas_phy_match(struct attribute_container *cont, struct device *dev) 362{ 363 struct Scsi_Host *shost; 364 struct sas_internal *i; 365 366 if (!scsi_is_sas_phy(dev)) 367 return 0; 368 shost = dev_to_shost(dev->parent); 369 370 if (!shost->transportt) 371 return 0; 372 if (shost->transportt->host_attrs.ac.class != 373 &sas_host_class.class) 374 return 0; 375 376 i = to_sas_internal(shost->transportt); 377 return &i->phy_attr_cont.ac == cont; 378} 379 380static void sas_phy_release(struct device *dev) 381{ 382 struct sas_phy *phy = dev_to_phy(dev); 383 384 put_device(dev->parent); 385 kfree(phy); 386} 387 388/** 389 * sas_phy_alloc -- allocates and initialize a SAS PHY structure 390 * @parent: Parent device 391 * @number: Phy index 392 * 393 * Allocates an SAS PHY structure. It will be added in the device tree 394 * below the device specified by @parent, which has to be either a Scsi_Host 395 * or sas_rphy. 396 * 397 * Returns: 398 * SAS PHY allocated or %NULL if the allocation failed. 399 */ 400struct sas_phy *sas_phy_alloc(struct device *parent, int number) 401{ 402 struct Scsi_Host *shost = dev_to_shost(parent); 403 struct sas_phy *phy; 404 405 phy = kzalloc(sizeof(*phy), GFP_KERNEL); 406 if (!phy) 407 return NULL; 408 409 get_device(parent); 410 411 phy->number = number; 412 413 device_initialize(&phy->dev); 414 phy->dev.parent = get_device(parent); 415 phy->dev.release = sas_phy_release; 416 if (scsi_is_sas_expander_device(parent)) { 417 struct sas_rphy *rphy = dev_to_rphy(parent); 418 sprintf(phy->dev.bus_id, "phy-%d-%d:%d", shost->host_no, 419 rphy->scsi_target_id, number); 420 } else 421 sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number); 422 423 transport_setup_device(&phy->dev); 424 425 return phy; 426} 427EXPORT_SYMBOL(sas_phy_alloc); 428 429/** 430 * sas_phy_add -- add a SAS PHY to the device hierachy 431 * @phy: The PHY to be added 432 * 433 * Publishes a SAS PHY to the rest of the system. 434 */ 435int sas_phy_add(struct sas_phy *phy) 436{ 437 int error; 438 439 error = device_add(&phy->dev); 440 if (!error) { 441 transport_add_device(&phy->dev); 442 transport_configure_device(&phy->dev); 443 } 444 445 return error; 446} 447EXPORT_SYMBOL(sas_phy_add); 448 449/** 450 * sas_phy_free -- free a SAS PHY 451 * @phy: SAS PHY to free 452 * 453 * Frees the specified SAS PHY. 454 * 455 * Note: 456 * This function must only be called on a PHY that has not 457 * sucessfully been added using sas_phy_add(). 458 */ 459void sas_phy_free(struct sas_phy *phy) 460{ 461 transport_destroy_device(&phy->dev); 462 put_device(phy->dev.parent); 463 put_device(phy->dev.parent); 464 put_device(phy->dev.parent); 465 kfree(phy); 466} 467EXPORT_SYMBOL(sas_phy_free); 468 469/** 470 * sas_phy_delete -- remove SAS PHY 471 * @phy: SAS PHY to remove 472 * 473 * Removes the specified SAS PHY. If the SAS PHY has an 474 * associated remote PHY it is removed before. 475 */ 476void 477sas_phy_delete(struct sas_phy *phy) 478{ 479 struct device *dev = &phy->dev; 480 481 if (phy->rphy) 482 sas_rphy_delete(phy->rphy); 483 484 transport_remove_device(dev); 485 device_del(dev); 486 transport_destroy_device(dev); 487 put_device(dev->parent); 488} 489EXPORT_SYMBOL(sas_phy_delete); 490 491/** 492 * scsi_is_sas_phy -- check if a struct device represents a SAS PHY 493 * @dev: device to check 494 * 495 * Returns: 496 * %1 if the device represents a SAS PHY, %0 else 497 */ 498int scsi_is_sas_phy(const struct device *dev) 499{ 500 return dev->release == sas_phy_release; 501} 502EXPORT_SYMBOL(scsi_is_sas_phy); 503 504/* 505 * SAS remote PHY attributes. 506 */ 507 508#define sas_rphy_show_simple(field, name, format_string, cast) \ 509static ssize_t \ 510show_sas_rphy_##name(struct class_device *cdev, char *buf) \ 511{ \ 512 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \ 513 \ 514 return snprintf(buf, 20, format_string, cast rphy->field); \ 515} 516 517#define sas_rphy_simple_attr(field, name, format_string, type) \ 518 sas_rphy_show_simple(field, name, format_string, (type)) \ 519static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \ 520 show_sas_rphy_##name, NULL) 521 522#define sas_rphy_show_protocol(field, name) \ 523static ssize_t \ 524show_sas_rphy_##name(struct class_device *cdev, char *buf) \ 525{ \ 526 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \ 527 \ 528 if (!rphy->field) \ 529 return snprintf(buf, 20, "none\n"); \ 530 return get_sas_protocol_names(rphy->field, buf); \ 531} 532 533#define sas_rphy_protocol_attr(field, name) \ 534 sas_rphy_show_protocol(field, name) \ 535static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \ 536 show_sas_rphy_##name, NULL) 537 538static ssize_t 539show_sas_rphy_device_type(struct class_device *cdev, char *buf) 540{ 541 struct sas_rphy *rphy = transport_class_to_rphy(cdev); 542 543 if (!rphy->identify.device_type) 544 return snprintf(buf, 20, "none\n"); 545 return get_sas_device_type_names( 546 rphy->identify.device_type, buf); 547} 548 549static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO, 550 show_sas_rphy_device_type, NULL); 551 552static ssize_t 553show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf) 554{ 555 struct sas_rphy *rphy = transport_class_to_rphy(cdev); 556 struct sas_phy *phy = dev_to_phy(rphy->dev.parent); 557 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 558 struct sas_internal *i = to_sas_internal(shost->transportt); 559 u64 identifier; 560 int error; 561 562 /* 563 * Only devices behind an expander are supported, because the 564 * enclosure identifier is a SMP feature. 565 */ 566 if (phy->local_attached) 567 return -EINVAL; 568 569 error = i->f->get_enclosure_identifier(rphy, &identifier); 570 if (error) 571 return error; 572 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier); 573} 574 575static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO, 576 show_sas_rphy_enclosure_identifier, NULL); 577 578static ssize_t 579show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf) 580{ 581 struct sas_rphy *rphy = transport_class_to_rphy(cdev); 582 struct sas_phy *phy = dev_to_phy(rphy->dev.parent); 583 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 584 struct sas_internal *i = to_sas_internal(shost->transportt); 585 int val; 586 587 if (phy->local_attached) 588 return -EINVAL; 589 590 val = i->f->get_bay_identifier(rphy); 591 if (val < 0) 592 return val; 593 return sprintf(buf, "%d\n", val); 594} 595 596static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO, 597 show_sas_rphy_bay_identifier, NULL); 598 599sas_rphy_protocol_attr(identify.initiator_port_protocols, 600 initiator_port_protocols); 601sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols); 602sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n", 603 unsigned long long); 604sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8); 605 606/* only need 8 bytes of data plus header (4 or 8) */ 607#define BUF_SIZE 64 608 609int sas_read_port_mode_page(struct scsi_device *sdev) 610{ 611 char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata; 612 struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target); 613 struct sas_end_device *rdev; 614 struct scsi_mode_data mode_data; 615 int res, error; 616 617 BUG_ON(rphy->identify.device_type != SAS_END_DEVICE); 618 619 rdev = rphy_to_end_device(rphy); 620 621 if (!buffer) 622 return -ENOMEM; 623 624 res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3, 625 &mode_data, NULL); 626 627 error = -EINVAL; 628 if (!scsi_status_is_good(res)) 629 goto out; 630 631 msdata = buffer + mode_data.header_length + 632 mode_data.block_descriptor_length; 633 634 if (msdata - buffer > BUF_SIZE - 8) 635 goto out; 636 637 error = 0; 638 639 rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0; 640 rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5]; 641 rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7]; 642 643 out: 644 kfree(buffer); 645 return error; 646} 647EXPORT_SYMBOL(sas_read_port_mode_page); 648 649static DECLARE_TRANSPORT_CLASS(sas_end_dev_class, 650 "sas_end_device", NULL, NULL, NULL); 651 652#define sas_end_dev_show_simple(field, name, format_string, cast) \ 653static ssize_t \ 654show_sas_end_dev_##name(struct class_device *cdev, char *buf) \ 655{ \ 656 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \ 657 struct sas_end_device *rdev = rphy_to_end_device(rphy); \ 658 \ 659 return snprintf(buf, 20, format_string, cast rdev->field); \ 660} 661 662#define sas_end_dev_simple_attr(field, name, format_string, type) \ 663 sas_end_dev_show_simple(field, name, format_string, (type)) \ 664static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \ 665 show_sas_end_dev_##name, NULL) 666 667sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int); 668sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout, 669 "%d\n", int); 670sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout, 671 "%d\n", int); 672 673static DECLARE_TRANSPORT_CLASS(sas_expander_class, 674 "sas_expander", NULL, NULL, NULL); 675 676#define sas_expander_show_simple(field, name, format_string, cast) \ 677static ssize_t \ 678show_sas_expander_##name(struct class_device *cdev, char *buf) \ 679{ \ 680 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \ 681 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \ 682 \ 683 return snprintf(buf, 20, format_string, cast edev->field); \ 684} 685 686#define sas_expander_simple_attr(field, name, format_string, type) \ 687 sas_expander_show_simple(field, name, format_string, (type)) \ 688static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO, \ 689 show_sas_expander_##name, NULL) 690 691sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *); 692sas_expander_simple_attr(product_id, product_id, "%s\n", char *); 693sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *); 694sas_expander_simple_attr(component_vendor_id, component_vendor_id, 695 "%s\n", char *); 696sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int); 697sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n", 698 unsigned int); 699sas_expander_simple_attr(level, level, "%d\n", int); 700 701static DECLARE_TRANSPORT_CLASS(sas_rphy_class, 702 "sas_rphy", NULL, NULL, NULL); 703 704static int sas_rphy_match(struct attribute_container *cont, struct device *dev) 705{ 706 struct Scsi_Host *shost; 707 struct sas_internal *i; 708 709 if (!scsi_is_sas_rphy(dev)) 710 return 0; 711 shost = dev_to_shost(dev->parent->parent); 712 713 if (!shost->transportt) 714 return 0; 715 if (shost->transportt->host_attrs.ac.class != 716 &sas_host_class.class) 717 return 0; 718 719 i = to_sas_internal(shost->transportt); 720 return &i->rphy_attr_cont.ac == cont; 721} 722 723static int sas_end_dev_match(struct attribute_container *cont, 724 struct device *dev) 725{ 726 struct Scsi_Host *shost; 727 struct sas_internal *i; 728 struct sas_rphy *rphy; 729 730 if (!scsi_is_sas_rphy(dev)) 731 return 0; 732 shost = dev_to_shost(dev->parent->parent); 733 rphy = dev_to_rphy(dev); 734 735 if (!shost->transportt) 736 return 0; 737 if (shost->transportt->host_attrs.ac.class != 738 &sas_host_class.class) 739 return 0; 740 741 i = to_sas_internal(shost->transportt); 742 return &i->end_dev_attr_cont.ac == cont && 743 rphy->identify.device_type == SAS_END_DEVICE && 744 /* FIXME: remove contained eventually */ 745 rphy->contained; 746} 747 748static int sas_expander_match(struct attribute_container *cont, 749 struct device *dev) 750{ 751 struct Scsi_Host *shost; 752 struct sas_internal *i; 753 struct sas_rphy *rphy; 754 755 if (!scsi_is_sas_rphy(dev)) 756 return 0; 757 shost = dev_to_shost(dev->parent->parent); 758 rphy = dev_to_rphy(dev); 759 760 if (!shost->transportt) 761 return 0; 762 if (shost->transportt->host_attrs.ac.class != 763 &sas_host_class.class) 764 return 0; 765 766 i = to_sas_internal(shost->transportt); 767 return &i->expander_attr_cont.ac == cont && 768 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE || 769 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE) && 770 /* FIXME: remove contained eventually */ 771 rphy->contained; 772} 773 774static void sas_rphy_release(struct device *dev) 775{ 776 struct sas_rphy *rphy = dev_to_rphy(dev); 777 778 put_device(dev->parent); 779 kfree(rphy); 780} 781 782/** 783 * sas_rphy_alloc -- allocates and initialize a SAS remote PHY structure 784 * @parent: SAS PHY this remote PHY is conneted to 785 * 786 * Allocates an SAS remote PHY structure, connected to @parent. 787 * 788 * Returns: 789 * SAS PHY allocated or %NULL if the allocation failed. 790 */ 791struct sas_rphy *sas_rphy_alloc(struct sas_phy *parent) 792{ 793 struct Scsi_Host *shost = dev_to_shost(&parent->dev); 794 struct sas_rphy *rphy; 795 796 rphy = kzalloc(sizeof(*rphy), GFP_KERNEL); 797 if (!rphy) { 798 put_device(&parent->dev); 799 return NULL; 800 } 801 802 device_initialize(&rphy->dev); 803 rphy->dev.parent = get_device(&parent->dev); 804 rphy->dev.release = sas_rphy_release; 805 sprintf(rphy->dev.bus_id, "rphy-%d:%d-%d", 806 shost->host_no, parent->port_identifier, parent->number); 807 transport_setup_device(&rphy->dev); 808 809 return rphy; 810} 811EXPORT_SYMBOL(sas_rphy_alloc); 812 813/** 814 * sas_end_device_alloc - allocate an rphy for an end device 815 * 816 * Allocates an SAS remote PHY structure, connected to @parent. 817 * 818 * Returns: 819 * SAS PHY allocated or %NULL if the allocation failed. 820 */ 821struct sas_rphy *sas_end_device_alloc(struct sas_phy *parent) 822{ 823 struct Scsi_Host *shost = dev_to_shost(&parent->dev); 824 struct sas_end_device *rdev; 825 826 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); 827 if (!rdev) { 828 put_device(&parent->dev); 829 return NULL; 830 } 831 832 device_initialize(&rdev->rphy.dev); 833 rdev->rphy.dev.parent = get_device(&parent->dev); 834 rdev->rphy.dev.release = sas_rphy_release; 835 sprintf(rdev->rphy.dev.bus_id, "rphy-%d:%d-%d", 836 shost->host_no, parent->port_identifier, parent->number); 837 rdev->rphy.identify.device_type = SAS_END_DEVICE; 838 /* FIXME: mark the rphy as being contained in a larger structure */ 839 rdev->rphy.contained = 1; 840 transport_setup_device(&rdev->rphy.dev); 841 842 return &rdev->rphy; 843} 844EXPORT_SYMBOL(sas_end_device_alloc); 845 846/** 847 * sas_expander_alloc - allocate an rphy for an end device 848 * 849 * Allocates an SAS remote PHY structure, connected to @parent. 850 * 851 * Returns: 852 * SAS PHY allocated or %NULL if the allocation failed. 853 */ 854struct sas_rphy *sas_expander_alloc(struct sas_phy *parent, 855 enum sas_device_type type) 856{ 857 struct Scsi_Host *shost = dev_to_shost(&parent->dev); 858 struct sas_expander_device *rdev; 859 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); 860 861 BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE && 862 type != SAS_FANOUT_EXPANDER_DEVICE); 863 864 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); 865 if (!rdev) { 866 put_device(&parent->dev); 867 return NULL; 868 } 869 870 device_initialize(&rdev->rphy.dev); 871 rdev->rphy.dev.parent = get_device(&parent->dev); 872 rdev->rphy.dev.release = sas_rphy_release; 873 mutex_lock(&sas_host->lock); 874 rdev->rphy.scsi_target_id = sas_host->next_expander_id++; 875 mutex_unlock(&sas_host->lock); 876 sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d", 877 shost->host_no, rdev->rphy.scsi_target_id); 878 rdev->rphy.identify.device_type = type; 879 /* FIXME: mark the rphy as being contained in a larger structure */ 880 rdev->rphy.contained = 1; 881 transport_setup_device(&rdev->rphy.dev); 882 883 return &rdev->rphy; 884} 885EXPORT_SYMBOL(sas_expander_alloc); 886 887/** 888 * sas_rphy_add -- add a SAS remote PHY to the device hierachy 889 * @rphy: The remote PHY to be added 890 * 891 * Publishes a SAS remote PHY to the rest of the system. 892 */ 893int sas_rphy_add(struct sas_rphy *rphy) 894{ 895 struct sas_phy *parent = dev_to_phy(rphy->dev.parent); 896 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent); 897 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); 898 struct sas_identify *identify = &rphy->identify; 899 int error; 900 901 if (parent->rphy) 902 return -ENXIO; 903 parent->rphy = rphy; 904 905 error = device_add(&rphy->dev); 906 if (error) 907 return error; 908 transport_add_device(&rphy->dev); 909 transport_configure_device(&rphy->dev); 910 911 mutex_lock(&sas_host->lock); 912 list_add_tail(&rphy->list, &sas_host->rphy_list); 913 if (identify->device_type == SAS_END_DEVICE && 914 (identify->target_port_protocols & 915 (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA))) 916 rphy->scsi_target_id = sas_host->next_target_id++; 917 mutex_unlock(&sas_host->lock); 918 919 if (identify->device_type == SAS_END_DEVICE && 920 rphy->scsi_target_id != -1) { 921 scsi_scan_target(&rphy->dev, parent->port_identifier, 922 rphy->scsi_target_id, ~0, 0); 923 } 924 925 return 0; 926} 927EXPORT_SYMBOL(sas_rphy_add); 928 929/** 930 * sas_rphy_free -- free a SAS remote PHY 931 * @rphy SAS remote PHY to free 932 * 933 * Frees the specified SAS remote PHY. 934 * 935 * Note: 936 * This function must only be called on a remote 937 * PHY that has not sucessfully been added using 938 * sas_rphy_add(). 939 */ 940void sas_rphy_free(struct sas_rphy *rphy) 941{ 942 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent); 943 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); 944 945 mutex_lock(&sas_host->lock); 946 list_del(&rphy->list); 947 mutex_unlock(&sas_host->lock); 948 949 transport_destroy_device(&rphy->dev); 950 put_device(rphy->dev.parent); 951 put_device(rphy->dev.parent); 952 put_device(rphy->dev.parent); 953 kfree(rphy); 954} 955EXPORT_SYMBOL(sas_rphy_free); 956 957/** 958 * sas_rphy_delete -- remove SAS remote PHY 959 * @rphy: SAS remote PHY to remove 960 * 961 * Removes the specified SAS remote PHY. 962 */ 963void 964sas_rphy_delete(struct sas_rphy *rphy) 965{ 966 struct device *dev = &rphy->dev; 967 struct sas_phy *parent = dev_to_phy(dev->parent); 968 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent); 969 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); 970 971 switch (rphy->identify.device_type) { 972 case SAS_END_DEVICE: 973 scsi_remove_target(dev); 974 break; 975 case SAS_EDGE_EXPANDER_DEVICE: 976 case SAS_FANOUT_EXPANDER_DEVICE: 977 device_for_each_child(dev, NULL, do_sas_phy_delete); 978 break; 979 default: 980 break; 981 } 982 983 transport_remove_device(dev); 984 device_del(dev); 985 transport_destroy_device(dev); 986 987 mutex_lock(&sas_host->lock); 988 list_del(&rphy->list); 989 mutex_unlock(&sas_host->lock); 990 991 parent->rphy = NULL; 992 993 put_device(&parent->dev); 994} 995EXPORT_SYMBOL(sas_rphy_delete); 996 997/** 998 * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY 999 * @dev: device to check 1000 * 1001 * Returns: 1002 * %1 if the device represents a SAS remote PHY, %0 else 1003 */ 1004int scsi_is_sas_rphy(const struct device *dev) 1005{ 1006 return dev->release == sas_rphy_release; 1007} 1008EXPORT_SYMBOL(scsi_is_sas_rphy); 1009 1010 1011/* 1012 * SCSI scan helper 1013 */ 1014 1015static int sas_user_scan(struct Scsi_Host *shost, uint channel, 1016 uint id, uint lun) 1017{ 1018 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); 1019 struct sas_rphy *rphy; 1020 1021 mutex_lock(&sas_host->lock); 1022 list_for_each_entry(rphy, &sas_host->rphy_list, list) { 1023 struct sas_phy *parent = dev_to_phy(rphy->dev.parent); 1024 1025 if (rphy->scsi_target_id == -1) 1026 continue; 1027 1028 if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) && 1029 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) { 1030 scsi_scan_target(&rphy->dev, parent->port_identifier, 1031 rphy->scsi_target_id, lun, 1); 1032 } 1033 } 1034 mutex_unlock(&sas_host->lock); 1035 1036 return 0; 1037} 1038 1039 1040/* 1041 * Setup / Teardown code 1042 */ 1043 1044#define SETUP_TEMPLATE(attrb, field, perm, test) \ 1045 i->private_##attrb[count] = class_device_attr_##field; \ 1046 i->private_##attrb[count].attr.mode = perm; \ 1047 i->private_##attrb[count].store = NULL; \ 1048 i->attrb[count] = &i->private_##attrb[count]; \ 1049 if (test) \ 1050 count++ 1051 1052 1053#define SETUP_RPORT_ATTRIBUTE(field) \ 1054 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1) 1055 1056#define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \ 1057 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func) 1058 1059#define SETUP_PORT_ATTRIBUTE(field) \ 1060 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1) 1061 1062#define SETUP_OPTIONAL_PORT_ATTRIBUTE(field, func) \ 1063 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func) 1064 1065#define SETUP_PORT_ATTRIBUTE_WRONLY(field) \ 1066 SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, 1) 1067 1068#define SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(field, func) \ 1069 SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, i->f->func) 1070 1071#define SETUP_END_DEV_ATTRIBUTE(field) \ 1072 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1) 1073 1074#define SETUP_EXPANDER_ATTRIBUTE(field) \ 1075 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1) 1076 1077/** 1078 * sas_attach_transport -- instantiate SAS transport template 1079 * @ft: SAS transport class function template 1080 */ 1081struct scsi_transport_template * 1082sas_attach_transport(struct sas_function_template *ft) 1083{ 1084 struct sas_internal *i; 1085 int count; 1086 1087 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL); 1088 if (!i) 1089 return NULL; 1090 1091 i->t.user_scan = sas_user_scan; 1092 1093 i->t.host_attrs.ac.attrs = &i->host_attrs[0]; 1094 i->t.host_attrs.ac.class = &sas_host_class.class; 1095 i->t.host_attrs.ac.match = sas_host_match; 1096 transport_container_register(&i->t.host_attrs); 1097 i->t.host_size = sizeof(struct sas_host_attrs); 1098 1099 i->phy_attr_cont.ac.class = &sas_phy_class.class; 1100 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0]; 1101 i->phy_attr_cont.ac.match = sas_phy_match; 1102 transport_container_register(&i->phy_attr_cont); 1103 1104 i->rphy_attr_cont.ac.class = &sas_rphy_class.class; 1105 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0]; 1106 i->rphy_attr_cont.ac.match = sas_rphy_match; 1107 transport_container_register(&i->rphy_attr_cont); 1108 1109 i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class; 1110 i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0]; 1111 i->end_dev_attr_cont.ac.match = sas_end_dev_match; 1112 transport_container_register(&i->end_dev_attr_cont); 1113 1114 i->expander_attr_cont.ac.class = &sas_expander_class.class; 1115 i->expander_attr_cont.ac.attrs = &i->expander_attrs[0]; 1116 i->expander_attr_cont.ac.match = sas_expander_match; 1117 transport_container_register(&i->expander_attr_cont); 1118 1119 i->f = ft; 1120 1121 count = 0; 1122 i->host_attrs[count] = NULL; 1123 1124 count = 0; 1125 SETUP_PORT_ATTRIBUTE(initiator_port_protocols); 1126 SETUP_PORT_ATTRIBUTE(target_port_protocols); 1127 SETUP_PORT_ATTRIBUTE(device_type); 1128 SETUP_PORT_ATTRIBUTE(sas_address); 1129 SETUP_PORT_ATTRIBUTE(phy_identifier); 1130 SETUP_PORT_ATTRIBUTE(port_identifier); 1131 SETUP_PORT_ATTRIBUTE(negotiated_linkrate); 1132 SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw); 1133 SETUP_PORT_ATTRIBUTE(minimum_linkrate); 1134 SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw); 1135 SETUP_PORT_ATTRIBUTE(maximum_linkrate); 1136 1137 SETUP_PORT_ATTRIBUTE(invalid_dword_count); 1138 SETUP_PORT_ATTRIBUTE(running_disparity_error_count); 1139 SETUP_PORT_ATTRIBUTE(loss_of_dword_sync_count); 1140 SETUP_PORT_ATTRIBUTE(phy_reset_problem_count); 1141 SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(link_reset, phy_reset); 1142 SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(hard_reset, phy_reset); 1143 i->phy_attrs[count] = NULL; 1144 1145 count = 0; 1146 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols); 1147 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols); 1148 SETUP_RPORT_ATTRIBUTE(rphy_device_type); 1149 SETUP_RPORT_ATTRIBUTE(rphy_sas_address); 1150 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier); 1151 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier, 1152 get_enclosure_identifier); 1153 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier, 1154 get_bay_identifier); 1155 i->rphy_attrs[count] = NULL; 1156 1157 count = 0; 1158 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning); 1159 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout); 1160 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout); 1161 i->end_dev_attrs[count] = NULL; 1162 1163 count = 0; 1164 SETUP_EXPANDER_ATTRIBUTE(vendor_id); 1165 SETUP_EXPANDER_ATTRIBUTE(product_id); 1166 SETUP_EXPANDER_ATTRIBUTE(product_rev); 1167 SETUP_EXPANDER_ATTRIBUTE(component_vendor_id); 1168 SETUP_EXPANDER_ATTRIBUTE(component_id); 1169 SETUP_EXPANDER_ATTRIBUTE(component_revision_id); 1170 SETUP_EXPANDER_ATTRIBUTE(level); 1171 i->expander_attrs[count] = NULL; 1172 1173 return &i->t; 1174} 1175EXPORT_SYMBOL(sas_attach_transport); 1176 1177/** 1178 * sas_release_transport -- release SAS transport template instance 1179 * @t: transport template instance 1180 */ 1181void sas_release_transport(struct scsi_transport_template *t) 1182{ 1183 struct sas_internal *i = to_sas_internal(t); 1184 1185 transport_container_unregister(&i->t.host_attrs); 1186 transport_container_unregister(&i->phy_attr_cont); 1187 transport_container_unregister(&i->rphy_attr_cont); 1188 transport_container_unregister(&i->end_dev_attr_cont); 1189 transport_container_unregister(&i->expander_attr_cont); 1190 1191 kfree(i); 1192} 1193EXPORT_SYMBOL(sas_release_transport); 1194 1195static __init int sas_transport_init(void) 1196{ 1197 int error; 1198 1199 error = transport_class_register(&sas_host_class); 1200 if (error) 1201 goto out; 1202 error = transport_class_register(&sas_phy_class); 1203 if (error) 1204 goto out_unregister_transport; 1205 error = transport_class_register(&sas_rphy_class); 1206 if (error) 1207 goto out_unregister_phy; 1208 error = transport_class_register(&sas_end_dev_class); 1209 if (error) 1210 goto out_unregister_rphy; 1211 error = transport_class_register(&sas_expander_class); 1212 if (error) 1213 goto out_unregister_end_dev; 1214 1215 return 0; 1216 1217 out_unregister_end_dev: 1218 transport_class_unregister(&sas_end_dev_class); 1219 out_unregister_rphy: 1220 transport_class_unregister(&sas_rphy_class); 1221 out_unregister_phy: 1222 transport_class_unregister(&sas_phy_class); 1223 out_unregister_transport: 1224 transport_class_unregister(&sas_host_class); 1225 out: 1226 return error; 1227 1228} 1229 1230static void __exit sas_transport_exit(void) 1231{ 1232 transport_class_unregister(&sas_host_class); 1233 transport_class_unregister(&sas_phy_class); 1234 transport_class_unregister(&sas_rphy_class); 1235 transport_class_unregister(&sas_end_dev_class); 1236 transport_class_unregister(&sas_expander_class); 1237} 1238 1239MODULE_AUTHOR("Christoph Hellwig"); 1240MODULE_DESCRIPTION("SAS Transphy Attributes"); 1241MODULE_LICENSE("GPL"); 1242 1243module_init(sas_transport_init); 1244module_exit(sas_transport_exit); 1245