Commit | Line | Data |
---|---|---|
d9027470 GG |
1 | /* |
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> | |
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> | |
35 | ||
36 | #include "libata.h" | |
37 | #include "libata-transport.h" | |
38 | ||
39 | #define ATA_PORT_ATTRS 2 | |
40 | #define ATA_LINK_ATTRS 3 | |
41 | #define ATA_DEV_ATTRS 9 | |
42 | ||
43 | struct scsi_transport_template; | |
44 | struct scsi_transport_template *ata_scsi_transport_template; | |
45 | ||
46 | struct ata_internal { | |
47 | struct scsi_transport_template t; | |
48 | ||
49 | struct device_attribute private_port_attrs[ATA_PORT_ATTRS]; | |
50 | struct device_attribute private_link_attrs[ATA_LINK_ATTRS]; | |
51 | struct device_attribute private_dev_attrs[ATA_DEV_ATTRS]; | |
52 | ||
53 | struct transport_container link_attr_cont; | |
54 | struct transport_container dev_attr_cont; | |
55 | ||
56 | /* | |
57 | * The array of null terminated pointers to attributes | |
58 | * needed by scsi_sysfs.c | |
59 | */ | |
60 | struct device_attribute *link_attrs[ATA_LINK_ATTRS + 1]; | |
61 | struct device_attribute *port_attrs[ATA_PORT_ATTRS + 1]; | |
62 | struct device_attribute *dev_attrs[ATA_DEV_ATTRS + 1]; | |
63 | }; | |
64 | #define to_ata_internal(tmpl) container_of(tmpl, struct ata_internal, t) | |
65 | ||
66 | ||
67 | #define tdev_to_device(d) \ | |
68 | container_of((d), struct ata_device, tdev) | |
69 | #define transport_class_to_dev(dev) \ | |
70 | tdev_to_device((dev)->parent) | |
71 | ||
72 | #define tdev_to_link(d) \ | |
73 | container_of((d), struct ata_link, tdev) | |
74 | #define transport_class_to_link(dev) \ | |
75 | tdev_to_link((dev)->parent) | |
76 | ||
77 | #define tdev_to_port(d) \ | |
78 | container_of((d), struct ata_port, tdev) | |
79 | #define transport_class_to_port(dev) \ | |
80 | tdev_to_port((dev)->parent) | |
81 | ||
82 | ||
83 | /* Device objects are always created whit link objects */ | |
84 | static int ata_tdev_add(struct ata_device *dev); | |
85 | static void ata_tdev_delete(struct ata_device *dev); | |
86 | ||
87 | ||
88 | /* | |
89 | * Hack to allow attributes of the same name in different objects. | |
90 | */ | |
91 | #define ATA_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \ | |
92 | struct device_attribute device_attr_##_prefix##_##_name = \ | |
93 | __ATTR(_name,_mode,_show,_store) | |
94 | ||
95 | #define ata_bitfield_name_match(title, table) \ | |
96 | static ssize_t \ | |
97 | get_ata_##title##_names(u32 table_key, char *buf) \ | |
98 | { \ | |
99 | char *prefix = ""; \ | |
100 | ssize_t len = 0; \ | |
101 | int i; \ | |
102 | \ | |
103 | for (i = 0; i < ARRAY_SIZE(table); i++) { \ | |
104 | if (table[i].value & table_key) { \ | |
105 | len += sprintf(buf + len, "%s%s", \ | |
106 | prefix, table[i].name); \ | |
107 | prefix = ", "; \ | |
108 | } \ | |
109 | } \ | |
110 | len += sprintf(buf + len, "\n"); \ | |
111 | return len; \ | |
112 | } | |
113 | ||
114 | #define ata_bitfield_name_search(title, table) \ | |
115 | static ssize_t \ | |
116 | get_ata_##title##_names(u32 table_key, char *buf) \ | |
117 | { \ | |
118 | ssize_t len = 0; \ | |
119 | int i; \ | |
120 | \ | |
121 | for (i = 0; i < ARRAY_SIZE(table); i++) { \ | |
122 | if (table[i].value == table_key) { \ | |
123 | len += sprintf(buf + len, "%s", \ | |
124 | table[i].name); \ | |
125 | break; \ | |
126 | } \ | |
127 | } \ | |
128 | len += sprintf(buf + len, "\n"); \ | |
129 | return len; \ | |
130 | } | |
131 | ||
132 | static struct { | |
133 | u32 value; | |
134 | char *name; | |
135 | } ata_class_names[] = { | |
136 | { ATA_DEV_UNKNOWN, "unknown" }, | |
137 | { ATA_DEV_ATA, "ata" }, | |
138 | { ATA_DEV_ATA_UNSUP, "ata" }, | |
139 | { ATA_DEV_ATAPI, "atapi" }, | |
140 | { ATA_DEV_ATAPI_UNSUP, "atapi" }, | |
141 | { ATA_DEV_PMP, "pmp" }, | |
142 | { ATA_DEV_PMP_UNSUP, "pmp" }, | |
143 | { ATA_DEV_SEMB, "semb" }, | |
144 | { ATA_DEV_SEMB_UNSUP, "semb" }, | |
145 | { ATA_DEV_NONE, "none" } | |
146 | }; | |
147 | ata_bitfield_name_search(class, ata_class_names) | |
148 | ||
149 | ||
150 | static struct { | |
151 | u32 value; | |
152 | char *name; | |
153 | } ata_err_names[] = { | |
154 | { AC_ERR_DEV, "DeviceError" }, | |
155 | { AC_ERR_HSM, "HostStateMachineError" }, | |
156 | { AC_ERR_TIMEOUT, "Timeout" }, | |
157 | { AC_ERR_MEDIA, "MediaError" }, | |
158 | { AC_ERR_ATA_BUS, "BusError" }, | |
159 | { AC_ERR_HOST_BUS, "HostBusError" }, | |
160 | { AC_ERR_SYSTEM, "SystemError" }, | |
161 | { AC_ERR_INVALID, "InvalidArg" }, | |
162 | { AC_ERR_OTHER, "Unknown" }, | |
163 | { AC_ERR_NODEV_HINT, "NoDeviceHint" }, | |
164 | { AC_ERR_NCQ, "NCQError" } | |
165 | }; | |
166 | ata_bitfield_name_match(err, ata_err_names) | |
167 | ||
168 | static struct { | |
169 | u32 value; | |
170 | char *name; | |
171 | } ata_xfer_names[] = { | |
172 | { XFER_UDMA_7, "XFER_UDMA_7" }, | |
173 | { XFER_UDMA_6, "XFER_UDMA_6" }, | |
174 | { XFER_UDMA_5, "XFER_UDMA_5" }, | |
175 | { XFER_UDMA_4, "XFER_UDMA_4" }, | |
176 | { XFER_UDMA_3, "XFER_UDMA_3" }, | |
177 | { XFER_UDMA_2, "XFER_UDMA_2" }, | |
178 | { XFER_UDMA_1, "XFER_UDMA_1" }, | |
179 | { XFER_UDMA_0, "XFER_UDMA_0" }, | |
180 | { XFER_MW_DMA_4, "XFER_MW_DMA_4" }, | |
181 | { XFER_MW_DMA_3, "XFER_MW_DMA_3" }, | |
182 | { XFER_MW_DMA_2, "XFER_MW_DMA_2" }, | |
183 | { XFER_MW_DMA_1, "XFER_MW_DMA_1" }, | |
184 | { XFER_MW_DMA_0, "XFER_MW_DMA_0" }, | |
185 | { XFER_SW_DMA_2, "XFER_SW_DMA_2" }, | |
186 | { XFER_SW_DMA_1, "XFER_SW_DMA_1" }, | |
187 | { XFER_SW_DMA_0, "XFER_SW_DMA_0" }, | |
188 | { XFER_PIO_6, "XFER_PIO_6" }, | |
189 | { XFER_PIO_5, "XFER_PIO_5" }, | |
190 | { XFER_PIO_4, "XFER_PIO_4" }, | |
191 | { XFER_PIO_3, "XFER_PIO_3" }, | |
192 | { XFER_PIO_2, "XFER_PIO_2" }, | |
193 | { XFER_PIO_1, "XFER_PIO_1" }, | |
194 | { XFER_PIO_0, "XFER_PIO_0" }, | |
195 | { XFER_PIO_SLOW, "XFER_PIO_SLOW" } | |
196 | }; | |
197 | ata_bitfield_name_match(xfer,ata_xfer_names) | |
198 | ||
199 | /* | |
200 | * ATA Port attributes | |
201 | */ | |
202 | #define ata_port_show_simple(field, name, format_string, cast) \ | |
203 | static ssize_t \ | |
204 | show_ata_port_##name(struct device *dev, \ | |
205 | struct device_attribute *attr, char *buf) \ | |
206 | { \ | |
207 | struct ata_port *ap = transport_class_to_port(dev); \ | |
208 | \ | |
209 | return snprintf(buf, 20, format_string, cast ap->field); \ | |
210 | } | |
211 | ||
212 | #define ata_port_simple_attr(field, name, format_string, type) \ | |
213 | ata_port_show_simple(field, name, format_string, (type)) \ | |
214 | static DEVICE_ATTR(name, S_IRUGO, show_ata_port_##name, NULL) | |
215 | ||
216 | ata_port_simple_attr(nr_pmp_links, nr_pmp_links, "%d\n", int); | |
217 | ata_port_simple_attr(stats.idle_irq, idle_irq, "%ld\n", unsigned long); | |
218 | ||
219 | static DECLARE_TRANSPORT_CLASS(ata_port_class, | |
220 | "ata_port", NULL, NULL, NULL); | |
221 | ||
222 | static void ata_tport_release(struct device *dev) | |
223 | { | |
224 | put_device(dev->parent); | |
225 | } | |
226 | ||
227 | /** | |
228 | * ata_is_port -- check if a struct device represents a ATA port | |
229 | * @dev: device to check | |
230 | * | |
231 | * Returns: | |
232 | * %1 if the device represents a ATA Port, %0 else | |
233 | */ | |
234 | int ata_is_port(const struct device *dev) | |
235 | { | |
236 | return dev->release == ata_tport_release; | |
237 | } | |
238 | ||
239 | static int ata_tport_match(struct attribute_container *cont, | |
240 | struct device *dev) | |
241 | { | |
242 | if (!ata_is_port(dev)) | |
243 | return 0; | |
244 | return &ata_scsi_transport_template->host_attrs.ac == cont; | |
245 | } | |
246 | ||
247 | /** | |
248 | * ata_tport_delete -- remove ATA PORT | |
249 | * @port: ATA PORT to remove | |
250 | * | |
251 | * Removes the specified ATA PORT. Remove the associated link as well. | |
252 | */ | |
253 | void ata_tport_delete(struct ata_port *ap) | |
254 | { | |
255 | struct device *dev = &ap->tdev; | |
256 | ||
257 | ata_tlink_delete(&ap->link); | |
258 | ||
259 | transport_remove_device(dev); | |
260 | device_del(dev); | |
261 | transport_destroy_device(dev); | |
262 | put_device(dev); | |
263 | } | |
264 | ||
265 | /** ata_tport_add - initialize a transport ATA port structure | |
266 | * | |
267 | * @parent: parent device | |
268 | * @ap: existing ata_port structure | |
269 | * | |
270 | * Initialize a ATA port structure for sysfs. It will be added to the device | |
271 | * tree below the device specified by @parent which could be a PCI device. | |
272 | * | |
273 | * Returns %0 on success | |
274 | */ | |
275 | int ata_tport_add(struct device *parent, | |
276 | struct ata_port *ap) | |
277 | { | |
278 | int error; | |
279 | struct device *dev = &ap->tdev; | |
280 | ||
281 | device_initialize(dev); | |
5ef41082 | 282 | dev->type = &ata_port_type; |
d9027470 GG |
283 | |
284 | dev->parent = get_device(parent); | |
285 | dev->release = ata_tport_release; | |
286 | dev_set_name(dev, "ata%d", ap->print_id); | |
287 | transport_setup_device(dev); | |
288 | error = device_add(dev); | |
289 | if (error) { | |
290 | goto tport_err; | |
291 | } | |
292 | ||
293 | transport_add_device(dev); | |
294 | transport_configure_device(dev); | |
295 | ||
296 | error = ata_tlink_add(&ap->link); | |
297 | if (error) { | |
298 | goto tport_link_err; | |
299 | } | |
300 | return 0; | |
301 | ||
302 | tport_link_err: | |
303 | transport_remove_device(dev); | |
304 | device_del(dev); | |
305 | ||
306 | tport_err: | |
307 | transport_destroy_device(dev); | |
308 | put_device(dev); | |
309 | return error; | |
310 | } | |
311 | ||
312 | ||
313 | /* | |
314 | * ATA link attributes | |
315 | */ | |
316 | ||
317 | ||
318 | #define ata_link_show_linkspeed(field) \ | |
319 | static ssize_t \ | |
320 | show_ata_link_##field(struct device *dev, \ | |
321 | struct device_attribute *attr, char *buf) \ | |
322 | { \ | |
323 | struct ata_link *link = transport_class_to_link(dev); \ | |
324 | \ | |
325 | return sprintf(buf,"%s\n", sata_spd_string(fls(link->field))); \ | |
326 | } | |
327 | ||
328 | #define ata_link_linkspeed_attr(field) \ | |
329 | ata_link_show_linkspeed(field) \ | |
330 | static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL) | |
331 | ||
332 | ata_link_linkspeed_attr(hw_sata_spd_limit); | |
333 | ata_link_linkspeed_attr(sata_spd_limit); | |
334 | ata_link_linkspeed_attr(sata_spd); | |
335 | ||
336 | ||
337 | static DECLARE_TRANSPORT_CLASS(ata_link_class, | |
338 | "ata_link", NULL, NULL, NULL); | |
339 | ||
340 | static void ata_tlink_release(struct device *dev) | |
341 | { | |
342 | put_device(dev->parent); | |
343 | } | |
344 | ||
345 | /** | |
346 | * ata_is_link -- check if a struct device represents a ATA link | |
347 | * @dev: device to check | |
348 | * | |
349 | * Returns: | |
350 | * %1 if the device represents a ATA link, %0 else | |
351 | */ | |
352 | int ata_is_link(const struct device *dev) | |
353 | { | |
354 | return dev->release == ata_tlink_release; | |
355 | } | |
356 | ||
357 | static int ata_tlink_match(struct attribute_container *cont, | |
358 | struct device *dev) | |
359 | { | |
360 | struct ata_internal* i = to_ata_internal(ata_scsi_transport_template); | |
361 | if (!ata_is_link(dev)) | |
362 | return 0; | |
363 | return &i->link_attr_cont.ac == cont; | |
364 | } | |
365 | ||
366 | /** | |
367 | * ata_tlink_delete -- remove ATA LINK | |
368 | * @port: ATA LINK to remove | |
369 | * | |
370 | * Removes the specified ATA LINK. remove associated ATA device(s) as well. | |
371 | */ | |
372 | void ata_tlink_delete(struct ata_link *link) | |
373 | { | |
374 | struct device *dev = &link->tdev; | |
375 | struct ata_device *ata_dev; | |
376 | ||
377 | ata_for_each_dev(ata_dev, link, ALL) { | |
378 | ata_tdev_delete(ata_dev); | |
379 | } | |
380 | ||
381 | transport_remove_device(dev); | |
382 | device_del(dev); | |
383 | transport_destroy_device(dev); | |
384 | put_device(dev); | |
385 | } | |
386 | ||
387 | /** | |
388 | * ata_tlink_add -- initialize a transport ATA link structure | |
389 | * @link: allocated ata_link structure. | |
390 | * | |
391 | * Initialize an ATA LINK structure for sysfs. It will be added in the | |
392 | * device tree below the ATA PORT it belongs to. | |
393 | * | |
394 | * Returns %0 on success | |
395 | */ | |
396 | int ata_tlink_add(struct ata_link *link) | |
397 | { | |
398 | struct device *dev = &link->tdev; | |
399 | struct ata_port *ap = link->ap; | |
400 | struct ata_device *ata_dev; | |
401 | int error; | |
402 | ||
403 | device_initialize(dev); | |
404 | dev->parent = get_device(&ap->tdev); | |
405 | dev->release = ata_tlink_release; | |
406 | if (ata_is_host_link(link)) | |
407 | dev_set_name(dev, "link%d", ap->print_id); | |
408 | else | |
409 | dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp); | |
410 | ||
411 | transport_setup_device(dev); | |
412 | ||
413 | error = device_add(dev); | |
414 | if (error) { | |
415 | goto tlink_err; | |
416 | } | |
417 | ||
418 | transport_add_device(dev); | |
419 | transport_configure_device(dev); | |
420 | ||
421 | ata_for_each_dev(ata_dev, link, ALL) { | |
422 | error = ata_tdev_add(ata_dev); | |
423 | if (error) { | |
424 | goto tlink_dev_err; | |
425 | } | |
426 | } | |
427 | return 0; | |
428 | tlink_dev_err: | |
429 | while (--ata_dev >= link->device) { | |
430 | ata_tdev_delete(ata_dev); | |
431 | } | |
432 | transport_remove_device(dev); | |
433 | device_del(dev); | |
434 | tlink_err: | |
435 | transport_destroy_device(dev); | |
436 | put_device(dev); | |
437 | return error; | |
438 | } | |
439 | ||
440 | /* | |
441 | * ATA device attributes | |
442 | */ | |
443 | ||
444 | #define ata_dev_show_class(title, field) \ | |
445 | static ssize_t \ | |
446 | show_ata_dev_##field(struct device *dev, \ | |
447 | struct device_attribute *attr, char *buf) \ | |
448 | { \ | |
449 | struct ata_device *ata_dev = transport_class_to_dev(dev); \ | |
450 | \ | |
451 | return get_ata_##title##_names(ata_dev->field, buf); \ | |
452 | } | |
453 | ||
454 | #define ata_dev_attr(title, field) \ | |
455 | ata_dev_show_class(title, field) \ | |
456 | static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL) | |
457 | ||
458 | ata_dev_attr(class, class); | |
459 | ata_dev_attr(xfer, pio_mode); | |
460 | ata_dev_attr(xfer, dma_mode); | |
461 | ata_dev_attr(xfer, xfer_mode); | |
462 | ||
463 | ||
464 | #define ata_dev_show_simple(field, format_string, cast) \ | |
465 | static ssize_t \ | |
466 | show_ata_dev_##field(struct device *dev, \ | |
467 | struct device_attribute *attr, char *buf) \ | |
468 | { \ | |
469 | struct ata_device *ata_dev = transport_class_to_dev(dev); \ | |
470 | \ | |
471 | return snprintf(buf, 20, format_string, cast ata_dev->field); \ | |
472 | } | |
473 | ||
474 | #define ata_dev_simple_attr(field, format_string, type) \ | |
475 | ata_dev_show_simple(field, format_string, (type)) \ | |
476 | static DEVICE_ATTR(field, S_IRUGO, \ | |
477 | show_ata_dev_##field, NULL) | |
478 | ||
479 | ata_dev_simple_attr(spdn_cnt, "%d\n", int); | |
480 | ||
481 | struct ata_show_ering_arg { | |
482 | char* buf; | |
483 | int written; | |
484 | }; | |
485 | ||
486 | static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg) | |
487 | { | |
488 | struct ata_show_ering_arg* arg = void_arg; | |
489 | struct timespec time; | |
490 | ||
491 | jiffies_to_timespec(ent->timestamp,&time); | |
492 | arg->written += sprintf(arg->buf + arg->written, | |
493 | "[%5lu.%06lu]", | |
494 | time.tv_sec, time.tv_nsec); | |
495 | arg->written += get_ata_err_names(ent->err_mask, | |
496 | arg->buf + arg->written); | |
497 | return 0; | |
498 | } | |
499 | ||
500 | static ssize_t | |
501 | show_ata_dev_ering(struct device *dev, | |
502 | struct device_attribute *attr, char *buf) | |
503 | { | |
504 | struct ata_device *ata_dev = transport_class_to_dev(dev); | |
505 | struct ata_show_ering_arg arg = { buf, 0 }; | |
506 | ||
507 | ata_ering_map(&ata_dev->ering, ata_show_ering, &arg); | |
508 | return arg.written; | |
509 | } | |
510 | ||
511 | ||
512 | static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL); | |
513 | ||
514 | static ssize_t | |
515 | show_ata_dev_id(struct device *dev, | |
516 | struct device_attribute *attr, char *buf) | |
517 | { | |
518 | struct ata_device *ata_dev = transport_class_to_dev(dev); | |
519 | int written = 0, i = 0; | |
520 | ||
521 | if (ata_dev->class == ATA_DEV_PMP) | |
522 | return 0; | |
523 | for(i=0;i<ATA_ID_WORDS;i++) { | |
524 | written += snprintf(buf+written, 20, "%04x%c", | |
525 | ata_dev->id[i], | |
526 | ((i+1) & 7) ? ' ' : '\n'); | |
527 | } | |
528 | return written; | |
529 | } | |
530 | ||
531 | static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL); | |
532 | ||
533 | static ssize_t | |
534 | show_ata_dev_gscr(struct device *dev, | |
535 | struct device_attribute *attr, char *buf) | |
536 | { | |
537 | struct ata_device *ata_dev = transport_class_to_dev(dev); | |
538 | int written = 0, i = 0; | |
539 | ||
540 | if (ata_dev->class != ATA_DEV_PMP) | |
541 | return 0; | |
542 | for(i=0;i<SATA_PMP_GSCR_DWORDS;i++) { | |
543 | written += snprintf(buf+written, 20, "%08x%c", | |
544 | ata_dev->gscr[i], | |
545 | ((i+1) & 3) ? ' ' : '\n'); | |
546 | } | |
547 | if (SATA_PMP_GSCR_DWORDS & 3) | |
548 | buf[written-1] = '\n'; | |
549 | return written; | |
550 | } | |
551 | ||
552 | static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL); | |
553 | ||
554 | static DECLARE_TRANSPORT_CLASS(ata_dev_class, | |
555 | "ata_device", NULL, NULL, NULL); | |
556 | ||
557 | static void ata_tdev_release(struct device *dev) | |
558 | { | |
559 | put_device(dev->parent); | |
560 | } | |
561 | ||
562 | /** | |
563 | * ata_is_ata_dev -- check if a struct device represents a ATA device | |
564 | * @dev: device to check | |
565 | * | |
566 | * Returns: | |
567 | * %1 if the device represents a ATA device, %0 else | |
568 | */ | |
569 | int ata_is_ata_dev(const struct device *dev) | |
570 | { | |
571 | return dev->release == ata_tdev_release; | |
572 | } | |
573 | ||
574 | static int ata_tdev_match(struct attribute_container *cont, | |
575 | struct device *dev) | |
576 | { | |
577 | struct ata_internal* i = to_ata_internal(ata_scsi_transport_template); | |
578 | if (!ata_is_ata_dev(dev)) | |
579 | return 0; | |
580 | return &i->dev_attr_cont.ac == cont; | |
581 | } | |
582 | ||
583 | /** | |
584 | * ata_tdev_free -- free a ATA LINK | |
585 | * @dev: ATA PHY to free | |
586 | * | |
587 | * Frees the specified ATA PHY. | |
588 | * | |
589 | * Note: | |
590 | * This function must only be called on a PHY that has not | |
591 | * successfully been added using ata_tdev_add(). | |
592 | */ | |
593 | static void ata_tdev_free(struct ata_device *dev) | |
594 | { | |
595 | transport_destroy_device(&dev->tdev); | |
596 | put_device(&dev->tdev); | |
597 | } | |
598 | ||
599 | /** | |
600 | * ata_tdev_delete -- remove ATA device | |
601 | * @port: ATA PORT to remove | |
602 | * | |
603 | * Removes the specified ATA device. | |
604 | */ | |
605 | static void ata_tdev_delete(struct ata_device *ata_dev) | |
606 | { | |
607 | struct device *dev = &ata_dev->tdev; | |
608 | ||
609 | transport_remove_device(dev); | |
610 | device_del(dev); | |
611 | ata_tdev_free(ata_dev); | |
612 | } | |
613 | ||
614 | ||
615 | /** | |
616 | * ata_tdev_add -- initialize a transport ATA device structure. | |
617 | * @ata_dev: ata_dev structure. | |
618 | * | |
619 | * Initialize an ATA device structure for sysfs. It will be added in the | |
620 | * device tree below the ATA LINK device it belongs to. | |
621 | * | |
622 | * Returns %0 on success | |
623 | */ | |
624 | static int ata_tdev_add(struct ata_device *ata_dev) | |
625 | { | |
626 | struct device *dev = &ata_dev->tdev; | |
627 | struct ata_link *link = ata_dev->link; | |
628 | struct ata_port *ap = link->ap; | |
629 | int error; | |
630 | ||
631 | device_initialize(dev); | |
632 | dev->parent = get_device(&link->tdev); | |
633 | dev->release = ata_tdev_release; | |
634 | if (ata_is_host_link(link)) | |
635 | dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno); | |
636 | else | |
637 | dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp); | |
638 | ||
639 | transport_setup_device(dev); | |
640 | error = device_add(dev); | |
641 | if (error) { | |
642 | ata_tdev_free(ata_dev); | |
643 | return error; | |
644 | } | |
645 | ||
646 | transport_add_device(dev); | |
647 | transport_configure_device(dev); | |
648 | return 0; | |
649 | } | |
650 | ||
651 | ||
652 | /* | |
653 | * Setup / Teardown code | |
654 | */ | |
655 | ||
656 | #define SETUP_TEMPLATE(attrb, field, perm, test) \ | |
657 | i->private_##attrb[count] = dev_attr_##field; \ | |
658 | i->private_##attrb[count].attr.mode = perm; \ | |
659 | i->attrb[count] = &i->private_##attrb[count]; \ | |
660 | if (test) \ | |
661 | count++ | |
662 | ||
663 | #define SETUP_LINK_ATTRIBUTE(field) \ | |
664 | SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1) | |
665 | ||
666 | #define SETUP_PORT_ATTRIBUTE(field) \ | |
667 | SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1) | |
668 | ||
669 | #define SETUP_DEV_ATTRIBUTE(field) \ | |
670 | SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1) | |
671 | ||
672 | /** | |
673 | * ata_attach_transport -- instantiate ATA transport template | |
674 | */ | |
675 | struct scsi_transport_template *ata_attach_transport(void) | |
676 | { | |
677 | struct ata_internal *i; | |
678 | int count; | |
679 | ||
680 | i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL); | |
681 | if (!i) | |
682 | return NULL; | |
683 | ||
684 | i->t.eh_strategy_handler = ata_scsi_error; | |
685 | i->t.eh_timed_out = ata_scsi_timed_out; | |
686 | i->t.user_scan = ata_scsi_user_scan; | |
687 | ||
688 | i->t.host_attrs.ac.attrs = &i->port_attrs[0]; | |
689 | i->t.host_attrs.ac.class = &ata_port_class.class; | |
690 | i->t.host_attrs.ac.match = ata_tport_match; | |
691 | transport_container_register(&i->t.host_attrs); | |
692 | ||
693 | i->link_attr_cont.ac.class = &ata_link_class.class; | |
694 | i->link_attr_cont.ac.attrs = &i->link_attrs[0]; | |
695 | i->link_attr_cont.ac.match = ata_tlink_match; | |
696 | transport_container_register(&i->link_attr_cont); | |
697 | ||
698 | i->dev_attr_cont.ac.class = &ata_dev_class.class; | |
699 | i->dev_attr_cont.ac.attrs = &i->dev_attrs[0]; | |
700 | i->dev_attr_cont.ac.match = ata_tdev_match; | |
701 | transport_container_register(&i->dev_attr_cont); | |
702 | ||
703 | count = 0; | |
704 | SETUP_PORT_ATTRIBUTE(nr_pmp_links); | |
705 | SETUP_PORT_ATTRIBUTE(idle_irq); | |
706 | BUG_ON(count > ATA_PORT_ATTRS); | |
707 | i->port_attrs[count] = NULL; | |
708 | ||
709 | count = 0; | |
710 | SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit); | |
711 | SETUP_LINK_ATTRIBUTE(sata_spd_limit); | |
712 | SETUP_LINK_ATTRIBUTE(sata_spd); | |
713 | BUG_ON(count > ATA_LINK_ATTRS); | |
714 | i->link_attrs[count] = NULL; | |
715 | ||
716 | count = 0; | |
717 | SETUP_DEV_ATTRIBUTE(class); | |
718 | SETUP_DEV_ATTRIBUTE(pio_mode); | |
719 | SETUP_DEV_ATTRIBUTE(dma_mode); | |
720 | SETUP_DEV_ATTRIBUTE(xfer_mode); | |
721 | SETUP_DEV_ATTRIBUTE(spdn_cnt); | |
722 | SETUP_DEV_ATTRIBUTE(ering); | |
723 | SETUP_DEV_ATTRIBUTE(id); | |
724 | SETUP_DEV_ATTRIBUTE(gscr); | |
725 | BUG_ON(count > ATA_DEV_ATTRS); | |
726 | i->dev_attrs[count] = NULL; | |
727 | ||
728 | return &i->t; | |
729 | } | |
730 | ||
731 | /** | |
732 | * ata_release_transport -- release ATA transport template instance | |
733 | * @t: transport template instance | |
734 | */ | |
735 | void ata_release_transport(struct scsi_transport_template *t) | |
736 | { | |
737 | struct ata_internal *i = to_ata_internal(t); | |
738 | ||
739 | transport_container_unregister(&i->t.host_attrs); | |
740 | transport_container_unregister(&i->link_attr_cont); | |
741 | transport_container_unregister(&i->dev_attr_cont); | |
742 | ||
743 | kfree(i); | |
744 | } | |
745 | ||
746 | __init int libata_transport_init(void) | |
747 | { | |
748 | int error; | |
749 | ||
750 | error = transport_class_register(&ata_link_class); | |
751 | if (error) | |
752 | goto out_unregister_transport; | |
753 | error = transport_class_register(&ata_port_class); | |
754 | if (error) | |
755 | goto out_unregister_link; | |
756 | error = transport_class_register(&ata_dev_class); | |
757 | if (error) | |
758 | goto out_unregister_port; | |
759 | return 0; | |
760 | ||
761 | out_unregister_port: | |
762 | transport_class_unregister(&ata_port_class); | |
763 | out_unregister_link: | |
764 | transport_class_unregister(&ata_link_class); | |
765 | out_unregister_transport: | |
766 | return error; | |
767 | ||
768 | } | |
769 | ||
770 | void __exit libata_transport_exit(void) | |
771 | { | |
772 | transport_class_unregister(&ata_link_class); | |
773 | transport_class_unregister(&ata_port_class); | |
774 | transport_class_unregister(&ata_dev_class); | |
775 | } |