1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2008 ioogle, Inc. All rights reserved.
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 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>
30 #include <linux/slab.h>
31 #include <scsi/scsi_transport.h>
32 #include <linux/libata.h>
33 #include <linux/hdreg.h>
34 #include <linux/uaccess.h>
35 #include <linux/pm_runtime.h>
36
37 #include "libata.h"
38 #include "libata-transport.h"
39
40 #define ATA_PORT_ATTRS 3
41 #define ATA_LINK_ATTRS 3
42 #define ATA_DEV_ATTRS 9
43
44 struct scsi_transport_template;
45 struct scsi_transport_template *ata_scsi_transport_template;
46
47 struct 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 */
85 static int ata_tdev_add(struct ata_device *dev);
86 static 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) \
97 static ssize_t \
98 get_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) \
116 static ssize_t \
117 get_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
133 static 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_ZAC, "zac" },
147 { ATA_DEV_NONE, "none" }
148 };
149 ata_bitfield_name_search(class, ata_class_names)
150
151
152 static 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 };
168 ata_bitfield_name_match(err, ata_err_names)
169
170 static 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 };
199 ata_bitfield_name_search(xfer, ata_xfer_names)
200
201 /*
202 * ATA Port attributes
203 */
204 #define ata_port_show_simple(field, name, format_string, cast) \
205 static ssize_t \
206 show_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 scnprintf(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)) \
216 static DEVICE_ATTR(name, S_IRUGO, show_ata_port_##name, NULL)
217
218 ata_port_simple_attr(nr_pmp_links, nr_pmp_links, "%d\n", int);
219 ata_port_simple_attr(stats.idle_irq, idle_irq, "%ld\n", unsigned long);
220 ata_port_simple_attr(local_port_no, port_no, "%u\n", unsigned int);
221
222 static DECLARE_TRANSPORT_CLASS(ata_port_class,
223 "ata_port", NULL, NULL, NULL);
224
ata_tport_release(struct device * dev)225 static void ata_tport_release(struct device *dev)
226 {
227 struct ata_port *ap = tdev_to_port(dev);
228 ata_host_put(ap->host);
229 }
230
231 /**
232 * ata_is_port -- check if a struct device represents a ATA port
233 * @dev: device to check
234 *
235 * Returns:
236 * %1 if the device represents a ATA Port, %0 else
237 */
ata_is_port(const struct device * dev)238 static int ata_is_port(const struct device *dev)
239 {
240 return dev->release == ata_tport_release;
241 }
242
ata_tport_match(struct attribute_container * cont,struct device * dev)243 static int ata_tport_match(struct attribute_container *cont,
244 struct device *dev)
245 {
246 if (!ata_is_port(dev))
247 return 0;
248 return &ata_scsi_transport_template->host_attrs.ac == cont;
249 }
250
251 /**
252 * ata_tport_delete -- remove ATA PORT
253 * @ap: ATA PORT to remove
254 *
255 * Removes the specified ATA PORT. Remove the associated link as well.
256 */
ata_tport_delete(struct ata_port * ap)257 void ata_tport_delete(struct ata_port *ap)
258 {
259 struct device *dev = &ap->tdev;
260
261 ata_tlink_delete(&ap->link);
262
263 transport_remove_device(dev);
264 device_del(dev);
265 transport_destroy_device(dev);
266 put_device(dev);
267 }
268
269 /** ata_tport_add - initialize a transport ATA port structure
270 *
271 * @parent: parent device
272 * @ap: existing ata_port structure
273 *
274 * Initialize a ATA port structure for sysfs. It will be added to the device
275 * tree below the device specified by @parent which could be a PCI device.
276 *
277 * Returns %0 on success
278 */
ata_tport_add(struct device * parent,struct ata_port * ap)279 int ata_tport_add(struct device *parent,
280 struct ata_port *ap)
281 {
282 int error;
283 struct device *dev = &ap->tdev;
284
285 device_initialize(dev);
286 dev->type = &ata_port_type;
287
288 dev->parent = parent;
289 ata_host_get(ap->host);
290 dev->release = ata_tport_release;
291 dev_set_name(dev, "ata%d", ap->print_id);
292 transport_setup_device(dev);
293 ata_acpi_bind_port(ap);
294 error = device_add(dev);
295 if (error) {
296 goto tport_err;
297 }
298
299 device_enable_async_suspend(dev);
300 pm_runtime_set_active(dev);
301 pm_runtime_enable(dev);
302 pm_runtime_forbid(dev);
303
304 error = transport_add_device(dev);
305 if (error)
306 goto tport_transport_add_err;
307 transport_configure_device(dev);
308
309 error = ata_tlink_add(&ap->link);
310 if (error) {
311 goto tport_link_err;
312 }
313 return 0;
314
315 tport_link_err:
316 transport_remove_device(dev);
317 tport_transport_add_err:
318 device_del(dev);
319
320 tport_err:
321 transport_destroy_device(dev);
322 put_device(dev);
323 return error;
324 }
325
326 /**
327 * ata_port_classify - determine device type based on ATA-spec signature
328 * @ap: ATA port device on which the classification should be run
329 * @tf: ATA taskfile register set for device to be identified
330 *
331 * A wrapper around ata_dev_classify() to provide additional logging
332 *
333 * RETURNS:
334 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
335 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
336 */
ata_port_classify(struct ata_port * ap,const struct ata_taskfile * tf)337 unsigned int ata_port_classify(struct ata_port *ap,
338 const struct ata_taskfile *tf)
339 {
340 int i;
341 unsigned int class = ata_dev_classify(tf);
342
343 /* Start with index '1' to skip the 'unknown' entry */
344 for (i = 1; i < ARRAY_SIZE(ata_class_names); i++) {
345 if (ata_class_names[i].value == class) {
346 ata_port_dbg(ap, "found %s device by sig\n",
347 ata_class_names[i].name);
348 return class;
349 }
350 }
351
352 ata_port_info(ap, "found unknown device (class %u)\n", class);
353 return class;
354 }
355 EXPORT_SYMBOL_GPL(ata_port_classify);
356
357 /*
358 * ATA link attributes
359 */
noop(int x)360 static int noop(int x) { return x; }
361
362 #define ata_link_show_linkspeed(field, format) \
363 static ssize_t \
364 show_ata_link_##field(struct device *dev, \
365 struct device_attribute *attr, char *buf) \
366 { \
367 struct ata_link *link = transport_class_to_link(dev); \
368 \
369 return sprintf(buf, "%s\n", sata_spd_string(format(link->field))); \
370 }
371
372 #define ata_link_linkspeed_attr(field, format) \
373 ata_link_show_linkspeed(field, format) \
374 static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL)
375
376 ata_link_linkspeed_attr(hw_sata_spd_limit, fls);
377 ata_link_linkspeed_attr(sata_spd_limit, fls);
378 ata_link_linkspeed_attr(sata_spd, noop);
379
380
381 static DECLARE_TRANSPORT_CLASS(ata_link_class,
382 "ata_link", NULL, NULL, NULL);
383
ata_tlink_release(struct device * dev)384 static void ata_tlink_release(struct device *dev)
385 {
386 }
387
388 /**
389 * ata_is_link -- check if a struct device represents a ATA link
390 * @dev: device to check
391 *
392 * Returns:
393 * %1 if the device represents a ATA link, %0 else
394 */
ata_is_link(const struct device * dev)395 static int ata_is_link(const struct device *dev)
396 {
397 return dev->release == ata_tlink_release;
398 }
399
ata_tlink_match(struct attribute_container * cont,struct device * dev)400 static int ata_tlink_match(struct attribute_container *cont,
401 struct device *dev)
402 {
403 struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
404 if (!ata_is_link(dev))
405 return 0;
406 return &i->link_attr_cont.ac == cont;
407 }
408
409 /**
410 * ata_tlink_delete -- remove ATA LINK
411 * @link: ATA LINK to remove
412 *
413 * Removes the specified ATA LINK. remove associated ATA device(s) as well.
414 */
ata_tlink_delete(struct ata_link * link)415 void ata_tlink_delete(struct ata_link *link)
416 {
417 struct device *dev = &link->tdev;
418 struct ata_device *ata_dev;
419
420 ata_for_each_dev(ata_dev, link, ALL) {
421 ata_tdev_delete(ata_dev);
422 }
423
424 transport_remove_device(dev);
425 device_del(dev);
426 transport_destroy_device(dev);
427 put_device(dev);
428 }
429
430 /**
431 * ata_tlink_add -- initialize a transport ATA link structure
432 * @link: allocated ata_link structure.
433 *
434 * Initialize an ATA LINK structure for sysfs. It will be added in the
435 * device tree below the ATA PORT it belongs to.
436 *
437 * Returns %0 on success
438 */
ata_tlink_add(struct ata_link * link)439 int ata_tlink_add(struct ata_link *link)
440 {
441 struct device *dev = &link->tdev;
442 struct ata_port *ap = link->ap;
443 struct ata_device *ata_dev;
444 int error;
445
446 device_initialize(dev);
447 dev->parent = &ap->tdev;
448 dev->release = ata_tlink_release;
449 if (ata_is_host_link(link))
450 dev_set_name(dev, "link%d", ap->print_id);
451 else
452 dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp);
453
454 transport_setup_device(dev);
455
456 error = device_add(dev);
457 if (error) {
458 goto tlink_err;
459 }
460
461 error = transport_add_device(dev);
462 if (error)
463 goto tlink_transport_err;
464 transport_configure_device(dev);
465
466 ata_for_each_dev(ata_dev, link, ALL) {
467 error = ata_tdev_add(ata_dev);
468 if (error) {
469 goto tlink_dev_err;
470 }
471 }
472 return 0;
473 tlink_dev_err:
474 while (--ata_dev >= link->device) {
475 ata_tdev_delete(ata_dev);
476 }
477 transport_remove_device(dev);
478 tlink_transport_err:
479 device_del(dev);
480 tlink_err:
481 transport_destroy_device(dev);
482 put_device(dev);
483 return error;
484 }
485
486 /*
487 * ATA device attributes
488 */
489
490 #define ata_dev_show_class(title, field) \
491 static ssize_t \
492 show_ata_dev_##field(struct device *dev, \
493 struct device_attribute *attr, char *buf) \
494 { \
495 struct ata_device *ata_dev = transport_class_to_dev(dev); \
496 \
497 return get_ata_##title##_names(ata_dev->field, buf); \
498 }
499
500 #define ata_dev_attr(title, field) \
501 ata_dev_show_class(title, field) \
502 static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL)
503
504 ata_dev_attr(class, class);
505 ata_dev_attr(xfer, pio_mode);
506 ata_dev_attr(xfer, dma_mode);
507 ata_dev_attr(xfer, xfer_mode);
508
509
510 #define ata_dev_show_simple(field, format_string, cast) \
511 static ssize_t \
512 show_ata_dev_##field(struct device *dev, \
513 struct device_attribute *attr, char *buf) \
514 { \
515 struct ata_device *ata_dev = transport_class_to_dev(dev); \
516 \
517 return scnprintf(buf, 20, format_string, cast ata_dev->field); \
518 }
519
520 #define ata_dev_simple_attr(field, format_string, type) \
521 ata_dev_show_simple(field, format_string, (type)) \
522 static DEVICE_ATTR(field, S_IRUGO, \
523 show_ata_dev_##field, NULL)
524
525 ata_dev_simple_attr(spdn_cnt, "%d\n", int);
526
527 struct ata_show_ering_arg {
528 char* buf;
529 int written;
530 };
531
ata_show_ering(struct ata_ering_entry * ent,void * void_arg)532 static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg)
533 {
534 struct ata_show_ering_arg* arg = void_arg;
535 u64 seconds;
536 u32 rem;
537
538 seconds = div_u64_rem(ent->timestamp, HZ, &rem);
539 arg->written += sprintf(arg->buf + arg->written,
540 "[%5llu.%09lu]", seconds,
541 rem * NSEC_PER_SEC / HZ);
542 arg->written += get_ata_err_names(ent->err_mask,
543 arg->buf + arg->written);
544 return 0;
545 }
546
547 static ssize_t
show_ata_dev_ering(struct device * dev,struct device_attribute * attr,char * buf)548 show_ata_dev_ering(struct device *dev,
549 struct device_attribute *attr, char *buf)
550 {
551 struct ata_device *ata_dev = transport_class_to_dev(dev);
552 struct ata_show_ering_arg arg = { buf, 0 };
553
554 ata_ering_map(&ata_dev->ering, ata_show_ering, &arg);
555 return arg.written;
556 }
557
558
559 static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL);
560
561 static ssize_t
show_ata_dev_id(struct device * dev,struct device_attribute * attr,char * buf)562 show_ata_dev_id(struct device *dev,
563 struct device_attribute *attr, char *buf)
564 {
565 struct ata_device *ata_dev = transport_class_to_dev(dev);
566 int written = 0, i = 0;
567
568 if (ata_dev->class == ATA_DEV_PMP)
569 return 0;
570 for(i=0;i<ATA_ID_WORDS;i++) {
571 written += scnprintf(buf+written, 20, "%04x%c",
572 ata_dev->id[i],
573 ((i+1) & 7) ? ' ' : '\n');
574 }
575 return written;
576 }
577
578 static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL);
579
580 static ssize_t
show_ata_dev_gscr(struct device * dev,struct device_attribute * attr,char * buf)581 show_ata_dev_gscr(struct device *dev,
582 struct device_attribute *attr, char *buf)
583 {
584 struct ata_device *ata_dev = transport_class_to_dev(dev);
585 int written = 0, i = 0;
586
587 if (ata_dev->class != ATA_DEV_PMP)
588 return 0;
589 for(i=0;i<SATA_PMP_GSCR_DWORDS;i++) {
590 written += scnprintf(buf+written, 20, "%08x%c",
591 ata_dev->gscr[i],
592 ((i+1) & 3) ? ' ' : '\n');
593 }
594 if (SATA_PMP_GSCR_DWORDS & 3)
595 buf[written-1] = '\n';
596 return written;
597 }
598
599 static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL);
600
601 static ssize_t
show_ata_dev_trim(struct device * dev,struct device_attribute * attr,char * buf)602 show_ata_dev_trim(struct device *dev,
603 struct device_attribute *attr, char *buf)
604 {
605 struct ata_device *ata_dev = transport_class_to_dev(dev);
606 unsigned char *mode;
607
608 if (!ata_id_has_trim(ata_dev->id))
609 mode = "unsupported";
610 else if (ata_dev->horkage & ATA_HORKAGE_NOTRIM)
611 mode = "forced_unsupported";
612 else if (ata_dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM)
613 mode = "forced_unqueued";
614 else if (ata_fpdma_dsm_supported(ata_dev))
615 mode = "queued";
616 else
617 mode = "unqueued";
618
619 return scnprintf(buf, 20, "%s\n", mode);
620 }
621
622 static DEVICE_ATTR(trim, S_IRUGO, show_ata_dev_trim, NULL);
623
624 static DECLARE_TRANSPORT_CLASS(ata_dev_class,
625 "ata_device", NULL, NULL, NULL);
626
ata_tdev_release(struct device * dev)627 static void ata_tdev_release(struct device *dev)
628 {
629 }
630
631 /**
632 * ata_is_ata_dev -- check if a struct device represents a ATA device
633 * @dev: device to check
634 *
635 * Returns:
636 * %1 if the device represents a ATA device, %0 else
637 */
ata_is_ata_dev(const struct device * dev)638 static int ata_is_ata_dev(const struct device *dev)
639 {
640 return dev->release == ata_tdev_release;
641 }
642
ata_tdev_match(struct attribute_container * cont,struct device * dev)643 static int ata_tdev_match(struct attribute_container *cont,
644 struct device *dev)
645 {
646 struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
647 if (!ata_is_ata_dev(dev))
648 return 0;
649 return &i->dev_attr_cont.ac == cont;
650 }
651
652 /**
653 * ata_tdev_free -- free a ATA LINK
654 * @dev: ATA PHY to free
655 *
656 * Frees the specified ATA PHY.
657 *
658 * Note:
659 * This function must only be called on a PHY that has not
660 * successfully been added using ata_tdev_add().
661 */
ata_tdev_free(struct ata_device * dev)662 static void ata_tdev_free(struct ata_device *dev)
663 {
664 transport_destroy_device(&dev->tdev);
665 put_device(&dev->tdev);
666 }
667
668 /**
669 * ata_tdev_delete -- remove ATA device
670 * @ata_dev: ATA device to remove
671 *
672 * Removes the specified ATA device.
673 */
ata_tdev_delete(struct ata_device * ata_dev)674 static void ata_tdev_delete(struct ata_device *ata_dev)
675 {
676 struct device *dev = &ata_dev->tdev;
677
678 transport_remove_device(dev);
679 device_del(dev);
680 ata_tdev_free(ata_dev);
681 }
682
683
684 /**
685 * ata_tdev_add -- initialize a transport ATA device structure.
686 * @ata_dev: ata_dev structure.
687 *
688 * Initialize an ATA device structure for sysfs. It will be added in the
689 * device tree below the ATA LINK device it belongs to.
690 *
691 * Returns %0 on success
692 */
ata_tdev_add(struct ata_device * ata_dev)693 static int ata_tdev_add(struct ata_device *ata_dev)
694 {
695 struct device *dev = &ata_dev->tdev;
696 struct ata_link *link = ata_dev->link;
697 struct ata_port *ap = link->ap;
698 int error;
699
700 device_initialize(dev);
701 dev->parent = &link->tdev;
702 dev->release = ata_tdev_release;
703 if (ata_is_host_link(link))
704 dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno);
705 else
706 dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp);
707
708 transport_setup_device(dev);
709 ata_acpi_bind_dev(ata_dev);
710 error = device_add(dev);
711 if (error) {
712 ata_tdev_free(ata_dev);
713 return error;
714 }
715
716 error = transport_add_device(dev);
717 if (error) {
718 device_del(dev);
719 ata_tdev_free(ata_dev);
720 return error;
721 }
722
723 transport_configure_device(dev);
724 return 0;
725 }
726
727
728 /*
729 * Setup / Teardown code
730 */
731
732 #define SETUP_TEMPLATE(attrb, field, perm, test) \
733 i->private_##attrb[count] = dev_attr_##field; \
734 i->private_##attrb[count].attr.mode = perm; \
735 i->attrb[count] = &i->private_##attrb[count]; \
736 if (test) \
737 count++
738
739 #define SETUP_LINK_ATTRIBUTE(field) \
740 SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1)
741
742 #define SETUP_PORT_ATTRIBUTE(field) \
743 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
744
745 #define SETUP_DEV_ATTRIBUTE(field) \
746 SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1)
747
748 /**
749 * ata_attach_transport -- instantiate ATA transport template
750 */
ata_attach_transport(void)751 struct scsi_transport_template *ata_attach_transport(void)
752 {
753 struct ata_internal *i;
754 int count;
755
756 i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL);
757 if (!i)
758 return NULL;
759
760 i->t.eh_strategy_handler = ata_scsi_error;
761 i->t.user_scan = ata_scsi_user_scan;
762
763 i->t.host_attrs.ac.attrs = &i->port_attrs[0];
764 i->t.host_attrs.ac.class = &ata_port_class.class;
765 i->t.host_attrs.ac.match = ata_tport_match;
766 transport_container_register(&i->t.host_attrs);
767
768 i->link_attr_cont.ac.class = &ata_link_class.class;
769 i->link_attr_cont.ac.attrs = &i->link_attrs[0];
770 i->link_attr_cont.ac.match = ata_tlink_match;
771 transport_container_register(&i->link_attr_cont);
772
773 i->dev_attr_cont.ac.class = &ata_dev_class.class;
774 i->dev_attr_cont.ac.attrs = &i->dev_attrs[0];
775 i->dev_attr_cont.ac.match = ata_tdev_match;
776 transport_container_register(&i->dev_attr_cont);
777
778 count = 0;
779 SETUP_PORT_ATTRIBUTE(nr_pmp_links);
780 SETUP_PORT_ATTRIBUTE(idle_irq);
781 SETUP_PORT_ATTRIBUTE(port_no);
782 BUG_ON(count > ATA_PORT_ATTRS);
783 i->port_attrs[count] = NULL;
784
785 count = 0;
786 SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit);
787 SETUP_LINK_ATTRIBUTE(sata_spd_limit);
788 SETUP_LINK_ATTRIBUTE(sata_spd);
789 BUG_ON(count > ATA_LINK_ATTRS);
790 i->link_attrs[count] = NULL;
791
792 count = 0;
793 SETUP_DEV_ATTRIBUTE(class);
794 SETUP_DEV_ATTRIBUTE(pio_mode);
795 SETUP_DEV_ATTRIBUTE(dma_mode);
796 SETUP_DEV_ATTRIBUTE(xfer_mode);
797 SETUP_DEV_ATTRIBUTE(spdn_cnt);
798 SETUP_DEV_ATTRIBUTE(ering);
799 SETUP_DEV_ATTRIBUTE(id);
800 SETUP_DEV_ATTRIBUTE(gscr);
801 SETUP_DEV_ATTRIBUTE(trim);
802 BUG_ON(count > ATA_DEV_ATTRS);
803 i->dev_attrs[count] = NULL;
804
805 return &i->t;
806 }
807
808 /**
809 * ata_release_transport -- release ATA transport template instance
810 * @t: transport template instance
811 */
ata_release_transport(struct scsi_transport_template * t)812 void ata_release_transport(struct scsi_transport_template *t)
813 {
814 struct ata_internal *i = to_ata_internal(t);
815
816 transport_container_unregister(&i->t.host_attrs);
817 transport_container_unregister(&i->link_attr_cont);
818 transport_container_unregister(&i->dev_attr_cont);
819
820 kfree(i);
821 }
822
libata_transport_init(void)823 __init int libata_transport_init(void)
824 {
825 int error;
826
827 error = transport_class_register(&ata_link_class);
828 if (error)
829 goto out_unregister_transport;
830 error = transport_class_register(&ata_port_class);
831 if (error)
832 goto out_unregister_link;
833 error = transport_class_register(&ata_dev_class);
834 if (error)
835 goto out_unregister_port;
836 return 0;
837
838 out_unregister_port:
839 transport_class_unregister(&ata_port_class);
840 out_unregister_link:
841 transport_class_unregister(&ata_link_class);
842 out_unregister_transport:
843 return error;
844
845 }
846
libata_transport_exit(void)847 void __exit libata_transport_exit(void)
848 {
849 transport_class_unregister(&ata_link_class);
850 transport_class_unregister(&ata_port_class);
851 transport_class_unregister(&ata_dev_class);
852 }
853