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