ide-dma.c 27 KB

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