Blame view

drivers/ata/libata-transport.c 20.2 KB
59bd9ded4   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
d9027470b   Gwendal Grignou   [libata] Add ATA ...
2
3
  /*
   *  Copyright 2008 ioogle, Inc.  All rights reserved.
d9027470b   Gwendal Grignou   [libata] Add ATA ...
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
   *
   * Libata transport class.
   *
   * The ATA transport class contains common code to deal with ATA HBAs,
   * an approximated representation of ATA topologies in the driver model,
   * and various sysfs attributes to expose these topologies and management
   * interfaces to user-space.
   *
   * There are 3 objects defined in in this class:
   * - ata_port
   * - ata_link
   * - ata_device
   * Each port has a link object. Each link can have up to two devices for PATA
   * and generally one for SATA.
   * If there is SATA port multiplier [PMP], 15 additional ata_link object are
   * created.
   *
   * These objects are created when the ata host is initialized and when a PMP is
   * found. They are removed only when the HBA is removed, cleaned before the
   * error handler runs.
   */
  
  
  #include <linux/kernel.h>
  #include <linux/blkdev.h>
  #include <linux/spinlock.h>
6a2148c6d   Jeff Garzik   drivers/ata/libat...
30
  #include <linux/slab.h>
d9027470b   Gwendal Grignou   [libata] Add ATA ...
31
32
33
34
  #include <scsi/scsi_transport.h>
  #include <linux/libata.h>
  #include <linux/hdreg.h>
  #include <linux/uaccess.h>
9ee4f3933   Lin Ming   ata: add ata port...
35
  #include <linux/pm_runtime.h>
d9027470b   Gwendal Grignou   [libata] Add ATA ...
36
37
38
  
  #include "libata.h"
  #include "libata-transport.h"
e628dc999   David Milburn   libata: export at...
39
  #define ATA_PORT_ATTRS		3
d9027470b   Gwendal Grignou   [libata] Add ATA ...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  #define ATA_LINK_ATTRS		3
  #define ATA_DEV_ATTRS		9
  
  struct scsi_transport_template;
  struct scsi_transport_template *ata_scsi_transport_template;
  
  struct ata_internal {
  	struct scsi_transport_template t;
  
  	struct device_attribute private_port_attrs[ATA_PORT_ATTRS];
  	struct device_attribute private_link_attrs[ATA_LINK_ATTRS];
  	struct device_attribute private_dev_attrs[ATA_DEV_ATTRS];
  
  	struct transport_container link_attr_cont;
  	struct transport_container dev_attr_cont;
  
  	/*
  	 * The array of null terminated pointers to attributes
  	 * needed by scsi_sysfs.c
  	 */
  	struct device_attribute *link_attrs[ATA_LINK_ATTRS + 1];
  	struct device_attribute *port_attrs[ATA_PORT_ATTRS + 1];
  	struct device_attribute *dev_attrs[ATA_DEV_ATTRS + 1];
  };
  #define to_ata_internal(tmpl)	container_of(tmpl, struct ata_internal, t)
  
  
  #define tdev_to_device(d)					\
  	container_of((d), struct ata_device, tdev)
  #define transport_class_to_dev(dev)				\
  	tdev_to_device((dev)->parent)
  
  #define tdev_to_link(d)						\
  	container_of((d), struct ata_link, tdev)
  #define transport_class_to_link(dev)				\
  	tdev_to_link((dev)->parent)
  
  #define tdev_to_port(d)						\
  	container_of((d), struct ata_port, tdev)
  #define transport_class_to_port(dev)				\
  	tdev_to_port((dev)->parent)
  
  
  /* Device objects are always created whit link objects */
  static int ata_tdev_add(struct ata_device *dev);
  static void ata_tdev_delete(struct ata_device *dev);
  
  
  /*
   * Hack to allow attributes of the same name in different objects.
   */
  #define ATA_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  	struct device_attribute device_attr_##_prefix##_##_name = \
  	__ATTR(_name,_mode,_show,_store)
  
  #define ata_bitfield_name_match(title, table)			\
  static ssize_t							\
  get_ata_##title##_names(u32 table_key, char *buf)		\
  {								\
  	char *prefix = "";					\
  	ssize_t len = 0;					\
  	int i;							\
  								\
  	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
  		if (table[i].value & table_key) {		\
  			len += sprintf(buf + len, "%s%s",	\
  				prefix, table[i].name);		\
  			prefix = ", ";				\
  		}						\
  	}							\
  	len += sprintf(buf + len, "
  ");			\
  	return len;						\
  }
  
  #define ata_bitfield_name_search(title, table)			\
  static ssize_t							\
  get_ata_##title##_names(u32 table_key, char *buf)		\
  {								\
  	ssize_t len = 0;					\
  	int i;							\
  								\
  	for (i = 0; i < ARRAY_SIZE(table); i++) {		\
  		if (table[i].value == table_key) {		\
  			len += sprintf(buf + len, "%s",		\
  				table[i].name);			\
  			break;					\
  		}						\
  	}							\
  	len += sprintf(buf + len, "
  ");			\
  	return len;						\
  }
  
  static struct {
  	u32		value;
  	char		*name;
  } ata_class_names[] = {
  	{ ATA_DEV_UNKNOWN,		"unknown" },
  	{ ATA_DEV_ATA,			"ata" },
  	{ ATA_DEV_ATA_UNSUP,		"ata" },
  	{ ATA_DEV_ATAPI,		"atapi" },
  	{ ATA_DEV_ATAPI_UNSUP,		"atapi" },
  	{ ATA_DEV_PMP,			"pmp" },
  	{ ATA_DEV_PMP_UNSUP,		"pmp" },
  	{ ATA_DEV_SEMB,			"semb" },
  	{ ATA_DEV_SEMB_UNSUP,		"semb" },
9162c6579   Hannes Reinecke   libata: Implement...
147
  	{ ATA_DEV_ZAC,			"zac" },
d9027470b   Gwendal Grignou   [libata] Add ATA ...
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
  	{ ATA_DEV_NONE,			"none" }
  };
  ata_bitfield_name_search(class, ata_class_names)
  
  
  static struct {
  	u32		value;
  	char		*name;
  } ata_err_names[] = {
  	{ AC_ERR_DEV,			"DeviceError" },
  	{ AC_ERR_HSM,			"HostStateMachineError" },
  	{ AC_ERR_TIMEOUT,		"Timeout" },
  	{ AC_ERR_MEDIA,			"MediaError" },
  	{ AC_ERR_ATA_BUS,		"BusError" },
  	{ AC_ERR_HOST_BUS,		"HostBusError" },
  	{ AC_ERR_SYSTEM,		"SystemError" },
  	{ AC_ERR_INVALID,		"InvalidArg" },
  	{ AC_ERR_OTHER,			"Unknown" },
  	{ AC_ERR_NODEV_HINT,		"NoDeviceHint" },
  	{ AC_ERR_NCQ,		 	"NCQError" }
  };
  ata_bitfield_name_match(err, ata_err_names)
  
  static struct {
  	u32		value;
  	char		*name;
  } ata_xfer_names[] = {
  	{ XFER_UDMA_7,			"XFER_UDMA_7" },
  	{ XFER_UDMA_6,			"XFER_UDMA_6" },
  	{ XFER_UDMA_5,			"XFER_UDMA_5" },
  	{ XFER_UDMA_4,			"XFER_UDMA_4" },
  	{ XFER_UDMA_3,			"XFER_UDMA_3" },
  	{ XFER_UDMA_2,			"XFER_UDMA_2" },
  	{ XFER_UDMA_1,			"XFER_UDMA_1" },
  	{ XFER_UDMA_0,			"XFER_UDMA_0" },
  	{ XFER_MW_DMA_4,		"XFER_MW_DMA_4" },
  	{ XFER_MW_DMA_3,		"XFER_MW_DMA_3" },
  	{ XFER_MW_DMA_2,		"XFER_MW_DMA_2" },
  	{ XFER_MW_DMA_1,		"XFER_MW_DMA_1" },
  	{ XFER_MW_DMA_0,		"XFER_MW_DMA_0" },
  	{ XFER_SW_DMA_2,		"XFER_SW_DMA_2" },
  	{ XFER_SW_DMA_1,		"XFER_SW_DMA_1" },
  	{ XFER_SW_DMA_0,		"XFER_SW_DMA_0" },
  	{ XFER_PIO_6,			"XFER_PIO_6" },
  	{ XFER_PIO_5,			"XFER_PIO_5" },
  	{ XFER_PIO_4,			"XFER_PIO_4" },
  	{ XFER_PIO_3,			"XFER_PIO_3" },
  	{ XFER_PIO_2,			"XFER_PIO_2" },
  	{ XFER_PIO_1,			"XFER_PIO_1" },
  	{ XFER_PIO_0,			"XFER_PIO_0" },
  	{ XFER_PIO_SLOW,		"XFER_PIO_SLOW" }
  };
  ata_bitfield_name_match(xfer,ata_xfer_names)
  
  /*
   * ATA Port attributes
   */
  #define ata_port_show_simple(field, name, format_string, cast)		\
  static ssize_t								\
  show_ata_port_##name(struct device *dev,				\
  		     struct device_attribute *attr, char *buf)		\
  {									\
  	struct ata_port *ap = transport_class_to_port(dev);		\
  									\
  	return snprintf(buf, 20, format_string, cast ap->field);	\
  }
  
  #define ata_port_simple_attr(field, name, format_string, type)		\
  	ata_port_show_simple(field, name, format_string, (type))	\
  static DEVICE_ATTR(name, S_IRUGO, show_ata_port_##name, NULL)
  
  ata_port_simple_attr(nr_pmp_links, nr_pmp_links, "%d
  ", int);
  ata_port_simple_attr(stats.idle_irq, idle_irq, "%ld
  ", unsigned long);
e628dc999   David Milburn   libata: export at...
223
224
  ata_port_simple_attr(local_port_no, port_no, "%u
  ", unsigned int);
d9027470b   Gwendal Grignou   [libata] Add ATA ...
225
226
227
228
229
230
  
  static DECLARE_TRANSPORT_CLASS(ata_port_class,
  			       "ata_port", NULL, NULL, NULL);
  
  static void ata_tport_release(struct device *dev)
  {
2623c7a5f   Taras Kondratiuk   libata: add refco...
231
232
  	struct ata_port *ap = tdev_to_port(dev);
  	ata_host_put(ap->host);
d9027470b   Gwendal Grignou   [libata] Add ATA ...
233
234
235
236
237
238
239
240
241
  }
  
  /**
   * ata_is_port --  check if a struct device represents a ATA port
   * @dev:	device to check
   *
   * Returns:
   *	%1 if the device represents a ATA Port, %0 else
   */
b0e73af78   H Hartley Sweeten   libata-transport....
242
  static int ata_is_port(const struct device *dev)
d9027470b   Gwendal Grignou   [libata] Add ATA ...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
  {
  	return dev->release == ata_tport_release;
  }
  
  static int ata_tport_match(struct attribute_container *cont,
  			   struct device *dev)
  {
  	if (!ata_is_port(dev))
  		return 0;
  	return &ata_scsi_transport_template->host_attrs.ac == cont;
  }
  
  /**
   * ata_tport_delete  --  remove ATA PORT
   * @port:	ATA PORT to remove
   *
   * Removes the specified ATA PORT.  Remove the associated link as well.
   */
  void ata_tport_delete(struct ata_port *ap)
  {
  	struct device *dev = &ap->tdev;
  
  	ata_tlink_delete(&ap->link);
  
  	transport_remove_device(dev);
  	device_del(dev);
  	transport_destroy_device(dev);
  	put_device(dev);
  }
  
  /** ata_tport_add - initialize a transport ATA port structure
   *
   * @parent:	parent device
   * @ap:		existing ata_port structure
   *
   * Initialize a ATA port structure for sysfs.  It will be added to the device
   * tree below the device specified by @parent which could be a PCI device.
   *
   * Returns %0 on success
   */
  int ata_tport_add(struct device *parent,
  		  struct ata_port *ap)
  {
  	int error;
  	struct device *dev = &ap->tdev;
  
  	device_initialize(dev);
5ef410829   Lin Ming   ata: add ata port...
290
  	dev->type = &ata_port_type;
d9027470b   Gwendal Grignou   [libata] Add ATA ...
291

d85fc67dd   Gwendal Grignou   libata: transport...
292
  	dev->parent = parent;
2623c7a5f   Taras Kondratiuk   libata: add refco...
293
  	ata_host_get(ap->host);
d9027470b   Gwendal Grignou   [libata] Add ATA ...
294
295
296
  	dev->release = ata_tport_release;
  	dev_set_name(dev, "ata%d", ap->print_id);
  	transport_setup_device(dev);
f1bc1e4c4   Aaron Lu   ata: acpi: rework...
297
  	ata_acpi_bind_port(ap);
d9027470b   Gwendal Grignou   [libata] Add ATA ...
298
299
300
301
  	error = device_add(dev);
  	if (error) {
  		goto tport_err;
  	}
966f1212e   Lin Ming   [libata] enable a...
302
  	device_enable_async_suspend(dev);
9ee4f3933   Lin Ming   ata: add ata port...
303
304
  	pm_runtime_set_active(dev);
  	pm_runtime_enable(dev);
0c8d32c27   Lin Ming   libata: forbid po...
305
  	pm_runtime_forbid(dev);
9ee4f3933   Lin Ming   ata: add ata port...
306

d9027470b   Gwendal Grignou   [libata] Add ATA ...
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  	transport_add_device(dev);
  	transport_configure_device(dev);
  
  	error = ata_tlink_add(&ap->link);
  	if (error) {
  		goto tport_link_err;
  	}
  	return 0;
  
   tport_link_err:
  	transport_remove_device(dev);
  	device_del(dev);
  
   tport_err:
  	transport_destroy_device(dev);
  	put_device(dev);
2623c7a5f   Taras Kondratiuk   libata: add refco...
323
  	ata_host_put(ap->host);
d9027470b   Gwendal Grignou   [libata] Add ATA ...
324
325
326
327
328
329
330
  	return error;
  }
  
  
  /*
   * ATA link attributes
   */
3e85c3ecb   Gwendal Grignou   libata: Fix displ...
331
  static int noop(int x) { return x; }
d9027470b   Gwendal Grignou   [libata] Add ATA ...
332

3e85c3ecb   Gwendal Grignou   libata: Fix displ...
333
  #define ata_link_show_linkspeed(field, format)			        \
d9027470b   Gwendal Grignou   [libata] Add ATA ...
334
335
336
337
338
339
  static ssize_t								\
  show_ata_link_##field(struct device *dev,				\
  		      struct device_attribute *attr, char *buf)		\
  {									\
  	struct ata_link *link = transport_class_to_link(dev);		\
  									\
3e85c3ecb   Gwendal Grignou   libata: Fix displ...
340
341
  	return sprintf(buf, "%s
  ", sata_spd_string(format(link->field))); \
d9027470b   Gwendal Grignou   [libata] Add ATA ...
342
  }
3e85c3ecb   Gwendal Grignou   libata: Fix displ...
343
344
  #define ata_link_linkspeed_attr(field, format)				\
  	ata_link_show_linkspeed(field, format)				\
d9027470b   Gwendal Grignou   [libata] Add ATA ...
345
  static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL)
3e85c3ecb   Gwendal Grignou   libata: Fix displ...
346
347
348
  ata_link_linkspeed_attr(hw_sata_spd_limit, fls);
  ata_link_linkspeed_attr(sata_spd_limit, fls);
  ata_link_linkspeed_attr(sata_spd, noop);
d9027470b   Gwendal Grignou   [libata] Add ATA ...
349
350
351
352
353
354
355
  
  
  static DECLARE_TRANSPORT_CLASS(ata_link_class,
  		"ata_link", NULL, NULL, NULL);
  
  static void ata_tlink_release(struct device *dev)
  {
d9027470b   Gwendal Grignou   [libata] Add ATA ...
356
357
358
359
360
361
362
363
364
  }
  
  /**
   * ata_is_link --  check if a struct device represents a ATA link
   * @dev:	device to check
   *
   * Returns:
   *	%1 if the device represents a ATA link, %0 else
   */
b0e73af78   H Hartley Sweeten   libata-transport....
365
  static int ata_is_link(const struct device *dev)
d9027470b   Gwendal Grignou   [libata] Add ATA ...
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
  {
  	return dev->release == ata_tlink_release;
  }
  
  static int ata_tlink_match(struct attribute_container *cont,
  			   struct device *dev)
  {
  	struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
  	if (!ata_is_link(dev))
  		return 0;
  	return &i->link_attr_cont.ac == cont;
  }
  
  /**
   * ata_tlink_delete  --  remove ATA LINK
   * @port:	ATA LINK to remove
   *
   * Removes the specified ATA LINK.  remove associated ATA device(s) as well.
   */
  void ata_tlink_delete(struct ata_link *link)
  {
  	struct device *dev = &link->tdev;
  	struct ata_device *ata_dev;
  
  	ata_for_each_dev(ata_dev, link, ALL) {
  		ata_tdev_delete(ata_dev);
  	}
  
  	transport_remove_device(dev);
  	device_del(dev);
  	transport_destroy_device(dev);
  	put_device(dev);
  }
  
  /**
   * ata_tlink_add  --  initialize a transport ATA link structure
   * @link:	allocated ata_link structure.
   *
   * Initialize an ATA LINK structure for sysfs.  It will be added in the
   * device tree below the ATA PORT it belongs to.
   *
   * Returns %0 on success
   */
  int ata_tlink_add(struct ata_link *link)
  {
  	struct device *dev = &link->tdev;
  	struct ata_port *ap = link->ap;
  	struct ata_device *ata_dev;
  	int error;
  
  	device_initialize(dev);
d85fc67dd   Gwendal Grignou   libata: transport...
417
  	dev->parent = &ap->tdev;
d9027470b   Gwendal Grignou   [libata] Add ATA ...
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
  	dev->release = ata_tlink_release;
  	if (ata_is_host_link(link))
  		dev_set_name(dev, "link%d", ap->print_id);
          else
  		dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp);
  
  	transport_setup_device(dev);
  
  	error = device_add(dev);
  	if (error) {
  		goto tlink_err;
  	}
  
  	transport_add_device(dev);
  	transport_configure_device(dev);
  
  	ata_for_each_dev(ata_dev, link, ALL) {
  		error = ata_tdev_add(ata_dev);
  		if (error) {
  			goto tlink_dev_err;
  		}
  	}
  	return 0;
    tlink_dev_err:
  	while (--ata_dev >= link->device) {
  		ata_tdev_delete(ata_dev);
  	}
  	transport_remove_device(dev);
  	device_del(dev);
    tlink_err:
  	transport_destroy_device(dev);
  	put_device(dev);
  	return error;
  }
  
  /*
   * ATA device attributes
   */
  
  #define ata_dev_show_class(title, field)				\
  static ssize_t								\
  show_ata_dev_##field(struct device *dev,				\
  		     struct device_attribute *attr, char *buf)		\
  {									\
  	struct ata_device *ata_dev = transport_class_to_dev(dev);	\
  									\
  	return get_ata_##title##_names(ata_dev->field, buf);		\
  }
  
  #define ata_dev_attr(title, field)					\
  	ata_dev_show_class(title, field)				\
  static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL)
  
  ata_dev_attr(class, class);
  ata_dev_attr(xfer, pio_mode);
  ata_dev_attr(xfer, dma_mode);
  ata_dev_attr(xfer, xfer_mode);
  
  
  #define ata_dev_show_simple(field, format_string, cast)		\
  static ssize_t								\
  show_ata_dev_##field(struct device *dev,				\
  		     struct device_attribute *attr, char *buf)		\
  {									\
  	struct ata_device *ata_dev = transport_class_to_dev(dev);	\
  									\
  	return snprintf(buf, 20, format_string, cast ata_dev->field);	\
  }
  
  #define ata_dev_simple_attr(field, format_string, type)	\
  	ata_dev_show_simple(field, format_string, (type))	\
  static DEVICE_ATTR(field, S_IRUGO, 			\
  		   show_ata_dev_##field, NULL)
  
  ata_dev_simple_attr(spdn_cnt, "%d
  ", int);
  
  struct ata_show_ering_arg {
  	char* buf;
  	int written;
  };
  
  static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg)
  {
  	struct ata_show_ering_arg* arg = void_arg;
f3f99d37e   Arnd Bergmann   ata: fix "ering" ...
503
504
  	u64 seconds;
  	u32 rem;
d9027470b   Gwendal Grignou   [libata] Add ATA ...
505

f3f99d37e   Arnd Bergmann   ata: fix "ering" ...
506
  	seconds = div_u64_rem(ent->timestamp, HZ, &rem);
d9027470b   Gwendal Grignou   [libata] Add ATA ...
507
  	arg->written += sprintf(arg->buf + arg->written,
f3f99d37e   Arnd Bergmann   ata: fix "ering" ...
508
509
  			        "[%5llu.%09lu]", seconds,
  				rem * NSEC_PER_SEC / HZ);
d9027470b   Gwendal Grignou   [libata] Add ATA ...
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
  	arg->written += get_ata_err_names(ent->err_mask,
  					  arg->buf + arg->written);
  	return 0;
  }
  
  static ssize_t
  show_ata_dev_ering(struct device *dev,
  		   struct device_attribute *attr, char *buf)
  {
  	struct ata_device *ata_dev = transport_class_to_dev(dev);
  	struct ata_show_ering_arg arg = { buf, 0 };
  
  	ata_ering_map(&ata_dev->ering, ata_show_ering, &arg);
  	return arg.written;
  }
  
  
  static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL);
  
  static ssize_t
  show_ata_dev_id(struct device *dev,
  		struct device_attribute *attr, char *buf)
  {
  	struct ata_device *ata_dev = transport_class_to_dev(dev);
  	int written = 0, i = 0;
  
  	if (ata_dev->class == ATA_DEV_PMP)
  		return 0;
  	for(i=0;i<ATA_ID_WORDS;i++)  {
  		written += snprintf(buf+written, 20, "%04x%c",
  				    ata_dev->id[i],
  				    ((i+1) & 7) ? ' ' : '
  ');
  	}
  	return written;
  }
  
  static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL);
  
  static ssize_t
  show_ata_dev_gscr(struct device *dev,
  		  struct device_attribute *attr, char *buf)
  {
  	struct ata_device *ata_dev = transport_class_to_dev(dev);
  	int written = 0, i = 0;
  
  	if (ata_dev->class != ATA_DEV_PMP)
  		return 0;
  	for(i=0;i<SATA_PMP_GSCR_DWORDS;i++)  {
  		written += snprintf(buf+written, 20, "%08x%c",
  				    ata_dev->gscr[i],
  				    ((i+1) & 3) ? ' ' : '
  ');
  	}
  	if (SATA_PMP_GSCR_DWORDS & 3)
  		buf[written-1] = '
  ';
  	return written;
  }
  
  static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL);
f30307416   Martin K. Petersen   libata: Expose TR...
571
572
573
574
575
576
577
578
579
  static ssize_t
  show_ata_dev_trim(struct device *dev,
  		  struct device_attribute *attr, char *buf)
  {
  	struct ata_device *ata_dev = transport_class_to_dev(dev);
  	unsigned char *mode;
  
  	if (!ata_id_has_trim(ata_dev->id))
  		mode = "unsupported";
71d126fd2   Arne Fitzenreiter   libata: add ATA_H...
580
581
  	else if (ata_dev->horkage & ATA_HORKAGE_NOTRIM)
  		mode = "forced_unsupported";
f30307416   Martin K. Petersen   libata: Expose TR...
582
583
584
585
586
587
588
589
590
591
592
593
  	else if (ata_dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM)
  			mode = "forced_unqueued";
  	else if (ata_fpdma_dsm_supported(ata_dev))
  		mode = "queued";
  	else
  		mode = "unqueued";
  
  	return snprintf(buf, 20, "%s
  ", mode);
  }
  
  static DEVICE_ATTR(trim, S_IRUGO, show_ata_dev_trim, NULL);
d9027470b   Gwendal Grignou   [libata] Add ATA ...
594
595
596
597
598
  static DECLARE_TRANSPORT_CLASS(ata_dev_class,
  			       "ata_device", NULL, NULL, NULL);
  
  static void ata_tdev_release(struct device *dev)
  {
d9027470b   Gwendal Grignou   [libata] Add ATA ...
599
600
601
602
603
604
605
606
607
  }
  
  /**
   * ata_is_ata_dev  --  check if a struct device represents a ATA device
   * @dev:	device to check
   *
   * Returns:
   *	%1 if the device represents a ATA device, %0 else
   */
b0e73af78   H Hartley Sweeten   libata-transport....
608
  static int ata_is_ata_dev(const struct device *dev)
d9027470b   Gwendal Grignou   [libata] Add ATA ...
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
  {
  	return dev->release == ata_tdev_release;
  }
  
  static int ata_tdev_match(struct attribute_container *cont,
  			  struct device *dev)
  {
  	struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
  	if (!ata_is_ata_dev(dev))
  		return 0;
  	return &i->dev_attr_cont.ac == cont;
  }
  
  /**
   * ata_tdev_free  --  free a ATA LINK
   * @dev:	ATA PHY to free
   *
   * Frees the specified ATA PHY.
   *
   * Note:
   *   This function must only be called on a PHY that has not
   *   successfully been added using ata_tdev_add().
   */
  static void ata_tdev_free(struct ata_device *dev)
  {
  	transport_destroy_device(&dev->tdev);
  	put_device(&dev->tdev);
  }
  
  /**
   * ata_tdev_delete  --  remove ATA device
   * @port:	ATA PORT to remove
   *
   * Removes the specified ATA device.
   */
  static void ata_tdev_delete(struct ata_device *ata_dev)
  {
  	struct device *dev = &ata_dev->tdev;
  
  	transport_remove_device(dev);
  	device_del(dev);
  	ata_tdev_free(ata_dev);
  }
  
  
  /**
   * ata_tdev_add  --  initialize a transport ATA device structure.
   * @ata_dev:	ata_dev structure.
   *
   * Initialize an ATA device structure for sysfs.  It will be added in the
   * device tree below the ATA LINK device it belongs to.
   *
   * Returns %0 on success
   */
  static int ata_tdev_add(struct ata_device *ata_dev)
  {
  	struct device *dev = &ata_dev->tdev;
  	struct ata_link *link = ata_dev->link;
  	struct ata_port *ap = link->ap;
  	int error;
  
  	device_initialize(dev);
d85fc67dd   Gwendal Grignou   libata: transport...
671
  	dev->parent = &link->tdev;
d9027470b   Gwendal Grignou   [libata] Add ATA ...
672
673
674
675
676
677
678
  	dev->release = ata_tdev_release;
  	if (ata_is_host_link(link))
  		dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno);
          else
  		dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp);
  
  	transport_setup_device(dev);
f1bc1e4c4   Aaron Lu   ata: acpi: rework...
679
  	ata_acpi_bind_dev(ata_dev);
d9027470b   Gwendal Grignou   [libata] Add ATA ...
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
  	error = device_add(dev);
  	if (error) {
  		ata_tdev_free(ata_dev);
  		return error;
  	}
  
  	transport_add_device(dev);
  	transport_configure_device(dev);
  	return 0;
  }
  
  
  /*
   * Setup / Teardown code
   */
  
  #define SETUP_TEMPLATE(attrb, field, perm, test)			\
  	i->private_##attrb[count] = dev_attr_##field;		       	\
  	i->private_##attrb[count].attr.mode = perm;			\
  	i->attrb[count] = &i->private_##attrb[count];			\
  	if (test)							\
  		count++
  
  #define SETUP_LINK_ATTRIBUTE(field)					\
  	SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1)
  
  #define SETUP_PORT_ATTRIBUTE(field)					\
  	SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
  
  #define SETUP_DEV_ATTRIBUTE(field)					\
  	SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1)
  
  /**
   * ata_attach_transport  --  instantiate ATA transport template
   */
  struct scsi_transport_template *ata_attach_transport(void)
  {
  	struct ata_internal *i;
  	int count;
  
  	i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL);
  	if (!i)
  		return NULL;
  
  	i->t.eh_strategy_handler	= ata_scsi_error;
d9027470b   Gwendal Grignou   [libata] Add ATA ...
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
  	i->t.user_scan			= ata_scsi_user_scan;
  
  	i->t.host_attrs.ac.attrs = &i->port_attrs[0];
  	i->t.host_attrs.ac.class = &ata_port_class.class;
  	i->t.host_attrs.ac.match = ata_tport_match;
  	transport_container_register(&i->t.host_attrs);
  
  	i->link_attr_cont.ac.class = &ata_link_class.class;
  	i->link_attr_cont.ac.attrs = &i->link_attrs[0];
  	i->link_attr_cont.ac.match = ata_tlink_match;
  	transport_container_register(&i->link_attr_cont);
  
  	i->dev_attr_cont.ac.class = &ata_dev_class.class;
  	i->dev_attr_cont.ac.attrs = &i->dev_attrs[0];
  	i->dev_attr_cont.ac.match = ata_tdev_match;
  	transport_container_register(&i->dev_attr_cont);
  
  	count = 0;
  	SETUP_PORT_ATTRIBUTE(nr_pmp_links);
  	SETUP_PORT_ATTRIBUTE(idle_irq);
e628dc999   David Milburn   libata: export at...
745
  	SETUP_PORT_ATTRIBUTE(port_no);
d9027470b   Gwendal Grignou   [libata] Add ATA ...
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
  	BUG_ON(count > ATA_PORT_ATTRS);
  	i->port_attrs[count] = NULL;
  
  	count = 0;
  	SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit);
  	SETUP_LINK_ATTRIBUTE(sata_spd_limit);
  	SETUP_LINK_ATTRIBUTE(sata_spd);
  	BUG_ON(count > ATA_LINK_ATTRS);
  	i->link_attrs[count] = NULL;
  
  	count = 0;
  	SETUP_DEV_ATTRIBUTE(class);
  	SETUP_DEV_ATTRIBUTE(pio_mode);
  	SETUP_DEV_ATTRIBUTE(dma_mode);
  	SETUP_DEV_ATTRIBUTE(xfer_mode);
  	SETUP_DEV_ATTRIBUTE(spdn_cnt);
  	SETUP_DEV_ATTRIBUTE(ering);
  	SETUP_DEV_ATTRIBUTE(id);
  	SETUP_DEV_ATTRIBUTE(gscr);
f30307416   Martin K. Petersen   libata: Expose TR...
765
  	SETUP_DEV_ATTRIBUTE(trim);
d9027470b   Gwendal Grignou   [libata] Add ATA ...
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
  	BUG_ON(count > ATA_DEV_ATTRS);
  	i->dev_attrs[count] = NULL;
  
  	return &i->t;
  }
  
  /**
   * ata_release_transport  --  release ATA transport template instance
   * @t:		transport template instance
   */
  void ata_release_transport(struct scsi_transport_template *t)
  {
  	struct ata_internal *i = to_ata_internal(t);
  
  	transport_container_unregister(&i->t.host_attrs);
  	transport_container_unregister(&i->link_attr_cont);
  	transport_container_unregister(&i->dev_attr_cont);
  
  	kfree(i);
  }
  
  __init int libata_transport_init(void)
  {
  	int error;
  
  	error = transport_class_register(&ata_link_class);
  	if (error)
  		goto out_unregister_transport;
  	error = transport_class_register(&ata_port_class);
  	if (error)
  		goto out_unregister_link;
  	error = transport_class_register(&ata_dev_class);
  	if (error)
  		goto out_unregister_port;
  	return 0;
  
   out_unregister_port:
  	transport_class_unregister(&ata_port_class);
   out_unregister_link:
  	transport_class_unregister(&ata_link_class);
   out_unregister_transport:
  	return error;
  
  }
  
  void __exit libata_transport_exit(void)
  {
  	transport_class_unregister(&ata_link_class);
  	transport_class_unregister(&ata_port_class);
  	transport_class_unregister(&ata_dev_class);
  }