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