ide-dma.c 25 KB

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