]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/scsi_transport_sas.c
[SCSI] mptfusion - fix panic in mptsas_slave_configure
[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,
9 * and various sysfs attributes to expose these topologies and managment
10 * interfaces to userspace.
11 *
12 * In addition to the basic SCSI core objects this transport class
13 * introduces two additional intermediate objects: The SAS PHY
14 * as represented by struct sas_phy defines an "outgoing" PHY on
15 * a SAS HBA or Expander, and the SAS remote PHY represented by
16 * struct sas_rphy defines an "incoming" PHY on a SAS Expander or
17 * end device. Note that this is purely a software concept, the
18 * underlying hardware for a PHY and a remote PHY is the exactly
19 * the same.
20 *
21 * There is no concept of a SAS port in this code, users can see
22 * what PHYs form a wide port based on the port_identifier attribute,
23 * which is the same for all PHYs in a port.
24 */
25
26#include <linux/init.h>
27#include <linux/module.h>
28#include <linux/err.h>
8c65b4a6
TS
29#include <linux/slab.h>
30#include <linux/string.h>
c7ebbbce 31
e02f3f59 32#include <scsi/scsi.h>
c7ebbbce
CH
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_transport.h>
36#include <scsi/scsi_transport_sas.h>
37
d6159c17 38#include "scsi_sas_internal.h"
c7ebbbce
CH
39struct sas_host_attrs {
40 struct list_head rphy_list;
e02f3f59 41 struct mutex lock;
c7ebbbce 42 u32 next_target_id;
79cb1819 43 u32 next_expander_id;
c7ebbbce
CH
44};
45#define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
46
47
48/*
49 * Hack to allow attributes of the same name in different objects.
50 */
51#define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
52 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
53 __ATTR(_name,_mode,_show,_store)
54
55
56/*
57 * Pretty printing helpers
58 */
59
60#define sas_bitfield_name_match(title, table) \
61static ssize_t \
62get_sas_##title##_names(u32 table_key, char *buf) \
63{ \
64 char *prefix = ""; \
65 ssize_t len = 0; \
66 int i; \
67 \
68 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
69 if (table[i].value & table_key) { \
70 len += sprintf(buf + len, "%s%s", \
71 prefix, table[i].name); \
72 prefix = ", "; \
73 } \
74 } \
75 len += sprintf(buf + len, "\n"); \
76 return len; \
77}
78
79#define sas_bitfield_name_search(title, table) \
80static ssize_t \
81get_sas_##title##_names(u32 table_key, char *buf) \
82{ \
83 ssize_t len = 0; \
84 int i; \
85 \
86 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
87 if (table[i].value == table_key) { \
88 len += sprintf(buf + len, "%s", \
89 table[i].name); \
90 break; \
91 } \
92 } \
93 len += sprintf(buf + len, "\n"); \
94 return len; \
95}
96
97static struct {
98 u32 value;
99 char *name;
100} sas_device_type_names[] = {
101 { SAS_PHY_UNUSED, "unused" },
102 { SAS_END_DEVICE, "end device" },
103 { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
104 { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
105};
106sas_bitfield_name_search(device_type, sas_device_type_names)
107
108
109static struct {
110 u32 value;
111 char *name;
112} sas_protocol_names[] = {
113 { SAS_PROTOCOL_SATA, "sata" },
114 { SAS_PROTOCOL_SMP, "smp" },
115 { SAS_PROTOCOL_STP, "stp" },
116 { SAS_PROTOCOL_SSP, "ssp" },
117};
118sas_bitfield_name_match(protocol, sas_protocol_names)
119
120static struct {
121 u32 value;
122 char *name;
123} sas_linkspeed_names[] = {
124 { SAS_LINK_RATE_UNKNOWN, "Unknown" },
125 { SAS_PHY_DISABLED, "Phy disabled" },
126 { SAS_LINK_RATE_FAILED, "Link Rate failed" },
127 { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
128 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
129 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
7e6dff62 130 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
c7ebbbce
CH
131};
132sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
133
134
135/*
136 * SAS host attributes
137 */
138
37be6eeb
JB
139static int sas_host_setup(struct transport_container *tc, struct device *dev,
140 struct class_device *cdev)
c7ebbbce
CH
141{
142 struct Scsi_Host *shost = dev_to_shost(dev);
143 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
144
145 INIT_LIST_HEAD(&sas_host->rphy_list);
e02f3f59 146 mutex_init(&sas_host->lock);
c7ebbbce 147 sas_host->next_target_id = 0;
79cb1819 148 sas_host->next_expander_id = 0;
c7ebbbce
CH
149 return 0;
150}
151
152static DECLARE_TRANSPORT_CLASS(sas_host_class,
153 "sas_host", sas_host_setup, NULL, NULL);
154
155static int sas_host_match(struct attribute_container *cont,
156 struct device *dev)
157{
158 struct Scsi_Host *shost;
159 struct sas_internal *i;
160
161 if (!scsi_is_host_device(dev))
162 return 0;
163 shost = dev_to_shost(dev);
164
165 if (!shost->transportt)
166 return 0;
167 if (shost->transportt->host_attrs.ac.class !=
168 &sas_host_class.class)
169 return 0;
170
171 i = to_sas_internal(shost->transportt);
172 return &i->t.host_attrs.ac == cont;
173}
174
175static int do_sas_phy_delete(struct device *dev, void *data)
176{
177 if (scsi_is_sas_phy(dev))
178 sas_phy_delete(dev_to_phy(dev));
179 return 0;
180}
181
182/**
183 * sas_remove_host -- tear down a Scsi_Host's SAS data structures
184 * @shost: Scsi Host that is torn down
185 *
186 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
187 * Must be called just before scsi_remove_host for SAS HBAs.
188 */
189void sas_remove_host(struct Scsi_Host *shost)
190{
191 device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete);
192}
193EXPORT_SYMBOL(sas_remove_host);
194
195
196/*
197 * SAS Port attributes
198 */
199
200#define sas_phy_show_simple(field, name, format_string, cast) \
201static ssize_t \
202show_sas_phy_##name(struct class_device *cdev, char *buf) \
203{ \
204 struct sas_phy *phy = transport_class_to_phy(cdev); \
205 \
206 return snprintf(buf, 20, format_string, cast phy->field); \
207}
208
209#define sas_phy_simple_attr(field, name, format_string, type) \
210 sas_phy_show_simple(field, name, format_string, (type)) \
211static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
212
213#define sas_phy_show_protocol(field, name) \
214static ssize_t \
215show_sas_phy_##name(struct class_device *cdev, char *buf) \
216{ \
217 struct sas_phy *phy = transport_class_to_phy(cdev); \
218 \
219 if (!phy->field) \
220 return snprintf(buf, 20, "none\n"); \
221 return get_sas_protocol_names(phy->field, buf); \
222}
223
224#define sas_phy_protocol_attr(field, name) \
225 sas_phy_show_protocol(field, name) \
226static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
227
228#define sas_phy_show_linkspeed(field) \
229static ssize_t \
230show_sas_phy_##field(struct class_device *cdev, char *buf) \
231{ \
232 struct sas_phy *phy = transport_class_to_phy(cdev); \
233 \
234 return get_sas_linkspeed_names(phy->field, buf); \
235}
236
237#define sas_phy_linkspeed_attr(field) \
238 sas_phy_show_linkspeed(field) \
239static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
240
c3ee74c4
CH
241#define sas_phy_show_linkerror(field) \
242static ssize_t \
243show_sas_phy_##field(struct class_device *cdev, char *buf) \
244{ \
245 struct sas_phy *phy = transport_class_to_phy(cdev); \
246 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
247 struct sas_internal *i = to_sas_internal(shost->transportt); \
248 int error; \
249 \
ac01bbbd
CH
250 if (!phy->local_attached) \
251 return -EINVAL; \
252 \
dd9fbb52 253 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
c3ee74c4
CH
254 if (error) \
255 return error; \
256 return snprintf(buf, 20, "%u\n", phy->field); \
257}
258
259#define sas_phy_linkerror_attr(field) \
260 sas_phy_show_linkerror(field) \
261static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
262
263
c7ebbbce
CH
264static ssize_t
265show_sas_device_type(struct class_device *cdev, char *buf)
266{
267 struct sas_phy *phy = transport_class_to_phy(cdev);
268
269 if (!phy->identify.device_type)
270 return snprintf(buf, 20, "none\n");
271 return get_sas_device_type_names(phy->identify.device_type, buf);
272}
c7ebbbce
CH
273static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
274
07ba3a95
CH
275static ssize_t do_sas_phy_reset(struct class_device *cdev,
276 size_t count, int hard_reset)
277{
278 struct sas_phy *phy = transport_class_to_phy(cdev);
279 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
280 struct sas_internal *i = to_sas_internal(shost->transportt);
281 int error;
282
283 if (!phy->local_attached)
284 return -EINVAL;
285
286 error = i->f->phy_reset(phy, hard_reset);
287 if (error)
288 return error;
289 return count;
290};
291
292static ssize_t store_sas_link_reset(struct class_device *cdev,
293 const char *buf, size_t count)
294{
295 return do_sas_phy_reset(cdev, count, 0);
296}
297static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
298
299static ssize_t store_sas_hard_reset(struct class_device *cdev,
300 const char *buf, size_t count)
301{
302 return do_sas_phy_reset(cdev, count, 1);
303}
304static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
305
c7ebbbce
CH
306sas_phy_protocol_attr(identify.initiator_port_protocols,
307 initiator_port_protocols);
308sas_phy_protocol_attr(identify.target_port_protocols,
309 target_port_protocols);
310sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
311 unsigned long long);
312sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
313sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
314sas_phy_linkspeed_attr(negotiated_linkrate);
315sas_phy_linkspeed_attr(minimum_linkrate_hw);
316sas_phy_linkspeed_attr(minimum_linkrate);
317sas_phy_linkspeed_attr(maximum_linkrate_hw);
318sas_phy_linkspeed_attr(maximum_linkrate);
c3ee74c4
CH
319sas_phy_linkerror_attr(invalid_dword_count);
320sas_phy_linkerror_attr(running_disparity_error_count);
321sas_phy_linkerror_attr(loss_of_dword_sync_count);
322sas_phy_linkerror_attr(phy_reset_problem_count);
c7ebbbce
CH
323
324
325static DECLARE_TRANSPORT_CLASS(sas_phy_class,
326 "sas_phy", NULL, NULL, NULL);
327
328static int sas_phy_match(struct attribute_container *cont, struct device *dev)
329{
330 struct Scsi_Host *shost;
331 struct sas_internal *i;
332
333 if (!scsi_is_sas_phy(dev))
334 return 0;
335 shost = dev_to_shost(dev->parent);
336
337 if (!shost->transportt)
338 return 0;
339 if (shost->transportt->host_attrs.ac.class !=
340 &sas_host_class.class)
341 return 0;
342
343 i = to_sas_internal(shost->transportt);
344 return &i->phy_attr_cont.ac == cont;
345}
346
347static void sas_phy_release(struct device *dev)
348{
349 struct sas_phy *phy = dev_to_phy(dev);
350
351 put_device(dev->parent);
352 kfree(phy);
353}
354
355/**
356 * sas_phy_alloc -- allocates and initialize a SAS PHY structure
357 * @parent: Parent device
d99ca418 358 * @number: Phy index
c7ebbbce
CH
359 *
360 * Allocates an SAS PHY structure. It will be added in the device tree
361 * below the device specified by @parent, which has to be either a Scsi_Host
362 * or sas_rphy.
363 *
364 * Returns:
365 * SAS PHY allocated or %NULL if the allocation failed.
366 */
367struct sas_phy *sas_phy_alloc(struct device *parent, int number)
368{
369 struct Scsi_Host *shost = dev_to_shost(parent);
370 struct sas_phy *phy;
371
24669f75 372 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
c7ebbbce
CH
373 if (!phy)
374 return NULL;
c7ebbbce 375
c7ebbbce
CH
376 phy->number = number;
377
378 device_initialize(&phy->dev);
379 phy->dev.parent = get_device(parent);
380 phy->dev.release = sas_phy_release;
79cb1819
JB
381 if (scsi_is_sas_expander_device(parent)) {
382 struct sas_rphy *rphy = dev_to_rphy(parent);
383 sprintf(phy->dev.bus_id, "phy-%d-%d:%d", shost->host_no,
384 rphy->scsi_target_id, number);
385 } else
386 sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
c7ebbbce
CH
387
388 transport_setup_device(&phy->dev);
389
390 return phy;
391}
392EXPORT_SYMBOL(sas_phy_alloc);
393
394/**
395 * sas_phy_add -- add a SAS PHY to the device hierachy
396 * @phy: The PHY to be added
397 *
398 * Publishes a SAS PHY to the rest of the system.
399 */
400int sas_phy_add(struct sas_phy *phy)
401{
402 int error;
403
404 error = device_add(&phy->dev);
405 if (!error) {
406 transport_add_device(&phy->dev);
407 transport_configure_device(&phy->dev);
408 }
409
410 return error;
411}
412EXPORT_SYMBOL(sas_phy_add);
413
414/**
415 * sas_phy_free -- free a SAS PHY
416 * @phy: SAS PHY to free
417 *
418 * Frees the specified SAS PHY.
419 *
420 * Note:
421 * This function must only be called on a PHY that has not
422 * sucessfully been added using sas_phy_add().
423 */
424void sas_phy_free(struct sas_phy *phy)
425{
426 transport_destroy_device(&phy->dev);
92aab646 427 put_device(&phy->dev);
c7ebbbce
CH
428}
429EXPORT_SYMBOL(sas_phy_free);
430
431/**
432 * sas_phy_delete -- remove SAS PHY
433 * @phy: SAS PHY to remove
434 *
435 * Removes the specified SAS PHY. If the SAS PHY has an
436 * associated remote PHY it is removed before.
437 */
438void
439sas_phy_delete(struct sas_phy *phy)
440{
441 struct device *dev = &phy->dev;
442
443 if (phy->rphy)
444 sas_rphy_delete(phy->rphy);
445
446 transport_remove_device(dev);
447 device_del(dev);
448 transport_destroy_device(dev);
92aab646 449 put_device(dev);
c7ebbbce
CH
450}
451EXPORT_SYMBOL(sas_phy_delete);
452
453/**
454 * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
455 * @dev: device to check
456 *
457 * Returns:
458 * %1 if the device represents a SAS PHY, %0 else
459 */
460int scsi_is_sas_phy(const struct device *dev)
461{
462 return dev->release == sas_phy_release;
463}
464EXPORT_SYMBOL(scsi_is_sas_phy);
465
466/*
467 * SAS remote PHY attributes.
468 */
469
470#define sas_rphy_show_simple(field, name, format_string, cast) \
471static ssize_t \
472show_sas_rphy_##name(struct class_device *cdev, char *buf) \
473{ \
474 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
475 \
476 return snprintf(buf, 20, format_string, cast rphy->field); \
477}
478
479#define sas_rphy_simple_attr(field, name, format_string, type) \
480 sas_rphy_show_simple(field, name, format_string, (type)) \
481static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
482 show_sas_rphy_##name, NULL)
483
484#define sas_rphy_show_protocol(field, name) \
485static ssize_t \
486show_sas_rphy_##name(struct class_device *cdev, char *buf) \
487{ \
488 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
489 \
490 if (!rphy->field) \
491 return snprintf(buf, 20, "none\n"); \
492 return get_sas_protocol_names(rphy->field, buf); \
493}
494
495#define sas_rphy_protocol_attr(field, name) \
496 sas_rphy_show_protocol(field, name) \
497static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
498 show_sas_rphy_##name, NULL)
499
500static ssize_t
501show_sas_rphy_device_type(struct class_device *cdev, char *buf)
502{
503 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
504
505 if (!rphy->identify.device_type)
506 return snprintf(buf, 20, "none\n");
507 return get_sas_device_type_names(
508 rphy->identify.device_type, buf);
509}
510
511static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
512 show_sas_rphy_device_type, NULL);
513
a0125641
CH
514static ssize_t
515show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
516{
517 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
518 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
519 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
520 struct sas_internal *i = to_sas_internal(shost->transportt);
521 u64 identifier;
522 int error;
523
524 /*
525 * Only devices behind an expander are supported, because the
526 * enclosure identifier is a SMP feature.
527 */
528 if (phy->local_attached)
529 return -EINVAL;
530
531 error = i->f->get_enclosure_identifier(rphy, &identifier);
532 if (error)
533 return error;
534 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
535}
536
537static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
538 show_sas_rphy_enclosure_identifier, NULL);
539
540static ssize_t
541show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
542{
543 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
544 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
545 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
546 struct sas_internal *i = to_sas_internal(shost->transportt);
547 int val;
548
549 if (phy->local_attached)
550 return -EINVAL;
551
552 val = i->f->get_bay_identifier(rphy);
553 if (val < 0)
554 return val;
555 return sprintf(buf, "%d\n", val);
556}
557
558static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
559 show_sas_rphy_bay_identifier, NULL);
560
c7ebbbce
CH
561sas_rphy_protocol_attr(identify.initiator_port_protocols,
562 initiator_port_protocols);
563sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
564sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
565 unsigned long long);
566sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
567
42ab0360
JB
568/* only need 8 bytes of data plus header (4 or 8) */
569#define BUF_SIZE 64
570
571int sas_read_port_mode_page(struct scsi_device *sdev)
572{
573 char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
574 struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
575 struct sas_end_device *rdev;
576 struct scsi_mode_data mode_data;
577 int res, error;
578
579 BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
580
581 rdev = rphy_to_end_device(rphy);
582
583 if (!buffer)
584 return -ENOMEM;
585
586 res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
587 &mode_data, NULL);
588
589 error = -EINVAL;
590 if (!scsi_status_is_good(res))
591 goto out;
592
593 msdata = buffer + mode_data.header_length +
594 mode_data.block_descriptor_length;
595
596 if (msdata - buffer > BUF_SIZE - 8)
597 goto out;
598
599 error = 0;
600
601 rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
602 rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
603 rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
604
605 out:
606 kfree(buffer);
607 return error;
608}
609EXPORT_SYMBOL(sas_read_port_mode_page);
610
79cb1819
JB
611static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
612 "sas_end_device", NULL, NULL, NULL);
613
42ab0360
JB
614#define sas_end_dev_show_simple(field, name, format_string, cast) \
615static ssize_t \
616show_sas_end_dev_##name(struct class_device *cdev, char *buf) \
617{ \
618 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
619 struct sas_end_device *rdev = rphy_to_end_device(rphy); \
620 \
621 return snprintf(buf, 20, format_string, cast rdev->field); \
622}
623
624#define sas_end_dev_simple_attr(field, name, format_string, type) \
625 sas_end_dev_show_simple(field, name, format_string, (type)) \
626static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
627 show_sas_end_dev_##name, NULL)
628
629sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
630sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
631 "%d\n", int);
632sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
633 "%d\n", int);
634
79cb1819
JB
635static DECLARE_TRANSPORT_CLASS(sas_expander_class,
636 "sas_expander", NULL, NULL, NULL);
637
638#define sas_expander_show_simple(field, name, format_string, cast) \
639static ssize_t \
640show_sas_expander_##name(struct class_device *cdev, char *buf) \
641{ \
642 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
643 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
644 \
645 return snprintf(buf, 20, format_string, cast edev->field); \
646}
647
648#define sas_expander_simple_attr(field, name, format_string, type) \
649 sas_expander_show_simple(field, name, format_string, (type)) \
650static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO, \
651 show_sas_expander_##name, NULL)
652
653sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
654sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
655sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
656sas_expander_simple_attr(component_vendor_id, component_vendor_id,
657 "%s\n", char *);
658sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
659sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
660 unsigned int);
661sas_expander_simple_attr(level, level, "%d\n", int);
42ab0360 662
c7ebbbce 663static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
2f8600df 664 "sas_device", NULL, NULL, NULL);
c7ebbbce
CH
665
666static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
667{
668 struct Scsi_Host *shost;
669 struct sas_internal *i;
670
671 if (!scsi_is_sas_rphy(dev))
672 return 0;
673 shost = dev_to_shost(dev->parent->parent);
674
675 if (!shost->transportt)
676 return 0;
677 if (shost->transportt->host_attrs.ac.class !=
678 &sas_host_class.class)
679 return 0;
680
681 i = to_sas_internal(shost->transportt);
682 return &i->rphy_attr_cont.ac == cont;
683}
684
42ab0360
JB
685static int sas_end_dev_match(struct attribute_container *cont,
686 struct device *dev)
687{
688 struct Scsi_Host *shost;
689 struct sas_internal *i;
690 struct sas_rphy *rphy;
691
692 if (!scsi_is_sas_rphy(dev))
693 return 0;
694 shost = dev_to_shost(dev->parent->parent);
695 rphy = dev_to_rphy(dev);
696
697 if (!shost->transportt)
698 return 0;
699 if (shost->transportt->host_attrs.ac.class !=
700 &sas_host_class.class)
701 return 0;
702
703 i = to_sas_internal(shost->transportt);
704 return &i->end_dev_attr_cont.ac == cont &&
2f8600df 705 rphy->identify.device_type == SAS_END_DEVICE;
42ab0360
JB
706}
707
79cb1819
JB
708static int sas_expander_match(struct attribute_container *cont,
709 struct device *dev)
710{
711 struct Scsi_Host *shost;
712 struct sas_internal *i;
713 struct sas_rphy *rphy;
714
715 if (!scsi_is_sas_rphy(dev))
716 return 0;
717 shost = dev_to_shost(dev->parent->parent);
718 rphy = dev_to_rphy(dev);
719
720 if (!shost->transportt)
721 return 0;
722 if (shost->transportt->host_attrs.ac.class !=
723 &sas_host_class.class)
724 return 0;
725
726 i = to_sas_internal(shost->transportt);
727 return &i->expander_attr_cont.ac == cont &&
728 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
2f8600df 729 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
79cb1819
JB
730}
731
2f8600df 732static void sas_expander_release(struct device *dev)
c7ebbbce
CH
733{
734 struct sas_rphy *rphy = dev_to_rphy(dev);
2f8600df 735 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
c7ebbbce
CH
736
737 put_device(dev->parent);
2f8600df 738 kfree(edev);
c7ebbbce
CH
739}
740
2f8600df 741static void sas_end_device_release(struct device *dev)
c7ebbbce 742{
2f8600df
JB
743 struct sas_rphy *rphy = dev_to_rphy(dev);
744 struct sas_end_device *edev = rphy_to_end_device(rphy);
c7ebbbce 745
2f8600df
JB
746 put_device(dev->parent);
747 kfree(edev);
c7ebbbce 748}
c7ebbbce 749
42ab0360
JB
750/**
751 * sas_end_device_alloc - allocate an rphy for an end device
752 *
753 * Allocates an SAS remote PHY structure, connected to @parent.
754 *
755 * Returns:
756 * SAS PHY allocated or %NULL if the allocation failed.
757 */
758struct sas_rphy *sas_end_device_alloc(struct sas_phy *parent)
759{
760 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
761 struct sas_end_device *rdev;
762
763 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
764 if (!rdev) {
42ab0360
JB
765 return NULL;
766 }
767
768 device_initialize(&rdev->rphy.dev);
769 rdev->rphy.dev.parent = get_device(&parent->dev);
2f8600df
JB
770 rdev->rphy.dev.release = sas_end_device_release;
771 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d-%d",
42ab0360
JB
772 shost->host_no, parent->port_identifier, parent->number);
773 rdev->rphy.identify.device_type = SAS_END_DEVICE;
42ab0360
JB
774 transport_setup_device(&rdev->rphy.dev);
775
776 return &rdev->rphy;
777}
778EXPORT_SYMBOL(sas_end_device_alloc);
779
79cb1819
JB
780/**
781 * sas_expander_alloc - allocate an rphy for an end device
782 *
783 * Allocates an SAS remote PHY structure, connected to @parent.
784 *
785 * Returns:
786 * SAS PHY allocated or %NULL if the allocation failed.
787 */
788struct sas_rphy *sas_expander_alloc(struct sas_phy *parent,
789 enum sas_device_type type)
790{
791 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
792 struct sas_expander_device *rdev;
793 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
794
795 BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
796 type != SAS_FANOUT_EXPANDER_DEVICE);
797
798 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
799 if (!rdev) {
79cb1819
JB
800 return NULL;
801 }
802
803 device_initialize(&rdev->rphy.dev);
804 rdev->rphy.dev.parent = get_device(&parent->dev);
2f8600df 805 rdev->rphy.dev.release = sas_expander_release;
79cb1819
JB
806 mutex_lock(&sas_host->lock);
807 rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
808 mutex_unlock(&sas_host->lock);
809 sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
810 shost->host_no, rdev->rphy.scsi_target_id);
811 rdev->rphy.identify.device_type = type;
79cb1819
JB
812 transport_setup_device(&rdev->rphy.dev);
813
814 return &rdev->rphy;
815}
816EXPORT_SYMBOL(sas_expander_alloc);
42ab0360 817
c7ebbbce
CH
818/**
819 * sas_rphy_add -- add a SAS remote PHY to the device hierachy
820 * @rphy: The remote PHY to be added
821 *
822 * Publishes a SAS remote PHY to the rest of the system.
823 */
824int sas_rphy_add(struct sas_rphy *rphy)
825{
826 struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
827 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
828 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
829 struct sas_identify *identify = &rphy->identify;
830 int error;
831
832 if (parent->rphy)
833 return -ENXIO;
834 parent->rphy = rphy;
835
836 error = device_add(&rphy->dev);
837 if (error)
838 return error;
839 transport_add_device(&rphy->dev);
840 transport_configure_device(&rphy->dev);
841
e02f3f59 842 mutex_lock(&sas_host->lock);
c7ebbbce
CH
843 list_add_tail(&rphy->list, &sas_host->rphy_list);
844 if (identify->device_type == SAS_END_DEVICE &&
845 (identify->target_port_protocols &
846 (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
847 rphy->scsi_target_id = sas_host->next_target_id++;
e02f3f59 848 mutex_unlock(&sas_host->lock);
c7ebbbce 849
79cb1819
JB
850 if (identify->device_type == SAS_END_DEVICE &&
851 rphy->scsi_target_id != -1) {
e6bc863c 852 scsi_scan_target(&rphy->dev, parent->port_identifier,
c7ebbbce
CH
853 rphy->scsi_target_id, ~0, 0);
854 }
855
856 return 0;
857}
858EXPORT_SYMBOL(sas_rphy_add);
859
860/**
861 * sas_rphy_free -- free a SAS remote PHY
862 * @rphy SAS remote PHY to free
863 *
864 * Frees the specified SAS remote PHY.
865 *
866 * Note:
867 * This function must only be called on a remote
868 * PHY that has not sucessfully been added using
869 * sas_rphy_add().
870 */
871void sas_rphy_free(struct sas_rphy *rphy)
872{
92aab646 873 struct device *dev = &rphy->dev;
c7ebbbce
CH
874 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
875 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
876
e02f3f59 877 mutex_lock(&sas_host->lock);
c7ebbbce 878 list_del(&rphy->list);
e02f3f59 879 mutex_unlock(&sas_host->lock);
c7ebbbce 880
92aab646 881 transport_destroy_device(dev);
2f8600df 882
92aab646 883 put_device(dev);
c7ebbbce
CH
884}
885EXPORT_SYMBOL(sas_rphy_free);
886
887/**
888 * sas_rphy_delete -- remove SAS remote PHY
889 * @rphy: SAS remote PHY to remove
890 *
891 * Removes the specified SAS remote PHY.
892 */
893void
894sas_rphy_delete(struct sas_rphy *rphy)
895{
896 struct device *dev = &rphy->dev;
897 struct sas_phy *parent = dev_to_phy(dev->parent);
898 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
899 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
900
d4054239
CH
901 switch (rphy->identify.device_type) {
902 case SAS_END_DEVICE:
903 scsi_remove_target(dev);
904 break;
905 case SAS_EDGE_EXPANDER_DEVICE:
906 case SAS_FANOUT_EXPANDER_DEVICE:
907 device_for_each_child(dev, NULL, do_sas_phy_delete);
908 break;
909 default:
910 break;
911 }
c7ebbbce 912
fe8b2304
CH
913 transport_remove_device(dev);
914 device_del(dev);
915 transport_destroy_device(dev);
c7ebbbce 916
e02f3f59 917 mutex_lock(&sas_host->lock);
c7ebbbce 918 list_del(&rphy->list);
e02f3f59 919 mutex_unlock(&sas_host->lock);
c7ebbbce 920
33b114e9
CH
921 parent->rphy = NULL;
922
92aab646 923 put_device(dev);
c7ebbbce
CH
924}
925EXPORT_SYMBOL(sas_rphy_delete);
926
927/**
928 * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
929 * @dev: device to check
930 *
931 * Returns:
932 * %1 if the device represents a SAS remote PHY, %0 else
933 */
934int scsi_is_sas_rphy(const struct device *dev)
935{
2f8600df
JB
936 return dev->release == sas_end_device_release ||
937 dev->release == sas_expander_release;
c7ebbbce
CH
938}
939EXPORT_SYMBOL(scsi_is_sas_rphy);
940
941
942/*
943 * SCSI scan helper
944 */
945
e02f3f59
CH
946static int sas_user_scan(struct Scsi_Host *shost, uint channel,
947 uint id, uint lun)
c7ebbbce
CH
948{
949 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
950 struct sas_rphy *rphy;
c7ebbbce 951
e02f3f59 952 mutex_lock(&sas_host->lock);
c7ebbbce
CH
953 list_for_each_entry(rphy, &sas_host->rphy_list, list) {
954 struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
e02f3f59
CH
955
956 if (rphy->scsi_target_id == -1)
957 continue;
958
e6bc863c 959 if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) &&
e02f3f59 960 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
e6bc863c 961 scsi_scan_target(&rphy->dev, parent->port_identifier,
e02f3f59
CH
962 rphy->scsi_target_id, lun, 1);
963 }
c7ebbbce 964 }
e02f3f59 965 mutex_unlock(&sas_host->lock);
c7ebbbce 966
e02f3f59 967 return 0;
c7ebbbce
CH
968}
969
970
971/*
972 * Setup / Teardown code
973 */
974
42ab0360
JB
975#define SETUP_TEMPLATE(attrb, field, perm, test) \
976 i->private_##attrb[count] = class_device_attr_##field; \
977 i->private_##attrb[count].attr.mode = perm; \
978 i->private_##attrb[count].store = NULL; \
979 i->attrb[count] = &i->private_##attrb[count]; \
980 if (test) \
981 count++
982
983
984#define SETUP_RPORT_ATTRIBUTE(field) \
985 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
c7ebbbce 986
dd9fbb52 987#define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
42ab0360 988 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
dd9fbb52 989
c7ebbbce 990#define SETUP_PORT_ATTRIBUTE(field) \
42ab0360 991 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
c7ebbbce 992
dd9fbb52 993#define SETUP_OPTIONAL_PORT_ATTRIBUTE(field, func) \
42ab0360 994 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
dd9fbb52 995
07ba3a95 996#define SETUP_PORT_ATTRIBUTE_WRONLY(field) \
42ab0360 997 SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, 1)
07ba3a95 998
dd9fbb52 999#define SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(field, func) \
42ab0360 1000 SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, i->f->func)
dd9fbb52 1001
42ab0360
JB
1002#define SETUP_END_DEV_ATTRIBUTE(field) \
1003 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
c7ebbbce 1004
79cb1819
JB
1005#define SETUP_EXPANDER_ATTRIBUTE(field) \
1006 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1007
c7ebbbce
CH
1008/**
1009 * sas_attach_transport -- instantiate SAS transport template
1010 * @ft: SAS transport class function template
1011 */
1012struct scsi_transport_template *
1013sas_attach_transport(struct sas_function_template *ft)
1014{
1015 struct sas_internal *i;
1016 int count;
1017
24669f75 1018 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
c7ebbbce
CH
1019 if (!i)
1020 return NULL;
c7ebbbce 1021
e02f3f59 1022 i->t.user_scan = sas_user_scan;
c7ebbbce
CH
1023
1024 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1025 i->t.host_attrs.ac.class = &sas_host_class.class;
1026 i->t.host_attrs.ac.match = sas_host_match;
1027 transport_container_register(&i->t.host_attrs);
1028 i->t.host_size = sizeof(struct sas_host_attrs);
1029
1030 i->phy_attr_cont.ac.class = &sas_phy_class.class;
1031 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1032 i->phy_attr_cont.ac.match = sas_phy_match;
1033 transport_container_register(&i->phy_attr_cont);
1034
1035 i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1036 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1037 i->rphy_attr_cont.ac.match = sas_rphy_match;
1038 transport_container_register(&i->rphy_attr_cont);
1039
42ab0360
JB
1040 i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1041 i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1042 i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1043 transport_container_register(&i->end_dev_attr_cont);
1044
79cb1819
JB
1045 i->expander_attr_cont.ac.class = &sas_expander_class.class;
1046 i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
1047 i->expander_attr_cont.ac.match = sas_expander_match;
1048 transport_container_register(&i->expander_attr_cont);
1049
c7ebbbce
CH
1050 i->f = ft;
1051
1052 count = 0;
1053 i->host_attrs[count] = NULL;
1054
1055 count = 0;
1056 SETUP_PORT_ATTRIBUTE(initiator_port_protocols);
1057 SETUP_PORT_ATTRIBUTE(target_port_protocols);
1058 SETUP_PORT_ATTRIBUTE(device_type);
1059 SETUP_PORT_ATTRIBUTE(sas_address);
1060 SETUP_PORT_ATTRIBUTE(phy_identifier);
1061 SETUP_PORT_ATTRIBUTE(port_identifier);
1062 SETUP_PORT_ATTRIBUTE(negotiated_linkrate);
1063 SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw);
1064 SETUP_PORT_ATTRIBUTE(minimum_linkrate);
1065 SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw);
1066 SETUP_PORT_ATTRIBUTE(maximum_linkrate);
c3ee74c4
CH
1067
1068 SETUP_PORT_ATTRIBUTE(invalid_dword_count);
1069 SETUP_PORT_ATTRIBUTE(running_disparity_error_count);
1070 SETUP_PORT_ATTRIBUTE(loss_of_dword_sync_count);
1071 SETUP_PORT_ATTRIBUTE(phy_reset_problem_count);
dd9fbb52
JB
1072 SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1073 SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
c7ebbbce
CH
1074 i->phy_attrs[count] = NULL;
1075
1076 count = 0;
1077 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1078 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1079 SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1080 SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1081 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
dd9fbb52
JB
1082 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1083 get_enclosure_identifier);
1084 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1085 get_bay_identifier);
c7ebbbce
CH
1086 i->rphy_attrs[count] = NULL;
1087
42ab0360
JB
1088 count = 0;
1089 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1090 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1091 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
1092 i->end_dev_attrs[count] = NULL;
1093
79cb1819
JB
1094 count = 0;
1095 SETUP_EXPANDER_ATTRIBUTE(vendor_id);
1096 SETUP_EXPANDER_ATTRIBUTE(product_id);
1097 SETUP_EXPANDER_ATTRIBUTE(product_rev);
1098 SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
1099 SETUP_EXPANDER_ATTRIBUTE(component_id);
1100 SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
1101 SETUP_EXPANDER_ATTRIBUTE(level);
1102 i->expander_attrs[count] = NULL;
1103
c7ebbbce
CH
1104 return &i->t;
1105}
1106EXPORT_SYMBOL(sas_attach_transport);
1107
1108/**
1109 * sas_release_transport -- release SAS transport template instance
1110 * @t: transport template instance
1111 */
1112void sas_release_transport(struct scsi_transport_template *t)
1113{
1114 struct sas_internal *i = to_sas_internal(t);
1115
1116 transport_container_unregister(&i->t.host_attrs);
1117 transport_container_unregister(&i->phy_attr_cont);
1118 transport_container_unregister(&i->rphy_attr_cont);
db82f841 1119 transport_container_unregister(&i->end_dev_attr_cont);
79cb1819 1120 transport_container_unregister(&i->expander_attr_cont);
c7ebbbce
CH
1121
1122 kfree(i);
1123}
1124EXPORT_SYMBOL(sas_release_transport);
1125
1126static __init int sas_transport_init(void)
1127{
1128 int error;
1129
1130 error = transport_class_register(&sas_host_class);
1131 if (error)
1132 goto out;
1133 error = transport_class_register(&sas_phy_class);
1134 if (error)
1135 goto out_unregister_transport;
1136 error = transport_class_register(&sas_rphy_class);
1137 if (error)
1138 goto out_unregister_phy;
42ab0360
JB
1139 error = transport_class_register(&sas_end_dev_class);
1140 if (error)
1141 goto out_unregister_rphy;
79cb1819
JB
1142 error = transport_class_register(&sas_expander_class);
1143 if (error)
1144 goto out_unregister_end_dev;
c7ebbbce
CH
1145
1146 return 0;
1147
79cb1819
JB
1148 out_unregister_end_dev:
1149 transport_class_unregister(&sas_end_dev_class);
42ab0360
JB
1150 out_unregister_rphy:
1151 transport_class_unregister(&sas_rphy_class);
c7ebbbce
CH
1152 out_unregister_phy:
1153 transport_class_unregister(&sas_phy_class);
1154 out_unregister_transport:
1155 transport_class_unregister(&sas_host_class);
1156 out:
1157 return error;
1158
1159}
1160
1161static void __exit sas_transport_exit(void)
1162{
1163 transport_class_unregister(&sas_host_class);
1164 transport_class_unregister(&sas_phy_class);
1165 transport_class_unregister(&sas_rphy_class);
42ab0360 1166 transport_class_unregister(&sas_end_dev_class);
79cb1819 1167 transport_class_unregister(&sas_expander_class);
c7ebbbce
CH
1168}
1169
1170MODULE_AUTHOR("Christoph Hellwig");
1171MODULE_DESCRIPTION("SAS Transphy Attributes");
1172MODULE_LICENSE("GPL");
1173
1174module_init(sas_transport_init);
1175module_exit(sas_transport_exit);