libata-transport.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * Copyright 2008 ioogle, Inc. All rights reserved.
  3. * Released under GPL v2.
  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 2
  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_NONE, "none" }
  127. };
  128. ata_bitfield_name_search(class, ata_class_names)
  129. static struct {
  130. u32 value;
  131. char *name;
  132. } ata_err_names[] = {
  133. { AC_ERR_DEV, "DeviceError" },
  134. { AC_ERR_HSM, "HostStateMachineError" },
  135. { AC_ERR_TIMEOUT, "Timeout" },
  136. { AC_ERR_MEDIA, "MediaError" },
  137. { AC_ERR_ATA_BUS, "BusError" },
  138. { AC_ERR_HOST_BUS, "HostBusError" },
  139. { AC_ERR_SYSTEM, "SystemError" },
  140. { AC_ERR_INVALID, "InvalidArg" },
  141. { AC_ERR_OTHER, "Unknown" },
  142. { AC_ERR_NODEV_HINT, "NoDeviceHint" },
  143. { AC_ERR_NCQ, "NCQError" }
  144. };
  145. ata_bitfield_name_match(err, ata_err_names)
  146. static struct {
  147. u32 value;
  148. char *name;
  149. } ata_xfer_names[] = {
  150. { XFER_UDMA_7, "XFER_UDMA_7" },
  151. { XFER_UDMA_6, "XFER_UDMA_6" },
  152. { XFER_UDMA_5, "XFER_UDMA_5" },
  153. { XFER_UDMA_4, "XFER_UDMA_4" },
  154. { XFER_UDMA_3, "XFER_UDMA_3" },
  155. { XFER_UDMA_2, "XFER_UDMA_2" },
  156. { XFER_UDMA_1, "XFER_UDMA_1" },
  157. { XFER_UDMA_0, "XFER_UDMA_0" },
  158. { XFER_MW_DMA_4, "XFER_MW_DMA_4" },
  159. { XFER_MW_DMA_3, "XFER_MW_DMA_3" },
  160. { XFER_MW_DMA_2, "XFER_MW_DMA_2" },
  161. { XFER_MW_DMA_1, "XFER_MW_DMA_1" },
  162. { XFER_MW_DMA_0, "XFER_MW_DMA_0" },
  163. { XFER_SW_DMA_2, "XFER_SW_DMA_2" },
  164. { XFER_SW_DMA_1, "XFER_SW_DMA_1" },
  165. { XFER_SW_DMA_0, "XFER_SW_DMA_0" },
  166. { XFER_PIO_6, "XFER_PIO_6" },
  167. { XFER_PIO_5, "XFER_PIO_5" },
  168. { XFER_PIO_4, "XFER_PIO_4" },
  169. { XFER_PIO_3, "XFER_PIO_3" },
  170. { XFER_PIO_2, "XFER_PIO_2" },
  171. { XFER_PIO_1, "XFER_PIO_1" },
  172. { XFER_PIO_0, "XFER_PIO_0" },
  173. { XFER_PIO_SLOW, "XFER_PIO_SLOW" }
  174. };
  175. ata_bitfield_name_match(xfer,ata_xfer_names)
  176. /*
  177. * ATA Port attributes
  178. */
  179. #define ata_port_show_simple(field, name, format_string, cast) \
  180. static ssize_t \
  181. show_ata_port_##name(struct device *dev, \
  182. struct device_attribute *attr, char *buf) \
  183. { \
  184. struct ata_port *ap = transport_class_to_port(dev); \
  185. \
  186. return snprintf(buf, 20, format_string, cast ap->field); \
  187. }
  188. #define ata_port_simple_attr(field, name, format_string, type) \
  189. ata_port_show_simple(field, name, format_string, (type)) \
  190. static DEVICE_ATTR(name, S_IRUGO, show_ata_port_##name, NULL)
  191. ata_port_simple_attr(nr_pmp_links, nr_pmp_links, "%d\n", int);
  192. ata_port_simple_attr(stats.idle_irq, idle_irq, "%ld\n", unsigned long);
  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. put_device(dev->parent);
  198. }
  199. /**
  200. * ata_is_port -- check if a struct device represents a ATA port
  201. * @dev: device to check
  202. *
  203. * Returns:
  204. * %1 if the device represents a ATA Port, %0 else
  205. */
  206. int ata_is_port(const struct device *dev)
  207. {
  208. return dev->release == ata_tport_release;
  209. }
  210. static int ata_tport_match(struct attribute_container *cont,
  211. struct device *dev)
  212. {
  213. if (!ata_is_port(dev))
  214. return 0;
  215. return &ata_scsi_transport_template->host_attrs.ac == cont;
  216. }
  217. /**
  218. * ata_tport_delete -- remove ATA PORT
  219. * @port: ATA PORT to remove
  220. *
  221. * Removes the specified ATA PORT. Remove the associated link as well.
  222. */
  223. void ata_tport_delete(struct ata_port *ap)
  224. {
  225. struct device *dev = &ap->tdev;
  226. ata_tlink_delete(&ap->link);
  227. transport_remove_device(dev);
  228. device_del(dev);
  229. transport_destroy_device(dev);
  230. put_device(dev);
  231. }
  232. /** ata_tport_add - initialize a transport ATA port structure
  233. *
  234. * @parent: parent device
  235. * @ap: existing ata_port structure
  236. *
  237. * Initialize a ATA port structure for sysfs. It will be added to the device
  238. * tree below the device specified by @parent which could be a PCI device.
  239. *
  240. * Returns %0 on success
  241. */
  242. int ata_tport_add(struct device *parent,
  243. struct ata_port *ap)
  244. {
  245. int error;
  246. struct device *dev = &ap->tdev;
  247. device_initialize(dev);
  248. dev->type = &ata_port_type;
  249. dev->parent = get_device(parent);
  250. dev->release = ata_tport_release;
  251. dev_set_name(dev, "ata%d", ap->print_id);
  252. transport_setup_device(dev);
  253. error = device_add(dev);
  254. if (error) {
  255. goto tport_err;
  256. }
  257. pm_runtime_set_active(dev);
  258. pm_runtime_enable(dev);
  259. transport_add_device(dev);
  260. transport_configure_device(dev);
  261. error = ata_tlink_add(&ap->link);
  262. if (error) {
  263. goto tport_link_err;
  264. }
  265. return 0;
  266. tport_link_err:
  267. transport_remove_device(dev);
  268. device_del(dev);
  269. tport_err:
  270. transport_destroy_device(dev);
  271. put_device(dev);
  272. return error;
  273. }
  274. /*
  275. * ATA link attributes
  276. */
  277. #define ata_link_show_linkspeed(field) \
  278. static ssize_t \
  279. show_ata_link_##field(struct device *dev, \
  280. struct device_attribute *attr, char *buf) \
  281. { \
  282. struct ata_link *link = transport_class_to_link(dev); \
  283. \
  284. return sprintf(buf,"%s\n", sata_spd_string(fls(link->field))); \
  285. }
  286. #define ata_link_linkspeed_attr(field) \
  287. ata_link_show_linkspeed(field) \
  288. static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL)
  289. ata_link_linkspeed_attr(hw_sata_spd_limit);
  290. ata_link_linkspeed_attr(sata_spd_limit);
  291. ata_link_linkspeed_attr(sata_spd);
  292. static DECLARE_TRANSPORT_CLASS(ata_link_class,
  293. "ata_link", NULL, NULL, NULL);
  294. static void ata_tlink_release(struct device *dev)
  295. {
  296. put_device(dev->parent);
  297. }
  298. /**
  299. * ata_is_link -- check if a struct device represents a ATA link
  300. * @dev: device to check
  301. *
  302. * Returns:
  303. * %1 if the device represents a ATA link, %0 else
  304. */
  305. int ata_is_link(const struct device *dev)
  306. {
  307. return dev->release == ata_tlink_release;
  308. }
  309. static int ata_tlink_match(struct attribute_container *cont,
  310. struct device *dev)
  311. {
  312. struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
  313. if (!ata_is_link(dev))
  314. return 0;
  315. return &i->link_attr_cont.ac == cont;
  316. }
  317. /**
  318. * ata_tlink_delete -- remove ATA LINK
  319. * @port: ATA LINK to remove
  320. *
  321. * Removes the specified ATA LINK. remove associated ATA device(s) as well.
  322. */
  323. void ata_tlink_delete(struct ata_link *link)
  324. {
  325. struct device *dev = &link->tdev;
  326. struct ata_device *ata_dev;
  327. ata_for_each_dev(ata_dev, link, ALL) {
  328. ata_tdev_delete(ata_dev);
  329. }
  330. transport_remove_device(dev);
  331. device_del(dev);
  332. transport_destroy_device(dev);
  333. put_device(dev);
  334. }
  335. /**
  336. * ata_tlink_add -- initialize a transport ATA link structure
  337. * @link: allocated ata_link structure.
  338. *
  339. * Initialize an ATA LINK structure for sysfs. It will be added in the
  340. * device tree below the ATA PORT it belongs to.
  341. *
  342. * Returns %0 on success
  343. */
  344. int ata_tlink_add(struct ata_link *link)
  345. {
  346. struct device *dev = &link->tdev;
  347. struct ata_port *ap = link->ap;
  348. struct ata_device *ata_dev;
  349. int error;
  350. device_initialize(dev);
  351. dev->parent = get_device(&ap->tdev);
  352. dev->release = ata_tlink_release;
  353. if (ata_is_host_link(link))
  354. dev_set_name(dev, "link%d", ap->print_id);
  355. else
  356. dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp);
  357. transport_setup_device(dev);
  358. error = device_add(dev);
  359. if (error) {
  360. goto tlink_err;
  361. }
  362. transport_add_device(dev);
  363. transport_configure_device(dev);
  364. ata_for_each_dev(ata_dev, link, ALL) {
  365. error = ata_tdev_add(ata_dev);
  366. if (error) {
  367. goto tlink_dev_err;
  368. }
  369. }
  370. return 0;
  371. tlink_dev_err:
  372. while (--ata_dev >= link->device) {
  373. ata_tdev_delete(ata_dev);
  374. }
  375. transport_remove_device(dev);
  376. device_del(dev);
  377. tlink_err:
  378. transport_destroy_device(dev);
  379. put_device(dev);
  380. return error;
  381. }
  382. /*
  383. * ATA device attributes
  384. */
  385. #define ata_dev_show_class(title, field) \
  386. static ssize_t \
  387. show_ata_dev_##field(struct device *dev, \
  388. struct device_attribute *attr, char *buf) \
  389. { \
  390. struct ata_device *ata_dev = transport_class_to_dev(dev); \
  391. \
  392. return get_ata_##title##_names(ata_dev->field, buf); \
  393. }
  394. #define ata_dev_attr(title, field) \
  395. ata_dev_show_class(title, field) \
  396. static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL)
  397. ata_dev_attr(class, class);
  398. ata_dev_attr(xfer, pio_mode);
  399. ata_dev_attr(xfer, dma_mode);
  400. ata_dev_attr(xfer, xfer_mode);
  401. #define ata_dev_show_simple(field, format_string, cast) \
  402. static ssize_t \
  403. show_ata_dev_##field(struct device *dev, \
  404. struct device_attribute *attr, char *buf) \
  405. { \
  406. struct ata_device *ata_dev = transport_class_to_dev(dev); \
  407. \
  408. return snprintf(buf, 20, format_string, cast ata_dev->field); \
  409. }
  410. #define ata_dev_simple_attr(field, format_string, type) \
  411. ata_dev_show_simple(field, format_string, (type)) \
  412. static DEVICE_ATTR(field, S_IRUGO, \
  413. show_ata_dev_##field, NULL)
  414. ata_dev_simple_attr(spdn_cnt, "%d\n", int);
  415. struct ata_show_ering_arg {
  416. char* buf;
  417. int written;
  418. };
  419. static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg)
  420. {
  421. struct ata_show_ering_arg* arg = void_arg;
  422. struct timespec time;
  423. jiffies_to_timespec(ent->timestamp,&time);
  424. arg->written += sprintf(arg->buf + arg->written,
  425. "[%5lu.%06lu]",
  426. time.tv_sec, time.tv_nsec);
  427. arg->written += get_ata_err_names(ent->err_mask,
  428. arg->buf + arg->written);
  429. return 0;
  430. }
  431. static ssize_t
  432. show_ata_dev_ering(struct device *dev,
  433. struct device_attribute *attr, char *buf)
  434. {
  435. struct ata_device *ata_dev = transport_class_to_dev(dev);
  436. struct ata_show_ering_arg arg = { buf, 0 };
  437. ata_ering_map(&ata_dev->ering, ata_show_ering, &arg);
  438. return arg.written;
  439. }
  440. static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL);
  441. static ssize_t
  442. show_ata_dev_id(struct device *dev,
  443. struct device_attribute *attr, char *buf)
  444. {
  445. struct ata_device *ata_dev = transport_class_to_dev(dev);
  446. int written = 0, i = 0;
  447. if (ata_dev->class == ATA_DEV_PMP)
  448. return 0;
  449. for(i=0;i<ATA_ID_WORDS;i++) {
  450. written += snprintf(buf+written, 20, "%04x%c",
  451. ata_dev->id[i],
  452. ((i+1) & 7) ? ' ' : '\n');
  453. }
  454. return written;
  455. }
  456. static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL);
  457. static ssize_t
  458. show_ata_dev_gscr(struct device *dev,
  459. struct device_attribute *attr, char *buf)
  460. {
  461. struct ata_device *ata_dev = transport_class_to_dev(dev);
  462. int written = 0, i = 0;
  463. if (ata_dev->class != ATA_DEV_PMP)
  464. return 0;
  465. for(i=0;i<SATA_PMP_GSCR_DWORDS;i++) {
  466. written += snprintf(buf+written, 20, "%08x%c",
  467. ata_dev->gscr[i],
  468. ((i+1) & 3) ? ' ' : '\n');
  469. }
  470. if (SATA_PMP_GSCR_DWORDS & 3)
  471. buf[written-1] = '\n';
  472. return written;
  473. }
  474. static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL);
  475. static DECLARE_TRANSPORT_CLASS(ata_dev_class,
  476. "ata_device", NULL, NULL, NULL);
  477. static void ata_tdev_release(struct device *dev)
  478. {
  479. put_device(dev->parent);
  480. }
  481. /**
  482. * ata_is_ata_dev -- check if a struct device represents a ATA device
  483. * @dev: device to check
  484. *
  485. * Returns:
  486. * %1 if the device represents a ATA device, %0 else
  487. */
  488. int ata_is_ata_dev(const struct device *dev)
  489. {
  490. return dev->release == ata_tdev_release;
  491. }
  492. static int ata_tdev_match(struct attribute_container *cont,
  493. struct device *dev)
  494. {
  495. struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
  496. if (!ata_is_ata_dev(dev))
  497. return 0;
  498. return &i->dev_attr_cont.ac == cont;
  499. }
  500. /**
  501. * ata_tdev_free -- free a ATA LINK
  502. * @dev: ATA PHY to free
  503. *
  504. * Frees the specified ATA PHY.
  505. *
  506. * Note:
  507. * This function must only be called on a PHY that has not
  508. * successfully been added using ata_tdev_add().
  509. */
  510. static void ata_tdev_free(struct ata_device *dev)
  511. {
  512. transport_destroy_device(&dev->tdev);
  513. put_device(&dev->tdev);
  514. }
  515. /**
  516. * ata_tdev_delete -- remove ATA device
  517. * @port: ATA PORT to remove
  518. *
  519. * Removes the specified ATA device.
  520. */
  521. static void ata_tdev_delete(struct ata_device *ata_dev)
  522. {
  523. struct device *dev = &ata_dev->tdev;
  524. transport_remove_device(dev);
  525. device_del(dev);
  526. ata_tdev_free(ata_dev);
  527. }
  528. /**
  529. * ata_tdev_add -- initialize a transport ATA device structure.
  530. * @ata_dev: ata_dev structure.
  531. *
  532. * Initialize an ATA device structure for sysfs. It will be added in the
  533. * device tree below the ATA LINK device it belongs to.
  534. *
  535. * Returns %0 on success
  536. */
  537. static int ata_tdev_add(struct ata_device *ata_dev)
  538. {
  539. struct device *dev = &ata_dev->tdev;
  540. struct ata_link *link = ata_dev->link;
  541. struct ata_port *ap = link->ap;
  542. int error;
  543. device_initialize(dev);
  544. dev->parent = get_device(&link->tdev);
  545. dev->release = ata_tdev_release;
  546. if (ata_is_host_link(link))
  547. dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno);
  548. else
  549. dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp);
  550. transport_setup_device(dev);
  551. error = device_add(dev);
  552. if (error) {
  553. ata_tdev_free(ata_dev);
  554. return error;
  555. }
  556. transport_add_device(dev);
  557. transport_configure_device(dev);
  558. return 0;
  559. }
  560. /*
  561. * Setup / Teardown code
  562. */
  563. #define SETUP_TEMPLATE(attrb, field, perm, test) \
  564. i->private_##attrb[count] = dev_attr_##field; \
  565. i->private_##attrb[count].attr.mode = perm; \
  566. i->attrb[count] = &i->private_##attrb[count]; \
  567. if (test) \
  568. count++
  569. #define SETUP_LINK_ATTRIBUTE(field) \
  570. SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1)
  571. #define SETUP_PORT_ATTRIBUTE(field) \
  572. SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
  573. #define SETUP_DEV_ATTRIBUTE(field) \
  574. SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1)
  575. /**
  576. * ata_attach_transport -- instantiate ATA transport template
  577. */
  578. struct scsi_transport_template *ata_attach_transport(void)
  579. {
  580. struct ata_internal *i;
  581. int count;
  582. i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL);
  583. if (!i)
  584. return NULL;
  585. i->t.eh_strategy_handler = ata_scsi_error;
  586. i->t.eh_timed_out = ata_scsi_timed_out;
  587. i->t.user_scan = ata_scsi_user_scan;
  588. i->t.host_attrs.ac.attrs = &i->port_attrs[0];
  589. i->t.host_attrs.ac.class = &ata_port_class.class;
  590. i->t.host_attrs.ac.match = ata_tport_match;
  591. transport_container_register(&i->t.host_attrs);
  592. i->link_attr_cont.ac.class = &ata_link_class.class;
  593. i->link_attr_cont.ac.attrs = &i->link_attrs[0];
  594. i->link_attr_cont.ac.match = ata_tlink_match;
  595. transport_container_register(&i->link_attr_cont);
  596. i->dev_attr_cont.ac.class = &ata_dev_class.class;
  597. i->dev_attr_cont.ac.attrs = &i->dev_attrs[0];
  598. i->dev_attr_cont.ac.match = ata_tdev_match;
  599. transport_container_register(&i->dev_attr_cont);
  600. count = 0;
  601. SETUP_PORT_ATTRIBUTE(nr_pmp_links);
  602. SETUP_PORT_ATTRIBUTE(idle_irq);
  603. BUG_ON(count > ATA_PORT_ATTRS);
  604. i->port_attrs[count] = NULL;
  605. count = 0;
  606. SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit);
  607. SETUP_LINK_ATTRIBUTE(sata_spd_limit);
  608. SETUP_LINK_ATTRIBUTE(sata_spd);
  609. BUG_ON(count > ATA_LINK_ATTRS);
  610. i->link_attrs[count] = NULL;
  611. count = 0;
  612. SETUP_DEV_ATTRIBUTE(class);
  613. SETUP_DEV_ATTRIBUTE(pio_mode);
  614. SETUP_DEV_ATTRIBUTE(dma_mode);
  615. SETUP_DEV_ATTRIBUTE(xfer_mode);
  616. SETUP_DEV_ATTRIBUTE(spdn_cnt);
  617. SETUP_DEV_ATTRIBUTE(ering);
  618. SETUP_DEV_ATTRIBUTE(id);
  619. SETUP_DEV_ATTRIBUTE(gscr);
  620. BUG_ON(count > ATA_DEV_ATTRS);
  621. i->dev_attrs[count] = NULL;
  622. return &i->t;
  623. }
  624. /**
  625. * ata_release_transport -- release ATA transport template instance
  626. * @t: transport template instance
  627. */
  628. void ata_release_transport(struct scsi_transport_template *t)
  629. {
  630. struct ata_internal *i = to_ata_internal(t);
  631. transport_container_unregister(&i->t.host_attrs);
  632. transport_container_unregister(&i->link_attr_cont);
  633. transport_container_unregister(&i->dev_attr_cont);
  634. kfree(i);
  635. }
  636. __init int libata_transport_init(void)
  637. {
  638. int error;
  639. error = transport_class_register(&ata_link_class);
  640. if (error)
  641. goto out_unregister_transport;
  642. error = transport_class_register(&ata_port_class);
  643. if (error)
  644. goto out_unregister_link;
  645. error = transport_class_register(&ata_dev_class);
  646. if (error)
  647. goto out_unregister_port;
  648. return 0;
  649. out_unregister_port:
  650. transport_class_unregister(&ata_port_class);
  651. out_unregister_link:
  652. transport_class_unregister(&ata_link_class);
  653. out_unregister_transport:
  654. return error;
  655. }
  656. void __exit libata_transport_exit(void)
  657. {
  658. transport_class_unregister(&ata_link_class);
  659. transport_class_unregister(&ata_port_class);
  660. transport_class_unregister(&ata_dev_class);
  661. }