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