ide-dma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * IDE DMA support (including IDE PCI BM-DMA).
  3. *
  4. * Copyright (C) 1995-1998 Mark Lord
  5. * Copyright (C) 1999-2000 Andre Hedrick <andre@linux-ide.org>
  6. * Copyright (C) 2004, 2007 Bartlomiej Zolnierkiewicz
  7. *
  8. * May be copied or modified under the terms of the GNU General Public License
  9. *
  10. * DMA is supported for all IDE devices (disk drives, cdroms, tapes, floppies).
  11. */
  12. /*
  13. * Special Thanks to Mark for his Six years of work.
  14. */
  15. /*
  16. * Thanks to "Christopher J. Reimer" <reimer@doe.carleton.ca> for
  17. * fixing the problem with the BIOS on some Acer motherboards.
  18. *
  19. * Thanks to "Benoit Poulot-Cazajous" <poulot@chorus.fr> for testing
  20. * "TX" chipset compatibility and for providing patches for the "TX" chipset.
  21. *
  22. * Thanks to Christian Brunner <chb@muc.de> for taking a good first crack
  23. * at generic DMA -- his patches were referred to when preparing this code.
  24. *
  25. * Most importantly, thanks to Robert Bringman <rob@mars.trion.com>
  26. * for supplying a Promise UDMA board & WD UDMA drive for this work!
  27. */
  28. #include <linux/types.h>
  29. #include <linux/kernel.h>
  30. #include <linux/ide.h>
  31. #include <linux/scatterlist.h>
  32. #include <linux/dma-mapping.h>
  33. static const struct drive_list_entry drive_whitelist[] = {
  34. { "Micropolis 2112A" , NULL },
  35. { "CONNER CTMA 4000" , NULL },
  36. { "CONNER CTT8000-A" , NULL },
  37. { "ST34342A" , NULL },
  38. { NULL , NULL }
  39. };
  40. static const struct drive_list_entry drive_blacklist[] = {
  41. { "WDC AC11000H" , NULL },
  42. { "WDC AC22100H" , NULL },
  43. { "WDC AC32500H" , NULL },
  44. { "WDC AC33100H" , NULL },
  45. { "WDC AC31600H" , NULL },
  46. { "WDC AC32100H" , "24.09P07" },
  47. { "WDC AC23200L" , "21.10N21" },
  48. { "Compaq CRD-8241B" , NULL },
  49. { "CRD-8400B" , NULL },
  50. { "CRD-8480B", NULL },
  51. { "CRD-8482B", NULL },
  52. { "CRD-84" , NULL },
  53. { "SanDisk SDP3B" , NULL },
  54. { "SanDisk SDP3B-64" , NULL },
  55. { "SANYO CD-ROM CRD" , NULL },
  56. { "HITACHI CDR-8" , NULL },
  57. { "HITACHI CDR-8335" , NULL },
  58. { "HITACHI CDR-8435" , NULL },
  59. { "Toshiba CD-ROM XM-6202B" , NULL },
  60. { "TOSHIBA CD-ROM XM-1702BC", NULL },
  61. { "CD-532E-A" , NULL },
  62. { "E-IDE CD-ROM CR-840", NULL },
  63. { "CD-ROM Drive/F5A", NULL },
  64. { "WPI CDD-820", NULL },
  65. { "SAMSUNG CD-ROM SC-148C", NULL },
  66. { "SAMSUNG CD-ROM SC", NULL },
  67. { "ATAPI CD-ROM DRIVE 40X MAXIMUM", NULL },
  68. { "_NEC DV5800A", NULL },
  69. { "SAMSUNG CD-ROM SN-124", "N001" },
  70. { "Seagate STT20000A", NULL },
  71. { "CD-ROM CDR_U200", "1.09" },
  72. { NULL , NULL }
  73. };
  74. /**
  75. * ide_dma_intr - IDE DMA interrupt handler
  76. * @drive: the drive the interrupt is for
  77. *
  78. * Handle an interrupt completing a read/write DMA transfer on an
  79. * IDE device
  80. */
  81. ide_startstop_t ide_dma_intr(ide_drive_t *drive)
  82. {
  83. ide_hwif_t *hwif = drive->hwif;
  84. u8 stat = 0, dma_stat = 0;
  85. dma_stat = hwif->dma_ops->dma_end(drive);
  86. stat = hwif->tp_ops->read_status(hwif);
  87. if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat | ATA_DRQ)) {
  88. if (!dma_stat) {
  89. struct request *rq = hwif->rq;
  90. task_end_request(drive, rq, stat);
  91. return ide_stopped;
  92. }
  93. printk(KERN_ERR "%s: %s: bad DMA status (0x%02x)\n",
  94. drive->name, __func__, dma_stat);
  95. }
  96. return ide_error(drive, "dma_intr", stat);
  97. }
  98. EXPORT_SYMBOL_GPL(ide_dma_intr);
  99. int ide_dma_good_drive(ide_drive_t *drive)
  100. {
  101. return ide_in_drive_list(drive->id, drive_whitelist);
  102. }
  103. /**
  104. * ide_build_sglist - map IDE scatter gather for DMA I/O
  105. * @drive: the drive to build the DMA table for
  106. * @rq: the request holding the sg list
  107. *
  108. * Perform the DMA mapping magic necessary to access the source or
  109. * target buffers of a request via DMA. The lower layers of the
  110. * kernel provide the necessary cache management so that we can
  111. * operate in a portable fashion.
  112. */
  113. int ide_build_sglist(ide_drive_t *drive, struct request *rq)
  114. {
  115. ide_hwif_t *hwif = drive->hwif;
  116. struct scatterlist *sg = hwif->sg_table;
  117. int i;
  118. ide_map_sg(drive, rq);
  119. if (rq_data_dir(rq) == READ)
  120. hwif->sg_dma_direction = DMA_FROM_DEVICE;
  121. else
  122. hwif->sg_dma_direction = DMA_TO_DEVICE;
  123. i = dma_map_sg(hwif->dev, sg, hwif->sg_nents, hwif->sg_dma_direction);
  124. if (i) {
  125. hwif->orig_sg_nents = hwif->sg_nents;
  126. hwif->sg_nents = i;
  127. }
  128. return i;
  129. }
  130. EXPORT_SYMBOL_GPL(ide_build_sglist);
  131. /**
  132. * ide_destroy_dmatable - clean up DMA mapping
  133. * @drive: The drive to unmap
  134. *
  135. * Teardown mappings after DMA has completed. This must be called
  136. * after the completion of each use of ide_build_dmatable and before
  137. * the next use of ide_build_dmatable. Failure to do so will cause
  138. * an oops as only one mapping can be live for each target at a given
  139. * time.
  140. */
  141. void ide_destroy_dmatable(ide_drive_t *drive)
  142. {
  143. ide_hwif_t *hwif = drive->hwif;
  144. dma_unmap_sg(hwif->dev, hwif->sg_table, hwif->orig_sg_nents,
  145. hwif->sg_dma_direction);
  146. }
  147. EXPORT_SYMBOL_GPL(ide_destroy_dmatable);
  148. /**
  149. * ide_dma_off_quietly - Generic DMA kill
  150. * @drive: drive to control
  151. *
  152. * Turn off the current DMA on this IDE controller.
  153. */
  154. void ide_dma_off_quietly(ide_drive_t *drive)
  155. {
  156. drive->dev_flags &= ~IDE_DFLAG_USING_DMA;
  157. ide_toggle_bounce(drive, 0);
  158. drive->hwif->dma_ops->dma_host_set(drive, 0);
  159. }
  160. EXPORT_SYMBOL(ide_dma_off_quietly);
  161. /**
  162. * ide_dma_off - disable DMA on a device
  163. * @drive: drive to disable DMA on
  164. *
  165. * Disable IDE DMA for a device on this IDE controller.
  166. * Inform the user that DMA has been disabled.
  167. */
  168. void ide_dma_off(ide_drive_t *drive)
  169. {
  170. printk(KERN_INFO "%s: DMA disabled\n", drive->name);
  171. ide_dma_off_quietly(drive);
  172. }
  173. EXPORT_SYMBOL(ide_dma_off);
  174. /**
  175. * ide_dma_on - Enable DMA on a device
  176. * @drive: drive to enable DMA on
  177. *
  178. * Enable IDE DMA for a device on this IDE controller.
  179. */
  180. void ide_dma_on(ide_drive_t *drive)
  181. {
  182. drive->dev_flags |= IDE_DFLAG_USING_DMA;
  183. ide_toggle_bounce(drive, 1);
  184. drive->hwif->dma_ops->dma_host_set(drive, 1);
  185. }
  186. int __ide_dma_bad_drive(ide_drive_t *drive)
  187. {
  188. u16 *id = drive->id;
  189. int blacklist = ide_in_drive_list(id, drive_blacklist);
  190. if (blacklist) {
  191. printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n",
  192. drive->name, (char *)&id[ATA_ID_PROD]);
  193. return blacklist;
  194. }
  195. return 0;
  196. }
  197. EXPORT_SYMBOL(__ide_dma_bad_drive);
  198. static const u8 xfer_mode_bases[] = {
  199. XFER_UDMA_0,
  200. XFER_MW_DMA_0,
  201. XFER_SW_DMA_0,
  202. };
  203. static unsigned int ide_get_mode_mask(ide_drive_t *drive, u8 base, u8 req_mode)
  204. {
  205. u16 *id = drive->id;
  206. ide_hwif_t *hwif = drive->hwif;
  207. const struct ide_port_ops *port_ops = hwif->port_ops;
  208. unsigned int mask = 0;
  209. switch (base) {
  210. case XFER_UDMA_0:
  211. if ((id[ATA_ID_FIELD_VALID] & 4) == 0)
  212. break;
  213. if (port_ops && port_ops->udma_filter)
  214. mask = port_ops->udma_filter(drive);
  215. else
  216. mask = hwif->ultra_mask;
  217. mask &= id[ATA_ID_UDMA_MODES];
  218. /*
  219. * avoid false cable warning from eighty_ninty_three()
  220. */
  221. if (req_mode > XFER_UDMA_2) {
  222. if ((mask & 0x78) && (eighty_ninty_three(drive) == 0))
  223. mask &= 0x07;
  224. }
  225. break;
  226. case XFER_MW_DMA_0:
  227. if ((id[ATA_ID_FIELD_VALID] & 2) == 0)
  228. break;
  229. if (port_ops && port_ops->mdma_filter)
  230. mask = port_ops->mdma_filter(drive);
  231. else
  232. mask = hwif->mwdma_mask;
  233. mask &= id[ATA_ID_MWDMA_MODES];
  234. break;
  235. case XFER_SW_DMA_0:
  236. if (id[ATA_ID_FIELD_VALID] & 2) {
  237. mask = id[ATA_ID_SWDMA_MODES] & hwif->swdma_mask;
  238. } else if (id[ATA_ID_OLD_DMA_MODES] >> 8) {
  239. u8 mode = id[ATA_ID_OLD_DMA_MODES] >> 8;
  240. /*
  241. * if the mode is valid convert it to the mask
  242. * (the maximum allowed mode is XFER_SW_DMA_2)
  243. */
  244. if (mode <= 2)
  245. mask = ((2 << mode) - 1) & hwif->swdma_mask;
  246. }
  247. break;
  248. default:
  249. BUG();
  250. break;
  251. }
  252. return mask;
  253. }
  254. /**
  255. * ide_find_dma_mode - compute DMA speed
  256. * @drive: IDE device
  257. * @req_mode: requested mode
  258. *
  259. * Checks the drive/host capabilities and finds the speed to use for
  260. * the DMA transfer. The speed is then limited by the requested mode.
  261. *
  262. * Returns 0 if the drive/host combination is incapable of DMA transfers
  263. * or if the requested mode is not a DMA mode.
  264. */
  265. u8 ide_find_dma_mode(ide_drive_t *drive, u8 req_mode)
  266. {
  267. ide_hwif_t *hwif = drive->hwif;
  268. unsigned int mask;
  269. int x, i;
  270. u8 mode = 0;
  271. if (drive->media != ide_disk) {
  272. if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
  273. return 0;
  274. }
  275. for (i = 0; i < ARRAY_SIZE(xfer_mode_bases); i++) {
  276. if (req_mode < xfer_mode_bases[i])
  277. continue;
  278. mask = ide_get_mode_mask(drive, xfer_mode_bases[i], req_mode);
  279. x = fls(mask) - 1;
  280. if (x >= 0) {
  281. mode = xfer_mode_bases[i] + x;
  282. break;
  283. }
  284. }
  285. if (hwif->chipset == ide_acorn && mode == 0) {
  286. /*
  287. * is this correct?
  288. */
  289. if (ide_dma_good_drive(drive) &&
  290. drive->id[ATA_ID_EIDE_DMA_TIME] < 150)
  291. mode = XFER_MW_DMA_1;
  292. }
  293. mode = min(mode, req_mode);
  294. printk(KERN_INFO "%s: %s mode selected\n", drive->name,
  295. mode ? ide_xfer_verbose(mode) : "no DMA");
  296. return mode;
  297. }
  298. EXPORT_SYMBOL_GPL(ide_find_dma_mode);
  299. static int ide_tune_dma(ide_drive_t *drive)
  300. {
  301. ide_hwif_t *hwif = drive->hwif;
  302. u8 speed;
  303. if (ata_id_has_dma(drive->id) == 0 ||
  304. (drive->dev_flags & IDE_DFLAG_NODMA))
  305. return 0;
  306. /* consult the list of known "bad" drives */
  307. if (__ide_dma_bad_drive(drive))
  308. return 0;
  309. if (ide_id_dma_bug(drive))
  310. return 0;
  311. if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
  312. return config_drive_for_dma(drive);
  313. speed = ide_max_dma_mode(drive);
  314. if (!speed)
  315. return 0;
  316. if (ide_set_dma_mode(drive, speed))
  317. return 0;
  318. return 1;
  319. }
  320. static int ide_dma_check(ide_drive_t *drive)
  321. {
  322. ide_hwif_t *hwif = drive->hwif;
  323. if (ide_tune_dma(drive))
  324. return 0;
  325. /* TODO: always do PIO fallback */
  326. if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
  327. return -1;
  328. ide_set_max_pio(drive);
  329. return -1;
  330. }
  331. int ide_id_dma_bug(ide_drive_t *drive)
  332. {
  333. u16 *id = drive->id;
  334. if (id[ATA_ID_FIELD_VALID] & 4) {
  335. if ((id[ATA_ID_UDMA_MODES] >> 8) &&
  336. (id[ATA_ID_MWDMA_MODES] >> 8))
  337. goto err_out;
  338. } else if (id[ATA_ID_FIELD_VALID] & 2) {
  339. if ((id[ATA_ID_MWDMA_MODES] >> 8) &&
  340. (id[ATA_ID_SWDMA_MODES] >> 8))
  341. goto err_out;
  342. }
  343. return 0;
  344. err_out:
  345. printk(KERN_ERR "%s: bad DMA info in identify block\n", drive->name);
  346. return 1;
  347. }
  348. int ide_set_dma(ide_drive_t *drive)
  349. {
  350. int rc;
  351. /*
  352. * Force DMAing for the beginning of the check.
  353. * Some chipsets appear to do interesting
  354. * things, if not checked and cleared.
  355. * PARANOIA!!!
  356. */
  357. ide_dma_off_quietly(drive);
  358. rc = ide_dma_check(drive);
  359. if (rc)
  360. return rc;
  361. ide_dma_on(drive);
  362. return 0;
  363. }
  364. void ide_check_dma_crc(ide_drive_t *drive)
  365. {
  366. u8 mode;
  367. ide_dma_off_quietly(drive);
  368. drive->crc_count = 0;
  369. mode = drive->current_speed;
  370. /*
  371. * Don't try non Ultra-DMA modes without iCRC's. Force the
  372. * device to PIO and make the user enable SWDMA/MWDMA modes.
  373. */
  374. if (mode > XFER_UDMA_0 && mode <= XFER_UDMA_7)
  375. mode--;
  376. else
  377. mode = XFER_PIO_4;
  378. ide_set_xfer_rate(drive, mode);
  379. if (drive->current_speed >= XFER_SW_DMA_0)
  380. ide_dma_on(drive);
  381. }
  382. void ide_dma_lost_irq(ide_drive_t *drive)
  383. {
  384. printk(KERN_ERR "%s: DMA interrupt recovery\n", drive->name);
  385. }
  386. EXPORT_SYMBOL_GPL(ide_dma_lost_irq);
  387. void ide_dma_timeout(ide_drive_t *drive)
  388. {
  389. ide_hwif_t *hwif = drive->hwif;
  390. printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
  391. if (hwif->dma_ops->dma_test_irq(drive))
  392. return;
  393. ide_dump_status(drive, "DMA timeout", hwif->tp_ops->read_status(hwif));
  394. hwif->dma_ops->dma_end(drive);
  395. }
  396. EXPORT_SYMBOL_GPL(ide_dma_timeout);
  397. void ide_release_dma_engine(ide_hwif_t *hwif)
  398. {
  399. if (hwif->dmatable_cpu) {
  400. int prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
  401. dma_free_coherent(hwif->dev, prd_size,
  402. hwif->dmatable_cpu, hwif->dmatable_dma);
  403. hwif->dmatable_cpu = NULL;
  404. }
  405. }
  406. EXPORT_SYMBOL_GPL(ide_release_dma_engine);
  407. int ide_allocate_dma_engine(ide_hwif_t *hwif)
  408. {
  409. int prd_size;
  410. if (hwif->prd_max_nents == 0)
  411. hwif->prd_max_nents = PRD_ENTRIES;
  412. if (hwif->prd_ent_size == 0)
  413. hwif->prd_ent_size = PRD_BYTES;
  414. prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
  415. hwif->dmatable_cpu = dma_alloc_coherent(hwif->dev, prd_size,
  416. &hwif->dmatable_dma,
  417. GFP_ATOMIC);
  418. if (hwif->dmatable_cpu == NULL) {
  419. printk(KERN_ERR "%s: unable to allocate PRD table\n",
  420. hwif->name);
  421. return -ENOMEM;
  422. }
  423. return 0;
  424. }
  425. EXPORT_SYMBOL_GPL(ide_allocate_dma_engine);