ide-dma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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. struct drive_list_entry {
  89. const char *id_model;
  90. const char *id_firmware;
  91. };
  92. static const struct drive_list_entry drive_whitelist [] = {
  93. { "Micropolis 2112A" , "ALL" },
  94. { "CONNER CTMA 4000" , "ALL" },
  95. { "CONNER CTT8000-A" , "ALL" },
  96. { "ST34342A" , "ALL" },
  97. { NULL , NULL }
  98. };
  99. static const struct drive_list_entry drive_blacklist [] = {
  100. { "WDC AC11000H" , "ALL" },
  101. { "WDC AC22100H" , "ALL" },
  102. { "WDC AC32500H" , "ALL" },
  103. { "WDC AC33100H" , "ALL" },
  104. { "WDC AC31600H" , "ALL" },
  105. { "WDC AC32100H" , "24.09P07" },
  106. { "WDC AC23200L" , "21.10N21" },
  107. { "Compaq CRD-8241B" , "ALL" },
  108. { "CRD-8400B" , "ALL" },
  109. { "CRD-8480B", "ALL" },
  110. { "CRD-8482B", "ALL" },
  111. { "CRD-84" , "ALL" },
  112. { "SanDisk SDP3B" , "ALL" },
  113. { "SanDisk SDP3B-64" , "ALL" },
  114. { "SANYO CD-ROM CRD" , "ALL" },
  115. { "HITACHI CDR-8" , "ALL" },
  116. { "HITACHI CDR-8335" , "ALL" },
  117. { "HITACHI CDR-8435" , "ALL" },
  118. { "Toshiba CD-ROM XM-6202B" , "ALL" },
  119. { "CD-532E-A" , "ALL" },
  120. { "E-IDE CD-ROM CR-840", "ALL" },
  121. { "CD-ROM Drive/F5A", "ALL" },
  122. { "WPI CDD-820", "ALL" },
  123. { "SAMSUNG CD-ROM SC-148C", "ALL" },
  124. { "SAMSUNG CD-ROM SC", "ALL" },
  125. { "SanDisk SDP3B-64" , "ALL" },
  126. { "ATAPI CD-ROM DRIVE 40X MAXIMUM", "ALL" },
  127. { "_NEC DV5800A", "ALL" },
  128. { NULL , NULL }
  129. };
  130. /**
  131. * in_drive_list - look for drive in black/white list
  132. * @id: drive identifier
  133. * @drive_table: list to inspect
  134. *
  135. * Look for a drive in the blacklist and the whitelist tables
  136. * Returns 1 if the drive is found in the table.
  137. */
  138. static int in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table)
  139. {
  140. for ( ; drive_table->id_model ; drive_table++)
  141. if ((!strcmp(drive_table->id_model, id->model)) &&
  142. ((strstr(drive_table->id_firmware, id->fw_rev)) ||
  143. (!strcmp(drive_table->id_firmware, "ALL"))))
  144. return 1;
  145. return 0;
  146. }
  147. /**
  148. * ide_dma_intr - IDE DMA interrupt handler
  149. * @drive: the drive the interrupt is for
  150. *
  151. * Handle an interrupt completing a read/write DMA transfer on an
  152. * IDE device
  153. */
  154. ide_startstop_t ide_dma_intr (ide_drive_t *drive)
  155. {
  156. u8 stat = 0, dma_stat = 0;
  157. dma_stat = HWIF(drive)->ide_dma_end(drive);
  158. stat = HWIF(drive)->INB(IDE_STATUS_REG); /* get drive status */
  159. if (OK_STAT(stat,DRIVE_READY,drive->bad_wstat|DRQ_STAT)) {
  160. if (!dma_stat) {
  161. struct request *rq = HWGROUP(drive)->rq;
  162. if (rq->rq_disk) {
  163. ide_driver_t *drv;
  164. drv = *(ide_driver_t **)rq->rq_disk->private_data;;
  165. drv->end_request(drive, 1, rq->nr_sectors);
  166. } else
  167. ide_end_request(drive, 1, rq->nr_sectors);
  168. return ide_stopped;
  169. }
  170. printk(KERN_ERR "%s: dma_intr: bad DMA status (dma_stat=%x)\n",
  171. drive->name, dma_stat);
  172. }
  173. return ide_error(drive, "dma_intr", stat);
  174. }
  175. EXPORT_SYMBOL_GPL(ide_dma_intr);
  176. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  177. /**
  178. * ide_build_sglist - map IDE scatter gather for DMA I/O
  179. * @drive: the drive to build the DMA table for
  180. * @rq: the request holding the sg list
  181. *
  182. * Perform the PCI mapping magic necessary to access the source or
  183. * target buffers of a request via PCI DMA. The lower layers of the
  184. * kernel provide the necessary cache management so that we can
  185. * operate in a portable fashion
  186. */
  187. int ide_build_sglist(ide_drive_t *drive, struct request *rq)
  188. {
  189. ide_hwif_t *hwif = HWIF(drive);
  190. struct scatterlist *sg = hwif->sg_table;
  191. if ((rq->flags & REQ_DRIVE_TASKFILE) && rq->nr_sectors > 256)
  192. BUG();
  193. ide_map_sg(drive, rq);
  194. if (rq_data_dir(rq) == READ)
  195. hwif->sg_dma_direction = PCI_DMA_FROMDEVICE;
  196. else
  197. hwif->sg_dma_direction = PCI_DMA_TODEVICE;
  198. return pci_map_sg(hwif->pci_dev, sg, hwif->sg_nents, hwif->sg_dma_direction);
  199. }
  200. EXPORT_SYMBOL_GPL(ide_build_sglist);
  201. /**
  202. * ide_build_dmatable - build IDE DMA table
  203. *
  204. * ide_build_dmatable() prepares a dma request. We map the command
  205. * to get the pci bus addresses of the buffers and then build up
  206. * the PRD table that the IDE layer wants to be fed. The code
  207. * knows about the 64K wrap bug in the CS5530.
  208. *
  209. * Returns the number of built PRD entries if all went okay,
  210. * returns 0 otherwise.
  211. *
  212. * May also be invoked from trm290.c
  213. */
  214. int ide_build_dmatable (ide_drive_t *drive, struct request *rq)
  215. {
  216. ide_hwif_t *hwif = HWIF(drive);
  217. unsigned int *table = hwif->dmatable_cpu;
  218. unsigned int is_trm290 = (hwif->chipset == ide_trm290) ? 1 : 0;
  219. unsigned int count = 0;
  220. int i;
  221. struct scatterlist *sg;
  222. hwif->sg_nents = i = ide_build_sglist(drive, rq);
  223. if (!i)
  224. return 0;
  225. sg = hwif->sg_table;
  226. while (i) {
  227. u32 cur_addr;
  228. u32 cur_len;
  229. cur_addr = sg_dma_address(sg);
  230. cur_len = sg_dma_len(sg);
  231. /*
  232. * Fill in the dma table, without crossing any 64kB boundaries.
  233. * Most hardware requires 16-bit alignment of all blocks,
  234. * but the trm290 requires 32-bit alignment.
  235. */
  236. while (cur_len) {
  237. if (count++ >= PRD_ENTRIES) {
  238. printk(KERN_ERR "%s: DMA table too small\n", drive->name);
  239. goto use_pio_instead;
  240. } else {
  241. u32 xcount, bcount = 0x10000 - (cur_addr & 0xffff);
  242. if (bcount > cur_len)
  243. bcount = cur_len;
  244. *table++ = cpu_to_le32(cur_addr);
  245. xcount = bcount & 0xffff;
  246. if (is_trm290)
  247. xcount = ((xcount >> 2) - 1) << 16;
  248. if (xcount == 0x0000) {
  249. /*
  250. * Most chipsets correctly interpret a length of 0x0000 as 64KB,
  251. * but at least one (e.g. CS5530) misinterprets it as zero (!).
  252. * So here we break the 64KB entry into two 32KB entries instead.
  253. */
  254. if (count++ >= PRD_ENTRIES) {
  255. printk(KERN_ERR "%s: DMA table too small\n", drive->name);
  256. goto use_pio_instead;
  257. }
  258. *table++ = cpu_to_le32(0x8000);
  259. *table++ = cpu_to_le32(cur_addr + 0x8000);
  260. xcount = 0x8000;
  261. }
  262. *table++ = cpu_to_le32(xcount);
  263. cur_addr += bcount;
  264. cur_len -= bcount;
  265. }
  266. }
  267. sg++;
  268. i--;
  269. }
  270. if (count) {
  271. if (!is_trm290)
  272. *--table |= cpu_to_le32(0x80000000);
  273. return count;
  274. }
  275. printk(KERN_ERR "%s: empty DMA table?\n", drive->name);
  276. use_pio_instead:
  277. pci_unmap_sg(hwif->pci_dev,
  278. hwif->sg_table,
  279. hwif->sg_nents,
  280. hwif->sg_dma_direction);
  281. return 0; /* revert to PIO for this request */
  282. }
  283. EXPORT_SYMBOL_GPL(ide_build_dmatable);
  284. /**
  285. * ide_destroy_dmatable - clean up DMA mapping
  286. * @drive: The drive to unmap
  287. *
  288. * Teardown mappings after DMA has completed. This must be called
  289. * after the completion of each use of ide_build_dmatable and before
  290. * the next use of ide_build_dmatable. Failure to do so will cause
  291. * an oops as only one mapping can be live for each target at a given
  292. * time.
  293. */
  294. void ide_destroy_dmatable (ide_drive_t *drive)
  295. {
  296. struct pci_dev *dev = HWIF(drive)->pci_dev;
  297. struct scatterlist *sg = HWIF(drive)->sg_table;
  298. int nents = HWIF(drive)->sg_nents;
  299. pci_unmap_sg(dev, sg, nents, HWIF(drive)->sg_dma_direction);
  300. }
  301. EXPORT_SYMBOL_GPL(ide_destroy_dmatable);
  302. /**
  303. * config_drive_for_dma - attempt to activate IDE DMA
  304. * @drive: the drive to place in DMA mode
  305. *
  306. * If the drive supports at least mode 2 DMA or UDMA of any kind
  307. * then attempt to place it into DMA mode. Drives that are known to
  308. * support DMA but predate the DMA properties or that are known
  309. * to have DMA handling bugs are also set up appropriately based
  310. * on the good/bad drive lists.
  311. */
  312. static int config_drive_for_dma (ide_drive_t *drive)
  313. {
  314. struct hd_driveid *id = drive->id;
  315. ide_hwif_t *hwif = HWIF(drive);
  316. if ((id->capability & 1) && hwif->autodma) {
  317. /*
  318. * Enable DMA on any drive that has
  319. * UltraDMA (mode 0/1/2/3/4/5/6) enabled
  320. */
  321. if ((id->field_valid & 4) && ((id->dma_ultra >> 8) & 0x7f))
  322. return hwif->ide_dma_on(drive);
  323. /*
  324. * Enable DMA on any drive that has mode2 DMA
  325. * (multi or single) enabled
  326. */
  327. if (id->field_valid & 2) /* regular DMA */
  328. if ((id->dma_mword & 0x404) == 0x404 ||
  329. (id->dma_1word & 0x404) == 0x404)
  330. return hwif->ide_dma_on(drive);
  331. /* Consult the list of known "good" drives */
  332. if (__ide_dma_good_drive(drive))
  333. return hwif->ide_dma_on(drive);
  334. }
  335. // if (hwif->tuneproc != NULL) hwif->tuneproc(drive, 255);
  336. return hwif->ide_dma_off_quietly(drive);
  337. }
  338. /**
  339. * dma_timer_expiry - handle a DMA timeout
  340. * @drive: Drive that timed out
  341. *
  342. * An IDE DMA transfer timed out. In the event of an error we ask
  343. * the driver to resolve the problem, if a DMA transfer is still
  344. * in progress we continue to wait (arguably we need to add a
  345. * secondary 'I don't care what the drive thinks' timeout here)
  346. * Finally if we have an interrupt we let it complete the I/O.
  347. * But only one time - we clear expiry and if it's still not
  348. * completed after WAIT_CMD, we error and retry in PIO.
  349. * This can occur if an interrupt is lost or due to hang or bugs.
  350. */
  351. static int dma_timer_expiry (ide_drive_t *drive)
  352. {
  353. ide_hwif_t *hwif = HWIF(drive);
  354. u8 dma_stat = hwif->INB(hwif->dma_status);
  355. printk(KERN_WARNING "%s: dma_timer_expiry: dma status == 0x%02x\n",
  356. drive->name, dma_stat);
  357. if ((dma_stat & 0x18) == 0x18) /* BUSY Stupid Early Timer !! */
  358. return WAIT_CMD;
  359. HWGROUP(drive)->expiry = NULL; /* one free ride for now */
  360. /* 1 dmaing, 2 error, 4 intr */
  361. if (dma_stat & 2) /* ERROR */
  362. return -1;
  363. if (dma_stat & 1) /* DMAing */
  364. return WAIT_CMD;
  365. if (dma_stat & 4) /* Got an Interrupt */
  366. return WAIT_CMD;
  367. return 0; /* Status is unknown -- reset the bus */
  368. }
  369. /**
  370. * __ide_dma_host_off - Generic DMA kill
  371. * @drive: drive to control
  372. *
  373. * Perform the generic IDE controller DMA off operation. This
  374. * works for most IDE bus mastering controllers
  375. */
  376. int __ide_dma_host_off (ide_drive_t *drive)
  377. {
  378. ide_hwif_t *hwif = HWIF(drive);
  379. u8 unit = (drive->select.b.unit & 0x01);
  380. u8 dma_stat = hwif->INB(hwif->dma_status);
  381. hwif->OUTB((dma_stat & ~(1<<(5+unit))), hwif->dma_status);
  382. return 0;
  383. }
  384. EXPORT_SYMBOL(__ide_dma_host_off);
  385. /**
  386. * __ide_dma_host_off_quietly - Generic DMA kill
  387. * @drive: drive to control
  388. *
  389. * Turn off the current DMA on this IDE controller.
  390. */
  391. int __ide_dma_off_quietly (ide_drive_t *drive)
  392. {
  393. drive->using_dma = 0;
  394. ide_toggle_bounce(drive, 0);
  395. if (HWIF(drive)->ide_dma_host_off(drive))
  396. return 1;
  397. return 0;
  398. }
  399. EXPORT_SYMBOL(__ide_dma_off_quietly);
  400. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
  401. /**
  402. * __ide_dma_off - disable DMA on a device
  403. * @drive: drive to disable DMA on
  404. *
  405. * Disable IDE DMA for a device on this IDE controller.
  406. * Inform the user that DMA has been disabled.
  407. */
  408. int __ide_dma_off (ide_drive_t *drive)
  409. {
  410. printk(KERN_INFO "%s: DMA disabled\n", drive->name);
  411. return HWIF(drive)->ide_dma_off_quietly(drive);
  412. }
  413. EXPORT_SYMBOL(__ide_dma_off);
  414. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  415. /**
  416. * __ide_dma_host_on - Enable DMA on a host
  417. * @drive: drive to enable for DMA
  418. *
  419. * Enable DMA on an IDE controller following generic bus mastering
  420. * IDE controller behaviour
  421. */
  422. int __ide_dma_host_on (ide_drive_t *drive)
  423. {
  424. if (drive->using_dma) {
  425. ide_hwif_t *hwif = HWIF(drive);
  426. u8 unit = (drive->select.b.unit & 0x01);
  427. u8 dma_stat = hwif->INB(hwif->dma_status);
  428. hwif->OUTB((dma_stat|(1<<(5+unit))), hwif->dma_status);
  429. return 0;
  430. }
  431. return 1;
  432. }
  433. EXPORT_SYMBOL(__ide_dma_host_on);
  434. /**
  435. * __ide_dma_on - Enable DMA on a device
  436. * @drive: drive to enable DMA on
  437. *
  438. * Enable IDE DMA for a device on this IDE controller.
  439. */
  440. int __ide_dma_on (ide_drive_t *drive)
  441. {
  442. /* consult the list of known "bad" drives */
  443. if (__ide_dma_bad_drive(drive))
  444. return 1;
  445. drive->using_dma = 1;
  446. ide_toggle_bounce(drive, 1);
  447. if (HWIF(drive)->ide_dma_host_on(drive))
  448. return 1;
  449. return 0;
  450. }
  451. EXPORT_SYMBOL(__ide_dma_on);
  452. /**
  453. * __ide_dma_check - check DMA setup
  454. * @drive: drive to check
  455. *
  456. * Don't use - due for extermination
  457. */
  458. int __ide_dma_check (ide_drive_t *drive)
  459. {
  460. return config_drive_for_dma(drive);
  461. }
  462. EXPORT_SYMBOL(__ide_dma_check);
  463. /**
  464. * ide_dma_setup - begin a DMA phase
  465. * @drive: target device
  466. *
  467. * Build an IDE DMA PRD (IDE speak for scatter gather table)
  468. * and then set up the DMA transfer registers for a device
  469. * that follows generic IDE PCI DMA behaviour. Controllers can
  470. * override this function if they need to
  471. *
  472. * Returns 0 on success. If a PIO fallback is required then 1
  473. * is returned.
  474. */
  475. int ide_dma_setup(ide_drive_t *drive)
  476. {
  477. ide_hwif_t *hwif = drive->hwif;
  478. struct request *rq = HWGROUP(drive)->rq;
  479. unsigned int reading;
  480. u8 dma_stat;
  481. if (rq_data_dir(rq))
  482. reading = 0;
  483. else
  484. reading = 1 << 3;
  485. /* fall back to pio! */
  486. if (!ide_build_dmatable(drive, rq)) {
  487. ide_map_sg(drive, rq);
  488. return 1;
  489. }
  490. /* PRD table */
  491. hwif->OUTL(hwif->dmatable_dma, hwif->dma_prdtable);
  492. /* specify r/w */
  493. hwif->OUTB(reading, hwif->dma_command);
  494. /* read dma_status for INTR & ERROR flags */
  495. dma_stat = hwif->INB(hwif->dma_status);
  496. /* clear INTR & ERROR flags */
  497. hwif->OUTB(dma_stat|6, hwif->dma_status);
  498. drive->waiting_for_dma = 1;
  499. return 0;
  500. }
  501. EXPORT_SYMBOL_GPL(ide_dma_setup);
  502. static void ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
  503. {
  504. /* issue cmd to drive */
  505. ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, dma_timer_expiry);
  506. }
  507. void ide_dma_start(ide_drive_t *drive)
  508. {
  509. ide_hwif_t *hwif = HWIF(drive);
  510. u8 dma_cmd = hwif->INB(hwif->dma_command);
  511. /* Note that this is done *after* the cmd has
  512. * been issued to the drive, as per the BM-IDE spec.
  513. * The Promise Ultra33 doesn't work correctly when
  514. * we do this part before issuing the drive cmd.
  515. */
  516. /* start DMA */
  517. hwif->OUTB(dma_cmd|1, hwif->dma_command);
  518. hwif->dma = 1;
  519. wmb();
  520. }
  521. EXPORT_SYMBOL_GPL(ide_dma_start);
  522. /* returns 1 on error, 0 otherwise */
  523. int __ide_dma_end (ide_drive_t *drive)
  524. {
  525. ide_hwif_t *hwif = HWIF(drive);
  526. u8 dma_stat = 0, dma_cmd = 0;
  527. drive->waiting_for_dma = 0;
  528. /* get dma_command mode */
  529. dma_cmd = hwif->INB(hwif->dma_command);
  530. /* stop DMA */
  531. hwif->OUTB(dma_cmd&~1, hwif->dma_command);
  532. /* get DMA status */
  533. dma_stat = hwif->INB(hwif->dma_status);
  534. /* clear the INTR & ERROR bits */
  535. hwif->OUTB(dma_stat|6, hwif->dma_status);
  536. /* purge DMA mappings */
  537. ide_destroy_dmatable(drive);
  538. /* verify good DMA status */
  539. hwif->dma = 0;
  540. wmb();
  541. return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0;
  542. }
  543. EXPORT_SYMBOL(__ide_dma_end);
  544. /* returns 1 if dma irq issued, 0 otherwise */
  545. static int __ide_dma_test_irq(ide_drive_t *drive)
  546. {
  547. ide_hwif_t *hwif = HWIF(drive);
  548. u8 dma_stat = hwif->INB(hwif->dma_status);
  549. #if 0 /* do not set unless you know what you are doing */
  550. if (dma_stat & 4) {
  551. u8 stat = hwif->INB(IDE_STATUS_REG);
  552. hwif->OUTB(hwif->dma_status, dma_stat & 0xE4);
  553. }
  554. #endif
  555. /* return 1 if INTR asserted */
  556. if ((dma_stat & 4) == 4)
  557. return 1;
  558. if (!drive->waiting_for_dma)
  559. printk(KERN_WARNING "%s: (%s) called while not waiting\n",
  560. drive->name, __FUNCTION__);
  561. return 0;
  562. }
  563. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
  564. int __ide_dma_bad_drive (ide_drive_t *drive)
  565. {
  566. struct hd_driveid *id = drive->id;
  567. int blacklist = in_drive_list(id, drive_blacklist);
  568. if (blacklist) {
  569. printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n",
  570. drive->name, id->model);
  571. return blacklist;
  572. }
  573. return 0;
  574. }
  575. EXPORT_SYMBOL(__ide_dma_bad_drive);
  576. int __ide_dma_good_drive (ide_drive_t *drive)
  577. {
  578. struct hd_driveid *id = drive->id;
  579. return in_drive_list(id, drive_whitelist);
  580. }
  581. EXPORT_SYMBOL(__ide_dma_good_drive);
  582. int ide_use_dma(ide_drive_t *drive)
  583. {
  584. struct hd_driveid *id = drive->id;
  585. ide_hwif_t *hwif = drive->hwif;
  586. /* consult the list of known "bad" drives */
  587. if (__ide_dma_bad_drive(drive))
  588. return 0;
  589. /* capable of UltraDMA modes */
  590. if (id->field_valid & 4) {
  591. if (hwif->ultra_mask & id->dma_ultra)
  592. return 1;
  593. }
  594. /* capable of regular DMA modes */
  595. if (id->field_valid & 2) {
  596. if (hwif->mwdma_mask & id->dma_mword)
  597. return 1;
  598. if (hwif->swdma_mask & id->dma_1word)
  599. return 1;
  600. }
  601. /* consult the list of known "good" drives */
  602. if (__ide_dma_good_drive(drive) && id->eide_dma_time < 150)
  603. return 1;
  604. return 0;
  605. }
  606. EXPORT_SYMBOL_GPL(ide_use_dma);
  607. void ide_dma_verbose(ide_drive_t *drive)
  608. {
  609. struct hd_driveid *id = drive->id;
  610. ide_hwif_t *hwif = HWIF(drive);
  611. if (id->field_valid & 4) {
  612. if ((id->dma_ultra >> 8) && (id->dma_mword >> 8))
  613. goto bug_dma_off;
  614. if (id->dma_ultra & ((id->dma_ultra >> 8) & hwif->ultra_mask)) {
  615. if (((id->dma_ultra >> 11) & 0x1F) &&
  616. eighty_ninty_three(drive)) {
  617. if ((id->dma_ultra >> 15) & 1) {
  618. printk(", UDMA(mode 7)");
  619. } else if ((id->dma_ultra >> 14) & 1) {
  620. printk(", UDMA(133)");
  621. } else if ((id->dma_ultra >> 13) & 1) {
  622. printk(", UDMA(100)");
  623. } else if ((id->dma_ultra >> 12) & 1) {
  624. printk(", UDMA(66)");
  625. } else if ((id->dma_ultra >> 11) & 1) {
  626. printk(", UDMA(44)");
  627. } else
  628. goto mode_two;
  629. } else {
  630. mode_two:
  631. if ((id->dma_ultra >> 10) & 1) {
  632. printk(", UDMA(33)");
  633. } else if ((id->dma_ultra >> 9) & 1) {
  634. printk(", UDMA(25)");
  635. } else if ((id->dma_ultra >> 8) & 1) {
  636. printk(", UDMA(16)");
  637. }
  638. }
  639. } else {
  640. printk(", (U)DMA"); /* Can be BIOS-enabled! */
  641. }
  642. } else if (id->field_valid & 2) {
  643. if ((id->dma_mword >> 8) && (id->dma_1word >> 8))
  644. goto bug_dma_off;
  645. printk(", DMA");
  646. } else if (id->field_valid & 1) {
  647. printk(", BUG");
  648. }
  649. return;
  650. bug_dma_off:
  651. printk(", BUG DMA OFF");
  652. hwif->ide_dma_off_quietly(drive);
  653. return;
  654. }
  655. EXPORT_SYMBOL(ide_dma_verbose);
  656. #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
  657. int __ide_dma_lostirq (ide_drive_t *drive)
  658. {
  659. printk("%s: DMA interrupt recovery\n", drive->name);
  660. return 1;
  661. }
  662. EXPORT_SYMBOL(__ide_dma_lostirq);
  663. int __ide_dma_timeout (ide_drive_t *drive)
  664. {
  665. printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
  666. if (HWIF(drive)->ide_dma_test_irq(drive))
  667. return 0;
  668. return HWIF(drive)->ide_dma_end(drive);
  669. }
  670. EXPORT_SYMBOL(__ide_dma_timeout);
  671. /*
  672. * Needed for allowing full modular support of ide-driver
  673. */
  674. static int ide_release_dma_engine(ide_hwif_t *hwif)
  675. {
  676. if (hwif->dmatable_cpu) {
  677. pci_free_consistent(hwif->pci_dev,
  678. PRD_ENTRIES * PRD_BYTES,
  679. hwif->dmatable_cpu,
  680. hwif->dmatable_dma);
  681. hwif->dmatable_cpu = NULL;
  682. }
  683. return 1;
  684. }
  685. static int ide_release_iomio_dma(ide_hwif_t *hwif)
  686. {
  687. if ((hwif->dma_extra) && (hwif->channel == 0))
  688. release_region((hwif->dma_base + 16), hwif->dma_extra);
  689. release_region(hwif->dma_base, 8);
  690. if (hwif->dma_base2)
  691. release_region(hwif->dma_base, 8);
  692. return 1;
  693. }
  694. /*
  695. * Needed for allowing full modular support of ide-driver
  696. */
  697. int ide_release_dma (ide_hwif_t *hwif)
  698. {
  699. if (hwif->mmio == 2)
  700. return 1;
  701. if (hwif->chipset == ide_etrax100)
  702. return 1;
  703. ide_release_dma_engine(hwif);
  704. return ide_release_iomio_dma(hwif);
  705. }
  706. static int ide_allocate_dma_engine(ide_hwif_t *hwif)
  707. {
  708. hwif->dmatable_cpu = pci_alloc_consistent(hwif->pci_dev,
  709. PRD_ENTRIES * PRD_BYTES,
  710. &hwif->dmatable_dma);
  711. if (hwif->dmatable_cpu)
  712. return 0;
  713. printk(KERN_ERR "%s: -- Error, unable to allocate%s DMA table(s).\n",
  714. hwif->cds->name, !hwif->dmatable_cpu ? " CPU" : "");
  715. ide_release_dma_engine(hwif);
  716. return 1;
  717. }
  718. static int ide_mapped_mmio_dma(ide_hwif_t *hwif, unsigned long base, unsigned int ports)
  719. {
  720. printk(KERN_INFO " %s: MMIO-DMA ", hwif->name);
  721. hwif->dma_base = base;
  722. if (hwif->cds->extra && hwif->channel == 0)
  723. hwif->dma_extra = hwif->cds->extra;
  724. if(hwif->mate)
  725. hwif->dma_master = (hwif->channel) ? hwif->mate->dma_base : base;
  726. else
  727. hwif->dma_master = base;
  728. return 0;
  729. }
  730. static int ide_iomio_dma(ide_hwif_t *hwif, unsigned long base, unsigned int ports)
  731. {
  732. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx",
  733. hwif->name, base, base + ports - 1);
  734. if (!request_region(base, ports, hwif->name)) {
  735. printk(" -- Error, ports in use.\n");
  736. return 1;
  737. }
  738. hwif->dma_base = base;
  739. if ((hwif->cds->extra) && (hwif->channel == 0)) {
  740. request_region(base+16, hwif->cds->extra, hwif->cds->name);
  741. hwif->dma_extra = hwif->cds->extra;
  742. }
  743. if(hwif->mate)
  744. hwif->dma_master = (hwif->channel) ? hwif->mate->dma_base : base;
  745. else
  746. hwif->dma_master = base;
  747. if (hwif->dma_base2) {
  748. if (!request_region(hwif->dma_base2, ports, hwif->name))
  749. {
  750. printk(" -- Error, secondary ports in use.\n");
  751. release_region(base, ports);
  752. return 1;
  753. }
  754. }
  755. return 0;
  756. }
  757. static int ide_dma_iobase(ide_hwif_t *hwif, unsigned long base, unsigned int ports)
  758. {
  759. if (hwif->mmio == 2)
  760. return ide_mapped_mmio_dma(hwif, base,ports);
  761. BUG_ON(hwif->mmio == 1);
  762. return ide_iomio_dma(hwif, base, ports);
  763. }
  764. /*
  765. * This can be called for a dynamically installed interface. Don't __init it
  766. */
  767. void ide_setup_dma (ide_hwif_t *hwif, unsigned long dma_base, unsigned int num_ports)
  768. {
  769. if (ide_dma_iobase(hwif, dma_base, num_ports))
  770. return;
  771. if (ide_allocate_dma_engine(hwif)) {
  772. ide_release_dma(hwif);
  773. return;
  774. }
  775. if (!(hwif->dma_command))
  776. hwif->dma_command = hwif->dma_base;
  777. if (!(hwif->dma_vendor1))
  778. hwif->dma_vendor1 = (hwif->dma_base + 1);
  779. if (!(hwif->dma_status))
  780. hwif->dma_status = (hwif->dma_base + 2);
  781. if (!(hwif->dma_vendor3))
  782. hwif->dma_vendor3 = (hwif->dma_base + 3);
  783. if (!(hwif->dma_prdtable))
  784. hwif->dma_prdtable = (hwif->dma_base + 4);
  785. if (!hwif->ide_dma_off_quietly)
  786. hwif->ide_dma_off_quietly = &__ide_dma_off_quietly;
  787. if (!hwif->ide_dma_host_off)
  788. hwif->ide_dma_host_off = &__ide_dma_host_off;
  789. if (!hwif->ide_dma_on)
  790. hwif->ide_dma_on = &__ide_dma_on;
  791. if (!hwif->ide_dma_host_on)
  792. hwif->ide_dma_host_on = &__ide_dma_host_on;
  793. if (!hwif->ide_dma_check)
  794. hwif->ide_dma_check = &__ide_dma_check;
  795. if (!hwif->dma_setup)
  796. hwif->dma_setup = &ide_dma_setup;
  797. if (!hwif->dma_exec_cmd)
  798. hwif->dma_exec_cmd = &ide_dma_exec_cmd;
  799. if (!hwif->dma_start)
  800. hwif->dma_start = &ide_dma_start;
  801. if (!hwif->ide_dma_end)
  802. hwif->ide_dma_end = &__ide_dma_end;
  803. if (!hwif->ide_dma_test_irq)
  804. hwif->ide_dma_test_irq = &__ide_dma_test_irq;
  805. if (!hwif->ide_dma_timeout)
  806. hwif->ide_dma_timeout = &__ide_dma_timeout;
  807. if (!hwif->ide_dma_lostirq)
  808. hwif->ide_dma_lostirq = &__ide_dma_lostirq;
  809. if (hwif->chipset != ide_trm290) {
  810. u8 dma_stat = hwif->INB(hwif->dma_status);
  811. printk(", BIOS settings: %s:%s, %s:%s",
  812. hwif->drives[0].name, (dma_stat & 0x20) ? "DMA" : "pio",
  813. hwif->drives[1].name, (dma_stat & 0x40) ? "DMA" : "pio");
  814. }
  815. printk("\n");
  816. if (!(hwif->dma_master))
  817. BUG();
  818. }
  819. EXPORT_SYMBOL_GPL(ide_setup_dma);
  820. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */