ide-dma.c 21 KB

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