ide-dma.c 24 KB

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