ide-dma.c 24 KB

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