libata-transport.c 22 KB

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