blob: e2d94972962d69d766e99e8ade594040ae8d9429 [file] [log] [blame]
Gwendal Grignoud9027472010-05-25 12:31:38 -07001/*
2 * Copyright 2008 ioogle, Inc. All rights reserved.
3 * Released under GPL v2.
4 *
5 * Libata transport class.
6 *
7 * The ATA transport class contains common code to deal with ATA HBAs,
8 * an approximated representation of ATA topologies in the driver model,
9 * and various sysfs attributes to expose these topologies and management
10 * interfaces to user-space.
11 *
12 * There are 3 objects defined in in this class:
13 * - ata_port
14 * - ata_link
15 * - ata_device
16 * Each port has a link object. Each link can have up to two devices for PATA
17 * and generally one for SATA.
18 * If there is SATA port multiplier [PMP], 15 additional ata_link object are
19 * created.
20 *
21 * These objects are created when the ata host is initialized and when a PMP is
22 * found. They are removed only when the HBA is removed, cleaned before the
23 * error handler runs.
24 */
25
26
27#include <linux/kernel.h>
28#include <linux/blkdev.h>
29#include <linux/spinlock.h>
Jeff Garzik6a2148c2010-08-19 16:11:32 -040030#include <linux/slab.h>
Gwendal Grignoud9027472010-05-25 12:31:38 -070031#include <scsi/scsi_transport.h>
32#include <linux/libata.h>
33#include <linux/hdreg.h>
34#include <linux/uaccess.h>
Lin Ming9ee4f392011-12-05 09:20:28 +080035#include <linux/pm_runtime.h>
Gwendal Grignoud9027472010-05-25 12:31:38 -070036
37#include "libata.h"
38#include "libata-transport.h"
39
David Milburne628dc92013-05-14 13:48:40 -050040#define ATA_PORT_ATTRS 3
Gwendal Grignoud9027472010-05-25 12:31:38 -070041#define ATA_LINK_ATTRS 3
42#define ATA_DEV_ATTRS 9
43
44struct scsi_transport_template;
45struct scsi_transport_template *ata_scsi_transport_template;
46
47struct ata_internal {
48 struct scsi_transport_template t;
49
50 struct device_attribute private_port_attrs[ATA_PORT_ATTRS];
51 struct device_attribute private_link_attrs[ATA_LINK_ATTRS];
52 struct device_attribute private_dev_attrs[ATA_DEV_ATTRS];
53
54 struct transport_container link_attr_cont;
55 struct transport_container dev_attr_cont;
56
57 /*
58 * The array of null terminated pointers to attributes
59 * needed by scsi_sysfs.c
60 */
61 struct device_attribute *link_attrs[ATA_LINK_ATTRS + 1];
62 struct device_attribute *port_attrs[ATA_PORT_ATTRS + 1];
63 struct device_attribute *dev_attrs[ATA_DEV_ATTRS + 1];
64};
65#define to_ata_internal(tmpl) container_of(tmpl, struct ata_internal, t)
66
67
68#define tdev_to_device(d) \
69 container_of((d), struct ata_device, tdev)
70#define transport_class_to_dev(dev) \
71 tdev_to_device((dev)->parent)
72
73#define tdev_to_link(d) \
74 container_of((d), struct ata_link, tdev)
75#define transport_class_to_link(dev) \
76 tdev_to_link((dev)->parent)
77
78#define tdev_to_port(d) \
79 container_of((d), struct ata_port, tdev)
80#define transport_class_to_port(dev) \
81 tdev_to_port((dev)->parent)
82
83
84/* Device objects are always created whit link objects */
85static int ata_tdev_add(struct ata_device *dev);
86static void ata_tdev_delete(struct ata_device *dev);
87
88
89/*
90 * Hack to allow attributes of the same name in different objects.
91 */
92#define ATA_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
93 struct device_attribute device_attr_##_prefix##_##_name = \
94 __ATTR(_name,_mode,_show,_store)
95
96#define ata_bitfield_name_match(title, table) \
97static ssize_t \
98get_ata_##title##_names(u32 table_key, char *buf) \
99{ \
100 char *prefix = ""; \
101 ssize_t len = 0; \
102 int i; \
103 \
104 for (i = 0; i < ARRAY_SIZE(table); i++) { \
105 if (table[i].value & table_key) { \
106 len += sprintf(buf + len, "%s%s", \
107 prefix, table[i].name); \
108 prefix = ", "; \
109 } \
110 } \
111 len += sprintf(buf + len, "\n"); \
112 return len; \
113}
114
115#define ata_bitfield_name_search(title, table) \
116static ssize_t \
117get_ata_##title##_names(u32 table_key, char *buf) \
118{ \
119 ssize_t len = 0; \
120 int i; \
121 \
122 for (i = 0; i < ARRAY_SIZE(table); i++) { \
123 if (table[i].value == table_key) { \
124 len += sprintf(buf + len, "%s", \
125 table[i].name); \
126 break; \
127 } \
128 } \
129 len += sprintf(buf + len, "\n"); \
130 return len; \
131}
132
133static struct {
134 u32 value;
135 char *name;
136} ata_class_names[] = {
137 { ATA_DEV_UNKNOWN, "unknown" },
138 { ATA_DEV_ATA, "ata" },
139 { ATA_DEV_ATA_UNSUP, "ata" },
140 { ATA_DEV_ATAPI, "atapi" },
141 { ATA_DEV_ATAPI_UNSUP, "atapi" },
142 { ATA_DEV_PMP, "pmp" },
143 { ATA_DEV_PMP_UNSUP, "pmp" },
144 { ATA_DEV_SEMB, "semb" },
145 { ATA_DEV_SEMB_UNSUP, "semb" },
Hannes Reinecke9162c652014-11-05 13:08:21 +0100146 { ATA_DEV_ZAC, "zac" },
Gwendal Grignoud9027472010-05-25 12:31:38 -0700147 { ATA_DEV_NONE, "none" }
148};
149ata_bitfield_name_search(class, ata_class_names)
150
151
152static struct {
153 u32 value;
154 char *name;
155} ata_err_names[] = {
156 { AC_ERR_DEV, "DeviceError" },
157 { AC_ERR_HSM, "HostStateMachineError" },
158 { AC_ERR_TIMEOUT, "Timeout" },
159 { AC_ERR_MEDIA, "MediaError" },
160 { AC_ERR_ATA_BUS, "BusError" },
161 { AC_ERR_HOST_BUS, "HostBusError" },
162 { AC_ERR_SYSTEM, "SystemError" },
163 { AC_ERR_INVALID, "InvalidArg" },
164 { AC_ERR_OTHER, "Unknown" },
165 { AC_ERR_NODEV_HINT, "NoDeviceHint" },
166 { AC_ERR_NCQ, "NCQError" }
167};
168ata_bitfield_name_match(err, ata_err_names)
169
170static struct {
171 u32 value;
172 char *name;
173} ata_xfer_names[] = {
174 { XFER_UDMA_7, "XFER_UDMA_7" },
175 { XFER_UDMA_6, "XFER_UDMA_6" },
176 { XFER_UDMA_5, "XFER_UDMA_5" },
177 { XFER_UDMA_4, "XFER_UDMA_4" },
178 { XFER_UDMA_3, "XFER_UDMA_3" },
179 { XFER_UDMA_2, "XFER_UDMA_2" },
180 { XFER_UDMA_1, "XFER_UDMA_1" },
181 { XFER_UDMA_0, "XFER_UDMA_0" },
182 { XFER_MW_DMA_4, "XFER_MW_DMA_4" },
183 { XFER_MW_DMA_3, "XFER_MW_DMA_3" },
184 { XFER_MW_DMA_2, "XFER_MW_DMA_2" },
185 { XFER_MW_DMA_1, "XFER_MW_DMA_1" },
186 { XFER_MW_DMA_0, "XFER_MW_DMA_0" },
187 { XFER_SW_DMA_2, "XFER_SW_DMA_2" },
188 { XFER_SW_DMA_1, "XFER_SW_DMA_1" },
189 { XFER_SW_DMA_0, "XFER_SW_DMA_0" },
190 { XFER_PIO_6, "XFER_PIO_6" },
191 { XFER_PIO_5, "XFER_PIO_5" },
192 { XFER_PIO_4, "XFER_PIO_4" },
193 { XFER_PIO_3, "XFER_PIO_3" },
194 { XFER_PIO_2, "XFER_PIO_2" },
195 { XFER_PIO_1, "XFER_PIO_1" },
196 { XFER_PIO_0, "XFER_PIO_0" },
197 { XFER_PIO_SLOW, "XFER_PIO_SLOW" }
198};
199ata_bitfield_name_match(xfer,ata_xfer_names)
200
201/*
202 * ATA Port attributes
203 */
204#define ata_port_show_simple(field, name, format_string, cast) \
205static ssize_t \
206show_ata_port_##name(struct device *dev, \
207 struct device_attribute *attr, char *buf) \
208{ \
209 struct ata_port *ap = transport_class_to_port(dev); \
210 \
211 return snprintf(buf, 20, format_string, cast ap->field); \
212}
213
214#define ata_port_simple_attr(field, name, format_string, type) \
215 ata_port_show_simple(field, name, format_string, (type)) \
216static DEVICE_ATTR(name, S_IRUGO, show_ata_port_##name, NULL)
217
218ata_port_simple_attr(nr_pmp_links, nr_pmp_links, "%d\n", int);
219ata_port_simple_attr(stats.idle_irq, idle_irq, "%ld\n", unsigned long);
David Milburne628dc92013-05-14 13:48:40 -0500220ata_port_simple_attr(local_port_no, port_no, "%u\n", unsigned int);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700221
222static DECLARE_TRANSPORT_CLASS(ata_port_class,
223 "ata_port", NULL, NULL, NULL);
224
225static void ata_tport_release(struct device *dev)
226{
227 put_device(dev->parent);
228}
229
230/**
231 * ata_is_port -- check if a struct device represents a ATA port
232 * @dev: device to check
233 *
234 * Returns:
235 * %1 if the device represents a ATA Port, %0 else
236 */
H Hartley Sweetenb0e73af2012-04-16 18:04:41 -0700237static int ata_is_port(const struct device *dev)
Gwendal Grignoud9027472010-05-25 12:31:38 -0700238{
239 return dev->release == ata_tport_release;
240}
241
242static int ata_tport_match(struct attribute_container *cont,
243 struct device *dev)
244{
245 if (!ata_is_port(dev))
246 return 0;
247 return &ata_scsi_transport_template->host_attrs.ac == cont;
248}
249
250/**
251 * ata_tport_delete -- remove ATA PORT
252 * @port: ATA PORT to remove
253 *
254 * Removes the specified ATA PORT. Remove the associated link as well.
255 */
256void ata_tport_delete(struct ata_port *ap)
257{
258 struct device *dev = &ap->tdev;
259
260 ata_tlink_delete(&ap->link);
261
262 transport_remove_device(dev);
263 device_del(dev);
264 transport_destroy_device(dev);
265 put_device(dev);
266}
267
268/** ata_tport_add - initialize a transport ATA port structure
269 *
270 * @parent: parent device
271 * @ap: existing ata_port structure
272 *
273 * Initialize a ATA port structure for sysfs. It will be added to the device
274 * tree below the device specified by @parent which could be a PCI device.
275 *
276 * Returns %0 on success
277 */
278int ata_tport_add(struct device *parent,
279 struct ata_port *ap)
280{
281 int error;
282 struct device *dev = &ap->tdev;
283
284 device_initialize(dev);
Lin Ming5ef41082011-12-05 09:20:27 +0800285 dev->type = &ata_port_type;
Gwendal Grignoud9027472010-05-25 12:31:38 -0700286
287 dev->parent = get_device(parent);
288 dev->release = ata_tport_release;
289 dev_set_name(dev, "ata%d", ap->print_id);
290 transport_setup_device(dev);
Aaron Luf1bc1e42013-08-23 10:17:54 +0800291 ata_acpi_bind_port(ap);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700292 error = device_add(dev);
293 if (error) {
294 goto tport_err;
295 }
296
Lin Ming966f1212012-01-16 13:23:23 +0800297 device_enable_async_suspend(dev);
Lin Ming9ee4f392011-12-05 09:20:28 +0800298 pm_runtime_set_active(dev);
299 pm_runtime_enable(dev);
Lin Ming0c8d32c2012-04-18 09:29:47 +0800300 pm_runtime_forbid(dev);
Lin Ming9ee4f392011-12-05 09:20:28 +0800301
Gwendal Grignoud9027472010-05-25 12:31:38 -0700302 transport_add_device(dev);
303 transport_configure_device(dev);
304
305 error = ata_tlink_add(&ap->link);
306 if (error) {
307 goto tport_link_err;
308 }
309 return 0;
310
311 tport_link_err:
312 transport_remove_device(dev);
313 device_del(dev);
314
315 tport_err:
316 transport_destroy_device(dev);
317 put_device(dev);
318 return error;
319}
320
321
322/*
323 * ATA link attributes
324 */
Gwendal Grignou3e85c3e2013-10-25 16:28:57 -0700325static int noop(int x) { return x; }
Gwendal Grignoud9027472010-05-25 12:31:38 -0700326
Gwendal Grignou3e85c3e2013-10-25 16:28:57 -0700327#define ata_link_show_linkspeed(field, format) \
Gwendal Grignoud9027472010-05-25 12:31:38 -0700328static ssize_t \
329show_ata_link_##field(struct device *dev, \
330 struct device_attribute *attr, char *buf) \
331{ \
332 struct ata_link *link = transport_class_to_link(dev); \
333 \
Gwendal Grignou3e85c3e2013-10-25 16:28:57 -0700334 return sprintf(buf, "%s\n", sata_spd_string(format(link->field))); \
Gwendal Grignoud9027472010-05-25 12:31:38 -0700335}
336
Gwendal Grignou3e85c3e2013-10-25 16:28:57 -0700337#define ata_link_linkspeed_attr(field, format) \
338 ata_link_show_linkspeed(field, format) \
Gwendal Grignoud9027472010-05-25 12:31:38 -0700339static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL)
340
Gwendal Grignou3e85c3e2013-10-25 16:28:57 -0700341ata_link_linkspeed_attr(hw_sata_spd_limit, fls);
342ata_link_linkspeed_attr(sata_spd_limit, fls);
343ata_link_linkspeed_attr(sata_spd, noop);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700344
345
346static DECLARE_TRANSPORT_CLASS(ata_link_class,
347 "ata_link", NULL, NULL, NULL);
348
349static void ata_tlink_release(struct device *dev)
350{
351 put_device(dev->parent);
352}
353
354/**
355 * ata_is_link -- check if a struct device represents a ATA link
356 * @dev: device to check
357 *
358 * Returns:
359 * %1 if the device represents a ATA link, %0 else
360 */
H Hartley Sweetenb0e73af2012-04-16 18:04:41 -0700361static int ata_is_link(const struct device *dev)
Gwendal Grignoud9027472010-05-25 12:31:38 -0700362{
363 return dev->release == ata_tlink_release;
364}
365
366static int ata_tlink_match(struct attribute_container *cont,
367 struct device *dev)
368{
369 struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
370 if (!ata_is_link(dev))
371 return 0;
372 return &i->link_attr_cont.ac == cont;
373}
374
375/**
376 * ata_tlink_delete -- remove ATA LINK
377 * @port: ATA LINK to remove
378 *
379 * Removes the specified ATA LINK. remove associated ATA device(s) as well.
380 */
381void ata_tlink_delete(struct ata_link *link)
382{
383 struct device *dev = &link->tdev;
384 struct ata_device *ata_dev;
385
386 ata_for_each_dev(ata_dev, link, ALL) {
387 ata_tdev_delete(ata_dev);
388 }
389
390 transport_remove_device(dev);
391 device_del(dev);
392 transport_destroy_device(dev);
393 put_device(dev);
394}
395
396/**
397 * ata_tlink_add -- initialize a transport ATA link structure
398 * @link: allocated ata_link structure.
399 *
400 * Initialize an ATA LINK structure for sysfs. It will be added in the
401 * device tree below the ATA PORT it belongs to.
402 *
403 * Returns %0 on success
404 */
405int ata_tlink_add(struct ata_link *link)
406{
407 struct device *dev = &link->tdev;
408 struct ata_port *ap = link->ap;
409 struct ata_device *ata_dev;
410 int error;
411
412 device_initialize(dev);
413 dev->parent = get_device(&ap->tdev);
414 dev->release = ata_tlink_release;
415 if (ata_is_host_link(link))
416 dev_set_name(dev, "link%d", ap->print_id);
417 else
418 dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp);
419
420 transport_setup_device(dev);
421
422 error = device_add(dev);
423 if (error) {
424 goto tlink_err;
425 }
426
427 transport_add_device(dev);
428 transport_configure_device(dev);
429
430 ata_for_each_dev(ata_dev, link, ALL) {
431 error = ata_tdev_add(ata_dev);
432 if (error) {
433 goto tlink_dev_err;
434 }
435 }
436 return 0;
437 tlink_dev_err:
438 while (--ata_dev >= link->device) {
439 ata_tdev_delete(ata_dev);
440 }
441 transport_remove_device(dev);
442 device_del(dev);
443 tlink_err:
444 transport_destroy_device(dev);
445 put_device(dev);
446 return error;
447}
448
449/*
450 * ATA device attributes
451 */
452
453#define ata_dev_show_class(title, field) \
454static ssize_t \
455show_ata_dev_##field(struct device *dev, \
456 struct device_attribute *attr, char *buf) \
457{ \
458 struct ata_device *ata_dev = transport_class_to_dev(dev); \
459 \
460 return get_ata_##title##_names(ata_dev->field, buf); \
461}
462
463#define ata_dev_attr(title, field) \
464 ata_dev_show_class(title, field) \
465static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL)
466
467ata_dev_attr(class, class);
468ata_dev_attr(xfer, pio_mode);
469ata_dev_attr(xfer, dma_mode);
470ata_dev_attr(xfer, xfer_mode);
471
472
473#define ata_dev_show_simple(field, format_string, cast) \
474static ssize_t \
475show_ata_dev_##field(struct device *dev, \
476 struct device_attribute *attr, char *buf) \
477{ \
478 struct ata_device *ata_dev = transport_class_to_dev(dev); \
479 \
480 return snprintf(buf, 20, format_string, cast ata_dev->field); \
481}
482
483#define ata_dev_simple_attr(field, format_string, type) \
484 ata_dev_show_simple(field, format_string, (type)) \
485static DEVICE_ATTR(field, S_IRUGO, \
486 show_ata_dev_##field, NULL)
487
488ata_dev_simple_attr(spdn_cnt, "%d\n", int);
489
490struct ata_show_ering_arg {
491 char* buf;
492 int written;
493};
494
495static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg)
496{
497 struct ata_show_ering_arg* arg = void_arg;
498 struct timespec time;
499
500 jiffies_to_timespec(ent->timestamp,&time);
501 arg->written += sprintf(arg->buf + arg->written,
502 "[%5lu.%06lu]",
503 time.tv_sec, time.tv_nsec);
504 arg->written += get_ata_err_names(ent->err_mask,
505 arg->buf + arg->written);
506 return 0;
507}
508
509static ssize_t
510show_ata_dev_ering(struct device *dev,
511 struct device_attribute *attr, char *buf)
512{
513 struct ata_device *ata_dev = transport_class_to_dev(dev);
514 struct ata_show_ering_arg arg = { buf, 0 };
515
516 ata_ering_map(&ata_dev->ering, ata_show_ering, &arg);
517 return arg.written;
518}
519
520
521static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL);
522
523static ssize_t
524show_ata_dev_id(struct device *dev,
525 struct device_attribute *attr, char *buf)
526{
527 struct ata_device *ata_dev = transport_class_to_dev(dev);
528 int written = 0, i = 0;
529
530 if (ata_dev->class == ATA_DEV_PMP)
531 return 0;
532 for(i=0;i<ATA_ID_WORDS;i++) {
533 written += snprintf(buf+written, 20, "%04x%c",
534 ata_dev->id[i],
535 ((i+1) & 7) ? ' ' : '\n');
536 }
537 return written;
538}
539
540static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL);
541
542static ssize_t
543show_ata_dev_gscr(struct device *dev,
544 struct device_attribute *attr, char *buf)
545{
546 struct ata_device *ata_dev = transport_class_to_dev(dev);
547 int written = 0, i = 0;
548
549 if (ata_dev->class != ATA_DEV_PMP)
550 return 0;
551 for(i=0;i<SATA_PMP_GSCR_DWORDS;i++) {
552 written += snprintf(buf+written, 20, "%08x%c",
553 ata_dev->gscr[i],
554 ((i+1) & 3) ? ' ' : '\n');
555 }
556 if (SATA_PMP_GSCR_DWORDS & 3)
557 buf[written-1] = '\n';
558 return written;
559}
560
561static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL);
562
Martin K. Petersenf3030742015-05-04 21:54:19 -0400563static ssize_t
564show_ata_dev_trim(struct device *dev,
565 struct device_attribute *attr, char *buf)
566{
567 struct ata_device *ata_dev = transport_class_to_dev(dev);
568 unsigned char *mode;
569
570 if (!ata_id_has_trim(ata_dev->id))
571 mode = "unsupported";
Arne Fitzenreiter71d126f2015-07-15 13:54:36 +0200572 else if (ata_dev->horkage & ATA_HORKAGE_NOTRIM)
573 mode = "forced_unsupported";
Martin K. Petersenf3030742015-05-04 21:54:19 -0400574 else if (ata_dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM)
575 mode = "forced_unqueued";
576 else if (ata_fpdma_dsm_supported(ata_dev))
577 mode = "queued";
578 else
579 mode = "unqueued";
580
581 return snprintf(buf, 20, "%s\n", mode);
582}
583
584static DEVICE_ATTR(trim, S_IRUGO, show_ata_dev_trim, NULL);
585
Gwendal Grignoud9027472010-05-25 12:31:38 -0700586static DECLARE_TRANSPORT_CLASS(ata_dev_class,
587 "ata_device", NULL, NULL, NULL);
588
589static void ata_tdev_release(struct device *dev)
590{
591 put_device(dev->parent);
592}
593
594/**
595 * ata_is_ata_dev -- check if a struct device represents a ATA device
596 * @dev: device to check
597 *
598 * Returns:
599 * %1 if the device represents a ATA device, %0 else
600 */
H Hartley Sweetenb0e73af2012-04-16 18:04:41 -0700601static int ata_is_ata_dev(const struct device *dev)
Gwendal Grignoud9027472010-05-25 12:31:38 -0700602{
603 return dev->release == ata_tdev_release;
604}
605
606static int ata_tdev_match(struct attribute_container *cont,
607 struct device *dev)
608{
609 struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
610 if (!ata_is_ata_dev(dev))
611 return 0;
612 return &i->dev_attr_cont.ac == cont;
613}
614
615/**
616 * ata_tdev_free -- free a ATA LINK
617 * @dev: ATA PHY to free
618 *
619 * Frees the specified ATA PHY.
620 *
621 * Note:
622 * This function must only be called on a PHY that has not
623 * successfully been added using ata_tdev_add().
624 */
625static void ata_tdev_free(struct ata_device *dev)
626{
627 transport_destroy_device(&dev->tdev);
628 put_device(&dev->tdev);
629}
630
631/**
632 * ata_tdev_delete -- remove ATA device
633 * @port: ATA PORT to remove
634 *
635 * Removes the specified ATA device.
636 */
637static void ata_tdev_delete(struct ata_device *ata_dev)
638{
639 struct device *dev = &ata_dev->tdev;
640
641 transport_remove_device(dev);
642 device_del(dev);
643 ata_tdev_free(ata_dev);
644}
645
646
647/**
648 * ata_tdev_add -- initialize a transport ATA device structure.
649 * @ata_dev: ata_dev structure.
650 *
651 * Initialize an ATA device structure for sysfs. It will be added in the
652 * device tree below the ATA LINK device it belongs to.
653 *
654 * Returns %0 on success
655 */
656static int ata_tdev_add(struct ata_device *ata_dev)
657{
658 struct device *dev = &ata_dev->tdev;
659 struct ata_link *link = ata_dev->link;
660 struct ata_port *ap = link->ap;
661 int error;
662
663 device_initialize(dev);
664 dev->parent = get_device(&link->tdev);
665 dev->release = ata_tdev_release;
666 if (ata_is_host_link(link))
667 dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno);
668 else
669 dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp);
670
671 transport_setup_device(dev);
Aaron Luf1bc1e42013-08-23 10:17:54 +0800672 ata_acpi_bind_dev(ata_dev);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700673 error = device_add(dev);
674 if (error) {
675 ata_tdev_free(ata_dev);
676 return error;
677 }
678
679 transport_add_device(dev);
680 transport_configure_device(dev);
681 return 0;
682}
683
684
685/*
686 * Setup / Teardown code
687 */
688
689#define SETUP_TEMPLATE(attrb, field, perm, test) \
690 i->private_##attrb[count] = dev_attr_##field; \
691 i->private_##attrb[count].attr.mode = perm; \
692 i->attrb[count] = &i->private_##attrb[count]; \
693 if (test) \
694 count++
695
696#define SETUP_LINK_ATTRIBUTE(field) \
697 SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1)
698
699#define SETUP_PORT_ATTRIBUTE(field) \
700 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
701
702#define SETUP_DEV_ATTRIBUTE(field) \
703 SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1)
704
705/**
706 * ata_attach_transport -- instantiate ATA transport template
707 */
708struct scsi_transport_template *ata_attach_transport(void)
709{
710 struct ata_internal *i;
711 int count;
712
713 i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL);
714 if (!i)
715 return NULL;
716
717 i->t.eh_strategy_handler = ata_scsi_error;
718 i->t.eh_timed_out = ata_scsi_timed_out;
719 i->t.user_scan = ata_scsi_user_scan;
720
721 i->t.host_attrs.ac.attrs = &i->port_attrs[0];
722 i->t.host_attrs.ac.class = &ata_port_class.class;
723 i->t.host_attrs.ac.match = ata_tport_match;
724 transport_container_register(&i->t.host_attrs);
725
726 i->link_attr_cont.ac.class = &ata_link_class.class;
727 i->link_attr_cont.ac.attrs = &i->link_attrs[0];
728 i->link_attr_cont.ac.match = ata_tlink_match;
729 transport_container_register(&i->link_attr_cont);
730
731 i->dev_attr_cont.ac.class = &ata_dev_class.class;
732 i->dev_attr_cont.ac.attrs = &i->dev_attrs[0];
733 i->dev_attr_cont.ac.match = ata_tdev_match;
734 transport_container_register(&i->dev_attr_cont);
735
736 count = 0;
737 SETUP_PORT_ATTRIBUTE(nr_pmp_links);
738 SETUP_PORT_ATTRIBUTE(idle_irq);
David Milburne628dc92013-05-14 13:48:40 -0500739 SETUP_PORT_ATTRIBUTE(port_no);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700740 BUG_ON(count > ATA_PORT_ATTRS);
741 i->port_attrs[count] = NULL;
742
743 count = 0;
744 SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit);
745 SETUP_LINK_ATTRIBUTE(sata_spd_limit);
746 SETUP_LINK_ATTRIBUTE(sata_spd);
747 BUG_ON(count > ATA_LINK_ATTRS);
748 i->link_attrs[count] = NULL;
749
750 count = 0;
751 SETUP_DEV_ATTRIBUTE(class);
752 SETUP_DEV_ATTRIBUTE(pio_mode);
753 SETUP_DEV_ATTRIBUTE(dma_mode);
754 SETUP_DEV_ATTRIBUTE(xfer_mode);
755 SETUP_DEV_ATTRIBUTE(spdn_cnt);
756 SETUP_DEV_ATTRIBUTE(ering);
757 SETUP_DEV_ATTRIBUTE(id);
758 SETUP_DEV_ATTRIBUTE(gscr);
Martin K. Petersenf3030742015-05-04 21:54:19 -0400759 SETUP_DEV_ATTRIBUTE(trim);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700760 BUG_ON(count > ATA_DEV_ATTRS);
761 i->dev_attrs[count] = NULL;
762
763 return &i->t;
764}
765
766/**
767 * ata_release_transport -- release ATA transport template instance
768 * @t: transport template instance
769 */
770void ata_release_transport(struct scsi_transport_template *t)
771{
772 struct ata_internal *i = to_ata_internal(t);
773
774 transport_container_unregister(&i->t.host_attrs);
775 transport_container_unregister(&i->link_attr_cont);
776 transport_container_unregister(&i->dev_attr_cont);
777
778 kfree(i);
779}
780
781__init int libata_transport_init(void)
782{
783 int error;
784
785 error = transport_class_register(&ata_link_class);
786 if (error)
787 goto out_unregister_transport;
788 error = transport_class_register(&ata_port_class);
789 if (error)
790 goto out_unregister_link;
791 error = transport_class_register(&ata_dev_class);
792 if (error)
793 goto out_unregister_port;
794 return 0;
795
796 out_unregister_port:
797 transport_class_unregister(&ata_port_class);
798 out_unregister_link:
799 transport_class_unregister(&ata_link_class);
800 out_unregister_transport:
801 return error;
802
803}
804
805void __exit libata_transport_exit(void)
806{
807 transport_class_unregister(&ata_link_class);
808 transport_class_unregister(&ata_port_class);
809 transport_class_unregister(&ata_dev_class);
810}