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