blob: 4d212b867c351697435fe7eb23d893b7f79c5667 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static const struct drive_list_entry drive_whitelist [] = {
40
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020041 { "Micropolis 2112A" , NULL },
42 { "CONNER CTMA 4000" , NULL },
43 { "CONNER CTT8000-A" , NULL },
44 { "ST34342A" , NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 { NULL , NULL }
46};
47
48static const struct drive_list_entry drive_blacklist [] = {
49
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020050 { "WDC AC11000H" , NULL },
51 { "WDC AC22100H" , NULL },
52 { "WDC AC32500H" , NULL },
53 { "WDC AC33100H" , NULL },
54 { "WDC AC31600H" , NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 { "WDC AC32100H" , "24.09P07" },
56 { "WDC AC23200L" , "21.10N21" },
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020057 { "Compaq CRD-8241B" , NULL },
58 { "CRD-8400B" , NULL },
59 { "CRD-8480B", NULL },
60 { "CRD-8482B", NULL },
61 { "CRD-84" , NULL },
62 { "SanDisk SDP3B" , NULL },
63 { "SanDisk SDP3B-64" , NULL },
64 { "SANYO CD-ROM CRD" , NULL },
65 { "HITACHI CDR-8" , NULL },
66 { "HITACHI CDR-8335" , NULL },
67 { "HITACHI CDR-8435" , NULL },
68 { "Toshiba CD-ROM XM-6202B" , NULL },
69 { "TOSHIBA CD-ROM XM-1702BC", NULL },
70 { "CD-532E-A" , NULL },
71 { "E-IDE CD-ROM CR-840", NULL },
72 { "CD-ROM Drive/F5A", NULL },
73 { "WPI CDD-820", NULL },
74 { "SAMSUNG CD-ROM SC-148C", NULL },
75 { "SAMSUNG CD-ROM SC", NULL },
76 { "ATAPI CD-ROM DRIVE 40X MAXIMUM", NULL },
77 { "_NEC DV5800A", NULL },
Junio C Hamano5a6248c2007-05-24 02:42:38 +020078 { "SAMSUNG CD-ROM SN-124", "N001" },
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020079 { "Seagate STT20000A", NULL },
Bartlomiej Zolnierkiewiczb0bc65b2007-11-27 21:35:56 +010080 { "CD-ROM CDR_U200", "1.09" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 { NULL , NULL }
82
83};
84
85/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * ide_dma_intr - IDE DMA interrupt handler
87 * @drive: the drive the interrupt is for
88 *
89 * Handle an interrupt completing a read/write DMA transfer on an
90 * IDE device
91 */
92
93ide_startstop_t ide_dma_intr (ide_drive_t *drive)
94{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +020095 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 u8 stat = 0, dma_stat = 0;
97
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +020098 dma_stat = hwif->dma_ops->dma_end(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020099 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100100
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200101 if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat | ATA_DRQ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (!dma_stat) {
103 struct request *rq = HWGROUP(drive)->rq;
104
Tejun Heo4d7a9842008-01-26 20:13:11 +0100105 task_end_request(drive, rq, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return ide_stopped;
107 }
108 printk(KERN_ERR "%s: dma_intr: bad DMA status (dma_stat=%x)\n",
109 drive->name, dma_stat);
110 }
111 return ide_error(drive, "dma_intr", stat);
112}
113
114EXPORT_SYMBOL_GPL(ide_dma_intr);
115
Bartlomiej Zolnierkiewicz75d7d962007-10-13 17:47:50 +0200116static int ide_dma_good_drive(ide_drive_t *drive)
117{
118 return ide_in_drive_list(drive->id, drive_whitelist);
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/**
122 * ide_build_sglist - map IDE scatter gather for DMA I/O
123 * @drive: the drive to build the DMA table for
124 * @rq: the request holding the sg list
125 *
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100126 * Perform the DMA mapping magic necessary to access the source or
127 * target buffers of a request via DMA. The lower layers of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * kernel provide the necessary cache management so that we can
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100129 * operate in a portable fashion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131
132int ide_build_sglist(ide_drive_t *drive, struct request *rq)
133{
134 ide_hwif_t *hwif = HWIF(drive);
135 struct scatterlist *sg = hwif->sg_table;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 ide_map_sg(drive, rq);
138
139 if (rq_data_dir(rq) == READ)
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100140 hwif->sg_dma_direction = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 else
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100142 hwif->sg_dma_direction = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100144 return dma_map_sg(hwif->dev, sg, hwif->sg_nents,
145 hwif->sg_dma_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148EXPORT_SYMBOL_GPL(ide_build_sglist);
149
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100150#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/**
152 * ide_build_dmatable - build IDE DMA table
153 *
154 * ide_build_dmatable() prepares a dma request. We map the command
155 * to get the pci bus addresses of the buffers and then build up
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200156 * the PRD table that the IDE layer wants to be fed.
157 *
158 * Most chipsets correctly interpret a length of 0x0000 as 64KB,
159 * but at least one (e.g. CS5530) misinterprets it as zero (!).
160 * So we break the 64KB entry into two 32KB entries instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 *
162 * Returns the number of built PRD entries if all went okay,
163 * returns 0 otherwise.
164 *
165 * May also be invoked from trm290.c
166 */
167
168int ide_build_dmatable (ide_drive_t *drive, struct request *rq)
169{
170 ide_hwif_t *hwif = HWIF(drive);
Harvey Harrison7fa897b2008-07-24 22:53:34 +0200171 __le32 *table = (__le32 *)hwif->dmatable_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned int is_trm290 = (hwif->chipset == ide_trm290) ? 1 : 0;
173 unsigned int count = 0;
174 int i;
175 struct scatterlist *sg;
176
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200177 hwif->sg_nents = ide_build_sglist(drive, rq);
178 if (hwif->sg_nents == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return 0;
180
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200181 for_each_sg(hwif->sg_table, sg, hwif->sg_nents, i) {
182 u32 cur_addr, cur_len, xcount, bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 cur_addr = sg_dma_address(sg);
185 cur_len = sg_dma_len(sg);
186
187 /*
188 * Fill in the dma table, without crossing any 64kB boundaries.
189 * Most hardware requires 16-bit alignment of all blocks,
190 * but the trm290 requires 32-bit alignment.
191 */
192
193 while (cur_len) {
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200194 if (count++ >= PRD_ENTRIES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto use_pio_instead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200197 bcount = 0x10000 - (cur_addr & 0xffff);
198 if (bcount > cur_len)
199 bcount = cur_len;
200 *table++ = cpu_to_le32(cur_addr);
201 xcount = bcount & 0xffff;
202 if (is_trm290)
203 xcount = ((xcount >> 2) - 1) << 16;
204 if (xcount == 0x0000) {
205 if (count++ >= PRD_ENTRIES)
206 goto use_pio_instead;
207 *table++ = cpu_to_le32(0x8000);
208 *table++ = cpu_to_le32(cur_addr + 0x8000);
209 xcount = 0x8000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200211 *table++ = cpu_to_le32(xcount);
212 cur_addr += bcount;
213 cur_len -= bcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
217 if (count) {
218 if (!is_trm290)
219 *--table |= cpu_to_le32(0x80000000);
220 return count;
221 }
Bartlomiej Zolnierkiewiczf6fb7862008-02-01 23:09:31 +0100222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223use_pio_instead:
Bartlomiej Zolnierkiewicz14c123f2008-10-13 21:39:47 +0200224 printk(KERN_ERR "%s: %s\n", drive->name,
225 count ? "DMA table too small" : "empty DMA table?");
226
Bartlomiej Zolnierkiewiczf6fb7862008-02-01 23:09:31 +0100227 ide_destroy_dmatable(drive);
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0; /* revert to PIO for this request */
230}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231EXPORT_SYMBOL_GPL(ide_build_dmatable);
Bartlomiej Zolnierkiewicz062f9f02008-02-01 23:09:32 +0100232#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234/**
235 * ide_destroy_dmatable - clean up DMA mapping
236 * @drive: The drive to unmap
237 *
238 * Teardown mappings after DMA has completed. This must be called
239 * after the completion of each use of ide_build_dmatable and before
240 * the next use of ide_build_dmatable. Failure to do so will cause
241 * an oops as only one mapping can be live for each target at a given
242 * time.
243 */
244
245void ide_destroy_dmatable (ide_drive_t *drive)
246{
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100247 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100249 dma_unmap_sg(hwif->dev, hwif->sg_table, hwif->sg_nents,
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100250 hwif->sg_dma_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253EXPORT_SYMBOL_GPL(ide_destroy_dmatable);
254
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100255#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/**
257 * config_drive_for_dma - attempt to activate IDE DMA
258 * @drive: the drive to place in DMA mode
259 *
260 * If the drive supports at least mode 2 DMA or UDMA of any kind
261 * then attempt to place it into DMA mode. Drives that are known to
262 * support DMA but predate the DMA properties or that are known
263 * to have DMA handling bugs are also set up appropriately based
264 * on the good/bad drive lists.
265 */
266
267static int config_drive_for_dma (ide_drive_t *drive)
268{
Bartlomiej Zolnierkiewicz1116fae2007-08-20 22:42:55 +0200269 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200270 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200272 if (drive->media != ide_disk) {
273 if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
Bartlomiej Zolnierkiewiczbcbf6ee2007-11-05 21:42:30 +0100274 return 0;
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200275 }
Bartlomiej Zolnierkiewicz1116fae2007-08-20 22:42:55 +0200276
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200277 /*
278 * Enable DMA on any drive that has
279 * UltraDMA (mode 0/1/2/3/4/5/6) enabled
280 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200281 if ((id[ATA_ID_FIELD_VALID] & 4) &&
282 ((id[ATA_ID_UDMA_MODES] >> 8) & 0x7f))
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200283 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200285 /*
286 * Enable DMA on any drive that has mode2 DMA
287 * (multi or single) enabled
288 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200289 if (id[ATA_ID_FIELD_VALID] & 2) /* regular DMA */
290 if ((id[ATA_ID_MWDMA_MODES] & 0x404) == 0x404 ||
291 (id[ATA_ID_SWDMA_MODES] & 0x404) == 0x404)
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200292 return 1;
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100293
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200294 /* Consult the list of known "good" drives */
295 if (ide_dma_good_drive(drive))
296 return 1;
297
298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301/**
302 * dma_timer_expiry - handle a DMA timeout
303 * @drive: Drive that timed out
304 *
305 * An IDE DMA transfer timed out. In the event of an error we ask
306 * the driver to resolve the problem, if a DMA transfer is still
307 * in progress we continue to wait (arguably we need to add a
308 * secondary 'I don't care what the drive thinks' timeout here)
309 * Finally if we have an interrupt we let it complete the I/O.
310 * But only one time - we clear expiry and if it's still not
311 * completed after WAIT_CMD, we error and retry in PIO.
312 * This can occur if an interrupt is lost or due to hang or bugs.
313 */
314
315static int dma_timer_expiry (ide_drive_t *drive)
316{
317 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200318 u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 printk(KERN_WARNING "%s: dma_timer_expiry: dma status == 0x%02x\n",
321 drive->name, dma_stat);
322
323 if ((dma_stat & 0x18) == 0x18) /* BUSY Stupid Early Timer !! */
324 return WAIT_CMD;
325
326 HWGROUP(drive)->expiry = NULL; /* one free ride for now */
327
328 /* 1 dmaing, 2 error, 4 intr */
329 if (dma_stat & 2) /* ERROR */
330 return -1;
331
332 if (dma_stat & 1) /* DMAing */
333 return WAIT_CMD;
334
335 if (dma_stat & 4) /* Got an Interrupt */
336 return WAIT_CMD;
337
338 return 0; /* Status is unknown -- reset the bus */
339}
340
341/**
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100342 * ide_dma_host_set - Enable/disable DMA on a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * @drive: drive to control
344 *
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100345 * Enable/disable DMA on an IDE controller following generic
346 * bus-mastering IDE controller behaviour.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
348
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100349void ide_dma_host_set(ide_drive_t *drive, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200352 u8 unit = drive->dn & 1;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200353 u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100355 if (on)
356 dma_stat |= (1 << (5 + unit));
357 else
358 dma_stat &= ~(1 << (5 + unit));
359
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200360 if (hwif->host_flags & IDE_HFLAG_MMIO)
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200361 writeb(dma_stat,
362 (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200363 else
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200364 outb(dma_stat, hwif->dma_base + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100367EXPORT_SYMBOL_GPL(ide_dma_host_set);
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100368#endif /* CONFIG_BLK_DEV_IDEDMA_SFF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370/**
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100371 * ide_dma_off_quietly - Generic DMA kill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * @drive: drive to control
373 *
374 * Turn off the current DMA on this IDE controller.
375 */
376
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100377void ide_dma_off_quietly(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200379 drive->dev_flags &= ~IDE_DFLAG_USING_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 ide_toggle_bounce(drive, 0);
381
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200382 drive->hwif->dma_ops->dma_host_set(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100385EXPORT_SYMBOL(ide_dma_off_quietly);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387/**
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100388 * ide_dma_off - disable DMA on a device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * @drive: drive to disable DMA on
390 *
391 * Disable IDE DMA for a device on this IDE controller.
392 * Inform the user that DMA has been disabled.
393 */
394
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100395void ide_dma_off(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 printk(KERN_INFO "%s: DMA disabled\n", drive->name);
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100398 ide_dma_off_quietly(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100401EXPORT_SYMBOL(ide_dma_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403/**
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100404 * ide_dma_on - Enable DMA on a device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 * @drive: drive to enable DMA on
406 *
407 * Enable IDE DMA for a device on this IDE controller.
408 */
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100409
410void ide_dma_on(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200412 drive->dev_flags |= IDE_DFLAG_USING_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ide_toggle_bounce(drive, 1);
414
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200415 drive->hwif->dma_ops->dma_host_set(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100418#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * ide_dma_setup - begin a DMA phase
421 * @drive: target device
422 *
423 * Build an IDE DMA PRD (IDE speak for scatter gather table)
424 * and then set up the DMA transfer registers for a device
425 * that follows generic IDE PCI DMA behaviour. Controllers can
426 * override this function if they need to
427 *
428 * Returns 0 on success. If a PIO fallback is required then 1
429 * is returned.
430 */
431
432int ide_dma_setup(ide_drive_t *drive)
433{
434 ide_hwif_t *hwif = drive->hwif;
435 struct request *rq = HWGROUP(drive)->rq;
436 unsigned int reading;
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200437 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 u8 dma_stat;
439
440 if (rq_data_dir(rq))
441 reading = 0;
442 else
443 reading = 1 << 3;
444
445 /* fall back to pio! */
446 if (!ide_build_dmatable(drive, rq)) {
447 ide_map_sg(drive, rq);
448 return 1;
449 }
450
451 /* PRD table */
Bartlomiej Zolnierkiewicz13572142008-07-15 21:21:49 +0200452 if (hwif->host_flags & IDE_HFLAG_MMIO)
Bartlomiej Zolnierkiewicz55224bc2008-04-28 23:44:42 +0200453 writel(hwif->dmatable_dma,
454 (void __iomem *)(hwif->dma_base + ATA_DMA_TABLE_OFS));
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100455 else
Bartlomiej Zolnierkiewicz55224bc2008-04-28 23:44:42 +0200456 outl(hwif->dmatable_dma, hwif->dma_base + ATA_DMA_TABLE_OFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* specify r/w */
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200459 if (mmio)
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200460 writeb(reading, (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200461 else
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200462 outb(reading, hwif->dma_base + ATA_DMA_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Bartlomiej Zolnierkiewiczb2f951a2008-07-23 19:55:50 +0200464 /* read DMA status for INTR & ERROR flags */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200465 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /* clear INTR & ERROR flags */
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200468 if (mmio)
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200469 writeb(dma_stat | 6,
470 (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200471 else
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200472 outb(dma_stat | 6, hwif->dma_base + ATA_DMA_STATUS);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 drive->waiting_for_dma = 1;
475 return 0;
476}
477
478EXPORT_SYMBOL_GPL(ide_dma_setup);
479
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200480void ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 /* issue cmd to drive */
483 ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, dma_timer_expiry);
484}
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200485EXPORT_SYMBOL_GPL(ide_dma_exec_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487void ide_dma_start(ide_drive_t *drive)
488{
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200489 ide_hwif_t *hwif = drive->hwif;
490 u8 dma_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* Note that this is done *after* the cmd has
493 * been issued to the drive, as per the BM-IDE spec.
494 * The Promise Ultra33 doesn't work correctly when
495 * we do this part before issuing the drive cmd.
496 */
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200497 if (hwif->host_flags & IDE_HFLAG_MMIO) {
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200498 dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200499 /* start DMA */
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200500 writeb(dma_cmd | 1,
501 (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200502 } else {
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200503 dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
504 outb(dma_cmd | 1, hwif->dma_base + ATA_DMA_CMD);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200505 }
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 wmb();
508}
509
510EXPORT_SYMBOL_GPL(ide_dma_start);
511
512/* returns 1 on error, 0 otherwise */
Bartlomiej Zolnierkiewicz653bcf52008-10-13 21:39:46 +0200513int ide_dma_end(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200515 ide_hwif_t *hwif = drive->hwif;
516 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 u8 dma_stat = 0, dma_cmd = 0;
518
519 drive->waiting_for_dma = 0;
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200520
521 if (mmio) {
522 /* get DMA command mode */
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200523 dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200524 /* stop DMA */
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200525 writeb(dma_cmd & ~1,
526 (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200527 } else {
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200528 dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
529 outb(dma_cmd & ~1, hwif->dma_base + ATA_DMA_CMD);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200530 }
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* get DMA status */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200533 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200534
535 if (mmio)
536 /* clear the INTR & ERROR bits */
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200537 writeb(dma_stat | 6,
538 (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200539 else
Bartlomiej Zolnierkiewiczcab7f8e2008-07-23 19:55:51 +0200540 outb(dma_stat | 6, hwif->dma_base + ATA_DMA_STATUS);
Bartlomiej Zolnierkiewiczab86f912008-07-23 19:55:50 +0200541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* purge DMA mappings */
543 ide_destroy_dmatable(drive);
544 /* verify good DMA status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 wmb();
546 return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0;
547}
Bartlomiej Zolnierkiewicz653bcf52008-10-13 21:39:46 +0200548EXPORT_SYMBOL_GPL(ide_dma_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550/* returns 1 if dma irq issued, 0 otherwise */
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200551int ide_dma_test_irq(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200554 u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /* return 1 if INTR asserted */
557 if ((dma_stat & 4) == 4)
558 return 1;
Bartlomiej Zolnierkiewiczc67c2162008-10-13 21:39:38 +0200559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return 0;
561}
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200562EXPORT_SYMBOL_GPL(ide_dma_test_irq);
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200563#else
564static inline int config_drive_for_dma(ide_drive_t *drive) { return 0; }
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100565#endif /* CONFIG_BLK_DEV_IDEDMA_SFF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567int __ide_dma_bad_drive (ide_drive_t *drive)
568{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200569 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Jordan Crouse65e5f2e2005-12-15 02:16:18 +0100571 int blacklist = ide_in_drive_list(id, drive_blacklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (blacklist) {
573 printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n",
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200574 drive->name, (char *)&id[ATA_ID_PROD]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return blacklist;
576 }
577 return 0;
578}
579
580EXPORT_SYMBOL(__ide_dma_bad_drive);
581
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200582static const u8 xfer_mode_bases[] = {
583 XFER_UDMA_0,
584 XFER_MW_DMA_0,
585 XFER_SW_DMA_0,
586};
587
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200588static unsigned int ide_get_mode_mask(ide_drive_t *drive, u8 base, u8 req_mode)
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200589{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200590 u16 *id = drive->id;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200591 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200592 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200593 unsigned int mask = 0;
594
595 switch(base) {
596 case XFER_UDMA_0:
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200597 if ((id[ATA_ID_FIELD_VALID] & 4) == 0)
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200598 break;
599
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200600 if (port_ops && port_ops->udma_filter)
601 mask = port_ops->udma_filter(drive);
Sergei Shtylyov851dd332007-10-11 23:53:59 +0200602 else
603 mask = hwif->ultra_mask;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200604 mask &= id[ATA_ID_UDMA_MODES];
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200605
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200606 /*
607 * avoid false cable warning from eighty_ninty_three()
608 */
609 if (req_mode > XFER_UDMA_2) {
610 if ((mask & 0x78) && (eighty_ninty_three(drive) == 0))
611 mask &= 0x07;
612 }
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200613 break;
614 case XFER_MW_DMA_0:
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200615 if ((id[ATA_ID_FIELD_VALID] & 2) == 0)
Sergei Shtylyovb4e44362007-10-11 23:53:58 +0200616 break;
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200617 if (port_ops && port_ops->mdma_filter)
618 mask = port_ops->mdma_filter(drive);
Sergei Shtylyovb4e44362007-10-11 23:53:58 +0200619 else
620 mask = hwif->mwdma_mask;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200621 mask &= id[ATA_ID_MWDMA_MODES];
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200622 break;
623 case XFER_SW_DMA_0:
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200624 if (id[ATA_ID_FIELD_VALID] & 2) {
625 mask = id[ATA_ID_SWDMA_MODES] & hwif->swdma_mask;
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200626 } else if (id[ATA_ID_OLD_DMA_MODES] >> 8) {
627 u8 mode = id[ATA_ID_OLD_DMA_MODES] >> 8;
Bartlomiej Zolnierkiewicz15a4f942007-07-09 23:17:57 +0200628
629 /*
630 * if the mode is valid convert it to the mask
631 * (the maximum allowed mode is XFER_SW_DMA_2)
632 */
633 if (mode <= 2)
634 mask = ((2 << mode) - 1) & hwif->swdma_mask;
635 }
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200636 break;
637 default:
638 BUG();
639 break;
640 }
641
642 return mask;
643}
644
645/**
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200646 * ide_find_dma_mode - compute DMA speed
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200647 * @drive: IDE device
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200648 * @req_mode: requested mode
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200649 *
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200650 * Checks the drive/host capabilities and finds the speed to use for
651 * the DMA transfer. The speed is then limited by the requested mode.
652 *
653 * Returns 0 if the drive/host combination is incapable of DMA transfers
654 * or if the requested mode is not a DMA mode.
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200655 */
656
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200657u8 ide_find_dma_mode(ide_drive_t *drive, u8 req_mode)
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200658{
659 ide_hwif_t *hwif = drive->hwif;
660 unsigned int mask;
661 int x, i;
662 u8 mode = 0;
663
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200664 if (drive->media != ide_disk) {
665 if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
666 return 0;
667 }
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200668
669 for (i = 0; i < ARRAY_SIZE(xfer_mode_bases); i++) {
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200670 if (req_mode < xfer_mode_bases[i])
671 continue;
672 mask = ide_get_mode_mask(drive, xfer_mode_bases[i], req_mode);
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200673 x = fls(mask) - 1;
674 if (x >= 0) {
675 mode = xfer_mode_bases[i] + x;
676 break;
677 }
678 }
679
Bartlomiej Zolnierkiewicz75d7d962007-10-13 17:47:50 +0200680 if (hwif->chipset == ide_acorn && mode == 0) {
681 /*
682 * is this correct?
683 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200684 if (ide_dma_good_drive(drive) &&
685 drive->id[ATA_ID_EIDE_DMA_TIME] < 150)
Bartlomiej Zolnierkiewicz75d7d962007-10-13 17:47:50 +0200686 mode = XFER_MW_DMA_1;
687 }
688
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100689 mode = min(mode, req_mode);
690
691 printk(KERN_INFO "%s: %s mode selected\n", drive->name,
Bartlomiej Zolnierkiewiczd34887d2007-11-05 21:42:27 +0100692 mode ? ide_xfer_verbose(mode) : "no DMA");
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200693
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100694 return mode;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200695}
696
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200697EXPORT_SYMBOL_GPL(ide_find_dma_mode);
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200698
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200699static int ide_tune_dma(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200700{
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +0100701 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200702 u8 speed;
703
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200704 if (ata_id_has_dma(drive->id) == 0 ||
705 (drive->dev_flags & IDE_DFLAG_NODMA))
Bartlomiej Zolnierkiewicz122ab082007-05-16 00:51:46 +0200706 return 0;
707
708 /* consult the list of known "bad" drives */
709 if (__ide_dma_bad_drive(drive))
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200710 return 0;
711
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100712 if (ide_id_dma_bug(drive))
713 return 0;
714
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +0100715 if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200716 return config_drive_for_dma(drive);
717
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200718 speed = ide_max_dma_mode(drive);
719
Bartlomiej Zolnierkiewicz951784b2008-04-26 17:36:38 +0200720 if (!speed)
721 return 0;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200722
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200723 if (ide_set_dma_mode(drive, speed))
Bartlomiej Zolnierkiewicz4728d542007-05-16 00:51:46 +0200724 return 0;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200725
Bartlomiej Zolnierkiewicz4728d542007-05-16 00:51:46 +0200726 return 1;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200727}
728
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200729static int ide_dma_check(ide_drive_t *drive)
730{
731 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200732
Bartlomiej Zolnierkiewiczba4b2e62008-07-23 19:55:55 +0200733 if (ide_tune_dma(drive))
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200734 return 0;
735
736 /* TODO: always do PIO fallback */
737 if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
738 return -1;
739
740 ide_set_max_pio(drive);
741
Bartlomiej Zolnierkiewiczba4b2e62008-07-23 19:55:55 +0200742 return -1;
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200743}
744
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100745int ide_id_dma_bug(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200747 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200749 if (id[ATA_ID_FIELD_VALID] & 4) {
750 if ((id[ATA_ID_UDMA_MODES] >> 8) &&
751 (id[ATA_ID_MWDMA_MODES] >> 8))
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100752 goto err_out;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200753 } else if (id[ATA_ID_FIELD_VALID] & 2) {
754 if ((id[ATA_ID_MWDMA_MODES] >> 8) &&
755 (id[ATA_ID_SWDMA_MODES] >> 8))
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100756 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100758 return 0;
759err_out:
760 printk(KERN_ERR "%s: bad DMA info in identify block\n", drive->name);
761 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100764int ide_set_dma(ide_drive_t *drive)
765{
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100766 int rc;
767
Bartlomiej Zolnierkiewicz7b905992008-01-25 22:17:19 +0100768 /*
769 * Force DMAing for the beginning of the check.
770 * Some chipsets appear to do interesting
771 * things, if not checked and cleared.
772 * PARANOIA!!!
773 */
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100774 ide_dma_off_quietly(drive);
Bartlomiej Zolnierkiewicz7b905992008-01-25 22:17:19 +0100775
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200776 rc = ide_dma_check(drive);
Bartlomiej Zolnierkiewicz7b905992008-01-25 22:17:19 +0100777 if (rc)
778 return rc;
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100779
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100780 ide_dma_on(drive);
781
782 return 0;
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100783}
784
Bartlomiej Zolnierkiewicz578cfa02008-02-02 19:56:47 +0100785void ide_check_dma_crc(ide_drive_t *drive)
786{
787 u8 mode;
788
789 ide_dma_off_quietly(drive);
790 drive->crc_count = 0;
791 mode = drive->current_speed;
792 /*
793 * Don't try non Ultra-DMA modes without iCRC's. Force the
794 * device to PIO and make the user enable SWDMA/MWDMA modes.
795 */
796 if (mode > XFER_UDMA_0 && mode <= XFER_UDMA_7)
797 mode--;
798 else
799 mode = XFER_PIO_4;
800 ide_set_xfer_rate(drive, mode);
801 if (drive->current_speed >= XFER_SW_DMA_0)
802 ide_dma_on(drive);
803}
804
Bartlomiej Zolnierkiewiczde23ec92008-10-13 21:39:46 +0200805void ide_dma_lost_irq(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Bartlomiej Zolnierkiewiczde23ec92008-10-13 21:39:46 +0200807 printk(KERN_ERR "%s: DMA interrupt recovery\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
Bartlomiej Zolnierkiewiczde23ec92008-10-13 21:39:46 +0200809EXPORT_SYMBOL_GPL(ide_dma_lost_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Bartlomiej Zolnierkiewiczffa15a62008-10-13 21:39:46 +0200811void ide_dma_timeout(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200813 ide_hwif_t *hwif = HWIF(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200815 printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
816
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200817 if (hwif->dma_ops->dma_test_irq(drive))
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200818 return;
819
Bartlomiej Zolnierkiewiczffa15a62008-10-13 21:39:46 +0200820 ide_dump_status(drive, "DMA timeout", hwif->tp_ops->read_status(hwif));
821
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200822 hwif->dma_ops->dma_end(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
Bartlomiej Zolnierkiewiczffa15a62008-10-13 21:39:46 +0200824EXPORT_SYMBOL_GPL(ide_dma_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Bartlomiej Zolnierkiewicz0d1bad22008-04-26 22:25:19 +0200826void ide_release_dma_engine(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 if (hwif->dmatable_cpu) {
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200829 int prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100830
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200831 dma_free_coherent(hwif->dev, prd_size,
832 hwif->dmatable_cpu, hwif->dmatable_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 hwif->dmatable_cpu = NULL;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200836EXPORT_SYMBOL_GPL(ide_release_dma_engine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Bartlomiej Zolnierkiewiczb8e73fb2008-04-26 22:25:21 +0200838int ide_allocate_dma_engine(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200840 int prd_size;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100841
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200842 if (hwif->prd_max_nents == 0)
843 hwif->prd_max_nents = PRD_ENTRIES;
844 if (hwif->prd_ent_size == 0)
845 hwif->prd_ent_size = PRD_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200847 prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200849 hwif->dmatable_cpu = dma_alloc_coherent(hwif->dev, prd_size,
850 &hwif->dmatable_dma,
851 GFP_ATOMIC);
852 if (hwif->dmatable_cpu == NULL) {
853 printk(KERN_ERR "%s: unable to allocate PRD table\n",
Bartlomiej Zolnierkiewicz5e59c232008-04-26 22:25:20 +0200854 hwif->name);
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200855 return -ENOMEM;
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
Bartlomiej Zolnierkiewiczb8e73fb2008-04-26 22:25:21 +0200860EXPORT_SYMBOL_GPL(ide_allocate_dma_engine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200862#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
Bartlomiej Zolnierkiewicz81e8d5a2008-07-23 19:55:51 +0200863const struct ide_dma_ops sff_dma_ops = {
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200864 .dma_host_set = ide_dma_host_set,
865 .dma_setup = ide_dma_setup,
866 .dma_exec_cmd = ide_dma_exec_cmd,
867 .dma_start = ide_dma_start,
Bartlomiej Zolnierkiewicz653bcf52008-10-13 21:39:46 +0200868 .dma_end = ide_dma_end,
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200869 .dma_test_irq = ide_dma_test_irq,
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200870 .dma_timeout = ide_dma_timeout,
871 .dma_lost_irq = ide_dma_lost_irq,
872};
Bartlomiej Zolnierkiewicz81e8d5a2008-07-23 19:55:51 +0200873EXPORT_SYMBOL_GPL(sff_dma_ops);
Sergei Shtylyov8e882ba2008-02-11 00:32:14 +0100874#endif /* CONFIG_BLK_DEV_IDEDMA_SFF */