blob: 3f949b5db353d56ef05584d8c106df3549e36bde [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bartlomiej Zolnierkiewicz204f47c2008-02-26 21:50:36 +01002 * IDE DMA support (including IDE PCI BM-DMA).
3 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * Copyright (C) 1995-1998 Mark Lord
5 * Copyright (C) 1999-2000 Andre Hedrick <andre@linux-ide.org>
6 * Copyright (C) 2004, 2007 Bartlomiej Zolnierkiewicz
Bartlomiej Zolnierkiewicz58f189f2008-02-01 23:09:33 +01007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * May be copied or modified under the terms of the GNU General Public License
Bartlomiej Zolnierkiewicz204f47c2008-02-26 21:50:36 +01009 *
10 * DMA is supported for all IDE devices (disk drives, cdroms, tapes, floppies).
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13/*
14 * Special Thanks to Mark for his Six years of work.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Thanks to "Christopher J. Reimer" <reimer@doe.carleton.ca> for
19 * fixing the problem with the BIOS on some Acer motherboards.
20 *
21 * Thanks to "Benoit Poulot-Cazajous" <poulot@chorus.fr> for testing
22 * "TX" chipset compatibility and for providing patches for the "TX" chipset.
23 *
24 * Thanks to Christian Brunner <chb@muc.de> for taking a good first crack
25 * at generic DMA -- his patches were referred to when preparing this code.
26 *
27 * Most importantly, thanks to Robert Bringman <rob@mars.trion.com>
28 * for supplying a Promise UDMA board & WD UDMA drive for this work!
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/types.h>
33#include <linux/kernel.h>
34#include <linux/timer.h>
35#include <linux/mm.h>
36#include <linux/interrupt.h>
37#include <linux/pci.h>
38#include <linux/init.h>
39#include <linux/ide.h>
40#include <linux/delay.h>
41#include <linux/scatterlist.h>
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +010042#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <asm/io.h>
45#include <asm/irq.h>
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static const struct drive_list_entry drive_whitelist [] = {
48
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020049 { "Micropolis 2112A" , NULL },
50 { "CONNER CTMA 4000" , NULL },
51 { "CONNER CTT8000-A" , NULL },
52 { "ST34342A" , NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 { NULL , NULL }
54};
55
56static const struct drive_list_entry drive_blacklist [] = {
57
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020058 { "WDC AC11000H" , NULL },
59 { "WDC AC22100H" , NULL },
60 { "WDC AC32500H" , NULL },
61 { "WDC AC33100H" , NULL },
62 { "WDC AC31600H" , NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 { "WDC AC32100H" , "24.09P07" },
64 { "WDC AC23200L" , "21.10N21" },
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020065 { "Compaq CRD-8241B" , NULL },
66 { "CRD-8400B" , NULL },
67 { "CRD-8480B", NULL },
68 { "CRD-8482B", NULL },
69 { "CRD-84" , NULL },
70 { "SanDisk SDP3B" , NULL },
71 { "SanDisk SDP3B-64" , NULL },
72 { "SANYO CD-ROM CRD" , NULL },
73 { "HITACHI CDR-8" , NULL },
74 { "HITACHI CDR-8335" , NULL },
75 { "HITACHI CDR-8435" , NULL },
76 { "Toshiba CD-ROM XM-6202B" , NULL },
77 { "TOSHIBA CD-ROM XM-1702BC", NULL },
78 { "CD-532E-A" , NULL },
79 { "E-IDE CD-ROM CR-840", NULL },
80 { "CD-ROM Drive/F5A", NULL },
81 { "WPI CDD-820", NULL },
82 { "SAMSUNG CD-ROM SC-148C", NULL },
83 { "SAMSUNG CD-ROM SC", NULL },
84 { "ATAPI CD-ROM DRIVE 40X MAXIMUM", NULL },
85 { "_NEC DV5800A", NULL },
Junio C Hamano5a6248c2007-05-24 02:42:38 +020086 { "SAMSUNG CD-ROM SN-124", "N001" },
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020087 { "Seagate STT20000A", NULL },
Bartlomiej Zolnierkiewiczb0bc65b2007-11-27 21:35:56 +010088 { "CD-ROM CDR_U200", "1.09" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 { NULL , NULL }
90
91};
92
93/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * ide_dma_intr - IDE DMA interrupt handler
95 * @drive: the drive the interrupt is for
96 *
97 * Handle an interrupt completing a read/write DMA transfer on an
98 * IDE device
99 */
100
101ide_startstop_t ide_dma_intr (ide_drive_t *drive)
102{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200103 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 u8 stat = 0, dma_stat = 0;
105
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200106 dma_stat = hwif->dma_ops->dma_end(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200107 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100108
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200109 if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat | ATA_DRQ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (!dma_stat) {
111 struct request *rq = HWGROUP(drive)->rq;
112
Tejun Heo4d7a9842008-01-26 20:13:11 +0100113 task_end_request(drive, rq, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return ide_stopped;
115 }
116 printk(KERN_ERR "%s: dma_intr: bad DMA status (dma_stat=%x)\n",
117 drive->name, dma_stat);
118 }
119 return ide_error(drive, "dma_intr", stat);
120}
121
122EXPORT_SYMBOL_GPL(ide_dma_intr);
123
Bartlomiej Zolnierkiewicz75d7d962007-10-13 17:47:50 +0200124static int ide_dma_good_drive(ide_drive_t *drive)
125{
126 return ide_in_drive_list(drive->id, drive_whitelist);
127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/**
130 * ide_build_sglist - map IDE scatter gather for DMA I/O
131 * @drive: the drive to build the DMA table for
132 * @rq: the request holding the sg list
133 *
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100134 * Perform the DMA mapping magic necessary to access the source or
135 * target buffers of a request via DMA. The lower layers of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * kernel provide the necessary cache management so that we can
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100137 * operate in a portable fashion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 */
139
140int ide_build_sglist(ide_drive_t *drive, struct request *rq)
141{
142 ide_hwif_t *hwif = HWIF(drive);
143 struct scatterlist *sg = hwif->sg_table;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 ide_map_sg(drive, rq);
146
147 if (rq_data_dir(rq) == READ)
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100148 hwif->sg_dma_direction = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 else
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100150 hwif->sg_dma_direction = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100152 return dma_map_sg(hwif->dev, sg, hwif->sg_nents,
153 hwif->sg_dma_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156EXPORT_SYMBOL_GPL(ide_build_sglist);
157
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100158#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/**
160 * ide_build_dmatable - build IDE DMA table
161 *
162 * ide_build_dmatable() prepares a dma request. We map the command
163 * to get the pci bus addresses of the buffers and then build up
164 * the PRD table that the IDE layer wants to be fed. The code
165 * knows about the 64K wrap bug in the CS5530.
166 *
167 * Returns the number of built PRD entries if all went okay,
168 * returns 0 otherwise.
169 *
170 * May also be invoked from trm290.c
171 */
172
173int ide_build_dmatable (ide_drive_t *drive, struct request *rq)
174{
175 ide_hwif_t *hwif = HWIF(drive);
Harvey Harrison7fa897b2008-07-24 22:53:34 +0200176 __le32 *table = (__le32 *)hwif->dmatable_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned int is_trm290 = (hwif->chipset == ide_trm290) ? 1 : 0;
178 unsigned int count = 0;
179 int i;
180 struct scatterlist *sg;
181
182 hwif->sg_nents = i = ide_build_sglist(drive, rq);
183
184 if (!i)
185 return 0;
186
187 sg = hwif->sg_table;
188 while (i) {
189 u32 cur_addr;
190 u32 cur_len;
191
192 cur_addr = sg_dma_address(sg);
193 cur_len = sg_dma_len(sg);
194
195 /*
196 * Fill in the dma table, without crossing any 64kB boundaries.
197 * Most hardware requires 16-bit alignment of all blocks,
198 * but the trm290 requires 32-bit alignment.
199 */
200
201 while (cur_len) {
202 if (count++ >= PRD_ENTRIES) {
203 printk(KERN_ERR "%s: DMA table too small\n", drive->name);
204 goto use_pio_instead;
205 } else {
206 u32 xcount, bcount = 0x10000 - (cur_addr & 0xffff);
207
208 if (bcount > cur_len)
209 bcount = cur_len;
210 *table++ = cpu_to_le32(cur_addr);
211 xcount = bcount & 0xffff;
212 if (is_trm290)
213 xcount = ((xcount >> 2) - 1) << 16;
Sergei Shtylylov22e05b42008-10-05 18:23:27 +0200214 else if (xcount == 0x0000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /*
216 * Most chipsets correctly interpret a length of 0x0000 as 64KB,
217 * but at least one (e.g. CS5530) misinterprets it as zero (!).
218 * So here we break the 64KB entry into two 32KB entries instead.
219 */
220 if (count++ >= PRD_ENTRIES) {
221 printk(KERN_ERR "%s: DMA table too small\n", drive->name);
222 goto use_pio_instead;
223 }
224 *table++ = cpu_to_le32(0x8000);
225 *table++ = cpu_to_le32(cur_addr + 0x8000);
226 xcount = 0x8000;
227 }
228 *table++ = cpu_to_le32(xcount);
229 cur_addr += bcount;
230 cur_len -= bcount;
231 }
232 }
233
Jens Axboe55c16a72007-07-25 08:13:56 +0200234 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 i--;
236 }
237
238 if (count) {
239 if (!is_trm290)
240 *--table |= cpu_to_le32(0x80000000);
241 return count;
242 }
Bartlomiej Zolnierkiewiczf6fb7862008-02-01 23:09:31 +0100243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 printk(KERN_ERR "%s: empty DMA table?\n", drive->name);
Bartlomiej Zolnierkiewiczf6fb7862008-02-01 23:09:31 +0100245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246use_pio_instead:
Bartlomiej Zolnierkiewiczf6fb7862008-02-01 23:09:31 +0100247 ide_destroy_dmatable(drive);
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0; /* revert to PIO for this request */
250}
251
252EXPORT_SYMBOL_GPL(ide_build_dmatable);
Bartlomiej Zolnierkiewicz062f9f02008-02-01 23:09:32 +0100253#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255/**
256 * ide_destroy_dmatable - clean up DMA mapping
257 * @drive: The drive to unmap
258 *
259 * Teardown mappings after DMA has completed. This must be called
260 * after the completion of each use of ide_build_dmatable and before
261 * the next use of ide_build_dmatable. Failure to do so will cause
262 * an oops as only one mapping can be live for each target at a given
263 * time.
264 */
265
266void ide_destroy_dmatable (ide_drive_t *drive)
267{
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100268 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100270 dma_unmap_sg(hwif->dev, hwif->sg_table, hwif->sg_nents,
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100271 hwif->sg_dma_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274EXPORT_SYMBOL_GPL(ide_destroy_dmatable);
275
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100276#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/**
278 * config_drive_for_dma - attempt to activate IDE DMA
279 * @drive: the drive to place in DMA mode
280 *
281 * If the drive supports at least mode 2 DMA or UDMA of any kind
282 * then attempt to place it into DMA mode. Drives that are known to
283 * support DMA but predate the DMA properties or that are known
284 * to have DMA handling bugs are also set up appropriately based
285 * on the good/bad drive lists.
286 */
287
288static int config_drive_for_dma (ide_drive_t *drive)
289{
Bartlomiej Zolnierkiewicz1116fae2007-08-20 22:42:55 +0200290 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200291 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200293 if (drive->media != ide_disk) {
294 if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
Bartlomiej Zolnierkiewiczbcbf6ee2007-11-05 21:42:30 +0100295 return 0;
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200296 }
Bartlomiej Zolnierkiewicz1116fae2007-08-20 22:42:55 +0200297
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200298 /*
299 * Enable DMA on any drive that has
300 * UltraDMA (mode 0/1/2/3/4/5/6) enabled
301 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200302 if ((id[ATA_ID_FIELD_VALID] & 4) &&
303 ((id[ATA_ID_UDMA_MODES] >> 8) & 0x7f))
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200304 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200306 /*
307 * Enable DMA on any drive that has mode2 DMA
308 * (multi or single) enabled
309 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200310 if (id[ATA_ID_FIELD_VALID] & 2) /* regular DMA */
311 if ((id[ATA_ID_MWDMA_MODES] & 0x404) == 0x404 ||
312 (id[ATA_ID_SWDMA_MODES] & 0x404) == 0x404)
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200313 return 1;
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100314
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200315 /* Consult the list of known "good" drives */
316 if (ide_dma_good_drive(drive))
317 return 1;
318
319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322/**
323 * dma_timer_expiry - handle a DMA timeout
324 * @drive: Drive that timed out
325 *
326 * An IDE DMA transfer timed out. In the event of an error we ask
327 * the driver to resolve the problem, if a DMA transfer is still
328 * in progress we continue to wait (arguably we need to add a
329 * secondary 'I don't care what the drive thinks' timeout here)
330 * Finally if we have an interrupt we let it complete the I/O.
331 * But only one time - we clear expiry and if it's still not
332 * completed after WAIT_CMD, we error and retry in PIO.
333 * This can occur if an interrupt is lost or due to hang or bugs.
334 */
335
336static int dma_timer_expiry (ide_drive_t *drive)
337{
338 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200339 u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 printk(KERN_WARNING "%s: dma_timer_expiry: dma status == 0x%02x\n",
342 drive->name, dma_stat);
343
344 if ((dma_stat & 0x18) == 0x18) /* BUSY Stupid Early Timer !! */
345 return WAIT_CMD;
346
347 HWGROUP(drive)->expiry = NULL; /* one free ride for now */
348
349 /* 1 dmaing, 2 error, 4 intr */
350 if (dma_stat & 2) /* ERROR */
351 return -1;
352
353 if (dma_stat & 1) /* DMAing */
354 return WAIT_CMD;
355
356 if (dma_stat & 4) /* Got an Interrupt */
357 return WAIT_CMD;
358
359 return 0; /* Status is unknown -- reset the bus */
360}
361
362/**
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100363 * ide_dma_host_set - Enable/disable DMA on a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * @drive: drive to control
365 *
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100366 * Enable/disable DMA on an IDE controller following generic
367 * bus-mastering IDE controller behaviour.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
369
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100370void ide_dma_host_set(ide_drive_t *drive, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200373 u8 unit = drive->dn & 1;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200374 u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100376 if (on)
377 dma_stat |= (1 << (5 + unit));
378 else
379 dma_stat &= ~(1 << (5 + unit));
380
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200381 if (hwif->host_flags & IDE_HFLAG_MMIO)
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200382 writeb(dma_stat,
383 (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200384 else
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200385 outb(dma_stat, hwif->dma_base + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100388EXPORT_SYMBOL_GPL(ide_dma_host_set);
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100389#endif /* CONFIG_BLK_DEV_IDEDMA_SFF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391/**
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100392 * ide_dma_off_quietly - Generic DMA kill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 * @drive: drive to control
394 *
395 * Turn off the current DMA on this IDE controller.
396 */
397
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100398void ide_dma_off_quietly(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200400 drive->dev_flags &= ~IDE_DFLAG_USING_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 ide_toggle_bounce(drive, 0);
402
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200403 drive->hwif->dma_ops->dma_host_set(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100406EXPORT_SYMBOL(ide_dma_off_quietly);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408/**
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100409 * ide_dma_off - disable DMA on a device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * @drive: drive to disable DMA on
411 *
412 * Disable IDE DMA for a device on this IDE controller.
413 * Inform the user that DMA has been disabled.
414 */
415
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100416void ide_dma_off(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 printk(KERN_INFO "%s: DMA disabled\n", drive->name);
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100419 ide_dma_off_quietly(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100422EXPORT_SYMBOL(ide_dma_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/**
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100425 * ide_dma_on - Enable DMA on a device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * @drive: drive to enable DMA on
427 *
428 * Enable IDE DMA for a device on this IDE controller.
429 */
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100430
431void ide_dma_on(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200433 drive->dev_flags |= IDE_DFLAG_USING_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 ide_toggle_bounce(drive, 1);
435
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200436 drive->hwif->dma_ops->dma_host_set(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100439#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * ide_dma_setup - begin a DMA phase
442 * @drive: target device
443 *
444 * Build an IDE DMA PRD (IDE speak for scatter gather table)
445 * and then set up the DMA transfer registers for a device
446 * that follows generic IDE PCI DMA behaviour. Controllers can
447 * override this function if they need to
448 *
449 * Returns 0 on success. If a PIO fallback is required then 1
450 * is returned.
451 */
452
453int ide_dma_setup(ide_drive_t *drive)
454{
455 ide_hwif_t *hwif = drive->hwif;
456 struct request *rq = HWGROUP(drive)->rq;
457 unsigned int reading;
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200458 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 u8 dma_stat;
460
461 if (rq_data_dir(rq))
462 reading = 0;
463 else
464 reading = 1 << 3;
465
466 /* fall back to pio! */
467 if (!ide_build_dmatable(drive, rq)) {
468 ide_map_sg(drive, rq);
469 return 1;
470 }
471
472 /* PRD table */
Bartlomiej Zolnierkiewicz13572142008-07-15 21:21:49 +0200473 if (hwif->host_flags & IDE_HFLAG_MMIO)
Bartlomiej Zolnierkiewicz55224bc2008-04-28 23:44:42 +0200474 writel(hwif->dmatable_dma,
475 (void __iomem *)(hwif->dma_base + ATA_DMA_TABLE_OFS));
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100476 else
Bartlomiej Zolnierkiewicz55224bc2008-04-28 23:44:42 +0200477 outl(hwif->dmatable_dma, hwif->dma_base + ATA_DMA_TABLE_OFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /* specify r/w */
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200480 if (mmio)
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200481 writeb(reading, (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200482 else
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200483 outb(reading, hwif->dma_base + ATA_DMA_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Bartlomiej Zolnierkiewiczb2f951a2008-07-23 19:55:50 +0200485 /* read DMA status for INTR & ERROR flags */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200486 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* clear INTR & ERROR flags */
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200489 if (mmio)
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200490 writeb(dma_stat | 6,
491 (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200492 else
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200493 outb(dma_stat | 6, hwif->dma_base + ATA_DMA_STATUS);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 drive->waiting_for_dma = 1;
496 return 0;
497}
498
499EXPORT_SYMBOL_GPL(ide_dma_setup);
500
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200501void ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 /* issue cmd to drive */
504 ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, dma_timer_expiry);
505}
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200506EXPORT_SYMBOL_GPL(ide_dma_exec_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508void ide_dma_start(ide_drive_t *drive)
509{
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200510 ide_hwif_t *hwif = drive->hwif;
511 u8 dma_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* Note that this is done *after* the cmd has
514 * been issued to the drive, as per the BM-IDE spec.
515 * The Promise Ultra33 doesn't work correctly when
516 * we do this part before issuing the drive cmd.
517 */
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200518 if (hwif->host_flags & IDE_HFLAG_MMIO) {
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200519 dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200520 /* start DMA */
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200521 writeb(dma_cmd | 1,
522 (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200523 } else {
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200524 dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
525 outb(dma_cmd | 1, hwif->dma_base + ATA_DMA_CMD);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200526 }
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 wmb();
529}
530
531EXPORT_SYMBOL_GPL(ide_dma_start);
532
533/* returns 1 on error, 0 otherwise */
Bartlomiej Zolnierkiewicz653bcf52008-10-13 21:39:46 +0200534int ide_dma_end(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200536 ide_hwif_t *hwif = drive->hwif;
537 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 u8 dma_stat = 0, dma_cmd = 0;
539
540 drive->waiting_for_dma = 0;
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200541
542 if (mmio) {
543 /* get DMA command mode */
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200544 dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200545 /* stop DMA */
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200546 writeb(dma_cmd & ~1,
547 (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200548 } else {
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200549 dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
550 outb(dma_cmd & ~1, hwif->dma_base + ATA_DMA_CMD);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200551 }
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* get DMA status */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200554 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200555
556 if (mmio)
557 /* clear the INTR & ERROR bits */
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200558 writeb(dma_stat | 6,
559 (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200560 else
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200561 outb(dma_stat | 6, hwif->dma_base + ATA_DMA_STATUS);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* purge DMA mappings */
564 ide_destroy_dmatable(drive);
565 /* verify good DMA status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 wmb();
567 return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0;
568}
Bartlomiej Zolnierkiewicz653bcf52008-10-13 21:39:46 +0200569EXPORT_SYMBOL_GPL(ide_dma_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571/* returns 1 if dma irq issued, 0 otherwise */
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200572int ide_dma_test_irq(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200575 u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* return 1 if INTR asserted */
578 if ((dma_stat & 4) == 4)
579 return 1;
Bartlomiej Zolnierkiewiczc67c2162008-10-13 21:39:38 +0200580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 0;
582}
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200583EXPORT_SYMBOL_GPL(ide_dma_test_irq);
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200584#else
585static inline int config_drive_for_dma(ide_drive_t *drive) { return 0; }
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100586#endif /* CONFIG_BLK_DEV_IDEDMA_SFF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588int __ide_dma_bad_drive (ide_drive_t *drive)
589{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200590 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Jordan Crouse65e5f2e2005-12-15 02:16:18 +0100592 int blacklist = ide_in_drive_list(id, drive_blacklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (blacklist) {
594 printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n",
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200595 drive->name, (char *)&id[ATA_ID_PROD]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return blacklist;
597 }
598 return 0;
599}
600
601EXPORT_SYMBOL(__ide_dma_bad_drive);
602
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200603static const u8 xfer_mode_bases[] = {
604 XFER_UDMA_0,
605 XFER_MW_DMA_0,
606 XFER_SW_DMA_0,
607};
608
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200609static unsigned int ide_get_mode_mask(ide_drive_t *drive, u8 base, u8 req_mode)
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200610{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200611 u16 *id = drive->id;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200612 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200613 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200614 unsigned int mask = 0;
615
616 switch(base) {
617 case XFER_UDMA_0:
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200618 if ((id[ATA_ID_FIELD_VALID] & 4) == 0)
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200619 break;
620
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200621 if (port_ops && port_ops->udma_filter)
622 mask = port_ops->udma_filter(drive);
Sergei Shtylyov851dd332007-10-11 23:53:59 +0200623 else
624 mask = hwif->ultra_mask;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200625 mask &= id[ATA_ID_UDMA_MODES];
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200626
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200627 /*
628 * avoid false cable warning from eighty_ninty_three()
629 */
630 if (req_mode > XFER_UDMA_2) {
631 if ((mask & 0x78) && (eighty_ninty_three(drive) == 0))
632 mask &= 0x07;
633 }
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200634 break;
635 case XFER_MW_DMA_0:
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200636 if ((id[ATA_ID_FIELD_VALID] & 2) == 0)
Sergei Shtylyovb4e44362007-10-11 23:53:58 +0200637 break;
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200638 if (port_ops && port_ops->mdma_filter)
639 mask = port_ops->mdma_filter(drive);
Sergei Shtylyovb4e44362007-10-11 23:53:58 +0200640 else
641 mask = hwif->mwdma_mask;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200642 mask &= id[ATA_ID_MWDMA_MODES];
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200643 break;
644 case XFER_SW_DMA_0:
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200645 if (id[ATA_ID_FIELD_VALID] & 2) {
646 mask = id[ATA_ID_SWDMA_MODES] & hwif->swdma_mask;
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200647 } else if (id[ATA_ID_OLD_DMA_MODES] >> 8) {
648 u8 mode = id[ATA_ID_OLD_DMA_MODES] >> 8;
Bartlomiej Zolnierkiewicz15a4f942007-07-09 23:17:57 +0200649
650 /*
651 * if the mode is valid convert it to the mask
652 * (the maximum allowed mode is XFER_SW_DMA_2)
653 */
654 if (mode <= 2)
655 mask = ((2 << mode) - 1) & hwif->swdma_mask;
656 }
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200657 break;
658 default:
659 BUG();
660 break;
661 }
662
663 return mask;
664}
665
666/**
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200667 * ide_find_dma_mode - compute DMA speed
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200668 * @drive: IDE device
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200669 * @req_mode: requested mode
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200670 *
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200671 * Checks the drive/host capabilities and finds the speed to use for
672 * the DMA transfer. The speed is then limited by the requested mode.
673 *
674 * Returns 0 if the drive/host combination is incapable of DMA transfers
675 * or if the requested mode is not a DMA mode.
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200676 */
677
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200678u8 ide_find_dma_mode(ide_drive_t *drive, u8 req_mode)
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200679{
680 ide_hwif_t *hwif = drive->hwif;
681 unsigned int mask;
682 int x, i;
683 u8 mode = 0;
684
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200685 if (drive->media != ide_disk) {
686 if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
687 return 0;
688 }
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200689
690 for (i = 0; i < ARRAY_SIZE(xfer_mode_bases); i++) {
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200691 if (req_mode < xfer_mode_bases[i])
692 continue;
693 mask = ide_get_mode_mask(drive, xfer_mode_bases[i], req_mode);
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200694 x = fls(mask) - 1;
695 if (x >= 0) {
696 mode = xfer_mode_bases[i] + x;
697 break;
698 }
699 }
700
Bartlomiej Zolnierkiewicz75d7d962007-10-13 17:47:50 +0200701 if (hwif->chipset == ide_acorn && mode == 0) {
702 /*
703 * is this correct?
704 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200705 if (ide_dma_good_drive(drive) &&
706 drive->id[ATA_ID_EIDE_DMA_TIME] < 150)
Bartlomiej Zolnierkiewicz75d7d962007-10-13 17:47:50 +0200707 mode = XFER_MW_DMA_1;
708 }
709
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100710 mode = min(mode, req_mode);
711
712 printk(KERN_INFO "%s: %s mode selected\n", drive->name,
Bartlomiej Zolnierkiewiczd34887d2007-11-05 21:42:27 +0100713 mode ? ide_xfer_verbose(mode) : "no DMA");
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200714
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100715 return mode;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200716}
717
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200718EXPORT_SYMBOL_GPL(ide_find_dma_mode);
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200719
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200720static int ide_tune_dma(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200721{
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +0100722 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200723 u8 speed;
724
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200725 if (ata_id_has_dma(drive->id) == 0 ||
726 (drive->dev_flags & IDE_DFLAG_NODMA))
Bartlomiej Zolnierkiewicz122ab082007-05-16 00:51:46 +0200727 return 0;
728
729 /* consult the list of known "bad" drives */
730 if (__ide_dma_bad_drive(drive))
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200731 return 0;
732
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100733 if (ide_id_dma_bug(drive))
734 return 0;
735
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +0100736 if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200737 return config_drive_for_dma(drive);
738
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200739 speed = ide_max_dma_mode(drive);
740
Bartlomiej Zolnierkiewicz951784b2008-04-26 17:36:38 +0200741 if (!speed)
742 return 0;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200743
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200744 if (ide_set_dma_mode(drive, speed))
Bartlomiej Zolnierkiewicz4728d542007-05-16 00:51:46 +0200745 return 0;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200746
Bartlomiej Zolnierkiewicz4728d542007-05-16 00:51:46 +0200747 return 1;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200748}
749
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200750static int ide_dma_check(ide_drive_t *drive)
751{
752 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200753
Bartlomiej Zolnierkiewiczba4b2e62008-07-23 19:55:55 +0200754 if (ide_tune_dma(drive))
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200755 return 0;
756
757 /* TODO: always do PIO fallback */
758 if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
759 return -1;
760
761 ide_set_max_pio(drive);
762
Bartlomiej Zolnierkiewiczba4b2e62008-07-23 19:55:55 +0200763 return -1;
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200764}
765
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100766int ide_id_dma_bug(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200768 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200770 if (id[ATA_ID_FIELD_VALID] & 4) {
771 if ((id[ATA_ID_UDMA_MODES] >> 8) &&
772 (id[ATA_ID_MWDMA_MODES] >> 8))
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100773 goto err_out;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200774 } else if (id[ATA_ID_FIELD_VALID] & 2) {
775 if ((id[ATA_ID_MWDMA_MODES] >> 8) &&
776 (id[ATA_ID_SWDMA_MODES] >> 8))
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100777 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100779 return 0;
780err_out:
781 printk(KERN_ERR "%s: bad DMA info in identify block\n", drive->name);
782 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100785int ide_set_dma(ide_drive_t *drive)
786{
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100787 int rc;
788
Bartlomiej Zolnierkiewicz7b905992008-01-25 22:17:19 +0100789 /*
790 * Force DMAing for the beginning of the check.
791 * Some chipsets appear to do interesting
792 * things, if not checked and cleared.
793 * PARANOIA!!!
794 */
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100795 ide_dma_off_quietly(drive);
Bartlomiej Zolnierkiewicz7b905992008-01-25 22:17:19 +0100796
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200797 rc = ide_dma_check(drive);
Bartlomiej Zolnierkiewicz7b905992008-01-25 22:17:19 +0100798 if (rc)
799 return rc;
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100800
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100801 ide_dma_on(drive);
802
803 return 0;
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100804}
805
Bartlomiej Zolnierkiewicz578cfa02008-02-02 19:56:47 +0100806void ide_check_dma_crc(ide_drive_t *drive)
807{
808 u8 mode;
809
810 ide_dma_off_quietly(drive);
811 drive->crc_count = 0;
812 mode = drive->current_speed;
813 /*
814 * Don't try non Ultra-DMA modes without iCRC's. Force the
815 * device to PIO and make the user enable SWDMA/MWDMA modes.
816 */
817 if (mode > XFER_UDMA_0 && mode <= XFER_UDMA_7)
818 mode--;
819 else
820 mode = XFER_PIO_4;
821 ide_set_xfer_rate(drive, mode);
822 if (drive->current_speed >= XFER_SW_DMA_0)
823 ide_dma_on(drive);
824}
825
Bartlomiej Zolnierkiewiczde23ec92008-10-13 21:39:46 +0200826void ide_dma_lost_irq(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Bartlomiej Zolnierkiewiczde23ec92008-10-13 21:39:46 +0200828 printk(KERN_ERR "%s: DMA interrupt recovery\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
Bartlomiej Zolnierkiewiczde23ec92008-10-13 21:39:46 +0200830EXPORT_SYMBOL_GPL(ide_dma_lost_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Bartlomiej Zolnierkiewiczffa15a62008-10-13 21:39:46 +0200832void ide_dma_timeout(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200834 ide_hwif_t *hwif = HWIF(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200836 printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
837
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200838 if (hwif->dma_ops->dma_test_irq(drive))
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200839 return;
840
Bartlomiej Zolnierkiewiczffa15a62008-10-13 21:39:46 +0200841 ide_dump_status(drive, "DMA timeout", hwif->tp_ops->read_status(hwif));
842
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200843 hwif->dma_ops->dma_end(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
Bartlomiej Zolnierkiewiczffa15a62008-10-13 21:39:46 +0200845EXPORT_SYMBOL_GPL(ide_dma_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Bartlomiej Zolnierkiewicz0d1bad22008-04-26 22:25:19 +0200847void ide_release_dma_engine(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 if (hwif->dmatable_cpu) {
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200850 int prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100851
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200852 dma_free_coherent(hwif->dev, prd_size,
853 hwif->dmatable_cpu, hwif->dmatable_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 hwif->dmatable_cpu = NULL;
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200857EXPORT_SYMBOL_GPL(ide_release_dma_engine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Bartlomiej Zolnierkiewiczb8e73fb2008-04-26 22:25:21 +0200859int ide_allocate_dma_engine(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200861 int prd_size;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100862
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200863 if (hwif->prd_max_nents == 0)
864 hwif->prd_max_nents = PRD_ENTRIES;
865 if (hwif->prd_ent_size == 0)
866 hwif->prd_ent_size = PRD_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200868 prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200870 hwif->dmatable_cpu = dma_alloc_coherent(hwif->dev, prd_size,
871 &hwif->dmatable_dma,
872 GFP_ATOMIC);
873 if (hwif->dmatable_cpu == NULL) {
874 printk(KERN_ERR "%s: unable to allocate PRD table\n",
Bartlomiej Zolnierkiewicz5e59c232008-04-26 22:25:20 +0200875 hwif->name);
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200876 return -ENOMEM;
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200879 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
Bartlomiej Zolnierkiewiczb8e73fb2008-04-26 22:25:21 +0200881EXPORT_SYMBOL_GPL(ide_allocate_dma_engine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200883#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
Bartlomiej Zolnierkiewicz81e8d5a2008-07-23 19:55:51 +0200884const struct ide_dma_ops sff_dma_ops = {
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200885 .dma_host_set = ide_dma_host_set,
886 .dma_setup = ide_dma_setup,
887 .dma_exec_cmd = ide_dma_exec_cmd,
888 .dma_start = ide_dma_start,
Bartlomiej Zolnierkiewicz653bcf52008-10-13 21:39:46 +0200889 .dma_end = ide_dma_end,
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200890 .dma_test_irq = ide_dma_test_irq,
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200891 .dma_timeout = ide_dma_timeout,
892 .dma_lost_irq = ide_dma_lost_irq,
893};
Bartlomiej Zolnierkiewicz81e8d5a2008-07-23 19:55:51 +0200894EXPORT_SYMBOL_GPL(sff_dma_ops);
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100895#endif /* CONFIG_BLK_DEV_IDEDMA_SFF */