ide-dma.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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" , "ALL" },
  89. { "CONNER CTMA 4000" , "ALL" },
  90. { "CONNER CTT8000-A" , "ALL" },
  91. { "ST34342A" , "ALL" },
  92. { NULL , NULL }
  93. };
  94. static const struct drive_list_entry drive_blacklist [] = {
  95. { "WDC AC11000H" , "ALL" },
  96. { "WDC AC22100H" , "ALL" },
  97. { "WDC AC32500H" , "ALL" },
  98. { "WDC AC33100H" , "ALL" },
  99. { "WDC AC31600H" , "ALL" },
  100. { "WDC AC32100H" , "24.09P07" },
  101. { "WDC AC23200L" , "21.10N21" },
  102. { "Compaq CRD-8241B" , "ALL" },
  103. { "CRD-8400B" , "ALL" },
  104. { "CRD-8480B", "ALL" },
  105. { "CRD-8482B", "ALL" },
  106. { "CRD-84" , "ALL" },
  107. { "SanDisk SDP3B" , "ALL" },
  108. { "SanDisk SDP3B-64" , "ALL" },
  109. { "SANYO CD-ROM CRD" , "ALL" },
  110. { "HITACHI CDR-8" , "ALL" },
  111. { "HITACHI CDR-8335" , "ALL" },
  112. { "HITACHI CDR-8435" , "ALL" },
  113. { "Toshiba CD-ROM XM-6202B" , "ALL" },
  114. { "CD-532E-A" , "ALL" },
  115. { "E-IDE CD-ROM CR-840", "ALL" },
  116. { "CD-ROM Drive/F5A", "ALL" },
  117. { "WPI CDD-820", "ALL" },
  118. { "SAMSUNG CD-ROM SC-148C", "ALL" },
  119. { "SAMSUNG CD-ROM SC", "ALL" },
  120. { "SanDisk SDP3B-64" , "ALL" },
  121. { "ATAPI CD-ROM DRIVE 40X MAXIMUM", "ALL" },
  122. { "_NEC DV5800A", "ALL" },
  123. { NULL , NULL }
  124. };
  125. /**
  126. * ide_in_drive_list - look for drive in black/white list
  127. * @id: drive identifier
  128. * @drive_table: list to inspect
  129. *
  130. * Look for a drive in the blacklist and the whitelist tables
  131. * Returns 1 if the drive is found in the table.
  132. */
  133. int ide_in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table)
  134. {
  135. for ( ; drive_table->id_model ; drive_table++)
  136. if ((!strcmp(drive_table->id_model, id->model)) &&
  137. ((strstr(id->fw_rev, drive_table->id_firmware)) ||
  138. (!strcmp(drive_table->id_firmware, "ALL"))))
  139. return 1;
  140. return 0;
  141. }
  142. /**
  143. * ide_dma_intr - IDE DMA interrupt handler
  144. * @drive: the drive the interrupt is for
  145. *
  146. * Handle an interrupt completing a read/write DMA transfer on an
  147. * IDE device
  148. */
  149. ide_startstop_t ide_dma_intr (ide_drive_t *drive)
  150. {
  151. u8 stat = 0, dma_stat = 0;
  152. dma_stat = HWIF(drive)->ide_dma_end(drive);
  153. stat = HWIF(drive)->INB(IDE_STATUS_REG); /* get drive status */
  154. if (OK_STAT(stat,DRIVE_READY,drive->bad_wstat|DRQ_STAT)) {
  155. if (!dma_stat) {
  156. struct request *rq = HWGROUP(drive)->rq;
  157. if (rq->rq_disk) {
  158. ide_driver_t *drv;
  159. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  160. drv->end_request(drive, 1, rq->nr_sectors);
  161. } else
  162. ide_end_request(drive, 1, rq->nr_sectors);
  163. return ide_stopped;
  164. }
  165. printk(KERN_ERR "%s: dma_intr: bad DMA status (dma_stat=%x)\n",
  166. drive->name, dma_stat);
  167. }
  168. return ide_error(drive, "dma_intr", stat);
  169. }
  170. EXPORT_SYMBOL_GPL(ide_dma_intr);
  171. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  172. /**
  173. * ide_build_sglist - map IDE scatter gather for DMA I/O
  174. * @drive: the drive to build the DMA table for
  175. * @rq: the request holding the sg list
  176. *
  177. * Perform the PCI mapping magic necessary to access the source or
  178. * target buffers of a request via PCI DMA. The lower layers of the
  179. * kernel provide the necessary cache management so that we can
  180. * operate in a portable fashion
  181. */
  182. int ide_build_sglist(ide_drive_t *drive, struct request *rq)
  183. {
  184. ide_hwif_t *hwif = HWIF(drive);
  185. struct scatterlist *sg = hwif->sg_table;
  186. BUG_ON((rq->cmd_type == REQ_TYPE_ATA_TASKFILE) && rq->nr_sectors > 256);
  187. ide_map_sg(drive, rq);
  188. if (rq_data_dir(rq) == READ)
  189. hwif->sg_dma_direction = PCI_DMA_FROMDEVICE;
  190. else
  191. hwif->sg_dma_direction = PCI_DMA_TODEVICE;
  192. return pci_map_sg(hwif->pci_dev, sg, hwif->sg_nents, hwif->sg_dma_direction);
  193. }
  194. EXPORT_SYMBOL_GPL(ide_build_sglist);
  195. /**
  196. * ide_build_dmatable - build IDE DMA table
  197. *
  198. * ide_build_dmatable() prepares a dma request. We map the command
  199. * to get the pci bus addresses of the buffers and then build up
  200. * the PRD table that the IDE layer wants to be fed. The code
  201. * knows about the 64K wrap bug in the CS5530.
  202. *
  203. * Returns the number of built PRD entries if all went okay,
  204. * returns 0 otherwise.
  205. *
  206. * May also be invoked from trm290.c
  207. */
  208. int ide_build_dmatable (ide_drive_t *drive, struct request *rq)
  209. {
  210. ide_hwif_t *hwif = HWIF(drive);
  211. unsigned int *table = hwif->dmatable_cpu;
  212. unsigned int is_trm290 = (hwif->chipset == ide_trm290) ? 1 : 0;
  213. unsigned int count = 0;
  214. int i;
  215. struct scatterlist *sg;
  216. hwif->sg_nents = i = ide_build_sglist(drive, rq);
  217. if (!i)
  218. return 0;
  219. sg = hwif->sg_table;
  220. while (i) {
  221. u32 cur_addr;
  222. u32 cur_len;
  223. cur_addr = sg_dma_address(sg);
  224. cur_len = sg_dma_len(sg);
  225. /*
  226. * Fill in the dma table, without crossing any 64kB boundaries.
  227. * Most hardware requires 16-bit alignment of all blocks,
  228. * but the trm290 requires 32-bit alignment.
  229. */
  230. while (cur_len) {
  231. if (count++ >= PRD_ENTRIES) {
  232. printk(KERN_ERR "%s: DMA table too small\n", drive->name);
  233. goto use_pio_instead;
  234. } else {
  235. u32 xcount, bcount = 0x10000 - (cur_addr & 0xffff);
  236. if (bcount > cur_len)
  237. bcount = cur_len;
  238. *table++ = cpu_to_le32(cur_addr);
  239. xcount = bcount & 0xffff;
  240. if (is_trm290)
  241. xcount = ((xcount >> 2) - 1) << 16;
  242. if (xcount == 0x0000) {
  243. /*
  244. * Most chipsets correctly interpret a length of 0x0000 as 64KB,
  245. * but at least one (e.g. CS5530) misinterprets it as zero (!).
  246. * So here we break the 64KB entry into two 32KB entries instead.
  247. */
  248. if (count++ >= PRD_ENTRIES) {
  249. printk(KERN_ERR "%s: DMA table too small\n", drive->name);
  250. goto use_pio_instead;
  251. }
  252. *table++ = cpu_to_le32(0x8000);
  253. *table++ = cpu_to_le32(cur_addr + 0x8000);
  254. xcount = 0x8000;
  255. }
  256. *table++ = cpu_to_le32(xcount);
  257. cur_addr += bcount;
  258. cur_len -= bcount;
  259. }
  260. }
  261. sg++;
  262. i--;
  263. }
  264. if (count) {
  265. if (!is_trm290)
  266. *--table |= cpu_to_le32(0x80000000);
  267. return count;
  268. }
  269. printk(KERN_ERR "%s: empty DMA table?\n", drive->name);
  270. use_pio_instead:
  271. pci_unmap_sg(hwif->pci_dev,
  272. hwif->sg_table,
  273. hwif->sg_nents,
  274. hwif->sg_dma_direction);
  275. return 0; /* revert to PIO for this request */
  276. }
  277. EXPORT_SYMBOL_GPL(ide_build_dmatable);
  278. /**
  279. * ide_destroy_dmatable - clean up DMA mapping
  280. * @drive: The drive to unmap
  281. *
  282. * Teardown mappings after DMA has completed. This must be called
  283. * after the completion of each use of ide_build_dmatable and before
  284. * the next use of ide_build_dmatable. Failure to do so will cause
  285. * an oops as only one mapping can be live for each target at a given
  286. * time.
  287. */
  288. void ide_destroy_dmatable (ide_drive_t *drive)
  289. {
  290. struct pci_dev *dev = HWIF(drive)->pci_dev;
  291. struct scatterlist *sg = HWIF(drive)->sg_table;
  292. int nents = HWIF(drive)->sg_nents;
  293. pci_unmap_sg(dev, sg, nents, HWIF(drive)->sg_dma_direction);
  294. }
  295. EXPORT_SYMBOL_GPL(ide_destroy_dmatable);
  296. /**
  297. * config_drive_for_dma - attempt to activate IDE DMA
  298. * @drive: the drive to place in DMA mode
  299. *
  300. * If the drive supports at least mode 2 DMA or UDMA of any kind
  301. * then attempt to place it into DMA mode. Drives that are known to
  302. * support DMA but predate the DMA properties or that are known
  303. * to have DMA handling bugs are also set up appropriately based
  304. * on the good/bad drive lists.
  305. */
  306. static int config_drive_for_dma (ide_drive_t *drive)
  307. {
  308. struct hd_driveid *id = drive->id;
  309. if ((id->capability & 1) && drive->hwif->autodma) {
  310. /*
  311. * Enable DMA on any drive that has
  312. * UltraDMA (mode 0/1/2/3/4/5/6) enabled
  313. */
  314. if ((id->field_valid & 4) && ((id->dma_ultra >> 8) & 0x7f))
  315. return 0;
  316. /*
  317. * Enable DMA on any drive that has mode2 DMA
  318. * (multi or single) enabled
  319. */
  320. if (id->field_valid & 2) /* regular DMA */
  321. if ((id->dma_mword & 0x404) == 0x404 ||
  322. (id->dma_1word & 0x404) == 0x404)
  323. return 0;
  324. /* Consult the list of known "good" drives */
  325. if (__ide_dma_good_drive(drive))
  326. return 0;
  327. }
  328. return -1;
  329. }
  330. /**
  331. * dma_timer_expiry - handle a DMA timeout
  332. * @drive: Drive that timed out
  333. *
  334. * An IDE DMA transfer timed out. In the event of an error we ask
  335. * the driver to resolve the problem, if a DMA transfer is still
  336. * in progress we continue to wait (arguably we need to add a
  337. * secondary 'I don't care what the drive thinks' timeout here)
  338. * Finally if we have an interrupt we let it complete the I/O.
  339. * But only one time - we clear expiry and if it's still not
  340. * completed after WAIT_CMD, we error and retry in PIO.
  341. * This can occur if an interrupt is lost or due to hang or bugs.
  342. */
  343. static int dma_timer_expiry (ide_drive_t *drive)
  344. {
  345. ide_hwif_t *hwif = HWIF(drive);
  346. u8 dma_stat = hwif->INB(hwif->dma_status);
  347. printk(KERN_WARNING "%s: dma_timer_expiry: dma status == 0x%02x\n",
  348. drive->name, dma_stat);
  349. if ((dma_stat & 0x18) == 0x18) /* BUSY Stupid Early Timer !! */
  350. return WAIT_CMD;
  351. HWGROUP(drive)->expiry = NULL; /* one free ride for now */
  352. /* 1 dmaing, 2 error, 4 intr */
  353. if (dma_stat & 2) /* ERROR */
  354. return -1;
  355. if (dma_stat & 1) /* DMAing */
  356. return WAIT_CMD;
  357. if (dma_stat & 4) /* Got an Interrupt */
  358. return WAIT_CMD;
  359. return 0; /* Status is unknown -- reset the bus */
  360. }
  361. /**
  362. * ide_dma_host_off - Generic DMA kill
  363. * @drive: drive to control
  364. *
  365. * Perform the generic IDE controller DMA off operation. This
  366. * works for most IDE bus mastering controllers
  367. */
  368. void ide_dma_host_off(ide_drive_t *drive)
  369. {
  370. ide_hwif_t *hwif = HWIF(drive);
  371. u8 unit = (drive->select.b.unit & 0x01);
  372. u8 dma_stat = hwif->INB(hwif->dma_status);
  373. hwif->OUTB((dma_stat & ~(1<<(5+unit))), hwif->dma_status);
  374. }
  375. EXPORT_SYMBOL(ide_dma_host_off);
  376. /**
  377. * ide_dma_off_quietly - Generic DMA kill
  378. * @drive: drive to control
  379. *
  380. * Turn off the current DMA on this IDE controller.
  381. */
  382. void ide_dma_off_quietly(ide_drive_t *drive)
  383. {
  384. drive->using_dma = 0;
  385. ide_toggle_bounce(drive, 0);
  386. drive->hwif->dma_host_off(drive);
  387. }
  388. EXPORT_SYMBOL(ide_dma_off_quietly);
  389. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
  390. /**
  391. * ide_dma_off - disable DMA on a device
  392. * @drive: drive to disable DMA on
  393. *
  394. * Disable IDE DMA for a device on this IDE controller.
  395. * Inform the user that DMA has been disabled.
  396. */
  397. void ide_dma_off(ide_drive_t *drive)
  398. {
  399. printk(KERN_INFO "%s: DMA disabled\n", drive->name);
  400. drive->hwif->dma_off_quietly(drive);
  401. }
  402. EXPORT_SYMBOL(ide_dma_off);
  403. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  404. /**
  405. * ide_dma_host_on - Enable DMA on a host
  406. * @drive: drive to enable for DMA
  407. *
  408. * Enable DMA on an IDE controller following generic bus mastering
  409. * IDE controller behaviour
  410. */
  411. void ide_dma_host_on(ide_drive_t *drive)
  412. {
  413. if (drive->using_dma) {
  414. ide_hwif_t *hwif = HWIF(drive);
  415. u8 unit = (drive->select.b.unit & 0x01);
  416. u8 dma_stat = hwif->INB(hwif->dma_status);
  417. hwif->OUTB((dma_stat|(1<<(5+unit))), hwif->dma_status);
  418. }
  419. }
  420. EXPORT_SYMBOL(ide_dma_host_on);
  421. /**
  422. * __ide_dma_on - Enable DMA on a device
  423. * @drive: drive to enable DMA on
  424. *
  425. * Enable IDE DMA for a device on this IDE controller.
  426. */
  427. int __ide_dma_on (ide_drive_t *drive)
  428. {
  429. /* consult the list of known "bad" drives */
  430. if (__ide_dma_bad_drive(drive))
  431. return 1;
  432. drive->using_dma = 1;
  433. ide_toggle_bounce(drive, 1);
  434. drive->hwif->dma_host_on(drive);
  435. return 0;
  436. }
  437. EXPORT_SYMBOL(__ide_dma_on);
  438. /**
  439. * __ide_dma_check - check DMA setup
  440. * @drive: drive to check
  441. *
  442. * Don't use - due for extermination
  443. */
  444. int __ide_dma_check (ide_drive_t *drive)
  445. {
  446. return config_drive_for_dma(drive);
  447. }
  448. EXPORT_SYMBOL(__ide_dma_check);
  449. /**
  450. * ide_dma_setup - begin a DMA phase
  451. * @drive: target device
  452. *
  453. * Build an IDE DMA PRD (IDE speak for scatter gather table)
  454. * and then set up the DMA transfer registers for a device
  455. * that follows generic IDE PCI DMA behaviour. Controllers can
  456. * override this function if they need to
  457. *
  458. * Returns 0 on success. If a PIO fallback is required then 1
  459. * is returned.
  460. */
  461. int ide_dma_setup(ide_drive_t *drive)
  462. {
  463. ide_hwif_t *hwif = drive->hwif;
  464. struct request *rq = HWGROUP(drive)->rq;
  465. unsigned int reading;
  466. u8 dma_stat;
  467. if (rq_data_dir(rq))
  468. reading = 0;
  469. else
  470. reading = 1 << 3;
  471. /* fall back to pio! */
  472. if (!ide_build_dmatable(drive, rq)) {
  473. ide_map_sg(drive, rq);
  474. return 1;
  475. }
  476. /* PRD table */
  477. if (hwif->mmio)
  478. writel(hwif->dmatable_dma, (void __iomem *)hwif->dma_prdtable);
  479. else
  480. outl(hwif->dmatable_dma, hwif->dma_prdtable);
  481. /* specify r/w */
  482. hwif->OUTB(reading, hwif->dma_command);
  483. /* read dma_status for INTR & ERROR flags */
  484. dma_stat = hwif->INB(hwif->dma_status);
  485. /* clear INTR & ERROR flags */
  486. hwif->OUTB(dma_stat|6, hwif->dma_status);
  487. drive->waiting_for_dma = 1;
  488. return 0;
  489. }
  490. EXPORT_SYMBOL_GPL(ide_dma_setup);
  491. static void ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
  492. {
  493. /* issue cmd to drive */
  494. ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, dma_timer_expiry);
  495. }
  496. void ide_dma_start(ide_drive_t *drive)
  497. {
  498. ide_hwif_t *hwif = HWIF(drive);
  499. u8 dma_cmd = hwif->INB(hwif->dma_command);
  500. /* Note that this is done *after* the cmd has
  501. * been issued to the drive, as per the BM-IDE spec.
  502. * The Promise Ultra33 doesn't work correctly when
  503. * we do this part before issuing the drive cmd.
  504. */
  505. /* start DMA */
  506. hwif->OUTB(dma_cmd|1, hwif->dma_command);
  507. hwif->dma = 1;
  508. wmb();
  509. }
  510. EXPORT_SYMBOL_GPL(ide_dma_start);
  511. /* returns 1 on error, 0 otherwise */
  512. int __ide_dma_end (ide_drive_t *drive)
  513. {
  514. ide_hwif_t *hwif = HWIF(drive);
  515. u8 dma_stat = 0, dma_cmd = 0;
  516. drive->waiting_for_dma = 0;
  517. /* get dma_command mode */
  518. dma_cmd = hwif->INB(hwif->dma_command);
  519. /* stop DMA */
  520. hwif->OUTB(dma_cmd&~1, hwif->dma_command);
  521. /* get DMA status */
  522. dma_stat = hwif->INB(hwif->dma_status);
  523. /* clear the INTR & ERROR bits */
  524. hwif->OUTB(dma_stat|6, hwif->dma_status);
  525. /* purge DMA mappings */
  526. ide_destroy_dmatable(drive);
  527. /* verify good DMA status */
  528. hwif->dma = 0;
  529. wmb();
  530. return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0;
  531. }
  532. EXPORT_SYMBOL(__ide_dma_end);
  533. /* returns 1 if dma irq issued, 0 otherwise */
  534. static int __ide_dma_test_irq(ide_drive_t *drive)
  535. {
  536. ide_hwif_t *hwif = HWIF(drive);
  537. u8 dma_stat = hwif->INB(hwif->dma_status);
  538. #if 0 /* do not set unless you know what you are doing */
  539. if (dma_stat & 4) {
  540. u8 stat = hwif->INB(IDE_STATUS_REG);
  541. hwif->OUTB(hwif->dma_status, dma_stat & 0xE4);
  542. }
  543. #endif
  544. /* return 1 if INTR asserted */
  545. if ((dma_stat & 4) == 4)
  546. return 1;
  547. if (!drive->waiting_for_dma)
  548. printk(KERN_WARNING "%s: (%s) called while not waiting\n",
  549. drive->name, __FUNCTION__);
  550. return 0;
  551. }
  552. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
  553. int __ide_dma_bad_drive (ide_drive_t *drive)
  554. {
  555. struct hd_driveid *id = drive->id;
  556. int blacklist = ide_in_drive_list(id, drive_blacklist);
  557. if (blacklist) {
  558. printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n",
  559. drive->name, id->model);
  560. return blacklist;
  561. }
  562. return 0;
  563. }
  564. EXPORT_SYMBOL(__ide_dma_bad_drive);
  565. int __ide_dma_good_drive (ide_drive_t *drive)
  566. {
  567. struct hd_driveid *id = drive->id;
  568. return ide_in_drive_list(id, drive_whitelist);
  569. }
  570. EXPORT_SYMBOL(__ide_dma_good_drive);
  571. static const u8 xfer_mode_bases[] = {
  572. XFER_UDMA_0,
  573. XFER_MW_DMA_0,
  574. XFER_SW_DMA_0,
  575. };
  576. static unsigned int ide_get_mode_mask(ide_drive_t *drive, u8 base)
  577. {
  578. struct hd_driveid *id = drive->id;
  579. ide_hwif_t *hwif = drive->hwif;
  580. unsigned int mask = 0;
  581. switch(base) {
  582. case XFER_UDMA_0:
  583. if ((id->field_valid & 4) == 0)
  584. break;
  585. mask = id->dma_ultra & hwif->ultra_mask;
  586. if (hwif->udma_filter)
  587. mask &= hwif->udma_filter(drive);
  588. if ((mask & 0x78) && (eighty_ninty_three(drive) == 0))
  589. mask &= 0x07;
  590. break;
  591. case XFER_MW_DMA_0:
  592. if (id->field_valid & 2)
  593. mask = id->dma_mword & hwif->mwdma_mask;
  594. break;
  595. case XFER_SW_DMA_0:
  596. if (id->field_valid & 2)
  597. mask = id->dma_1word & hwif->swdma_mask;
  598. break;
  599. default:
  600. BUG();
  601. break;
  602. }
  603. return mask;
  604. }
  605. /**
  606. * ide_max_dma_mode - compute DMA speed
  607. * @drive: IDE device
  608. *
  609. * Checks the drive capabilities and returns the speed to use
  610. * for the DMA transfer. Returns 0 if the drive is incapable
  611. * of DMA transfers.
  612. */
  613. u8 ide_max_dma_mode(ide_drive_t *drive)
  614. {
  615. ide_hwif_t *hwif = drive->hwif;
  616. unsigned int mask;
  617. int x, i;
  618. u8 mode = 0;
  619. if (drive->media != ide_disk && hwif->atapi_dma == 0)
  620. return 0;
  621. for (i = 0; i < ARRAY_SIZE(xfer_mode_bases); i++) {
  622. mask = ide_get_mode_mask(drive, xfer_mode_bases[i]);
  623. x = fls(mask) - 1;
  624. if (x >= 0) {
  625. mode = xfer_mode_bases[i] + x;
  626. break;
  627. }
  628. }
  629. printk(KERN_DEBUG "%s: selected mode 0x%x\n", drive->name, mode);
  630. return mode;
  631. }
  632. EXPORT_SYMBOL_GPL(ide_max_dma_mode);
  633. int ide_tune_dma(ide_drive_t *drive)
  634. {
  635. u8 speed;
  636. if ((drive->id->capability & 1) == 0 || drive->autodma == 0)
  637. return 0;
  638. /* consult the list of known "bad" drives */
  639. if (__ide_dma_bad_drive(drive))
  640. return 0;
  641. speed = ide_max_dma_mode(drive);
  642. if (!speed)
  643. return 0;
  644. if (drive->hwif->speedproc(drive, speed))
  645. return 0;
  646. return 1;
  647. }
  648. EXPORT_SYMBOL_GPL(ide_tune_dma);
  649. void ide_dma_verbose(ide_drive_t *drive)
  650. {
  651. struct hd_driveid *id = drive->id;
  652. ide_hwif_t *hwif = HWIF(drive);
  653. if (id->field_valid & 4) {
  654. if ((id->dma_ultra >> 8) && (id->dma_mword >> 8))
  655. goto bug_dma_off;
  656. if (id->dma_ultra & ((id->dma_ultra >> 8) & hwif->ultra_mask)) {
  657. if (((id->dma_ultra >> 11) & 0x1F) &&
  658. eighty_ninty_three(drive)) {
  659. if ((id->dma_ultra >> 15) & 1) {
  660. printk(", UDMA(mode 7)");
  661. } else if ((id->dma_ultra >> 14) & 1) {
  662. printk(", UDMA(133)");
  663. } else if ((id->dma_ultra >> 13) & 1) {
  664. printk(", UDMA(100)");
  665. } else if ((id->dma_ultra >> 12) & 1) {
  666. printk(", UDMA(66)");
  667. } else if ((id->dma_ultra >> 11) & 1) {
  668. printk(", UDMA(44)");
  669. } else
  670. goto mode_two;
  671. } else {
  672. mode_two:
  673. if ((id->dma_ultra >> 10) & 1) {
  674. printk(", UDMA(33)");
  675. } else if ((id->dma_ultra >> 9) & 1) {
  676. printk(", UDMA(25)");
  677. } else if ((id->dma_ultra >> 8) & 1) {
  678. printk(", UDMA(16)");
  679. }
  680. }
  681. } else {
  682. printk(", (U)DMA"); /* Can be BIOS-enabled! */
  683. }
  684. } else if (id->field_valid & 2) {
  685. if ((id->dma_mword >> 8) && (id->dma_1word >> 8))
  686. goto bug_dma_off;
  687. printk(", DMA");
  688. } else if (id->field_valid & 1) {
  689. goto bug_dma_off;
  690. }
  691. return;
  692. bug_dma_off:
  693. printk(", BUG DMA OFF");
  694. hwif->dma_off_quietly(drive);
  695. return;
  696. }
  697. EXPORT_SYMBOL(ide_dma_verbose);
  698. int ide_set_dma(ide_drive_t *drive)
  699. {
  700. ide_hwif_t *hwif = drive->hwif;
  701. int rc;
  702. rc = hwif->ide_dma_check(drive);
  703. switch(rc) {
  704. case -1: /* DMA needs to be disabled */
  705. hwif->dma_off_quietly(drive);
  706. return -1;
  707. case 0: /* DMA needs to be enabled */
  708. return hwif->ide_dma_on(drive);
  709. case 1: /* DMA setting cannot be changed */
  710. break;
  711. default:
  712. BUG();
  713. break;
  714. }
  715. return rc;
  716. }
  717. EXPORT_SYMBOL_GPL(ide_set_dma);
  718. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  719. int __ide_dma_lostirq (ide_drive_t *drive)
  720. {
  721. printk("%s: DMA interrupt recovery\n", drive->name);
  722. return 1;
  723. }
  724. EXPORT_SYMBOL(__ide_dma_lostirq);
  725. int __ide_dma_timeout (ide_drive_t *drive)
  726. {
  727. printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
  728. if (HWIF(drive)->ide_dma_test_irq(drive))
  729. return 0;
  730. return HWIF(drive)->ide_dma_end(drive);
  731. }
  732. EXPORT_SYMBOL(__ide_dma_timeout);
  733. /*
  734. * Needed for allowing full modular support of ide-driver
  735. */
  736. static int ide_release_dma_engine(ide_hwif_t *hwif)
  737. {
  738. if (hwif->dmatable_cpu) {
  739. pci_free_consistent(hwif->pci_dev,
  740. PRD_ENTRIES * PRD_BYTES,
  741. hwif->dmatable_cpu,
  742. hwif->dmatable_dma);
  743. hwif->dmatable_cpu = NULL;
  744. }
  745. return 1;
  746. }
  747. static int ide_release_iomio_dma(ide_hwif_t *hwif)
  748. {
  749. release_region(hwif->dma_base, 8);
  750. if (hwif->extra_ports)
  751. release_region(hwif->extra_base, hwif->extra_ports);
  752. return 1;
  753. }
  754. /*
  755. * Needed for allowing full modular support of ide-driver
  756. */
  757. int ide_release_dma(ide_hwif_t *hwif)
  758. {
  759. ide_release_dma_engine(hwif);
  760. if (hwif->mmio)
  761. return 1;
  762. else
  763. return ide_release_iomio_dma(hwif);
  764. }
  765. static int ide_allocate_dma_engine(ide_hwif_t *hwif)
  766. {
  767. hwif->dmatable_cpu = pci_alloc_consistent(hwif->pci_dev,
  768. PRD_ENTRIES * PRD_BYTES,
  769. &hwif->dmatable_dma);
  770. if (hwif->dmatable_cpu)
  771. return 0;
  772. printk(KERN_ERR "%s: -- Error, unable to allocate DMA table.\n",
  773. hwif->cds->name);
  774. return 1;
  775. }
  776. static int ide_mapped_mmio_dma(ide_hwif_t *hwif, unsigned long base, unsigned int ports)
  777. {
  778. printk(KERN_INFO " %s: MMIO-DMA ", hwif->name);
  779. hwif->dma_base = base;
  780. if(hwif->mate)
  781. hwif->dma_master = (hwif->channel) ? hwif->mate->dma_base : base;
  782. else
  783. hwif->dma_master = base;
  784. return 0;
  785. }
  786. static int ide_iomio_dma(ide_hwif_t *hwif, unsigned long base, unsigned int ports)
  787. {
  788. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx",
  789. hwif->name, base, base + ports - 1);
  790. if (!request_region(base, ports, hwif->name)) {
  791. printk(" -- Error, ports in use.\n");
  792. return 1;
  793. }
  794. hwif->dma_base = base;
  795. if (hwif->cds->extra) {
  796. hwif->extra_base = base + (hwif->channel ? 8 : 16);
  797. if (!hwif->mate || !hwif->mate->extra_ports) {
  798. if (!request_region(hwif->extra_base,
  799. hwif->cds->extra, hwif->cds->name)) {
  800. printk(" -- Error, extra ports in use.\n");
  801. release_region(base, ports);
  802. return 1;
  803. }
  804. hwif->extra_ports = hwif->cds->extra;
  805. }
  806. }
  807. if(hwif->mate)
  808. hwif->dma_master = (hwif->channel) ? hwif->mate->dma_base:base;
  809. else
  810. hwif->dma_master = base;
  811. return 0;
  812. }
  813. static int ide_dma_iobase(ide_hwif_t *hwif, unsigned long base, unsigned int ports)
  814. {
  815. if (hwif->mmio)
  816. return ide_mapped_mmio_dma(hwif, base,ports);
  817. return ide_iomio_dma(hwif, base, ports);
  818. }
  819. /*
  820. * This can be called for a dynamically installed interface. Don't __init it
  821. */
  822. void ide_setup_dma (ide_hwif_t *hwif, unsigned long dma_base, unsigned int num_ports)
  823. {
  824. if (ide_dma_iobase(hwif, dma_base, num_ports))
  825. return;
  826. if (ide_allocate_dma_engine(hwif)) {
  827. ide_release_dma(hwif);
  828. return;
  829. }
  830. if (!(hwif->dma_command))
  831. hwif->dma_command = hwif->dma_base;
  832. if (!(hwif->dma_vendor1))
  833. hwif->dma_vendor1 = (hwif->dma_base + 1);
  834. if (!(hwif->dma_status))
  835. hwif->dma_status = (hwif->dma_base + 2);
  836. if (!(hwif->dma_vendor3))
  837. hwif->dma_vendor3 = (hwif->dma_base + 3);
  838. if (!(hwif->dma_prdtable))
  839. hwif->dma_prdtable = (hwif->dma_base + 4);
  840. if (!hwif->dma_off_quietly)
  841. hwif->dma_off_quietly = &ide_dma_off_quietly;
  842. if (!hwif->dma_host_off)
  843. hwif->dma_host_off = &ide_dma_host_off;
  844. if (!hwif->ide_dma_on)
  845. hwif->ide_dma_on = &__ide_dma_on;
  846. if (!hwif->dma_host_on)
  847. hwif->dma_host_on = &ide_dma_host_on;
  848. if (!hwif->ide_dma_check)
  849. hwif->ide_dma_check = &__ide_dma_check;
  850. if (!hwif->dma_setup)
  851. hwif->dma_setup = &ide_dma_setup;
  852. if (!hwif->dma_exec_cmd)
  853. hwif->dma_exec_cmd = &ide_dma_exec_cmd;
  854. if (!hwif->dma_start)
  855. hwif->dma_start = &ide_dma_start;
  856. if (!hwif->ide_dma_end)
  857. hwif->ide_dma_end = &__ide_dma_end;
  858. if (!hwif->ide_dma_test_irq)
  859. hwif->ide_dma_test_irq = &__ide_dma_test_irq;
  860. if (!hwif->ide_dma_timeout)
  861. hwif->ide_dma_timeout = &__ide_dma_timeout;
  862. if (!hwif->ide_dma_lostirq)
  863. hwif->ide_dma_lostirq = &__ide_dma_lostirq;
  864. if (hwif->chipset != ide_trm290) {
  865. u8 dma_stat = hwif->INB(hwif->dma_status);
  866. printk(", BIOS settings: %s:%s, %s:%s",
  867. hwif->drives[0].name, (dma_stat & 0x20) ? "DMA" : "pio",
  868. hwif->drives[1].name, (dma_stat & 0x40) ? "DMA" : "pio");
  869. }
  870. printk("\n");
  871. BUG_ON(!hwif->dma_master);
  872. }
  873. EXPORT_SYMBOL_GPL(ide_setup_dma);
  874. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */