pata_acpi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * ACPI PATA driver
  3. *
  4. * (c) 2007 Red Hat <alan@redhat.com>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/pci.h>
  9. #include <linux/init.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <scsi/scsi_host.h>
  14. #include <acpi/acpi_bus.h>
  15. #include <acpi/acnames.h>
  16. #include <acpi/acnamesp.h>
  17. #include <acpi/acparser.h>
  18. #include <acpi/acexcep.h>
  19. #include <acpi/acmacros.h>
  20. #include <acpi/actypes.h>
  21. #include <linux/libata.h>
  22. #include <linux/ata.h>
  23. #define DRV_NAME "pata_acpi"
  24. #define DRV_VERSION "0.2.3"
  25. struct pata_acpi {
  26. struct ata_acpi_gtm gtm;
  27. void *last;
  28. unsigned long mask[2];
  29. };
  30. /**
  31. * pacpi_pre_reset - check for 40/80 pin
  32. * @ap: Port
  33. * @deadline: deadline jiffies for the operation
  34. *
  35. * Perform the PATA port setup we need.
  36. */
  37. static int pacpi_pre_reset(struct ata_link *link, unsigned long deadline)
  38. {
  39. struct ata_port *ap = link->ap;
  40. struct pata_acpi *acpi = ap->private_data;
  41. if (ap->acpi_handle == NULL || ata_acpi_gtm(ap, &acpi->gtm) < 0)
  42. return -ENODEV;
  43. return ata_std_prereset(link, deadline);
  44. }
  45. /**
  46. * pacpi_cable_detect - cable type detection
  47. * @ap: port to detect
  48. *
  49. * Perform device specific cable detection
  50. */
  51. static int pacpi_cable_detect(struct ata_port *ap)
  52. {
  53. struct pata_acpi *acpi = ap->private_data;
  54. if ((acpi->mask[0] | acpi->mask[1]) & (0xF8 << ATA_SHIFT_UDMA))
  55. return ATA_CBL_PATA80;
  56. else
  57. return ATA_CBL_PATA40;
  58. }
  59. /**
  60. * pacpi_error_handler - Setup and error handler
  61. * @ap: Port to handle
  62. *
  63. * LOCKING:
  64. * None (inherited from caller).
  65. */
  66. static void pacpi_error_handler(struct ata_port *ap)
  67. {
  68. return ata_bmdma_drive_eh(ap, pacpi_pre_reset, ata_std_softreset,
  69. NULL, ata_std_postreset);
  70. }
  71. /**
  72. * pacpi_discover_modes - filter non ACPI modes
  73. * @adev: ATA device
  74. * @mask: proposed modes
  75. *
  76. * Try the modes available and see which ones the ACPI method will
  77. * set up sensibly. From this we get a mask of ACPI modes we can use
  78. */
  79. static unsigned long pacpi_discover_modes(struct ata_port *ap, struct ata_device *adev)
  80. {
  81. struct pata_acpi *acpi = ap->private_data;
  82. struct ata_acpi_gtm probe;
  83. unsigned int xfer_mask;
  84. probe = acpi->gtm;
  85. ata_acpi_gtm(ap, &probe);
  86. xfer_mask = ata_acpi_gtm_xfermask(adev, &probe);
  87. if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
  88. ap->cbl = ATA_CBL_PATA80;
  89. return xfer_mask;
  90. }
  91. /**
  92. * pacpi_mode_filter - mode filter for ACPI
  93. * @adev: device
  94. * @mask: mask of valid modes
  95. *
  96. * Filter the valid mode list according to our own specific rules, in
  97. * this case the list of discovered valid modes obtained by ACPI probing
  98. */
  99. static unsigned long pacpi_mode_filter(struct ata_device *adev, unsigned long mask)
  100. {
  101. struct pata_acpi *acpi = adev->link->ap->private_data;
  102. return ata_pci_default_filter(adev, mask & acpi->mask[adev->devno]);
  103. }
  104. /**
  105. * pacpi_set_piomode - set initial PIO mode data
  106. * @ap: ATA interface
  107. * @adev: ATA device
  108. */
  109. static void pacpi_set_piomode(struct ata_port *ap, struct ata_device *adev)
  110. {
  111. int unit = adev->devno;
  112. struct pata_acpi *acpi = ap->private_data;
  113. const struct ata_timing *t;
  114. if (!(acpi->gtm.flags & 0x10))
  115. unit = 0;
  116. /* Now stuff the nS values into the structure */
  117. t = ata_timing_find_mode(adev->pio_mode);
  118. acpi->gtm.drive[unit].pio = t->cycle;
  119. ata_acpi_stm(ap, &acpi->gtm);
  120. /* See what mode we actually got */
  121. ata_acpi_gtm(ap, &acpi->gtm);
  122. }
  123. /**
  124. * pacpi_set_dmamode - set initial DMA mode data
  125. * @ap: ATA interface
  126. * @adev: ATA device
  127. */
  128. static void pacpi_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  129. {
  130. int unit = adev->devno;
  131. struct pata_acpi *acpi = ap->private_data;
  132. const struct ata_timing *t;
  133. if (!(acpi->gtm.flags & 0x10))
  134. unit = 0;
  135. /* Now stuff the nS values into the structure */
  136. t = ata_timing_find_mode(adev->dma_mode);
  137. if (adev->dma_mode >= XFER_UDMA_0) {
  138. acpi->gtm.drive[unit].dma = t->udma;
  139. acpi->gtm.flags |= (1 << (2 * unit));
  140. } else {
  141. acpi->gtm.drive[unit].dma = t->cycle;
  142. acpi->gtm.flags &= ~(1 << (2 * unit));
  143. }
  144. ata_acpi_stm(ap, &acpi->gtm);
  145. /* See what mode we actually got */
  146. ata_acpi_gtm(ap, &acpi->gtm);
  147. }
  148. /**
  149. * pacpi_qc_issue_prot - command issue
  150. * @qc: command pending
  151. *
  152. * Called when the libata layer is about to issue a command. We wrap
  153. * this interface so that we can load the correct ATA timings if
  154. * neccessary.
  155. */
  156. static unsigned int pacpi_qc_issue_prot(struct ata_queued_cmd *qc)
  157. {
  158. struct ata_port *ap = qc->ap;
  159. struct ata_device *adev = qc->dev;
  160. struct pata_acpi *acpi = ap->private_data;
  161. if (acpi->gtm.flags & 0x10)
  162. return ata_qc_issue_prot(qc);
  163. if (adev != acpi->last) {
  164. pacpi_set_piomode(ap, adev);
  165. if (adev->dma_mode)
  166. pacpi_set_dmamode(ap, adev);
  167. acpi->last = adev;
  168. }
  169. return ata_qc_issue_prot(qc);
  170. }
  171. /**
  172. * pacpi_port_start - port setup
  173. * @ap: ATA port being set up
  174. *
  175. * Use the port_start hook to maintain private control structures
  176. */
  177. static int pacpi_port_start(struct ata_port *ap)
  178. {
  179. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  180. struct pata_acpi *acpi;
  181. int ret;
  182. if (ap->acpi_handle == NULL)
  183. return -ENODEV;
  184. acpi = ap->private_data = devm_kzalloc(&pdev->dev, sizeof(struct pata_acpi), GFP_KERNEL);
  185. if (ap->private_data == NULL)
  186. return -ENOMEM;
  187. acpi->mask[0] = pacpi_discover_modes(ap, &ap->link.device[0]);
  188. acpi->mask[1] = pacpi_discover_modes(ap, &ap->link.device[1]);
  189. ret = ata_sff_port_start(ap);
  190. if (ret < 0)
  191. return ret;
  192. return ret;
  193. }
  194. static struct scsi_host_template pacpi_sht = {
  195. .module = THIS_MODULE,
  196. .name = DRV_NAME,
  197. .ioctl = ata_scsi_ioctl,
  198. .queuecommand = ata_scsi_queuecmd,
  199. .can_queue = ATA_DEF_QUEUE,
  200. .this_id = ATA_SHT_THIS_ID,
  201. .sg_tablesize = LIBATA_MAX_PRD,
  202. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  203. .emulated = ATA_SHT_EMULATED,
  204. .use_clustering = ATA_SHT_USE_CLUSTERING,
  205. .proc_name = DRV_NAME,
  206. .dma_boundary = ATA_DMA_BOUNDARY,
  207. .slave_configure = ata_scsi_slave_config,
  208. .slave_destroy = ata_scsi_slave_destroy,
  209. /* Use standard CHS mapping rules */
  210. .bios_param = ata_std_bios_param,
  211. };
  212. static const struct ata_port_operations pacpi_ops = {
  213. .set_piomode = pacpi_set_piomode,
  214. .set_dmamode = pacpi_set_dmamode,
  215. .mode_filter = pacpi_mode_filter,
  216. /* Task file is PCI ATA format, use helpers */
  217. .tf_load = ata_tf_load,
  218. .tf_read = ata_tf_read,
  219. .check_status = ata_check_status,
  220. .exec_command = ata_exec_command,
  221. .dev_select = ata_std_dev_select,
  222. .freeze = ata_bmdma_freeze,
  223. .thaw = ata_bmdma_thaw,
  224. .error_handler = pacpi_error_handler,
  225. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  226. .cable_detect = pacpi_cable_detect,
  227. /* BMDMA handling is PCI ATA format, use helpers */
  228. .bmdma_setup = ata_bmdma_setup,
  229. .bmdma_start = ata_bmdma_start,
  230. .bmdma_stop = ata_bmdma_stop,
  231. .bmdma_status = ata_bmdma_status,
  232. .qc_prep = ata_qc_prep,
  233. .qc_issue = pacpi_qc_issue_prot,
  234. .data_xfer = ata_data_xfer,
  235. /* Timeout handling */
  236. .irq_handler = ata_interrupt,
  237. .irq_clear = ata_bmdma_irq_clear,
  238. .irq_on = ata_irq_on,
  239. /* Generic PATA PCI ATA helpers */
  240. .port_start = pacpi_port_start,
  241. };
  242. /**
  243. * pacpi_init_one - Register ACPI ATA PCI device with kernel services
  244. * @pdev: PCI device to register
  245. * @ent: Entry in pacpi_pci_tbl matching with @pdev
  246. *
  247. * Called from kernel PCI layer.
  248. *
  249. * LOCKING:
  250. * Inherited from PCI layer (may sleep).
  251. *
  252. * RETURNS:
  253. * Zero on success, or -ERRNO value.
  254. */
  255. static int pacpi_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
  256. {
  257. static const struct ata_port_info info = {
  258. .sht = &pacpi_sht,
  259. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  260. .pio_mask = 0x1f,
  261. .mwdma_mask = 0x07,
  262. .udma_mask = 0x7f,
  263. .port_ops = &pacpi_ops,
  264. };
  265. const struct ata_port_info *ppi[] = { &info, NULL };
  266. return ata_pci_init_one(pdev, ppi);
  267. }
  268. static const struct pci_device_id pacpi_pci_tbl[] = {
  269. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL, 1},
  270. { } /* terminate list */
  271. };
  272. static struct pci_driver pacpi_pci_driver = {
  273. .name = DRV_NAME,
  274. .id_table = pacpi_pci_tbl,
  275. .probe = pacpi_init_one,
  276. .remove = ata_pci_remove_one,
  277. #ifdef CONFIG_PM
  278. .suspend = ata_pci_device_suspend,
  279. .resume = ata_pci_device_resume,
  280. #endif
  281. };
  282. static int __init pacpi_init(void)
  283. {
  284. return pci_register_driver(&pacpi_pci_driver);
  285. }
  286. static void __exit pacpi_exit(void)
  287. {
  288. pci_unregister_driver(&pacpi_pci_driver);
  289. }
  290. module_init(pacpi_init);
  291. module_exit(pacpi_exit);
  292. MODULE_AUTHOR("Alan Cox");
  293. MODULE_DESCRIPTION("SCSI low-level driver for ATA in ACPI mode");
  294. MODULE_LICENSE("GPL");
  295. MODULE_DEVICE_TABLE(pci, pacpi_pci_tbl);
  296. MODULE_VERSION(DRV_VERSION);