libata-transport.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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 <scsi/scsi_transport.h>
  29. #include <linux/libata.h>
  30. #include <linux/hdreg.h>
  31. #include <linux/uaccess.h>
  32. #include "libata.h"
  33. #include "libata-transport.h"
  34. #define ATA_PORT_ATTRS 2
  35. #define ATA_LINK_ATTRS 3
  36. #define ATA_DEV_ATTRS 9
  37. struct scsi_transport_template;
  38. struct scsi_transport_template *ata_scsi_transport_template;
  39. struct ata_internal {
  40. struct scsi_transport_template t;
  41. struct device_attribute private_port_attrs[ATA_PORT_ATTRS];
  42. struct device_attribute private_link_attrs[ATA_LINK_ATTRS];
  43. struct device_attribute private_dev_attrs[ATA_DEV_ATTRS];
  44. struct transport_container link_attr_cont;
  45. struct transport_container dev_attr_cont;
  46. /*
  47. * The array of null terminated pointers to attributes
  48. * needed by scsi_sysfs.c
  49. */
  50. struct device_attribute *link_attrs[ATA_LINK_ATTRS + 1];
  51. struct device_attribute *port_attrs[ATA_PORT_ATTRS + 1];
  52. struct device_attribute *dev_attrs[ATA_DEV_ATTRS + 1];
  53. };
  54. #define to_ata_internal(tmpl) container_of(tmpl, struct ata_internal, t)
  55. #define tdev_to_device(d) \
  56. container_of((d), struct ata_device, tdev)
  57. #define transport_class_to_dev(dev) \
  58. tdev_to_device((dev)->parent)
  59. #define tdev_to_link(d) \
  60. container_of((d), struct ata_link, tdev)
  61. #define transport_class_to_link(dev) \
  62. tdev_to_link((dev)->parent)
  63. #define tdev_to_port(d) \
  64. container_of((d), struct ata_port, tdev)
  65. #define transport_class_to_port(dev) \
  66. tdev_to_port((dev)->parent)
  67. /* Device objects are always created whit link objects */
  68. static int ata_tdev_add(struct ata_device *dev);
  69. static void ata_tdev_delete(struct ata_device *dev);
  70. /*
  71. * Hack to allow attributes of the same name in different objects.
  72. */
  73. #define ATA_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  74. struct device_attribute device_attr_##_prefix##_##_name = \
  75. __ATTR(_name,_mode,_show,_store)
  76. #define ata_bitfield_name_match(title, table) \
  77. static ssize_t \
  78. get_ata_##title##_names(u32 table_key, char *buf) \
  79. { \
  80. char *prefix = ""; \
  81. ssize_t len = 0; \
  82. int i; \
  83. \
  84. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  85. if (table[i].value & table_key) { \
  86. len += sprintf(buf + len, "%s%s", \
  87. prefix, table[i].name); \
  88. prefix = ", "; \
  89. } \
  90. } \
  91. len += sprintf(buf + len, "\n"); \
  92. return len; \
  93. }
  94. #define ata_bitfield_name_search(title, table) \
  95. static ssize_t \
  96. get_ata_##title##_names(u32 table_key, char *buf) \
  97. { \
  98. ssize_t len = 0; \
  99. int i; \
  100. \
  101. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  102. if (table[i].value == table_key) { \
  103. len += sprintf(buf + len, "%s", \
  104. table[i].name); \
  105. break; \
  106. } \
  107. } \
  108. len += sprintf(buf + len, "\n"); \
  109. return len; \
  110. }
  111. static struct {
  112. u32 value;
  113. char *name;
  114. } ata_class_names[] = {
  115. { ATA_DEV_UNKNOWN, "unknown" },
  116. { ATA_DEV_ATA, "ata" },
  117. { ATA_DEV_ATA_UNSUP, "ata" },
  118. { ATA_DEV_ATAPI, "atapi" },
  119. { ATA_DEV_ATAPI_UNSUP, "atapi" },
  120. { ATA_DEV_PMP, "pmp" },
  121. { ATA_DEV_PMP_UNSUP, "pmp" },
  122. { ATA_DEV_SEMB, "semb" },
  123. { ATA_DEV_SEMB_UNSUP, "semb" },
  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_match(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 snprintf(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. static DECLARE_TRANSPORT_CLASS(ata_port_class,
  192. "ata_port", NULL, NULL, NULL);
  193. static void ata_tport_release(struct device *dev)
  194. {
  195. put_device(dev->parent);
  196. }
  197. /**
  198. * ata_is_port -- check if a struct device represents a ATA port
  199. * @dev: device to check
  200. *
  201. * Returns:
  202. * %1 if the device represents a ATA Port, %0 else
  203. */
  204. int ata_is_port(const struct device *dev)
  205. {
  206. return dev->release == ata_tport_release;
  207. }
  208. static int ata_tport_match(struct attribute_container *cont,
  209. struct device *dev)
  210. {
  211. if (!ata_is_port(dev))
  212. return 0;
  213. return &ata_scsi_transport_template->host_attrs.ac == cont;
  214. }
  215. /**
  216. * ata_tport_delete -- remove ATA PORT
  217. * @port: ATA PORT to remove
  218. *
  219. * Removes the specified ATA PORT. Remove the associated link as well.
  220. */
  221. void ata_tport_delete(struct ata_port *ap)
  222. {
  223. struct device *dev = &ap->tdev;
  224. ata_tlink_delete(&ap->link);
  225. transport_remove_device(dev);
  226. device_del(dev);
  227. transport_destroy_device(dev);
  228. put_device(dev);
  229. }
  230. /** ata_tport_add - initialize a transport ATA port structure
  231. *
  232. * @parent: parent device
  233. * @ap: existing ata_port structure
  234. *
  235. * Initialize a ATA port structure for sysfs. It will be added to the device
  236. * tree below the device specified by @parent which could be a PCI device.
  237. *
  238. * Returns %0 on success
  239. */
  240. int ata_tport_add(struct device *parent,
  241. struct ata_port *ap)
  242. {
  243. int error;
  244. struct device *dev = &ap->tdev;
  245. device_initialize(dev);
  246. dev->parent = get_device(parent);
  247. dev->release = ata_tport_release;
  248. dev_set_name(dev, "ata%d", ap->print_id);
  249. transport_setup_device(dev);
  250. error = device_add(dev);
  251. if (error) {
  252. goto tport_err;
  253. }
  254. transport_add_device(dev);
  255. transport_configure_device(dev);
  256. error = ata_tlink_add(&ap->link);
  257. if (error) {
  258. goto tport_link_err;
  259. }
  260. return 0;
  261. tport_link_err:
  262. transport_remove_device(dev);
  263. device_del(dev);
  264. tport_err:
  265. transport_destroy_device(dev);
  266. put_device(dev);
  267. return error;
  268. }
  269. /*
  270. * ATA link attributes
  271. */
  272. #define ata_link_show_linkspeed(field) \
  273. static ssize_t \
  274. show_ata_link_##field(struct device *dev, \
  275. struct device_attribute *attr, char *buf) \
  276. { \
  277. struct ata_link *link = transport_class_to_link(dev); \
  278. \
  279. return sprintf(buf,"%s\n", sata_spd_string(fls(link->field))); \
  280. }
  281. #define ata_link_linkspeed_attr(field) \
  282. ata_link_show_linkspeed(field) \
  283. static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL)
  284. ata_link_linkspeed_attr(hw_sata_spd_limit);
  285. ata_link_linkspeed_attr(sata_spd_limit);
  286. ata_link_linkspeed_attr(sata_spd);
  287. static DECLARE_TRANSPORT_CLASS(ata_link_class,
  288. "ata_link", NULL, NULL, NULL);
  289. static void ata_tlink_release(struct device *dev)
  290. {
  291. put_device(dev->parent);
  292. }
  293. /**
  294. * ata_is_link -- check if a struct device represents a ATA link
  295. * @dev: device to check
  296. *
  297. * Returns:
  298. * %1 if the device represents a ATA link, %0 else
  299. */
  300. int ata_is_link(const struct device *dev)
  301. {
  302. return dev->release == ata_tlink_release;
  303. }
  304. static int ata_tlink_match(struct attribute_container *cont,
  305. struct device *dev)
  306. {
  307. struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
  308. if (!ata_is_link(dev))
  309. return 0;
  310. return &i->link_attr_cont.ac == cont;
  311. }
  312. /**
  313. * ata_tlink_delete -- remove ATA LINK
  314. * @port: ATA LINK to remove
  315. *
  316. * Removes the specified ATA LINK. remove associated ATA device(s) as well.
  317. */
  318. void ata_tlink_delete(struct ata_link *link)
  319. {
  320. struct device *dev = &link->tdev;
  321. struct ata_device *ata_dev;
  322. ata_for_each_dev(ata_dev, link, ALL) {
  323. ata_tdev_delete(ata_dev);
  324. }
  325. transport_remove_device(dev);
  326. device_del(dev);
  327. transport_destroy_device(dev);
  328. put_device(dev);
  329. }
  330. /**
  331. * ata_tlink_add -- initialize a transport ATA link structure
  332. * @link: allocated ata_link structure.
  333. *
  334. * Initialize an ATA LINK structure for sysfs. It will be added in the
  335. * device tree below the ATA PORT it belongs to.
  336. *
  337. * Returns %0 on success
  338. */
  339. int ata_tlink_add(struct ata_link *link)
  340. {
  341. struct device *dev = &link->tdev;
  342. struct ata_port *ap = link->ap;
  343. struct ata_device *ata_dev;
  344. int error;
  345. device_initialize(dev);
  346. dev->parent = get_device(&ap->tdev);
  347. dev->release = ata_tlink_release;
  348. if (ata_is_host_link(link))
  349. dev_set_name(dev, "link%d", ap->print_id);
  350. else
  351. dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp);
  352. transport_setup_device(dev);
  353. error = device_add(dev);
  354. if (error) {
  355. goto tlink_err;
  356. }
  357. transport_add_device(dev);
  358. transport_configure_device(dev);
  359. ata_for_each_dev(ata_dev, link, ALL) {
  360. error = ata_tdev_add(ata_dev);
  361. if (error) {
  362. goto tlink_dev_err;
  363. }
  364. }
  365. return 0;
  366. tlink_dev_err:
  367. while (--ata_dev >= link->device) {
  368. ata_tdev_delete(ata_dev);
  369. }
  370. transport_remove_device(dev);
  371. device_del(dev);
  372. tlink_err:
  373. transport_destroy_device(dev);
  374. put_device(dev);
  375. return error;
  376. }
  377. /*
  378. * ATA device attributes
  379. */
  380. #define ata_dev_show_class(title, field) \
  381. static ssize_t \
  382. show_ata_dev_##field(struct device *dev, \
  383. struct device_attribute *attr, char *buf) \
  384. { \
  385. struct ata_device *ata_dev = transport_class_to_dev(dev); \
  386. \
  387. return get_ata_##title##_names(ata_dev->field, buf); \
  388. }
  389. #define ata_dev_attr(title, field) \
  390. ata_dev_show_class(title, field) \
  391. static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL)
  392. ata_dev_attr(class, class);
  393. ata_dev_attr(xfer, pio_mode);
  394. ata_dev_attr(xfer, dma_mode);
  395. ata_dev_attr(xfer, xfer_mode);
  396. #define ata_dev_show_simple(field, format_string, cast) \
  397. static ssize_t \
  398. show_ata_dev_##field(struct device *dev, \
  399. struct device_attribute *attr, char *buf) \
  400. { \
  401. struct ata_device *ata_dev = transport_class_to_dev(dev); \
  402. \
  403. return snprintf(buf, 20, format_string, cast ata_dev->field); \
  404. }
  405. #define ata_dev_simple_attr(field, format_string, type) \
  406. ata_dev_show_simple(field, format_string, (type)) \
  407. static DEVICE_ATTR(field, S_IRUGO, \
  408. show_ata_dev_##field, NULL)
  409. ata_dev_simple_attr(spdn_cnt, "%d\n", int);
  410. struct ata_show_ering_arg {
  411. char* buf;
  412. int written;
  413. };
  414. static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg)
  415. {
  416. struct ata_show_ering_arg* arg = void_arg;
  417. struct timespec time;
  418. jiffies_to_timespec(ent->timestamp,&time);
  419. arg->written += sprintf(arg->buf + arg->written,
  420. "[%5lu.%06lu]",
  421. time.tv_sec, time.tv_nsec);
  422. arg->written += get_ata_err_names(ent->err_mask,
  423. arg->buf + arg->written);
  424. return 0;
  425. }
  426. static ssize_t
  427. show_ata_dev_ering(struct device *dev,
  428. struct device_attribute *attr, char *buf)
  429. {
  430. struct ata_device *ata_dev = transport_class_to_dev(dev);
  431. struct ata_show_ering_arg arg = { buf, 0 };
  432. ata_ering_map(&ata_dev->ering, ata_show_ering, &arg);
  433. return arg.written;
  434. }
  435. static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL);
  436. static ssize_t
  437. show_ata_dev_id(struct device *dev,
  438. struct device_attribute *attr, char *buf)
  439. {
  440. struct ata_device *ata_dev = transport_class_to_dev(dev);
  441. int written = 0, i = 0;
  442. if (ata_dev->class == ATA_DEV_PMP)
  443. return 0;
  444. for(i=0;i<ATA_ID_WORDS;i++) {
  445. written += snprintf(buf+written, 20, "%04x%c",
  446. ata_dev->id[i],
  447. ((i+1) & 7) ? ' ' : '\n');
  448. }
  449. return written;
  450. }
  451. static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL);
  452. static ssize_t
  453. show_ata_dev_gscr(struct device *dev,
  454. struct device_attribute *attr, char *buf)
  455. {
  456. struct ata_device *ata_dev = transport_class_to_dev(dev);
  457. int written = 0, i = 0;
  458. if (ata_dev->class != ATA_DEV_PMP)
  459. return 0;
  460. for(i=0;i<SATA_PMP_GSCR_DWORDS;i++) {
  461. written += snprintf(buf+written, 20, "%08x%c",
  462. ata_dev->gscr[i],
  463. ((i+1) & 3) ? ' ' : '\n');
  464. }
  465. if (SATA_PMP_GSCR_DWORDS & 3)
  466. buf[written-1] = '\n';
  467. return written;
  468. }
  469. static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL);
  470. static DECLARE_TRANSPORT_CLASS(ata_dev_class,
  471. "ata_device", NULL, NULL, NULL);
  472. static void ata_tdev_release(struct device *dev)
  473. {
  474. put_device(dev->parent);
  475. }
  476. /**
  477. * ata_is_ata_dev -- check if a struct device represents a ATA device
  478. * @dev: device to check
  479. *
  480. * Returns:
  481. * %1 if the device represents a ATA device, %0 else
  482. */
  483. int ata_is_ata_dev(const struct device *dev)
  484. {
  485. return dev->release == ata_tdev_release;
  486. }
  487. static int ata_tdev_match(struct attribute_container *cont,
  488. struct device *dev)
  489. {
  490. struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
  491. if (!ata_is_ata_dev(dev))
  492. return 0;
  493. return &i->dev_attr_cont.ac == cont;
  494. }
  495. /**
  496. * ata_tdev_free -- free a ATA LINK
  497. * @dev: ATA PHY to free
  498. *
  499. * Frees the specified ATA PHY.
  500. *
  501. * Note:
  502. * This function must only be called on a PHY that has not
  503. * successfully been added using ata_tdev_add().
  504. */
  505. static void ata_tdev_free(struct ata_device *dev)
  506. {
  507. transport_destroy_device(&dev->tdev);
  508. put_device(&dev->tdev);
  509. }
  510. /**
  511. * ata_tdev_delete -- remove ATA device
  512. * @port: ATA PORT to remove
  513. *
  514. * Removes the specified ATA device.
  515. */
  516. static void ata_tdev_delete(struct ata_device *ata_dev)
  517. {
  518. struct device *dev = &ata_dev->tdev;
  519. transport_remove_device(dev);
  520. device_del(dev);
  521. ata_tdev_free(ata_dev);
  522. }
  523. /**
  524. * ata_tdev_add -- initialize a transport ATA device structure.
  525. * @ata_dev: ata_dev structure.
  526. *
  527. * Initialize an ATA device structure for sysfs. It will be added in the
  528. * device tree below the ATA LINK device it belongs to.
  529. *
  530. * Returns %0 on success
  531. */
  532. static int ata_tdev_add(struct ata_device *ata_dev)
  533. {
  534. struct device *dev = &ata_dev->tdev;
  535. struct ata_link *link = ata_dev->link;
  536. struct ata_port *ap = link->ap;
  537. int error;
  538. device_initialize(dev);
  539. dev->parent = get_device(&link->tdev);
  540. dev->release = ata_tdev_release;
  541. if (ata_is_host_link(link))
  542. dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno);
  543. else
  544. dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp);
  545. transport_setup_device(dev);
  546. error = device_add(dev);
  547. if (error) {
  548. ata_tdev_free(ata_dev);
  549. return error;
  550. }
  551. transport_add_device(dev);
  552. transport_configure_device(dev);
  553. return 0;
  554. }
  555. /*
  556. * Setup / Teardown code
  557. */
  558. #define SETUP_TEMPLATE(attrb, field, perm, test) \
  559. i->private_##attrb[count] = dev_attr_##field; \
  560. i->private_##attrb[count].attr.mode = perm; \
  561. i->attrb[count] = &i->private_##attrb[count]; \
  562. if (test) \
  563. count++
  564. #define SETUP_LINK_ATTRIBUTE(field) \
  565. SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1)
  566. #define SETUP_PORT_ATTRIBUTE(field) \
  567. SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
  568. #define SETUP_DEV_ATTRIBUTE(field) \
  569. SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1)
  570. /**
  571. * ata_attach_transport -- instantiate ATA transport template
  572. */
  573. struct scsi_transport_template *ata_attach_transport(void)
  574. {
  575. struct ata_internal *i;
  576. int count;
  577. i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL);
  578. if (!i)
  579. return NULL;
  580. i->t.eh_strategy_handler = ata_scsi_error;
  581. i->t.eh_timed_out = ata_scsi_timed_out;
  582. i->t.user_scan = ata_scsi_user_scan;
  583. i->t.host_attrs.ac.attrs = &i->port_attrs[0];
  584. i->t.host_attrs.ac.class = &ata_port_class.class;
  585. i->t.host_attrs.ac.match = ata_tport_match;
  586. transport_container_register(&i->t.host_attrs);
  587. i->link_attr_cont.ac.class = &ata_link_class.class;
  588. i->link_attr_cont.ac.attrs = &i->link_attrs[0];
  589. i->link_attr_cont.ac.match = ata_tlink_match;
  590. transport_container_register(&i->link_attr_cont);
  591. i->dev_attr_cont.ac.class = &ata_dev_class.class;
  592. i->dev_attr_cont.ac.attrs = &i->dev_attrs[0];
  593. i->dev_attr_cont.ac.match = ata_tdev_match;
  594. transport_container_register(&i->dev_attr_cont);
  595. count = 0;
  596. SETUP_PORT_ATTRIBUTE(nr_pmp_links);
  597. SETUP_PORT_ATTRIBUTE(idle_irq);
  598. BUG_ON(count > ATA_PORT_ATTRS);
  599. i->port_attrs[count] = NULL;
  600. count = 0;
  601. SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit);
  602. SETUP_LINK_ATTRIBUTE(sata_spd_limit);
  603. SETUP_LINK_ATTRIBUTE(sata_spd);
  604. BUG_ON(count > ATA_LINK_ATTRS);
  605. i->link_attrs[count] = NULL;
  606. count = 0;
  607. SETUP_DEV_ATTRIBUTE(class);
  608. SETUP_DEV_ATTRIBUTE(pio_mode);
  609. SETUP_DEV_ATTRIBUTE(dma_mode);
  610. SETUP_DEV_ATTRIBUTE(xfer_mode);
  611. SETUP_DEV_ATTRIBUTE(spdn_cnt);
  612. SETUP_DEV_ATTRIBUTE(ering);
  613. SETUP_DEV_ATTRIBUTE(id);
  614. SETUP_DEV_ATTRIBUTE(gscr);
  615. BUG_ON(count > ATA_DEV_ATTRS);
  616. i->dev_attrs[count] = NULL;
  617. return &i->t;
  618. }
  619. /**
  620. * ata_release_transport -- release ATA transport template instance
  621. * @t: transport template instance
  622. */
  623. void ata_release_transport(struct scsi_transport_template *t)
  624. {
  625. struct ata_internal *i = to_ata_internal(t);
  626. transport_container_unregister(&i->t.host_attrs);
  627. transport_container_unregister(&i->link_attr_cont);
  628. transport_container_unregister(&i->dev_attr_cont);
  629. kfree(i);
  630. }
  631. __init int libata_transport_init(void)
  632. {
  633. int error;
  634. error = transport_class_register(&ata_link_class);
  635. if (error)
  636. goto out_unregister_transport;
  637. error = transport_class_register(&ata_port_class);
  638. if (error)
  639. goto out_unregister_link;
  640. error = transport_class_register(&ata_dev_class);
  641. if (error)
  642. goto out_unregister_port;
  643. return 0;
  644. out_unregister_port:
  645. transport_class_unregister(&ata_port_class);
  646. out_unregister_link:
  647. transport_class_unregister(&ata_link_class);
  648. out_unregister_transport:
  649. return error;
  650. }
  651. void __exit libata_transport_exit(void)
  652. {
  653. transport_class_unregister(&ata_link_class);
  654. transport_class_unregister(&ata_port_class);
  655. transport_class_unregister(&ata_dev_class);
  656. }