ide-dma.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. #include <asm/io.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. u8 stat = 0, dma_stat = 0;
  86. dma_stat = hwif->dma_ops->dma_end(drive);
  87. stat = hwif->tp_ops->read_status(hwif);
  88. if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat | ATA_DRQ)) {
  89. if (!dma_stat) {
  90. struct request *rq = HWGROUP(drive)->rq;
  91. task_end_request(drive, rq, stat);
  92. return ide_stopped;
  93. }
  94. printk(KERN_ERR "%s: dma_intr: bad DMA status (dma_stat=%x)\n",
  95. drive->name, dma_stat);
  96. }
  97. return ide_error(drive, "dma_intr", stat);
  98. }
  99. EXPORT_SYMBOL_GPL(ide_dma_intr);
  100. static int ide_dma_good_drive(ide_drive_t *drive)
  101. {
  102. return ide_in_drive_list(drive->id, drive_whitelist);
  103. }
  104. /**
  105. * ide_build_sglist - map IDE scatter gather for DMA I/O
  106. * @drive: the drive to build the DMA table for
  107. * @rq: the request holding the sg list
  108. *
  109. * Perform the DMA mapping magic necessary to access the source or
  110. * target buffers of a request via DMA. The lower layers of the
  111. * kernel provide the necessary cache management so that we can
  112. * operate in a portable fashion.
  113. */
  114. int ide_build_sglist(ide_drive_t *drive, struct request *rq)
  115. {
  116. ide_hwif_t *hwif = HWIF(drive);
  117. struct scatterlist *sg = hwif->sg_table;
  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. return dma_map_sg(hwif->dev, sg, hwif->sg_nents,
  124. hwif->sg_dma_direction);
  125. }
  126. EXPORT_SYMBOL_GPL(ide_build_sglist);
  127. #ifdef CONFIG_BLK_DEV_IDEDMA_SFF
  128. /**
  129. * ide_build_dmatable - build IDE DMA table
  130. *
  131. * ide_build_dmatable() prepares a dma request. We map the command
  132. * to get the pci bus addresses of the buffers and then build up
  133. * the PRD table that the IDE layer wants to be fed. The code
  134. * knows about the 64K wrap bug in the CS5530.
  135. *
  136. * Returns the number of built PRD entries if all went okay,
  137. * returns 0 otherwise.
  138. *
  139. * May also be invoked from trm290.c
  140. */
  141. int ide_build_dmatable (ide_drive_t *drive, struct request *rq)
  142. {
  143. ide_hwif_t *hwif = HWIF(drive);
  144. __le32 *table = (__le32 *)hwif->dmatable_cpu;
  145. unsigned int is_trm290 = (hwif->chipset == ide_trm290) ? 1 : 0;
  146. unsigned int count = 0;
  147. int i;
  148. struct scatterlist *sg;
  149. hwif->sg_nents = i = ide_build_sglist(drive, rq);
  150. if (!i)
  151. return 0;
  152. sg = hwif->sg_table;
  153. while (i) {
  154. u32 cur_addr;
  155. u32 cur_len;
  156. cur_addr = sg_dma_address(sg);
  157. cur_len = sg_dma_len(sg);
  158. /*
  159. * Fill in the dma table, without crossing any 64kB boundaries.
  160. * Most hardware requires 16-bit alignment of all blocks,
  161. * but the trm290 requires 32-bit alignment.
  162. */
  163. while (cur_len) {
  164. if (count++ >= PRD_ENTRIES) {
  165. printk(KERN_ERR "%s: DMA table too small\n", drive->name);
  166. goto use_pio_instead;
  167. } else {
  168. u32 xcount, bcount = 0x10000 - (cur_addr & 0xffff);
  169. if (bcount > cur_len)
  170. bcount = cur_len;
  171. *table++ = cpu_to_le32(cur_addr);
  172. xcount = bcount & 0xffff;
  173. if (is_trm290)
  174. xcount = ((xcount >> 2) - 1) << 16;
  175. else if (xcount == 0x0000) {
  176. /*
  177. * Most chipsets correctly interpret a length of 0x0000 as 64KB,
  178. * but at least one (e.g. CS5530) misinterprets it as zero (!).
  179. * So here we break the 64KB entry into two 32KB entries instead.
  180. */
  181. if (count++ >= PRD_ENTRIES) {
  182. printk(KERN_ERR "%s: DMA table too small\n", drive->name);
  183. goto use_pio_instead;
  184. }
  185. *table++ = cpu_to_le32(0x8000);
  186. *table++ = cpu_to_le32(cur_addr + 0x8000);
  187. xcount = 0x8000;
  188. }
  189. *table++ = cpu_to_le32(xcount);
  190. cur_addr += bcount;
  191. cur_len -= bcount;
  192. }
  193. }
  194. sg = sg_next(sg);
  195. i--;
  196. }
  197. if (count) {
  198. if (!is_trm290)
  199. *--table |= cpu_to_le32(0x80000000);
  200. return count;
  201. }
  202. printk(KERN_ERR "%s: empty DMA table?\n", drive->name);
  203. use_pio_instead:
  204. ide_destroy_dmatable(drive);
  205. return 0; /* revert to PIO for this request */
  206. }
  207. EXPORT_SYMBOL_GPL(ide_build_dmatable);
  208. #endif
  209. /**
  210. * ide_destroy_dmatable - clean up DMA mapping
  211. * @drive: The drive to unmap
  212. *
  213. * Teardown mappings after DMA has completed. This must be called
  214. * after the completion of each use of ide_build_dmatable and before
  215. * the next use of ide_build_dmatable. Failure to do so will cause
  216. * an oops as only one mapping can be live for each target at a given
  217. * time.
  218. */
  219. void ide_destroy_dmatable (ide_drive_t *drive)
  220. {
  221. ide_hwif_t *hwif = drive->hwif;
  222. dma_unmap_sg(hwif->dev, hwif->sg_table, hwif->sg_nents,
  223. hwif->sg_dma_direction);
  224. }
  225. EXPORT_SYMBOL_GPL(ide_destroy_dmatable);
  226. #ifdef CONFIG_BLK_DEV_IDEDMA_SFF
  227. /**
  228. * config_drive_for_dma - attempt to activate IDE DMA
  229. * @drive: the drive to place in DMA mode
  230. *
  231. * If the drive supports at least mode 2 DMA or UDMA of any kind
  232. * then attempt to place it into DMA mode. Drives that are known to
  233. * support DMA but predate the DMA properties or that are known
  234. * to have DMA handling bugs are also set up appropriately based
  235. * on the good/bad drive lists.
  236. */
  237. static int config_drive_for_dma (ide_drive_t *drive)
  238. {
  239. ide_hwif_t *hwif = drive->hwif;
  240. u16 *id = drive->id;
  241. if (drive->media != ide_disk) {
  242. if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
  243. return 0;
  244. }
  245. /*
  246. * Enable DMA on any drive that has
  247. * UltraDMA (mode 0/1/2/3/4/5/6) enabled
  248. */
  249. if ((id[ATA_ID_FIELD_VALID] & 4) &&
  250. ((id[ATA_ID_UDMA_MODES] >> 8) & 0x7f))
  251. return 1;
  252. /*
  253. * Enable DMA on any drive that has mode2 DMA
  254. * (multi or single) enabled
  255. */
  256. if (id[ATA_ID_FIELD_VALID] & 2) /* regular DMA */
  257. if ((id[ATA_ID_MWDMA_MODES] & 0x404) == 0x404 ||
  258. (id[ATA_ID_SWDMA_MODES] & 0x404) == 0x404)
  259. return 1;
  260. /* Consult the list of known "good" drives */
  261. if (ide_dma_good_drive(drive))
  262. return 1;
  263. return 0;
  264. }
  265. /**
  266. * dma_timer_expiry - handle a DMA timeout
  267. * @drive: Drive that timed out
  268. *
  269. * An IDE DMA transfer timed out. In the event of an error we ask
  270. * the driver to resolve the problem, if a DMA transfer is still
  271. * in progress we continue to wait (arguably we need to add a
  272. * secondary 'I don't care what the drive thinks' timeout here)
  273. * Finally if we have an interrupt we let it complete the I/O.
  274. * But only one time - we clear expiry and if it's still not
  275. * completed after WAIT_CMD, we error and retry in PIO.
  276. * This can occur if an interrupt is lost or due to hang or bugs.
  277. */
  278. static int dma_timer_expiry (ide_drive_t *drive)
  279. {
  280. ide_hwif_t *hwif = HWIF(drive);
  281. u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
  282. printk(KERN_WARNING "%s: dma_timer_expiry: dma status == 0x%02x\n",
  283. drive->name, dma_stat);
  284. if ((dma_stat & 0x18) == 0x18) /* BUSY Stupid Early Timer !! */
  285. return WAIT_CMD;
  286. HWGROUP(drive)->expiry = NULL; /* one free ride for now */
  287. /* 1 dmaing, 2 error, 4 intr */
  288. if (dma_stat & 2) /* ERROR */
  289. return -1;
  290. if (dma_stat & 1) /* DMAing */
  291. return WAIT_CMD;
  292. if (dma_stat & 4) /* Got an Interrupt */
  293. return WAIT_CMD;
  294. return 0; /* Status is unknown -- reset the bus */
  295. }
  296. /**
  297. * ide_dma_host_set - Enable/disable DMA on a host
  298. * @drive: drive to control
  299. *
  300. * Enable/disable DMA on an IDE controller following generic
  301. * bus-mastering IDE controller behaviour.
  302. */
  303. void ide_dma_host_set(ide_drive_t *drive, int on)
  304. {
  305. ide_hwif_t *hwif = HWIF(drive);
  306. u8 unit = drive->dn & 1;
  307. u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
  308. if (on)
  309. dma_stat |= (1 << (5 + unit));
  310. else
  311. dma_stat &= ~(1 << (5 + unit));
  312. if (hwif->host_flags & IDE_HFLAG_MMIO)
  313. writeb(dma_stat,
  314. (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
  315. else
  316. outb(dma_stat, hwif->dma_base + ATA_DMA_STATUS);
  317. }
  318. EXPORT_SYMBOL_GPL(ide_dma_host_set);
  319. #endif /* CONFIG_BLK_DEV_IDEDMA_SFF */
  320. /**
  321. * ide_dma_off_quietly - Generic DMA kill
  322. * @drive: drive to control
  323. *
  324. * Turn off the current DMA on this IDE controller.
  325. */
  326. void ide_dma_off_quietly(ide_drive_t *drive)
  327. {
  328. drive->dev_flags &= ~IDE_DFLAG_USING_DMA;
  329. ide_toggle_bounce(drive, 0);
  330. drive->hwif->dma_ops->dma_host_set(drive, 0);
  331. }
  332. EXPORT_SYMBOL(ide_dma_off_quietly);
  333. /**
  334. * ide_dma_off - disable DMA on a device
  335. * @drive: drive to disable DMA on
  336. *
  337. * Disable IDE DMA for a device on this IDE controller.
  338. * Inform the user that DMA has been disabled.
  339. */
  340. void ide_dma_off(ide_drive_t *drive)
  341. {
  342. printk(KERN_INFO "%s: DMA disabled\n", drive->name);
  343. ide_dma_off_quietly(drive);
  344. }
  345. EXPORT_SYMBOL(ide_dma_off);
  346. /**
  347. * ide_dma_on - Enable DMA on a device
  348. * @drive: drive to enable DMA on
  349. *
  350. * Enable IDE DMA for a device on this IDE controller.
  351. */
  352. void ide_dma_on(ide_drive_t *drive)
  353. {
  354. drive->dev_flags |= IDE_DFLAG_USING_DMA;
  355. ide_toggle_bounce(drive, 1);
  356. drive->hwif->dma_ops->dma_host_set(drive, 1);
  357. }
  358. #ifdef CONFIG_BLK_DEV_IDEDMA_SFF
  359. /**
  360. * ide_dma_setup - begin a DMA phase
  361. * @drive: target device
  362. *
  363. * Build an IDE DMA PRD (IDE speak for scatter gather table)
  364. * and then set up the DMA transfer registers for a device
  365. * that follows generic IDE PCI DMA behaviour. Controllers can
  366. * override this function if they need to
  367. *
  368. * Returns 0 on success. If a PIO fallback is required then 1
  369. * is returned.
  370. */
  371. int ide_dma_setup(ide_drive_t *drive)
  372. {
  373. ide_hwif_t *hwif = drive->hwif;
  374. struct request *rq = HWGROUP(drive)->rq;
  375. unsigned int reading;
  376. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  377. u8 dma_stat;
  378. if (rq_data_dir(rq))
  379. reading = 0;
  380. else
  381. reading = 1 << 3;
  382. /* fall back to pio! */
  383. if (!ide_build_dmatable(drive, rq)) {
  384. ide_map_sg(drive, rq);
  385. return 1;
  386. }
  387. /* PRD table */
  388. if (hwif->host_flags & IDE_HFLAG_MMIO)
  389. writel(hwif->dmatable_dma,
  390. (void __iomem *)(hwif->dma_base + ATA_DMA_TABLE_OFS));
  391. else
  392. outl(hwif->dmatable_dma, hwif->dma_base + ATA_DMA_TABLE_OFS);
  393. /* specify r/w */
  394. if (mmio)
  395. writeb(reading, (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  396. else
  397. outb(reading, hwif->dma_base + ATA_DMA_CMD);
  398. /* read DMA status for INTR & ERROR flags */
  399. dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
  400. /* clear INTR & ERROR flags */
  401. if (mmio)
  402. writeb(dma_stat | 6,
  403. (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
  404. else
  405. outb(dma_stat | 6, hwif->dma_base + ATA_DMA_STATUS);
  406. drive->waiting_for_dma = 1;
  407. return 0;
  408. }
  409. EXPORT_SYMBOL_GPL(ide_dma_setup);
  410. void ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
  411. {
  412. /* issue cmd to drive */
  413. ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, dma_timer_expiry);
  414. }
  415. EXPORT_SYMBOL_GPL(ide_dma_exec_cmd);
  416. void ide_dma_start(ide_drive_t *drive)
  417. {
  418. ide_hwif_t *hwif = drive->hwif;
  419. u8 dma_cmd;
  420. /* Note that this is done *after* the cmd has
  421. * been issued to the drive, as per the BM-IDE spec.
  422. * The Promise Ultra33 doesn't work correctly when
  423. * we do this part before issuing the drive cmd.
  424. */
  425. if (hwif->host_flags & IDE_HFLAG_MMIO) {
  426. dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  427. /* start DMA */
  428. writeb(dma_cmd | 1,
  429. (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  430. } else {
  431. dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
  432. outb(dma_cmd | 1, hwif->dma_base + ATA_DMA_CMD);
  433. }
  434. wmb();
  435. }
  436. EXPORT_SYMBOL_GPL(ide_dma_start);
  437. /* returns 1 on error, 0 otherwise */
  438. int ide_dma_end(ide_drive_t *drive)
  439. {
  440. ide_hwif_t *hwif = drive->hwif;
  441. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  442. u8 dma_stat = 0, dma_cmd = 0;
  443. drive->waiting_for_dma = 0;
  444. if (mmio) {
  445. /* get DMA command mode */
  446. dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  447. /* stop DMA */
  448. writeb(dma_cmd & ~1,
  449. (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  450. } else {
  451. dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
  452. outb(dma_cmd & ~1, hwif->dma_base + ATA_DMA_CMD);
  453. }
  454. /* get DMA status */
  455. dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
  456. if (mmio)
  457. /* clear the INTR & ERROR bits */
  458. writeb(dma_stat | 6,
  459. (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
  460. else
  461. outb(dma_stat | 6, hwif->dma_base + ATA_DMA_STATUS);
  462. /* purge DMA mappings */
  463. ide_destroy_dmatable(drive);
  464. /* verify good DMA status */
  465. wmb();
  466. return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0;
  467. }
  468. EXPORT_SYMBOL_GPL(ide_dma_end);
  469. /* returns 1 if dma irq issued, 0 otherwise */
  470. int ide_dma_test_irq(ide_drive_t *drive)
  471. {
  472. ide_hwif_t *hwif = HWIF(drive);
  473. u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
  474. /* return 1 if INTR asserted */
  475. if ((dma_stat & 4) == 4)
  476. return 1;
  477. return 0;
  478. }
  479. EXPORT_SYMBOL_GPL(ide_dma_test_irq);
  480. #else
  481. static inline int config_drive_for_dma(ide_drive_t *drive) { return 0; }
  482. #endif /* CONFIG_BLK_DEV_IDEDMA_SFF */
  483. int __ide_dma_bad_drive (ide_drive_t *drive)
  484. {
  485. u16 *id = drive->id;
  486. int blacklist = ide_in_drive_list(id, drive_blacklist);
  487. if (blacklist) {
  488. printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n",
  489. drive->name, (char *)&id[ATA_ID_PROD]);
  490. return blacklist;
  491. }
  492. return 0;
  493. }
  494. EXPORT_SYMBOL(__ide_dma_bad_drive);
  495. static const u8 xfer_mode_bases[] = {
  496. XFER_UDMA_0,
  497. XFER_MW_DMA_0,
  498. XFER_SW_DMA_0,
  499. };
  500. static unsigned int ide_get_mode_mask(ide_drive_t *drive, u8 base, u8 req_mode)
  501. {
  502. u16 *id = drive->id;
  503. ide_hwif_t *hwif = drive->hwif;
  504. const struct ide_port_ops *port_ops = hwif->port_ops;
  505. unsigned int mask = 0;
  506. switch(base) {
  507. case XFER_UDMA_0:
  508. if ((id[ATA_ID_FIELD_VALID] & 4) == 0)
  509. break;
  510. if (port_ops && port_ops->udma_filter)
  511. mask = port_ops->udma_filter(drive);
  512. else
  513. mask = hwif->ultra_mask;
  514. mask &= id[ATA_ID_UDMA_MODES];
  515. /*
  516. * avoid false cable warning from eighty_ninty_three()
  517. */
  518. if (req_mode > XFER_UDMA_2) {
  519. if ((mask & 0x78) && (eighty_ninty_three(drive) == 0))
  520. mask &= 0x07;
  521. }
  522. break;
  523. case XFER_MW_DMA_0:
  524. if ((id[ATA_ID_FIELD_VALID] & 2) == 0)
  525. break;
  526. if (port_ops && port_ops->mdma_filter)
  527. mask = port_ops->mdma_filter(drive);
  528. else
  529. mask = hwif->mwdma_mask;
  530. mask &= id[ATA_ID_MWDMA_MODES];
  531. break;
  532. case XFER_SW_DMA_0:
  533. if (id[ATA_ID_FIELD_VALID] & 2) {
  534. mask = id[ATA_ID_SWDMA_MODES] & hwif->swdma_mask;
  535. } else if (id[ATA_ID_OLD_DMA_MODES] >> 8) {
  536. u8 mode = id[ATA_ID_OLD_DMA_MODES] >> 8;
  537. /*
  538. * if the mode is valid convert it to the mask
  539. * (the maximum allowed mode is XFER_SW_DMA_2)
  540. */
  541. if (mode <= 2)
  542. mask = ((2 << mode) - 1) & hwif->swdma_mask;
  543. }
  544. break;
  545. default:
  546. BUG();
  547. break;
  548. }
  549. return mask;
  550. }
  551. /**
  552. * ide_find_dma_mode - compute DMA speed
  553. * @drive: IDE device
  554. * @req_mode: requested mode
  555. *
  556. * Checks the drive/host capabilities and finds the speed to use for
  557. * the DMA transfer. The speed is then limited by the requested mode.
  558. *
  559. * Returns 0 if the drive/host combination is incapable of DMA transfers
  560. * or if the requested mode is not a DMA mode.
  561. */
  562. u8 ide_find_dma_mode(ide_drive_t *drive, u8 req_mode)
  563. {
  564. ide_hwif_t *hwif = drive->hwif;
  565. unsigned int mask;
  566. int x, i;
  567. u8 mode = 0;
  568. if (drive->media != ide_disk) {
  569. if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
  570. return 0;
  571. }
  572. for (i = 0; i < ARRAY_SIZE(xfer_mode_bases); i++) {
  573. if (req_mode < xfer_mode_bases[i])
  574. continue;
  575. mask = ide_get_mode_mask(drive, xfer_mode_bases[i], req_mode);
  576. x = fls(mask) - 1;
  577. if (x >= 0) {
  578. mode = xfer_mode_bases[i] + x;
  579. break;
  580. }
  581. }
  582. if (hwif->chipset == ide_acorn && mode == 0) {
  583. /*
  584. * is this correct?
  585. */
  586. if (ide_dma_good_drive(drive) &&
  587. drive->id[ATA_ID_EIDE_DMA_TIME] < 150)
  588. mode = XFER_MW_DMA_1;
  589. }
  590. mode = min(mode, req_mode);
  591. printk(KERN_INFO "%s: %s mode selected\n", drive->name,
  592. mode ? ide_xfer_verbose(mode) : "no DMA");
  593. return mode;
  594. }
  595. EXPORT_SYMBOL_GPL(ide_find_dma_mode);
  596. static int ide_tune_dma(ide_drive_t *drive)
  597. {
  598. ide_hwif_t *hwif = drive->hwif;
  599. u8 speed;
  600. if (ata_id_has_dma(drive->id) == 0 ||
  601. (drive->dev_flags & IDE_DFLAG_NODMA))
  602. return 0;
  603. /* consult the list of known "bad" drives */
  604. if (__ide_dma_bad_drive(drive))
  605. return 0;
  606. if (ide_id_dma_bug(drive))
  607. return 0;
  608. if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
  609. return config_drive_for_dma(drive);
  610. speed = ide_max_dma_mode(drive);
  611. if (!speed)
  612. return 0;
  613. if (ide_set_dma_mode(drive, speed))
  614. return 0;
  615. return 1;
  616. }
  617. static int ide_dma_check(ide_drive_t *drive)
  618. {
  619. ide_hwif_t *hwif = drive->hwif;
  620. if (ide_tune_dma(drive))
  621. return 0;
  622. /* TODO: always do PIO fallback */
  623. if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
  624. return -1;
  625. ide_set_max_pio(drive);
  626. return -1;
  627. }
  628. int ide_id_dma_bug(ide_drive_t *drive)
  629. {
  630. u16 *id = drive->id;
  631. if (id[ATA_ID_FIELD_VALID] & 4) {
  632. if ((id[ATA_ID_UDMA_MODES] >> 8) &&
  633. (id[ATA_ID_MWDMA_MODES] >> 8))
  634. goto err_out;
  635. } else if (id[ATA_ID_FIELD_VALID] & 2) {
  636. if ((id[ATA_ID_MWDMA_MODES] >> 8) &&
  637. (id[ATA_ID_SWDMA_MODES] >> 8))
  638. goto err_out;
  639. }
  640. return 0;
  641. err_out:
  642. printk(KERN_ERR "%s: bad DMA info in identify block\n", drive->name);
  643. return 1;
  644. }
  645. int ide_set_dma(ide_drive_t *drive)
  646. {
  647. int rc;
  648. /*
  649. * Force DMAing for the beginning of the check.
  650. * Some chipsets appear to do interesting
  651. * things, if not checked and cleared.
  652. * PARANOIA!!!
  653. */
  654. ide_dma_off_quietly(drive);
  655. rc = ide_dma_check(drive);
  656. if (rc)
  657. return rc;
  658. ide_dma_on(drive);
  659. return 0;
  660. }
  661. void ide_check_dma_crc(ide_drive_t *drive)
  662. {
  663. u8 mode;
  664. ide_dma_off_quietly(drive);
  665. drive->crc_count = 0;
  666. mode = drive->current_speed;
  667. /*
  668. * Don't try non Ultra-DMA modes without iCRC's. Force the
  669. * device to PIO and make the user enable SWDMA/MWDMA modes.
  670. */
  671. if (mode > XFER_UDMA_0 && mode <= XFER_UDMA_7)
  672. mode--;
  673. else
  674. mode = XFER_PIO_4;
  675. ide_set_xfer_rate(drive, mode);
  676. if (drive->current_speed >= XFER_SW_DMA_0)
  677. ide_dma_on(drive);
  678. }
  679. void ide_dma_lost_irq(ide_drive_t *drive)
  680. {
  681. printk(KERN_ERR "%s: DMA interrupt recovery\n", drive->name);
  682. }
  683. EXPORT_SYMBOL_GPL(ide_dma_lost_irq);
  684. void ide_dma_timeout(ide_drive_t *drive)
  685. {
  686. ide_hwif_t *hwif = HWIF(drive);
  687. printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
  688. if (hwif->dma_ops->dma_test_irq(drive))
  689. return;
  690. ide_dump_status(drive, "DMA timeout", hwif->tp_ops->read_status(hwif));
  691. hwif->dma_ops->dma_end(drive);
  692. }
  693. EXPORT_SYMBOL_GPL(ide_dma_timeout);
  694. void ide_release_dma_engine(ide_hwif_t *hwif)
  695. {
  696. if (hwif->dmatable_cpu) {
  697. int prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
  698. dma_free_coherent(hwif->dev, prd_size,
  699. hwif->dmatable_cpu, hwif->dmatable_dma);
  700. hwif->dmatable_cpu = NULL;
  701. }
  702. }
  703. EXPORT_SYMBOL_GPL(ide_release_dma_engine);
  704. int ide_allocate_dma_engine(ide_hwif_t *hwif)
  705. {
  706. int prd_size;
  707. if (hwif->prd_max_nents == 0)
  708. hwif->prd_max_nents = PRD_ENTRIES;
  709. if (hwif->prd_ent_size == 0)
  710. hwif->prd_ent_size = PRD_BYTES;
  711. prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
  712. hwif->dmatable_cpu = dma_alloc_coherent(hwif->dev, prd_size,
  713. &hwif->dmatable_dma,
  714. GFP_ATOMIC);
  715. if (hwif->dmatable_cpu == NULL) {
  716. printk(KERN_ERR "%s: unable to allocate PRD table\n",
  717. hwif->name);
  718. return -ENOMEM;
  719. }
  720. return 0;
  721. }
  722. EXPORT_SYMBOL_GPL(ide_allocate_dma_engine);
  723. #ifdef CONFIG_BLK_DEV_IDEDMA_SFF
  724. const struct ide_dma_ops sff_dma_ops = {
  725. .dma_host_set = ide_dma_host_set,
  726. .dma_setup = ide_dma_setup,
  727. .dma_exec_cmd = ide_dma_exec_cmd,
  728. .dma_start = ide_dma_start,
  729. .dma_end = ide_dma_end,
  730. .dma_test_irq = ide_dma_test_irq,
  731. .dma_timeout = ide_dma_timeout,
  732. .dma_lost_irq = ide_dma_lost_irq,
  733. };
  734. EXPORT_SYMBOL_GPL(sff_dma_ops);
  735. #endif /* CONFIG_BLK_DEV_IDEDMA_SFF */