ide-dma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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/config.h>
  75. #include <linux/module.h>
  76. #include <linux/types.h>
  77. #include <linux/kernel.h>
  78. #include <linux/timer.h>
  79. #include <linux/mm.h>
  80. #include <linux/interrupt.h>
  81. #include <linux/pci.h>
  82. #include <linux/init.h>
  83. #include <linux/ide.h>
  84. #include <linux/delay.h>
  85. #include <linux/scatterlist.h>
  86. #include <asm/io.h>
  87. #include <asm/irq.h>
  88. static const struct drive_list_entry drive_whitelist [] = {
  89. { "Micropolis 2112A" , "ALL" },
  90. { "CONNER CTMA 4000" , "ALL" },
  91. { "CONNER CTT8000-A" , "ALL" },
  92. { "ST34342A" , "ALL" },
  93. { NULL , NULL }
  94. };
  95. static const struct drive_list_entry drive_blacklist [] = {
  96. { "WDC AC11000H" , "ALL" },
  97. { "WDC AC22100H" , "ALL" },
  98. { "WDC AC32500H" , "ALL" },
  99. { "WDC AC33100H" , "ALL" },
  100. { "WDC AC31600H" , "ALL" },
  101. { "WDC AC32100H" , "24.09P07" },
  102. { "WDC AC23200L" , "21.10N21" },
  103. { "Compaq CRD-8241B" , "ALL" },
  104. { "CRD-8400B" , "ALL" },
  105. { "CRD-8480B", "ALL" },
  106. { "CRD-8482B", "ALL" },
  107. { "CRD-84" , "ALL" },
  108. { "SanDisk SDP3B" , "ALL" },
  109. { "SanDisk SDP3B-64" , "ALL" },
  110. { "SANYO CD-ROM CRD" , "ALL" },
  111. { "HITACHI CDR-8" , "ALL" },
  112. { "HITACHI CDR-8335" , "ALL" },
  113. { "HITACHI CDR-8435" , "ALL" },
  114. { "Toshiba CD-ROM XM-6202B" , "ALL" },
  115. { "CD-532E-A" , "ALL" },
  116. { "E-IDE CD-ROM CR-840", "ALL" },
  117. { "CD-ROM Drive/F5A", "ALL" },
  118. { "WPI CDD-820", "ALL" },
  119. { "SAMSUNG CD-ROM SC-148C", "ALL" },
  120. { "SAMSUNG CD-ROM SC", "ALL" },
  121. { "SanDisk SDP3B-64" , "ALL" },
  122. { "ATAPI CD-ROM DRIVE 40X MAXIMUM", "ALL" },
  123. { "_NEC DV5800A", "ALL" },
  124. { NULL , NULL }
  125. };
  126. /**
  127. * ide_in_drive_list - look for drive in black/white list
  128. * @id: drive identifier
  129. * @drive_table: list to inspect
  130. *
  131. * Look for a drive in the blacklist and the whitelist tables
  132. * Returns 1 if the drive is found in the table.
  133. */
  134. int ide_in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table)
  135. {
  136. for ( ; drive_table->id_model ; drive_table++)
  137. if ((!strcmp(drive_table->id_model, id->model)) &&
  138. ((strstr(drive_table->id_firmware, id->fw_rev)) ||
  139. (!strcmp(drive_table->id_firmware, "ALL"))))
  140. return 1;
  141. return 0;
  142. }
  143. EXPORT_SYMBOL_GPL(ide_in_drive_list);
  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->flags & REQ_DRIVE_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. ide_hwif_t *hwif = HWIF(drive);
  312. if ((id->capability & 1) && hwif->autodma) {
  313. /*
  314. * Enable DMA on any drive that has
  315. * UltraDMA (mode 0/1/2/3/4/5/6) enabled
  316. */
  317. if ((id->field_valid & 4) && ((id->dma_ultra >> 8) & 0x7f))
  318. return hwif->ide_dma_on(drive);
  319. /*
  320. * Enable DMA on any drive that has mode2 DMA
  321. * (multi or single) enabled
  322. */
  323. if (id->field_valid & 2) /* regular DMA */
  324. if ((id->dma_mword & 0x404) == 0x404 ||
  325. (id->dma_1word & 0x404) == 0x404)
  326. return hwif->ide_dma_on(drive);
  327. /* Consult the list of known "good" drives */
  328. if (__ide_dma_good_drive(drive))
  329. return hwif->ide_dma_on(drive);
  330. }
  331. // if (hwif->tuneproc != NULL) hwif->tuneproc(drive, 255);
  332. return hwif->ide_dma_off_quietly(drive);
  333. }
  334. /**
  335. * dma_timer_expiry - handle a DMA timeout
  336. * @drive: Drive that timed out
  337. *
  338. * An IDE DMA transfer timed out. In the event of an error we ask
  339. * the driver to resolve the problem, if a DMA transfer is still
  340. * in progress we continue to wait (arguably we need to add a
  341. * secondary 'I don't care what the drive thinks' timeout here)
  342. * Finally if we have an interrupt we let it complete the I/O.
  343. * But only one time - we clear expiry and if it's still not
  344. * completed after WAIT_CMD, we error and retry in PIO.
  345. * This can occur if an interrupt is lost or due to hang or bugs.
  346. */
  347. static int dma_timer_expiry (ide_drive_t *drive)
  348. {
  349. ide_hwif_t *hwif = HWIF(drive);
  350. u8 dma_stat = hwif->INB(hwif->dma_status);
  351. printk(KERN_WARNING "%s: dma_timer_expiry: dma status == 0x%02x\n",
  352. drive->name, dma_stat);
  353. if ((dma_stat & 0x18) == 0x18) /* BUSY Stupid Early Timer !! */
  354. return WAIT_CMD;
  355. HWGROUP(drive)->expiry = NULL; /* one free ride for now */
  356. /* 1 dmaing, 2 error, 4 intr */
  357. if (dma_stat & 2) /* ERROR */
  358. return -1;
  359. if (dma_stat & 1) /* DMAing */
  360. return WAIT_CMD;
  361. if (dma_stat & 4) /* Got an Interrupt */
  362. return WAIT_CMD;
  363. return 0; /* Status is unknown -- reset the bus */
  364. }
  365. /**
  366. * __ide_dma_host_off - Generic DMA kill
  367. * @drive: drive to control
  368. *
  369. * Perform the generic IDE controller DMA off operation. This
  370. * works for most IDE bus mastering controllers
  371. */
  372. int __ide_dma_host_off (ide_drive_t *drive)
  373. {
  374. ide_hwif_t *hwif = HWIF(drive);
  375. u8 unit = (drive->select.b.unit & 0x01);
  376. u8 dma_stat = hwif->INB(hwif->dma_status);
  377. hwif->OUTB((dma_stat & ~(1<<(5+unit))), hwif->dma_status);
  378. return 0;
  379. }
  380. EXPORT_SYMBOL(__ide_dma_host_off);
  381. /**
  382. * __ide_dma_host_off_quietly - Generic DMA kill
  383. * @drive: drive to control
  384. *
  385. * Turn off the current DMA on this IDE controller.
  386. */
  387. int __ide_dma_off_quietly (ide_drive_t *drive)
  388. {
  389. drive->using_dma = 0;
  390. ide_toggle_bounce(drive, 0);
  391. if (HWIF(drive)->ide_dma_host_off(drive))
  392. return 1;
  393. return 0;
  394. }
  395. EXPORT_SYMBOL(__ide_dma_off_quietly);
  396. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
  397. /**
  398. * __ide_dma_off - disable DMA on a device
  399. * @drive: drive to disable DMA on
  400. *
  401. * Disable IDE DMA for a device on this IDE controller.
  402. * Inform the user that DMA has been disabled.
  403. */
  404. int __ide_dma_off (ide_drive_t *drive)
  405. {
  406. printk(KERN_INFO "%s: DMA disabled\n", drive->name);
  407. return HWIF(drive)->ide_dma_off_quietly(drive);
  408. }
  409. EXPORT_SYMBOL(__ide_dma_off);
  410. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  411. /**
  412. * __ide_dma_host_on - Enable DMA on a host
  413. * @drive: drive to enable for DMA
  414. *
  415. * Enable DMA on an IDE controller following generic bus mastering
  416. * IDE controller behaviour
  417. */
  418. int __ide_dma_host_on (ide_drive_t *drive)
  419. {
  420. if (drive->using_dma) {
  421. ide_hwif_t *hwif = HWIF(drive);
  422. u8 unit = (drive->select.b.unit & 0x01);
  423. u8 dma_stat = hwif->INB(hwif->dma_status);
  424. hwif->OUTB((dma_stat|(1<<(5+unit))), hwif->dma_status);
  425. return 0;
  426. }
  427. return 1;
  428. }
  429. EXPORT_SYMBOL(__ide_dma_host_on);
  430. /**
  431. * __ide_dma_on - Enable DMA on a device
  432. * @drive: drive to enable DMA on
  433. *
  434. * Enable IDE DMA for a device on this IDE controller.
  435. */
  436. int __ide_dma_on (ide_drive_t *drive)
  437. {
  438. /* consult the list of known "bad" drives */
  439. if (__ide_dma_bad_drive(drive))
  440. return 1;
  441. drive->using_dma = 1;
  442. ide_toggle_bounce(drive, 1);
  443. if (HWIF(drive)->ide_dma_host_on(drive))
  444. return 1;
  445. return 0;
  446. }
  447. EXPORT_SYMBOL(__ide_dma_on);
  448. /**
  449. * __ide_dma_check - check DMA setup
  450. * @drive: drive to check
  451. *
  452. * Don't use - due for extermination
  453. */
  454. int __ide_dma_check (ide_drive_t *drive)
  455. {
  456. return config_drive_for_dma(drive);
  457. }
  458. EXPORT_SYMBOL(__ide_dma_check);
  459. /**
  460. * ide_dma_setup - begin a DMA phase
  461. * @drive: target device
  462. *
  463. * Build an IDE DMA PRD (IDE speak for scatter gather table)
  464. * and then set up the DMA transfer registers for a device
  465. * that follows generic IDE PCI DMA behaviour. Controllers can
  466. * override this function if they need to
  467. *
  468. * Returns 0 on success. If a PIO fallback is required then 1
  469. * is returned.
  470. */
  471. int ide_dma_setup(ide_drive_t *drive)
  472. {
  473. ide_hwif_t *hwif = drive->hwif;
  474. struct request *rq = HWGROUP(drive)->rq;
  475. unsigned int reading;
  476. u8 dma_stat;
  477. if (rq_data_dir(rq))
  478. reading = 0;
  479. else
  480. reading = 1 << 3;
  481. /* fall back to pio! */
  482. if (!ide_build_dmatable(drive, rq)) {
  483. ide_map_sg(drive, rq);
  484. return 1;
  485. }
  486. /* PRD table */
  487. hwif->OUTL(hwif->dmatable_dma, hwif->dma_prdtable);
  488. /* specify r/w */
  489. hwif->OUTB(reading, hwif->dma_command);
  490. /* read dma_status for INTR & ERROR flags */
  491. dma_stat = hwif->INB(hwif->dma_status);
  492. /* clear INTR & ERROR flags */
  493. hwif->OUTB(dma_stat|6, hwif->dma_status);
  494. drive->waiting_for_dma = 1;
  495. return 0;
  496. }
  497. EXPORT_SYMBOL_GPL(ide_dma_setup);
  498. static void ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
  499. {
  500. /* issue cmd to drive */
  501. ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, dma_timer_expiry);
  502. }
  503. void ide_dma_start(ide_drive_t *drive)
  504. {
  505. ide_hwif_t *hwif = HWIF(drive);
  506. u8 dma_cmd = hwif->INB(hwif->dma_command);
  507. /* Note that this is done *after* the cmd has
  508. * been issued to the drive, as per the BM-IDE spec.
  509. * The Promise Ultra33 doesn't work correctly when
  510. * we do this part before issuing the drive cmd.
  511. */
  512. /* start DMA */
  513. hwif->OUTB(dma_cmd|1, hwif->dma_command);
  514. hwif->dma = 1;
  515. wmb();
  516. }
  517. EXPORT_SYMBOL_GPL(ide_dma_start);
  518. /* returns 1 on error, 0 otherwise */
  519. int __ide_dma_end (ide_drive_t *drive)
  520. {
  521. ide_hwif_t *hwif = HWIF(drive);
  522. u8 dma_stat = 0, dma_cmd = 0;
  523. drive->waiting_for_dma = 0;
  524. /* get dma_command mode */
  525. dma_cmd = hwif->INB(hwif->dma_command);
  526. /* stop DMA */
  527. hwif->OUTB(dma_cmd&~1, hwif->dma_command);
  528. /* get DMA status */
  529. dma_stat = hwif->INB(hwif->dma_status);
  530. /* clear the INTR & ERROR bits */
  531. hwif->OUTB(dma_stat|6, hwif->dma_status);
  532. /* purge DMA mappings */
  533. ide_destroy_dmatable(drive);
  534. /* verify good DMA status */
  535. hwif->dma = 0;
  536. wmb();
  537. return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0;
  538. }
  539. EXPORT_SYMBOL(__ide_dma_end);
  540. /* returns 1 if dma irq issued, 0 otherwise */
  541. static int __ide_dma_test_irq(ide_drive_t *drive)
  542. {
  543. ide_hwif_t *hwif = HWIF(drive);
  544. u8 dma_stat = hwif->INB(hwif->dma_status);
  545. #if 0 /* do not set unless you know what you are doing */
  546. if (dma_stat & 4) {
  547. u8 stat = hwif->INB(IDE_STATUS_REG);
  548. hwif->OUTB(hwif->dma_status, dma_stat & 0xE4);
  549. }
  550. #endif
  551. /* return 1 if INTR asserted */
  552. if ((dma_stat & 4) == 4)
  553. return 1;
  554. if (!drive->waiting_for_dma)
  555. printk(KERN_WARNING "%s: (%s) called while not waiting\n",
  556. drive->name, __FUNCTION__);
  557. return 0;
  558. }
  559. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
  560. int __ide_dma_bad_drive (ide_drive_t *drive)
  561. {
  562. struct hd_driveid *id = drive->id;
  563. int blacklist = ide_in_drive_list(id, drive_blacklist);
  564. if (blacklist) {
  565. printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n",
  566. drive->name, id->model);
  567. return blacklist;
  568. }
  569. return 0;
  570. }
  571. EXPORT_SYMBOL(__ide_dma_bad_drive);
  572. int __ide_dma_good_drive (ide_drive_t *drive)
  573. {
  574. struct hd_driveid *id = drive->id;
  575. return ide_in_drive_list(id, drive_whitelist);
  576. }
  577. EXPORT_SYMBOL(__ide_dma_good_drive);
  578. int ide_use_dma(ide_drive_t *drive)
  579. {
  580. struct hd_driveid *id = drive->id;
  581. ide_hwif_t *hwif = drive->hwif;
  582. /* consult the list of known "bad" drives */
  583. if (__ide_dma_bad_drive(drive))
  584. return 0;
  585. /* capable of UltraDMA modes */
  586. if (id->field_valid & 4) {
  587. if (hwif->ultra_mask & id->dma_ultra)
  588. return 1;
  589. }
  590. /* capable of regular DMA modes */
  591. if (id->field_valid & 2) {
  592. if (hwif->mwdma_mask & id->dma_mword)
  593. return 1;
  594. if (hwif->swdma_mask & id->dma_1word)
  595. return 1;
  596. }
  597. /* consult the list of known "good" drives */
  598. if (__ide_dma_good_drive(drive) && id->eide_dma_time < 150)
  599. return 1;
  600. return 0;
  601. }
  602. EXPORT_SYMBOL_GPL(ide_use_dma);
  603. void ide_dma_verbose(ide_drive_t *drive)
  604. {
  605. struct hd_driveid *id = drive->id;
  606. ide_hwif_t *hwif = HWIF(drive);
  607. if (id->field_valid & 4) {
  608. if ((id->dma_ultra >> 8) && (id->dma_mword >> 8))
  609. goto bug_dma_off;
  610. if (id->dma_ultra & ((id->dma_ultra >> 8) & hwif->ultra_mask)) {
  611. if (((id->dma_ultra >> 11) & 0x1F) &&
  612. eighty_ninty_three(drive)) {
  613. if ((id->dma_ultra >> 15) & 1) {
  614. printk(", UDMA(mode 7)");
  615. } else if ((id->dma_ultra >> 14) & 1) {
  616. printk(", UDMA(133)");
  617. } else if ((id->dma_ultra >> 13) & 1) {
  618. printk(", UDMA(100)");
  619. } else if ((id->dma_ultra >> 12) & 1) {
  620. printk(", UDMA(66)");
  621. } else if ((id->dma_ultra >> 11) & 1) {
  622. printk(", UDMA(44)");
  623. } else
  624. goto mode_two;
  625. } else {
  626. mode_two:
  627. if ((id->dma_ultra >> 10) & 1) {
  628. printk(", UDMA(33)");
  629. } else if ((id->dma_ultra >> 9) & 1) {
  630. printk(", UDMA(25)");
  631. } else if ((id->dma_ultra >> 8) & 1) {
  632. printk(", UDMA(16)");
  633. }
  634. }
  635. } else {
  636. printk(", (U)DMA"); /* Can be BIOS-enabled! */
  637. }
  638. } else if (id->field_valid & 2) {
  639. if ((id->dma_mword >> 8) && (id->dma_1word >> 8))
  640. goto bug_dma_off;
  641. printk(", DMA");
  642. } else if (id->field_valid & 1) {
  643. printk(", BUG");
  644. }
  645. return;
  646. bug_dma_off:
  647. printk(", BUG DMA OFF");
  648. hwif->ide_dma_off_quietly(drive);
  649. return;
  650. }
  651. EXPORT_SYMBOL(ide_dma_verbose);
  652. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  653. int __ide_dma_lostirq (ide_drive_t *drive)
  654. {
  655. printk("%s: DMA interrupt recovery\n", drive->name);
  656. return 1;
  657. }
  658. EXPORT_SYMBOL(__ide_dma_lostirq);
  659. int __ide_dma_timeout (ide_drive_t *drive)
  660. {
  661. printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
  662. if (HWIF(drive)->ide_dma_test_irq(drive))
  663. return 0;
  664. return HWIF(drive)->ide_dma_end(drive);
  665. }
  666. EXPORT_SYMBOL(__ide_dma_timeout);
  667. /*
  668. * Needed for allowing full modular support of ide-driver
  669. */
  670. static int ide_release_dma_engine(ide_hwif_t *hwif)
  671. {
  672. if (hwif->dmatable_cpu) {
  673. pci_free_consistent(hwif->pci_dev,
  674. PRD_ENTRIES * PRD_BYTES,
  675. hwif->dmatable_cpu,
  676. hwif->dmatable_dma);
  677. hwif->dmatable_cpu = NULL;
  678. }
  679. return 1;
  680. }
  681. static int ide_release_iomio_dma(ide_hwif_t *hwif)
  682. {
  683. if ((hwif->dma_extra) && (hwif->channel == 0))
  684. release_region((hwif->dma_base + 16), hwif->dma_extra);
  685. release_region(hwif->dma_base, 8);
  686. if (hwif->dma_base2)
  687. release_region(hwif->dma_base, 8);
  688. return 1;
  689. }
  690. /*
  691. * Needed for allowing full modular support of ide-driver
  692. */
  693. int ide_release_dma (ide_hwif_t *hwif)
  694. {
  695. if (hwif->mmio == 2)
  696. return 1;
  697. if (hwif->chipset == ide_etrax100)
  698. return 1;
  699. ide_release_dma_engine(hwif);
  700. return ide_release_iomio_dma(hwif);
  701. }
  702. static int ide_allocate_dma_engine(ide_hwif_t *hwif)
  703. {
  704. hwif->dmatable_cpu = pci_alloc_consistent(hwif->pci_dev,
  705. PRD_ENTRIES * PRD_BYTES,
  706. &hwif->dmatable_dma);
  707. if (hwif->dmatable_cpu)
  708. return 0;
  709. printk(KERN_ERR "%s: -- Error, unable to allocate%s DMA table(s).\n",
  710. hwif->cds->name, !hwif->dmatable_cpu ? " CPU" : "");
  711. ide_release_dma_engine(hwif);
  712. return 1;
  713. }
  714. static int ide_mapped_mmio_dma(ide_hwif_t *hwif, unsigned long base, unsigned int ports)
  715. {
  716. printk(KERN_INFO " %s: MMIO-DMA ", hwif->name);
  717. hwif->dma_base = base;
  718. if (hwif->cds->extra && hwif->channel == 0)
  719. hwif->dma_extra = hwif->cds->extra;
  720. if(hwif->mate)
  721. hwif->dma_master = (hwif->channel) ? hwif->mate->dma_base : base;
  722. else
  723. hwif->dma_master = base;
  724. return 0;
  725. }
  726. static int ide_iomio_dma(ide_hwif_t *hwif, unsigned long base, unsigned int ports)
  727. {
  728. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx",
  729. hwif->name, base, base + ports - 1);
  730. if (!request_region(base, ports, hwif->name)) {
  731. printk(" -- Error, ports in use.\n");
  732. return 1;
  733. }
  734. hwif->dma_base = base;
  735. if ((hwif->cds->extra) && (hwif->channel == 0)) {
  736. request_region(base+16, hwif->cds->extra, hwif->cds->name);
  737. hwif->dma_extra = hwif->cds->extra;
  738. }
  739. if(hwif->mate)
  740. hwif->dma_master = (hwif->channel) ? hwif->mate->dma_base : base;
  741. else
  742. hwif->dma_master = base;
  743. if (hwif->dma_base2) {
  744. if (!request_region(hwif->dma_base2, ports, hwif->name))
  745. {
  746. printk(" -- Error, secondary ports in use.\n");
  747. release_region(base, ports);
  748. return 1;
  749. }
  750. }
  751. return 0;
  752. }
  753. static int ide_dma_iobase(ide_hwif_t *hwif, unsigned long base, unsigned int ports)
  754. {
  755. if (hwif->mmio == 2)
  756. return ide_mapped_mmio_dma(hwif, base,ports);
  757. BUG_ON(hwif->mmio == 1);
  758. return ide_iomio_dma(hwif, base, ports);
  759. }
  760. /*
  761. * This can be called for a dynamically installed interface. Don't __init it
  762. */
  763. void ide_setup_dma (ide_hwif_t *hwif, unsigned long dma_base, unsigned int num_ports)
  764. {
  765. if (ide_dma_iobase(hwif, dma_base, num_ports))
  766. return;
  767. if (ide_allocate_dma_engine(hwif)) {
  768. ide_release_dma(hwif);
  769. return;
  770. }
  771. if (!(hwif->dma_command))
  772. hwif->dma_command = hwif->dma_base;
  773. if (!(hwif->dma_vendor1))
  774. hwif->dma_vendor1 = (hwif->dma_base + 1);
  775. if (!(hwif->dma_status))
  776. hwif->dma_status = (hwif->dma_base + 2);
  777. if (!(hwif->dma_vendor3))
  778. hwif->dma_vendor3 = (hwif->dma_base + 3);
  779. if (!(hwif->dma_prdtable))
  780. hwif->dma_prdtable = (hwif->dma_base + 4);
  781. if (!hwif->ide_dma_off_quietly)
  782. hwif->ide_dma_off_quietly = &__ide_dma_off_quietly;
  783. if (!hwif->ide_dma_host_off)
  784. hwif->ide_dma_host_off = &__ide_dma_host_off;
  785. if (!hwif->ide_dma_on)
  786. hwif->ide_dma_on = &__ide_dma_on;
  787. if (!hwif->ide_dma_host_on)
  788. hwif->ide_dma_host_on = &__ide_dma_host_on;
  789. if (!hwif->ide_dma_check)
  790. hwif->ide_dma_check = &__ide_dma_check;
  791. if (!hwif->dma_setup)
  792. hwif->dma_setup = &ide_dma_setup;
  793. if (!hwif->dma_exec_cmd)
  794. hwif->dma_exec_cmd = &ide_dma_exec_cmd;
  795. if (!hwif->dma_start)
  796. hwif->dma_start = &ide_dma_start;
  797. if (!hwif->ide_dma_end)
  798. hwif->ide_dma_end = &__ide_dma_end;
  799. if (!hwif->ide_dma_test_irq)
  800. hwif->ide_dma_test_irq = &__ide_dma_test_irq;
  801. if (!hwif->ide_dma_timeout)
  802. hwif->ide_dma_timeout = &__ide_dma_timeout;
  803. if (!hwif->ide_dma_lostirq)
  804. hwif->ide_dma_lostirq = &__ide_dma_lostirq;
  805. if (hwif->chipset != ide_trm290) {
  806. u8 dma_stat = hwif->INB(hwif->dma_status);
  807. printk(", BIOS settings: %s:%s, %s:%s",
  808. hwif->drives[0].name, (dma_stat & 0x20) ? "DMA" : "pio",
  809. hwif->drives[1].name, (dma_stat & 0x40) ? "DMA" : "pio");
  810. }
  811. printk("\n");
  812. BUG_ON(!hwif->dma_master);
  813. }
  814. EXPORT_SYMBOL_GPL(ide_setup_dma);
  815. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */