pata_acpi.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. /* Welcome to ACPI, bring a bucket */
  72. static const unsigned int pio_cycle[7] = {
  73. 600, 383, 240, 180, 120, 100, 80
  74. };
  75. static const unsigned int mwdma_cycle[5] = {
  76. 480, 150, 120, 100, 80
  77. };
  78. static const unsigned int udma_cycle[7] = {
  79. 120, 80, 60, 45, 30, 20, 15
  80. };
  81. /**
  82. * pacpi_discover_modes - filter non ACPI modes
  83. * @adev: ATA device
  84. * @mask: proposed modes
  85. *
  86. * Try the modes available and see which ones the ACPI method will
  87. * set up sensibly. From this we get a mask of ACPI modes we can use
  88. */
  89. static unsigned long pacpi_discover_modes(struct ata_port *ap, struct ata_device *adev)
  90. {
  91. int unit = adev->devno;
  92. struct pata_acpi *acpi = ap->private_data;
  93. int i;
  94. u32 t;
  95. unsigned long mask = (0x7f << ATA_SHIFT_UDMA) | (0x7 << ATA_SHIFT_MWDMA) | (0x1F << ATA_SHIFT_PIO);
  96. struct ata_acpi_gtm probe;
  97. probe = acpi->gtm;
  98. /* We always use the 0 slot for crap hardware */
  99. if (!(probe.flags & 0x10))
  100. unit = 0;
  101. ata_acpi_gtm(ap, &probe);
  102. /* Start by scanning for PIO modes */
  103. for (i = 0; i < 7; i++) {
  104. t = probe.drive[unit].pio;
  105. if (t <= pio_cycle[i]) {
  106. mask |= (2 << (ATA_SHIFT_PIO + i)) - 1;
  107. break;
  108. }
  109. }
  110. /* See if we have MWDMA or UDMA data. We don't bother with MWDMA
  111. if UDMA is availabe as this means the BIOS set UDMA and our
  112. error changedown if it works is UDMA to PIO anyway */
  113. if (probe.flags & (1 << (2 * unit))) {
  114. /* MWDMA */
  115. for (i = 0; i < 5; i++) {
  116. t = probe.drive[unit].dma;
  117. if (t <= mwdma_cycle[i]) {
  118. mask |= (2 << (ATA_SHIFT_MWDMA + i)) - 1;
  119. break;
  120. }
  121. }
  122. } else {
  123. /* UDMA */
  124. for (i = 0; i < 7; i++) {
  125. t = probe.drive[unit].dma;
  126. if (t <= udma_cycle[i]) {
  127. mask |= (2 << (ATA_SHIFT_UDMA + i)) - 1;
  128. break;
  129. }
  130. }
  131. }
  132. if (mask & (0xF8 << ATA_SHIFT_UDMA))
  133. ap->cbl = ATA_CBL_PATA80;
  134. return mask;
  135. }
  136. /**
  137. * pacpi_mode_filter - mode filter for ACPI
  138. * @adev: device
  139. * @mask: mask of valid modes
  140. *
  141. * Filter the valid mode list according to our own specific rules, in
  142. * this case the list of discovered valid modes obtained by ACPI probing
  143. */
  144. static unsigned long pacpi_mode_filter(struct ata_device *adev, unsigned long mask)
  145. {
  146. struct pata_acpi *acpi = adev->link->ap->private_data;
  147. return ata_pci_default_filter(adev, mask & acpi->mask[adev->devno]);
  148. }
  149. /**
  150. * pacpi_set_piomode - set initial PIO mode data
  151. * @ap: ATA interface
  152. * @adev: ATA device
  153. */
  154. static void pacpi_set_piomode(struct ata_port *ap, struct ata_device *adev)
  155. {
  156. int unit = adev->devno;
  157. struct pata_acpi *acpi = ap->private_data;
  158. if(!(acpi->gtm.flags & 0x10))
  159. unit = 0;
  160. /* Now stuff the nS values into the structure */
  161. acpi->gtm.drive[unit].pio = pio_cycle[adev->pio_mode - XFER_PIO_0];
  162. ata_acpi_stm(ap, &acpi->gtm);
  163. /* See what mode we actually got */
  164. ata_acpi_gtm(ap, &acpi->gtm);
  165. }
  166. /**
  167. * pacpi_set_dmamode - set initial DMA mode data
  168. * @ap: ATA interface
  169. * @adev: ATA device
  170. */
  171. static void pacpi_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  172. {
  173. int unit = adev->devno;
  174. struct pata_acpi *acpi = ap->private_data;
  175. if(!(acpi->gtm.flags & 0x10))
  176. unit = 0;
  177. /* Now stuff the nS values into the structure */
  178. if (adev->dma_mode >= XFER_UDMA_0) {
  179. acpi->gtm.drive[unit].dma = udma_cycle[adev->dma_mode - XFER_UDMA_0];
  180. acpi->gtm.flags |= (1 << (2 * unit));
  181. } else {
  182. acpi->gtm.drive[unit].dma = mwdma_cycle[adev->dma_mode - XFER_MW_DMA_0];
  183. acpi->gtm.flags &= ~(1 << (2 * unit));
  184. }
  185. ata_acpi_stm(ap, &acpi->gtm);
  186. /* See what mode we actually got */
  187. ata_acpi_gtm(ap, &acpi->gtm);
  188. }
  189. /**
  190. * pacpi_qc_issue_prot - command issue
  191. * @qc: command pending
  192. *
  193. * Called when the libata layer is about to issue a command. We wrap
  194. * this interface so that we can load the correct ATA timings if
  195. * neccessary.
  196. */
  197. static unsigned int pacpi_qc_issue_prot(struct ata_queued_cmd *qc)
  198. {
  199. struct ata_port *ap = qc->ap;
  200. struct ata_device *adev = qc->dev;
  201. struct pata_acpi *acpi = ap->private_data;
  202. if (acpi->gtm.flags & 0x10)
  203. return ata_qc_issue_prot(qc);
  204. if (adev != acpi->last) {
  205. pacpi_set_piomode(ap, adev);
  206. if (adev->dma_mode)
  207. pacpi_set_dmamode(ap, adev);
  208. acpi->last = adev;
  209. }
  210. return ata_qc_issue_prot(qc);
  211. }
  212. /**
  213. * pacpi_port_start - port setup
  214. * @ap: ATA port being set up
  215. *
  216. * Use the port_start hook to maintain private control structures
  217. */
  218. static int pacpi_port_start(struct ata_port *ap)
  219. {
  220. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  221. struct pata_acpi *acpi;
  222. int ret;
  223. if (ap->acpi_handle == NULL)
  224. return -ENODEV;
  225. acpi = ap->private_data = devm_kzalloc(&pdev->dev, sizeof(struct pata_acpi), GFP_KERNEL);
  226. if (ap->private_data == NULL)
  227. return -ENOMEM;
  228. acpi->mask[0] = pacpi_discover_modes(ap, &ap->link.device[0]);
  229. acpi->mask[1] = pacpi_discover_modes(ap, &ap->link.device[1]);
  230. ret = ata_sff_port_start(ap);
  231. if (ret < 0)
  232. return ret;
  233. return ret;
  234. }
  235. static struct scsi_host_template pacpi_sht = {
  236. .module = THIS_MODULE,
  237. .name = DRV_NAME,
  238. .ioctl = ata_scsi_ioctl,
  239. .queuecommand = ata_scsi_queuecmd,
  240. .can_queue = ATA_DEF_QUEUE,
  241. .this_id = ATA_SHT_THIS_ID,
  242. .sg_tablesize = LIBATA_MAX_PRD,
  243. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  244. .emulated = ATA_SHT_EMULATED,
  245. .use_clustering = ATA_SHT_USE_CLUSTERING,
  246. .proc_name = DRV_NAME,
  247. .dma_boundary = ATA_DMA_BOUNDARY,
  248. .slave_configure = ata_scsi_slave_config,
  249. .slave_destroy = ata_scsi_slave_destroy,
  250. /* Use standard CHS mapping rules */
  251. .bios_param = ata_std_bios_param,
  252. };
  253. static const struct ata_port_operations pacpi_ops = {
  254. .set_piomode = pacpi_set_piomode,
  255. .set_dmamode = pacpi_set_dmamode,
  256. .mode_filter = pacpi_mode_filter,
  257. /* Task file is PCI ATA format, use helpers */
  258. .tf_load = ata_tf_load,
  259. .tf_read = ata_tf_read,
  260. .check_status = ata_check_status,
  261. .exec_command = ata_exec_command,
  262. .dev_select = ata_std_dev_select,
  263. .freeze = ata_bmdma_freeze,
  264. .thaw = ata_bmdma_thaw,
  265. .error_handler = pacpi_error_handler,
  266. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  267. .cable_detect = pacpi_cable_detect,
  268. /* BMDMA handling is PCI ATA format, use helpers */
  269. .bmdma_setup = ata_bmdma_setup,
  270. .bmdma_start = ata_bmdma_start,
  271. .bmdma_stop = ata_bmdma_stop,
  272. .bmdma_status = ata_bmdma_status,
  273. .qc_prep = ata_qc_prep,
  274. .qc_issue = pacpi_qc_issue_prot,
  275. .data_xfer = ata_data_xfer,
  276. /* Timeout handling */
  277. .irq_handler = ata_interrupt,
  278. .irq_clear = ata_bmdma_irq_clear,
  279. .irq_on = ata_irq_on,
  280. /* Generic PATA PCI ATA helpers */
  281. .port_start = pacpi_port_start,
  282. };
  283. /**
  284. * pacpi_init_one - Register ACPI ATA PCI device with kernel services
  285. * @pdev: PCI device to register
  286. * @ent: Entry in pacpi_pci_tbl matching with @pdev
  287. *
  288. * Called from kernel PCI layer.
  289. *
  290. * LOCKING:
  291. * Inherited from PCI layer (may sleep).
  292. *
  293. * RETURNS:
  294. * Zero on success, or -ERRNO value.
  295. */
  296. static int pacpi_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
  297. {
  298. static const struct ata_port_info info = {
  299. .sht = &pacpi_sht,
  300. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  301. .pio_mask = 0x1f,
  302. .mwdma_mask = 0x07,
  303. .udma_mask = 0x7f,
  304. .port_ops = &pacpi_ops,
  305. };
  306. const struct ata_port_info *ppi[] = { &info, NULL };
  307. return ata_pci_init_one(pdev, ppi);
  308. }
  309. static const struct pci_device_id pacpi_pci_tbl[] = {
  310. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL, 1},
  311. { } /* terminate list */
  312. };
  313. static struct pci_driver pacpi_pci_driver = {
  314. .name = DRV_NAME,
  315. .id_table = pacpi_pci_tbl,
  316. .probe = pacpi_init_one,
  317. .remove = ata_pci_remove_one,
  318. #ifdef CONFIG_PM
  319. .suspend = ata_pci_device_suspend,
  320. .resume = ata_pci_device_resume,
  321. #endif
  322. };
  323. static int __init pacpi_init(void)
  324. {
  325. return pci_register_driver(&pacpi_pci_driver);
  326. }
  327. static void __exit pacpi_exit(void)
  328. {
  329. pci_unregister_driver(&pacpi_pci_driver);
  330. }
  331. module_init(pacpi_init);
  332. module_exit(pacpi_exit);
  333. MODULE_AUTHOR("Alan Cox");
  334. MODULE_DESCRIPTION("SCSI low-level driver for ATA in ACPI mode");
  335. MODULE_LICENSE("GPL");
  336. MODULE_DEVICE_TABLE(pci, pacpi_pci_tbl);
  337. MODULE_VERSION(DRV_VERSION);