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/jiffies.h> 29#include <linux/err.h> 30#include <linux/slab.h> 31#include <linux/string.h> 32 33#include <scsi/scsi.h> 34#include <scsi/scsi_device.h> 35#include <scsi/scsi_host.h> 36#include <scsi/scsi_transport.h> 37#include <scsi/scsi_transport_sas.h> 38 39#include "scsi_sas_internal.h" 40struct sas_host_attrs { 41 struct list_head rphy_list; 42 struct mutex lock; 43 u32 next_target_id; 44 u32 next_expander_id; 45 int next_port_id; 46}; 47#define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data) 48 49 50/* 51 * Hack to allow attributes of the same name in different objects. 52 */ 53#define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \ 54 struct class_device_attribute class_device_attr_##_prefix##_##_name = \ 55 __ATTR(_name,_mode,_show,_store) 56 57 58/* 59 * Pretty printing helpers 60 */ 61 62#define sas_bitfield_name_match(title, table) \ 63static ssize_t \ 64get_sas_##title##_names(u32 table_key, char *buf) \ 65{ \ 66 char *prefix = ""; \ 67 ssize_t len = 0; \ 68 int i; \ 69 \ 70 for (i = 0; i < ARRAY_SIZE(table); i++) { \ 71 if (table[i].value & table_key) { \ 72 len += sprintf(buf + len, "%s%s", \ 73 prefix, table[i].name); \ 74 prefix = ", "; \ 75 } \ 76 } \ 77 len += sprintf(buf + len, "\n"); \ 78 return len; \ 79} 80 81#define sas_bitfield_name_set(title, table) \ 82static ssize_t \ 83set_sas_##title##_names(u32 *table_key, const char *buf) \ 84{ \ 85 ssize_t len = 0; \ 86 int i; \ 87 \ 88 for (i = 0; i < ARRAY_SIZE(table); i++) { \ 89 len = strlen(table[i].name); \ 90 if (strncmp(buf, table[i].name, len) == 0 && \ 91 (buf[len] == '\n' || buf[len] == '\0')) { \ 92 *table_key = table[i].value; \ 93 return 0; \ 94 } \ 95 } \ 96 return -EINVAL; \ 97} 98 99#define sas_bitfield_name_search(title, table) \ 100static ssize_t \ 101get_sas_##title##_names(u32 table_key, char *buf) \ 102{ \ 103 ssize_t len = 0; \ 104 int i; \ 105 \ 106 for (i = 0; i < ARRAY_SIZE(table); i++) { \ 107 if (table[i].value == table_key) { \ 108 len += sprintf(buf + len, "%s", \ 109 table[i].name); \ 110 break; \ 111 } \ 112 } \ 113 len += sprintf(buf + len, "\n"); \ 114 return len; \ 115} 116 117static struct { 118 u32 value; 119 char *name; 120} sas_device_type_names[] = { 121 { SAS_PHY_UNUSED, "unused" }, 122 { SAS_END_DEVICE, "end device" }, 123 { SAS_EDGE_EXPANDER_DEVICE, "edge expander" }, 124 { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" }, 125}; 126sas_bitfield_name_search(device_type, sas_device_type_names) 127 128 129static struct { 130 u32 value; 131 char *name; 132} sas_protocol_names[] = { 133 { SAS_PROTOCOL_SATA, "sata" }, 134 { SAS_PROTOCOL_SMP, "smp" }, 135 { SAS_PROTOCOL_STP, "stp" }, 136 { SAS_PROTOCOL_SSP, "ssp" }, 137}; 138sas_bitfield_name_match(protocol, sas_protocol_names) 139 140static struct { 141 u32 value; 142 char *name; 143} sas_linkspeed_names[] = { 144 { SAS_LINK_RATE_UNKNOWN, "Unknown" }, 145 { SAS_PHY_DISABLED, "Phy disabled" }, 146 { SAS_LINK_RATE_FAILED, "Link Rate failed" }, 147 { SAS_SATA_SPINUP_HOLD, "Spin-up hold" }, 148 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" }, 149 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" }, 150 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" }, 151}; 152sas_bitfield_name_search(linkspeed, sas_linkspeed_names) 153sas_bitfield_name_set(linkspeed, sas_linkspeed_names) 154 155/* 156 * SAS host attributes 157 */ 158 159static int sas_host_setup(struct transport_container *tc, struct device *dev, 160 struct class_device *cdev) 161{ 162 struct Scsi_Host *shost = dev_to_shost(dev); 163 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); 164 165 INIT_LIST_HEAD(&sas_host->rphy_list); 166 mutex_init(&sas_host->lock); 167 sas_host->next_target_id = 0; 168 sas_host->next_expander_id = 0; 169 sas_host->next_port_id = 0; 170 return 0; 171} 172 173static DECLARE_TRANSPORT_CLASS(sas_host_class, 174 "sas_host", sas_host_setup, NULL, NULL); 175 176static int sas_host_match(struct attribute_container *cont, 177 struct device *dev) 178{ 179 struct Scsi_Host *shost; 180 struct sas_internal *i; 181 182 if (!scsi_is_host_device(dev)) 183 return 0; 184 shost = dev_to_shost(dev); 185 186 if (!shost->transportt) 187 return 0; 188 if (shost->transportt->host_attrs.ac.class != 189 &sas_host_class.class) 190 return 0; 191 192 i = to_sas_internal(shost->transportt); 193 return &i->t.host_attrs.ac == cont; 194} 195 196static int do_sas_phy_delete(struct device *dev, void *data) 197{ 198 int pass = (int)(unsigned long)data; 199 200 if (pass == 0 && scsi_is_sas_port(dev)) 201 sas_port_delete(dev_to_sas_port(dev)); 202 else if (pass == 1 && scsi_is_sas_phy(dev)) 203 sas_phy_delete(dev_to_phy(dev)); 204 return 0; 205} 206 207/** 208 * sas_remove_children -- tear down a devices SAS data structures 209 * @dev: device belonging to the sas object 210 * 211 * Removes all SAS PHYs and remote PHYs for a given object 212 */ 213void sas_remove_children(struct device *dev) 214{ 215 device_for_each_child(dev, (void *)0, do_sas_phy_delete); 216 device_for_each_child(dev, (void *)1, do_sas_phy_delete); 217} 218EXPORT_SYMBOL(sas_remove_children); 219 220/** 221 * sas_remove_host -- tear down a Scsi_Host's SAS data structures 222 * @shost: Scsi Host that is torn down 223 * 224 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host. 225 * Must be called just before scsi_remove_host for SAS HBAs. 226 */ 227void sas_remove_host(struct Scsi_Host *shost) 228{ 229 sas_remove_children(&shost->shost_gendev); 230} 231EXPORT_SYMBOL(sas_remove_host); 232 233 234/* 235 * SAS Phy attributes 236 */ 237 238#define sas_phy_show_simple(field, name, format_string, cast) \ 239static ssize_t \ 240show_sas_phy_##name(struct class_device *cdev, char *buf) \ 241{ \ 242 struct sas_phy *phy = transport_class_to_phy(cdev); \ 243 \ 244 return snprintf(buf, 20, format_string, cast phy->field); \ 245} 246 247#define sas_phy_simple_attr(field, name, format_string, type) \ 248 sas_phy_show_simple(field, name, format_string, (type)) \ 249static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL) 250 251#define sas_phy_show_protocol(field, name) \ 252static ssize_t \ 253show_sas_phy_##name(struct class_device *cdev, char *buf) \ 254{ \ 255 struct sas_phy *phy = transport_class_to_phy(cdev); \ 256 \ 257 if (!phy->field) \ 258 return snprintf(buf, 20, "none\n"); \ 259 return get_sas_protocol_names(phy->field, buf); \ 260} 261 262#define sas_phy_protocol_attr(field, name) \ 263 sas_phy_show_protocol(field, name) \ 264static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL) 265 266#define sas_phy_show_linkspeed(field) \ 267static ssize_t \ 268show_sas_phy_##field(struct class_device *cdev, char *buf) \ 269{ \ 270 struct sas_phy *phy = transport_class_to_phy(cdev); \ 271 \ 272 return get_sas_linkspeed_names(phy->field, buf); \ 273} 274 275/* Fudge to tell if we're minimum or maximum */ 276#define sas_phy_store_linkspeed(field) \ 277static ssize_t \ 278store_sas_phy_##field(struct class_device *cdev, const char *buf, \ 279 size_t count) \ 280{ \ 281 struct sas_phy *phy = transport_class_to_phy(cdev); \ 282 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \ 283 struct sas_internal *i = to_sas_internal(shost->transportt); \ 284 u32 value; \ 285 struct sas_phy_linkrates rates = {0}; \ 286 int error; \ 287 \ 288 error = set_sas_linkspeed_names(&value, buf); \ 289 if (error) \ 290 return error; \ 291 rates.field = value; \ 292 error = i->f->set_phy_speed(phy, &rates); \ 293 \ 294 return error ? error : count; \ 295} 296 297#define sas_phy_linkspeed_rw_attr(field) \ 298 sas_phy_show_linkspeed(field) \ 299 sas_phy_store_linkspeed(field) \ 300static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, \ 301 store_sas_phy_##field) 302 303#define sas_phy_linkspeed_attr(field) \ 304 sas_phy_show_linkspeed(field) \ 305static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL) 306 307 308#define sas_phy_show_linkerror(field) \ 309static ssize_t \ 310show_sas_phy_##field(struct class_device *cdev, char *buf) \ 311{ \ 312 struct sas_phy *phy = transport_class_to_phy(cdev); \ 313 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \ 314 struct sas_internal *i = to_sas_internal(shost->transportt); \ 315 int error; \ 316 \ 317 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \ 318 if (error) \ 319 return error; \ 320 return snprintf(buf, 20, "%u\n", phy->field); \ 321} 322 323#define sas_phy_linkerror_attr(field) \ 324 sas_phy_show_linkerror(field) \ 325static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL) 326 327 328static ssize_t 329show_sas_device_type(struct class_device *cdev, char *buf) 330{ 331 struct sas_phy *phy = transport_class_to_phy(cdev); 332 333 if (!phy->identify.device_type) 334 return snprintf(buf, 20, "none\n"); 335 return get_sas_device_type_names(phy->identify.device_type, buf); 336} 337static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL); 338 339static ssize_t do_sas_phy_enable(struct class_device *cdev, 340 size_t count, int enable) 341{ 342 struct sas_phy *phy = transport_class_to_phy(cdev); 343 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 344 struct sas_internal *i = to_sas_internal(shost->transportt); 345 int error; 346 347 error = i->f->phy_enable(phy, enable); 348 if (error) 349 return error; 350 phy->enabled = enable; 351 return count; 352}; 353 354static ssize_t store_sas_phy_enable(struct class_device *cdev, 355 const char *buf, size_t count) 356{ 357 if (count < 1) 358 return -EINVAL; 359 360 switch (buf[0]) { 361 case '0': 362 do_sas_phy_enable(cdev, count, 0); 363 break; 364 case '1': 365 do_sas_phy_enable(cdev, count, 1); 366 break; 367 default: 368 return -EINVAL; 369 } 370 371 return count; 372} 373 374static ssize_t show_sas_phy_enable(struct class_device *cdev, char *buf) 375{ 376 struct sas_phy *phy = transport_class_to_phy(cdev); 377 378 return snprintf(buf, 20, "%d", phy->enabled); 379} 380 381static CLASS_DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable, 382 store_sas_phy_enable); 383 384static ssize_t do_sas_phy_reset(struct class_device *cdev, 385 size_t count, int hard_reset) 386{ 387 struct sas_phy *phy = transport_class_to_phy(cdev); 388 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 389 struct sas_internal *i = to_sas_internal(shost->transportt); 390 int error; 391 392 error = i->f->phy_reset(phy, hard_reset); 393 if (error) 394 return error; 395 return count; 396}; 397 398static ssize_t store_sas_link_reset(struct class_device *cdev, 399 const char *buf, size_t count) 400{ 401 return do_sas_phy_reset(cdev, count, 0); 402} 403static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset); 404 405static ssize_t store_sas_hard_reset(struct class_device *cdev, 406 const char *buf, size_t count) 407{ 408 return do_sas_phy_reset(cdev, count, 1); 409} 410static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset); 411 412sas_phy_protocol_attr(identify.initiator_port_protocols, 413 initiator_port_protocols); 414sas_phy_protocol_attr(identify.target_port_protocols, 415 target_port_protocols); 416sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n", 417 unsigned long long); 418sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8); 419//sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int); 420sas_phy_linkspeed_attr(negotiated_linkrate); 421sas_phy_linkspeed_attr(minimum_linkrate_hw); 422sas_phy_linkspeed_rw_attr(minimum_linkrate); 423sas_phy_linkspeed_attr(maximum_linkrate_hw); 424sas_phy_linkspeed_rw_attr(maximum_linkrate); 425sas_phy_linkerror_attr(invalid_dword_count); 426sas_phy_linkerror_attr(running_disparity_error_count); 427sas_phy_linkerror_attr(loss_of_dword_sync_count); 428sas_phy_linkerror_attr(phy_reset_problem_count); 429 430 431static DECLARE_TRANSPORT_CLASS(sas_phy_class, 432 "sas_phy", NULL, NULL, NULL); 433 434static int sas_phy_match(struct attribute_container *cont, struct device *dev) 435{ 436 struct Scsi_Host *shost; 437 struct sas_internal *i; 438 439 if (!scsi_is_sas_phy(dev)) 440 return 0; 441 shost = dev_to_shost(dev->parent); 442 443 if (!shost->transportt) 444 return 0; 445 if (shost->transportt->host_attrs.ac.class != 446 &sas_host_class.class) 447 return 0; 448 449 i = to_sas_internal(shost->transportt); 450 return &i->phy_attr_cont.ac == cont; 451} 452 453static void sas_phy_release(struct device *dev) 454{ 455 struct sas_phy *phy = dev_to_phy(dev); 456 457 put_device(dev->parent); 458 kfree(phy); 459} 460 461/** 462 * sas_phy_alloc -- allocates and initialize a SAS PHY structure 463 * @parent: Parent device 464 * @number: Phy index 465 * 466 * Allocates an SAS PHY structure. It will be added in the device tree 467 * below the device specified by @parent, which has to be either a Scsi_Host 468 * or sas_rphy. 469 * 470 * Returns: 471 * SAS PHY allocated or %NULL if the allocation failed. 472 */ 473struct sas_phy *sas_phy_alloc(struct device *parent, int number) 474{ 475 struct Scsi_Host *shost = dev_to_shost(parent); 476 struct sas_phy *phy; 477 478 phy = kzalloc(sizeof(*phy), GFP_KERNEL); 479 if (!phy) 480 return NULL; 481 482 phy->number = number; 483 phy->enabled = 1; 484 485 device_initialize(&phy->dev); 486 phy->dev.parent = get_device(parent); 487 phy->dev.release = sas_phy_release; 488 INIT_LIST_HEAD(&phy->port_siblings); 489 if (scsi_is_sas_expander_device(parent)) { 490 struct sas_rphy *rphy = dev_to_rphy(parent); 491 sprintf(phy->dev.bus_id, "phy-%d:%d:%d", shost->host_no, 492 rphy->scsi_target_id, number); 493 } else 494 sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number); 495 496 transport_setup_device(&phy->dev); 497 498 return phy; 499} 500EXPORT_SYMBOL(sas_phy_alloc); 501 502/** 503 * sas_phy_add -- add a SAS PHY to the device hierarchy 504 * @phy: The PHY to be added 505 * 506 * Publishes a SAS PHY to the rest of the system. 507 */ 508int sas_phy_add(struct sas_phy *phy) 509{ 510 int error; 511 512 error = device_add(&phy->dev); 513 if (!error) { 514 transport_add_device(&phy->dev); 515 transport_configure_device(&phy->dev); 516 } 517 518 return error; 519} 520EXPORT_SYMBOL(sas_phy_add); 521 522/** 523 * sas_phy_free -- free a SAS PHY 524 * @phy: SAS PHY to free 525 * 526 * Frees the specified SAS PHY. 527 * 528 * Note: 529 * This function must only be called on a PHY that has not 530 * sucessfully been added using sas_phy_add(). 531 */ 532void sas_phy_free(struct sas_phy *phy) 533{ 534 transport_destroy_device(&phy->dev); 535 put_device(&phy->dev); 536} 537EXPORT_SYMBOL(sas_phy_free); 538 539/** 540 * sas_phy_delete -- remove SAS PHY 541 * @phy: SAS PHY to remove 542 * 543 * Removes the specified SAS PHY. If the SAS PHY has an 544 * associated remote PHY it is removed before. 545 */ 546void 547sas_phy_delete(struct sas_phy *phy) 548{ 549 struct device *dev = &phy->dev; 550 551 /* this happens if the phy is still part of a port when deleted */ 552 BUG_ON(!list_empty(&phy->port_siblings)); 553 554 transport_remove_device(dev); 555 device_del(dev); 556 transport_destroy_device(dev); 557 put_device(dev); 558} 559EXPORT_SYMBOL(sas_phy_delete); 560 561/** 562 * scsi_is_sas_phy -- check if a struct device represents a SAS PHY 563 * @dev: device to check 564 * 565 * Returns: 566 * %1 if the device represents a SAS PHY, %0 else 567 */ 568int scsi_is_sas_phy(const struct device *dev) 569{ 570 return dev->release == sas_phy_release; 571} 572EXPORT_SYMBOL(scsi_is_sas_phy); 573 574/* 575 * SAS Port attributes 576 */ 577#define sas_port_show_simple(field, name, format_string, cast) \ 578static ssize_t \ 579show_sas_port_##name(struct class_device *cdev, char *buf) \ 580{ \ 581 struct sas_port *port = transport_class_to_sas_port(cdev); \ 582 \ 583 return snprintf(buf, 20, format_string, cast port->field); \ 584} 585 586#define sas_port_simple_attr(field, name, format_string, type) \ 587 sas_port_show_simple(field, name, format_string, (type)) \ 588static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL) 589 590sas_port_simple_attr(num_phys, num_phys, "%d\n", int); 591 592static DECLARE_TRANSPORT_CLASS(sas_port_class, 593 "sas_port", NULL, NULL, NULL); 594 595static int sas_port_match(struct attribute_container *cont, struct device *dev) 596{ 597 struct Scsi_Host *shost; 598 struct sas_internal *i; 599 600 if (!scsi_is_sas_port(dev)) 601 return 0; 602 shost = dev_to_shost(dev->parent); 603 604 if (!shost->transportt) 605 return 0; 606 if (shost->transportt->host_attrs.ac.class != 607 &sas_host_class.class) 608 return 0; 609 610 i = to_sas_internal(shost->transportt); 611 return &i->port_attr_cont.ac == cont; 612} 613 614 615static void sas_port_release(struct device *dev) 616{ 617 struct sas_port *port = dev_to_sas_port(dev); 618 619 BUG_ON(!list_empty(&port->phy_list)); 620 621 put_device(dev->parent); 622 kfree(port); 623} 624 625static void sas_port_create_link(struct sas_port *port, 626 struct sas_phy *phy) 627{ 628 int res; 629 630 res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj, 631 phy->dev.bus_id); 632 if (res) 633 goto err; 634 res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port"); 635 if (res) 636 goto err; 637 return; 638err: 639 printk(KERN_ERR "%s: Cannot create port links, err=%d\n", 640 __FUNCTION__, res); 641} 642 643static void sas_port_delete_link(struct sas_port *port, 644 struct sas_phy *phy) 645{ 646 sysfs_remove_link(&port->dev.kobj, phy->dev.bus_id); 647 sysfs_remove_link(&phy->dev.kobj, "port"); 648} 649 650/** sas_port_alloc - allocate and initialize a SAS port structure 651 * 652 * @parent: parent device 653 * @port_id: port number 654 * 655 * Allocates a SAS port structure. It will be added to the device tree 656 * below the device specified by @parent which must be either a Scsi_Host 657 * or a sas_expander_device. 658 * 659 * Returns %NULL on error 660 */ 661struct sas_port *sas_port_alloc(struct device *parent, int port_id) 662{ 663 struct Scsi_Host *shost = dev_to_shost(parent); 664 struct sas_port *port; 665 666 port = kzalloc(sizeof(*port), GFP_KERNEL); 667 if (!port) 668 return NULL; 669 670 port->port_identifier = port_id; 671 672 device_initialize(&port->dev); 673 674 port->dev.parent = get_device(parent); 675 port->dev.release = sas_port_release; 676 677 mutex_init(&port->phy_list_mutex); 678 INIT_LIST_HEAD(&port->phy_list); 679 680 if (scsi_is_sas_expander_device(parent)) { 681 struct sas_rphy *rphy = dev_to_rphy(parent); 682 sprintf(port->dev.bus_id, "port-%d:%d:%d", shost->host_no, 683 rphy->scsi_target_id, port->port_identifier); 684 } else 685 sprintf(port->dev.bus_id, "port-%d:%d", shost->host_no, 686 port->port_identifier); 687 688 transport_setup_device(&port->dev); 689 690 return port; 691} 692EXPORT_SYMBOL(sas_port_alloc); 693 694/** sas_port_alloc_num - allocate and initialize a SAS port structure 695 * 696 * @parent: parent device 697 * 698 * Allocates a SAS port structure and a number to go with it. This 699 * interface is really for adapters where the port number has no 700 * meansing, so the sas class should manage them. It will be added to 701 * the device tree below the device specified by @parent which must be 702 * either a Scsi_Host or a sas_expander_device. 703 * 704 * Returns %NULL on error 705 */ 706struct sas_port *sas_port_alloc_num(struct device *parent) 707{ 708 int index; 709 struct Scsi_Host *shost = dev_to_shost(parent); 710 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); 711 712 mutex_lock(&sas_host->lock); 713 if (scsi_is_sas_expander_device(parent)) { 714 struct sas_rphy *rphy = dev_to_rphy(parent); 715 struct sas_expander_device *exp = rphy_to_expander_device(rphy); 716 717 index = exp->next_port_id++; 718 } else 719 index = sas_host->next_port_id++; 720 mutex_unlock(&sas_host->lock); 721 return sas_port_alloc(parent, index); 722} 723EXPORT_SYMBOL(sas_port_alloc_num); 724 725/** 726 * sas_port_add - add a SAS port to the device hierarchy 727 * 728 * @port: port to be added 729 * 730 * publishes a port to the rest of the system 731 */ 732int sas_port_add(struct sas_port *port) 733{ 734 int error; 735 736 /* No phys should be added until this is made visible */ 737 BUG_ON(!list_empty(&port->phy_list)); 738 739 error = device_add(&port->dev); 740 741 if (error) 742 return error; 743 744 transport_add_device(&port->dev); 745 transport_configure_device(&port->dev); 746 747 return 0; 748} 749EXPORT_SYMBOL(sas_port_add); 750 751/** 752 * sas_port_free -- free a SAS PORT 753 * @port: SAS PORT to free 754 * 755 * Frees the specified SAS PORT. 756 * 757 * Note: 758 * This function must only be called on a PORT that has not 759 * sucessfully been added using sas_port_add(). 760 */ 761void sas_port_free(struct sas_port *port) 762{ 763 transport_destroy_device(&port->dev); 764 put_device(&port->dev); 765} 766EXPORT_SYMBOL(sas_port_free); 767 768/** 769 * sas_port_delete -- remove SAS PORT 770 * @port: SAS PORT to remove 771 * 772 * Removes the specified SAS PORT. If the SAS PORT has an 773 * associated phys, unlink them from the port as well. 774 */ 775void sas_port_delete(struct sas_port *port) 776{ 777 struct device *dev = &port->dev; 778 struct sas_phy *phy, *tmp_phy; 779 780 if (port->rphy) { 781 sas_rphy_delete(port->rphy); 782 port->rphy = NULL; 783 } 784 785 mutex_lock(&port->phy_list_mutex); 786 list_for_each_entry_safe(phy, tmp_phy, &port->phy_list, 787 port_siblings) { 788 sas_port_delete_link(port, phy); 789 list_del_init(&phy->port_siblings); 790 } 791 mutex_unlock(&port->phy_list_mutex); 792 793 if (port->is_backlink) { 794 struct device *parent = port->dev.parent; 795 796 sysfs_remove_link(&port->dev.kobj, parent->bus_id); 797 port->is_backlink = 0; 798 } 799 800 transport_remove_device(dev); 801 device_del(dev); 802 transport_destroy_device(dev); 803 put_device(dev); 804} 805EXPORT_SYMBOL(sas_port_delete); 806 807/** 808 * scsi_is_sas_port -- check if a struct device represents a SAS port 809 * @dev: device to check 810 * 811 * Returns: 812 * %1 if the device represents a SAS Port, %0 else 813 */ 814int scsi_is_sas_port(const struct device *dev) 815{ 816 return dev->release == sas_port_release; 817} 818EXPORT_SYMBOL(scsi_is_sas_port); 819 820/** 821 * sas_port_add_phy - add another phy to a port to form a wide port 822 * @port: port to add the phy to 823 * @phy: phy to add 824 * 825 * When a port is initially created, it is empty (has no phys). All 826 * ports must have at least one phy to operated, and all wide ports 827 * must have at least two. The current code makes no difference 828 * between ports and wide ports, but the only object that can be 829 * connected to a remote device is a port, so ports must be formed on 830 * all devices with phys if they're connected to anything. 831 */ 832void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy) 833{ 834 mutex_lock(&port->phy_list_mutex); 835 if (unlikely(!list_empty(&phy->port_siblings))) { 836 /* make sure we're already on this port */ 837 struct sas_phy *tmp; 838 839 list_for_each_entry(tmp, &port->phy_list, port_siblings) 840 if (tmp == phy) 841 break; 842 /* If this trips, you added a phy that was already 843 * part of a different port */ 844 if (unlikely(tmp != phy)) { 845 dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n", phy->dev.bus_id); 846 BUG(); 847 } 848 } else { 849 sas_port_create_link(port, phy); 850 list_add_tail(&phy->port_siblings, &port->phy_list); 851 port->num_phys++; 852 } 853 mutex_unlock(&port->phy_list_mutex); 854} 855EXPORT_SYMBOL(sas_port_add_phy); 856 857/** 858 * sas_port_delete_phy - remove a phy from a port or wide port 859 * @port: port to remove the phy from 860 * @phy: phy to remove 861 * 862 * This operation is used for tearing down ports again. It must be 863 * done to every port or wide port before calling sas_port_delete. 864 */ 865void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy) 866{ 867 mutex_lock(&port->phy_list_mutex); 868 sas_port_delete_link(port, phy); 869 list_del_init(&phy->port_siblings); 870 port->num_phys--; 871 mutex_unlock(&port->phy_list_mutex); 872} 873EXPORT_SYMBOL(sas_port_delete_phy); 874 875void sas_port_mark_backlink(struct sas_port *port) 876{ 877 int res; 878 struct device *parent = port->dev.parent->parent->parent; 879 880 if (port->is_backlink) 881 return; 882 port->is_backlink = 1; 883 res = sysfs_create_link(&port->dev.kobj, &parent->kobj, 884 parent->bus_id); 885 if (res) 886 goto err; 887 return; 888err: 889 printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n", 890 __FUNCTION__, res); 891 892} 893EXPORT_SYMBOL(sas_port_mark_backlink); 894 895/* 896 * SAS remote PHY attributes. 897 */ 898 899#define sas_rphy_show_simple(field, name, format_string, cast) \ 900static ssize_t \ 901show_sas_rphy_##name(struct class_device *cdev, char *buf) \ 902{ \ 903 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \ 904 \ 905 return snprintf(buf, 20, format_string, cast rphy->field); \ 906} 907 908#define sas_rphy_simple_attr(field, name, format_string, type) \ 909 sas_rphy_show_simple(field, name, format_string, (type)) \ 910static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \ 911 show_sas_rphy_##name, NULL) 912 913#define sas_rphy_show_protocol(field, name) \ 914static ssize_t \ 915show_sas_rphy_##name(struct class_device *cdev, char *buf) \ 916{ \ 917 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \ 918 \ 919 if (!rphy->field) \ 920 return snprintf(buf, 20, "none\n"); \ 921 return get_sas_protocol_names(rphy->field, buf); \ 922} 923 924#define sas_rphy_protocol_attr(field, name) \ 925 sas_rphy_show_protocol(field, name) \ 926static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \ 927 show_sas_rphy_##name, NULL) 928 929static ssize_t 930show_sas_rphy_device_type(struct class_device *cdev, char *buf) 931{ 932 struct sas_rphy *rphy = transport_class_to_rphy(cdev); 933 934 if (!rphy->identify.device_type) 935 return snprintf(buf, 20, "none\n"); 936 return get_sas_device_type_names( 937 rphy->identify.device_type, buf); 938} 939 940static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO, 941 show_sas_rphy_device_type, NULL); 942 943static ssize_t 944show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf) 945{ 946 struct sas_rphy *rphy = transport_class_to_rphy(cdev); 947 struct sas_phy *phy = dev_to_phy(rphy->dev.parent); 948 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 949 struct sas_internal *i = to_sas_internal(shost->transportt); 950 u64 identifier; 951 int error; 952 953 /* 954 * Only devices behind an expander are supported, because the 955 * enclosure identifier is a SMP feature. 956 */ 957 if (scsi_is_sas_phy_local(phy)) 958 return -EINVAL; 959 960 error = i->f->get_enclosure_identifier(rphy, &identifier); 961 if (error) 962 return error; 963 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier); 964} 965 966static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO, 967 show_sas_rphy_enclosure_identifier, NULL); 968 969static ssize_t 970show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf) 971{ 972 struct sas_rphy *rphy = transport_class_to_rphy(cdev); 973 struct sas_phy *phy = dev_to_phy(rphy->dev.parent); 974 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 975 struct sas_internal *i = to_sas_internal(shost->transportt); 976 int val; 977 978 if (scsi_is_sas_phy_local(phy)) 979 return -EINVAL; 980 981 val = i->f->get_bay_identifier(rphy); 982 if (val < 0) 983 return val; 984 return sprintf(buf, "%d\n", val); 985} 986 987static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO, 988 show_sas_rphy_bay_identifier, NULL); 989 990sas_rphy_protocol_attr(identify.initiator_port_protocols, 991 initiator_port_protocols); 992sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols); 993sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n", 994 unsigned long long); 995sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8); 996 997/* only need 8 bytes of data plus header (4 or 8) */ 998#define BUF_SIZE 64 999 1000int sas_read_port_mode_page(struct scsi_device *sdev) 1001{ 1002 char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata; 1003 struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target); 1004 struct sas_end_device *rdev; 1005 struct scsi_mode_data mode_data; 1006 int res, error; 1007 1008 BUG_ON(rphy->identify.device_type != SAS_END_DEVICE); 1009 1010 rdev = rphy_to_end_device(rphy); 1011 1012 if (!buffer) 1013 return -ENOMEM; 1014 1015 res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3, 1016 &mode_data, NULL); 1017 1018 error = -EINVAL; 1019 if (!scsi_status_is_good(res)) 1020 goto out; 1021 1022 msdata = buffer + mode_data.header_length + 1023 mode_data.block_descriptor_length; 1024 1025 if (msdata - buffer > BUF_SIZE - 8) 1026 goto out; 1027 1028 error = 0; 1029 1030 rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0; 1031 rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5]; 1032 rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7]; 1033 1034 out: 1035 kfree(buffer); 1036 return error; 1037} 1038EXPORT_SYMBOL(sas_read_port_mode_page); 1039 1040static DECLARE_TRANSPORT_CLASS(sas_end_dev_class, 1041 "sas_end_device", NULL, NULL, NULL); 1042 1043#define sas_end_dev_show_simple(field, name, format_string, cast) \ 1044static ssize_t \ 1045show_sas_end_dev_##name(struct class_device *cdev, char *buf) \ 1046{ \ 1047 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \ 1048 struct sas_end_device *rdev = rphy_to_end_device(rphy); \ 1049 \ 1050 return snprintf(buf, 20, format_string, cast rdev->field); \ 1051} 1052 1053#define sas_end_dev_simple_attr(field, name, format_string, type) \ 1054 sas_end_dev_show_simple(field, name, format_string, (type)) \ 1055static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \ 1056 show_sas_end_dev_##name, NULL) 1057 1058sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int); 1059sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout, 1060 "%d\n", int); 1061sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout, 1062 "%d\n", int); 1063 1064static DECLARE_TRANSPORT_CLASS(sas_expander_class, 1065 "sas_expander", NULL, NULL, NULL); 1066 1067#define sas_expander_show_simple(field, name, format_string, cast) \ 1068static ssize_t \ 1069show_sas_expander_##name(struct class_device *cdev, char *buf) \ 1070{ \ 1071 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \ 1072 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \ 1073 \ 1074 return snprintf(buf, 20, format_string, cast edev->field); \ 1075} 1076 1077#define sas_expander_simple_attr(field, name, format_string, type) \ 1078 sas_expander_show_simple(field, name, format_string, (type)) \ 1079static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO, \ 1080 show_sas_expander_##name, NULL) 1081 1082sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *); 1083sas_expander_simple_attr(product_id, product_id, "%s\n", char *); 1084sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *); 1085sas_expander_simple_attr(component_vendor_id, component_vendor_id, 1086 "%s\n", char *); 1087sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int); 1088sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n", 1089 unsigned int); 1090sas_expander_simple_attr(level, level, "%d\n", int); 1091 1092static DECLARE_TRANSPORT_CLASS(sas_rphy_class, 1093 "sas_device", NULL, NULL, NULL); 1094 1095static int sas_rphy_match(struct attribute_container *cont, struct device *dev) 1096{ 1097 struct Scsi_Host *shost; 1098 struct sas_internal *i; 1099 1100 if (!scsi_is_sas_rphy(dev)) 1101 return 0; 1102 shost = dev_to_shost(dev->parent->parent); 1103 1104 if (!shost->transportt) 1105 return 0; 1106 if (shost->transportt->host_attrs.ac.class != 1107 &sas_host_class.class) 1108 return 0; 1109 1110 i = to_sas_internal(shost->transportt); 1111 return &i->rphy_attr_cont.ac == cont; 1112} 1113 1114static int sas_end_dev_match(struct attribute_container *cont, 1115 struct device *dev) 1116{ 1117 struct Scsi_Host *shost; 1118 struct sas_internal *i; 1119 struct sas_rphy *rphy; 1120 1121 if (!scsi_is_sas_rphy(dev)) 1122 return 0; 1123 shost = dev_to_shost(dev->parent->parent); 1124 rphy = dev_to_rphy(dev); 1125 1126 if (!shost->transportt) 1127 return 0; 1128 if (shost->transportt->host_attrs.ac.class != 1129 &sas_host_class.class) 1130 return 0; 1131 1132 i = to_sas_internal(shost->transportt); 1133 return &i->end_dev_attr_cont.ac == cont && 1134 rphy->identify.device_type == SAS_END_DEVICE; 1135} 1136 1137static int sas_expander_match(struct attribute_container *cont, 1138 struct device *dev) 1139{ 1140 struct Scsi_Host *shost; 1141 struct sas_internal *i; 1142 struct sas_rphy *rphy; 1143 1144 if (!scsi_is_sas_rphy(dev)) 1145 return 0; 1146 shost = dev_to_shost(dev->parent->parent); 1147 rphy = dev_to_rphy(dev); 1148 1149 if (!shost->transportt) 1150 return 0; 1151 if (shost->transportt->host_attrs.ac.class != 1152 &sas_host_class.class) 1153 return 0; 1154 1155 i = to_sas_internal(shost->transportt); 1156 return &i->expander_attr_cont.ac == cont && 1157 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE || 1158 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE); 1159} 1160 1161static void sas_expander_release(struct device *dev) 1162{ 1163 struct sas_rphy *rphy = dev_to_rphy(dev); 1164 struct sas_expander_device *edev = rphy_to_expander_device(rphy); 1165 1166 put_device(dev->parent); 1167 kfree(edev); 1168} 1169 1170static void sas_end_device_release(struct device *dev) 1171{ 1172 struct sas_rphy *rphy = dev_to_rphy(dev); 1173 struct sas_end_device *edev = rphy_to_end_device(rphy); 1174 1175 put_device(dev->parent); 1176 kfree(edev); 1177} 1178 1179/** 1180 * sas_rphy_initialize - common rphy intialization 1181 * @rphy: rphy to initialise 1182 * 1183 * Used by both sas_end_device_alloc() and sas_expander_alloc() to 1184 * initialise the common rphy component of each. 1185 */ 1186static void sas_rphy_initialize(struct sas_rphy *rphy) 1187{ 1188 INIT_LIST_HEAD(&rphy->list); 1189} 1190 1191/** 1192 * sas_end_device_alloc - allocate an rphy for an end device 1193 * 1194 * Allocates an SAS remote PHY structure, connected to @parent. 1195 * 1196 * Returns: 1197 * SAS PHY allocated or %NULL if the allocation failed. 1198 */ 1199struct sas_rphy *sas_end_device_alloc(struct sas_port *parent) 1200{ 1201 struct Scsi_Host *shost = dev_to_shost(&parent->dev); 1202 struct sas_end_device *rdev; 1203 1204 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); 1205 if (!rdev) { 1206 return NULL; 1207 } 1208 1209 device_initialize(&rdev->rphy.dev); 1210 rdev->rphy.dev.parent = get_device(&parent->dev); 1211 rdev->rphy.dev.release = sas_end_device_release; 1212 if (scsi_is_sas_expander_device(parent->dev.parent)) { 1213 struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent); 1214 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d:%d", 1215 shost->host_no, rphy->scsi_target_id, parent->port_identifier); 1216 } else 1217 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d", 1218 shost->host_no, parent->port_identifier); 1219 rdev->rphy.identify.device_type = SAS_END_DEVICE; 1220 sas_rphy_initialize(&rdev->rphy); 1221 transport_setup_device(&rdev->rphy.dev); 1222 1223 return &rdev->rphy; 1224} 1225EXPORT_SYMBOL(sas_end_device_alloc); 1226 1227/** 1228 * sas_expander_alloc - allocate an rphy for an end device 1229 * 1230 * Allocates an SAS remote PHY structure, connected to @parent. 1231 * 1232 * Returns: 1233 * SAS PHY allocated or %NULL if the allocation failed. 1234 */ 1235struct sas_rphy *sas_expander_alloc(struct sas_port *parent, 1236 enum sas_device_type type) 1237{ 1238 struct Scsi_Host *shost = dev_to_shost(&parent->dev); 1239 struct sas_expander_device *rdev; 1240 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); 1241 1242 BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE && 1243 type != SAS_FANOUT_EXPANDER_DEVICE); 1244 1245 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); 1246 if (!rdev) { 1247 return NULL; 1248 } 1249 1250 device_initialize(&rdev->rphy.dev); 1251 rdev->rphy.dev.parent = get_device(&parent->dev); 1252 rdev->rphy.dev.release = sas_expander_release; 1253 mutex_lock(&sas_host->lock); 1254 rdev->rphy.scsi_target_id = sas_host->next_expander_id++; 1255 mutex_unlock(&sas_host->lock); 1256 sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d", 1257 shost->host_no, rdev->rphy.scsi_target_id); 1258 rdev->rphy.identify.device_type = type; 1259 sas_rphy_initialize(&rdev->rphy); 1260 transport_setup_device(&rdev->rphy.dev); 1261 1262 return &rdev->rphy; 1263} 1264EXPORT_SYMBOL(sas_expander_alloc); 1265 1266/** 1267 * sas_rphy_add -- add a SAS remote PHY to the device hierarchy 1268 * @rphy: The remote PHY to be added 1269 * 1270 * Publishes a SAS remote PHY to the rest of the system. 1271 */ 1272int sas_rphy_add(struct sas_rphy *rphy) 1273{ 1274 struct sas_port *parent = dev_to_sas_port(rphy->dev.parent); 1275 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent); 1276 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); 1277 struct sas_identify *identify = &rphy->identify; 1278 int error; 1279 1280 if (parent->rphy) 1281 return -ENXIO; 1282 parent->rphy = rphy; 1283 1284 error = device_add(&rphy->dev); 1285 if (error) 1286 return error; 1287 transport_add_device(&rphy->dev); 1288 transport_configure_device(&rphy->dev); 1289 1290 mutex_lock(&sas_host->lock); 1291 list_add_tail(&rphy->list, &sas_host->rphy_list); 1292 if (identify->device_type == SAS_END_DEVICE && 1293 (identify->target_port_protocols & 1294 (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA))) 1295 rphy->scsi_target_id = sas_host->next_target_id++; 1296 else if (identify->device_type == SAS_END_DEVICE) 1297 rphy->scsi_target_id = -1; 1298 mutex_unlock(&sas_host->lock); 1299 1300 if (identify->device_type == SAS_END_DEVICE && 1301 rphy->scsi_target_id != -1) { 1302 scsi_scan_target(&rphy->dev, 0, 1303 rphy->scsi_target_id, SCAN_WILD_CARD, 0); 1304 } 1305 1306 return 0; 1307} 1308EXPORT_SYMBOL(sas_rphy_add); 1309 1310/** 1311 * sas_rphy_free -- free a SAS remote PHY 1312 * @rphy SAS remote PHY to free 1313 * 1314 * Frees the specified SAS remote PHY. 1315 * 1316 * Note: 1317 * This function must only be called on a remote 1318 * PHY that has not sucessfully been added using 1319 * sas_rphy_add() (or has been sas_rphy_remove()'d) 1320 */ 1321void sas_rphy_free(struct sas_rphy *rphy) 1322{ 1323 struct device *dev = &rphy->dev; 1324 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent); 1325 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); 1326 1327 mutex_lock(&sas_host->lock); 1328 list_del(&rphy->list); 1329 mutex_unlock(&sas_host->lock); 1330 1331 transport_destroy_device(dev); 1332 1333 put_device(dev); 1334} 1335EXPORT_SYMBOL(sas_rphy_free); 1336 1337/** 1338 * sas_rphy_delete -- remove and free SAS remote PHY 1339 * @rphy: SAS remote PHY to remove and free 1340 * 1341 * Removes the specified SAS remote PHY and frees it. 1342 */ 1343void 1344sas_rphy_delete(struct sas_rphy *rphy) 1345{ 1346 sas_rphy_remove(rphy); 1347 sas_rphy_free(rphy); 1348} 1349EXPORT_SYMBOL(sas_rphy_delete); 1350 1351/** 1352 * sas_rphy_remove -- remove SAS remote PHY 1353 * @rphy: SAS remote phy to remove 1354 * 1355 * Removes the specified SAS remote PHY. 1356 */ 1357void 1358sas_rphy_remove(struct sas_rphy *rphy) 1359{ 1360 struct device *dev = &rphy->dev; 1361 struct sas_port *parent = dev_to_sas_port(dev->parent); 1362 1363 switch (rphy->identify.device_type) { 1364 case SAS_END_DEVICE: 1365 scsi_remove_target(dev); 1366 break; 1367 case SAS_EDGE_EXPANDER_DEVICE: 1368 case SAS_FANOUT_EXPANDER_DEVICE: 1369 sas_remove_children(dev); 1370 break; 1371 default: 1372 break; 1373 } 1374 1375 transport_remove_device(dev); 1376 device_del(dev); 1377 1378 parent->rphy = NULL; 1379} 1380EXPORT_SYMBOL(sas_rphy_remove); 1381 1382/** 1383 * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY 1384 * @dev: device to check 1385 * 1386 * Returns: 1387 * %1 if the device represents a SAS remote PHY, %0 else 1388 */ 1389int scsi_is_sas_rphy(const struct device *dev) 1390{ 1391 return dev->release == sas_end_device_release || 1392 dev->release == sas_expander_release; 1393} 1394EXPORT_SYMBOL(scsi_is_sas_rphy); 1395 1396 1397/* 1398 * SCSI scan helper 1399 */ 1400 1401static int sas_user_scan(struct Scsi_Host *shost, uint channel, 1402 uint id, uint lun) 1403{ 1404 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); 1405 struct sas_rphy *rphy; 1406 1407 mutex_lock(&sas_host->lock); 1408 list_for_each_entry(rphy, &sas_host->rphy_list, list) { 1409 if (rphy->identify.device_type != SAS_END_DEVICE || 1410 rphy->scsi_target_id == -1) 1411 continue; 1412 1413 if ((channel == SCAN_WILD_CARD || channel == 0) && 1414 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) { 1415 scsi_scan_target(&rphy->dev, 0, 1416 rphy->scsi_target_id, lun, 1); 1417 } 1418 } 1419 mutex_unlock(&sas_host->lock); 1420 1421 return 0; 1422} 1423 1424 1425/* 1426 * Setup / Teardown code 1427 */ 1428 1429#define SETUP_TEMPLATE(attrb, field, perm, test) \ 1430 i->private_##attrb[count] = class_device_attr_##field; \ 1431 i->private_##attrb[count].attr.mode = perm; \ 1432 i->attrb[count] = &i->private_##attrb[count]; \ 1433 if (test) \ 1434 count++ 1435 1436#define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm) \ 1437 i->private_##attrb[count] = class_device_attr_##field; \ 1438 i->private_##attrb[count].attr.mode = perm; \ 1439 if (ro_test) { \ 1440 i->private_##attrb[count].attr.mode = ro_perm; \ 1441 i->private_##attrb[count].store = NULL; \ 1442 } \ 1443 i->attrb[count] = &i->private_##attrb[count]; \ 1444 if (test) \ 1445 count++ 1446 1447#define SETUP_RPORT_ATTRIBUTE(field) \ 1448 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1) 1449 1450#define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \ 1451 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func) 1452 1453#define SETUP_PHY_ATTRIBUTE(field) \ 1454 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1) 1455 1456#define SETUP_PHY_ATTRIBUTE_RW(field) \ 1457 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \ 1458 !i->f->set_phy_speed, S_IRUGO) 1459 1460#define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func) \ 1461 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \ 1462 !i->f->func, S_IRUGO) 1463 1464#define SETUP_PORT_ATTRIBUTE(field) \ 1465 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1) 1466 1467#define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \ 1468 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func) 1469 1470#define SETUP_PHY_ATTRIBUTE_WRONLY(field) \ 1471 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1) 1472 1473#define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \ 1474 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func) 1475 1476#define SETUP_END_DEV_ATTRIBUTE(field) \ 1477 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1) 1478 1479#define SETUP_EXPANDER_ATTRIBUTE(field) \ 1480 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1) 1481 1482/** 1483 * sas_attach_transport -- instantiate SAS transport template 1484 * @ft: SAS transport class function template 1485 */ 1486struct scsi_transport_template * 1487sas_attach_transport(struct sas_function_template *ft) 1488{ 1489 struct sas_internal *i; 1490 int count; 1491 1492 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL); 1493 if (!i) 1494 return NULL; 1495 1496 i->t.user_scan = sas_user_scan; 1497 1498 i->t.host_attrs.ac.attrs = &i->host_attrs[0]; 1499 i->t.host_attrs.ac.class = &sas_host_class.class; 1500 i->t.host_attrs.ac.match = sas_host_match; 1501 transport_container_register(&i->t.host_attrs); 1502 i->t.host_size = sizeof(struct sas_host_attrs); 1503 1504 i->phy_attr_cont.ac.class = &sas_phy_class.class; 1505 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0]; 1506 i->phy_attr_cont.ac.match = sas_phy_match; 1507 transport_container_register(&i->phy_attr_cont); 1508 1509 i->port_attr_cont.ac.class = &sas_port_class.class; 1510 i->port_attr_cont.ac.attrs = &i->port_attrs[0]; 1511 i->port_attr_cont.ac.match = sas_port_match; 1512 transport_container_register(&i->port_attr_cont); 1513 1514 i->rphy_attr_cont.ac.class = &sas_rphy_class.class; 1515 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0]; 1516 i->rphy_attr_cont.ac.match = sas_rphy_match; 1517 transport_container_register(&i->rphy_attr_cont); 1518 1519 i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class; 1520 i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0]; 1521 i->end_dev_attr_cont.ac.match = sas_end_dev_match; 1522 transport_container_register(&i->end_dev_attr_cont); 1523 1524 i->expander_attr_cont.ac.class = &sas_expander_class.class; 1525 i->expander_attr_cont.ac.attrs = &i->expander_attrs[0]; 1526 i->expander_attr_cont.ac.match = sas_expander_match; 1527 transport_container_register(&i->expander_attr_cont); 1528 1529 i->f = ft; 1530 1531 count = 0; 1532 SETUP_PORT_ATTRIBUTE(num_phys); 1533 i->host_attrs[count] = NULL; 1534 1535 count = 0; 1536 SETUP_PHY_ATTRIBUTE(initiator_port_protocols); 1537 SETUP_PHY_ATTRIBUTE(target_port_protocols); 1538 SETUP_PHY_ATTRIBUTE(device_type); 1539 SETUP_PHY_ATTRIBUTE(sas_address); 1540 SETUP_PHY_ATTRIBUTE(phy_identifier); 1541 //SETUP_PHY_ATTRIBUTE(port_identifier); 1542 SETUP_PHY_ATTRIBUTE(negotiated_linkrate); 1543 SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw); 1544 SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate); 1545 SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw); 1546 SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate); 1547 1548 SETUP_PHY_ATTRIBUTE(invalid_dword_count); 1549 SETUP_PHY_ATTRIBUTE(running_disparity_error_count); 1550 SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count); 1551 SETUP_PHY_ATTRIBUTE(phy_reset_problem_count); 1552 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset); 1553 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset); 1554 SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable); 1555 i->phy_attrs[count] = NULL; 1556 1557 count = 0; 1558 SETUP_PORT_ATTRIBUTE(num_phys); 1559 i->port_attrs[count] = NULL; 1560 1561 count = 0; 1562 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols); 1563 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols); 1564 SETUP_RPORT_ATTRIBUTE(rphy_device_type); 1565 SETUP_RPORT_ATTRIBUTE(rphy_sas_address); 1566 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier); 1567 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier, 1568 get_enclosure_identifier); 1569 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier, 1570 get_bay_identifier); 1571 i->rphy_attrs[count] = NULL; 1572 1573 count = 0; 1574 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning); 1575 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout); 1576 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout); 1577 i->end_dev_attrs[count] = NULL; 1578 1579 count = 0; 1580 SETUP_EXPANDER_ATTRIBUTE(vendor_id); 1581 SETUP_EXPANDER_ATTRIBUTE(product_id); 1582 SETUP_EXPANDER_ATTRIBUTE(product_rev); 1583 SETUP_EXPANDER_ATTRIBUTE(component_vendor_id); 1584 SETUP_EXPANDER_ATTRIBUTE(component_id); 1585 SETUP_EXPANDER_ATTRIBUTE(component_revision_id); 1586 SETUP_EXPANDER_ATTRIBUTE(level); 1587 i->expander_attrs[count] = NULL; 1588 1589 return &i->t; 1590} 1591EXPORT_SYMBOL(sas_attach_transport); 1592 1593/** 1594 * sas_release_transport -- release SAS transport template instance 1595 * @t: transport template instance 1596 */ 1597void sas_release_transport(struct scsi_transport_template *t) 1598{ 1599 struct sas_internal *i = to_sas_internal(t); 1600 1601 transport_container_unregister(&i->t.host_attrs); 1602 transport_container_unregister(&i->phy_attr_cont); 1603 transport_container_unregister(&i->port_attr_cont); 1604 transport_container_unregister(&i->rphy_attr_cont); 1605 transport_container_unregister(&i->end_dev_attr_cont); 1606 transport_container_unregister(&i->expander_attr_cont); 1607 1608 kfree(i); 1609} 1610EXPORT_SYMBOL(sas_release_transport); 1611 1612static __init int sas_transport_init(void) 1613{ 1614 int error; 1615 1616 error = transport_class_register(&sas_host_class); 1617 if (error) 1618 goto out; 1619 error = transport_class_register(&sas_phy_class); 1620 if (error) 1621 goto out_unregister_transport; 1622 error = transport_class_register(&sas_port_class); 1623 if (error) 1624 goto out_unregister_phy; 1625 error = transport_class_register(&sas_rphy_class); 1626 if (error) 1627 goto out_unregister_port; 1628 error = transport_class_register(&sas_end_dev_class); 1629 if (error) 1630 goto out_unregister_rphy; 1631 error = transport_class_register(&sas_expander_class); 1632 if (error) 1633 goto out_unregister_end_dev; 1634 1635 return 0; 1636 1637 out_unregister_end_dev: 1638 transport_class_unregister(&sas_end_dev_class); 1639 out_unregister_rphy: 1640 transport_class_unregister(&sas_rphy_class); 1641 out_unregister_port: 1642 transport_class_unregister(&sas_port_class); 1643 out_unregister_phy: 1644 transport_class_unregister(&sas_phy_class); 1645 out_unregister_transport: 1646 transport_class_unregister(&sas_host_class); 1647 out: 1648 return error; 1649 1650} 1651 1652static void __exit sas_transport_exit(void) 1653{ 1654 transport_class_unregister(&sas_host_class); 1655 transport_class_unregister(&sas_phy_class); 1656 transport_class_unregister(&sas_port_class); 1657 transport_class_unregister(&sas_rphy_class); 1658 transport_class_unregister(&sas_end_dev_class); 1659 transport_class_unregister(&sas_expander_class); 1660} 1661 1662MODULE_AUTHOR("Christoph Hellwig"); 1663MODULE_DESCRIPTION("SAS Transport Attributes"); 1664MODULE_LICENSE("GPL"); 1665 1666module_init(sas_transport_init); 1667module_exit(sas_transport_exit); 1668