blob: 43d4fbb377da07c1ba22dad5a1dbb24d4d3418a1 [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" },
146 { ATA_DEV_NONE, "none" }
147};
148ata_bitfield_name_search(class, ata_class_names)
149
150
151static struct {
152 u32 value;
153 char *name;
154} ata_err_names[] = {
155 { AC_ERR_DEV, "DeviceError" },
156 { AC_ERR_HSM, "HostStateMachineError" },
157 { AC_ERR_TIMEOUT, "Timeout" },
158 { AC_ERR_MEDIA, "MediaError" },
159 { AC_ERR_ATA_BUS, "BusError" },
160 { AC_ERR_HOST_BUS, "HostBusError" },
161 { AC_ERR_SYSTEM, "SystemError" },
162 { AC_ERR_INVALID, "InvalidArg" },
163 { AC_ERR_OTHER, "Unknown" },
164 { AC_ERR_NODEV_HINT, "NoDeviceHint" },
165 { AC_ERR_NCQ, "NCQError" }
166};
167ata_bitfield_name_match(err, ata_err_names)
168
169static struct {
170 u32 value;
171 char *name;
172} ata_xfer_names[] = {
173 { XFER_UDMA_7, "XFER_UDMA_7" },
174 { XFER_UDMA_6, "XFER_UDMA_6" },
175 { XFER_UDMA_5, "XFER_UDMA_5" },
176 { XFER_UDMA_4, "XFER_UDMA_4" },
177 { XFER_UDMA_3, "XFER_UDMA_3" },
178 { XFER_UDMA_2, "XFER_UDMA_2" },
179 { XFER_UDMA_1, "XFER_UDMA_1" },
180 { XFER_UDMA_0, "XFER_UDMA_0" },
181 { XFER_MW_DMA_4, "XFER_MW_DMA_4" },
182 { XFER_MW_DMA_3, "XFER_MW_DMA_3" },
183 { XFER_MW_DMA_2, "XFER_MW_DMA_2" },
184 { XFER_MW_DMA_1, "XFER_MW_DMA_1" },
185 { XFER_MW_DMA_0, "XFER_MW_DMA_0" },
186 { XFER_SW_DMA_2, "XFER_SW_DMA_2" },
187 { XFER_SW_DMA_1, "XFER_SW_DMA_1" },
188 { XFER_SW_DMA_0, "XFER_SW_DMA_0" },
189 { XFER_PIO_6, "XFER_PIO_6" },
190 { XFER_PIO_5, "XFER_PIO_5" },
191 { XFER_PIO_4, "XFER_PIO_4" },
192 { XFER_PIO_3, "XFER_PIO_3" },
193 { XFER_PIO_2, "XFER_PIO_2" },
194 { XFER_PIO_1, "XFER_PIO_1" },
195 { XFER_PIO_0, "XFER_PIO_0" },
196 { XFER_PIO_SLOW, "XFER_PIO_SLOW" }
197};
198ata_bitfield_name_match(xfer,ata_xfer_names)
199
200/*
201 * ATA Port attributes
202 */
203#define ata_port_show_simple(field, name, format_string, cast) \
204static ssize_t \
205show_ata_port_##name(struct device *dev, \
206 struct device_attribute *attr, char *buf) \
207{ \
208 struct ata_port *ap = transport_class_to_port(dev); \
209 \
210 return snprintf(buf, 20, format_string, cast ap->field); \
211}
212
213#define ata_port_simple_attr(field, name, format_string, type) \
214 ata_port_show_simple(field, name, format_string, (type)) \
215static DEVICE_ATTR(name, S_IRUGO, show_ata_port_##name, NULL)
216
217ata_port_simple_attr(nr_pmp_links, nr_pmp_links, "%d\n", int);
218ata_port_simple_attr(stats.idle_irq, idle_irq, "%ld\n", unsigned long);
David Milburne628dc92013-05-14 13:48:40 -0500219ata_port_simple_attr(local_port_no, port_no, "%u\n", unsigned int);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700220
221static DECLARE_TRANSPORT_CLASS(ata_port_class,
222 "ata_port", NULL, NULL, NULL);
223
224static void ata_tport_release(struct device *dev)
225{
Gwendal Grignoud9027472010-05-25 12:31:38 -0700226}
227
228/**
229 * ata_is_port -- check if a struct device represents a ATA port
230 * @dev: device to check
231 *
232 * Returns:
233 * %1 if the device represents a ATA Port, %0 else
234 */
H Hartley Sweetenb0e73af2012-04-16 18:04:41 -0700235static int ata_is_port(const struct device *dev)
Gwendal Grignoud9027472010-05-25 12:31:38 -0700236{
237 return dev->release == ata_tport_release;
238}
239
240static int ata_tport_match(struct attribute_container *cont,
241 struct device *dev)
242{
243 if (!ata_is_port(dev))
244 return 0;
245 return &ata_scsi_transport_template->host_attrs.ac == cont;
246}
247
248/**
249 * ata_tport_delete -- remove ATA PORT
250 * @port: ATA PORT to remove
251 *
252 * Removes the specified ATA PORT. Remove the associated link as well.
253 */
254void ata_tport_delete(struct ata_port *ap)
255{
256 struct device *dev = &ap->tdev;
257
258 ata_tlink_delete(&ap->link);
259
260 transport_remove_device(dev);
261 device_del(dev);
262 transport_destroy_device(dev);
263 put_device(dev);
264}
265
266/** ata_tport_add - initialize a transport ATA port structure
267 *
268 * @parent: parent device
269 * @ap: existing ata_port structure
270 *
271 * Initialize a ATA port structure for sysfs. It will be added to the device
272 * tree below the device specified by @parent which could be a PCI device.
273 *
274 * Returns %0 on success
275 */
276int ata_tport_add(struct device *parent,
277 struct ata_port *ap)
278{
279 int error;
280 struct device *dev = &ap->tdev;
281
282 device_initialize(dev);
Lin Ming5ef41082011-12-05 09:20:27 +0800283 dev->type = &ata_port_type;
Gwendal Grignoud9027472010-05-25 12:31:38 -0700284
Gwendal Grignoub8710e22017-03-03 09:00:09 -0800285 dev->parent = parent;
Gwendal Grignoud9027472010-05-25 12:31:38 -0700286 dev->release = ata_tport_release;
287 dev_set_name(dev, "ata%d", ap->print_id);
288 transport_setup_device(dev);
Aaron Luf1bc1e42013-08-23 10:17:54 +0800289 ata_acpi_bind_port(ap);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700290 error = device_add(dev);
291 if (error) {
292 goto tport_err;
293 }
294
Lin Ming966f1212012-01-16 13:23:23 +0800295 device_enable_async_suspend(dev);
Lin Ming9ee4f392011-12-05 09:20:28 +0800296 pm_runtime_set_active(dev);
297 pm_runtime_enable(dev);
Lin Ming0c8d32c2012-04-18 09:29:47 +0800298 pm_runtime_forbid(dev);
Lin Ming9ee4f392011-12-05 09:20:28 +0800299
Gwendal Grignoud9027472010-05-25 12:31:38 -0700300 transport_add_device(dev);
301 transport_configure_device(dev);
302
303 error = ata_tlink_add(&ap->link);
304 if (error) {
305 goto tport_link_err;
306 }
307 return 0;
308
309 tport_link_err:
310 transport_remove_device(dev);
311 device_del(dev);
312
313 tport_err:
314 transport_destroy_device(dev);
315 put_device(dev);
316 return error;
317}
318
319
320/*
321 * ATA link attributes
322 */
Gwendal Grignou3e85c3e2013-10-25 16:28:57 -0700323static int noop(int x) { return x; }
Gwendal Grignoud9027472010-05-25 12:31:38 -0700324
Gwendal Grignou3e85c3e2013-10-25 16:28:57 -0700325#define ata_link_show_linkspeed(field, format) \
Gwendal Grignoud9027472010-05-25 12:31:38 -0700326static ssize_t \
327show_ata_link_##field(struct device *dev, \
328 struct device_attribute *attr, char *buf) \
329{ \
330 struct ata_link *link = transport_class_to_link(dev); \
331 \
Gwendal Grignou3e85c3e2013-10-25 16:28:57 -0700332 return sprintf(buf, "%s\n", sata_spd_string(format(link->field))); \
Gwendal Grignoud9027472010-05-25 12:31:38 -0700333}
334
Gwendal Grignou3e85c3e2013-10-25 16:28:57 -0700335#define ata_link_linkspeed_attr(field, format) \
336 ata_link_show_linkspeed(field, format) \
Gwendal Grignoud9027472010-05-25 12:31:38 -0700337static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL)
338
Gwendal Grignou3e85c3e2013-10-25 16:28:57 -0700339ata_link_linkspeed_attr(hw_sata_spd_limit, fls);
340ata_link_linkspeed_attr(sata_spd_limit, fls);
341ata_link_linkspeed_attr(sata_spd, noop);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700342
343
344static DECLARE_TRANSPORT_CLASS(ata_link_class,
345 "ata_link", NULL, NULL, NULL);
346
347static void ata_tlink_release(struct device *dev)
348{
Gwendal Grignoud9027472010-05-25 12:31:38 -0700349}
350
351/**
352 * ata_is_link -- check if a struct device represents a ATA link
353 * @dev: device to check
354 *
355 * Returns:
356 * %1 if the device represents a ATA link, %0 else
357 */
H Hartley Sweetenb0e73af2012-04-16 18:04:41 -0700358static int ata_is_link(const struct device *dev)
Gwendal Grignoud9027472010-05-25 12:31:38 -0700359{
360 return dev->release == ata_tlink_release;
361}
362
363static int ata_tlink_match(struct attribute_container *cont,
364 struct device *dev)
365{
366 struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
367 if (!ata_is_link(dev))
368 return 0;
369 return &i->link_attr_cont.ac == cont;
370}
371
372/**
373 * ata_tlink_delete -- remove ATA LINK
374 * @port: ATA LINK to remove
375 *
376 * Removes the specified ATA LINK. remove associated ATA device(s) as well.
377 */
378void ata_tlink_delete(struct ata_link *link)
379{
380 struct device *dev = &link->tdev;
381 struct ata_device *ata_dev;
382
383 ata_for_each_dev(ata_dev, link, ALL) {
384 ata_tdev_delete(ata_dev);
385 }
386
387 transport_remove_device(dev);
388 device_del(dev);
389 transport_destroy_device(dev);
390 put_device(dev);
391}
392
393/**
394 * ata_tlink_add -- initialize a transport ATA link structure
395 * @link: allocated ata_link structure.
396 *
397 * Initialize an ATA LINK structure for sysfs. It will be added in the
398 * device tree below the ATA PORT it belongs to.
399 *
400 * Returns %0 on success
401 */
402int ata_tlink_add(struct ata_link *link)
403{
404 struct device *dev = &link->tdev;
405 struct ata_port *ap = link->ap;
406 struct ata_device *ata_dev;
407 int error;
408
409 device_initialize(dev);
Gwendal Grignoub8710e22017-03-03 09:00:09 -0800410 dev->parent = &ap->tdev;
Gwendal Grignoud9027472010-05-25 12:31:38 -0700411 dev->release = ata_tlink_release;
412 if (ata_is_host_link(link))
413 dev_set_name(dev, "link%d", ap->print_id);
414 else
415 dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp);
416
417 transport_setup_device(dev);
418
419 error = device_add(dev);
420 if (error) {
421 goto tlink_err;
422 }
423
424 transport_add_device(dev);
425 transport_configure_device(dev);
426
427 ata_for_each_dev(ata_dev, link, ALL) {
428 error = ata_tdev_add(ata_dev);
429 if (error) {
430 goto tlink_dev_err;
431 }
432 }
433 return 0;
434 tlink_dev_err:
435 while (--ata_dev >= link->device) {
436 ata_tdev_delete(ata_dev);
437 }
438 transport_remove_device(dev);
439 device_del(dev);
440 tlink_err:
441 transport_destroy_device(dev);
442 put_device(dev);
443 return error;
444}
445
446/*
447 * ATA device attributes
448 */
449
450#define ata_dev_show_class(title, field) \
451static ssize_t \
452show_ata_dev_##field(struct device *dev, \
453 struct device_attribute *attr, char *buf) \
454{ \
455 struct ata_device *ata_dev = transport_class_to_dev(dev); \
456 \
457 return get_ata_##title##_names(ata_dev->field, buf); \
458}
459
460#define ata_dev_attr(title, field) \
461 ata_dev_show_class(title, field) \
462static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL)
463
464ata_dev_attr(class, class);
465ata_dev_attr(xfer, pio_mode);
466ata_dev_attr(xfer, dma_mode);
467ata_dev_attr(xfer, xfer_mode);
468
469
470#define ata_dev_show_simple(field, format_string, cast) \
471static ssize_t \
472show_ata_dev_##field(struct device *dev, \
473 struct device_attribute *attr, char *buf) \
474{ \
475 struct ata_device *ata_dev = transport_class_to_dev(dev); \
476 \
477 return snprintf(buf, 20, format_string, cast ata_dev->field); \
478}
479
480#define ata_dev_simple_attr(field, format_string, type) \
481 ata_dev_show_simple(field, format_string, (type)) \
482static DEVICE_ATTR(field, S_IRUGO, \
483 show_ata_dev_##field, NULL)
484
485ata_dev_simple_attr(spdn_cnt, "%d\n", int);
486
487struct ata_show_ering_arg {
488 char* buf;
489 int written;
490};
491
492static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg)
493{
494 struct ata_show_ering_arg* arg = void_arg;
495 struct timespec time;
496
497 jiffies_to_timespec(ent->timestamp,&time);
498 arg->written += sprintf(arg->buf + arg->written,
499 "[%5lu.%06lu]",
500 time.tv_sec, time.tv_nsec);
501 arg->written += get_ata_err_names(ent->err_mask,
502 arg->buf + arg->written);
503 return 0;
504}
505
506static ssize_t
507show_ata_dev_ering(struct device *dev,
508 struct device_attribute *attr, char *buf)
509{
510 struct ata_device *ata_dev = transport_class_to_dev(dev);
511 struct ata_show_ering_arg arg = { buf, 0 };
512
513 ata_ering_map(&ata_dev->ering, ata_show_ering, &arg);
514 return arg.written;
515}
516
517
518static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL);
519
520static ssize_t
521show_ata_dev_id(struct device *dev,
522 struct device_attribute *attr, char *buf)
523{
524 struct ata_device *ata_dev = transport_class_to_dev(dev);
525 int written = 0, i = 0;
526
527 if (ata_dev->class == ATA_DEV_PMP)
528 return 0;
529 for(i=0;i<ATA_ID_WORDS;i++) {
530 written += snprintf(buf+written, 20, "%04x%c",
531 ata_dev->id[i],
532 ((i+1) & 7) ? ' ' : '\n');
533 }
534 return written;
535}
536
537static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL);
538
539static ssize_t
540show_ata_dev_gscr(struct device *dev,
541 struct device_attribute *attr, char *buf)
542{
543 struct ata_device *ata_dev = transport_class_to_dev(dev);
544 int written = 0, i = 0;
545
546 if (ata_dev->class != ATA_DEV_PMP)
547 return 0;
548 for(i=0;i<SATA_PMP_GSCR_DWORDS;i++) {
549 written += snprintf(buf+written, 20, "%08x%c",
550 ata_dev->gscr[i],
551 ((i+1) & 3) ? ' ' : '\n');
552 }
553 if (SATA_PMP_GSCR_DWORDS & 3)
554 buf[written-1] = '\n';
555 return written;
556}
557
558static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL);
559
Martin K. Petersen90eebd32015-05-04 21:54:19 -0400560static ssize_t
561show_ata_dev_trim(struct device *dev,
562 struct device_attribute *attr, char *buf)
563{
564 struct ata_device *ata_dev = transport_class_to_dev(dev);
565 unsigned char *mode;
566
567 if (!ata_id_has_trim(ata_dev->id))
568 mode = "unsupported";
Arne Fitzenreiter3fa84d62015-07-15 13:54:36 +0200569 else if (ata_dev->horkage & ATA_HORKAGE_NOTRIM)
570 mode = "forced_unsupported";
Martin K. Petersen90eebd32015-05-04 21:54:19 -0400571 else if (ata_dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM)
572 mode = "forced_unqueued";
573 else if (ata_fpdma_dsm_supported(ata_dev))
574 mode = "queued";
575 else
576 mode = "unqueued";
577
578 return snprintf(buf, 20, "%s\n", mode);
579}
580
581static DEVICE_ATTR(trim, S_IRUGO, show_ata_dev_trim, NULL);
582
Gwendal Grignoud9027472010-05-25 12:31:38 -0700583static DECLARE_TRANSPORT_CLASS(ata_dev_class,
584 "ata_device", NULL, NULL, NULL);
585
586static void ata_tdev_release(struct device *dev)
587{
Gwendal Grignoud9027472010-05-25 12:31:38 -0700588}
589
590/**
591 * ata_is_ata_dev -- check if a struct device represents a ATA device
592 * @dev: device to check
593 *
594 * Returns:
595 * %1 if the device represents a ATA device, %0 else
596 */
H Hartley Sweetenb0e73af2012-04-16 18:04:41 -0700597static int ata_is_ata_dev(const struct device *dev)
Gwendal Grignoud9027472010-05-25 12:31:38 -0700598{
599 return dev->release == ata_tdev_release;
600}
601
602static int ata_tdev_match(struct attribute_container *cont,
603 struct device *dev)
604{
605 struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
606 if (!ata_is_ata_dev(dev))
607 return 0;
608 return &i->dev_attr_cont.ac == cont;
609}
610
611/**
612 * ata_tdev_free -- free a ATA LINK
613 * @dev: ATA PHY to free
614 *
615 * Frees the specified ATA PHY.
616 *
617 * Note:
618 * This function must only be called on a PHY that has not
619 * successfully been added using ata_tdev_add().
620 */
621static void ata_tdev_free(struct ata_device *dev)
622{
623 transport_destroy_device(&dev->tdev);
624 put_device(&dev->tdev);
625}
626
627/**
628 * ata_tdev_delete -- remove ATA device
629 * @port: ATA PORT to remove
630 *
631 * Removes the specified ATA device.
632 */
633static void ata_tdev_delete(struct ata_device *ata_dev)
634{
635 struct device *dev = &ata_dev->tdev;
636
637 transport_remove_device(dev);
638 device_del(dev);
639 ata_tdev_free(ata_dev);
640}
641
642
643/**
644 * ata_tdev_add -- initialize a transport ATA device structure.
645 * @ata_dev: ata_dev structure.
646 *
647 * Initialize an ATA device structure for sysfs. It will be added in the
648 * device tree below the ATA LINK device it belongs to.
649 *
650 * Returns %0 on success
651 */
652static int ata_tdev_add(struct ata_device *ata_dev)
653{
654 struct device *dev = &ata_dev->tdev;
655 struct ata_link *link = ata_dev->link;
656 struct ata_port *ap = link->ap;
657 int error;
658
659 device_initialize(dev);
Gwendal Grignoub8710e22017-03-03 09:00:09 -0800660 dev->parent = &link->tdev;
Gwendal Grignoud9027472010-05-25 12:31:38 -0700661 dev->release = ata_tdev_release;
662 if (ata_is_host_link(link))
663 dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno);
664 else
665 dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp);
666
667 transport_setup_device(dev);
Aaron Luf1bc1e42013-08-23 10:17:54 +0800668 ata_acpi_bind_dev(ata_dev);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700669 error = device_add(dev);
670 if (error) {
671 ata_tdev_free(ata_dev);
672 return error;
673 }
674
675 transport_add_device(dev);
676 transport_configure_device(dev);
677 return 0;
678}
679
680
681/*
682 * Setup / Teardown code
683 */
684
685#define SETUP_TEMPLATE(attrb, field, perm, test) \
686 i->private_##attrb[count] = dev_attr_##field; \
687 i->private_##attrb[count].attr.mode = perm; \
688 i->attrb[count] = &i->private_##attrb[count]; \
689 if (test) \
690 count++
691
692#define SETUP_LINK_ATTRIBUTE(field) \
693 SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1)
694
695#define SETUP_PORT_ATTRIBUTE(field) \
696 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
697
698#define SETUP_DEV_ATTRIBUTE(field) \
699 SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1)
700
701/**
702 * ata_attach_transport -- instantiate ATA transport template
703 */
704struct scsi_transport_template *ata_attach_transport(void)
705{
706 struct ata_internal *i;
707 int count;
708
709 i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL);
710 if (!i)
711 return NULL;
712
713 i->t.eh_strategy_handler = ata_scsi_error;
714 i->t.eh_timed_out = ata_scsi_timed_out;
715 i->t.user_scan = ata_scsi_user_scan;
716
717 i->t.host_attrs.ac.attrs = &i->port_attrs[0];
718 i->t.host_attrs.ac.class = &ata_port_class.class;
719 i->t.host_attrs.ac.match = ata_tport_match;
720 transport_container_register(&i->t.host_attrs);
721
722 i->link_attr_cont.ac.class = &ata_link_class.class;
723 i->link_attr_cont.ac.attrs = &i->link_attrs[0];
724 i->link_attr_cont.ac.match = ata_tlink_match;
725 transport_container_register(&i->link_attr_cont);
726
727 i->dev_attr_cont.ac.class = &ata_dev_class.class;
728 i->dev_attr_cont.ac.attrs = &i->dev_attrs[0];
729 i->dev_attr_cont.ac.match = ata_tdev_match;
730 transport_container_register(&i->dev_attr_cont);
731
732 count = 0;
733 SETUP_PORT_ATTRIBUTE(nr_pmp_links);
734 SETUP_PORT_ATTRIBUTE(idle_irq);
David Milburne628dc92013-05-14 13:48:40 -0500735 SETUP_PORT_ATTRIBUTE(port_no);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700736 BUG_ON(count > ATA_PORT_ATTRS);
737 i->port_attrs[count] = NULL;
738
739 count = 0;
740 SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit);
741 SETUP_LINK_ATTRIBUTE(sata_spd_limit);
742 SETUP_LINK_ATTRIBUTE(sata_spd);
743 BUG_ON(count > ATA_LINK_ATTRS);
744 i->link_attrs[count] = NULL;
745
746 count = 0;
747 SETUP_DEV_ATTRIBUTE(class);
748 SETUP_DEV_ATTRIBUTE(pio_mode);
749 SETUP_DEV_ATTRIBUTE(dma_mode);
750 SETUP_DEV_ATTRIBUTE(xfer_mode);
751 SETUP_DEV_ATTRIBUTE(spdn_cnt);
752 SETUP_DEV_ATTRIBUTE(ering);
753 SETUP_DEV_ATTRIBUTE(id);
754 SETUP_DEV_ATTRIBUTE(gscr);
Martin K. Petersen90eebd32015-05-04 21:54:19 -0400755 SETUP_DEV_ATTRIBUTE(trim);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700756 BUG_ON(count > ATA_DEV_ATTRS);
757 i->dev_attrs[count] = NULL;
758
759 return &i->t;
760}
761
762/**
763 * ata_release_transport -- release ATA transport template instance
764 * @t: transport template instance
765 */
766void ata_release_transport(struct scsi_transport_template *t)
767{
768 struct ata_internal *i = to_ata_internal(t);
769
770 transport_container_unregister(&i->t.host_attrs);
771 transport_container_unregister(&i->link_attr_cont);
772 transport_container_unregister(&i->dev_attr_cont);
773
774 kfree(i);
775}
776
777__init int libata_transport_init(void)
778{
779 int error;
780
781 error = transport_class_register(&ata_link_class);
782 if (error)
783 goto out_unregister_transport;
784 error = transport_class_register(&ata_port_class);
785 if (error)
786 goto out_unregister_link;
787 error = transport_class_register(&ata_dev_class);
788 if (error)
789 goto out_unregister_port;
790 return 0;
791
792 out_unregister_port:
793 transport_class_unregister(&ata_port_class);
794 out_unregister_link:
795 transport_class_unregister(&ata_link_class);
796 out_unregister_transport:
797 return error;
798
799}
800
801void __exit libata_transport_exit(void)
802{
803 transport_class_unregister(&ata_link_class);
804 transport_class_unregister(&ata_port_class);
805 transport_class_unregister(&ata_dev_class);
806}