ide-dma.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /*
  2. * linux/drivers/ide/ide-dma.c Version 4.10 June 9, 2000
  3. *
  4. * Copyright (c) 1999-2000 Andre Hedrick <andre@linux-ide.org>
  5. * May be copied or modified under the terms of the GNU General Public License
  6. */
  7. /*
  8. * Special Thanks to Mark for his Six years of work.
  9. *
  10. * Copyright (c) 1995-1998 Mark Lord
  11. * May be copied or modified under the terms of the GNU General Public License
  12. */
  13. /*
  14. * This module provides support for the bus-master IDE DMA functions
  15. * of various PCI chipsets, including the Intel PIIX (i82371FB for
  16. * the 430 FX chipset), the PIIX3 (i82371SB for the 430 HX/VX and
  17. * 440 chipsets), and the PIIX4 (i82371AB for the 430 TX chipset)
  18. * ("PIIX" stands for "PCI ISA IDE Xcellerator").
  19. *
  20. * Pretty much the same code works for other IDE PCI bus-mastering chipsets.
  21. *
  22. * DMA is supported for all IDE devices (disk drives, cdroms, tapes, floppies).
  23. *
  24. * By default, DMA support is prepared for use, but is currently enabled only
  25. * for drives which already have DMA enabled (UltraDMA or mode 2 multi/single),
  26. * or which are recognized as "good" (see table below). Drives with only mode0
  27. * or mode1 (multi/single) DMA should also work with this chipset/driver
  28. * (eg. MC2112A) but are not enabled by default.
  29. *
  30. * Use "hdparm -i" to view modes supported by a given drive.
  31. *
  32. * The hdparm-3.5 (or later) utility can be used for manually enabling/disabling
  33. * DMA support, but must be (re-)compiled against this kernel version or later.
  34. *
  35. * To enable DMA, use "hdparm -d1 /dev/hd?" on a per-drive basis after booting.
  36. * If problems arise, ide.c will disable DMA operation after a few retries.
  37. * This error recovery mechanism works and has been extremely well exercised.
  38. *
  39. * IDE drives, depending on their vintage, may support several different modes
  40. * of DMA operation. The boot-time modes are indicated with a "*" in
  41. * the "hdparm -i" listing, and can be changed with *knowledgeable* use of
  42. * the "hdparm -X" feature. There is seldom a need to do this, as drives
  43. * normally power-up with their "best" PIO/DMA modes enabled.
  44. *
  45. * Testing has been done with a rather extensive number of drives,
  46. * with Quantum & Western Digital models generally outperforming the pack,
  47. * and Fujitsu & Conner (and some Seagate which are really Conner) drives
  48. * showing more lackluster throughput.
  49. *
  50. * Keep an eye on /var/adm/messages for "DMA disabled" messages.
  51. *
  52. * Some people have reported trouble with Intel Zappa motherboards.
  53. * This can be fixed by upgrading the AMI BIOS to version 1.00.04.BS0,
  54. * available from ftp://ftp.intel.com/pub/bios/10004bs0.exe
  55. * (thanks to Glen Morrell <glen@spin.Stanford.edu> for researching this).
  56. *
  57. * Thanks to "Christopher J. Reimer" <reimer@doe.carleton.ca> for
  58. * fixing the problem with the BIOS on some Acer motherboards.
  59. *
  60. * Thanks to "Benoit Poulot-Cazajous" <poulot@chorus.fr> for testing
  61. * "TX" chipset compatibility and for providing patches for the "TX" chipset.
  62. *
  63. * Thanks to Christian Brunner <chb@muc.de> for taking a good first crack
  64. * at generic DMA -- his patches were referred to when preparing this code.
  65. *
  66. * Most importantly, thanks to Robert Bringman <rob@mars.trion.com>
  67. * for supplying a Promise UDMA board & WD UDMA drive for this work!
  68. *
  69. * And, yes, Intel Zappa boards really *do* use both PIIX IDE ports.
  70. *
  71. * ATA-66/100 and recovery functions, I forgot the rest......
  72. *
  73. */
  74. #include <linux/module.h>
  75. #include <linux/types.h>
  76. #include <linux/kernel.h>
  77. #include <linux/timer.h>
  78. #include <linux/mm.h>
  79. #include <linux/interrupt.h>
  80. #include <linux/pci.h>
  81. #include <linux/init.h>
  82. #include <linux/ide.h>
  83. #include <linux/delay.h>
  84. #include <linux/scatterlist.h>
  85. #include <asm/io.h>
  86. #include <asm/irq.h>
  87. static const struct drive_list_entry drive_whitelist [] = {
  88. { "Micropolis 2112A" , NULL },
  89. { "CONNER CTMA 4000" , NULL },
  90. { "CONNER CTT8000-A" , NULL },
  91. { "ST34342A" , NULL },
  92. { NULL , NULL }
  93. };
  94. static const struct drive_list_entry drive_blacklist [] = {
  95. { "WDC AC11000H" , NULL },
  96. { "WDC AC22100H" , NULL },
  97. { "WDC AC32500H" , NULL },
  98. { "WDC AC33100H" , NULL },
  99. { "WDC AC31600H" , NULL },
  100. { "WDC AC32100H" , "24.09P07" },
  101. { "WDC AC23200L" , "21.10N21" },
  102. { "Compaq CRD-8241B" , NULL },
  103. { "CRD-8400B" , NULL },
  104. { "CRD-8480B", NULL },
  105. { "CRD-8482B", NULL },
  106. { "CRD-84" , NULL },
  107. { "SanDisk SDP3B" , NULL },
  108. { "SanDisk SDP3B-64" , NULL },
  109. { "SANYO CD-ROM CRD" , NULL },
  110. { "HITACHI CDR-8" , NULL },
  111. { "HITACHI CDR-8335" , NULL },
  112. { "HITACHI CDR-8435" , NULL },
  113. { "Toshiba CD-ROM XM-6202B" , NULL },
  114. { "TOSHIBA CD-ROM XM-1702BC", NULL },
  115. { "CD-532E-A" , NULL },
  116. { "E-IDE CD-ROM CR-840", NULL },
  117. { "CD-ROM Drive/F5A", NULL },
  118. { "WPI CDD-820", NULL },
  119. { "SAMSUNG CD-ROM SC-148C", NULL },
  120. { "SAMSUNG CD-ROM SC", NULL },
  121. { "ATAPI CD-ROM DRIVE 40X MAXIMUM", NULL },
  122. { "_NEC DV5800A", NULL },
  123. { "SAMSUNG CD-ROM SN-124", "N001" },
  124. { "Seagate STT20000A", NULL },
  125. { NULL , NULL }
  126. };
  127. /**
  128. * ide_dma_intr - IDE DMA interrupt handler
  129. * @drive: the drive the interrupt is for
  130. *
  131. * Handle an interrupt completing a read/write DMA transfer on an
  132. * IDE device
  133. */
  134. ide_startstop_t ide_dma_intr (ide_drive_t *drive)
  135. {
  136. u8 stat = 0, dma_stat = 0;
  137. dma_stat = HWIF(drive)->ide_dma_end(drive);
  138. stat = HWIF(drive)->INB(IDE_STATUS_REG); /* get drive status */
  139. if (OK_STAT(stat,DRIVE_READY,drive->bad_wstat|DRQ_STAT)) {
  140. if (!dma_stat) {
  141. struct request *rq = HWGROUP(drive)->rq;
  142. if (rq->rq_disk) {
  143. ide_driver_t *drv;
  144. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  145. drv->end_request(drive, 1, rq->nr_sectors);
  146. } else
  147. ide_end_request(drive, 1, rq->nr_sectors);
  148. return ide_stopped;
  149. }
  150. printk(KERN_ERR "%s: dma_intr: bad DMA status (dma_stat=%x)\n",
  151. drive->name, dma_stat);
  152. }
  153. return ide_error(drive, "dma_intr", stat);
  154. }
  155. EXPORT_SYMBOL_GPL(ide_dma_intr);
  156. static int ide_dma_good_drive(ide_drive_t *drive)
  157. {
  158. return ide_in_drive_list(drive->id, drive_whitelist);
  159. }
  160. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  161. /**
  162. * ide_build_sglist - map IDE scatter gather for DMA I/O
  163. * @drive: the drive to build the DMA table for
  164. * @rq: the request holding the sg list
  165. *
  166. * Perform the PCI mapping magic necessary to access the source or
  167. * target buffers of a request via PCI DMA. The lower layers of the
  168. * kernel provide the necessary cache management so that we can
  169. * operate in a portable fashion
  170. */
  171. int ide_build_sglist(ide_drive_t *drive, struct request *rq)
  172. {
  173. ide_hwif_t *hwif = HWIF(drive);
  174. struct scatterlist *sg = hwif->sg_table;
  175. BUG_ON((rq->cmd_type == REQ_TYPE_ATA_TASKFILE) && rq->nr_sectors > 256);
  176. ide_map_sg(drive, rq);
  177. if (rq_data_dir(rq) == READ)
  178. hwif->sg_dma_direction = PCI_DMA_FROMDEVICE;
  179. else
  180. hwif->sg_dma_direction = PCI_DMA_TODEVICE;
  181. return pci_map_sg(hwif->pci_dev, sg, hwif->sg_nents, hwif->sg_dma_direction);
  182. }
  183. EXPORT_SYMBOL_GPL(ide_build_sglist);
  184. /**
  185. * ide_build_dmatable - build IDE DMA table
  186. *
  187. * ide_build_dmatable() prepares a dma request. We map the command
  188. * to get the pci bus addresses of the buffers and then build up
  189. * the PRD table that the IDE layer wants to be fed. The code
  190. * knows about the 64K wrap bug in the CS5530.
  191. *
  192. * Returns the number of built PRD entries if all went okay,
  193. * returns 0 otherwise.
  194. *
  195. * May also be invoked from trm290.c
  196. */
  197. int ide_build_dmatable (ide_drive_t *drive, struct request *rq)
  198. {
  199. ide_hwif_t *hwif = HWIF(drive);
  200. unsigned int *table = hwif->dmatable_cpu;
  201. unsigned int is_trm290 = (hwif->chipset == ide_trm290) ? 1 : 0;
  202. unsigned int count = 0;
  203. int i;
  204. struct scatterlist *sg;
  205. hwif->sg_nents = i = ide_build_sglist(drive, rq);
  206. if (!i)
  207. return 0;
  208. sg = hwif->sg_table;
  209. while (i) {
  210. u32 cur_addr;
  211. u32 cur_len;
  212. cur_addr = sg_dma_address(sg);
  213. cur_len = sg_dma_len(sg);
  214. /*
  215. * Fill in the dma table, without crossing any 64kB boundaries.
  216. * Most hardware requires 16-bit alignment of all blocks,
  217. * but the trm290 requires 32-bit alignment.
  218. */
  219. while (cur_len) {
  220. if (count++ >= PRD_ENTRIES) {
  221. printk(KERN_ERR "%s: DMA table too small\n", drive->name);
  222. goto use_pio_instead;
  223. } else {
  224. u32 xcount, bcount = 0x10000 - (cur_addr & 0xffff);
  225. if (bcount > cur_len)
  226. bcount = cur_len;
  227. *table++ = cpu_to_le32(cur_addr);
  228. xcount = bcount & 0xffff;
  229. if (is_trm290)
  230. xcount = ((xcount >> 2) - 1) << 16;
  231. if (xcount == 0x0000) {
  232. /*
  233. * Most chipsets correctly interpret a length of 0x0000 as 64KB,
  234. * but at least one (e.g. CS5530) misinterprets it as zero (!).
  235. * So here we break the 64KB entry into two 32KB entries instead.
  236. */
  237. if (count++ >= PRD_ENTRIES) {
  238. printk(KERN_ERR "%s: DMA table too small\n", drive->name);
  239. goto use_pio_instead;
  240. }
  241. *table++ = cpu_to_le32(0x8000);
  242. *table++ = cpu_to_le32(cur_addr + 0x8000);
  243. xcount = 0x8000;
  244. }
  245. *table++ = cpu_to_le32(xcount);
  246. cur_addr += bcount;
  247. cur_len -= bcount;
  248. }
  249. }
  250. sg++;
  251. i--;
  252. }
  253. if (count) {
  254. if (!is_trm290)
  255. *--table |= cpu_to_le32(0x80000000);
  256. return count;
  257. }
  258. printk(KERN_ERR "%s: empty DMA table?\n", drive->name);
  259. use_pio_instead:
  260. pci_unmap_sg(hwif->pci_dev,
  261. hwif->sg_table,
  262. hwif->sg_nents,
  263. hwif->sg_dma_direction);
  264. return 0; /* revert to PIO for this request */
  265. }
  266. EXPORT_SYMBOL_GPL(ide_build_dmatable);
  267. /**
  268. * ide_destroy_dmatable - clean up DMA mapping
  269. * @drive: The drive to unmap
  270. *
  271. * Teardown mappings after DMA has completed. This must be called
  272. * after the completion of each use of ide_build_dmatable and before
  273. * the next use of ide_build_dmatable. Failure to do so will cause
  274. * an oops as only one mapping can be live for each target at a given
  275. * time.
  276. */
  277. void ide_destroy_dmatable (ide_drive_t *drive)
  278. {
  279. struct pci_dev *dev = HWIF(drive)->pci_dev;
  280. struct scatterlist *sg = HWIF(drive)->sg_table;
  281. int nents = HWIF(drive)->sg_nents;
  282. pci_unmap_sg(dev, sg, nents, HWIF(drive)->sg_dma_direction);
  283. }
  284. EXPORT_SYMBOL_GPL(ide_destroy_dmatable);
  285. /**
  286. * config_drive_for_dma - attempt to activate IDE DMA
  287. * @drive: the drive to place in DMA mode
  288. *
  289. * If the drive supports at least mode 2 DMA or UDMA of any kind
  290. * then attempt to place it into DMA mode. Drives that are known to
  291. * support DMA but predate the DMA properties or that are known
  292. * to have DMA handling bugs are also set up appropriately based
  293. * on the good/bad drive lists.
  294. */
  295. static int config_drive_for_dma (ide_drive_t *drive)
  296. {
  297. ide_hwif_t *hwif = drive->hwif;
  298. struct hd_driveid *id = drive->id;
  299. /* consult the list of known "bad" drives */
  300. if (__ide_dma_bad_drive(drive))
  301. return -1;
  302. if (drive->media != ide_disk && hwif->atapi_dma == 0)
  303. return -1;
  304. if ((id->capability & 1) && drive->autodma) {
  305. /*
  306. * Enable DMA on any drive that has
  307. * UltraDMA (mode 0/1/2/3/4/5/6) enabled
  308. */
  309. if ((id->field_valid & 4) && ((id->dma_ultra >> 8) & 0x7f))
  310. return 0;
  311. /*
  312. * Enable DMA on any drive that has mode2 DMA
  313. * (multi or single) enabled
  314. */
  315. if (id->field_valid & 2) /* regular DMA */
  316. if ((id->dma_mword & 0x404) == 0x404 ||
  317. (id->dma_1word & 0x404) == 0x404)
  318. return 0;
  319. /* Consult the list of known "good" drives */
  320. if (ide_dma_good_drive(drive))
  321. return 0;
  322. }
  323. return -1;
  324. }
  325. /**
  326. * dma_timer_expiry - handle a DMA timeout
  327. * @drive: Drive that timed out
  328. *
  329. * An IDE DMA transfer timed out. In the event of an error we ask
  330. * the driver to resolve the problem, if a DMA transfer is still
  331. * in progress we continue to wait (arguably we need to add a
  332. * secondary 'I don't care what the drive thinks' timeout here)
  333. * Finally if we have an interrupt we let it complete the I/O.
  334. * But only one time - we clear expiry and if it's still not
  335. * completed after WAIT_CMD, we error and retry in PIO.
  336. * This can occur if an interrupt is lost or due to hang or bugs.
  337. */
  338. static int dma_timer_expiry (ide_drive_t *drive)
  339. {
  340. ide_hwif_t *hwif = HWIF(drive);
  341. u8 dma_stat = hwif->INB(hwif->dma_status);
  342. printk(KERN_WARNING "%s: dma_timer_expiry: dma status == 0x%02x\n",
  343. drive->name, dma_stat);
  344. if ((dma_stat & 0x18) == 0x18) /* BUSY Stupid Early Timer !! */
  345. return WAIT_CMD;
  346. HWGROUP(drive)->expiry = NULL; /* one free ride for now */
  347. /* 1 dmaing, 2 error, 4 intr */
  348. if (dma_stat & 2) /* ERROR */
  349. return -1;
  350. if (dma_stat & 1) /* DMAing */
  351. return WAIT_CMD;
  352. if (dma_stat & 4) /* Got an Interrupt */
  353. return WAIT_CMD;
  354. return 0; /* Status is unknown -- reset the bus */
  355. }
  356. /**
  357. * ide_dma_host_off - Generic DMA kill
  358. * @drive: drive to control
  359. *
  360. * Perform the generic IDE controller DMA off operation. This
  361. * works for most IDE bus mastering controllers
  362. */
  363. void ide_dma_host_off(ide_drive_t *drive)
  364. {
  365. ide_hwif_t *hwif = HWIF(drive);
  366. u8 unit = (drive->select.b.unit & 0x01);
  367. u8 dma_stat = hwif->INB(hwif->dma_status);
  368. hwif->OUTB((dma_stat & ~(1<<(5+unit))), hwif->dma_status);
  369. }
  370. EXPORT_SYMBOL(ide_dma_host_off);
  371. /**
  372. * ide_dma_off_quietly - Generic DMA kill
  373. * @drive: drive to control
  374. *
  375. * Turn off the current DMA on this IDE controller.
  376. */
  377. void ide_dma_off_quietly(ide_drive_t *drive)
  378. {
  379. drive->using_dma = 0;
  380. ide_toggle_bounce(drive, 0);
  381. drive->hwif->dma_host_off(drive);
  382. }
  383. EXPORT_SYMBOL(ide_dma_off_quietly);
  384. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
  385. /**
  386. * ide_dma_off - disable DMA on a device
  387. * @drive: drive to disable DMA on
  388. *
  389. * Disable IDE DMA for a device on this IDE controller.
  390. * Inform the user that DMA has been disabled.
  391. */
  392. void ide_dma_off(ide_drive_t *drive)
  393. {
  394. printk(KERN_INFO "%s: DMA disabled\n", drive->name);
  395. drive->hwif->dma_off_quietly(drive);
  396. }
  397. EXPORT_SYMBOL(ide_dma_off);
  398. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  399. /**
  400. * ide_dma_host_on - Enable DMA on a host
  401. * @drive: drive to enable for DMA
  402. *
  403. * Enable DMA on an IDE controller following generic bus mastering
  404. * IDE controller behaviour
  405. */
  406. void ide_dma_host_on(ide_drive_t *drive)
  407. {
  408. if (drive->using_dma) {
  409. ide_hwif_t *hwif = HWIF(drive);
  410. u8 unit = (drive->select.b.unit & 0x01);
  411. u8 dma_stat = hwif->INB(hwif->dma_status);
  412. hwif->OUTB((dma_stat|(1<<(5+unit))), hwif->dma_status);
  413. }
  414. }
  415. EXPORT_SYMBOL(ide_dma_host_on);
  416. /**
  417. * __ide_dma_on - Enable DMA on a device
  418. * @drive: drive to enable DMA on
  419. *
  420. * Enable IDE DMA for a device on this IDE controller.
  421. */
  422. int __ide_dma_on (ide_drive_t *drive)
  423. {
  424. /* consult the list of known "bad" drives */
  425. if (__ide_dma_bad_drive(drive))
  426. return 1;
  427. drive->using_dma = 1;
  428. ide_toggle_bounce(drive, 1);
  429. drive->hwif->dma_host_on(drive);
  430. return 0;
  431. }
  432. EXPORT_SYMBOL(__ide_dma_on);
  433. /**
  434. * ide_dma_setup - begin a DMA phase
  435. * @drive: target device
  436. *
  437. * Build an IDE DMA PRD (IDE speak for scatter gather table)
  438. * and then set up the DMA transfer registers for a device
  439. * that follows generic IDE PCI DMA behaviour. Controllers can
  440. * override this function if they need to
  441. *
  442. * Returns 0 on success. If a PIO fallback is required then 1
  443. * is returned.
  444. */
  445. int ide_dma_setup(ide_drive_t *drive)
  446. {
  447. ide_hwif_t *hwif = drive->hwif;
  448. struct request *rq = HWGROUP(drive)->rq;
  449. unsigned int reading;
  450. u8 dma_stat;
  451. if (rq_data_dir(rq))
  452. reading = 0;
  453. else
  454. reading = 1 << 3;
  455. /* fall back to pio! */
  456. if (!ide_build_dmatable(drive, rq)) {
  457. ide_map_sg(drive, rq);
  458. return 1;
  459. }
  460. /* PRD table */
  461. if (hwif->mmio)
  462. writel(hwif->dmatable_dma, (void __iomem *)hwif->dma_prdtable);
  463. else
  464. outl(hwif->dmatable_dma, hwif->dma_prdtable);
  465. /* specify r/w */
  466. hwif->OUTB(reading, hwif->dma_command);
  467. /* read dma_status for INTR & ERROR flags */
  468. dma_stat = hwif->INB(hwif->dma_status);
  469. /* clear INTR & ERROR flags */
  470. hwif->OUTB(dma_stat|6, hwif->dma_status);
  471. drive->waiting_for_dma = 1;
  472. return 0;
  473. }
  474. EXPORT_SYMBOL_GPL(ide_dma_setup);
  475. static void ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
  476. {
  477. /* issue cmd to drive */
  478. ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, dma_timer_expiry);
  479. }
  480. void ide_dma_start(ide_drive_t *drive)
  481. {
  482. ide_hwif_t *hwif = HWIF(drive);
  483. u8 dma_cmd = hwif->INB(hwif->dma_command);
  484. /* Note that this is done *after* the cmd has
  485. * been issued to the drive, as per the BM-IDE spec.
  486. * The Promise Ultra33 doesn't work correctly when
  487. * we do this part before issuing the drive cmd.
  488. */
  489. /* start DMA */
  490. hwif->OUTB(dma_cmd|1, hwif->dma_command);
  491. hwif->dma = 1;
  492. wmb();
  493. }
  494. EXPORT_SYMBOL_GPL(ide_dma_start);
  495. /* returns 1 on error, 0 otherwise */
  496. int __ide_dma_end (ide_drive_t *drive)
  497. {
  498. ide_hwif_t *hwif = HWIF(drive);
  499. u8 dma_stat = 0, dma_cmd = 0;
  500. drive->waiting_for_dma = 0;
  501. /* get dma_command mode */
  502. dma_cmd = hwif->INB(hwif->dma_command);
  503. /* stop DMA */
  504. hwif->OUTB(dma_cmd&~1, hwif->dma_command);
  505. /* get DMA status */
  506. dma_stat = hwif->INB(hwif->dma_status);
  507. /* clear the INTR & ERROR bits */
  508. hwif->OUTB(dma_stat|6, hwif->dma_status);
  509. /* purge DMA mappings */
  510. ide_destroy_dmatable(drive);
  511. /* verify good DMA status */
  512. hwif->dma = 0;
  513. wmb();
  514. return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0;
  515. }
  516. EXPORT_SYMBOL(__ide_dma_end);
  517. /* returns 1 if dma irq issued, 0 otherwise */
  518. static int __ide_dma_test_irq(ide_drive_t *drive)
  519. {
  520. ide_hwif_t *hwif = HWIF(drive);
  521. u8 dma_stat = hwif->INB(hwif->dma_status);
  522. #if 0 /* do not set unless you know what you are doing */
  523. if (dma_stat & 4) {
  524. u8 stat = hwif->INB(IDE_STATUS_REG);
  525. hwif->OUTB(hwif->dma_status, dma_stat & 0xE4);
  526. }
  527. #endif
  528. /* return 1 if INTR asserted */
  529. if ((dma_stat & 4) == 4)
  530. return 1;
  531. if (!drive->waiting_for_dma)
  532. printk(KERN_WARNING "%s: (%s) called while not waiting\n",
  533. drive->name, __FUNCTION__);
  534. return 0;
  535. }
  536. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
  537. int __ide_dma_bad_drive (ide_drive_t *drive)
  538. {
  539. struct hd_driveid *id = drive->id;
  540. int blacklist = ide_in_drive_list(id, drive_blacklist);
  541. if (blacklist) {
  542. printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n",
  543. drive->name, id->model);
  544. return blacklist;
  545. }
  546. return 0;
  547. }
  548. EXPORT_SYMBOL(__ide_dma_bad_drive);
  549. static const u8 xfer_mode_bases[] = {
  550. XFER_UDMA_0,
  551. XFER_MW_DMA_0,
  552. XFER_SW_DMA_0,
  553. };
  554. static unsigned int ide_get_mode_mask(ide_drive_t *drive, u8 base, u8 req_mode)
  555. {
  556. struct hd_driveid *id = drive->id;
  557. ide_hwif_t *hwif = drive->hwif;
  558. unsigned int mask = 0;
  559. switch(base) {
  560. case XFER_UDMA_0:
  561. if ((id->field_valid & 4) == 0)
  562. break;
  563. if (hwif->udma_filter)
  564. mask = hwif->udma_filter(drive);
  565. else
  566. mask = hwif->ultra_mask;
  567. mask &= id->dma_ultra;
  568. /*
  569. * avoid false cable warning from eighty_ninty_three()
  570. */
  571. if (req_mode > XFER_UDMA_2) {
  572. if ((mask & 0x78) && (eighty_ninty_three(drive) == 0))
  573. mask &= 0x07;
  574. }
  575. break;
  576. case XFER_MW_DMA_0:
  577. if ((id->field_valid & 2) == 0)
  578. break;
  579. if (hwif->mdma_filter)
  580. mask = hwif->mdma_filter(drive);
  581. else
  582. mask = hwif->mwdma_mask;
  583. mask &= id->dma_mword;
  584. break;
  585. case XFER_SW_DMA_0:
  586. if (id->field_valid & 2) {
  587. mask = id->dma_1word & hwif->swdma_mask;
  588. } else if (id->tDMA) {
  589. /*
  590. * ide_fix_driveid() doesn't convert ->tDMA to the
  591. * CPU endianness so we need to do it here
  592. */
  593. u8 mode = le16_to_cpu(id->tDMA);
  594. /*
  595. * if the mode is valid convert it to the mask
  596. * (the maximum allowed mode is XFER_SW_DMA_2)
  597. */
  598. if (mode <= 2)
  599. mask = ((2 << mode) - 1) & hwif->swdma_mask;
  600. }
  601. break;
  602. default:
  603. BUG();
  604. break;
  605. }
  606. return mask;
  607. }
  608. /**
  609. * ide_find_dma_mode - compute DMA speed
  610. * @drive: IDE device
  611. * @req_mode: requested mode
  612. *
  613. * Checks the drive/host capabilities and finds the speed to use for
  614. * the DMA transfer. The speed is then limited by the requested mode.
  615. *
  616. * Returns 0 if the drive/host combination is incapable of DMA transfers
  617. * or if the requested mode is not a DMA mode.
  618. */
  619. u8 ide_find_dma_mode(ide_drive_t *drive, u8 req_mode)
  620. {
  621. ide_hwif_t *hwif = drive->hwif;
  622. unsigned int mask;
  623. int x, i;
  624. u8 mode = 0;
  625. if (drive->media != ide_disk && hwif->atapi_dma == 0)
  626. return 0;
  627. for (i = 0; i < ARRAY_SIZE(xfer_mode_bases); i++) {
  628. if (req_mode < xfer_mode_bases[i])
  629. continue;
  630. mask = ide_get_mode_mask(drive, xfer_mode_bases[i], req_mode);
  631. x = fls(mask) - 1;
  632. if (x >= 0) {
  633. mode = xfer_mode_bases[i] + x;
  634. break;
  635. }
  636. }
  637. if (hwif->chipset == ide_acorn && mode == 0) {
  638. /*
  639. * is this correct?
  640. */
  641. if (ide_dma_good_drive(drive) && drive->id->eide_dma_time < 150)
  642. mode = XFER_MW_DMA_1;
  643. }
  644. printk(KERN_DEBUG "%s: selected mode 0x%x\n", drive->name, mode);
  645. return min(mode, req_mode);
  646. }
  647. EXPORT_SYMBOL_GPL(ide_find_dma_mode);
  648. int ide_tune_dma(ide_drive_t *drive)
  649. {
  650. u8 speed;
  651. if ((drive->id->capability & 1) == 0 || drive->autodma == 0)
  652. return 0;
  653. /* consult the list of known "bad" drives */
  654. if (__ide_dma_bad_drive(drive))
  655. return 0;
  656. speed = ide_max_dma_mode(drive);
  657. if (!speed)
  658. return 0;
  659. if (drive->hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
  660. return 0;
  661. if (ide_set_dma_mode(drive, speed))
  662. return 0;
  663. return 1;
  664. }
  665. EXPORT_SYMBOL_GPL(ide_tune_dma);
  666. void ide_dma_verbose(ide_drive_t *drive)
  667. {
  668. struct hd_driveid *id = drive->id;
  669. ide_hwif_t *hwif = HWIF(drive);
  670. if (id->field_valid & 4) {
  671. if ((id->dma_ultra >> 8) && (id->dma_mword >> 8))
  672. goto bug_dma_off;
  673. if (id->dma_ultra & ((id->dma_ultra >> 8) & hwif->ultra_mask)) {
  674. if (((id->dma_ultra >> 11) & 0x1F) &&
  675. eighty_ninty_three(drive)) {
  676. if ((id->dma_ultra >> 15) & 1) {
  677. printk(", UDMA(mode 7)");
  678. } else if ((id->dma_ultra >> 14) & 1) {
  679. printk(", UDMA(133)");
  680. } else if ((id->dma_ultra >> 13) & 1) {
  681. printk(", UDMA(100)");
  682. } else if ((id->dma_ultra >> 12) & 1) {
  683. printk(", UDMA(66)");
  684. } else if ((id->dma_ultra >> 11) & 1) {
  685. printk(", UDMA(44)");
  686. } else
  687. goto mode_two;
  688. } else {
  689. mode_two:
  690. if ((id->dma_ultra >> 10) & 1) {
  691. printk(", UDMA(33)");
  692. } else if ((id->dma_ultra >> 9) & 1) {
  693. printk(", UDMA(25)");
  694. } else if ((id->dma_ultra >> 8) & 1) {
  695. printk(", UDMA(16)");
  696. }
  697. }
  698. } else {
  699. printk(", (U)DMA"); /* Can be BIOS-enabled! */
  700. }
  701. } else if (id->field_valid & 2) {
  702. if ((id->dma_mword >> 8) && (id->dma_1word >> 8))
  703. goto bug_dma_off;
  704. printk(", DMA");
  705. } else if (id->field_valid & 1) {
  706. goto bug_dma_off;
  707. }
  708. return;
  709. bug_dma_off:
  710. printk(", BUG DMA OFF");
  711. hwif->dma_off_quietly(drive);
  712. return;
  713. }
  714. EXPORT_SYMBOL(ide_dma_verbose);
  715. int ide_set_dma(ide_drive_t *drive)
  716. {
  717. ide_hwif_t *hwif = drive->hwif;
  718. int rc;
  719. rc = hwif->ide_dma_check(drive);
  720. switch(rc) {
  721. case -1: /* DMA needs to be disabled */
  722. hwif->dma_off_quietly(drive);
  723. return -1;
  724. case 0: /* DMA needs to be enabled */
  725. return hwif->ide_dma_on(drive);
  726. case 1: /* DMA setting cannot be changed */
  727. break;
  728. default:
  729. BUG();
  730. break;
  731. }
  732. return rc;
  733. }
  734. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  735. void ide_dma_lost_irq (ide_drive_t *drive)
  736. {
  737. printk("%s: DMA interrupt recovery\n", drive->name);
  738. }
  739. EXPORT_SYMBOL(ide_dma_lost_irq);
  740. void ide_dma_timeout (ide_drive_t *drive)
  741. {
  742. ide_hwif_t *hwif = HWIF(drive);
  743. printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
  744. if (hwif->ide_dma_test_irq(drive))
  745. return;
  746. hwif->ide_dma_end(drive);
  747. }
  748. EXPORT_SYMBOL(ide_dma_timeout);
  749. /*
  750. * Needed for allowing full modular support of ide-driver
  751. */
  752. static int ide_release_dma_engine(ide_hwif_t *hwif)
  753. {
  754. if (hwif->dmatable_cpu) {
  755. pci_free_consistent(hwif->pci_dev,
  756. PRD_ENTRIES * PRD_BYTES,
  757. hwif->dmatable_cpu,
  758. hwif->dmatable_dma);
  759. hwif->dmatable_cpu = NULL;
  760. }
  761. return 1;
  762. }
  763. static int ide_release_iomio_dma(ide_hwif_t *hwif)
  764. {
  765. release_region(hwif->dma_base, 8);
  766. if (hwif->extra_ports)
  767. release_region(hwif->extra_base, hwif->extra_ports);
  768. return 1;
  769. }
  770. /*
  771. * Needed for allowing full modular support of ide-driver
  772. */
  773. int ide_release_dma(ide_hwif_t *hwif)
  774. {
  775. ide_release_dma_engine(hwif);
  776. if (hwif->mmio)
  777. return 1;
  778. else
  779. return ide_release_iomio_dma(hwif);
  780. }
  781. static int ide_allocate_dma_engine(ide_hwif_t *hwif)
  782. {
  783. hwif->dmatable_cpu = pci_alloc_consistent(hwif->pci_dev,
  784. PRD_ENTRIES * PRD_BYTES,
  785. &hwif->dmatable_dma);
  786. if (hwif->dmatable_cpu)
  787. return 0;
  788. printk(KERN_ERR "%s: -- Error, unable to allocate DMA table.\n",
  789. hwif->cds->name);
  790. return 1;
  791. }
  792. static int ide_mapped_mmio_dma(ide_hwif_t *hwif, unsigned long base, unsigned int ports)
  793. {
  794. printk(KERN_INFO " %s: MMIO-DMA ", hwif->name);
  795. hwif->dma_base = base;
  796. if(hwif->mate)
  797. hwif->dma_master = (hwif->channel) ? hwif->mate->dma_base : base;
  798. else
  799. hwif->dma_master = base;
  800. return 0;
  801. }
  802. static int ide_iomio_dma(ide_hwif_t *hwif, unsigned long base, unsigned int ports)
  803. {
  804. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx",
  805. hwif->name, base, base + ports - 1);
  806. if (!request_region(base, ports, hwif->name)) {
  807. printk(" -- Error, ports in use.\n");
  808. return 1;
  809. }
  810. hwif->dma_base = base;
  811. if (hwif->cds->extra) {
  812. hwif->extra_base = base + (hwif->channel ? 8 : 16);
  813. if (!hwif->mate || !hwif->mate->extra_ports) {
  814. if (!request_region(hwif->extra_base,
  815. hwif->cds->extra, hwif->cds->name)) {
  816. printk(" -- Error, extra ports in use.\n");
  817. release_region(base, ports);
  818. return 1;
  819. }
  820. hwif->extra_ports = hwif->cds->extra;
  821. }
  822. }
  823. if(hwif->mate)
  824. hwif->dma_master = (hwif->channel) ? hwif->mate->dma_base:base;
  825. else
  826. hwif->dma_master = base;
  827. return 0;
  828. }
  829. static int ide_dma_iobase(ide_hwif_t *hwif, unsigned long base, unsigned int ports)
  830. {
  831. if (hwif->mmio)
  832. return ide_mapped_mmio_dma(hwif, base,ports);
  833. return ide_iomio_dma(hwif, base, ports);
  834. }
  835. /*
  836. * This can be called for a dynamically installed interface. Don't __init it
  837. */
  838. void ide_setup_dma (ide_hwif_t *hwif, unsigned long dma_base, unsigned int num_ports)
  839. {
  840. if (ide_dma_iobase(hwif, dma_base, num_ports))
  841. return;
  842. if (ide_allocate_dma_engine(hwif)) {
  843. ide_release_dma(hwif);
  844. return;
  845. }
  846. if (!(hwif->dma_command))
  847. hwif->dma_command = hwif->dma_base;
  848. if (!(hwif->dma_vendor1))
  849. hwif->dma_vendor1 = (hwif->dma_base + 1);
  850. if (!(hwif->dma_status))
  851. hwif->dma_status = (hwif->dma_base + 2);
  852. if (!(hwif->dma_vendor3))
  853. hwif->dma_vendor3 = (hwif->dma_base + 3);
  854. if (!(hwif->dma_prdtable))
  855. hwif->dma_prdtable = (hwif->dma_base + 4);
  856. if (!hwif->dma_off_quietly)
  857. hwif->dma_off_quietly = &ide_dma_off_quietly;
  858. if (!hwif->dma_host_off)
  859. hwif->dma_host_off = &ide_dma_host_off;
  860. if (!hwif->ide_dma_on)
  861. hwif->ide_dma_on = &__ide_dma_on;
  862. if (!hwif->dma_host_on)
  863. hwif->dma_host_on = &ide_dma_host_on;
  864. if (!hwif->ide_dma_check)
  865. hwif->ide_dma_check = &config_drive_for_dma;
  866. if (!hwif->dma_setup)
  867. hwif->dma_setup = &ide_dma_setup;
  868. if (!hwif->dma_exec_cmd)
  869. hwif->dma_exec_cmd = &ide_dma_exec_cmd;
  870. if (!hwif->dma_start)
  871. hwif->dma_start = &ide_dma_start;
  872. if (!hwif->ide_dma_end)
  873. hwif->ide_dma_end = &__ide_dma_end;
  874. if (!hwif->ide_dma_test_irq)
  875. hwif->ide_dma_test_irq = &__ide_dma_test_irq;
  876. if (!hwif->dma_timeout)
  877. hwif->dma_timeout = &ide_dma_timeout;
  878. if (!hwif->dma_lost_irq)
  879. hwif->dma_lost_irq = &ide_dma_lost_irq;
  880. if (hwif->chipset != ide_trm290) {
  881. u8 dma_stat = hwif->INB(hwif->dma_status);
  882. printk(", BIOS settings: %s:%s, %s:%s",
  883. hwif->drives[0].name, (dma_stat & 0x20) ? "DMA" : "pio",
  884. hwif->drives[1].name, (dma_stat & 0x40) ? "DMA" : "pio");
  885. }
  886. printk("\n");
  887. BUG_ON(!hwif->dma_master);
  888. }
  889. EXPORT_SYMBOL_GPL(ide_setup_dma);
  890. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */