ide-dma.c 21 KB

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