Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 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 | |
| 30 | #include <scsi/scsi_device.h> |
| 31 | #include <scsi/scsi_host.h> |
| 32 | #include <scsi/scsi_transport.h> |
| 33 | #include <scsi/scsi_transport_sas.h> |
| 34 | |
| 35 | |
| 36 | #define SAS_HOST_ATTRS 0 |
Christoph Hellwig | c3ee74c | 2005-09-19 21:59:42 +0200 | [diff] [blame^] | 37 | #define SAS_PORT_ATTRS 15 |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 38 | #define SAS_RPORT_ATTRS 5 |
| 39 | |
| 40 | struct sas_internal { |
| 41 | struct scsi_transport_template t; |
| 42 | struct sas_function_template *f; |
| 43 | |
| 44 | struct class_device_attribute private_host_attrs[SAS_HOST_ATTRS]; |
| 45 | struct class_device_attribute private_phy_attrs[SAS_PORT_ATTRS]; |
| 46 | struct class_device_attribute private_rphy_attrs[SAS_RPORT_ATTRS]; |
| 47 | |
| 48 | struct transport_container phy_attr_cont; |
| 49 | struct transport_container rphy_attr_cont; |
| 50 | |
| 51 | /* |
| 52 | * The array of null terminated pointers to attributes |
| 53 | * needed by scsi_sysfs.c |
| 54 | */ |
| 55 | struct class_device_attribute *host_attrs[SAS_HOST_ATTRS + 1]; |
| 56 | struct class_device_attribute *phy_attrs[SAS_PORT_ATTRS + 1]; |
| 57 | struct class_device_attribute *rphy_attrs[SAS_RPORT_ATTRS + 1]; |
| 58 | }; |
| 59 | #define to_sas_internal(tmpl) container_of(tmpl, struct sas_internal, t) |
| 60 | |
| 61 | struct sas_host_attrs { |
| 62 | struct list_head rphy_list; |
| 63 | spinlock_t lock; |
| 64 | u32 next_target_id; |
| 65 | }; |
| 66 | #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data) |
| 67 | |
| 68 | |
| 69 | /* |
| 70 | * Hack to allow attributes of the same name in different objects. |
| 71 | */ |
| 72 | #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \ |
| 73 | struct class_device_attribute class_device_attr_##_prefix##_##_name = \ |
| 74 | __ATTR(_name,_mode,_show,_store) |
| 75 | |
| 76 | |
| 77 | /* |
| 78 | * Pretty printing helpers |
| 79 | */ |
| 80 | |
| 81 | #define sas_bitfield_name_match(title, table) \ |
| 82 | static ssize_t \ |
| 83 | get_sas_##title##_names(u32 table_key, char *buf) \ |
| 84 | { \ |
| 85 | char *prefix = ""; \ |
| 86 | ssize_t len = 0; \ |
| 87 | int i; \ |
| 88 | \ |
| 89 | for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \ |
| 90 | if (table[i].value & table_key) { \ |
| 91 | len += sprintf(buf + len, "%s%s", \ |
| 92 | prefix, table[i].name); \ |
| 93 | prefix = ", "; \ |
| 94 | } \ |
| 95 | } \ |
| 96 | len += sprintf(buf + len, "\n"); \ |
| 97 | return len; \ |
| 98 | } |
| 99 | |
| 100 | #define sas_bitfield_name_search(title, table) \ |
| 101 | static ssize_t \ |
| 102 | get_sas_##title##_names(u32 table_key, char *buf) \ |
| 103 | { \ |
| 104 | ssize_t len = 0; \ |
| 105 | int i; \ |
| 106 | \ |
| 107 | for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \ |
| 108 | if (table[i].value == table_key) { \ |
| 109 | len += sprintf(buf + len, "%s", \ |
| 110 | table[i].name); \ |
| 111 | break; \ |
| 112 | } \ |
| 113 | } \ |
| 114 | len += sprintf(buf + len, "\n"); \ |
| 115 | return len; \ |
| 116 | } |
| 117 | |
| 118 | static struct { |
| 119 | u32 value; |
| 120 | char *name; |
| 121 | } sas_device_type_names[] = { |
| 122 | { SAS_PHY_UNUSED, "unused" }, |
| 123 | { SAS_END_DEVICE, "end device" }, |
| 124 | { SAS_EDGE_EXPANDER_DEVICE, "edge expander" }, |
| 125 | { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" }, |
| 126 | }; |
| 127 | sas_bitfield_name_search(device_type, sas_device_type_names) |
| 128 | |
| 129 | |
| 130 | static struct { |
| 131 | u32 value; |
| 132 | char *name; |
| 133 | } sas_protocol_names[] = { |
| 134 | { SAS_PROTOCOL_SATA, "sata" }, |
| 135 | { SAS_PROTOCOL_SMP, "smp" }, |
| 136 | { SAS_PROTOCOL_STP, "stp" }, |
| 137 | { SAS_PROTOCOL_SSP, "ssp" }, |
| 138 | }; |
| 139 | sas_bitfield_name_match(protocol, sas_protocol_names) |
| 140 | |
| 141 | static struct { |
| 142 | u32 value; |
| 143 | char *name; |
| 144 | } sas_linkspeed_names[] = { |
| 145 | { SAS_LINK_RATE_UNKNOWN, "Unknown" }, |
| 146 | { SAS_PHY_DISABLED, "Phy disabled" }, |
| 147 | { SAS_LINK_RATE_FAILED, "Link Rate failed" }, |
| 148 | { SAS_SATA_SPINUP_HOLD, "Spin-up hold" }, |
| 149 | { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" }, |
| 150 | { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" }, |
| 151 | }; |
| 152 | sas_bitfield_name_search(linkspeed, sas_linkspeed_names) |
| 153 | |
| 154 | |
| 155 | /* |
| 156 | * SAS host attributes |
| 157 | */ |
| 158 | |
James Bottomley | 37be6ee | 2005-09-09 18:38:27 -0500 | [diff] [blame] | 159 | static int sas_host_setup(struct transport_container *tc, struct device *dev, |
| 160 | struct class_device *cdev) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 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 | spin_lock_init(&sas_host->lock); |
| 167 | sas_host->next_target_id = 0; |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static DECLARE_TRANSPORT_CLASS(sas_host_class, |
| 172 | "sas_host", sas_host_setup, NULL, NULL); |
| 173 | |
| 174 | static int sas_host_match(struct attribute_container *cont, |
| 175 | struct device *dev) |
| 176 | { |
| 177 | struct Scsi_Host *shost; |
| 178 | struct sas_internal *i; |
| 179 | |
| 180 | if (!scsi_is_host_device(dev)) |
| 181 | return 0; |
| 182 | shost = dev_to_shost(dev); |
| 183 | |
| 184 | if (!shost->transportt) |
| 185 | return 0; |
| 186 | if (shost->transportt->host_attrs.ac.class != |
| 187 | &sas_host_class.class) |
| 188 | return 0; |
| 189 | |
| 190 | i = to_sas_internal(shost->transportt); |
| 191 | return &i->t.host_attrs.ac == cont; |
| 192 | } |
| 193 | |
| 194 | static int do_sas_phy_delete(struct device *dev, void *data) |
| 195 | { |
| 196 | if (scsi_is_sas_phy(dev)) |
| 197 | sas_phy_delete(dev_to_phy(dev)); |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * sas_remove_host -- tear down a Scsi_Host's SAS data structures |
| 203 | * @shost: Scsi Host that is torn down |
| 204 | * |
| 205 | * Removes all SAS PHYs and remote PHYs for a given Scsi_Host. |
| 206 | * Must be called just before scsi_remove_host for SAS HBAs. |
| 207 | */ |
| 208 | void sas_remove_host(struct Scsi_Host *shost) |
| 209 | { |
| 210 | device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete); |
| 211 | } |
| 212 | EXPORT_SYMBOL(sas_remove_host); |
| 213 | |
| 214 | |
| 215 | /* |
| 216 | * SAS Port attributes |
| 217 | */ |
| 218 | |
| 219 | #define sas_phy_show_simple(field, name, format_string, cast) \ |
| 220 | static ssize_t \ |
| 221 | show_sas_phy_##name(struct class_device *cdev, char *buf) \ |
| 222 | { \ |
| 223 | struct sas_phy *phy = transport_class_to_phy(cdev); \ |
| 224 | \ |
| 225 | return snprintf(buf, 20, format_string, cast phy->field); \ |
| 226 | } |
| 227 | |
| 228 | #define sas_phy_simple_attr(field, name, format_string, type) \ |
| 229 | sas_phy_show_simple(field, name, format_string, (type)) \ |
| 230 | static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL) |
| 231 | |
| 232 | #define sas_phy_show_protocol(field, name) \ |
| 233 | static ssize_t \ |
| 234 | show_sas_phy_##name(struct class_device *cdev, char *buf) \ |
| 235 | { \ |
| 236 | struct sas_phy *phy = transport_class_to_phy(cdev); \ |
| 237 | \ |
| 238 | if (!phy->field) \ |
| 239 | return snprintf(buf, 20, "none\n"); \ |
| 240 | return get_sas_protocol_names(phy->field, buf); \ |
| 241 | } |
| 242 | |
| 243 | #define sas_phy_protocol_attr(field, name) \ |
| 244 | sas_phy_show_protocol(field, name) \ |
| 245 | static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL) |
| 246 | |
| 247 | #define sas_phy_show_linkspeed(field) \ |
| 248 | static ssize_t \ |
| 249 | show_sas_phy_##field(struct class_device *cdev, char *buf) \ |
| 250 | { \ |
| 251 | struct sas_phy *phy = transport_class_to_phy(cdev); \ |
| 252 | \ |
| 253 | return get_sas_linkspeed_names(phy->field, buf); \ |
| 254 | } |
| 255 | |
| 256 | #define sas_phy_linkspeed_attr(field) \ |
| 257 | sas_phy_show_linkspeed(field) \ |
| 258 | static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL) |
| 259 | |
Christoph Hellwig | c3ee74c | 2005-09-19 21:59:42 +0200 | [diff] [blame^] | 260 | #define sas_phy_show_linkerror(field) \ |
| 261 | static ssize_t \ |
| 262 | show_sas_phy_##field(struct class_device *cdev, char *buf) \ |
| 263 | { \ |
| 264 | struct sas_phy *phy = transport_class_to_phy(cdev); \ |
| 265 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \ |
| 266 | struct sas_internal *i = to_sas_internal(shost->transportt); \ |
| 267 | int error; \ |
| 268 | \ |
| 269 | error = i->f->get_linkerrors(phy); \ |
| 270 | if (error) \ |
| 271 | return error; \ |
| 272 | return snprintf(buf, 20, "%u\n", phy->field); \ |
| 273 | } |
| 274 | |
| 275 | #define sas_phy_linkerror_attr(field) \ |
| 276 | sas_phy_show_linkerror(field) \ |
| 277 | static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL) |
| 278 | |
| 279 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 280 | static ssize_t |
| 281 | show_sas_device_type(struct class_device *cdev, char *buf) |
| 282 | { |
| 283 | struct sas_phy *phy = transport_class_to_phy(cdev); |
| 284 | |
| 285 | if (!phy->identify.device_type) |
| 286 | return snprintf(buf, 20, "none\n"); |
| 287 | return get_sas_device_type_names(phy->identify.device_type, buf); |
| 288 | } |
| 289 | |
| 290 | static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL); |
| 291 | |
| 292 | sas_phy_protocol_attr(identify.initiator_port_protocols, |
| 293 | initiator_port_protocols); |
| 294 | sas_phy_protocol_attr(identify.target_port_protocols, |
| 295 | target_port_protocols); |
| 296 | sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n", |
| 297 | unsigned long long); |
| 298 | sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8); |
| 299 | sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8); |
| 300 | sas_phy_linkspeed_attr(negotiated_linkrate); |
| 301 | sas_phy_linkspeed_attr(minimum_linkrate_hw); |
| 302 | sas_phy_linkspeed_attr(minimum_linkrate); |
| 303 | sas_phy_linkspeed_attr(maximum_linkrate_hw); |
| 304 | sas_phy_linkspeed_attr(maximum_linkrate); |
Christoph Hellwig | c3ee74c | 2005-09-19 21:59:42 +0200 | [diff] [blame^] | 305 | sas_phy_linkerror_attr(invalid_dword_count); |
| 306 | sas_phy_linkerror_attr(running_disparity_error_count); |
| 307 | sas_phy_linkerror_attr(loss_of_dword_sync_count); |
| 308 | sas_phy_linkerror_attr(phy_reset_problem_count); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 309 | |
| 310 | |
| 311 | static DECLARE_TRANSPORT_CLASS(sas_phy_class, |
| 312 | "sas_phy", NULL, NULL, NULL); |
| 313 | |
| 314 | static int sas_phy_match(struct attribute_container *cont, struct device *dev) |
| 315 | { |
| 316 | struct Scsi_Host *shost; |
| 317 | struct sas_internal *i; |
| 318 | |
| 319 | if (!scsi_is_sas_phy(dev)) |
| 320 | return 0; |
| 321 | shost = dev_to_shost(dev->parent); |
| 322 | |
| 323 | if (!shost->transportt) |
| 324 | return 0; |
| 325 | if (shost->transportt->host_attrs.ac.class != |
| 326 | &sas_host_class.class) |
| 327 | return 0; |
| 328 | |
| 329 | i = to_sas_internal(shost->transportt); |
| 330 | return &i->phy_attr_cont.ac == cont; |
| 331 | } |
| 332 | |
| 333 | static void sas_phy_release(struct device *dev) |
| 334 | { |
| 335 | struct sas_phy *phy = dev_to_phy(dev); |
| 336 | |
| 337 | put_device(dev->parent); |
| 338 | kfree(phy); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * sas_phy_alloc -- allocates and initialize a SAS PHY structure |
| 343 | * @parent: Parent device |
| 344 | * @number: Port number |
| 345 | * |
| 346 | * Allocates an SAS PHY structure. It will be added in the device tree |
| 347 | * below the device specified by @parent, which has to be either a Scsi_Host |
| 348 | * or sas_rphy. |
| 349 | * |
| 350 | * Returns: |
| 351 | * SAS PHY allocated or %NULL if the allocation failed. |
| 352 | */ |
| 353 | struct sas_phy *sas_phy_alloc(struct device *parent, int number) |
| 354 | { |
| 355 | struct Scsi_Host *shost = dev_to_shost(parent); |
| 356 | struct sas_phy *phy; |
| 357 | |
| 358 | phy = kmalloc(sizeof(*phy), GFP_KERNEL); |
| 359 | if (!phy) |
| 360 | return NULL; |
| 361 | memset(phy, 0, sizeof(*phy)); |
| 362 | |
| 363 | get_device(parent); |
| 364 | |
| 365 | phy->number = number; |
| 366 | |
| 367 | device_initialize(&phy->dev); |
| 368 | phy->dev.parent = get_device(parent); |
| 369 | phy->dev.release = sas_phy_release; |
| 370 | sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number); |
| 371 | |
| 372 | transport_setup_device(&phy->dev); |
| 373 | |
| 374 | return phy; |
| 375 | } |
| 376 | EXPORT_SYMBOL(sas_phy_alloc); |
| 377 | |
| 378 | /** |
| 379 | * sas_phy_add -- add a SAS PHY to the device hierachy |
| 380 | * @phy: The PHY to be added |
| 381 | * |
| 382 | * Publishes a SAS PHY to the rest of the system. |
| 383 | */ |
| 384 | int sas_phy_add(struct sas_phy *phy) |
| 385 | { |
| 386 | int error; |
| 387 | |
| 388 | error = device_add(&phy->dev); |
| 389 | if (!error) { |
| 390 | transport_add_device(&phy->dev); |
| 391 | transport_configure_device(&phy->dev); |
| 392 | } |
| 393 | |
| 394 | return error; |
| 395 | } |
| 396 | EXPORT_SYMBOL(sas_phy_add); |
| 397 | |
| 398 | /** |
| 399 | * sas_phy_free -- free a SAS PHY |
| 400 | * @phy: SAS PHY to free |
| 401 | * |
| 402 | * Frees the specified SAS PHY. |
| 403 | * |
| 404 | * Note: |
| 405 | * This function must only be called on a PHY that has not |
| 406 | * sucessfully been added using sas_phy_add(). |
| 407 | */ |
| 408 | void sas_phy_free(struct sas_phy *phy) |
| 409 | { |
| 410 | transport_destroy_device(&phy->dev); |
| 411 | put_device(phy->dev.parent); |
| 412 | put_device(phy->dev.parent); |
| 413 | put_device(phy->dev.parent); |
| 414 | kfree(phy); |
| 415 | } |
| 416 | EXPORT_SYMBOL(sas_phy_free); |
| 417 | |
| 418 | /** |
| 419 | * sas_phy_delete -- remove SAS PHY |
| 420 | * @phy: SAS PHY to remove |
| 421 | * |
| 422 | * Removes the specified SAS PHY. If the SAS PHY has an |
| 423 | * associated remote PHY it is removed before. |
| 424 | */ |
| 425 | void |
| 426 | sas_phy_delete(struct sas_phy *phy) |
| 427 | { |
| 428 | struct device *dev = &phy->dev; |
| 429 | |
| 430 | if (phy->rphy) |
| 431 | sas_rphy_delete(phy->rphy); |
| 432 | |
| 433 | transport_remove_device(dev); |
| 434 | device_del(dev); |
| 435 | transport_destroy_device(dev); |
| 436 | put_device(dev->parent); |
| 437 | } |
| 438 | EXPORT_SYMBOL(sas_phy_delete); |
| 439 | |
| 440 | /** |
| 441 | * scsi_is_sas_phy -- check if a struct device represents a SAS PHY |
| 442 | * @dev: device to check |
| 443 | * |
| 444 | * Returns: |
| 445 | * %1 if the device represents a SAS PHY, %0 else |
| 446 | */ |
| 447 | int scsi_is_sas_phy(const struct device *dev) |
| 448 | { |
| 449 | return dev->release == sas_phy_release; |
| 450 | } |
| 451 | EXPORT_SYMBOL(scsi_is_sas_phy); |
| 452 | |
| 453 | /* |
| 454 | * SAS remote PHY attributes. |
| 455 | */ |
| 456 | |
| 457 | #define sas_rphy_show_simple(field, name, format_string, cast) \ |
| 458 | static ssize_t \ |
| 459 | show_sas_rphy_##name(struct class_device *cdev, char *buf) \ |
| 460 | { \ |
| 461 | struct sas_rphy *rphy = transport_class_to_rphy(cdev); \ |
| 462 | \ |
| 463 | return snprintf(buf, 20, format_string, cast rphy->field); \ |
| 464 | } |
| 465 | |
| 466 | #define sas_rphy_simple_attr(field, name, format_string, type) \ |
| 467 | sas_rphy_show_simple(field, name, format_string, (type)) \ |
| 468 | static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \ |
| 469 | show_sas_rphy_##name, NULL) |
| 470 | |
| 471 | #define sas_rphy_show_protocol(field, name) \ |
| 472 | static ssize_t \ |
| 473 | show_sas_rphy_##name(struct class_device *cdev, char *buf) \ |
| 474 | { \ |
| 475 | struct sas_rphy *rphy = transport_class_to_rphy(cdev); \ |
| 476 | \ |
| 477 | if (!rphy->field) \ |
| 478 | return snprintf(buf, 20, "none\n"); \ |
| 479 | return get_sas_protocol_names(rphy->field, buf); \ |
| 480 | } |
| 481 | |
| 482 | #define sas_rphy_protocol_attr(field, name) \ |
| 483 | sas_rphy_show_protocol(field, name) \ |
| 484 | static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \ |
| 485 | show_sas_rphy_##name, NULL) |
| 486 | |
| 487 | static ssize_t |
| 488 | show_sas_rphy_device_type(struct class_device *cdev, char *buf) |
| 489 | { |
| 490 | struct sas_rphy *rphy = transport_class_to_rphy(cdev); |
| 491 | |
| 492 | if (!rphy->identify.device_type) |
| 493 | return snprintf(buf, 20, "none\n"); |
| 494 | return get_sas_device_type_names( |
| 495 | rphy->identify.device_type, buf); |
| 496 | } |
| 497 | |
| 498 | static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO, |
| 499 | show_sas_rphy_device_type, NULL); |
| 500 | |
| 501 | sas_rphy_protocol_attr(identify.initiator_port_protocols, |
| 502 | initiator_port_protocols); |
| 503 | sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols); |
| 504 | sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n", |
| 505 | unsigned long long); |
| 506 | sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8); |
| 507 | |
| 508 | static DECLARE_TRANSPORT_CLASS(sas_rphy_class, |
| 509 | "sas_rphy", NULL, NULL, NULL); |
| 510 | |
| 511 | static int sas_rphy_match(struct attribute_container *cont, struct device *dev) |
| 512 | { |
| 513 | struct Scsi_Host *shost; |
| 514 | struct sas_internal *i; |
| 515 | |
| 516 | if (!scsi_is_sas_rphy(dev)) |
| 517 | return 0; |
| 518 | shost = dev_to_shost(dev->parent->parent); |
| 519 | |
| 520 | if (!shost->transportt) |
| 521 | return 0; |
| 522 | if (shost->transportt->host_attrs.ac.class != |
| 523 | &sas_host_class.class) |
| 524 | return 0; |
| 525 | |
| 526 | i = to_sas_internal(shost->transportt); |
| 527 | return &i->rphy_attr_cont.ac == cont; |
| 528 | } |
| 529 | |
| 530 | static void sas_rphy_release(struct device *dev) |
| 531 | { |
| 532 | struct sas_rphy *rphy = dev_to_rphy(dev); |
| 533 | |
| 534 | put_device(dev->parent); |
| 535 | kfree(rphy); |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * sas_rphy_alloc -- allocates and initialize a SAS remote PHY structure |
| 540 | * @parent: SAS PHY this remote PHY is conneted to |
| 541 | * |
| 542 | * Allocates an SAS remote PHY structure, connected to @parent. |
| 543 | * |
| 544 | * Returns: |
| 545 | * SAS PHY allocated or %NULL if the allocation failed. |
| 546 | */ |
| 547 | struct sas_rphy *sas_rphy_alloc(struct sas_phy *parent) |
| 548 | { |
| 549 | struct Scsi_Host *shost = dev_to_shost(&parent->dev); |
| 550 | struct sas_rphy *rphy; |
| 551 | |
| 552 | rphy = kmalloc(sizeof(*rphy), GFP_KERNEL); |
| 553 | if (!rphy) { |
| 554 | put_device(&parent->dev); |
| 555 | return NULL; |
| 556 | } |
| 557 | memset(rphy, 0, sizeof(*rphy)); |
| 558 | |
| 559 | device_initialize(&rphy->dev); |
| 560 | rphy->dev.parent = get_device(&parent->dev); |
| 561 | rphy->dev.release = sas_rphy_release; |
| 562 | sprintf(rphy->dev.bus_id, "rphy-%d:%d", |
| 563 | shost->host_no, parent->number); |
| 564 | transport_setup_device(&rphy->dev); |
| 565 | |
| 566 | return rphy; |
| 567 | } |
| 568 | EXPORT_SYMBOL(sas_rphy_alloc); |
| 569 | |
| 570 | /** |
| 571 | * sas_rphy_add -- add a SAS remote PHY to the device hierachy |
| 572 | * @rphy: The remote PHY to be added |
| 573 | * |
| 574 | * Publishes a SAS remote PHY to the rest of the system. |
| 575 | */ |
| 576 | int sas_rphy_add(struct sas_rphy *rphy) |
| 577 | { |
| 578 | struct sas_phy *parent = dev_to_phy(rphy->dev.parent); |
| 579 | struct Scsi_Host *shost = dev_to_shost(parent->dev.parent); |
| 580 | struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); |
| 581 | struct sas_identify *identify = &rphy->identify; |
| 582 | int error; |
| 583 | |
| 584 | if (parent->rphy) |
| 585 | return -ENXIO; |
| 586 | parent->rphy = rphy; |
| 587 | |
| 588 | error = device_add(&rphy->dev); |
| 589 | if (error) |
| 590 | return error; |
| 591 | transport_add_device(&rphy->dev); |
| 592 | transport_configure_device(&rphy->dev); |
| 593 | |
| 594 | spin_lock(&sas_host->lock); |
| 595 | list_add_tail(&rphy->list, &sas_host->rphy_list); |
| 596 | if (identify->device_type == SAS_END_DEVICE && |
| 597 | (identify->target_port_protocols & |
| 598 | (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA))) |
| 599 | rphy->scsi_target_id = sas_host->next_target_id++; |
| 600 | else |
| 601 | rphy->scsi_target_id = -1; |
| 602 | spin_unlock(&sas_host->lock); |
| 603 | |
| 604 | if (rphy->scsi_target_id != -1) { |
| 605 | scsi_scan_target(&rphy->dev, parent->number, |
| 606 | rphy->scsi_target_id, ~0, 0); |
| 607 | } |
| 608 | |
| 609 | return 0; |
| 610 | } |
| 611 | EXPORT_SYMBOL(sas_rphy_add); |
| 612 | |
| 613 | /** |
| 614 | * sas_rphy_free -- free a SAS remote PHY |
| 615 | * @rphy SAS remote PHY to free |
| 616 | * |
| 617 | * Frees the specified SAS remote PHY. |
| 618 | * |
| 619 | * Note: |
| 620 | * This function must only be called on a remote |
| 621 | * PHY that has not sucessfully been added using |
| 622 | * sas_rphy_add(). |
| 623 | */ |
| 624 | void sas_rphy_free(struct sas_rphy *rphy) |
| 625 | { |
| 626 | struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent); |
| 627 | struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); |
| 628 | |
| 629 | spin_lock(&sas_host->lock); |
| 630 | list_del(&rphy->list); |
| 631 | spin_unlock(&sas_host->lock); |
| 632 | |
| 633 | transport_destroy_device(&rphy->dev); |
| 634 | put_device(rphy->dev.parent); |
| 635 | put_device(rphy->dev.parent); |
| 636 | put_device(rphy->dev.parent); |
| 637 | kfree(rphy); |
| 638 | } |
| 639 | EXPORT_SYMBOL(sas_rphy_free); |
| 640 | |
| 641 | /** |
| 642 | * sas_rphy_delete -- remove SAS remote PHY |
| 643 | * @rphy: SAS remote PHY to remove |
| 644 | * |
| 645 | * Removes the specified SAS remote PHY. |
| 646 | */ |
| 647 | void |
| 648 | sas_rphy_delete(struct sas_rphy *rphy) |
| 649 | { |
| 650 | struct device *dev = &rphy->dev; |
| 651 | struct sas_phy *parent = dev_to_phy(dev->parent); |
| 652 | struct Scsi_Host *shost = dev_to_shost(parent->dev.parent); |
| 653 | struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); |
| 654 | |
Christoph Hellwig | fe8b230 | 2005-09-25 23:10:33 +0200 | [diff] [blame] | 655 | scsi_remove_target(dev); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 656 | |
Christoph Hellwig | fe8b230 | 2005-09-25 23:10:33 +0200 | [diff] [blame] | 657 | transport_remove_device(dev); |
| 658 | device_del(dev); |
| 659 | transport_destroy_device(dev); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 660 | |
| 661 | spin_lock(&sas_host->lock); |
| 662 | list_del(&rphy->list); |
| 663 | spin_unlock(&sas_host->lock); |
| 664 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 665 | put_device(&parent->dev); |
| 666 | } |
| 667 | EXPORT_SYMBOL(sas_rphy_delete); |
| 668 | |
| 669 | /** |
| 670 | * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY |
| 671 | * @dev: device to check |
| 672 | * |
| 673 | * Returns: |
| 674 | * %1 if the device represents a SAS remote PHY, %0 else |
| 675 | */ |
| 676 | int scsi_is_sas_rphy(const struct device *dev) |
| 677 | { |
| 678 | return dev->release == sas_rphy_release; |
| 679 | } |
| 680 | EXPORT_SYMBOL(scsi_is_sas_rphy); |
| 681 | |
| 682 | |
| 683 | /* |
| 684 | * SCSI scan helper |
| 685 | */ |
| 686 | |
| 687 | static struct device *sas_target_parent(struct Scsi_Host *shost, |
| 688 | int channel, uint id) |
| 689 | { |
| 690 | struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); |
| 691 | struct sas_rphy *rphy; |
| 692 | struct device *dev = NULL; |
| 693 | |
| 694 | spin_lock(&sas_host->lock); |
| 695 | list_for_each_entry(rphy, &sas_host->rphy_list, list) { |
| 696 | struct sas_phy *parent = dev_to_phy(rphy->dev.parent); |
| 697 | if (parent->number == channel && |
| 698 | rphy->scsi_target_id == id) |
| 699 | dev = &rphy->dev; |
| 700 | } |
| 701 | spin_unlock(&sas_host->lock); |
| 702 | |
| 703 | return dev; |
| 704 | } |
| 705 | |
| 706 | |
| 707 | /* |
| 708 | * Setup / Teardown code |
| 709 | */ |
| 710 | |
| 711 | #define SETUP_RPORT_ATTRIBUTE(field) \ |
| 712 | i->private_rphy_attrs[count] = class_device_attr_##field; \ |
| 713 | i->private_rphy_attrs[count].attr.mode = S_IRUGO; \ |
| 714 | i->private_rphy_attrs[count].store = NULL; \ |
| 715 | i->rphy_attrs[count] = &i->private_rphy_attrs[count]; \ |
| 716 | count++ |
| 717 | |
| 718 | #define SETUP_PORT_ATTRIBUTE(field) \ |
| 719 | i->private_phy_attrs[count] = class_device_attr_##field; \ |
| 720 | i->private_phy_attrs[count].attr.mode = S_IRUGO; \ |
| 721 | i->private_phy_attrs[count].store = NULL; \ |
| 722 | i->phy_attrs[count] = &i->private_phy_attrs[count]; \ |
| 723 | count++ |
| 724 | |
| 725 | |
| 726 | /** |
| 727 | * sas_attach_transport -- instantiate SAS transport template |
| 728 | * @ft: SAS transport class function template |
| 729 | */ |
| 730 | struct scsi_transport_template * |
| 731 | sas_attach_transport(struct sas_function_template *ft) |
| 732 | { |
| 733 | struct sas_internal *i; |
| 734 | int count; |
| 735 | |
| 736 | i = kmalloc(sizeof(struct sas_internal), GFP_KERNEL); |
| 737 | if (!i) |
| 738 | return NULL; |
| 739 | memset(i, 0, sizeof(struct sas_internal)); |
| 740 | |
| 741 | i->t.target_parent = sas_target_parent; |
| 742 | |
| 743 | i->t.host_attrs.ac.attrs = &i->host_attrs[0]; |
| 744 | i->t.host_attrs.ac.class = &sas_host_class.class; |
| 745 | i->t.host_attrs.ac.match = sas_host_match; |
| 746 | transport_container_register(&i->t.host_attrs); |
| 747 | i->t.host_size = sizeof(struct sas_host_attrs); |
| 748 | |
| 749 | i->phy_attr_cont.ac.class = &sas_phy_class.class; |
| 750 | i->phy_attr_cont.ac.attrs = &i->phy_attrs[0]; |
| 751 | i->phy_attr_cont.ac.match = sas_phy_match; |
| 752 | transport_container_register(&i->phy_attr_cont); |
| 753 | |
| 754 | i->rphy_attr_cont.ac.class = &sas_rphy_class.class; |
| 755 | i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0]; |
| 756 | i->rphy_attr_cont.ac.match = sas_rphy_match; |
| 757 | transport_container_register(&i->rphy_attr_cont); |
| 758 | |
| 759 | i->f = ft; |
| 760 | |
| 761 | count = 0; |
| 762 | i->host_attrs[count] = NULL; |
| 763 | |
| 764 | count = 0; |
| 765 | SETUP_PORT_ATTRIBUTE(initiator_port_protocols); |
| 766 | SETUP_PORT_ATTRIBUTE(target_port_protocols); |
| 767 | SETUP_PORT_ATTRIBUTE(device_type); |
| 768 | SETUP_PORT_ATTRIBUTE(sas_address); |
| 769 | SETUP_PORT_ATTRIBUTE(phy_identifier); |
| 770 | SETUP_PORT_ATTRIBUTE(port_identifier); |
| 771 | SETUP_PORT_ATTRIBUTE(negotiated_linkrate); |
| 772 | SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw); |
| 773 | SETUP_PORT_ATTRIBUTE(minimum_linkrate); |
| 774 | SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw); |
| 775 | SETUP_PORT_ATTRIBUTE(maximum_linkrate); |
Christoph Hellwig | c3ee74c | 2005-09-19 21:59:42 +0200 | [diff] [blame^] | 776 | |
| 777 | SETUP_PORT_ATTRIBUTE(invalid_dword_count); |
| 778 | SETUP_PORT_ATTRIBUTE(running_disparity_error_count); |
| 779 | SETUP_PORT_ATTRIBUTE(loss_of_dword_sync_count); |
| 780 | SETUP_PORT_ATTRIBUTE(phy_reset_problem_count); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 781 | i->phy_attrs[count] = NULL; |
| 782 | |
| 783 | count = 0; |
| 784 | SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols); |
| 785 | SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols); |
| 786 | SETUP_RPORT_ATTRIBUTE(rphy_device_type); |
| 787 | SETUP_RPORT_ATTRIBUTE(rphy_sas_address); |
| 788 | SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier); |
| 789 | i->rphy_attrs[count] = NULL; |
| 790 | |
| 791 | return &i->t; |
| 792 | } |
| 793 | EXPORT_SYMBOL(sas_attach_transport); |
| 794 | |
| 795 | /** |
| 796 | * sas_release_transport -- release SAS transport template instance |
| 797 | * @t: transport template instance |
| 798 | */ |
| 799 | void sas_release_transport(struct scsi_transport_template *t) |
| 800 | { |
| 801 | struct sas_internal *i = to_sas_internal(t); |
| 802 | |
| 803 | transport_container_unregister(&i->t.host_attrs); |
| 804 | transport_container_unregister(&i->phy_attr_cont); |
| 805 | transport_container_unregister(&i->rphy_attr_cont); |
| 806 | |
| 807 | kfree(i); |
| 808 | } |
| 809 | EXPORT_SYMBOL(sas_release_transport); |
| 810 | |
| 811 | static __init int sas_transport_init(void) |
| 812 | { |
| 813 | int error; |
| 814 | |
| 815 | error = transport_class_register(&sas_host_class); |
| 816 | if (error) |
| 817 | goto out; |
| 818 | error = transport_class_register(&sas_phy_class); |
| 819 | if (error) |
| 820 | goto out_unregister_transport; |
| 821 | error = transport_class_register(&sas_rphy_class); |
| 822 | if (error) |
| 823 | goto out_unregister_phy; |
| 824 | |
| 825 | return 0; |
| 826 | |
| 827 | out_unregister_phy: |
| 828 | transport_class_unregister(&sas_phy_class); |
| 829 | out_unregister_transport: |
| 830 | transport_class_unregister(&sas_host_class); |
| 831 | out: |
| 832 | return error; |
| 833 | |
| 834 | } |
| 835 | |
| 836 | static void __exit sas_transport_exit(void) |
| 837 | { |
| 838 | transport_class_unregister(&sas_host_class); |
| 839 | transport_class_unregister(&sas_phy_class); |
| 840 | transport_class_unregister(&sas_rphy_class); |
| 841 | } |
| 842 | |
| 843 | MODULE_AUTHOR("Christoph Hellwig"); |
| 844 | MODULE_DESCRIPTION("SAS Transphy Attributes"); |
| 845 | MODULE_LICENSE("GPL"); |
| 846 | |
| 847 | module_init(sas_transport_init); |
| 848 | module_exit(sas_transport_exit); |