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