blob: eab5c7c6f3c788994c2370a1475efa51446c9d7f [file] [log] [blame]
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02001/*
Christoph Hellwiga0125642006-02-16 13:31:47 +01002 * Copyright (C) 2005-2006 Dell Inc.
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +02003 * 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>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080029#include <linux/slab.h>
30#include <linux/string.h>
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020031
Christoph Hellwige02f3f52006-01-13 19:04:00 +010032#include <scsi/scsi.h>
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020033#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
38
39#define SAS_HOST_ATTRS 0
Christoph Hellwig07ba3a92005-10-19 20:01:31 +020040#define SAS_PORT_ATTRS 17
Christoph Hellwiga0125642006-02-16 13:31:47 +010041#define SAS_RPORT_ATTRS 7
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020042
43struct sas_internal {
44 struct scsi_transport_template t;
45 struct sas_function_template *f;
46
47 struct class_device_attribute private_host_attrs[SAS_HOST_ATTRS];
48 struct class_device_attribute private_phy_attrs[SAS_PORT_ATTRS];
49 struct class_device_attribute private_rphy_attrs[SAS_RPORT_ATTRS];
50
51 struct transport_container phy_attr_cont;
52 struct transport_container rphy_attr_cont;
53
54 /*
55 * The array of null terminated pointers to attributes
56 * needed by scsi_sysfs.c
57 */
58 struct class_device_attribute *host_attrs[SAS_HOST_ATTRS + 1];
59 struct class_device_attribute *phy_attrs[SAS_PORT_ATTRS + 1];
60 struct class_device_attribute *rphy_attrs[SAS_RPORT_ATTRS + 1];
61};
62#define to_sas_internal(tmpl) container_of(tmpl, struct sas_internal, t)
63
64struct sas_host_attrs {
65 struct list_head rphy_list;
Christoph Hellwige02f3f52006-01-13 19:04:00 +010066 struct mutex lock;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +020067 u32 next_target_id;
68};
69#define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
70
71
72/*
73 * Hack to allow attributes of the same name in different objects.
74 */
75#define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
76 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
77 __ATTR(_name,_mode,_show,_store)
78
79
80/*
81 * Pretty printing helpers
82 */
83
84#define sas_bitfield_name_match(title, table) \
85static ssize_t \
86get_sas_##title##_names(u32 table_key, char *buf) \
87{ \
88 char *prefix = ""; \
89 ssize_t len = 0; \
90 int i; \
91 \
92 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
93 if (table[i].value & table_key) { \
94 len += sprintf(buf + len, "%s%s", \
95 prefix, table[i].name); \
96 prefix = ", "; \
97 } \
98 } \
99 len += sprintf(buf + len, "\n"); \
100 return len; \
101}
102
103#define sas_bitfield_name_search(title, table) \
104static ssize_t \
105get_sas_##title##_names(u32 table_key, char *buf) \
106{ \
107 ssize_t len = 0; \
108 int i; \
109 \
110 for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
111 if (table[i].value == table_key) { \
112 len += sprintf(buf + len, "%s", \
113 table[i].name); \
114 break; \
115 } \
116 } \
117 len += sprintf(buf + len, "\n"); \
118 return len; \
119}
120
121static struct {
122 u32 value;
123 char *name;
124} sas_device_type_names[] = {
125 { SAS_PHY_UNUSED, "unused" },
126 { SAS_END_DEVICE, "end device" },
127 { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
128 { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
129};
130sas_bitfield_name_search(device_type, sas_device_type_names)
131
132
133static struct {
134 u32 value;
135 char *name;
136} sas_protocol_names[] = {
137 { SAS_PROTOCOL_SATA, "sata" },
138 { SAS_PROTOCOL_SMP, "smp" },
139 { SAS_PROTOCOL_STP, "stp" },
140 { SAS_PROTOCOL_SSP, "ssp" },
141};
142sas_bitfield_name_match(protocol, sas_protocol_names)
143
144static struct {
145 u32 value;
146 char *name;
147} sas_linkspeed_names[] = {
148 { SAS_LINK_RATE_UNKNOWN, "Unknown" },
149 { SAS_PHY_DISABLED, "Phy disabled" },
150 { SAS_LINK_RATE_FAILED, "Link Rate failed" },
151 { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
152 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
153 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
154};
155sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
156
157
158/*
159 * SAS host attributes
160 */
161
James Bottomley37be6ee2005-09-09 18:38:27 -0500162static int sas_host_setup(struct transport_container *tc, struct device *dev,
163 struct class_device *cdev)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200164{
165 struct Scsi_Host *shost = dev_to_shost(dev);
166 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
167
168 INIT_LIST_HEAD(&sas_host->rphy_list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100169 mutex_init(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200170 sas_host->next_target_id = 0;
171 return 0;
172}
173
174static DECLARE_TRANSPORT_CLASS(sas_host_class,
175 "sas_host", sas_host_setup, NULL, NULL);
176
177static int sas_host_match(struct attribute_container *cont,
178 struct device *dev)
179{
180 struct Scsi_Host *shost;
181 struct sas_internal *i;
182
183 if (!scsi_is_host_device(dev))
184 return 0;
185 shost = dev_to_shost(dev);
186
187 if (!shost->transportt)
188 return 0;
189 if (shost->transportt->host_attrs.ac.class !=
190 &sas_host_class.class)
191 return 0;
192
193 i = to_sas_internal(shost->transportt);
194 return &i->t.host_attrs.ac == cont;
195}
196
197static int do_sas_phy_delete(struct device *dev, void *data)
198{
199 if (scsi_is_sas_phy(dev))
200 sas_phy_delete(dev_to_phy(dev));
201 return 0;
202}
203
204/**
205 * sas_remove_host -- tear down a Scsi_Host's SAS data structures
206 * @shost: Scsi Host that is torn down
207 *
208 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
209 * Must be called just before scsi_remove_host for SAS HBAs.
210 */
211void sas_remove_host(struct Scsi_Host *shost)
212{
213 device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete);
214}
215EXPORT_SYMBOL(sas_remove_host);
216
217
218/*
219 * SAS Port attributes
220 */
221
222#define sas_phy_show_simple(field, name, format_string, cast) \
223static ssize_t \
224show_sas_phy_##name(struct class_device *cdev, char *buf) \
225{ \
226 struct sas_phy *phy = transport_class_to_phy(cdev); \
227 \
228 return snprintf(buf, 20, format_string, cast phy->field); \
229}
230
231#define sas_phy_simple_attr(field, name, format_string, type) \
232 sas_phy_show_simple(field, name, format_string, (type)) \
233static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
234
235#define sas_phy_show_protocol(field, name) \
236static ssize_t \
237show_sas_phy_##name(struct class_device *cdev, char *buf) \
238{ \
239 struct sas_phy *phy = transport_class_to_phy(cdev); \
240 \
241 if (!phy->field) \
242 return snprintf(buf, 20, "none\n"); \
243 return get_sas_protocol_names(phy->field, buf); \
244}
245
246#define sas_phy_protocol_attr(field, name) \
247 sas_phy_show_protocol(field, name) \
248static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
249
250#define sas_phy_show_linkspeed(field) \
251static ssize_t \
252show_sas_phy_##field(struct class_device *cdev, char *buf) \
253{ \
254 struct sas_phy *phy = transport_class_to_phy(cdev); \
255 \
256 return get_sas_linkspeed_names(phy->field, buf); \
257}
258
259#define sas_phy_linkspeed_attr(field) \
260 sas_phy_show_linkspeed(field) \
261static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
262
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200263#define sas_phy_show_linkerror(field) \
264static ssize_t \
265show_sas_phy_##field(struct class_device *cdev, char *buf) \
266{ \
267 struct sas_phy *phy = transport_class_to_phy(cdev); \
268 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
269 struct sas_internal *i = to_sas_internal(shost->transportt); \
270 int error; \
271 \
Christoph Hellwigac01bbb2005-10-19 20:01:17 +0200272 if (!phy->local_attached) \
273 return -EINVAL; \
274 \
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200275 error = i->f->get_linkerrors(phy); \
276 if (error) \
277 return error; \
278 return snprintf(buf, 20, "%u\n", phy->field); \
279}
280
281#define sas_phy_linkerror_attr(field) \
282 sas_phy_show_linkerror(field) \
283static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
284
285
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200286static ssize_t
287show_sas_device_type(struct class_device *cdev, char *buf)
288{
289 struct sas_phy *phy = transport_class_to_phy(cdev);
290
291 if (!phy->identify.device_type)
292 return snprintf(buf, 20, "none\n");
293 return get_sas_device_type_names(phy->identify.device_type, buf);
294}
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200295static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
296
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200297static ssize_t do_sas_phy_reset(struct class_device *cdev,
298 size_t count, int hard_reset)
299{
300 struct sas_phy *phy = transport_class_to_phy(cdev);
301 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
302 struct sas_internal *i = to_sas_internal(shost->transportt);
303 int error;
304
305 if (!phy->local_attached)
306 return -EINVAL;
307
308 error = i->f->phy_reset(phy, hard_reset);
309 if (error)
310 return error;
311 return count;
312};
313
314static ssize_t store_sas_link_reset(struct class_device *cdev,
315 const char *buf, size_t count)
316{
317 return do_sas_phy_reset(cdev, count, 0);
318}
319static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
320
321static ssize_t store_sas_hard_reset(struct class_device *cdev,
322 const char *buf, size_t count)
323{
324 return do_sas_phy_reset(cdev, count, 1);
325}
326static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
327
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200328sas_phy_protocol_attr(identify.initiator_port_protocols,
329 initiator_port_protocols);
330sas_phy_protocol_attr(identify.target_port_protocols,
331 target_port_protocols);
332sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
333 unsigned long long);
334sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
335sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
336sas_phy_linkspeed_attr(negotiated_linkrate);
337sas_phy_linkspeed_attr(minimum_linkrate_hw);
338sas_phy_linkspeed_attr(minimum_linkrate);
339sas_phy_linkspeed_attr(maximum_linkrate_hw);
340sas_phy_linkspeed_attr(maximum_linkrate);
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200341sas_phy_linkerror_attr(invalid_dword_count);
342sas_phy_linkerror_attr(running_disparity_error_count);
343sas_phy_linkerror_attr(loss_of_dword_sync_count);
344sas_phy_linkerror_attr(phy_reset_problem_count);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200345
346
347static DECLARE_TRANSPORT_CLASS(sas_phy_class,
348 "sas_phy", NULL, NULL, NULL);
349
350static int sas_phy_match(struct attribute_container *cont, struct device *dev)
351{
352 struct Scsi_Host *shost;
353 struct sas_internal *i;
354
355 if (!scsi_is_sas_phy(dev))
356 return 0;
357 shost = dev_to_shost(dev->parent);
358
359 if (!shost->transportt)
360 return 0;
361 if (shost->transportt->host_attrs.ac.class !=
362 &sas_host_class.class)
363 return 0;
364
365 i = to_sas_internal(shost->transportt);
366 return &i->phy_attr_cont.ac == cont;
367}
368
369static void sas_phy_release(struct device *dev)
370{
371 struct sas_phy *phy = dev_to_phy(dev);
372
373 put_device(dev->parent);
374 kfree(phy);
375}
376
377/**
378 * sas_phy_alloc -- allocates and initialize a SAS PHY structure
379 * @parent: Parent device
Moore, Ericd99ca412006-01-26 16:20:02 -0700380 * @number: Phy index
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200381 *
382 * Allocates an SAS PHY structure. It will be added in the device tree
383 * below the device specified by @parent, which has to be either a Scsi_Host
384 * or sas_rphy.
385 *
386 * Returns:
387 * SAS PHY allocated or %NULL if the allocation failed.
388 */
389struct sas_phy *sas_phy_alloc(struct device *parent, int number)
390{
391 struct Scsi_Host *shost = dev_to_shost(parent);
392 struct sas_phy *phy;
393
Jes Sorensen24669f752006-01-16 10:31:18 -0500394 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200395 if (!phy)
396 return NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200397
398 get_device(parent);
399
400 phy->number = number;
401
402 device_initialize(&phy->dev);
403 phy->dev.parent = get_device(parent);
404 phy->dev.release = sas_phy_release;
405 sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
406
407 transport_setup_device(&phy->dev);
408
409 return phy;
410}
411EXPORT_SYMBOL(sas_phy_alloc);
412
413/**
414 * sas_phy_add -- add a SAS PHY to the device hierachy
415 * @phy: The PHY to be added
416 *
417 * Publishes a SAS PHY to the rest of the system.
418 */
419int sas_phy_add(struct sas_phy *phy)
420{
421 int error;
422
423 error = device_add(&phy->dev);
424 if (!error) {
425 transport_add_device(&phy->dev);
426 transport_configure_device(&phy->dev);
427 }
428
429 return error;
430}
431EXPORT_SYMBOL(sas_phy_add);
432
433/**
434 * sas_phy_free -- free a SAS PHY
435 * @phy: SAS PHY to free
436 *
437 * Frees the specified SAS PHY.
438 *
439 * Note:
440 * This function must only be called on a PHY that has not
441 * sucessfully been added using sas_phy_add().
442 */
443void sas_phy_free(struct sas_phy *phy)
444{
445 transport_destroy_device(&phy->dev);
446 put_device(phy->dev.parent);
447 put_device(phy->dev.parent);
448 put_device(phy->dev.parent);
449 kfree(phy);
450}
451EXPORT_SYMBOL(sas_phy_free);
452
453/**
454 * sas_phy_delete -- remove SAS PHY
455 * @phy: SAS PHY to remove
456 *
457 * Removes the specified SAS PHY. If the SAS PHY has an
458 * associated remote PHY it is removed before.
459 */
460void
461sas_phy_delete(struct sas_phy *phy)
462{
463 struct device *dev = &phy->dev;
464
465 if (phy->rphy)
466 sas_rphy_delete(phy->rphy);
467
468 transport_remove_device(dev);
469 device_del(dev);
470 transport_destroy_device(dev);
471 put_device(dev->parent);
472}
473EXPORT_SYMBOL(sas_phy_delete);
474
475/**
476 * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
477 * @dev: device to check
478 *
479 * Returns:
480 * %1 if the device represents a SAS PHY, %0 else
481 */
482int scsi_is_sas_phy(const struct device *dev)
483{
484 return dev->release == sas_phy_release;
485}
486EXPORT_SYMBOL(scsi_is_sas_phy);
487
488/*
489 * SAS remote PHY attributes.
490 */
491
492#define sas_rphy_show_simple(field, name, format_string, cast) \
493static ssize_t \
494show_sas_rphy_##name(struct class_device *cdev, char *buf) \
495{ \
496 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
497 \
498 return snprintf(buf, 20, format_string, cast rphy->field); \
499}
500
501#define sas_rphy_simple_attr(field, name, format_string, type) \
502 sas_rphy_show_simple(field, name, format_string, (type)) \
503static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
504 show_sas_rphy_##name, NULL)
505
506#define sas_rphy_show_protocol(field, name) \
507static ssize_t \
508show_sas_rphy_##name(struct class_device *cdev, char *buf) \
509{ \
510 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
511 \
512 if (!rphy->field) \
513 return snprintf(buf, 20, "none\n"); \
514 return get_sas_protocol_names(rphy->field, buf); \
515}
516
517#define sas_rphy_protocol_attr(field, name) \
518 sas_rphy_show_protocol(field, name) \
519static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
520 show_sas_rphy_##name, NULL)
521
522static ssize_t
523show_sas_rphy_device_type(struct class_device *cdev, char *buf)
524{
525 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
526
527 if (!rphy->identify.device_type)
528 return snprintf(buf, 20, "none\n");
529 return get_sas_device_type_names(
530 rphy->identify.device_type, buf);
531}
532
533static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
534 show_sas_rphy_device_type, NULL);
535
Christoph Hellwiga0125642006-02-16 13:31:47 +0100536static ssize_t
537show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
538{
539 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
540 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
541 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
542 struct sas_internal *i = to_sas_internal(shost->transportt);
543 u64 identifier;
544 int error;
545
546 /*
547 * Only devices behind an expander are supported, because the
548 * enclosure identifier is a SMP feature.
549 */
550 if (phy->local_attached)
551 return -EINVAL;
552
553 error = i->f->get_enclosure_identifier(rphy, &identifier);
554 if (error)
555 return error;
556 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
557}
558
559static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
560 show_sas_rphy_enclosure_identifier, NULL);
561
562static ssize_t
563show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
564{
565 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
566 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
567 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
568 struct sas_internal *i = to_sas_internal(shost->transportt);
569 int val;
570
571 if (phy->local_attached)
572 return -EINVAL;
573
574 val = i->f->get_bay_identifier(rphy);
575 if (val < 0)
576 return val;
577 return sprintf(buf, "%d\n", val);
578}
579
580static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
581 show_sas_rphy_bay_identifier, NULL);
582
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200583sas_rphy_protocol_attr(identify.initiator_port_protocols,
584 initiator_port_protocols);
585sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
586sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
587 unsigned long long);
588sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
589
590static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
591 "sas_rphy", NULL, NULL, NULL);
592
593static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
594{
595 struct Scsi_Host *shost;
596 struct sas_internal *i;
597
598 if (!scsi_is_sas_rphy(dev))
599 return 0;
600 shost = dev_to_shost(dev->parent->parent);
601
602 if (!shost->transportt)
603 return 0;
604 if (shost->transportt->host_attrs.ac.class !=
605 &sas_host_class.class)
606 return 0;
607
608 i = to_sas_internal(shost->transportt);
609 return &i->rphy_attr_cont.ac == cont;
610}
611
612static void sas_rphy_release(struct device *dev)
613{
614 struct sas_rphy *rphy = dev_to_rphy(dev);
615
616 put_device(dev->parent);
617 kfree(rphy);
618}
619
620/**
621 * sas_rphy_alloc -- allocates and initialize a SAS remote PHY structure
622 * @parent: SAS PHY this remote PHY is conneted to
623 *
624 * Allocates an SAS remote PHY structure, connected to @parent.
625 *
626 * Returns:
627 * SAS PHY allocated or %NULL if the allocation failed.
628 */
629struct sas_rphy *sas_rphy_alloc(struct sas_phy *parent)
630{
631 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
632 struct sas_rphy *rphy;
633
Jes Sorensen24669f752006-01-16 10:31:18 -0500634 rphy = kzalloc(sizeof(*rphy), GFP_KERNEL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200635 if (!rphy) {
636 put_device(&parent->dev);
637 return NULL;
638 }
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200639
640 device_initialize(&rphy->dev);
641 rphy->dev.parent = get_device(&parent->dev);
642 rphy->dev.release = sas_rphy_release;
Moore, Ericd99ca412006-01-26 16:20:02 -0700643 sprintf(rphy->dev.bus_id, "rphy-%d:%d-%d",
644 shost->host_no, parent->port_identifier, parent->number);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200645 transport_setup_device(&rphy->dev);
646
647 return rphy;
648}
649EXPORT_SYMBOL(sas_rphy_alloc);
650
651/**
652 * sas_rphy_add -- add a SAS remote PHY to the device hierachy
653 * @rphy: The remote PHY to be added
654 *
655 * Publishes a SAS remote PHY to the rest of the system.
656 */
657int sas_rphy_add(struct sas_rphy *rphy)
658{
659 struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
660 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
661 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
662 struct sas_identify *identify = &rphy->identify;
663 int error;
664
665 if (parent->rphy)
666 return -ENXIO;
667 parent->rphy = rphy;
668
669 error = device_add(&rphy->dev);
670 if (error)
671 return error;
672 transport_add_device(&rphy->dev);
673 transport_configure_device(&rphy->dev);
674
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100675 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200676 list_add_tail(&rphy->list, &sas_host->rphy_list);
677 if (identify->device_type == SAS_END_DEVICE &&
678 (identify->target_port_protocols &
679 (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
680 rphy->scsi_target_id = sas_host->next_target_id++;
681 else
682 rphy->scsi_target_id = -1;
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100683 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200684
685 if (rphy->scsi_target_id != -1) {
Moore, Erice6bc8632006-01-13 16:33:59 -0700686 scsi_scan_target(&rphy->dev, parent->port_identifier,
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200687 rphy->scsi_target_id, ~0, 0);
688 }
689
690 return 0;
691}
692EXPORT_SYMBOL(sas_rphy_add);
693
694/**
695 * sas_rphy_free -- free a SAS remote PHY
696 * @rphy SAS remote PHY to free
697 *
698 * Frees the specified SAS remote PHY.
699 *
700 * Note:
701 * This function must only be called on a remote
702 * PHY that has not sucessfully been added using
703 * sas_rphy_add().
704 */
705void sas_rphy_free(struct sas_rphy *rphy)
706{
707 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
708 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
709
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100710 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200711 list_del(&rphy->list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100712 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200713
714 transport_destroy_device(&rphy->dev);
715 put_device(rphy->dev.parent);
716 put_device(rphy->dev.parent);
717 put_device(rphy->dev.parent);
718 kfree(rphy);
719}
720EXPORT_SYMBOL(sas_rphy_free);
721
722/**
723 * sas_rphy_delete -- remove SAS remote PHY
724 * @rphy: SAS remote PHY to remove
725 *
726 * Removes the specified SAS remote PHY.
727 */
728void
729sas_rphy_delete(struct sas_rphy *rphy)
730{
731 struct device *dev = &rphy->dev;
732 struct sas_phy *parent = dev_to_phy(dev->parent);
733 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
734 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
735
Christoph Hellwigd4054232006-01-04 13:45:20 +0100736 switch (rphy->identify.device_type) {
737 case SAS_END_DEVICE:
738 scsi_remove_target(dev);
739 break;
740 case SAS_EDGE_EXPANDER_DEVICE:
741 case SAS_FANOUT_EXPANDER_DEVICE:
742 device_for_each_child(dev, NULL, do_sas_phy_delete);
743 break;
744 default:
745 break;
746 }
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200747
Christoph Hellwigfe8b2302005-09-25 23:10:33 +0200748 transport_remove_device(dev);
749 device_del(dev);
750 transport_destroy_device(dev);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200751
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100752 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200753 list_del(&rphy->list);
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100754 mutex_unlock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200755
Christoph Hellwig33b114e2006-01-11 14:20:43 +0100756 parent->rphy = NULL;
757
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200758 put_device(&parent->dev);
759}
760EXPORT_SYMBOL(sas_rphy_delete);
761
762/**
763 * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
764 * @dev: device to check
765 *
766 * Returns:
767 * %1 if the device represents a SAS remote PHY, %0 else
768 */
769int scsi_is_sas_rphy(const struct device *dev)
770{
771 return dev->release == sas_rphy_release;
772}
773EXPORT_SYMBOL(scsi_is_sas_rphy);
774
775
776/*
777 * SCSI scan helper
778 */
779
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100780static int sas_user_scan(struct Scsi_Host *shost, uint channel,
781 uint id, uint lun)
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200782{
783 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
784 struct sas_rphy *rphy;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200785
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100786 mutex_lock(&sas_host->lock);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200787 list_for_each_entry(rphy, &sas_host->rphy_list, list) {
788 struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200789
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100790 if (rphy->scsi_target_id == -1)
791 continue;
792
Moore, Erice6bc8632006-01-13 16:33:59 -0700793 if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) &&
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100794 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
Moore, Erice6bc8632006-01-13 16:33:59 -0700795 scsi_scan_target(&rphy->dev, parent->port_identifier,
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100796 rphy->scsi_target_id, lun, 1);
797 }
798 }
799 mutex_unlock(&sas_host->lock);
800
801 return 0;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200802}
803
804
805/*
806 * Setup / Teardown code
807 */
808
809#define SETUP_RPORT_ATTRIBUTE(field) \
810 i->private_rphy_attrs[count] = class_device_attr_##field; \
811 i->private_rphy_attrs[count].attr.mode = S_IRUGO; \
812 i->private_rphy_attrs[count].store = NULL; \
813 i->rphy_attrs[count] = &i->private_rphy_attrs[count]; \
814 count++
815
816#define SETUP_PORT_ATTRIBUTE(field) \
817 i->private_phy_attrs[count] = class_device_attr_##field; \
818 i->private_phy_attrs[count].attr.mode = S_IRUGO; \
819 i->private_phy_attrs[count].store = NULL; \
820 i->phy_attrs[count] = &i->private_phy_attrs[count]; \
821 count++
822
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200823#define SETUP_PORT_ATTRIBUTE_WRONLY(field) \
824 i->private_phy_attrs[count] = class_device_attr_##field; \
825 i->private_phy_attrs[count].attr.mode = S_IWUGO; \
826 i->private_phy_attrs[count].show = NULL; \
827 i->phy_attrs[count] = &i->private_phy_attrs[count]; \
828 count++
829
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200830
831/**
832 * sas_attach_transport -- instantiate SAS transport template
833 * @ft: SAS transport class function template
834 */
835struct scsi_transport_template *
836sas_attach_transport(struct sas_function_template *ft)
837{
838 struct sas_internal *i;
839 int count;
840
Jes Sorensen24669f752006-01-16 10:31:18 -0500841 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200842 if (!i)
843 return NULL;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200844
Christoph Hellwige02f3f52006-01-13 19:04:00 +0100845 i->t.user_scan = sas_user_scan;
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200846
847 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
848 i->t.host_attrs.ac.class = &sas_host_class.class;
849 i->t.host_attrs.ac.match = sas_host_match;
850 transport_container_register(&i->t.host_attrs);
851 i->t.host_size = sizeof(struct sas_host_attrs);
852
853 i->phy_attr_cont.ac.class = &sas_phy_class.class;
854 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
855 i->phy_attr_cont.ac.match = sas_phy_match;
856 transport_container_register(&i->phy_attr_cont);
857
858 i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
859 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
860 i->rphy_attr_cont.ac.match = sas_rphy_match;
861 transport_container_register(&i->rphy_attr_cont);
862
863 i->f = ft;
864
865 count = 0;
866 i->host_attrs[count] = NULL;
867
868 count = 0;
869 SETUP_PORT_ATTRIBUTE(initiator_port_protocols);
870 SETUP_PORT_ATTRIBUTE(target_port_protocols);
871 SETUP_PORT_ATTRIBUTE(device_type);
872 SETUP_PORT_ATTRIBUTE(sas_address);
873 SETUP_PORT_ATTRIBUTE(phy_identifier);
874 SETUP_PORT_ATTRIBUTE(port_identifier);
875 SETUP_PORT_ATTRIBUTE(negotiated_linkrate);
876 SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw);
877 SETUP_PORT_ATTRIBUTE(minimum_linkrate);
878 SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw);
879 SETUP_PORT_ATTRIBUTE(maximum_linkrate);
Christoph Hellwigc3ee74c2005-09-19 21:59:42 +0200880
881 SETUP_PORT_ATTRIBUTE(invalid_dword_count);
882 SETUP_PORT_ATTRIBUTE(running_disparity_error_count);
883 SETUP_PORT_ATTRIBUTE(loss_of_dword_sync_count);
884 SETUP_PORT_ATTRIBUTE(phy_reset_problem_count);
Christoph Hellwig07ba3a92005-10-19 20:01:31 +0200885 SETUP_PORT_ATTRIBUTE_WRONLY(link_reset);
886 SETUP_PORT_ATTRIBUTE_WRONLY(hard_reset);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200887 i->phy_attrs[count] = NULL;
888
889 count = 0;
890 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
891 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
892 SETUP_RPORT_ATTRIBUTE(rphy_device_type);
893 SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
894 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
Christoph Hellwiga0125642006-02-16 13:31:47 +0100895 SETUP_RPORT_ATTRIBUTE(rphy_enclosure_identifier);
896 SETUP_RPORT_ATTRIBUTE(rphy_bay_identifier);
Christoph Hellwigc7ebbbc2005-09-09 16:22:50 +0200897 i->rphy_attrs[count] = NULL;
898
899 return &i->t;
900}
901EXPORT_SYMBOL(sas_attach_transport);
902
903/**
904 * sas_release_transport -- release SAS transport template instance
905 * @t: transport template instance
906 */
907void sas_release_transport(struct scsi_transport_template *t)
908{
909 struct sas_internal *i = to_sas_internal(t);
910
911 transport_container_unregister(&i->t.host_attrs);
912 transport_container_unregister(&i->phy_attr_cont);
913 transport_container_unregister(&i->rphy_attr_cont);
914
915 kfree(i);
916}
917EXPORT_SYMBOL(sas_release_transport);
918
919static __init int sas_transport_init(void)
920{
921 int error;
922
923 error = transport_class_register(&sas_host_class);
924 if (error)
925 goto out;
926 error = transport_class_register(&sas_phy_class);
927 if (error)
928 goto out_unregister_transport;
929 error = transport_class_register(&sas_rphy_class);
930 if (error)
931 goto out_unregister_phy;
932
933 return 0;
934
935 out_unregister_phy:
936 transport_class_unregister(&sas_phy_class);
937 out_unregister_transport:
938 transport_class_unregister(&sas_host_class);
939 out:
940 return error;
941
942}
943
944static void __exit sas_transport_exit(void)
945{
946 transport_class_unregister(&sas_host_class);
947 transport_class_unregister(&sas_phy_class);
948 transport_class_unregister(&sas_rphy_class);
949}
950
951MODULE_AUTHOR("Christoph Hellwig");
952MODULE_DESCRIPTION("SAS Transphy Attributes");
953MODULE_LICENSE("GPL");
954
955module_init(sas_transport_init);
956module_exit(sas_transport_exit);