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