Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1 | /* |
Christoph Hellwig | a012564 | 2006-02-16 13:31:47 +0100 | [diff] [blame] | 2 | * Copyright (C) 2005-2006 Dell Inc. |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 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, |
Joe Perches | b1c1181 | 2008-02-03 17:28:22 +0200 | [diff] [blame] | 9 | * and various sysfs attributes to expose these topologies and management |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 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> |
Al Viro | f6a5703 | 2006-10-18 01:47:25 -0400 | [diff] [blame] | 28 | #include <linux/jiffies.h> |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 29 | #include <linux/err.h> |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 30 | #include <linux/slab.h> |
| 31 | #include <linux/string.h> |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 32 | #include <linux/blkdev.h> |
| 33 | #include <linux/bsg.h> |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 34 | |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 35 | #include <scsi/scsi.h> |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 36 | #include <scsi/scsi_device.h> |
| 37 | #include <scsi/scsi_host.h> |
| 38 | #include <scsi/scsi_transport.h> |
| 39 | #include <scsi/scsi_transport_sas.h> |
| 40 | |
James Bottomley | d6159c1 | 2006-03-27 16:45:34 -0600 | [diff] [blame] | 41 | #include "scsi_sas_internal.h" |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 42 | struct sas_host_attrs { |
| 43 | struct list_head rphy_list; |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 44 | struct mutex lock; |
James Bottomley | b6aff66 | 2007-07-20 11:10:05 -0500 | [diff] [blame] | 45 | struct request_queue *q; |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 46 | u32 next_target_id; |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 47 | u32 next_expander_id; |
James Bottomley | c9fefeb | 2006-07-02 11:10:18 -0500 | [diff] [blame] | 48 | int next_port_id; |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 49 | }; |
| 50 | #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data) |
| 51 | |
| 52 | |
| 53 | /* |
| 54 | * Hack to allow attributes of the same name in different objects. |
| 55 | */ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 56 | #define SAS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \ |
| 57 | struct device_attribute dev_attr_##_prefix##_##_name = \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 58 | __ATTR(_name,_mode,_show,_store) |
| 59 | |
| 60 | |
| 61 | /* |
| 62 | * Pretty printing helpers |
| 63 | */ |
| 64 | |
| 65 | #define sas_bitfield_name_match(title, table) \ |
| 66 | static ssize_t \ |
| 67 | get_sas_##title##_names(u32 table_key, char *buf) \ |
| 68 | { \ |
| 69 | char *prefix = ""; \ |
| 70 | ssize_t len = 0; \ |
| 71 | int i; \ |
| 72 | \ |
Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 73 | for (i = 0; i < ARRAY_SIZE(table); i++) { \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 74 | if (table[i].value & table_key) { \ |
| 75 | len += sprintf(buf + len, "%s%s", \ |
| 76 | prefix, table[i].name); \ |
| 77 | prefix = ", "; \ |
| 78 | } \ |
| 79 | } \ |
| 80 | len += sprintf(buf + len, "\n"); \ |
| 81 | return len; \ |
| 82 | } |
| 83 | |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 84 | #define sas_bitfield_name_set(title, table) \ |
| 85 | static ssize_t \ |
| 86 | set_sas_##title##_names(u32 *table_key, const char *buf) \ |
| 87 | { \ |
| 88 | ssize_t len = 0; \ |
| 89 | int i; \ |
| 90 | \ |
| 91 | for (i = 0; i < ARRAY_SIZE(table); i++) { \ |
| 92 | len = strlen(table[i].name); \ |
| 93 | if (strncmp(buf, table[i].name, len) == 0 && \ |
| 94 | (buf[len] == '\n' || buf[len] == '\0')) { \ |
| 95 | *table_key = table[i].value; \ |
| 96 | return 0; \ |
| 97 | } \ |
| 98 | } \ |
| 99 | return -EINVAL; \ |
| 100 | } |
| 101 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 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 | \ |
Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 109 | for (i = 0; i < ARRAY_SIZE(table); i++) { \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 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" }, |
James Bottomley | 7e6dff6 | 2006-03-02 14:12:56 -0600 | [diff] [blame] | 153 | { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" }, |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 154 | }; |
| 155 | sas_bitfield_name_search(linkspeed, sas_linkspeed_names) |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 156 | sas_bitfield_name_set(linkspeed, sas_linkspeed_names) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 157 | |
James Bottomley | 0f88009 | 2010-01-18 10:14:51 -0600 | [diff] [blame] | 158 | static struct sas_end_device *sas_sdev_to_rdev(struct scsi_device *sdev) |
| 159 | { |
| 160 | struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target); |
| 161 | struct sas_end_device *rdev; |
| 162 | |
| 163 | BUG_ON(rphy->identify.device_type != SAS_END_DEVICE); |
| 164 | |
| 165 | rdev = rphy_to_end_device(rphy); |
| 166 | return rdev; |
| 167 | } |
| 168 | |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 169 | static void sas_smp_request(struct request_queue *q, struct Scsi_Host *shost, |
| 170 | struct sas_rphy *rphy) |
| 171 | { |
| 172 | struct request *req; |
| 173 | int ret; |
| 174 | int (*handler)(struct Scsi_Host *, struct sas_rphy *, struct request *); |
| 175 | |
| 176 | while (!blk_queue_plugged(q)) { |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 177 | req = blk_fetch_request(q); |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 178 | if (!req) |
| 179 | break; |
| 180 | |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 181 | spin_unlock_irq(q->queue_lock); |
| 182 | |
| 183 | handler = to_sas_internal(shost->transportt)->f->smp_handler; |
| 184 | ret = handler(shost, rphy, req); |
James Bottomley | 2d507a0 | 2007-12-29 10:59:53 -0600 | [diff] [blame] | 185 | req->errors = ret; |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 186 | |
FUJITA Tomonori | 93bdcba | 2009-06-17 15:10:10 +0900 | [diff] [blame] | 187 | blk_end_request_all(req, ret); |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 188 | |
FUJITA Tomonori | 93bdcba | 2009-06-17 15:10:10 +0900 | [diff] [blame] | 189 | spin_lock_irq(q->queue_lock); |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | static void sas_host_smp_request(struct request_queue *q) |
| 194 | { |
| 195 | sas_smp_request(q, (struct Scsi_Host *)q->queuedata, NULL); |
| 196 | } |
| 197 | |
| 198 | static void sas_non_host_smp_request(struct request_queue *q) |
| 199 | { |
| 200 | struct sas_rphy *rphy = q->queuedata; |
| 201 | sas_smp_request(q, rphy_to_shost(rphy), rphy); |
| 202 | } |
| 203 | |
FUJITA Tomonori | 93c20a5 | 2008-04-19 00:43:15 +0900 | [diff] [blame] | 204 | static void sas_host_release(struct device *dev) |
| 205 | { |
| 206 | struct Scsi_Host *shost = dev_to_shost(dev); |
| 207 | struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); |
| 208 | struct request_queue *q = sas_host->q; |
| 209 | |
| 210 | if (q) |
| 211 | blk_cleanup_queue(q); |
| 212 | } |
| 213 | |
James Bottomley | 39dca55 | 2007-07-20 18:22:17 -0500 | [diff] [blame] | 214 | static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy) |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 215 | { |
| 216 | struct request_queue *q; |
| 217 | int error; |
James Bottomley | 39dca55 | 2007-07-20 18:22:17 -0500 | [diff] [blame] | 218 | struct device *dev; |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 219 | char namebuf[20]; |
James Bottomley | 39dca55 | 2007-07-20 18:22:17 -0500 | [diff] [blame] | 220 | const char *name; |
FUJITA Tomonori | 93c20a5 | 2008-04-19 00:43:15 +0900 | [diff] [blame] | 221 | void (*release)(struct device *); |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 222 | |
| 223 | if (!to_sas_internal(shost->transportt)->f->smp_handler) { |
| 224 | printk("%s can't handle SMP requests\n", shost->hostt->name); |
| 225 | return 0; |
| 226 | } |
| 227 | |
James Bottomley | 39dca55 | 2007-07-20 18:22:17 -0500 | [diff] [blame] | 228 | if (rphy) { |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 229 | q = blk_init_queue(sas_non_host_smp_request, NULL); |
James Bottomley | 39dca55 | 2007-07-20 18:22:17 -0500 | [diff] [blame] | 230 | dev = &rphy->dev; |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 231 | name = dev_name(dev); |
FUJITA Tomonori | 93c20a5 | 2008-04-19 00:43:15 +0900 | [diff] [blame] | 232 | release = NULL; |
James Bottomley | 39dca55 | 2007-07-20 18:22:17 -0500 | [diff] [blame] | 233 | } else { |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 234 | q = blk_init_queue(sas_host_smp_request, NULL); |
James Bottomley | 39dca55 | 2007-07-20 18:22:17 -0500 | [diff] [blame] | 235 | dev = &shost->shost_gendev; |
| 236 | snprintf(namebuf, sizeof(namebuf), |
| 237 | "sas_host%d", shost->host_no); |
| 238 | name = namebuf; |
FUJITA Tomonori | 93c20a5 | 2008-04-19 00:43:15 +0900 | [diff] [blame] | 239 | release = sas_host_release; |
James Bottomley | 39dca55 | 2007-07-20 18:22:17 -0500 | [diff] [blame] | 240 | } |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 241 | if (!q) |
| 242 | return -ENOMEM; |
| 243 | |
FUJITA Tomonori | 93c20a5 | 2008-04-19 00:43:15 +0900 | [diff] [blame] | 244 | error = bsg_register_queue(q, dev, name, release); |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 245 | if (error) { |
| 246 | blk_cleanup_queue(q); |
| 247 | return -ENOMEM; |
| 248 | } |
| 249 | |
| 250 | if (rphy) |
James Bottomley | b6aff66 | 2007-07-20 11:10:05 -0500 | [diff] [blame] | 251 | rphy->q = q; |
| 252 | else |
| 253 | to_sas_host_attrs(shost)->q = q; |
| 254 | |
| 255 | if (rphy) |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 256 | q->queuedata = rphy; |
| 257 | else |
| 258 | q->queuedata = shost; |
| 259 | |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 260 | queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q); |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
James Bottomley | b6aff66 | 2007-07-20 11:10:05 -0500 | [diff] [blame] | 264 | static void sas_bsg_remove(struct Scsi_Host *shost, struct sas_rphy *rphy) |
| 265 | { |
| 266 | struct request_queue *q; |
| 267 | |
| 268 | if (rphy) |
| 269 | q = rphy->q; |
| 270 | else |
| 271 | q = to_sas_host_attrs(shost)->q; |
| 272 | |
| 273 | if (!q) |
| 274 | return; |
| 275 | |
| 276 | bsg_unregister_queue(q); |
James Bottomley | b6aff66 | 2007-07-20 11:10:05 -0500 | [diff] [blame] | 277 | } |
| 278 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 279 | /* |
| 280 | * SAS host attributes |
| 281 | */ |
| 282 | |
James Bottomley | 37be6ee | 2005-09-09 18:38:27 -0500 | [diff] [blame] | 283 | static int sas_host_setup(struct transport_container *tc, struct device *dev, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 284 | struct device *cdev) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 285 | { |
| 286 | struct Scsi_Host *shost = dev_to_shost(dev); |
| 287 | struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); |
| 288 | |
| 289 | INIT_LIST_HEAD(&sas_host->rphy_list); |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 290 | mutex_init(&sas_host->lock); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 291 | sas_host->next_target_id = 0; |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 292 | sas_host->next_expander_id = 0; |
James Bottomley | c9fefeb | 2006-07-02 11:10:18 -0500 | [diff] [blame] | 293 | sas_host->next_port_id = 0; |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 294 | |
James Bottomley | 39dca55 | 2007-07-20 18:22:17 -0500 | [diff] [blame] | 295 | if (sas_bsg_initialize(shost, NULL)) |
FUJITA Tomonori | 7aa68e8 | 2007-07-09 12:52:06 +0900 | [diff] [blame] | 296 | dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n", |
| 297 | shost->host_no); |
| 298 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
James Bottomley | b6aff66 | 2007-07-20 11:10:05 -0500 | [diff] [blame] | 302 | static int sas_host_remove(struct transport_container *tc, struct device *dev, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 303 | struct device *cdev) |
James Bottomley | b6aff66 | 2007-07-20 11:10:05 -0500 | [diff] [blame] | 304 | { |
| 305 | struct Scsi_Host *shost = dev_to_shost(dev); |
| 306 | |
| 307 | sas_bsg_remove(shost, NULL); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 312 | static DECLARE_TRANSPORT_CLASS(sas_host_class, |
James Bottomley | b6aff66 | 2007-07-20 11:10:05 -0500 | [diff] [blame] | 313 | "sas_host", sas_host_setup, sas_host_remove, NULL); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 314 | |
| 315 | static int sas_host_match(struct attribute_container *cont, |
| 316 | struct device *dev) |
| 317 | { |
| 318 | struct Scsi_Host *shost; |
| 319 | struct sas_internal *i; |
| 320 | |
| 321 | if (!scsi_is_host_device(dev)) |
| 322 | return 0; |
| 323 | shost = dev_to_shost(dev); |
| 324 | |
| 325 | if (!shost->transportt) |
| 326 | return 0; |
| 327 | if (shost->transportt->host_attrs.ac.class != |
| 328 | &sas_host_class.class) |
| 329 | return 0; |
| 330 | |
| 331 | i = to_sas_internal(shost->transportt); |
| 332 | return &i->t.host_attrs.ac == cont; |
| 333 | } |
| 334 | |
| 335 | static int do_sas_phy_delete(struct device *dev, void *data) |
| 336 | { |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 337 | int pass = (int)(unsigned long)data; |
| 338 | |
| 339 | if (pass == 0 && scsi_is_sas_port(dev)) |
| 340 | sas_port_delete(dev_to_sas_port(dev)); |
| 341 | else if (pass == 1 && scsi_is_sas_phy(dev)) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 342 | sas_phy_delete(dev_to_phy(dev)); |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 347 | * sas_remove_children - tear down a devices SAS data structures |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 348 | * @dev: device belonging to the sas object |
| 349 | * |
| 350 | * Removes all SAS PHYs and remote PHYs for a given object |
| 351 | */ |
| 352 | void sas_remove_children(struct device *dev) |
| 353 | { |
| 354 | device_for_each_child(dev, (void *)0, do_sas_phy_delete); |
| 355 | device_for_each_child(dev, (void *)1, do_sas_phy_delete); |
| 356 | } |
| 357 | EXPORT_SYMBOL(sas_remove_children); |
| 358 | |
| 359 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 360 | * sas_remove_host - tear down a Scsi_Host's SAS data structures |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 361 | * @shost: Scsi Host that is torn down |
| 362 | * |
| 363 | * Removes all SAS PHYs and remote PHYs for a given Scsi_Host. |
| 364 | * Must be called just before scsi_remove_host for SAS HBAs. |
| 365 | */ |
| 366 | void sas_remove_host(struct Scsi_Host *shost) |
| 367 | { |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 368 | sas_remove_children(&shost->shost_gendev); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 369 | } |
| 370 | EXPORT_SYMBOL(sas_remove_host); |
| 371 | |
James Bottomley | 0f88009 | 2010-01-18 10:14:51 -0600 | [diff] [blame] | 372 | /** |
| 373 | * sas_tlr_supported - checking TLR bit in vpd 0x90 |
| 374 | * @sdev: scsi device struct |
| 375 | * |
| 376 | * Check Transport Layer Retries are supported or not. |
| 377 | * If vpd page 0x90 is present, TRL is supported. |
| 378 | * |
| 379 | */ |
| 380 | unsigned int |
| 381 | sas_tlr_supported(struct scsi_device *sdev) |
| 382 | { |
| 383 | const int vpd_len = 32; |
| 384 | struct sas_end_device *rdev = sas_sdev_to_rdev(sdev); |
| 385 | char *buffer = kzalloc(vpd_len, GFP_KERNEL); |
| 386 | int ret = 0; |
| 387 | |
| 388 | if (scsi_get_vpd_page(sdev, 0x90, buffer, vpd_len)) |
| 389 | goto out; |
| 390 | |
| 391 | /* |
| 392 | * Magic numbers: the VPD Protocol page (0x90) |
| 393 | * has a 4 byte header and then one entry per device port |
| 394 | * the TLR bit is at offset 8 on each port entry |
| 395 | * if we take the first port, that's at total offset 12 |
| 396 | */ |
| 397 | ret = buffer[12] & 0x01; |
| 398 | |
| 399 | out: |
| 400 | kfree(buffer); |
| 401 | rdev->tlr_supported = ret; |
| 402 | return ret; |
| 403 | |
| 404 | } |
| 405 | EXPORT_SYMBOL_GPL(sas_tlr_supported); |
| 406 | |
| 407 | /** |
| 408 | * sas_disable_tlr - setting TLR flags |
| 409 | * @sdev: scsi device struct |
| 410 | * |
| 411 | * Seting tlr_enabled flag to 0. |
| 412 | * |
| 413 | */ |
| 414 | void |
| 415 | sas_disable_tlr(struct scsi_device *sdev) |
| 416 | { |
| 417 | struct sas_end_device *rdev = sas_sdev_to_rdev(sdev); |
| 418 | |
| 419 | rdev->tlr_enabled = 0; |
| 420 | } |
| 421 | EXPORT_SYMBOL_GPL(sas_disable_tlr); |
| 422 | |
| 423 | /** |
| 424 | * sas_enable_tlr - setting TLR flags |
| 425 | * @sdev: scsi device struct |
| 426 | * |
| 427 | * Seting tlr_enabled flag 1. |
| 428 | * |
| 429 | */ |
| 430 | void sas_enable_tlr(struct scsi_device *sdev) |
| 431 | { |
| 432 | unsigned int tlr_supported = 0; |
| 433 | tlr_supported = sas_tlr_supported(sdev); |
| 434 | |
| 435 | if (tlr_supported) { |
| 436 | struct sas_end_device *rdev = sas_sdev_to_rdev(sdev); |
| 437 | |
| 438 | rdev->tlr_enabled = 1; |
| 439 | } |
| 440 | |
| 441 | return; |
| 442 | } |
| 443 | EXPORT_SYMBOL_GPL(sas_enable_tlr); |
| 444 | |
| 445 | unsigned int sas_is_tlr_enabled(struct scsi_device *sdev) |
| 446 | { |
| 447 | struct sas_end_device *rdev = sas_sdev_to_rdev(sdev); |
| 448 | return rdev->tlr_enabled; |
| 449 | } |
| 450 | EXPORT_SYMBOL_GPL(sas_is_tlr_enabled); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 451 | |
| 452 | /* |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 453 | * SAS Phy attributes |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 454 | */ |
| 455 | |
| 456 | #define sas_phy_show_simple(field, name, format_string, cast) \ |
| 457 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 458 | show_sas_phy_##name(struct device *dev, \ |
| 459 | struct device_attribute *attr, char *buf) \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 460 | { \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 461 | struct sas_phy *phy = transport_class_to_phy(dev); \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 462 | \ |
| 463 | return snprintf(buf, 20, format_string, cast phy->field); \ |
| 464 | } |
| 465 | |
| 466 | #define sas_phy_simple_attr(field, name, format_string, type) \ |
| 467 | sas_phy_show_simple(field, name, format_string, (type)) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 468 | static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 469 | |
| 470 | #define sas_phy_show_protocol(field, name) \ |
| 471 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 472 | show_sas_phy_##name(struct device *dev, \ |
| 473 | struct device_attribute *attr, char *buf) \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 474 | { \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 475 | struct sas_phy *phy = transport_class_to_phy(dev); \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 476 | \ |
| 477 | if (!phy->field) \ |
| 478 | return snprintf(buf, 20, "none\n"); \ |
| 479 | return get_sas_protocol_names(phy->field, buf); \ |
| 480 | } |
| 481 | |
| 482 | #define sas_phy_protocol_attr(field, name) \ |
| 483 | sas_phy_show_protocol(field, name) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 484 | static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 485 | |
| 486 | #define sas_phy_show_linkspeed(field) \ |
| 487 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 488 | show_sas_phy_##field(struct device *dev, \ |
| 489 | struct device_attribute *attr, char *buf) \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 490 | { \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 491 | struct sas_phy *phy = transport_class_to_phy(dev); \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 492 | \ |
| 493 | return get_sas_linkspeed_names(phy->field, buf); \ |
| 494 | } |
| 495 | |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 496 | /* Fudge to tell if we're minimum or maximum */ |
| 497 | #define sas_phy_store_linkspeed(field) \ |
| 498 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 499 | store_sas_phy_##field(struct device *dev, \ |
| 500 | struct device_attribute *attr, \ |
| 501 | const char *buf, size_t count) \ |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 502 | { \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 503 | struct sas_phy *phy = transport_class_to_phy(dev); \ |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 504 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \ |
| 505 | struct sas_internal *i = to_sas_internal(shost->transportt); \ |
| 506 | u32 value; \ |
| 507 | struct sas_phy_linkrates rates = {0}; \ |
| 508 | int error; \ |
| 509 | \ |
| 510 | error = set_sas_linkspeed_names(&value, buf); \ |
| 511 | if (error) \ |
| 512 | return error; \ |
| 513 | rates.field = value; \ |
| 514 | error = i->f->set_phy_speed(phy, &rates); \ |
| 515 | \ |
| 516 | return error ? error : count; \ |
| 517 | } |
| 518 | |
| 519 | #define sas_phy_linkspeed_rw_attr(field) \ |
| 520 | sas_phy_show_linkspeed(field) \ |
| 521 | sas_phy_store_linkspeed(field) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 522 | static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, \ |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 523 | store_sas_phy_##field) |
| 524 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 525 | #define sas_phy_linkspeed_attr(field) \ |
| 526 | sas_phy_show_linkspeed(field) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 527 | static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 528 | |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 529 | |
Christoph Hellwig | c3ee74c | 2005-09-19 21:59:42 +0200 | [diff] [blame] | 530 | #define sas_phy_show_linkerror(field) \ |
| 531 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 532 | show_sas_phy_##field(struct device *dev, \ |
| 533 | struct device_attribute *attr, char *buf) \ |
Christoph Hellwig | c3ee74c | 2005-09-19 21:59:42 +0200 | [diff] [blame] | 534 | { \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 535 | struct sas_phy *phy = transport_class_to_phy(dev); \ |
Christoph Hellwig | c3ee74c | 2005-09-19 21:59:42 +0200 | [diff] [blame] | 536 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \ |
| 537 | struct sas_internal *i = to_sas_internal(shost->transportt); \ |
| 538 | int error; \ |
| 539 | \ |
James Bottomley | dd9fbb52 | 2006-03-02 16:01:31 -0600 | [diff] [blame] | 540 | error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \ |
Christoph Hellwig | c3ee74c | 2005-09-19 21:59:42 +0200 | [diff] [blame] | 541 | if (error) \ |
| 542 | return error; \ |
| 543 | return snprintf(buf, 20, "%u\n", phy->field); \ |
| 544 | } |
| 545 | |
| 546 | #define sas_phy_linkerror_attr(field) \ |
| 547 | sas_phy_show_linkerror(field) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 548 | static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL) |
Christoph Hellwig | c3ee74c | 2005-09-19 21:59:42 +0200 | [diff] [blame] | 549 | |
| 550 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 551 | static ssize_t |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 552 | show_sas_device_type(struct device *dev, |
| 553 | struct device_attribute *attr, char *buf) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 554 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 555 | struct sas_phy *phy = transport_class_to_phy(dev); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 556 | |
| 557 | if (!phy->identify.device_type) |
| 558 | return snprintf(buf, 20, "none\n"); |
| 559 | return get_sas_device_type_names(phy->identify.device_type, buf); |
| 560 | } |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 561 | static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 562 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 563 | static ssize_t do_sas_phy_enable(struct device *dev, |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 564 | size_t count, int enable) |
| 565 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 566 | struct sas_phy *phy = transport_class_to_phy(dev); |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 567 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); |
| 568 | struct sas_internal *i = to_sas_internal(shost->transportt); |
| 569 | int error; |
| 570 | |
| 571 | error = i->f->phy_enable(phy, enable); |
| 572 | if (error) |
| 573 | return error; |
| 574 | phy->enabled = enable; |
| 575 | return count; |
| 576 | }; |
| 577 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 578 | static ssize_t |
| 579 | store_sas_phy_enable(struct device *dev, struct device_attribute *attr, |
| 580 | const char *buf, size_t count) |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 581 | { |
| 582 | if (count < 1) |
| 583 | return -EINVAL; |
| 584 | |
| 585 | switch (buf[0]) { |
| 586 | case '0': |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 587 | do_sas_phy_enable(dev, count, 0); |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 588 | break; |
| 589 | case '1': |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 590 | do_sas_phy_enable(dev, count, 1); |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 591 | break; |
| 592 | default: |
| 593 | return -EINVAL; |
| 594 | } |
| 595 | |
| 596 | return count; |
| 597 | } |
| 598 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 599 | static ssize_t |
| 600 | show_sas_phy_enable(struct device *dev, struct device_attribute *attr, |
| 601 | char *buf) |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 602 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 603 | struct sas_phy *phy = transport_class_to_phy(dev); |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 604 | |
| 605 | return snprintf(buf, 20, "%d", phy->enabled); |
| 606 | } |
| 607 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 608 | static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable, |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 609 | store_sas_phy_enable); |
| 610 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 611 | static ssize_t |
| 612 | do_sas_phy_reset(struct device *dev, size_t count, int hard_reset) |
Christoph Hellwig | 07ba3a9 | 2005-10-19 20:01:31 +0200 | [diff] [blame] | 613 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 614 | struct sas_phy *phy = transport_class_to_phy(dev); |
Christoph Hellwig | 07ba3a9 | 2005-10-19 20:01:31 +0200 | [diff] [blame] | 615 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); |
| 616 | struct sas_internal *i = to_sas_internal(shost->transportt); |
| 617 | int error; |
| 618 | |
Christoph Hellwig | 07ba3a9 | 2005-10-19 20:01:31 +0200 | [diff] [blame] | 619 | error = i->f->phy_reset(phy, hard_reset); |
| 620 | if (error) |
| 621 | return error; |
| 622 | return count; |
| 623 | }; |
| 624 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 625 | static ssize_t |
| 626 | store_sas_link_reset(struct device *dev, struct device_attribute *attr, |
| 627 | const char *buf, size_t count) |
Christoph Hellwig | 07ba3a9 | 2005-10-19 20:01:31 +0200 | [diff] [blame] | 628 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 629 | return do_sas_phy_reset(dev, count, 0); |
Christoph Hellwig | 07ba3a9 | 2005-10-19 20:01:31 +0200 | [diff] [blame] | 630 | } |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 631 | static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset); |
Christoph Hellwig | 07ba3a9 | 2005-10-19 20:01:31 +0200 | [diff] [blame] | 632 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 633 | static ssize_t |
| 634 | store_sas_hard_reset(struct device *dev, struct device_attribute *attr, |
| 635 | const char *buf, size_t count) |
Christoph Hellwig | 07ba3a9 | 2005-10-19 20:01:31 +0200 | [diff] [blame] | 636 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 637 | return do_sas_phy_reset(dev, count, 1); |
Christoph Hellwig | 07ba3a9 | 2005-10-19 20:01:31 +0200 | [diff] [blame] | 638 | } |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 639 | static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset); |
Christoph Hellwig | 07ba3a9 | 2005-10-19 20:01:31 +0200 | [diff] [blame] | 640 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 641 | sas_phy_protocol_attr(identify.initiator_port_protocols, |
| 642 | initiator_port_protocols); |
| 643 | sas_phy_protocol_attr(identify.target_port_protocols, |
| 644 | target_port_protocols); |
| 645 | sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n", |
| 646 | unsigned long long); |
| 647 | sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8); |
James Bottomley | c9fefeb | 2006-07-02 11:10:18 -0500 | [diff] [blame] | 648 | //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 649 | sas_phy_linkspeed_attr(negotiated_linkrate); |
| 650 | sas_phy_linkspeed_attr(minimum_linkrate_hw); |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 651 | sas_phy_linkspeed_rw_attr(minimum_linkrate); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 652 | sas_phy_linkspeed_attr(maximum_linkrate_hw); |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 653 | sas_phy_linkspeed_rw_attr(maximum_linkrate); |
Christoph Hellwig | c3ee74c | 2005-09-19 21:59:42 +0200 | [diff] [blame] | 654 | sas_phy_linkerror_attr(invalid_dword_count); |
| 655 | sas_phy_linkerror_attr(running_disparity_error_count); |
| 656 | sas_phy_linkerror_attr(loss_of_dword_sync_count); |
| 657 | sas_phy_linkerror_attr(phy_reset_problem_count); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 658 | |
| 659 | |
| 660 | static DECLARE_TRANSPORT_CLASS(sas_phy_class, |
| 661 | "sas_phy", NULL, NULL, NULL); |
| 662 | |
| 663 | static int sas_phy_match(struct attribute_container *cont, struct device *dev) |
| 664 | { |
| 665 | struct Scsi_Host *shost; |
| 666 | struct sas_internal *i; |
| 667 | |
| 668 | if (!scsi_is_sas_phy(dev)) |
| 669 | return 0; |
| 670 | shost = dev_to_shost(dev->parent); |
| 671 | |
| 672 | if (!shost->transportt) |
| 673 | return 0; |
| 674 | if (shost->transportt->host_attrs.ac.class != |
| 675 | &sas_host_class.class) |
| 676 | return 0; |
| 677 | |
| 678 | i = to_sas_internal(shost->transportt); |
| 679 | return &i->phy_attr_cont.ac == cont; |
| 680 | } |
| 681 | |
| 682 | static void sas_phy_release(struct device *dev) |
| 683 | { |
| 684 | struct sas_phy *phy = dev_to_phy(dev); |
| 685 | |
| 686 | put_device(dev->parent); |
| 687 | kfree(phy); |
| 688 | } |
| 689 | |
| 690 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 691 | * sas_phy_alloc - allocates and initialize a SAS PHY structure |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 692 | * @parent: Parent device |
Moore, Eric | d99ca41 | 2006-01-26 16:20:02 -0700 | [diff] [blame] | 693 | * @number: Phy index |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 694 | * |
| 695 | * Allocates an SAS PHY structure. It will be added in the device tree |
| 696 | * below the device specified by @parent, which has to be either a Scsi_Host |
| 697 | * or sas_rphy. |
| 698 | * |
| 699 | * Returns: |
| 700 | * SAS PHY allocated or %NULL if the allocation failed. |
| 701 | */ |
| 702 | struct sas_phy *sas_phy_alloc(struct device *parent, int number) |
| 703 | { |
| 704 | struct Scsi_Host *shost = dev_to_shost(parent); |
| 705 | struct sas_phy *phy; |
| 706 | |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 707 | phy = kzalloc(sizeof(*phy), GFP_KERNEL); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 708 | if (!phy) |
| 709 | return NULL; |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 710 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 711 | phy->number = number; |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 712 | phy->enabled = 1; |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 713 | |
| 714 | device_initialize(&phy->dev); |
| 715 | phy->dev.parent = get_device(parent); |
| 716 | phy->dev.release = sas_phy_release; |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 717 | INIT_LIST_HEAD(&phy->port_siblings); |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 718 | if (scsi_is_sas_expander_device(parent)) { |
| 719 | struct sas_rphy *rphy = dev_to_rphy(parent); |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 720 | dev_set_name(&phy->dev, "phy-%d:%d:%d", shost->host_no, |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 721 | rphy->scsi_target_id, number); |
| 722 | } else |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 723 | dev_set_name(&phy->dev, "phy-%d:%d", shost->host_no, number); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 724 | |
| 725 | transport_setup_device(&phy->dev); |
| 726 | |
| 727 | return phy; |
| 728 | } |
| 729 | EXPORT_SYMBOL(sas_phy_alloc); |
| 730 | |
| 731 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 732 | * sas_phy_add - add a SAS PHY to the device hierarchy |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 733 | * @phy: The PHY to be added |
| 734 | * |
| 735 | * Publishes a SAS PHY to the rest of the system. |
| 736 | */ |
| 737 | int sas_phy_add(struct sas_phy *phy) |
| 738 | { |
| 739 | int error; |
| 740 | |
| 741 | error = device_add(&phy->dev); |
| 742 | if (!error) { |
| 743 | transport_add_device(&phy->dev); |
| 744 | transport_configure_device(&phy->dev); |
| 745 | } |
| 746 | |
| 747 | return error; |
| 748 | } |
| 749 | EXPORT_SYMBOL(sas_phy_add); |
| 750 | |
| 751 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 752 | * sas_phy_free - free a SAS PHY |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 753 | * @phy: SAS PHY to free |
| 754 | * |
| 755 | * Frees the specified SAS PHY. |
| 756 | * |
| 757 | * Note: |
| 758 | * This function must only be called on a PHY that has not |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 759 | * successfully been added using sas_phy_add(). |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 760 | */ |
| 761 | void sas_phy_free(struct sas_phy *phy) |
| 762 | { |
| 763 | transport_destroy_device(&phy->dev); |
Mike Anderson | 92aab64 | 2006-03-27 09:37:28 -0800 | [diff] [blame] | 764 | put_device(&phy->dev); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 765 | } |
| 766 | EXPORT_SYMBOL(sas_phy_free); |
| 767 | |
| 768 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 769 | * sas_phy_delete - remove SAS PHY |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 770 | * @phy: SAS PHY to remove |
| 771 | * |
| 772 | * Removes the specified SAS PHY. If the SAS PHY has an |
| 773 | * associated remote PHY it is removed before. |
| 774 | */ |
| 775 | void |
| 776 | sas_phy_delete(struct sas_phy *phy) |
| 777 | { |
| 778 | struct device *dev = &phy->dev; |
| 779 | |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 780 | /* this happens if the phy is still part of a port when deleted */ |
| 781 | BUG_ON(!list_empty(&phy->port_siblings)); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 782 | |
| 783 | transport_remove_device(dev); |
| 784 | device_del(dev); |
| 785 | transport_destroy_device(dev); |
Mike Anderson | 92aab64 | 2006-03-27 09:37:28 -0800 | [diff] [blame] | 786 | put_device(dev); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 787 | } |
| 788 | EXPORT_SYMBOL(sas_phy_delete); |
| 789 | |
| 790 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 791 | * scsi_is_sas_phy - check if a struct device represents a SAS PHY |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 792 | * @dev: device to check |
| 793 | * |
| 794 | * Returns: |
| 795 | * %1 if the device represents a SAS PHY, %0 else |
| 796 | */ |
| 797 | int scsi_is_sas_phy(const struct device *dev) |
| 798 | { |
| 799 | return dev->release == sas_phy_release; |
| 800 | } |
| 801 | EXPORT_SYMBOL(scsi_is_sas_phy); |
| 802 | |
| 803 | /* |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 804 | * SAS Port attributes |
| 805 | */ |
| 806 | #define sas_port_show_simple(field, name, format_string, cast) \ |
| 807 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 808 | show_sas_port_##name(struct device *dev, \ |
| 809 | struct device_attribute *attr, char *buf) \ |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 810 | { \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 811 | struct sas_port *port = transport_class_to_sas_port(dev); \ |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 812 | \ |
| 813 | return snprintf(buf, 20, format_string, cast port->field); \ |
| 814 | } |
| 815 | |
| 816 | #define sas_port_simple_attr(field, name, format_string, type) \ |
| 817 | sas_port_show_simple(field, name, format_string, (type)) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 818 | static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL) |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 819 | |
| 820 | sas_port_simple_attr(num_phys, num_phys, "%d\n", int); |
| 821 | |
| 822 | static DECLARE_TRANSPORT_CLASS(sas_port_class, |
| 823 | "sas_port", NULL, NULL, NULL); |
| 824 | |
| 825 | static int sas_port_match(struct attribute_container *cont, struct device *dev) |
| 826 | { |
| 827 | struct Scsi_Host *shost; |
| 828 | struct sas_internal *i; |
| 829 | |
| 830 | if (!scsi_is_sas_port(dev)) |
| 831 | return 0; |
| 832 | shost = dev_to_shost(dev->parent); |
| 833 | |
| 834 | if (!shost->transportt) |
| 835 | return 0; |
| 836 | if (shost->transportt->host_attrs.ac.class != |
| 837 | &sas_host_class.class) |
| 838 | return 0; |
| 839 | |
| 840 | i = to_sas_internal(shost->transportt); |
| 841 | return &i->port_attr_cont.ac == cont; |
| 842 | } |
| 843 | |
| 844 | |
| 845 | static void sas_port_release(struct device *dev) |
| 846 | { |
| 847 | struct sas_port *port = dev_to_sas_port(dev); |
| 848 | |
| 849 | BUG_ON(!list_empty(&port->phy_list)); |
| 850 | |
| 851 | put_device(dev->parent); |
| 852 | kfree(port); |
| 853 | } |
| 854 | |
| 855 | static void sas_port_create_link(struct sas_port *port, |
| 856 | struct sas_phy *phy) |
| 857 | { |
Darrick J. Wong | 2143496 | 2007-01-26 14:08:46 -0800 | [diff] [blame] | 858 | int res; |
| 859 | |
| 860 | res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj, |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 861 | dev_name(&phy->dev)); |
Darrick J. Wong | 2143496 | 2007-01-26 14:08:46 -0800 | [diff] [blame] | 862 | if (res) |
| 863 | goto err; |
| 864 | res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port"); |
| 865 | if (res) |
| 866 | goto err; |
| 867 | return; |
| 868 | err: |
| 869 | printk(KERN_ERR "%s: Cannot create port links, err=%d\n", |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 870 | __func__, res); |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | static void sas_port_delete_link(struct sas_port *port, |
| 874 | struct sas_phy *phy) |
| 875 | { |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 876 | sysfs_remove_link(&port->dev.kobj, dev_name(&phy->dev)); |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 877 | sysfs_remove_link(&phy->dev.kobj, "port"); |
| 878 | } |
| 879 | |
| 880 | /** sas_port_alloc - allocate and initialize a SAS port structure |
| 881 | * |
| 882 | * @parent: parent device |
| 883 | * @port_id: port number |
| 884 | * |
| 885 | * Allocates a SAS port structure. It will be added to the device tree |
| 886 | * below the device specified by @parent which must be either a Scsi_Host |
| 887 | * or a sas_expander_device. |
| 888 | * |
| 889 | * Returns %NULL on error |
| 890 | */ |
| 891 | struct sas_port *sas_port_alloc(struct device *parent, int port_id) |
| 892 | { |
| 893 | struct Scsi_Host *shost = dev_to_shost(parent); |
| 894 | struct sas_port *port; |
| 895 | |
| 896 | port = kzalloc(sizeof(*port), GFP_KERNEL); |
| 897 | if (!port) |
| 898 | return NULL; |
| 899 | |
| 900 | port->port_identifier = port_id; |
| 901 | |
| 902 | device_initialize(&port->dev); |
| 903 | |
| 904 | port->dev.parent = get_device(parent); |
| 905 | port->dev.release = sas_port_release; |
| 906 | |
| 907 | mutex_init(&port->phy_list_mutex); |
| 908 | INIT_LIST_HEAD(&port->phy_list); |
| 909 | |
| 910 | if (scsi_is_sas_expander_device(parent)) { |
| 911 | struct sas_rphy *rphy = dev_to_rphy(parent); |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 912 | dev_set_name(&port->dev, "port-%d:%d:%d", shost->host_no, |
| 913 | rphy->scsi_target_id, port->port_identifier); |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 914 | } else |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 915 | dev_set_name(&port->dev, "port-%d:%d", shost->host_no, |
| 916 | port->port_identifier); |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 917 | |
| 918 | transport_setup_device(&port->dev); |
| 919 | |
| 920 | return port; |
| 921 | } |
| 922 | EXPORT_SYMBOL(sas_port_alloc); |
| 923 | |
James Bottomley | c9fefeb | 2006-07-02 11:10:18 -0500 | [diff] [blame] | 924 | /** sas_port_alloc_num - allocate and initialize a SAS port structure |
| 925 | * |
| 926 | * @parent: parent device |
| 927 | * |
| 928 | * Allocates a SAS port structure and a number to go with it. This |
| 929 | * interface is really for adapters where the port number has no |
| 930 | * meansing, so the sas class should manage them. It will be added to |
| 931 | * the device tree below the device specified by @parent which must be |
| 932 | * either a Scsi_Host or a sas_expander_device. |
| 933 | * |
| 934 | * Returns %NULL on error |
| 935 | */ |
| 936 | struct sas_port *sas_port_alloc_num(struct device *parent) |
| 937 | { |
| 938 | int index; |
| 939 | struct Scsi_Host *shost = dev_to_shost(parent); |
| 940 | struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); |
| 941 | |
| 942 | /* FIXME: use idr for this eventually */ |
| 943 | mutex_lock(&sas_host->lock); |
| 944 | if (scsi_is_sas_expander_device(parent)) { |
| 945 | struct sas_rphy *rphy = dev_to_rphy(parent); |
| 946 | struct sas_expander_device *exp = rphy_to_expander_device(rphy); |
| 947 | |
| 948 | index = exp->next_port_id++; |
| 949 | } else |
| 950 | index = sas_host->next_port_id++; |
| 951 | mutex_unlock(&sas_host->lock); |
| 952 | return sas_port_alloc(parent, index); |
| 953 | } |
| 954 | EXPORT_SYMBOL(sas_port_alloc_num); |
| 955 | |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 956 | /** |
| 957 | * sas_port_add - add a SAS port to the device hierarchy |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 958 | * @port: port to be added |
| 959 | * |
| 960 | * publishes a port to the rest of the system |
| 961 | */ |
| 962 | int sas_port_add(struct sas_port *port) |
| 963 | { |
| 964 | int error; |
| 965 | |
| 966 | /* No phys should be added until this is made visible */ |
| 967 | BUG_ON(!list_empty(&port->phy_list)); |
| 968 | |
| 969 | error = device_add(&port->dev); |
| 970 | |
| 971 | if (error) |
| 972 | return error; |
| 973 | |
| 974 | transport_add_device(&port->dev); |
| 975 | transport_configure_device(&port->dev); |
| 976 | |
| 977 | return 0; |
| 978 | } |
| 979 | EXPORT_SYMBOL(sas_port_add); |
| 980 | |
| 981 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 982 | * sas_port_free - free a SAS PORT |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 983 | * @port: SAS PORT to free |
| 984 | * |
| 985 | * Frees the specified SAS PORT. |
| 986 | * |
| 987 | * Note: |
| 988 | * This function must only be called on a PORT that has not |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 989 | * successfully been added using sas_port_add(). |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 990 | */ |
| 991 | void sas_port_free(struct sas_port *port) |
| 992 | { |
| 993 | transport_destroy_device(&port->dev); |
| 994 | put_device(&port->dev); |
| 995 | } |
| 996 | EXPORT_SYMBOL(sas_port_free); |
| 997 | |
| 998 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 999 | * sas_port_delete - remove SAS PORT |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1000 | * @port: SAS PORT to remove |
| 1001 | * |
| 1002 | * Removes the specified SAS PORT. If the SAS PORT has an |
| 1003 | * associated phys, unlink them from the port as well. |
| 1004 | */ |
| 1005 | void sas_port_delete(struct sas_port *port) |
| 1006 | { |
| 1007 | struct device *dev = &port->dev; |
| 1008 | struct sas_phy *phy, *tmp_phy; |
| 1009 | |
| 1010 | if (port->rphy) { |
| 1011 | sas_rphy_delete(port->rphy); |
| 1012 | port->rphy = NULL; |
| 1013 | } |
| 1014 | |
| 1015 | mutex_lock(&port->phy_list_mutex); |
| 1016 | list_for_each_entry_safe(phy, tmp_phy, &port->phy_list, |
| 1017 | port_siblings) { |
| 1018 | sas_port_delete_link(port, phy); |
| 1019 | list_del_init(&phy->port_siblings); |
| 1020 | } |
| 1021 | mutex_unlock(&port->phy_list_mutex); |
| 1022 | |
James Bottomley | a0e1b6e | 2006-07-09 12:38:19 -0500 | [diff] [blame] | 1023 | if (port->is_backlink) { |
| 1024 | struct device *parent = port->dev.parent; |
| 1025 | |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 1026 | sysfs_remove_link(&port->dev.kobj, dev_name(parent)); |
James Bottomley | a0e1b6e | 2006-07-09 12:38:19 -0500 | [diff] [blame] | 1027 | port->is_backlink = 0; |
| 1028 | } |
| 1029 | |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1030 | transport_remove_device(dev); |
| 1031 | device_del(dev); |
| 1032 | transport_destroy_device(dev); |
| 1033 | put_device(dev); |
| 1034 | } |
| 1035 | EXPORT_SYMBOL(sas_port_delete); |
| 1036 | |
| 1037 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1038 | * scsi_is_sas_port - check if a struct device represents a SAS port |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1039 | * @dev: device to check |
| 1040 | * |
| 1041 | * Returns: |
| 1042 | * %1 if the device represents a SAS Port, %0 else |
| 1043 | */ |
| 1044 | int scsi_is_sas_port(const struct device *dev) |
| 1045 | { |
| 1046 | return dev->release == sas_port_release; |
| 1047 | } |
| 1048 | EXPORT_SYMBOL(scsi_is_sas_port); |
| 1049 | |
| 1050 | /** |
| 1051 | * sas_port_add_phy - add another phy to a port to form a wide port |
| 1052 | * @port: port to add the phy to |
| 1053 | * @phy: phy to add |
| 1054 | * |
| 1055 | * When a port is initially created, it is empty (has no phys). All |
| 1056 | * ports must have at least one phy to operated, and all wide ports |
| 1057 | * must have at least two. The current code makes no difference |
| 1058 | * between ports and wide ports, but the only object that can be |
| 1059 | * connected to a remote device is a port, so ports must be formed on |
| 1060 | * all devices with phys if they're connected to anything. |
| 1061 | */ |
| 1062 | void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy) |
| 1063 | { |
| 1064 | mutex_lock(&port->phy_list_mutex); |
| 1065 | if (unlikely(!list_empty(&phy->port_siblings))) { |
| 1066 | /* make sure we're already on this port */ |
| 1067 | struct sas_phy *tmp; |
| 1068 | |
| 1069 | list_for_each_entry(tmp, &port->phy_list, port_siblings) |
| 1070 | if (tmp == phy) |
| 1071 | break; |
| 1072 | /* If this trips, you added a phy that was already |
| 1073 | * part of a different port */ |
| 1074 | if (unlikely(tmp != phy)) { |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 1075 | dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n", |
| 1076 | dev_name(&phy->dev)); |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1077 | BUG(); |
| 1078 | } |
| 1079 | } else { |
| 1080 | sas_port_create_link(port, phy); |
| 1081 | list_add_tail(&phy->port_siblings, &port->phy_list); |
| 1082 | port->num_phys++; |
| 1083 | } |
| 1084 | mutex_unlock(&port->phy_list_mutex); |
| 1085 | } |
| 1086 | EXPORT_SYMBOL(sas_port_add_phy); |
| 1087 | |
| 1088 | /** |
| 1089 | * sas_port_delete_phy - remove a phy from a port or wide port |
| 1090 | * @port: port to remove the phy from |
| 1091 | * @phy: phy to remove |
| 1092 | * |
| 1093 | * This operation is used for tearing down ports again. It must be |
| 1094 | * done to every port or wide port before calling sas_port_delete. |
| 1095 | */ |
| 1096 | void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy) |
| 1097 | { |
| 1098 | mutex_lock(&port->phy_list_mutex); |
| 1099 | sas_port_delete_link(port, phy); |
| 1100 | list_del_init(&phy->port_siblings); |
| 1101 | port->num_phys--; |
| 1102 | mutex_unlock(&port->phy_list_mutex); |
| 1103 | } |
| 1104 | EXPORT_SYMBOL(sas_port_delete_phy); |
| 1105 | |
James Bottomley | a0e1b6e | 2006-07-09 12:38:19 -0500 | [diff] [blame] | 1106 | void sas_port_mark_backlink(struct sas_port *port) |
| 1107 | { |
Darrick J. Wong | 2143496 | 2007-01-26 14:08:46 -0800 | [diff] [blame] | 1108 | int res; |
James Bottomley | a0e1b6e | 2006-07-09 12:38:19 -0500 | [diff] [blame] | 1109 | struct device *parent = port->dev.parent->parent->parent; |
| 1110 | |
| 1111 | if (port->is_backlink) |
| 1112 | return; |
| 1113 | port->is_backlink = 1; |
Darrick J. Wong | 2143496 | 2007-01-26 14:08:46 -0800 | [diff] [blame] | 1114 | res = sysfs_create_link(&port->dev.kobj, &parent->kobj, |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 1115 | dev_name(parent)); |
Darrick J. Wong | 2143496 | 2007-01-26 14:08:46 -0800 | [diff] [blame] | 1116 | if (res) |
| 1117 | goto err; |
| 1118 | return; |
| 1119 | err: |
| 1120 | printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n", |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 1121 | __func__, res); |
James Bottomley | a0e1b6e | 2006-07-09 12:38:19 -0500 | [diff] [blame] | 1122 | |
| 1123 | } |
| 1124 | EXPORT_SYMBOL(sas_port_mark_backlink); |
| 1125 | |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1126 | /* |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1127 | * SAS remote PHY attributes. |
| 1128 | */ |
| 1129 | |
| 1130 | #define sas_rphy_show_simple(field, name, format_string, cast) \ |
| 1131 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1132 | show_sas_rphy_##name(struct device *dev, \ |
| 1133 | struct device_attribute *attr, char *buf) \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1134 | { \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1135 | struct sas_rphy *rphy = transport_class_to_rphy(dev); \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1136 | \ |
| 1137 | return snprintf(buf, 20, format_string, cast rphy->field); \ |
| 1138 | } |
| 1139 | |
| 1140 | #define sas_rphy_simple_attr(field, name, format_string, type) \ |
| 1141 | sas_rphy_show_simple(field, name, format_string, (type)) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1142 | static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1143 | show_sas_rphy_##name, NULL) |
| 1144 | |
| 1145 | #define sas_rphy_show_protocol(field, name) \ |
| 1146 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1147 | show_sas_rphy_##name(struct device *dev, \ |
| 1148 | struct device_attribute *attr, char *buf) \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1149 | { \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1150 | struct sas_rphy *rphy = transport_class_to_rphy(dev); \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1151 | \ |
| 1152 | if (!rphy->field) \ |
| 1153 | return snprintf(buf, 20, "none\n"); \ |
| 1154 | return get_sas_protocol_names(rphy->field, buf); \ |
| 1155 | } |
| 1156 | |
| 1157 | #define sas_rphy_protocol_attr(field, name) \ |
| 1158 | sas_rphy_show_protocol(field, name) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1159 | static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \ |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1160 | show_sas_rphy_##name, NULL) |
| 1161 | |
| 1162 | static ssize_t |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1163 | show_sas_rphy_device_type(struct device *dev, |
| 1164 | struct device_attribute *attr, char *buf) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1165 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1166 | struct sas_rphy *rphy = transport_class_to_rphy(dev); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1167 | |
| 1168 | if (!rphy->identify.device_type) |
| 1169 | return snprintf(buf, 20, "none\n"); |
| 1170 | return get_sas_device_type_names( |
| 1171 | rphy->identify.device_type, buf); |
| 1172 | } |
| 1173 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1174 | static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO, |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1175 | show_sas_rphy_device_type, NULL); |
| 1176 | |
Christoph Hellwig | a012564 | 2006-02-16 13:31:47 +0100 | [diff] [blame] | 1177 | static ssize_t |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1178 | show_sas_rphy_enclosure_identifier(struct device *dev, |
| 1179 | struct device_attribute *attr, char *buf) |
Christoph Hellwig | a012564 | 2006-02-16 13:31:47 +0100 | [diff] [blame] | 1180 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1181 | struct sas_rphy *rphy = transport_class_to_rphy(dev); |
Christoph Hellwig | a012564 | 2006-02-16 13:31:47 +0100 | [diff] [blame] | 1182 | struct sas_phy *phy = dev_to_phy(rphy->dev.parent); |
| 1183 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); |
| 1184 | struct sas_internal *i = to_sas_internal(shost->transportt); |
| 1185 | u64 identifier; |
| 1186 | int error; |
| 1187 | |
| 1188 | /* |
| 1189 | * Only devices behind an expander are supported, because the |
| 1190 | * enclosure identifier is a SMP feature. |
| 1191 | */ |
James Bottomley | f4ad7b5 | 2006-08-25 13:48:18 -0500 | [diff] [blame] | 1192 | if (scsi_is_sas_phy_local(phy)) |
Christoph Hellwig | a012564 | 2006-02-16 13:31:47 +0100 | [diff] [blame] | 1193 | return -EINVAL; |
| 1194 | |
| 1195 | error = i->f->get_enclosure_identifier(rphy, &identifier); |
| 1196 | if (error) |
| 1197 | return error; |
| 1198 | return sprintf(buf, "0x%llx\n", (unsigned long long)identifier); |
| 1199 | } |
| 1200 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1201 | static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO, |
Christoph Hellwig | a012564 | 2006-02-16 13:31:47 +0100 | [diff] [blame] | 1202 | show_sas_rphy_enclosure_identifier, NULL); |
| 1203 | |
| 1204 | static ssize_t |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1205 | show_sas_rphy_bay_identifier(struct device *dev, |
| 1206 | struct device_attribute *attr, char *buf) |
Christoph Hellwig | a012564 | 2006-02-16 13:31:47 +0100 | [diff] [blame] | 1207 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1208 | struct sas_rphy *rphy = transport_class_to_rphy(dev); |
Christoph Hellwig | a012564 | 2006-02-16 13:31:47 +0100 | [diff] [blame] | 1209 | struct sas_phy *phy = dev_to_phy(rphy->dev.parent); |
| 1210 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); |
| 1211 | struct sas_internal *i = to_sas_internal(shost->transportt); |
| 1212 | int val; |
| 1213 | |
James Bottomley | f4ad7b5 | 2006-08-25 13:48:18 -0500 | [diff] [blame] | 1214 | if (scsi_is_sas_phy_local(phy)) |
Christoph Hellwig | a012564 | 2006-02-16 13:31:47 +0100 | [diff] [blame] | 1215 | return -EINVAL; |
| 1216 | |
| 1217 | val = i->f->get_bay_identifier(rphy); |
| 1218 | if (val < 0) |
| 1219 | return val; |
| 1220 | return sprintf(buf, "%d\n", val); |
| 1221 | } |
| 1222 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1223 | static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO, |
Christoph Hellwig | a012564 | 2006-02-16 13:31:47 +0100 | [diff] [blame] | 1224 | show_sas_rphy_bay_identifier, NULL); |
| 1225 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1226 | sas_rphy_protocol_attr(identify.initiator_port_protocols, |
| 1227 | initiator_port_protocols); |
| 1228 | sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols); |
| 1229 | sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n", |
| 1230 | unsigned long long); |
| 1231 | sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8); |
| 1232 | |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1233 | /* only need 8 bytes of data plus header (4 or 8) */ |
| 1234 | #define BUF_SIZE 64 |
| 1235 | |
| 1236 | int sas_read_port_mode_page(struct scsi_device *sdev) |
| 1237 | { |
| 1238 | char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata; |
James Bottomley | 0f88009 | 2010-01-18 10:14:51 -0600 | [diff] [blame] | 1239 | struct sas_end_device *rdev = sas_sdev_to_rdev(sdev); |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1240 | struct scsi_mode_data mode_data; |
| 1241 | int res, error; |
| 1242 | |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1243 | if (!buffer) |
| 1244 | return -ENOMEM; |
| 1245 | |
| 1246 | res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3, |
| 1247 | &mode_data, NULL); |
| 1248 | |
| 1249 | error = -EINVAL; |
| 1250 | if (!scsi_status_is_good(res)) |
| 1251 | goto out; |
| 1252 | |
| 1253 | msdata = buffer + mode_data.header_length + |
| 1254 | mode_data.block_descriptor_length; |
| 1255 | |
| 1256 | if (msdata - buffer > BUF_SIZE - 8) |
| 1257 | goto out; |
| 1258 | |
| 1259 | error = 0; |
| 1260 | |
| 1261 | rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0; |
| 1262 | rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5]; |
| 1263 | rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7]; |
| 1264 | |
| 1265 | out: |
| 1266 | kfree(buffer); |
| 1267 | return error; |
| 1268 | } |
| 1269 | EXPORT_SYMBOL(sas_read_port_mode_page); |
| 1270 | |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1271 | static DECLARE_TRANSPORT_CLASS(sas_end_dev_class, |
| 1272 | "sas_end_device", NULL, NULL, NULL); |
| 1273 | |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1274 | #define sas_end_dev_show_simple(field, name, format_string, cast) \ |
| 1275 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1276 | show_sas_end_dev_##name(struct device *dev, \ |
| 1277 | struct device_attribute *attr, char *buf) \ |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1278 | { \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1279 | struct sas_rphy *rphy = transport_class_to_rphy(dev); \ |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1280 | struct sas_end_device *rdev = rphy_to_end_device(rphy); \ |
| 1281 | \ |
| 1282 | return snprintf(buf, 20, format_string, cast rdev->field); \ |
| 1283 | } |
| 1284 | |
| 1285 | #define sas_end_dev_simple_attr(field, name, format_string, type) \ |
| 1286 | sas_end_dev_show_simple(field, name, format_string, (type)) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1287 | static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO, \ |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1288 | show_sas_end_dev_##name, NULL) |
| 1289 | |
| 1290 | sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int); |
| 1291 | sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout, |
| 1292 | "%d\n", int); |
| 1293 | sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout, |
| 1294 | "%d\n", int); |
James Bottomley | 0f88009 | 2010-01-18 10:14:51 -0600 | [diff] [blame] | 1295 | sas_end_dev_simple_attr(tlr_supported, tlr_supported, |
| 1296 | "%d\n", int); |
| 1297 | sas_end_dev_simple_attr(tlr_enabled, tlr_enabled, |
| 1298 | "%d\n", int); |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1299 | |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1300 | static DECLARE_TRANSPORT_CLASS(sas_expander_class, |
| 1301 | "sas_expander", NULL, NULL, NULL); |
| 1302 | |
| 1303 | #define sas_expander_show_simple(field, name, format_string, cast) \ |
| 1304 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1305 | show_sas_expander_##name(struct device *dev, \ |
| 1306 | struct device_attribute *attr, char *buf) \ |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1307 | { \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1308 | struct sas_rphy *rphy = transport_class_to_rphy(dev); \ |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1309 | struct sas_expander_device *edev = rphy_to_expander_device(rphy); \ |
| 1310 | \ |
| 1311 | return snprintf(buf, 20, format_string, cast edev->field); \ |
| 1312 | } |
| 1313 | |
| 1314 | #define sas_expander_simple_attr(field, name, format_string, type) \ |
| 1315 | sas_expander_show_simple(field, name, format_string, (type)) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1316 | static SAS_DEVICE_ATTR(expander, name, S_IRUGO, \ |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1317 | show_sas_expander_##name, NULL) |
| 1318 | |
| 1319 | sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *); |
| 1320 | sas_expander_simple_attr(product_id, product_id, "%s\n", char *); |
| 1321 | sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *); |
| 1322 | sas_expander_simple_attr(component_vendor_id, component_vendor_id, |
| 1323 | "%s\n", char *); |
| 1324 | sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int); |
| 1325 | sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n", |
| 1326 | unsigned int); |
| 1327 | sas_expander_simple_attr(level, level, "%d\n", int); |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1328 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1329 | static DECLARE_TRANSPORT_CLASS(sas_rphy_class, |
James Bottomley | 2f8600d | 2006-03-18 15:00:50 -0600 | [diff] [blame] | 1330 | "sas_device", NULL, NULL, NULL); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1331 | |
| 1332 | static int sas_rphy_match(struct attribute_container *cont, struct device *dev) |
| 1333 | { |
| 1334 | struct Scsi_Host *shost; |
| 1335 | struct sas_internal *i; |
| 1336 | |
| 1337 | if (!scsi_is_sas_rphy(dev)) |
| 1338 | return 0; |
| 1339 | shost = dev_to_shost(dev->parent->parent); |
| 1340 | |
| 1341 | if (!shost->transportt) |
| 1342 | return 0; |
| 1343 | if (shost->transportt->host_attrs.ac.class != |
| 1344 | &sas_host_class.class) |
| 1345 | return 0; |
| 1346 | |
| 1347 | i = to_sas_internal(shost->transportt); |
| 1348 | return &i->rphy_attr_cont.ac == cont; |
| 1349 | } |
| 1350 | |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1351 | static int sas_end_dev_match(struct attribute_container *cont, |
| 1352 | struct device *dev) |
| 1353 | { |
| 1354 | struct Scsi_Host *shost; |
| 1355 | struct sas_internal *i; |
| 1356 | struct sas_rphy *rphy; |
| 1357 | |
| 1358 | if (!scsi_is_sas_rphy(dev)) |
| 1359 | return 0; |
| 1360 | shost = dev_to_shost(dev->parent->parent); |
| 1361 | rphy = dev_to_rphy(dev); |
| 1362 | |
| 1363 | if (!shost->transportt) |
| 1364 | return 0; |
| 1365 | if (shost->transportt->host_attrs.ac.class != |
| 1366 | &sas_host_class.class) |
| 1367 | return 0; |
| 1368 | |
| 1369 | i = to_sas_internal(shost->transportt); |
| 1370 | return &i->end_dev_attr_cont.ac == cont && |
James Bottomley | 2f8600d | 2006-03-18 15:00:50 -0600 | [diff] [blame] | 1371 | rphy->identify.device_type == SAS_END_DEVICE; |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1372 | } |
| 1373 | |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1374 | static int sas_expander_match(struct attribute_container *cont, |
| 1375 | struct device *dev) |
| 1376 | { |
| 1377 | struct Scsi_Host *shost; |
| 1378 | struct sas_internal *i; |
| 1379 | struct sas_rphy *rphy; |
| 1380 | |
| 1381 | if (!scsi_is_sas_rphy(dev)) |
| 1382 | return 0; |
| 1383 | shost = dev_to_shost(dev->parent->parent); |
| 1384 | rphy = dev_to_rphy(dev); |
| 1385 | |
| 1386 | if (!shost->transportt) |
| 1387 | return 0; |
| 1388 | if (shost->transportt->host_attrs.ac.class != |
| 1389 | &sas_host_class.class) |
| 1390 | return 0; |
| 1391 | |
| 1392 | i = to_sas_internal(shost->transportt); |
| 1393 | return &i->expander_attr_cont.ac == cont && |
| 1394 | (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE || |
James Bottomley | 2f8600d | 2006-03-18 15:00:50 -0600 | [diff] [blame] | 1395 | rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE); |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1396 | } |
| 1397 | |
James Bottomley | 2f8600d | 2006-03-18 15:00:50 -0600 | [diff] [blame] | 1398 | static void sas_expander_release(struct device *dev) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1399 | { |
| 1400 | struct sas_rphy *rphy = dev_to_rphy(dev); |
James Bottomley | 2f8600d | 2006-03-18 15:00:50 -0600 | [diff] [blame] | 1401 | struct sas_expander_device *edev = rphy_to_expander_device(rphy); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1402 | |
FUJITA Tomonori | 93c20a5 | 2008-04-19 00:43:15 +0900 | [diff] [blame] | 1403 | if (rphy->q) |
| 1404 | blk_cleanup_queue(rphy->q); |
| 1405 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1406 | put_device(dev->parent); |
James Bottomley | 2f8600d | 2006-03-18 15:00:50 -0600 | [diff] [blame] | 1407 | kfree(edev); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1408 | } |
| 1409 | |
James Bottomley | 2f8600d | 2006-03-18 15:00:50 -0600 | [diff] [blame] | 1410 | static void sas_end_device_release(struct device *dev) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1411 | { |
James Bottomley | 2f8600d | 2006-03-18 15:00:50 -0600 | [diff] [blame] | 1412 | struct sas_rphy *rphy = dev_to_rphy(dev); |
| 1413 | struct sas_end_device *edev = rphy_to_end_device(rphy); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1414 | |
FUJITA Tomonori | 93c20a5 | 2008-04-19 00:43:15 +0900 | [diff] [blame] | 1415 | if (rphy->q) |
| 1416 | blk_cleanup_queue(rphy->q); |
| 1417 | |
James Bottomley | 2f8600d | 2006-03-18 15:00:50 -0600 | [diff] [blame] | 1418 | put_device(dev->parent); |
| 1419 | kfree(edev); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1420 | } |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1421 | |
| 1422 | /** |
James Bottomley | c5943d3 | 2006-06-12 09:09:18 -0500 | [diff] [blame] | 1423 | * sas_rphy_initialize - common rphy intialization |
| 1424 | * @rphy: rphy to initialise |
| 1425 | * |
| 1426 | * Used by both sas_end_device_alloc() and sas_expander_alloc() to |
| 1427 | * initialise the common rphy component of each. |
| 1428 | */ |
| 1429 | static void sas_rphy_initialize(struct sas_rphy *rphy) |
| 1430 | { |
| 1431 | INIT_LIST_HEAD(&rphy->list); |
| 1432 | } |
| 1433 | |
| 1434 | /** |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1435 | * sas_end_device_alloc - allocate an rphy for an end device |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1436 | * @parent: which port |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1437 | * |
| 1438 | * Allocates an SAS remote PHY structure, connected to @parent. |
| 1439 | * |
| 1440 | * Returns: |
| 1441 | * SAS PHY allocated or %NULL if the allocation failed. |
| 1442 | */ |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1443 | struct sas_rphy *sas_end_device_alloc(struct sas_port *parent) |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1444 | { |
| 1445 | struct Scsi_Host *shost = dev_to_shost(&parent->dev); |
| 1446 | struct sas_end_device *rdev; |
| 1447 | |
| 1448 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
| 1449 | if (!rdev) { |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1450 | return NULL; |
| 1451 | } |
| 1452 | |
| 1453 | device_initialize(&rdev->rphy.dev); |
| 1454 | rdev->rphy.dev.parent = get_device(&parent->dev); |
James Bottomley | 2f8600d | 2006-03-18 15:00:50 -0600 | [diff] [blame] | 1455 | rdev->rphy.dev.release = sas_end_device_release; |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1456 | if (scsi_is_sas_expander_device(parent->dev.parent)) { |
| 1457 | struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent); |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 1458 | dev_set_name(&rdev->rphy.dev, "end_device-%d:%d:%d", |
| 1459 | shost->host_no, rphy->scsi_target_id, |
| 1460 | parent->port_identifier); |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1461 | } else |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 1462 | dev_set_name(&rdev->rphy.dev, "end_device-%d:%d", |
| 1463 | shost->host_no, parent->port_identifier); |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1464 | rdev->rphy.identify.device_type = SAS_END_DEVICE; |
James Bottomley | c5943d3 | 2006-06-12 09:09:18 -0500 | [diff] [blame] | 1465 | sas_rphy_initialize(&rdev->rphy); |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1466 | transport_setup_device(&rdev->rphy.dev); |
| 1467 | |
| 1468 | return &rdev->rphy; |
| 1469 | } |
| 1470 | EXPORT_SYMBOL(sas_end_device_alloc); |
| 1471 | |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1472 | /** |
| 1473 | * sas_expander_alloc - allocate an rphy for an end device |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1474 | * @parent: which port |
| 1475 | * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1476 | * |
| 1477 | * Allocates an SAS remote PHY structure, connected to @parent. |
| 1478 | * |
| 1479 | * Returns: |
| 1480 | * SAS PHY allocated or %NULL if the allocation failed. |
| 1481 | */ |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1482 | struct sas_rphy *sas_expander_alloc(struct sas_port *parent, |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1483 | enum sas_device_type type) |
| 1484 | { |
| 1485 | struct Scsi_Host *shost = dev_to_shost(&parent->dev); |
| 1486 | struct sas_expander_device *rdev; |
| 1487 | struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); |
| 1488 | |
| 1489 | BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE && |
| 1490 | type != SAS_FANOUT_EXPANDER_DEVICE); |
| 1491 | |
| 1492 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
| 1493 | if (!rdev) { |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1494 | return NULL; |
| 1495 | } |
| 1496 | |
| 1497 | device_initialize(&rdev->rphy.dev); |
| 1498 | rdev->rphy.dev.parent = get_device(&parent->dev); |
James Bottomley | 2f8600d | 2006-03-18 15:00:50 -0600 | [diff] [blame] | 1499 | rdev->rphy.dev.release = sas_expander_release; |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1500 | mutex_lock(&sas_host->lock); |
| 1501 | rdev->rphy.scsi_target_id = sas_host->next_expander_id++; |
| 1502 | mutex_unlock(&sas_host->lock); |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 1503 | dev_set_name(&rdev->rphy.dev, "expander-%d:%d", |
| 1504 | shost->host_no, rdev->rphy.scsi_target_id); |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1505 | rdev->rphy.identify.device_type = type; |
James Bottomley | c5943d3 | 2006-06-12 09:09:18 -0500 | [diff] [blame] | 1506 | sas_rphy_initialize(&rdev->rphy); |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1507 | transport_setup_device(&rdev->rphy.dev); |
| 1508 | |
| 1509 | return &rdev->rphy; |
| 1510 | } |
| 1511 | EXPORT_SYMBOL(sas_expander_alloc); |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1512 | |
| 1513 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1514 | * sas_rphy_add - add a SAS remote PHY to the device hierarchy |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1515 | * @rphy: The remote PHY to be added |
| 1516 | * |
| 1517 | * Publishes a SAS remote PHY to the rest of the system. |
| 1518 | */ |
| 1519 | int sas_rphy_add(struct sas_rphy *rphy) |
| 1520 | { |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1521 | struct sas_port *parent = dev_to_sas_port(rphy->dev.parent); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1522 | struct Scsi_Host *shost = dev_to_shost(parent->dev.parent); |
| 1523 | struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); |
| 1524 | struct sas_identify *identify = &rphy->identify; |
| 1525 | int error; |
| 1526 | |
| 1527 | if (parent->rphy) |
| 1528 | return -ENXIO; |
| 1529 | parent->rphy = rphy; |
| 1530 | |
| 1531 | error = device_add(&rphy->dev); |
| 1532 | if (error) |
| 1533 | return error; |
| 1534 | transport_add_device(&rphy->dev); |
| 1535 | transport_configure_device(&rphy->dev); |
James Bottomley | 39dca55 | 2007-07-20 18:22:17 -0500 | [diff] [blame] | 1536 | if (sas_bsg_initialize(shost, rphy)) |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 1537 | printk("fail to a bsg device %s\n", dev_name(&rphy->dev)); |
James Bottomley | 39dca55 | 2007-07-20 18:22:17 -0500 | [diff] [blame] | 1538 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1539 | |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 1540 | mutex_lock(&sas_host->lock); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1541 | list_add_tail(&rphy->list, &sas_host->rphy_list); |
| 1542 | if (identify->device_type == SAS_END_DEVICE && |
| 1543 | (identify->target_port_protocols & |
| 1544 | (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA))) |
| 1545 | rphy->scsi_target_id = sas_host->next_target_id++; |
James Bottomley | 7676f83 | 2006-04-14 09:47:59 -0500 | [diff] [blame] | 1546 | else if (identify->device_type == SAS_END_DEVICE) |
| 1547 | rphy->scsi_target_id = -1; |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 1548 | mutex_unlock(&sas_host->lock); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1549 | |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1550 | if (identify->device_type == SAS_END_DEVICE && |
| 1551 | rphy->scsi_target_id != -1) { |
James Bottomley | e8bf394 | 2006-07-11 17:49:34 -0400 | [diff] [blame] | 1552 | scsi_scan_target(&rphy->dev, 0, |
Darrick J. Wong | c8490f3 | 2007-01-11 14:15:09 -0800 | [diff] [blame] | 1553 | rphy->scsi_target_id, SCAN_WILD_CARD, 0); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | return 0; |
| 1557 | } |
| 1558 | EXPORT_SYMBOL(sas_rphy_add); |
| 1559 | |
| 1560 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1561 | * sas_rphy_free - free a SAS remote PHY |
| 1562 | * @rphy: SAS remote PHY to free |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1563 | * |
| 1564 | * Frees the specified SAS remote PHY. |
| 1565 | * |
| 1566 | * Note: |
| 1567 | * This function must only be called on a remote |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1568 | * PHY that has not successfully been added using |
Darrick J. Wong | 6f63caa | 2007-01-26 14:08:43 -0800 | [diff] [blame] | 1569 | * sas_rphy_add() (or has been sas_rphy_remove()'d) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1570 | */ |
| 1571 | void sas_rphy_free(struct sas_rphy *rphy) |
| 1572 | { |
Mike Anderson | 92aab64 | 2006-03-27 09:37:28 -0800 | [diff] [blame] | 1573 | struct device *dev = &rphy->dev; |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1574 | struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent); |
| 1575 | struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); |
| 1576 | |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 1577 | mutex_lock(&sas_host->lock); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1578 | list_del(&rphy->list); |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 1579 | mutex_unlock(&sas_host->lock); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1580 | |
James Bottomley | b6aff66 | 2007-07-20 11:10:05 -0500 | [diff] [blame] | 1581 | sas_bsg_remove(shost, rphy); |
| 1582 | |
Mike Anderson | 92aab64 | 2006-03-27 09:37:28 -0800 | [diff] [blame] | 1583 | transport_destroy_device(dev); |
James Bottomley | 2f8600d | 2006-03-18 15:00:50 -0600 | [diff] [blame] | 1584 | |
Mike Anderson | 92aab64 | 2006-03-27 09:37:28 -0800 | [diff] [blame] | 1585 | put_device(dev); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1586 | } |
| 1587 | EXPORT_SYMBOL(sas_rphy_free); |
| 1588 | |
| 1589 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1590 | * sas_rphy_delete - remove and free SAS remote PHY |
Darrick J. Wong | 6f63caa | 2007-01-26 14:08:43 -0800 | [diff] [blame] | 1591 | * @rphy: SAS remote PHY to remove and free |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1592 | * |
Darrick J. Wong | 6f63caa | 2007-01-26 14:08:43 -0800 | [diff] [blame] | 1593 | * Removes the specified SAS remote PHY and frees it. |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1594 | */ |
| 1595 | void |
| 1596 | sas_rphy_delete(struct sas_rphy *rphy) |
| 1597 | { |
Darrick J. Wong | 6f63caa | 2007-01-26 14:08:43 -0800 | [diff] [blame] | 1598 | sas_rphy_remove(rphy); |
| 1599 | sas_rphy_free(rphy); |
| 1600 | } |
| 1601 | EXPORT_SYMBOL(sas_rphy_delete); |
| 1602 | |
| 1603 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1604 | * sas_rphy_remove - remove SAS remote PHY |
Darrick J. Wong | 6f63caa | 2007-01-26 14:08:43 -0800 | [diff] [blame] | 1605 | * @rphy: SAS remote phy to remove |
| 1606 | * |
| 1607 | * Removes the specified SAS remote PHY. |
| 1608 | */ |
| 1609 | void |
| 1610 | sas_rphy_remove(struct sas_rphy *rphy) |
| 1611 | { |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1612 | struct device *dev = &rphy->dev; |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1613 | struct sas_port *parent = dev_to_sas_port(dev->parent); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1614 | |
Christoph Hellwig | d405423 | 2006-01-04 13:45:20 +0100 | [diff] [blame] | 1615 | switch (rphy->identify.device_type) { |
| 1616 | case SAS_END_DEVICE: |
| 1617 | scsi_remove_target(dev); |
| 1618 | break; |
| 1619 | case SAS_EDGE_EXPANDER_DEVICE: |
| 1620 | case SAS_FANOUT_EXPANDER_DEVICE: |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1621 | sas_remove_children(dev); |
Christoph Hellwig | d405423 | 2006-01-04 13:45:20 +0100 | [diff] [blame] | 1622 | break; |
| 1623 | default: |
| 1624 | break; |
| 1625 | } |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1626 | |
Christoph Hellwig | fe8b230 | 2005-09-25 23:10:33 +0200 | [diff] [blame] | 1627 | transport_remove_device(dev); |
| 1628 | device_del(dev); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1629 | |
Christoph Hellwig | 33b114e | 2006-01-11 14:20:43 +0100 | [diff] [blame] | 1630 | parent->rphy = NULL; |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1631 | } |
Darrick J. Wong | 6f63caa | 2007-01-26 14:08:43 -0800 | [diff] [blame] | 1632 | EXPORT_SYMBOL(sas_rphy_remove); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1633 | |
| 1634 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1635 | * scsi_is_sas_rphy - check if a struct device represents a SAS remote PHY |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1636 | * @dev: device to check |
| 1637 | * |
| 1638 | * Returns: |
| 1639 | * %1 if the device represents a SAS remote PHY, %0 else |
| 1640 | */ |
| 1641 | int scsi_is_sas_rphy(const struct device *dev) |
| 1642 | { |
James Bottomley | 2f8600d | 2006-03-18 15:00:50 -0600 | [diff] [blame] | 1643 | return dev->release == sas_end_device_release || |
| 1644 | dev->release == sas_expander_release; |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1645 | } |
| 1646 | EXPORT_SYMBOL(scsi_is_sas_rphy); |
| 1647 | |
| 1648 | |
| 1649 | /* |
| 1650 | * SCSI scan helper |
| 1651 | */ |
| 1652 | |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 1653 | static int sas_user_scan(struct Scsi_Host *shost, uint channel, |
| 1654 | uint id, uint lun) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1655 | { |
| 1656 | struct sas_host_attrs *sas_host = to_sas_host_attrs(shost); |
| 1657 | struct sas_rphy *rphy; |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1658 | |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 1659 | mutex_lock(&sas_host->lock); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1660 | list_for_each_entry(rphy, &sas_host->rphy_list, list) { |
James Bottomley | 6d99a3f | 2006-05-19 10:49:37 -0500 | [diff] [blame] | 1661 | if (rphy->identify.device_type != SAS_END_DEVICE || |
| 1662 | rphy->scsi_target_id == -1) |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 1663 | continue; |
| 1664 | |
James Bottomley | e8bf394 | 2006-07-11 17:49:34 -0400 | [diff] [blame] | 1665 | if ((channel == SCAN_WILD_CARD || channel == 0) && |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 1666 | (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) { |
James Bottomley | e8bf394 | 2006-07-11 17:49:34 -0400 | [diff] [blame] | 1667 | scsi_scan_target(&rphy->dev, 0, |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 1668 | rphy->scsi_target_id, lun, 1); |
| 1669 | } |
| 1670 | } |
| 1671 | mutex_unlock(&sas_host->lock); |
| 1672 | |
| 1673 | return 0; |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | |
| 1677 | /* |
| 1678 | * Setup / Teardown code |
| 1679 | */ |
| 1680 | |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 1681 | #define SETUP_TEMPLATE(attrb, field, perm, test) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1682 | i->private_##attrb[count] = dev_attr_##field; \ |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1683 | i->private_##attrb[count].attr.mode = perm; \ |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1684 | i->attrb[count] = &i->private_##attrb[count]; \ |
| 1685 | if (test) \ |
| 1686 | count++ |
| 1687 | |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 1688 | #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1689 | i->private_##attrb[count] = dev_attr_##field; \ |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 1690 | i->private_##attrb[count].attr.mode = perm; \ |
| 1691 | if (ro_test) { \ |
| 1692 | i->private_##attrb[count].attr.mode = ro_perm; \ |
| 1693 | i->private_##attrb[count].store = NULL; \ |
| 1694 | } \ |
| 1695 | i->attrb[count] = &i->private_##attrb[count]; \ |
| 1696 | if (test) \ |
| 1697 | count++ |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1698 | |
| 1699 | #define SETUP_RPORT_ATTRIBUTE(field) \ |
| 1700 | SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1701 | |
James Bottomley | dd9fbb52 | 2006-03-02 16:01:31 -0600 | [diff] [blame] | 1702 | #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \ |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1703 | SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func) |
James Bottomley | dd9fbb52 | 2006-03-02 16:01:31 -0600 | [diff] [blame] | 1704 | |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1705 | #define SETUP_PHY_ATTRIBUTE(field) \ |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1706 | SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1707 | |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 1708 | #define SETUP_PHY_ATTRIBUTE_RW(field) \ |
| 1709 | SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \ |
| 1710 | !i->f->set_phy_speed, S_IRUGO) |
| 1711 | |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 1712 | #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func) \ |
| 1713 | SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \ |
| 1714 | !i->f->func, S_IRUGO) |
| 1715 | |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1716 | #define SETUP_PORT_ATTRIBUTE(field) \ |
| 1717 | SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1) |
| 1718 | |
| 1719 | #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \ |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1720 | SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func) |
James Bottomley | dd9fbb52 | 2006-03-02 16:01:31 -0600 | [diff] [blame] | 1721 | |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1722 | #define SETUP_PHY_ATTRIBUTE_WRONLY(field) \ |
Darrick J. Wong | fe3b5bf | 2007-01-11 14:15:35 -0800 | [diff] [blame] | 1723 | SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1) |
Christoph Hellwig | 07ba3a9 | 2005-10-19 20:01:31 +0200 | [diff] [blame] | 1724 | |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1725 | #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \ |
Darrick J. Wong | fe3b5bf | 2007-01-11 14:15:35 -0800 | [diff] [blame] | 1726 | SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func) |
James Bottomley | dd9fbb52 | 2006-03-02 16:01:31 -0600 | [diff] [blame] | 1727 | |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1728 | #define SETUP_END_DEV_ATTRIBUTE(field) \ |
| 1729 | SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1) |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1730 | |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1731 | #define SETUP_EXPANDER_ATTRIBUTE(field) \ |
| 1732 | SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1) |
| 1733 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1734 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1735 | * sas_attach_transport - instantiate SAS transport template |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1736 | * @ft: SAS transport class function template |
| 1737 | */ |
| 1738 | struct scsi_transport_template * |
| 1739 | sas_attach_transport(struct sas_function_template *ft) |
| 1740 | { |
| 1741 | struct sas_internal *i; |
| 1742 | int count; |
| 1743 | |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 1744 | i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1745 | if (!i) |
| 1746 | return NULL; |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1747 | |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 1748 | i->t.user_scan = sas_user_scan; |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1749 | |
| 1750 | i->t.host_attrs.ac.attrs = &i->host_attrs[0]; |
| 1751 | i->t.host_attrs.ac.class = &sas_host_class.class; |
| 1752 | i->t.host_attrs.ac.match = sas_host_match; |
| 1753 | transport_container_register(&i->t.host_attrs); |
| 1754 | i->t.host_size = sizeof(struct sas_host_attrs); |
| 1755 | |
| 1756 | i->phy_attr_cont.ac.class = &sas_phy_class.class; |
| 1757 | i->phy_attr_cont.ac.attrs = &i->phy_attrs[0]; |
| 1758 | i->phy_attr_cont.ac.match = sas_phy_match; |
| 1759 | transport_container_register(&i->phy_attr_cont); |
| 1760 | |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1761 | i->port_attr_cont.ac.class = &sas_port_class.class; |
| 1762 | i->port_attr_cont.ac.attrs = &i->port_attrs[0]; |
| 1763 | i->port_attr_cont.ac.match = sas_port_match; |
| 1764 | transport_container_register(&i->port_attr_cont); |
| 1765 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1766 | i->rphy_attr_cont.ac.class = &sas_rphy_class.class; |
| 1767 | i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0]; |
| 1768 | i->rphy_attr_cont.ac.match = sas_rphy_match; |
| 1769 | transport_container_register(&i->rphy_attr_cont); |
| 1770 | |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1771 | i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class; |
| 1772 | i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0]; |
| 1773 | i->end_dev_attr_cont.ac.match = sas_end_dev_match; |
| 1774 | transport_container_register(&i->end_dev_attr_cont); |
| 1775 | |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1776 | i->expander_attr_cont.ac.class = &sas_expander_class.class; |
| 1777 | i->expander_attr_cont.ac.attrs = &i->expander_attrs[0]; |
| 1778 | i->expander_attr_cont.ac.match = sas_expander_match; |
| 1779 | transport_container_register(&i->expander_attr_cont); |
| 1780 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1781 | i->f = ft; |
| 1782 | |
| 1783 | count = 0; |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1784 | SETUP_PHY_ATTRIBUTE(initiator_port_protocols); |
| 1785 | SETUP_PHY_ATTRIBUTE(target_port_protocols); |
| 1786 | SETUP_PHY_ATTRIBUTE(device_type); |
| 1787 | SETUP_PHY_ATTRIBUTE(sas_address); |
| 1788 | SETUP_PHY_ATTRIBUTE(phy_identifier); |
| 1789 | //SETUP_PHY_ATTRIBUTE(port_identifier); |
| 1790 | SETUP_PHY_ATTRIBUTE(negotiated_linkrate); |
| 1791 | SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw); |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 1792 | SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate); |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1793 | SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw); |
James Bottomley | d24e1ee | 2006-09-06 19:25:22 -0500 | [diff] [blame] | 1794 | SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate); |
Christoph Hellwig | c3ee74c | 2005-09-19 21:59:42 +0200 | [diff] [blame] | 1795 | |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1796 | SETUP_PHY_ATTRIBUTE(invalid_dword_count); |
| 1797 | SETUP_PHY_ATTRIBUTE(running_disparity_error_count); |
| 1798 | SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count); |
| 1799 | SETUP_PHY_ATTRIBUTE(phy_reset_problem_count); |
| 1800 | SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset); |
| 1801 | SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset); |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 1802 | SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1803 | i->phy_attrs[count] = NULL; |
| 1804 | |
| 1805 | count = 0; |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1806 | SETUP_PORT_ATTRIBUTE(num_phys); |
| 1807 | i->port_attrs[count] = NULL; |
| 1808 | |
| 1809 | count = 0; |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1810 | SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols); |
| 1811 | SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols); |
| 1812 | SETUP_RPORT_ATTRIBUTE(rphy_device_type); |
| 1813 | SETUP_RPORT_ATTRIBUTE(rphy_sas_address); |
| 1814 | SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier); |
James Bottomley | dd9fbb52 | 2006-03-02 16:01:31 -0600 | [diff] [blame] | 1815 | SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier, |
| 1816 | get_enclosure_identifier); |
| 1817 | SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier, |
| 1818 | get_bay_identifier); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1819 | i->rphy_attrs[count] = NULL; |
| 1820 | |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1821 | count = 0; |
| 1822 | SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning); |
| 1823 | SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout); |
| 1824 | SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout); |
James Bottomley | 0f88009 | 2010-01-18 10:14:51 -0600 | [diff] [blame] | 1825 | SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_supported); |
| 1826 | SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_enabled); |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1827 | i->end_dev_attrs[count] = NULL; |
| 1828 | |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1829 | count = 0; |
| 1830 | SETUP_EXPANDER_ATTRIBUTE(vendor_id); |
| 1831 | SETUP_EXPANDER_ATTRIBUTE(product_id); |
| 1832 | SETUP_EXPANDER_ATTRIBUTE(product_rev); |
| 1833 | SETUP_EXPANDER_ATTRIBUTE(component_vendor_id); |
| 1834 | SETUP_EXPANDER_ATTRIBUTE(component_id); |
| 1835 | SETUP_EXPANDER_ATTRIBUTE(component_revision_id); |
| 1836 | SETUP_EXPANDER_ATTRIBUTE(level); |
| 1837 | i->expander_attrs[count] = NULL; |
| 1838 | |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1839 | return &i->t; |
| 1840 | } |
| 1841 | EXPORT_SYMBOL(sas_attach_transport); |
| 1842 | |
| 1843 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1844 | * sas_release_transport - release SAS transport template instance |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1845 | * @t: transport template instance |
| 1846 | */ |
| 1847 | void sas_release_transport(struct scsi_transport_template *t) |
| 1848 | { |
| 1849 | struct sas_internal *i = to_sas_internal(t); |
| 1850 | |
| 1851 | transport_container_unregister(&i->t.host_attrs); |
| 1852 | transport_container_unregister(&i->phy_attr_cont); |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1853 | transport_container_unregister(&i->port_attr_cont); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1854 | transport_container_unregister(&i->rphy_attr_cont); |
James Bottomley | db82f84 | 2006-03-09 22:06:36 -0500 | [diff] [blame] | 1855 | transport_container_unregister(&i->end_dev_attr_cont); |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1856 | transport_container_unregister(&i->expander_attr_cont); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1857 | |
| 1858 | kfree(i); |
| 1859 | } |
| 1860 | EXPORT_SYMBOL(sas_release_transport); |
| 1861 | |
| 1862 | static __init int sas_transport_init(void) |
| 1863 | { |
| 1864 | int error; |
| 1865 | |
| 1866 | error = transport_class_register(&sas_host_class); |
| 1867 | if (error) |
| 1868 | goto out; |
| 1869 | error = transport_class_register(&sas_phy_class); |
| 1870 | if (error) |
| 1871 | goto out_unregister_transport; |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1872 | error = transport_class_register(&sas_port_class); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1873 | if (error) |
| 1874 | goto out_unregister_phy; |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1875 | error = transport_class_register(&sas_rphy_class); |
| 1876 | if (error) |
| 1877 | goto out_unregister_port; |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1878 | error = transport_class_register(&sas_end_dev_class); |
| 1879 | if (error) |
| 1880 | goto out_unregister_rphy; |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1881 | error = transport_class_register(&sas_expander_class); |
| 1882 | if (error) |
| 1883 | goto out_unregister_end_dev; |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1884 | |
| 1885 | return 0; |
| 1886 | |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1887 | out_unregister_end_dev: |
| 1888 | transport_class_unregister(&sas_end_dev_class); |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1889 | out_unregister_rphy: |
| 1890 | transport_class_unregister(&sas_rphy_class); |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1891 | out_unregister_port: |
| 1892 | transport_class_unregister(&sas_port_class); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1893 | out_unregister_phy: |
| 1894 | transport_class_unregister(&sas_phy_class); |
| 1895 | out_unregister_transport: |
| 1896 | transport_class_unregister(&sas_host_class); |
| 1897 | out: |
| 1898 | return error; |
| 1899 | |
| 1900 | } |
| 1901 | |
| 1902 | static void __exit sas_transport_exit(void) |
| 1903 | { |
| 1904 | transport_class_unregister(&sas_host_class); |
| 1905 | transport_class_unregister(&sas_phy_class); |
James Bottomley | 65c92b0 | 2006-06-28 12:22:50 -0400 | [diff] [blame] | 1906 | transport_class_unregister(&sas_port_class); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1907 | transport_class_unregister(&sas_rphy_class); |
James Bottomley | 42ab036 | 2006-03-04 09:10:18 -0600 | [diff] [blame] | 1908 | transport_class_unregister(&sas_end_dev_class); |
James Bottomley | 79cb181 | 2006-03-13 13:50:04 -0600 | [diff] [blame] | 1909 | transport_class_unregister(&sas_expander_class); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | MODULE_AUTHOR("Christoph Hellwig"); |
Alexis Bruemmer | 86b9c4c | 2007-01-16 15:36:12 -0800 | [diff] [blame] | 1913 | MODULE_DESCRIPTION("SAS Transport Attributes"); |
Christoph Hellwig | c7ebbbc | 2005-09-09 16:22:50 +0200 | [diff] [blame] | 1914 | MODULE_LICENSE("GPL"); |
| 1915 | |
| 1916 | module_init(sas_transport_init); |
| 1917 | module_exit(sas_transport_exit); |