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