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