libata-transport.c 19 KB

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