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