ide-dma.c 26 KB

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