blob: d935a6ec022f63b6010c5a156ff37dc7963b4753 [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/types.h>
32#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/ide.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/scatterlist.h>
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +010035#include <linux/dma-mapping.h>
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +020036#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +020038static const struct drive_list_entry drive_whitelist[] = {
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020039 { "Micropolis 2112A" , NULL },
40 { "CONNER CTMA 4000" , NULL },
41 { "CONNER CTT8000-A" , NULL },
42 { "ST34342A" , NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 { NULL , NULL }
44};
45
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +020046static const struct drive_list_entry drive_blacklist[] = {
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020047 { "WDC AC11000H" , NULL },
48 { "WDC AC22100H" , NULL },
49 { "WDC AC32500H" , NULL },
50 { "WDC AC33100H" , NULL },
51 { "WDC AC31600H" , NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 { "WDC AC32100H" , "24.09P07" },
53 { "WDC AC23200L" , "21.10N21" },
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020054 { "Compaq CRD-8241B" , NULL },
55 { "CRD-8400B" , NULL },
56 { "CRD-8480B", NULL },
57 { "CRD-8482B", NULL },
58 { "CRD-84" , NULL },
59 { "SanDisk SDP3B" , NULL },
60 { "SanDisk SDP3B-64" , NULL },
61 { "SANYO CD-ROM CRD" , NULL },
62 { "HITACHI CDR-8" , NULL },
63 { "HITACHI CDR-8335" , NULL },
64 { "HITACHI CDR-8435" , NULL },
65 { "Toshiba CD-ROM XM-6202B" , NULL },
66 { "TOSHIBA CD-ROM XM-1702BC", NULL },
67 { "CD-532E-A" , NULL },
68 { "E-IDE CD-ROM CR-840", NULL },
69 { "CD-ROM Drive/F5A", NULL },
70 { "WPI CDD-820", NULL },
71 { "SAMSUNG CD-ROM SC-148C", NULL },
72 { "SAMSUNG CD-ROM SC", NULL },
73 { "ATAPI CD-ROM DRIVE 40X MAXIMUM", NULL },
74 { "_NEC DV5800A", NULL },
Junio C Hamano5a6248c2007-05-24 02:42:38 +020075 { "SAMSUNG CD-ROM SN-124", "N001" },
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020076 { "Seagate STT20000A", NULL },
Bartlomiej Zolnierkiewiczb0bc65b2007-11-27 21:35:56 +010077 { "CD-ROM CDR_U200", "1.09" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 { NULL , NULL }
79
80};
81
82/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * ide_dma_intr - IDE DMA interrupt handler
84 * @drive: the drive the interrupt is for
85 *
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +020086 * Handle an interrupt completing a read/write DMA transfer on an
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * IDE device
88 */
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +020089
90ide_startstop_t ide_dma_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +020092 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 u8 stat = 0, dma_stat = 0;
94
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +020095 dma_stat = hwif->dma_ops->dma_end(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020096 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +010097
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +020098 if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat | ATA_DRQ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (!dma_stat) {
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200100 struct request *rq = hwif->hwgroup->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Tejun Heo4d7a9842008-01-26 20:13:11 +0100102 task_end_request(drive, rq, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return ide_stopped;
104 }
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200105 printk(KERN_ERR "%s: %s: bad DMA status (0x%02x)\n",
106 drive->name, __func__, dma_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108 return ide_error(drive, "dma_intr", stat);
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110EXPORT_SYMBOL_GPL(ide_dma_intr);
111
Bartlomiej Zolnierkiewicz75d7d962007-10-13 17:47:50 +0200112static int ide_dma_good_drive(ide_drive_t *drive)
113{
114 return ide_in_drive_list(drive->id, drive_whitelist);
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/**
118 * ide_build_sglist - map IDE scatter gather for DMA I/O
119 * @drive: the drive to build the DMA table for
120 * @rq: the request holding the sg list
121 *
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100122 * Perform the DMA mapping magic necessary to access the source or
123 * target buffers of a request via DMA. The lower layers of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * kernel provide the necessary cache management so that we can
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100125 * operate in a portable fashion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
127
128int ide_build_sglist(ide_drive_t *drive, struct request *rq)
129{
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200130 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct scatterlist *sg = hwif->sg_table;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 ide_map_sg(drive, rq);
134
135 if (rq_data_dir(rq) == READ)
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100136 hwif->sg_dma_direction = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 else
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100138 hwif->sg_dma_direction = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100140 return dma_map_sg(hwif->dev, sg, hwif->sg_nents,
141 hwif->sg_dma_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143EXPORT_SYMBOL_GPL(ide_build_sglist);
144
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100145#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/**
147 * ide_build_dmatable - build IDE DMA table
148 *
149 * ide_build_dmatable() prepares a dma request. We map the command
150 * to get the pci bus addresses of the buffers and then build up
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200151 * the PRD table that the IDE layer wants to be fed.
152 *
153 * Most chipsets correctly interpret a length of 0x0000 as 64KB,
154 * but at least one (e.g. CS5530) misinterprets it as zero (!).
155 * So we break the 64KB entry into two 32KB entries instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 *
157 * Returns the number of built PRD entries if all went okay,
158 * returns 0 otherwise.
159 *
160 * May also be invoked from trm290.c
161 */
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200162
163int ide_build_dmatable(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200165 ide_hwif_t *hwif = drive->hwif;
Harvey Harrison7fa897b2008-07-24 22:53:34 +0200166 __le32 *table = (__le32 *)hwif->dmatable_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 unsigned int is_trm290 = (hwif->chipset == ide_trm290) ? 1 : 0;
168 unsigned int count = 0;
169 int i;
170 struct scatterlist *sg;
171
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200172 hwif->sg_nents = ide_build_sglist(drive, rq);
173 if (hwif->sg_nents == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 0;
175
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200176 for_each_sg(hwif->sg_table, sg, hwif->sg_nents, i) {
177 u32 cur_addr, cur_len, xcount, bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 cur_addr = sg_dma_address(sg);
180 cur_len = sg_dma_len(sg);
181
182 /*
183 * Fill in the dma table, without crossing any 64kB boundaries.
184 * Most hardware requires 16-bit alignment of all blocks,
185 * but the trm290 requires 32-bit alignment.
186 */
187
188 while (cur_len) {
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200189 if (count++ >= PRD_ENTRIES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto use_pio_instead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200192 bcount = 0x10000 - (cur_addr & 0xffff);
193 if (bcount > cur_len)
194 bcount = cur_len;
195 *table++ = cpu_to_le32(cur_addr);
196 xcount = bcount & 0xffff;
197 if (is_trm290)
198 xcount = ((xcount >> 2) - 1) << 16;
199 if (xcount == 0x0000) {
200 if (count++ >= PRD_ENTRIES)
201 goto use_pio_instead;
202 *table++ = cpu_to_le32(0x8000);
203 *table++ = cpu_to_le32(cur_addr + 0x8000);
204 xcount = 0x8000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200206 *table++ = cpu_to_le32(xcount);
207 cur_addr += bcount;
208 cur_len -= bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
212 if (count) {
213 if (!is_trm290)
214 *--table |= cpu_to_le32(0x80000000);
215 return count;
216 }
Bartlomiej Zolnierkiewiczf6fb7862008-02-01 23:09:31 +0100217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218use_pio_instead:
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200219 printk(KERN_ERR "%s: %s\n", drive->name,
220 count ? "DMA table too small" : "empty DMA table?");
221
Bartlomiej Zolnierkiewiczf6fb7862008-02-01 23:09:31 +0100222 ide_destroy_dmatable(drive);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 0; /* revert to PIO for this request */
225}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226EXPORT_SYMBOL_GPL(ide_build_dmatable);
Bartlomiej Zolnierkiewicz062f9f02008-02-01 23:09:32 +0100227#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229/**
230 * ide_destroy_dmatable - clean up DMA mapping
231 * @drive: The drive to unmap
232 *
233 * Teardown mappings after DMA has completed. This must be called
234 * after the completion of each use of ide_build_dmatable and before
235 * the next use of ide_build_dmatable. Failure to do so will cause
236 * an oops as only one mapping can be live for each target at a given
237 * time.
238 */
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200239
240void ide_destroy_dmatable(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100242 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100244 dma_unmap_sg(hwif->dev, hwif->sg_table, hwif->sg_nents,
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100245 hwif->sg_dma_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247EXPORT_SYMBOL_GPL(ide_destroy_dmatable);
248
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100249#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/**
251 * config_drive_for_dma - attempt to activate IDE DMA
252 * @drive: the drive to place in DMA mode
253 *
254 * If the drive supports at least mode 2 DMA or UDMA of any kind
255 * then attempt to place it into DMA mode. Drives that are known to
256 * support DMA but predate the DMA properties or that are known
257 * to have DMA handling bugs are also set up appropriately based
258 * on the good/bad drive lists.
259 */
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200260
261static int config_drive_for_dma(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Bartlomiej Zolnierkiewicz1116fae2007-08-20 22:42:55 +0200263 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200264 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200266 if (drive->media != ide_disk) {
267 if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
Bartlomiej Zolnierkiewiczbcbf6ee2007-11-05 21:42:30 +0100268 return 0;
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200269 }
Bartlomiej Zolnierkiewicz1116fae2007-08-20 22:42:55 +0200270
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200271 /*
272 * Enable DMA on any drive that has
273 * UltraDMA (mode 0/1/2/3/4/5/6) enabled
274 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200275 if ((id[ATA_ID_FIELD_VALID] & 4) &&
276 ((id[ATA_ID_UDMA_MODES] >> 8) & 0x7f))
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200277 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200279 /*
280 * Enable DMA on any drive that has mode2 DMA
281 * (multi or single) enabled
282 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200283 if (id[ATA_ID_FIELD_VALID] & 2) /* regular DMA */
284 if ((id[ATA_ID_MWDMA_MODES] & 0x404) == 0x404 ||
285 (id[ATA_ID_SWDMA_MODES] & 0x404) == 0x404)
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200286 return 1;
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100287
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200288 /* Consult the list of known "good" drives */
289 if (ide_dma_good_drive(drive))
290 return 1;
291
292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295/**
296 * dma_timer_expiry - handle a DMA timeout
297 * @drive: Drive that timed out
298 *
299 * An IDE DMA transfer timed out. In the event of an error we ask
300 * the driver to resolve the problem, if a DMA transfer is still
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200301 * in progress we continue to wait (arguably we need to add a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * secondary 'I don't care what the drive thinks' timeout here)
303 * Finally if we have an interrupt we let it complete the I/O.
304 * But only one time - we clear expiry and if it's still not
305 * completed after WAIT_CMD, we error and retry in PIO.
306 * This can occur if an interrupt is lost or due to hang or bugs.
307 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200309static int dma_timer_expiry(ide_drive_t *drive)
310{
311 ide_hwif_t *hwif = drive->hwif;
312 u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
313
314 printk(KERN_WARNING "%s: %s: DMA status (0x%02x)\n",
315 drive->name, __func__, dma_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 if ((dma_stat & 0x18) == 0x18) /* BUSY Stupid Early Timer !! */
318 return WAIT_CMD;
319
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200320 hwif->hwgroup->expiry = NULL; /* one free ride for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 /* 1 dmaing, 2 error, 4 intr */
323 if (dma_stat & 2) /* ERROR */
324 return -1;
325
326 if (dma_stat & 1) /* DMAing */
327 return WAIT_CMD;
328
329 if (dma_stat & 4) /* Got an Interrupt */
330 return WAIT_CMD;
331
332 return 0; /* Status is unknown -- reset the bus */
333}
334
335/**
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100336 * ide_dma_host_set - Enable/disable DMA on a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * @drive: drive to control
338 *
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100339 * Enable/disable DMA on an IDE controller following generic
340 * bus-mastering IDE controller behaviour.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 */
342
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100343void ide_dma_host_set(ide_drive_t *drive, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200345 ide_hwif_t *hwif = drive->hwif;
346 u8 unit = drive->dn & 1;
347 u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100349 if (on)
350 dma_stat |= (1 << (5 + unit));
351 else
352 dma_stat &= ~(1 << (5 + unit));
353
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200354 if (hwif->host_flags & IDE_HFLAG_MMIO)
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200355 writeb(dma_stat,
356 (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200357 else
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200358 outb(dma_stat, hwif->dma_base + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100360EXPORT_SYMBOL_GPL(ide_dma_host_set);
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100361#endif /* CONFIG_BLK_DEV_IDEDMA_SFF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363/**
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100364 * ide_dma_off_quietly - Generic DMA kill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * @drive: drive to control
366 *
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200367 * Turn off the current DMA on this IDE controller.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
369
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100370void ide_dma_off_quietly(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200372 drive->dev_flags &= ~IDE_DFLAG_USING_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 ide_toggle_bounce(drive, 0);
374
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200375 drive->hwif->dma_ops->dma_host_set(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100377EXPORT_SYMBOL(ide_dma_off_quietly);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379/**
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100380 * ide_dma_off - disable DMA on a device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * @drive: drive to disable DMA on
382 *
383 * Disable IDE DMA for a device on this IDE controller.
384 * Inform the user that DMA has been disabled.
385 */
386
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100387void ide_dma_off(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 printk(KERN_INFO "%s: DMA disabled\n", drive->name);
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100390 ide_dma_off_quietly(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100392EXPORT_SYMBOL(ide_dma_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/**
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100395 * ide_dma_on - Enable DMA on a device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * @drive: drive to enable DMA on
397 *
398 * Enable IDE DMA for a device on this IDE controller.
399 */
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100400
401void ide_dma_on(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200403 drive->dev_flags |= IDE_DFLAG_USING_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 ide_toggle_bounce(drive, 1);
405
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200406 drive->hwif->dma_ops->dma_host_set(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100409#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * ide_dma_setup - begin a DMA phase
412 * @drive: target device
413 *
414 * Build an IDE DMA PRD (IDE speak for scatter gather table)
415 * and then set up the DMA transfer registers for a device
416 * that follows generic IDE PCI DMA behaviour. Controllers can
417 * override this function if they need to
418 *
419 * Returns 0 on success. If a PIO fallback is required then 1
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200420 * is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 */
422
423int ide_dma_setup(ide_drive_t *drive)
424{
425 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200426 struct request *rq = hwif->hwgroup->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 unsigned int reading;
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200428 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 u8 dma_stat;
430
431 if (rq_data_dir(rq))
432 reading = 0;
433 else
434 reading = 1 << 3;
435
436 /* fall back to pio! */
437 if (!ide_build_dmatable(drive, rq)) {
438 ide_map_sg(drive, rq);
439 return 1;
440 }
441
442 /* PRD table */
Bartlomiej Zolnierkiewicz13572142008-07-15 21:21:49 +0200443 if (hwif->host_flags & IDE_HFLAG_MMIO)
Bartlomiej Zolnierkiewicz55224bc2008-04-28 23:44:42 +0200444 writel(hwif->dmatable_dma,
445 (void __iomem *)(hwif->dma_base + ATA_DMA_TABLE_OFS));
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100446 else
Bartlomiej Zolnierkiewicz55224bc2008-04-28 23:44:42 +0200447 outl(hwif->dmatable_dma, hwif->dma_base + ATA_DMA_TABLE_OFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* specify r/w */
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200450 if (mmio)
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200451 writeb(reading, (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200452 else
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200453 outb(reading, hwif->dma_base + ATA_DMA_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Bartlomiej Zolnierkiewiczb2f951a2008-07-23 19:55:50 +0200455 /* read DMA status for INTR & ERROR flags */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200456 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* clear INTR & ERROR flags */
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200459 if (mmio)
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200460 writeb(dma_stat | 6,
461 (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200462 else
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200463 outb(dma_stat | 6, hwif->dma_base + ATA_DMA_STATUS);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 drive->waiting_for_dma = 1;
466 return 0;
467}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468EXPORT_SYMBOL_GPL(ide_dma_setup);
469
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200470void ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 /* issue cmd to drive */
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200473 ide_execute_command(drive, command, &ide_dma_intr, 2 * WAIT_CMD,
474 dma_timer_expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200476EXPORT_SYMBOL_GPL(ide_dma_exec_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478void ide_dma_start(ide_drive_t *drive)
479{
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200480 ide_hwif_t *hwif = drive->hwif;
481 u8 dma_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* Note that this is done *after* the cmd has
484 * been issued to the drive, as per the BM-IDE spec.
485 * The Promise Ultra33 doesn't work correctly when
486 * we do this part before issuing the drive cmd.
487 */
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200488 if (hwif->host_flags & IDE_HFLAG_MMIO) {
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200489 dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200490 /* start DMA */
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200491 writeb(dma_cmd | 1,
492 (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200493 } else {
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200494 dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
495 outb(dma_cmd | 1, hwif->dma_base + ATA_DMA_CMD);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200496 }
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 wmb();
499}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500EXPORT_SYMBOL_GPL(ide_dma_start);
501
502/* returns 1 on error, 0 otherwise */
Bartlomiej Zolnierkiewicz653bcf52008-10-13 21:39:46 +0200503int ide_dma_end(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200505 ide_hwif_t *hwif = drive->hwif;
506 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 u8 dma_stat = 0, dma_cmd = 0;
508
509 drive->waiting_for_dma = 0;
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200510
511 if (mmio) {
512 /* get DMA command mode */
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200513 dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200514 /* stop DMA */
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200515 writeb(dma_cmd & ~1,
516 (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200517 } else {
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200518 dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
519 outb(dma_cmd & ~1, hwif->dma_base + ATA_DMA_CMD);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200520 }
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* get DMA status */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200523 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200524
525 if (mmio)
526 /* clear the INTR & ERROR bits */
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200527 writeb(dma_stat | 6,
528 (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200529 else
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200530 outb(dma_stat | 6, hwif->dma_base + ATA_DMA_STATUS);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* purge DMA mappings */
533 ide_destroy_dmatable(drive);
534 /* verify good DMA status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 wmb();
536 return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0;
537}
Bartlomiej Zolnierkiewicz653bcf52008-10-13 21:39:46 +0200538EXPORT_SYMBOL_GPL(ide_dma_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540/* returns 1 if dma irq issued, 0 otherwise */
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200541int ide_dma_test_irq(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200543 ide_hwif_t *hwif = drive->hwif;
544 u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* return 1 if INTR asserted */
547 if ((dma_stat & 4) == 4)
548 return 1;
Bartlomiej Zolnierkiewiczc67c2162008-10-13 21:39:38 +0200549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 0;
551}
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200552EXPORT_SYMBOL_GPL(ide_dma_test_irq);
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200553#else
554static inline int config_drive_for_dma(ide_drive_t *drive) { return 0; }
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100555#endif /* CONFIG_BLK_DEV_IDEDMA_SFF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200557int __ide_dma_bad_drive(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200559 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Jordan Crouse65e5f2e2005-12-15 02:16:18 +0100561 int blacklist = ide_in_drive_list(id, drive_blacklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (blacklist) {
563 printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n",
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200564 drive->name, (char *)&id[ATA_ID_PROD]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return blacklist;
566 }
567 return 0;
568}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569EXPORT_SYMBOL(__ide_dma_bad_drive);
570
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200571static const u8 xfer_mode_bases[] = {
572 XFER_UDMA_0,
573 XFER_MW_DMA_0,
574 XFER_SW_DMA_0,
575};
576
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200577static unsigned int ide_get_mode_mask(ide_drive_t *drive, u8 base, u8 req_mode)
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200578{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200579 u16 *id = drive->id;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200580 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200581 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200582 unsigned int mask = 0;
583
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200584 switch (base) {
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200585 case XFER_UDMA_0:
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200586 if ((id[ATA_ID_FIELD_VALID] & 4) == 0)
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200587 break;
588
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200589 if (port_ops && port_ops->udma_filter)
590 mask = port_ops->udma_filter(drive);
Sergei Shtylyov851dd332007-10-11 23:53:59 +0200591 else
592 mask = hwif->ultra_mask;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200593 mask &= id[ATA_ID_UDMA_MODES];
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200594
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200595 /*
596 * avoid false cable warning from eighty_ninty_three()
597 */
598 if (req_mode > XFER_UDMA_2) {
599 if ((mask & 0x78) && (eighty_ninty_three(drive) == 0))
600 mask &= 0x07;
601 }
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200602 break;
603 case XFER_MW_DMA_0:
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200604 if ((id[ATA_ID_FIELD_VALID] & 2) == 0)
Sergei Shtylyovb4e44362007-10-11 23:53:58 +0200605 break;
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200606 if (port_ops && port_ops->mdma_filter)
607 mask = port_ops->mdma_filter(drive);
Sergei Shtylyovb4e44362007-10-11 23:53:58 +0200608 else
609 mask = hwif->mwdma_mask;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200610 mask &= id[ATA_ID_MWDMA_MODES];
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200611 break;
612 case XFER_SW_DMA_0:
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200613 if (id[ATA_ID_FIELD_VALID] & 2) {
614 mask = id[ATA_ID_SWDMA_MODES] & hwif->swdma_mask;
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200615 } else if (id[ATA_ID_OLD_DMA_MODES] >> 8) {
616 u8 mode = id[ATA_ID_OLD_DMA_MODES] >> 8;
Bartlomiej Zolnierkiewicz15a4f942007-07-09 23:17:57 +0200617
618 /*
619 * if the mode is valid convert it to the mask
620 * (the maximum allowed mode is XFER_SW_DMA_2)
621 */
622 if (mode <= 2)
623 mask = ((2 << mode) - 1) & hwif->swdma_mask;
624 }
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200625 break;
626 default:
627 BUG();
628 break;
629 }
630
631 return mask;
632}
633
634/**
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200635 * ide_find_dma_mode - compute DMA speed
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200636 * @drive: IDE device
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200637 * @req_mode: requested mode
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200638 *
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200639 * Checks the drive/host capabilities and finds the speed to use for
640 * the DMA transfer. The speed is then limited by the requested mode.
641 *
642 * Returns 0 if the drive/host combination is incapable of DMA transfers
643 * or if the requested mode is not a DMA mode.
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200644 */
645
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200646u8 ide_find_dma_mode(ide_drive_t *drive, u8 req_mode)
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200647{
648 ide_hwif_t *hwif = drive->hwif;
649 unsigned int mask;
650 int x, i;
651 u8 mode = 0;
652
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200653 if (drive->media != ide_disk) {
654 if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
655 return 0;
656 }
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200657
658 for (i = 0; i < ARRAY_SIZE(xfer_mode_bases); i++) {
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200659 if (req_mode < xfer_mode_bases[i])
660 continue;
661 mask = ide_get_mode_mask(drive, xfer_mode_bases[i], req_mode);
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200662 x = fls(mask) - 1;
663 if (x >= 0) {
664 mode = xfer_mode_bases[i] + x;
665 break;
666 }
667 }
668
Bartlomiej Zolnierkiewicz75d7d962007-10-13 17:47:50 +0200669 if (hwif->chipset == ide_acorn && mode == 0) {
670 /*
671 * is this correct?
672 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200673 if (ide_dma_good_drive(drive) &&
674 drive->id[ATA_ID_EIDE_DMA_TIME] < 150)
Bartlomiej Zolnierkiewicz75d7d962007-10-13 17:47:50 +0200675 mode = XFER_MW_DMA_1;
676 }
677
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100678 mode = min(mode, req_mode);
679
680 printk(KERN_INFO "%s: %s mode selected\n", drive->name,
Bartlomiej Zolnierkiewiczd34887d2007-11-05 21:42:27 +0100681 mode ? ide_xfer_verbose(mode) : "no DMA");
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200682
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100683 return mode;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200684}
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200685EXPORT_SYMBOL_GPL(ide_find_dma_mode);
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200686
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200687static int ide_tune_dma(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200688{
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +0100689 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200690 u8 speed;
691
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200692 if (ata_id_has_dma(drive->id) == 0 ||
693 (drive->dev_flags & IDE_DFLAG_NODMA))
Bartlomiej Zolnierkiewicz122ab082007-05-16 00:51:46 +0200694 return 0;
695
696 /* consult the list of known "bad" drives */
697 if (__ide_dma_bad_drive(drive))
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200698 return 0;
699
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100700 if (ide_id_dma_bug(drive))
701 return 0;
702
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +0100703 if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200704 return config_drive_for_dma(drive);
705
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200706 speed = ide_max_dma_mode(drive);
707
Bartlomiej Zolnierkiewicz951784b2008-04-26 17:36:38 +0200708 if (!speed)
709 return 0;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200710
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200711 if (ide_set_dma_mode(drive, speed))
Bartlomiej Zolnierkiewicz4728d542007-05-16 00:51:46 +0200712 return 0;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200713
Bartlomiej Zolnierkiewicz4728d542007-05-16 00:51:46 +0200714 return 1;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200715}
716
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200717static int ide_dma_check(ide_drive_t *drive)
718{
719 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200720
Bartlomiej Zolnierkiewiczba4b2e62008-07-23 19:55:55 +0200721 if (ide_tune_dma(drive))
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200722 return 0;
723
724 /* TODO: always do PIO fallback */
725 if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
726 return -1;
727
728 ide_set_max_pio(drive);
729
Bartlomiej Zolnierkiewiczba4b2e62008-07-23 19:55:55 +0200730 return -1;
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200731}
732
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100733int ide_id_dma_bug(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200735 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200737 if (id[ATA_ID_FIELD_VALID] & 4) {
738 if ((id[ATA_ID_UDMA_MODES] >> 8) &&
739 (id[ATA_ID_MWDMA_MODES] >> 8))
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100740 goto err_out;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200741 } else if (id[ATA_ID_FIELD_VALID] & 2) {
742 if ((id[ATA_ID_MWDMA_MODES] >> 8) &&
743 (id[ATA_ID_SWDMA_MODES] >> 8))
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100744 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100746 return 0;
747err_out:
748 printk(KERN_ERR "%s: bad DMA info in identify block\n", drive->name);
749 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100752int ide_set_dma(ide_drive_t *drive)
753{
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100754 int rc;
755
Bartlomiej Zolnierkiewicz7b905992008-01-25 22:17:19 +0100756 /*
757 * Force DMAing for the beginning of the check.
758 * Some chipsets appear to do interesting
759 * things, if not checked and cleared.
760 * PARANOIA!!!
761 */
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100762 ide_dma_off_quietly(drive);
Bartlomiej Zolnierkiewicz7b905992008-01-25 22:17:19 +0100763
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200764 rc = ide_dma_check(drive);
Bartlomiej Zolnierkiewicz7b905992008-01-25 22:17:19 +0100765 if (rc)
766 return rc;
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100767
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100768 ide_dma_on(drive);
769
770 return 0;
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100771}
772
Bartlomiej Zolnierkiewicz578cfa02008-02-02 19:56:47 +0100773void ide_check_dma_crc(ide_drive_t *drive)
774{
775 u8 mode;
776
777 ide_dma_off_quietly(drive);
778 drive->crc_count = 0;
779 mode = drive->current_speed;
780 /*
781 * Don't try non Ultra-DMA modes without iCRC's. Force the
782 * device to PIO and make the user enable SWDMA/MWDMA modes.
783 */
784 if (mode > XFER_UDMA_0 && mode <= XFER_UDMA_7)
785 mode--;
786 else
787 mode = XFER_PIO_4;
788 ide_set_xfer_rate(drive, mode);
789 if (drive->current_speed >= XFER_SW_DMA_0)
790 ide_dma_on(drive);
791}
792
Bartlomiej Zolnierkiewiczde23ec92008-10-13 21:39:46 +0200793void ide_dma_lost_irq(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Bartlomiej Zolnierkiewiczde23ec92008-10-13 21:39:46 +0200795 printk(KERN_ERR "%s: DMA interrupt recovery\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
Bartlomiej Zolnierkiewiczde23ec92008-10-13 21:39:46 +0200797EXPORT_SYMBOL_GPL(ide_dma_lost_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Bartlomiej Zolnierkiewiczffa15a62008-10-13 21:39:46 +0200799void ide_dma_timeout(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200801 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200803 printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
804
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200805 if (hwif->dma_ops->dma_test_irq(drive))
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200806 return;
807
Bartlomiej Zolnierkiewiczffa15a62008-10-13 21:39:46 +0200808 ide_dump_status(drive, "DMA timeout", hwif->tp_ops->read_status(hwif));
809
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200810 hwif->dma_ops->dma_end(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
Bartlomiej Zolnierkiewiczffa15a62008-10-13 21:39:46 +0200812EXPORT_SYMBOL_GPL(ide_dma_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Bartlomiej Zolnierkiewicz0d1bad22008-04-26 22:25:19 +0200814void ide_release_dma_engine(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 if (hwif->dmatable_cpu) {
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200817 int prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100818
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200819 dma_free_coherent(hwif->dev, prd_size,
820 hwif->dmatable_cpu, hwif->dmatable_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 hwif->dmatable_cpu = NULL;
822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200824EXPORT_SYMBOL_GPL(ide_release_dma_engine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Bartlomiej Zolnierkiewiczb8e73fb2008-04-26 22:25:21 +0200826int ide_allocate_dma_engine(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200828 int prd_size;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100829
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200830 if (hwif->prd_max_nents == 0)
831 hwif->prd_max_nents = PRD_ENTRIES;
832 if (hwif->prd_ent_size == 0)
833 hwif->prd_ent_size = PRD_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200835 prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200837 hwif->dmatable_cpu = dma_alloc_coherent(hwif->dev, prd_size,
838 &hwif->dmatable_dma,
839 GFP_ATOMIC);
840 if (hwif->dmatable_cpu == NULL) {
841 printk(KERN_ERR "%s: unable to allocate PRD table\n",
Bartlomiej Zolnierkiewicz5e59c232008-04-26 22:25:20 +0200842 hwif->name);
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200843 return -ENOMEM;
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
Bartlomiej Zolnierkiewiczb8e73fb2008-04-26 22:25:21 +0200848EXPORT_SYMBOL_GPL(ide_allocate_dma_engine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200850#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
Bartlomiej Zolnierkiewicz81e8d5a2008-07-23 19:55:51 +0200851const struct ide_dma_ops sff_dma_ops = {
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200852 .dma_host_set = ide_dma_host_set,
853 .dma_setup = ide_dma_setup,
854 .dma_exec_cmd = ide_dma_exec_cmd,
855 .dma_start = ide_dma_start,
Bartlomiej Zolnierkiewicz653bcf52008-10-13 21:39:46 +0200856 .dma_end = ide_dma_end,
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200857 .dma_test_irq = ide_dma_test_irq,
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200858 .dma_timeout = ide_dma_timeout,
859 .dma_lost_irq = ide_dma_lost_irq,
860};
Bartlomiej Zolnierkiewicz81e8d5a2008-07-23 19:55:51 +0200861EXPORT_SYMBOL_GPL(sff_dma_ops);
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100862#endif /* CONFIG_BLK_DEV_IDEDMA_SFF */