ide-dma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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/gfp.h>
  30. #include <linux/kernel.h>
  31. #include <linux/ide.h>
  32. #include <linux/scatterlist.h>
  33. #include <linux/dma-mapping.h>
  34. static const struct drive_list_entry drive_whitelist[] = {
  35. { "Micropolis 2112A" , NULL },
  36. { "CONNER CTMA 4000" , NULL },
  37. { "CONNER CTT8000-A" , NULL },
  38. { "ST34342A" , NULL },
  39. { NULL , NULL }
  40. };
  41. static const struct drive_list_entry drive_blacklist[] = {
  42. { "WDC AC11000H" , NULL },
  43. { "WDC AC22100H" , NULL },
  44. { "WDC AC32500H" , NULL },
  45. { "WDC AC33100H" , NULL },
  46. { "WDC AC31600H" , NULL },
  47. { "WDC AC32100H" , "24.09P07" },
  48. { "WDC AC23200L" , "21.10N21" },
  49. { "Compaq CRD-8241B" , NULL },
  50. { "CRD-8400B" , NULL },
  51. { "CRD-8480B", NULL },
  52. { "CRD-8482B", NULL },
  53. { "CRD-84" , NULL },
  54. { "SanDisk SDP3B" , NULL },
  55. { "SanDisk SDP3B-64" , NULL },
  56. { "SANYO CD-ROM CRD" , NULL },
  57. { "HITACHI CDR-8" , NULL },
  58. { "HITACHI CDR-8335" , NULL },
  59. { "HITACHI CDR-8435" , NULL },
  60. { "Toshiba CD-ROM XM-6202B" , NULL },
  61. { "TOSHIBA CD-ROM XM-1702BC", NULL },
  62. { "CD-532E-A" , NULL },
  63. { "E-IDE CD-ROM CR-840", NULL },
  64. { "CD-ROM Drive/F5A", NULL },
  65. { "WPI CDD-820", NULL },
  66. { "SAMSUNG CD-ROM SC-148C", NULL },
  67. { "SAMSUNG CD-ROM SC", NULL },
  68. { "ATAPI CD-ROM DRIVE 40X MAXIMUM", NULL },
  69. { "_NEC DV5800A", NULL },
  70. { "SAMSUNG CD-ROM SN-124", "N001" },
  71. { "Seagate STT20000A", NULL },
  72. { "CD-ROM CDR_U200", "1.09" },
  73. { NULL , NULL }
  74. };
  75. /**
  76. * ide_dma_intr - IDE DMA interrupt handler
  77. * @drive: the drive the interrupt is for
  78. *
  79. * Handle an interrupt completing a read/write DMA transfer on an
  80. * IDE device
  81. */
  82. ide_startstop_t ide_dma_intr(ide_drive_t *drive)
  83. {
  84. ide_hwif_t *hwif = drive->hwif;
  85. struct ide_cmd *cmd = &hwif->cmd;
  86. u8 stat = 0, dma_stat = 0;
  87. drive->waiting_for_dma = 0;
  88. dma_stat = hwif->dma_ops->dma_end(drive);
  89. ide_dma_unmap_sg(drive, cmd);
  90. stat = hwif->tp_ops->read_status(hwif);
  91. if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat | ATA_DRQ)) {
  92. if (!dma_stat) {
  93. if ((cmd->tf_flags & IDE_TFLAG_FS) == 0)
  94. ide_finish_cmd(drive, cmd, stat);
  95. else
  96. ide_complete_rq(drive, 0,
  97. blk_rq_sectors(cmd->rq) << 9);
  98. return ide_stopped;
  99. }
  100. printk(KERN_ERR "%s: %s: bad DMA status (0x%02x)\n",
  101. drive->name, __func__, dma_stat);
  102. }
  103. return ide_error(drive, "dma_intr", stat);
  104. }
  105. int ide_dma_good_drive(ide_drive_t *drive)
  106. {
  107. return ide_in_drive_list(drive->id, drive_whitelist);
  108. }
  109. /**
  110. * ide_dma_map_sg - map IDE scatter gather for DMA I/O
  111. * @drive: the drive to map the DMA table for
  112. * @cmd: command
  113. *
  114. * Perform the DMA mapping magic necessary to access the source or
  115. * target buffers of a request via DMA. The lower layers of the
  116. * kernel provide the necessary cache management so that we can
  117. * operate in a portable fashion.
  118. */
  119. static int ide_dma_map_sg(ide_drive_t *drive, struct ide_cmd *cmd)
  120. {
  121. ide_hwif_t *hwif = drive->hwif;
  122. struct scatterlist *sg = hwif->sg_table;
  123. int i;
  124. if (cmd->tf_flags & IDE_TFLAG_WRITE)
  125. cmd->sg_dma_direction = DMA_TO_DEVICE;
  126. else
  127. cmd->sg_dma_direction = DMA_FROM_DEVICE;
  128. i = dma_map_sg(hwif->dev, sg, cmd->sg_nents, cmd->sg_dma_direction);
  129. if (i) {
  130. cmd->orig_sg_nents = cmd->sg_nents;
  131. cmd->sg_nents = i;
  132. }
  133. return i;
  134. }
  135. /**
  136. * ide_dma_unmap_sg - clean up DMA mapping
  137. * @drive: The drive to unmap
  138. *
  139. * Teardown mappings after DMA has completed. This must be called
  140. * after the completion of each use of ide_build_dmatable and before
  141. * the next use of ide_build_dmatable. Failure to do so will cause
  142. * an oops as only one mapping can be live for each target at a given
  143. * time.
  144. */
  145. void ide_dma_unmap_sg(ide_drive_t *drive, struct ide_cmd *cmd)
  146. {
  147. ide_hwif_t *hwif = drive->hwif;
  148. dma_unmap_sg(hwif->dev, hwif->sg_table, cmd->orig_sg_nents,
  149. cmd->sg_dma_direction);
  150. }
  151. EXPORT_SYMBOL_GPL(ide_dma_unmap_sg);
  152. /**
  153. * ide_dma_off_quietly - Generic DMA kill
  154. * @drive: drive to control
  155. *
  156. * Turn off the current DMA on this IDE controller.
  157. */
  158. void ide_dma_off_quietly(ide_drive_t *drive)
  159. {
  160. drive->dev_flags &= ~IDE_DFLAG_USING_DMA;
  161. ide_toggle_bounce(drive, 0);
  162. drive->hwif->dma_ops->dma_host_set(drive, 0);
  163. }
  164. EXPORT_SYMBOL(ide_dma_off_quietly);
  165. /**
  166. * ide_dma_off - disable DMA on a device
  167. * @drive: drive to disable DMA on
  168. *
  169. * Disable IDE DMA for a device on this IDE controller.
  170. * Inform the user that DMA has been disabled.
  171. */
  172. void ide_dma_off(ide_drive_t *drive)
  173. {
  174. printk(KERN_INFO "%s: DMA disabled\n", drive->name);
  175. ide_dma_off_quietly(drive);
  176. }
  177. EXPORT_SYMBOL(ide_dma_off);
  178. /**
  179. * ide_dma_on - Enable DMA on a device
  180. * @drive: drive to enable DMA on
  181. *
  182. * Enable IDE DMA for a device on this IDE controller.
  183. */
  184. void ide_dma_on(ide_drive_t *drive)
  185. {
  186. drive->dev_flags |= IDE_DFLAG_USING_DMA;
  187. ide_toggle_bounce(drive, 1);
  188. drive->hwif->dma_ops->dma_host_set(drive, 1);
  189. }
  190. int __ide_dma_bad_drive(ide_drive_t *drive)
  191. {
  192. u16 *id = drive->id;
  193. int blacklist = ide_in_drive_list(id, drive_blacklist);
  194. if (blacklist) {
  195. printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n",
  196. drive->name, (char *)&id[ATA_ID_PROD]);
  197. return blacklist;
  198. }
  199. return 0;
  200. }
  201. EXPORT_SYMBOL(__ide_dma_bad_drive);
  202. static const u8 xfer_mode_bases[] = {
  203. XFER_UDMA_0,
  204. XFER_MW_DMA_0,
  205. XFER_SW_DMA_0,
  206. };
  207. static unsigned int ide_get_mode_mask(ide_drive_t *drive, u8 base, u8 req_mode)
  208. {
  209. u16 *id = drive->id;
  210. ide_hwif_t *hwif = drive->hwif;
  211. const struct ide_port_ops *port_ops = hwif->port_ops;
  212. unsigned int mask = 0;
  213. switch (base) {
  214. case XFER_UDMA_0:
  215. if ((id[ATA_ID_FIELD_VALID] & 4) == 0)
  216. break;
  217. mask = id[ATA_ID_UDMA_MODES];
  218. if (port_ops && port_ops->udma_filter)
  219. mask &= port_ops->udma_filter(drive);
  220. else
  221. mask &= hwif->ultra_mask;
  222. /*
  223. * avoid false cable warning from eighty_ninty_three()
  224. */
  225. if (req_mode > XFER_UDMA_2) {
  226. if ((mask & 0x78) && (eighty_ninty_three(drive) == 0))
  227. mask &= 0x07;
  228. }
  229. break;
  230. case XFER_MW_DMA_0:
  231. mask = id[ATA_ID_MWDMA_MODES];
  232. /* Also look for the CF specific MWDMA modes... */
  233. if (ata_id_is_cfa(id) && (id[ATA_ID_CFA_MODES] & 0x38)) {
  234. u8 mode = ((id[ATA_ID_CFA_MODES] & 0x38) >> 3) - 1;
  235. mask |= ((2 << mode) - 1) << 3;
  236. }
  237. if (port_ops && port_ops->mdma_filter)
  238. mask &= port_ops->mdma_filter(drive);
  239. else
  240. mask &= hwif->mwdma_mask;
  241. break;
  242. case XFER_SW_DMA_0:
  243. mask = id[ATA_ID_SWDMA_MODES];
  244. if (!(mask & ATA_SWDMA2) && (id[ATA_ID_OLD_DMA_MODES] >> 8)) {
  245. u8 mode = id[ATA_ID_OLD_DMA_MODES] >> 8;
  246. /*
  247. * if the mode is valid convert it to the mask
  248. * (the maximum allowed mode is XFER_SW_DMA_2)
  249. */
  250. if (mode <= 2)
  251. mask = (2 << mode) - 1;
  252. }
  253. mask &= hwif->swdma_mask;
  254. break;
  255. default:
  256. BUG();
  257. break;
  258. }
  259. return mask;
  260. }
  261. /**
  262. * ide_find_dma_mode - compute DMA speed
  263. * @drive: IDE device
  264. * @req_mode: requested mode
  265. *
  266. * Checks the drive/host capabilities and finds the speed to use for
  267. * the DMA transfer. The speed is then limited by the requested mode.
  268. *
  269. * Returns 0 if the drive/host combination is incapable of DMA transfers
  270. * or if the requested mode is not a DMA mode.
  271. */
  272. u8 ide_find_dma_mode(ide_drive_t *drive, u8 req_mode)
  273. {
  274. ide_hwif_t *hwif = drive->hwif;
  275. unsigned int mask;
  276. int x, i;
  277. u8 mode = 0;
  278. if (drive->media != ide_disk) {
  279. if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
  280. return 0;
  281. }
  282. for (i = 0; i < ARRAY_SIZE(xfer_mode_bases); i++) {
  283. if (req_mode < xfer_mode_bases[i])
  284. continue;
  285. mask = ide_get_mode_mask(drive, xfer_mode_bases[i], req_mode);
  286. x = fls(mask) - 1;
  287. if (x >= 0) {
  288. mode = xfer_mode_bases[i] + x;
  289. break;
  290. }
  291. }
  292. if (hwif->chipset == ide_acorn && mode == 0) {
  293. /*
  294. * is this correct?
  295. */
  296. if (ide_dma_good_drive(drive) &&
  297. drive->id[ATA_ID_EIDE_DMA_TIME] < 150)
  298. mode = XFER_MW_DMA_1;
  299. }
  300. mode = min(mode, req_mode);
  301. printk(KERN_INFO "%s: %s mode selected\n", drive->name,
  302. mode ? ide_xfer_verbose(mode) : "no DMA");
  303. return mode;
  304. }
  305. static int ide_tune_dma(ide_drive_t *drive)
  306. {
  307. ide_hwif_t *hwif = drive->hwif;
  308. u8 speed;
  309. if (ata_id_has_dma(drive->id) == 0 ||
  310. (drive->dev_flags & IDE_DFLAG_NODMA))
  311. return 0;
  312. /* consult the list of known "bad" drives */
  313. if (__ide_dma_bad_drive(drive))
  314. return 0;
  315. if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
  316. return config_drive_for_dma(drive);
  317. speed = ide_max_dma_mode(drive);
  318. if (!speed)
  319. return 0;
  320. if (ide_set_dma_mode(drive, speed))
  321. return 0;
  322. return 1;
  323. }
  324. static int ide_dma_check(ide_drive_t *drive)
  325. {
  326. ide_hwif_t *hwif = drive->hwif;
  327. if (ide_tune_dma(drive))
  328. return 0;
  329. /* TODO: always do PIO fallback */
  330. if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
  331. return -1;
  332. ide_set_max_pio(drive);
  333. return -1;
  334. }
  335. int ide_set_dma(ide_drive_t *drive)
  336. {
  337. int rc;
  338. /*
  339. * Force DMAing for the beginning of the check.
  340. * Some chipsets appear to do interesting
  341. * things, if not checked and cleared.
  342. * PARANOIA!!!
  343. */
  344. ide_dma_off_quietly(drive);
  345. rc = ide_dma_check(drive);
  346. if (rc)
  347. return rc;
  348. ide_dma_on(drive);
  349. return 0;
  350. }
  351. void ide_check_dma_crc(ide_drive_t *drive)
  352. {
  353. u8 mode;
  354. ide_dma_off_quietly(drive);
  355. drive->crc_count = 0;
  356. mode = drive->current_speed;
  357. /*
  358. * Don't try non Ultra-DMA modes without iCRC's. Force the
  359. * device to PIO and make the user enable SWDMA/MWDMA modes.
  360. */
  361. if (mode > XFER_UDMA_0 && mode <= XFER_UDMA_7)
  362. mode--;
  363. else
  364. mode = XFER_PIO_4;
  365. ide_set_xfer_rate(drive, mode);
  366. if (drive->current_speed >= XFER_SW_DMA_0)
  367. ide_dma_on(drive);
  368. }
  369. void ide_dma_lost_irq(ide_drive_t *drive)
  370. {
  371. printk(KERN_ERR "%s: DMA interrupt recovery\n", drive->name);
  372. }
  373. EXPORT_SYMBOL_GPL(ide_dma_lost_irq);
  374. /*
  375. * un-busy the port etc, and clear any pending DMA status. we want to
  376. * retry the current request in pio mode instead of risking tossing it
  377. * all away
  378. */
  379. ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
  380. {
  381. ide_hwif_t *hwif = drive->hwif;
  382. const struct ide_dma_ops *dma_ops = hwif->dma_ops;
  383. struct ide_cmd *cmd = &hwif->cmd;
  384. ide_startstop_t ret = ide_stopped;
  385. /*
  386. * end current dma transaction
  387. */
  388. if (error < 0) {
  389. printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
  390. drive->waiting_for_dma = 0;
  391. (void)dma_ops->dma_end(drive);
  392. ide_dma_unmap_sg(drive, cmd);
  393. ret = ide_error(drive, "dma timeout error",
  394. hwif->tp_ops->read_status(hwif));
  395. } else {
  396. printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
  397. if (dma_ops->dma_clear)
  398. dma_ops->dma_clear(drive);
  399. printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
  400. if (dma_ops->dma_test_irq(drive) == 0) {
  401. ide_dump_status(drive, "DMA timeout",
  402. hwif->tp_ops->read_status(hwif));
  403. drive->waiting_for_dma = 0;
  404. (void)dma_ops->dma_end(drive);
  405. ide_dma_unmap_sg(drive, cmd);
  406. }
  407. }
  408. /*
  409. * disable dma for now, but remember that we did so because of
  410. * a timeout -- we'll reenable after we finish this next request
  411. * (or rather the first chunk of it) in pio.
  412. */
  413. drive->dev_flags |= IDE_DFLAG_DMA_PIO_RETRY;
  414. drive->retry_pio++;
  415. ide_dma_off_quietly(drive);
  416. /*
  417. * make sure request is sane
  418. */
  419. if (hwif->rq)
  420. hwif->rq->errors = 0;
  421. return ret;
  422. }
  423. void ide_release_dma_engine(ide_hwif_t *hwif)
  424. {
  425. if (hwif->dmatable_cpu) {
  426. int prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
  427. dma_free_coherent(hwif->dev, prd_size,
  428. hwif->dmatable_cpu, hwif->dmatable_dma);
  429. hwif->dmatable_cpu = NULL;
  430. }
  431. }
  432. EXPORT_SYMBOL_GPL(ide_release_dma_engine);
  433. int ide_allocate_dma_engine(ide_hwif_t *hwif)
  434. {
  435. int prd_size;
  436. if (hwif->prd_max_nents == 0)
  437. hwif->prd_max_nents = PRD_ENTRIES;
  438. if (hwif->prd_ent_size == 0)
  439. hwif->prd_ent_size = PRD_BYTES;
  440. prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
  441. hwif->dmatable_cpu = dma_alloc_coherent(hwif->dev, prd_size,
  442. &hwif->dmatable_dma,
  443. GFP_ATOMIC);
  444. if (hwif->dmatable_cpu == NULL) {
  445. printk(KERN_ERR "%s: unable to allocate PRD table\n",
  446. hwif->name);
  447. return -ENOMEM;
  448. }
  449. return 0;
  450. }
  451. EXPORT_SYMBOL_GPL(ide_allocate_dma_engine);
  452. int ide_dma_prepare(ide_drive_t *drive, struct ide_cmd *cmd)
  453. {
  454. const struct ide_dma_ops *dma_ops = drive->hwif->dma_ops;
  455. if ((drive->dev_flags & IDE_DFLAG_USING_DMA) == 0 ||
  456. (dma_ops->dma_check && dma_ops->dma_check(drive, cmd)))
  457. goto out;
  458. ide_map_sg(drive, cmd);
  459. if (ide_dma_map_sg(drive, cmd) == 0)
  460. goto out_map;
  461. if (dma_ops->dma_setup(drive, cmd))
  462. goto out_dma_unmap;
  463. drive->waiting_for_dma = 1;
  464. return 0;
  465. out_dma_unmap:
  466. ide_dma_unmap_sg(drive, cmd);
  467. out_map:
  468. ide_map_sg(drive, cmd);
  469. out:
  470. return 1;
  471. }