ide-dma-sff.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #include <linux/types.h>
  2. #include <linux/kernel.h>
  3. #include <linux/ide.h>
  4. #include <linux/scatterlist.h>
  5. #include <linux/dma-mapping.h>
  6. #include <linux/io.h>
  7. /**
  8. * config_drive_for_dma - attempt to activate IDE DMA
  9. * @drive: the drive to place in DMA mode
  10. *
  11. * If the drive supports at least mode 2 DMA or UDMA of any kind
  12. * then attempt to place it into DMA mode. Drives that are known to
  13. * support DMA but predate the DMA properties or that are known
  14. * to have DMA handling bugs are also set up appropriately based
  15. * on the good/bad drive lists.
  16. */
  17. int config_drive_for_dma(ide_drive_t *drive)
  18. {
  19. ide_hwif_t *hwif = drive->hwif;
  20. u16 *id = drive->id;
  21. if (drive->media != ide_disk) {
  22. if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
  23. return 0;
  24. }
  25. /*
  26. * Enable DMA on any drive that has
  27. * UltraDMA (mode 0/1/2/3/4/5/6) enabled
  28. */
  29. if ((id[ATA_ID_FIELD_VALID] & 4) &&
  30. ((id[ATA_ID_UDMA_MODES] >> 8) & 0x7f))
  31. return 1;
  32. /*
  33. * Enable DMA on any drive that has mode2 DMA
  34. * (multi or single) enabled
  35. */
  36. if (id[ATA_ID_FIELD_VALID] & 2) /* regular DMA */
  37. if ((id[ATA_ID_MWDMA_MODES] & 0x404) == 0x404 ||
  38. (id[ATA_ID_SWDMA_MODES] & 0x404) == 0x404)
  39. return 1;
  40. /* Consult the list of known "good" drives */
  41. if (ide_dma_good_drive(drive))
  42. return 1;
  43. return 0;
  44. }
  45. u8 ide_dma_sff_read_status(ide_hwif_t *hwif)
  46. {
  47. unsigned long addr = hwif->dma_base + ATA_DMA_STATUS;
  48. if (hwif->host_flags & IDE_HFLAG_MMIO)
  49. return readb((void __iomem *)addr);
  50. else
  51. return inb(addr);
  52. }
  53. EXPORT_SYMBOL_GPL(ide_dma_sff_read_status);
  54. static void ide_dma_sff_write_status(ide_hwif_t *hwif, u8 val)
  55. {
  56. unsigned long addr = hwif->dma_base + ATA_DMA_STATUS;
  57. if (hwif->host_flags & IDE_HFLAG_MMIO)
  58. writeb(val, (void __iomem *)addr);
  59. else
  60. outb(val, addr);
  61. }
  62. /**
  63. * ide_dma_host_set - Enable/disable DMA on a host
  64. * @drive: drive to control
  65. *
  66. * Enable/disable DMA on an IDE controller following generic
  67. * bus-mastering IDE controller behaviour.
  68. */
  69. void ide_dma_host_set(ide_drive_t *drive, int on)
  70. {
  71. ide_hwif_t *hwif = drive->hwif;
  72. u8 unit = drive->dn & 1;
  73. u8 dma_stat = hwif->dma_ops->dma_sff_read_status(hwif);
  74. if (on)
  75. dma_stat |= (1 << (5 + unit));
  76. else
  77. dma_stat &= ~(1 << (5 + unit));
  78. ide_dma_sff_write_status(hwif, dma_stat);
  79. }
  80. EXPORT_SYMBOL_GPL(ide_dma_host_set);
  81. /**
  82. * ide_build_dmatable - build IDE DMA table
  83. *
  84. * ide_build_dmatable() prepares a dma request. We map the command
  85. * to get the pci bus addresses of the buffers and then build up
  86. * the PRD table that the IDE layer wants to be fed.
  87. *
  88. * Most chipsets correctly interpret a length of 0x0000 as 64KB,
  89. * but at least one (e.g. CS5530) misinterprets it as zero (!).
  90. * So we break the 64KB entry into two 32KB entries instead.
  91. *
  92. * Returns the number of built PRD entries if all went okay,
  93. * returns 0 otherwise.
  94. *
  95. * May also be invoked from trm290.c
  96. */
  97. int ide_build_dmatable(ide_drive_t *drive, struct request *rq)
  98. {
  99. ide_hwif_t *hwif = drive->hwif;
  100. __le32 *table = (__le32 *)hwif->dmatable_cpu;
  101. unsigned int count = 0;
  102. int i;
  103. struct scatterlist *sg;
  104. u8 is_trm290 = !!(hwif->host_flags & IDE_HFLAG_TRM290);
  105. hwif->sg_nents = ide_build_sglist(drive, rq);
  106. if (hwif->sg_nents == 0)
  107. return 0;
  108. for_each_sg(hwif->sg_table, sg, hwif->sg_nents, i) {
  109. u32 cur_addr, cur_len, xcount, bcount;
  110. cur_addr = sg_dma_address(sg);
  111. cur_len = sg_dma_len(sg);
  112. /*
  113. * Fill in the dma table, without crossing any 64kB boundaries.
  114. * Most hardware requires 16-bit alignment of all blocks,
  115. * but the trm290 requires 32-bit alignment.
  116. */
  117. while (cur_len) {
  118. if (count++ >= PRD_ENTRIES)
  119. goto use_pio_instead;
  120. bcount = 0x10000 - (cur_addr & 0xffff);
  121. if (bcount > cur_len)
  122. bcount = cur_len;
  123. *table++ = cpu_to_le32(cur_addr);
  124. xcount = bcount & 0xffff;
  125. if (is_trm290)
  126. xcount = ((xcount >> 2) - 1) << 16;
  127. else if (xcount == 0x0000) {
  128. if (count++ >= PRD_ENTRIES)
  129. goto use_pio_instead;
  130. *table++ = cpu_to_le32(0x8000);
  131. *table++ = cpu_to_le32(cur_addr + 0x8000);
  132. xcount = 0x8000;
  133. }
  134. *table++ = cpu_to_le32(xcount);
  135. cur_addr += bcount;
  136. cur_len -= bcount;
  137. }
  138. }
  139. if (count) {
  140. if (!is_trm290)
  141. *--table |= cpu_to_le32(0x80000000);
  142. return count;
  143. }
  144. use_pio_instead:
  145. printk(KERN_ERR "%s: %s\n", drive->name,
  146. count ? "DMA table too small" : "empty DMA table?");
  147. ide_destroy_dmatable(drive);
  148. return 0; /* revert to PIO for this request */
  149. }
  150. EXPORT_SYMBOL_GPL(ide_build_dmatable);
  151. /**
  152. * ide_dma_setup - begin a DMA phase
  153. * @drive: target device
  154. *
  155. * Build an IDE DMA PRD (IDE speak for scatter gather table)
  156. * and then set up the DMA transfer registers for a device
  157. * that follows generic IDE PCI DMA behaviour. Controllers can
  158. * override this function if they need to
  159. *
  160. * Returns 0 on success. If a PIO fallback is required then 1
  161. * is returned.
  162. */
  163. int ide_dma_setup(ide_drive_t *drive)
  164. {
  165. ide_hwif_t *hwif = drive->hwif;
  166. struct request *rq = hwif->rq;
  167. unsigned int reading = rq_data_dir(rq) ? 0 : ATA_DMA_WR;
  168. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  169. u8 dma_stat;
  170. /* fall back to pio! */
  171. if (!ide_build_dmatable(drive, rq)) {
  172. ide_map_sg(drive, rq);
  173. return 1;
  174. }
  175. /* PRD table */
  176. if (mmio)
  177. writel(hwif->dmatable_dma,
  178. (void __iomem *)(hwif->dma_base + ATA_DMA_TABLE_OFS));
  179. else
  180. outl(hwif->dmatable_dma, hwif->dma_base + ATA_DMA_TABLE_OFS);
  181. /* specify r/w */
  182. if (mmio)
  183. writeb(reading, (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  184. else
  185. outb(reading, hwif->dma_base + ATA_DMA_CMD);
  186. /* read DMA status for INTR & ERROR flags */
  187. dma_stat = hwif->dma_ops->dma_sff_read_status(hwif);
  188. /* clear INTR & ERROR flags */
  189. ide_dma_sff_write_status(hwif, dma_stat | ATA_DMA_ERR | ATA_DMA_INTR);
  190. drive->waiting_for_dma = 1;
  191. return 0;
  192. }
  193. EXPORT_SYMBOL_GPL(ide_dma_setup);
  194. /**
  195. * dma_timer_expiry - handle a DMA timeout
  196. * @drive: Drive that timed out
  197. *
  198. * An IDE DMA transfer timed out. In the event of an error we ask
  199. * the driver to resolve the problem, if a DMA transfer is still
  200. * in progress we continue to wait (arguably we need to add a
  201. * secondary 'I don't care what the drive thinks' timeout here)
  202. * Finally if we have an interrupt we let it complete the I/O.
  203. * But only one time - we clear expiry and if it's still not
  204. * completed after WAIT_CMD, we error and retry in PIO.
  205. * This can occur if an interrupt is lost or due to hang or bugs.
  206. */
  207. static int dma_timer_expiry(ide_drive_t *drive)
  208. {
  209. ide_hwif_t *hwif = drive->hwif;
  210. u8 dma_stat = hwif->dma_ops->dma_sff_read_status(hwif);
  211. printk(KERN_WARNING "%s: %s: DMA status (0x%02x)\n",
  212. drive->name, __func__, dma_stat);
  213. if ((dma_stat & 0x18) == 0x18) /* BUSY Stupid Early Timer !! */
  214. return WAIT_CMD;
  215. hwif->expiry = NULL; /* one free ride for now */
  216. if (dma_stat & ATA_DMA_ERR) /* ERROR */
  217. return -1;
  218. if (dma_stat & ATA_DMA_ACTIVE) /* DMAing */
  219. return WAIT_CMD;
  220. if (dma_stat & ATA_DMA_INTR) /* Got an Interrupt */
  221. return WAIT_CMD;
  222. return 0; /* Status is unknown -- reset the bus */
  223. }
  224. void ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
  225. {
  226. /* issue cmd to drive */
  227. ide_execute_command(drive, command, &ide_dma_intr, 2 * WAIT_CMD,
  228. dma_timer_expiry);
  229. }
  230. EXPORT_SYMBOL_GPL(ide_dma_exec_cmd);
  231. void ide_dma_start(ide_drive_t *drive)
  232. {
  233. ide_hwif_t *hwif = drive->hwif;
  234. u8 dma_cmd;
  235. /* Note that this is done *after* the cmd has
  236. * been issued to the drive, as per the BM-IDE spec.
  237. * The Promise Ultra33 doesn't work correctly when
  238. * we do this part before issuing the drive cmd.
  239. */
  240. if (hwif->host_flags & IDE_HFLAG_MMIO) {
  241. dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  242. writeb(dma_cmd | ATA_DMA_START,
  243. (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  244. } else {
  245. dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
  246. outb(dma_cmd | ATA_DMA_START, hwif->dma_base + ATA_DMA_CMD);
  247. }
  248. wmb();
  249. }
  250. EXPORT_SYMBOL_GPL(ide_dma_start);
  251. /* returns 1 on error, 0 otherwise */
  252. int ide_dma_end(ide_drive_t *drive)
  253. {
  254. ide_hwif_t *hwif = drive->hwif;
  255. u8 dma_stat = 0, dma_cmd = 0, mask;
  256. drive->waiting_for_dma = 0;
  257. /* stop DMA */
  258. if (hwif->host_flags & IDE_HFLAG_MMIO) {
  259. dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  260. writeb(dma_cmd & ~ATA_DMA_START,
  261. (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  262. } else {
  263. dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
  264. outb(dma_cmd & ~ATA_DMA_START, hwif->dma_base + ATA_DMA_CMD);
  265. }
  266. /* get DMA status */
  267. dma_stat = hwif->dma_ops->dma_sff_read_status(hwif);
  268. /* clear INTR & ERROR bits */
  269. ide_dma_sff_write_status(hwif, dma_stat | ATA_DMA_ERR | ATA_DMA_INTR);
  270. /* purge DMA mappings */
  271. ide_destroy_dmatable(drive);
  272. wmb();
  273. /* verify good DMA status */
  274. mask = ATA_DMA_ACTIVE | ATA_DMA_ERR | ATA_DMA_INTR;
  275. if ((dma_stat & mask) != ATA_DMA_INTR)
  276. return 0x10 | dma_stat;
  277. return 0;
  278. }
  279. EXPORT_SYMBOL_GPL(ide_dma_end);
  280. /* returns 1 if dma irq issued, 0 otherwise */
  281. int ide_dma_test_irq(ide_drive_t *drive)
  282. {
  283. ide_hwif_t *hwif = drive->hwif;
  284. u8 dma_stat = hwif->dma_ops->dma_sff_read_status(hwif);
  285. return (dma_stat & ATA_DMA_INTR) ? 1 : 0;
  286. }
  287. EXPORT_SYMBOL_GPL(ide_dma_test_irq);
  288. const struct ide_dma_ops sff_dma_ops = {
  289. .dma_host_set = ide_dma_host_set,
  290. .dma_setup = ide_dma_setup,
  291. .dma_exec_cmd = ide_dma_exec_cmd,
  292. .dma_start = ide_dma_start,
  293. .dma_end = ide_dma_end,
  294. .dma_test_irq = ide_dma_test_irq,
  295. .dma_timeout = ide_dma_timeout,
  296. .dma_lost_irq = ide_dma_lost_irq,
  297. .dma_sff_read_status = ide_dma_sff_read_status,
  298. };
  299. EXPORT_SYMBOL_GPL(sff_dma_ops);