libata-transport.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2008 ioogle, Inc. All rights reserved.
  4. *
  5. * Libata transport class.
  6. *
  7. * The ATA transport class contains common code to deal with ATA HBAs,
  8. * an approximated representation of ATA topologies in the driver model,
  9. * and various sysfs attributes to expose these topologies and management
  10. * interfaces to user-space.
  11. *
  12. * There are 3 objects defined in in this class:
  13. * - ata_port
  14. * - ata_link
  15. * - ata_device
  16. * Each port has a link object. Each link can have up to two devices for PATA
  17. * and generally one for SATA.
  18. * If there is SATA port multiplier [PMP], 15 additional ata_link object are
  19. * created.
  20. *
  21. * These objects are created when the ata host is initialized and when a PMP is
  22. * found. They are removed only when the HBA is removed, cleaned before the
  23. * error handler runs.
  24. */
  25. #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_match(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. * @port: 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. /** ata_tport_add - initialize a transport ATA port structure
  236. *
  237. * @parent: parent device
  238. * @ap: existing ata_port structure
  239. *
  240. * Initialize a ATA port structure for sysfs. It will be added to the device
  241. * tree below the device specified by @parent which could be a PCI device.
  242. *
  243. * Returns %0 on success
  244. */
  245. int ata_tport_add(struct device *parent,
  246. struct ata_port *ap)
  247. {
  248. int error;
  249. struct device *dev = &ap->tdev;
  250. device_initialize(dev);
  251. dev->type = &ata_port_type;
  252. dev->parent = parent;
  253. ata_host_get(ap->host);
  254. dev->release = ata_tport_release;
  255. dev_set_name(dev, "ata%d", ap->print_id);
  256. transport_setup_device(dev);
  257. ata_acpi_bind_port(ap);
  258. error = device_add(dev);
  259. if (error) {
  260. goto tport_err;
  261. }
  262. device_enable_async_suspend(dev);
  263. pm_runtime_set_active(dev);
  264. pm_runtime_enable(dev);
  265. pm_runtime_forbid(dev);
  266. transport_add_device(dev);
  267. transport_configure_device(dev);
  268. error = ata_tlink_add(&ap->link);
  269. if (error) {
  270. goto tport_link_err;
  271. }
  272. return 0;
  273. tport_link_err:
  274. transport_remove_device(dev);
  275. device_del(dev);
  276. tport_err:
  277. transport_destroy_device(dev);
  278. put_device(dev);
  279. ata_host_put(ap->host);
  280. return error;
  281. }
  282. /*
  283. * ATA link attributes
  284. */
  285. static int noop(int x) { return x; }
  286. #define ata_link_show_linkspeed(field, format) \
  287. static ssize_t \
  288. show_ata_link_##field(struct device *dev, \
  289. struct device_attribute *attr, char *buf) \
  290. { \
  291. struct ata_link *link = transport_class_to_link(dev); \
  292. \
  293. return sprintf(buf, "%s\n", sata_spd_string(format(link->field))); \
  294. }
  295. #define ata_link_linkspeed_attr(field, format) \
  296. ata_link_show_linkspeed(field, format) \
  297. static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL)
  298. ata_link_linkspeed_attr(hw_sata_spd_limit, fls);
  299. ata_link_linkspeed_attr(sata_spd_limit, fls);
  300. ata_link_linkspeed_attr(sata_spd, noop);
  301. static DECLARE_TRANSPORT_CLASS(ata_link_class,
  302. "ata_link", NULL, NULL, NULL);
  303. static void ata_tlink_release(struct device *dev)
  304. {
  305. }
  306. /**
  307. * ata_is_link -- check if a struct device represents a ATA link
  308. * @dev: device to check
  309. *
  310. * Returns:
  311. * %1 if the device represents a ATA link, %0 else
  312. */
  313. static int ata_is_link(const struct device *dev)
  314. {
  315. return dev->release == ata_tlink_release;
  316. }
  317. static int ata_tlink_match(struct attribute_container *cont,
  318. struct device *dev)
  319. {
  320. struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
  321. if (!ata_is_link(dev))
  322. return 0;
  323. return &i->link_attr_cont.ac == cont;
  324. }
  325. /**
  326. * ata_tlink_delete -- remove ATA LINK
  327. * @port: ATA LINK to remove
  328. *
  329. * Removes the specified ATA LINK. remove associated ATA device(s) as well.
  330. */
  331. void ata_tlink_delete(struct ata_link *link)
  332. {
  333. struct device *dev = &link->tdev;
  334. struct ata_device *ata_dev;
  335. ata_for_each_dev(ata_dev, link, ALL) {
  336. ata_tdev_delete(ata_dev);
  337. }
  338. transport_remove_device(dev);
  339. device_del(dev);
  340. transport_destroy_device(dev);
  341. put_device(dev);
  342. }
  343. /**
  344. * ata_tlink_add -- initialize a transport ATA link structure
  345. * @link: allocated ata_link structure.
  346. *
  347. * Initialize an ATA LINK structure for sysfs. It will be added in the
  348. * device tree below the ATA PORT it belongs to.
  349. *
  350. * Returns %0 on success
  351. */
  352. int ata_tlink_add(struct ata_link *link)
  353. {
  354. struct device *dev = &link->tdev;
  355. struct ata_port *ap = link->ap;
  356. struct ata_device *ata_dev;
  357. int error;
  358. device_initialize(dev);
  359. dev->parent = &ap->tdev;
  360. dev->release = ata_tlink_release;
  361. if (ata_is_host_link(link))
  362. dev_set_name(dev, "link%d", ap->print_id);
  363. else
  364. dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp);
  365. transport_setup_device(dev);
  366. error = device_add(dev);
  367. if (error) {
  368. goto tlink_err;
  369. }
  370. transport_add_device(dev);
  371. transport_configure_device(dev);
  372. ata_for_each_dev(ata_dev, link, ALL) {
  373. error = ata_tdev_add(ata_dev);
  374. if (error) {
  375. goto tlink_dev_err;
  376. }
  377. }
  378. return 0;
  379. tlink_dev_err:
  380. while (--ata_dev >= link->device) {
  381. ata_tdev_delete(ata_dev);
  382. }
  383. transport_remove_device(dev);
  384. device_del(dev);
  385. tlink_err:
  386. transport_destroy_device(dev);
  387. put_device(dev);
  388. return error;
  389. }
  390. /*
  391. * ATA device attributes
  392. */
  393. #define ata_dev_show_class(title, field) \
  394. static ssize_t \
  395. show_ata_dev_##field(struct device *dev, \
  396. struct device_attribute *attr, char *buf) \
  397. { \
  398. struct ata_device *ata_dev = transport_class_to_dev(dev); \
  399. \
  400. return get_ata_##title##_names(ata_dev->field, buf); \
  401. }
  402. #define ata_dev_attr(title, field) \
  403. ata_dev_show_class(title, field) \
  404. static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL)
  405. ata_dev_attr(class, class);
  406. ata_dev_attr(xfer, pio_mode);
  407. ata_dev_attr(xfer, dma_mode);
  408. ata_dev_attr(xfer, xfer_mode);
  409. #define ata_dev_show_simple(field, format_string, cast) \
  410. static ssize_t \
  411. show_ata_dev_##field(struct device *dev, \
  412. struct device_attribute *attr, char *buf) \
  413. { \
  414. struct ata_device *ata_dev = transport_class_to_dev(dev); \
  415. \
  416. return scnprintf(buf, 20, format_string, cast ata_dev->field); \
  417. }
  418. #define ata_dev_simple_attr(field, format_string, type) \
  419. ata_dev_show_simple(field, format_string, (type)) \
  420. static DEVICE_ATTR(field, S_IRUGO, \
  421. show_ata_dev_##field, NULL)
  422. ata_dev_simple_attr(spdn_cnt, "%d\n", int);
  423. struct ata_show_ering_arg {
  424. char* buf;
  425. int written;
  426. };
  427. static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg)
  428. {
  429. struct ata_show_ering_arg* arg = void_arg;
  430. u64 seconds;
  431. u32 rem;
  432. seconds = div_u64_rem(ent->timestamp, HZ, &rem);
  433. arg->written += sprintf(arg->buf + arg->written,
  434. "[%5llu.%09lu]", seconds,
  435. rem * NSEC_PER_SEC / HZ);
  436. arg->written += get_ata_err_names(ent->err_mask,
  437. arg->buf + arg->written);
  438. return 0;
  439. }
  440. static ssize_t
  441. show_ata_dev_ering(struct device *dev,
  442. struct device_attribute *attr, char *buf)
  443. {
  444. struct ata_device *ata_dev = transport_class_to_dev(dev);
  445. struct ata_show_ering_arg arg = { buf, 0 };
  446. ata_ering_map(&ata_dev->ering, ata_show_ering, &arg);
  447. return arg.written;
  448. }
  449. static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL);
  450. static ssize_t
  451. show_ata_dev_id(struct device *dev,
  452. struct device_attribute *attr, char *buf)
  453. {
  454. struct ata_device *ata_dev = transport_class_to_dev(dev);
  455. int written = 0, i = 0;
  456. if (ata_dev->class == ATA_DEV_PMP)
  457. return 0;
  458. for(i=0;i<ATA_ID_WORDS;i++) {
  459. written += scnprintf(buf+written, 20, "%04x%c",
  460. ata_dev->id[i],
  461. ((i+1) & 7) ? ' ' : '\n');
  462. }
  463. return written;
  464. }
  465. static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL);
  466. static ssize_t
  467. show_ata_dev_gscr(struct device *dev,
  468. struct device_attribute *attr, char *buf)
  469. {
  470. struct ata_device *ata_dev = transport_class_to_dev(dev);
  471. int written = 0, i = 0;
  472. if (ata_dev->class != ATA_DEV_PMP)
  473. return 0;
  474. for(i=0;i<SATA_PMP_GSCR_DWORDS;i++) {
  475. written += scnprintf(buf+written, 20, "%08x%c",
  476. ata_dev->gscr[i],
  477. ((i+1) & 3) ? ' ' : '\n');
  478. }
  479. if (SATA_PMP_GSCR_DWORDS & 3)
  480. buf[written-1] = '\n';
  481. return written;
  482. }
  483. static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL);
  484. static ssize_t
  485. show_ata_dev_trim(struct device *dev,
  486. struct device_attribute *attr, char *buf)
  487. {
  488. struct ata_device *ata_dev = transport_class_to_dev(dev);
  489. unsigned char *mode;
  490. if (!ata_id_has_trim(ata_dev->id))
  491. mode = "unsupported";
  492. else if (ata_dev->horkage & ATA_HORKAGE_NOTRIM)
  493. mode = "forced_unsupported";
  494. else if (ata_dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM)
  495. mode = "forced_unqueued";
  496. else if (ata_fpdma_dsm_supported(ata_dev))
  497. mode = "queued";
  498. else
  499. mode = "unqueued";
  500. return scnprintf(buf, 20, "%s\n", mode);
  501. }
  502. static DEVICE_ATTR(trim, S_IRUGO, show_ata_dev_trim, NULL);
  503. static DECLARE_TRANSPORT_CLASS(ata_dev_class,
  504. "ata_device", NULL, NULL, NULL);
  505. static void ata_tdev_release(struct device *dev)
  506. {
  507. }
  508. /**
  509. * ata_is_ata_dev -- check if a struct device represents a ATA device
  510. * @dev: device to check
  511. *
  512. * Returns:
  513. * %1 if the device represents a ATA device, %0 else
  514. */
  515. static int ata_is_ata_dev(const struct device *dev)
  516. {
  517. return dev->release == ata_tdev_release;
  518. }
  519. static int ata_tdev_match(struct attribute_container *cont,
  520. struct device *dev)
  521. {
  522. struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
  523. if (!ata_is_ata_dev(dev))
  524. return 0;
  525. return &i->dev_attr_cont.ac == cont;
  526. }
  527. /**
  528. * ata_tdev_free -- free a ATA LINK
  529. * @dev: ATA PHY to free
  530. *
  531. * Frees the specified ATA PHY.
  532. *
  533. * Note:
  534. * This function must only be called on a PHY that has not
  535. * successfully been added using ata_tdev_add().
  536. */
  537. static void ata_tdev_free(struct ata_device *dev)
  538. {
  539. transport_destroy_device(&dev->tdev);
  540. put_device(&dev->tdev);
  541. }
  542. /**
  543. * ata_tdev_delete -- remove ATA device
  544. * @port: ATA PORT to remove
  545. *
  546. * Removes the specified ATA device.
  547. */
  548. static void ata_tdev_delete(struct ata_device *ata_dev)
  549. {
  550. struct device *dev = &ata_dev->tdev;
  551. transport_remove_device(dev);
  552. device_del(dev);
  553. ata_tdev_free(ata_dev);
  554. }
  555. /**
  556. * ata_tdev_add -- initialize a transport ATA device structure.
  557. * @ata_dev: ata_dev structure.
  558. *
  559. * Initialize an ATA device structure for sysfs. It will be added in the
  560. * device tree below the ATA LINK device it belongs to.
  561. *
  562. * Returns %0 on success
  563. */
  564. static int ata_tdev_add(struct ata_device *ata_dev)
  565. {
  566. struct device *dev = &ata_dev->tdev;
  567. struct ata_link *link = ata_dev->link;
  568. struct ata_port *ap = link->ap;
  569. int error;
  570. device_initialize(dev);
  571. dev->parent = &link->tdev;
  572. dev->release = ata_tdev_release;
  573. if (ata_is_host_link(link))
  574. dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno);
  575. else
  576. dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp);
  577. transport_setup_device(dev);
  578. ata_acpi_bind_dev(ata_dev);
  579. error = device_add(dev);
  580. if (error) {
  581. ata_tdev_free(ata_dev);
  582. return error;
  583. }
  584. transport_add_device(dev);
  585. transport_configure_device(dev);
  586. return 0;
  587. }
  588. /*
  589. * Setup / Teardown code
  590. */
  591. #define SETUP_TEMPLATE(attrb, field, perm, test) \
  592. i->private_##attrb[count] = dev_attr_##field; \
  593. i->private_##attrb[count].attr.mode = perm; \
  594. i->attrb[count] = &i->private_##attrb[count]; \
  595. if (test) \
  596. count++
  597. #define SETUP_LINK_ATTRIBUTE(field) \
  598. SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1)
  599. #define SETUP_PORT_ATTRIBUTE(field) \
  600. SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
  601. #define SETUP_DEV_ATTRIBUTE(field) \
  602. SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1)
  603. /**
  604. * ata_attach_transport -- instantiate ATA transport template
  605. */
  606. struct scsi_transport_template *ata_attach_transport(void)
  607. {
  608. struct ata_internal *i;
  609. int count;
  610. i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL);
  611. if (!i)
  612. return NULL;
  613. i->t.eh_strategy_handler = ata_scsi_error;
  614. i->t.user_scan = ata_scsi_user_scan;
  615. i->t.host_attrs.ac.attrs = &i->port_attrs[0];
  616. i->t.host_attrs.ac.class = &ata_port_class.class;
  617. i->t.host_attrs.ac.match = ata_tport_match;
  618. transport_container_register(&i->t.host_attrs);
  619. i->link_attr_cont.ac.class = &ata_link_class.class;
  620. i->link_attr_cont.ac.attrs = &i->link_attrs[0];
  621. i->link_attr_cont.ac.match = ata_tlink_match;
  622. transport_container_register(&i->link_attr_cont);
  623. i->dev_attr_cont.ac.class = &ata_dev_class.class;
  624. i->dev_attr_cont.ac.attrs = &i->dev_attrs[0];
  625. i->dev_attr_cont.ac.match = ata_tdev_match;
  626. transport_container_register(&i->dev_attr_cont);
  627. count = 0;
  628. SETUP_PORT_ATTRIBUTE(nr_pmp_links);
  629. SETUP_PORT_ATTRIBUTE(idle_irq);
  630. SETUP_PORT_ATTRIBUTE(port_no);
  631. BUG_ON(count > ATA_PORT_ATTRS);
  632. i->port_attrs[count] = NULL;
  633. count = 0;
  634. SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit);
  635. SETUP_LINK_ATTRIBUTE(sata_spd_limit);
  636. SETUP_LINK_ATTRIBUTE(sata_spd);
  637. BUG_ON(count > ATA_LINK_ATTRS);
  638. i->link_attrs[count] = NULL;
  639. count = 0;
  640. SETUP_DEV_ATTRIBUTE(class);
  641. SETUP_DEV_ATTRIBUTE(pio_mode);
  642. SETUP_DEV_ATTRIBUTE(dma_mode);
  643. SETUP_DEV_ATTRIBUTE(xfer_mode);
  644. SETUP_DEV_ATTRIBUTE(spdn_cnt);
  645. SETUP_DEV_ATTRIBUTE(ering);
  646. SETUP_DEV_ATTRIBUTE(id);
  647. SETUP_DEV_ATTRIBUTE(gscr);
  648. SETUP_DEV_ATTRIBUTE(trim);
  649. BUG_ON(count > ATA_DEV_ATTRS);
  650. i->dev_attrs[count] = NULL;
  651. return &i->t;
  652. }
  653. /**
  654. * ata_release_transport -- release ATA transport template instance
  655. * @t: transport template instance
  656. */
  657. void ata_release_transport(struct scsi_transport_template *t)
  658. {
  659. struct ata_internal *i = to_ata_internal(t);
  660. transport_container_unregister(&i->t.host_attrs);
  661. transport_container_unregister(&i->link_attr_cont);
  662. transport_container_unregister(&i->dev_attr_cont);
  663. kfree(i);
  664. }
  665. __init int libata_transport_init(void)
  666. {
  667. int error;
  668. error = transport_class_register(&ata_link_class);
  669. if (error)
  670. goto out_unregister_transport;
  671. error = transport_class_register(&ata_port_class);
  672. if (error)
  673. goto out_unregister_link;
  674. error = transport_class_register(&ata_dev_class);
  675. if (error)
  676. goto out_unregister_port;
  677. return 0;
  678. out_unregister_port:
  679. transport_class_unregister(&ata_port_class);
  680. out_unregister_link:
  681. transport_class_unregister(&ata_link_class);
  682. out_unregister_transport:
  683. return error;
  684. }
  685. void __exit libata_transport_exit(void)
  686. {
  687. transport_class_unregister(&ata_link_class);
  688. transport_class_unregister(&ata_port_class);
  689. transport_class_unregister(&ata_dev_class);
  690. }