libata-transport.c 21 KB

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