blob: fffd11717b2d169e3557c501db7c51780db5a4b1 [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
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +020037static const struct drive_list_entry drive_whitelist[] = {
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020038 { "Micropolis 2112A" , NULL },
39 { "CONNER CTMA 4000" , NULL },
40 { "CONNER CTT8000-A" , NULL },
41 { "ST34342A" , NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 { NULL , NULL }
43};
44
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +020045static const struct drive_list_entry drive_blacklist[] = {
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020046 { "WDC AC11000H" , NULL },
47 { "WDC AC22100H" , NULL },
48 { "WDC AC32500H" , NULL },
49 { "WDC AC33100H" , NULL },
50 { "WDC AC31600H" , NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 { "WDC AC32100H" , "24.09P07" },
52 { "WDC AC23200L" , "21.10N21" },
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020053 { "Compaq CRD-8241B" , NULL },
54 { "CRD-8400B" , NULL },
55 { "CRD-8480B", NULL },
56 { "CRD-8482B", NULL },
57 { "CRD-84" , NULL },
58 { "SanDisk SDP3B" , NULL },
59 { "SanDisk SDP3B-64" , NULL },
60 { "SANYO CD-ROM CRD" , NULL },
61 { "HITACHI CDR-8" , NULL },
62 { "HITACHI CDR-8335" , NULL },
63 { "HITACHI CDR-8435" , NULL },
64 { "Toshiba CD-ROM XM-6202B" , NULL },
65 { "TOSHIBA CD-ROM XM-1702BC", NULL },
66 { "CD-532E-A" , NULL },
67 { "E-IDE CD-ROM CR-840", NULL },
68 { "CD-ROM Drive/F5A", NULL },
69 { "WPI CDD-820", NULL },
70 { "SAMSUNG CD-ROM SC-148C", NULL },
71 { "SAMSUNG CD-ROM SC", NULL },
72 { "ATAPI CD-ROM DRIVE 40X MAXIMUM", NULL },
73 { "_NEC DV5800A", NULL },
Junio C Hamano5a6248c2007-05-24 02:42:38 +020074 { "SAMSUNG CD-ROM SN-124", "N001" },
Junio C Hamanoc2d3ce82007-07-09 23:17:56 +020075 { "Seagate STT20000A", NULL },
Bartlomiej Zolnierkiewiczb0bc65b2007-11-27 21:35:56 +010076 { "CD-ROM CDR_U200", "1.09" },
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 { NULL , NULL }
78
79};
80
81/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * ide_dma_intr - IDE DMA interrupt handler
83 * @drive: the drive the interrupt is for
84 *
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +020085 * Handle an interrupt completing a read/write DMA transfer on an
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * IDE device
87 */
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +020088
89ide_startstop_t ide_dma_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +020091 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 u8 stat = 0, dma_stat = 0;
93
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +020094 dma_stat = hwif->dma_ops->dma_end(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020095 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +010096
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +020097 if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat | ATA_DRQ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (!dma_stat) {
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +020099 struct request *rq = hwif->hwgroup->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Tejun Heo4d7a9842008-01-26 20:13:11 +0100101 task_end_request(drive, rq, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return ide_stopped;
103 }
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200104 printk(KERN_ERR "%s: %s: bad DMA status (0x%02x)\n",
105 drive->name, __func__, dma_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107 return ide_error(drive, "dma_intr", stat);
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109EXPORT_SYMBOL_GPL(ide_dma_intr);
110
Bartlomiej Zolnierkiewicz2dbe7e92008-10-13 21:39:47 +0200111int ide_dma_good_drive(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz75d7d962007-10-13 17:47:50 +0200112{
113 return ide_in_drive_list(drive->id, drive_whitelist);
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/**
117 * ide_build_sglist - map IDE scatter gather for DMA I/O
118 * @drive: the drive to build the DMA table for
119 * @rq: the request holding the sg list
120 *
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100121 * Perform the DMA mapping magic necessary to access the source or
122 * target buffers of a request via DMA. The lower layers of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * kernel provide the necessary cache management so that we can
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100124 * operate in a portable fashion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
126
127int ide_build_sglist(ide_drive_t *drive, struct request *rq)
128{
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200129 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct scatterlist *sg = hwif->sg_table;
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 ide_map_sg(drive, rq);
133
134 if (rq_data_dir(rq) == READ)
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100135 hwif->sg_dma_direction = DMA_FROM_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 else
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100137 hwif->sg_dma_direction = DMA_TO_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100139 return dma_map_sg(hwif->dev, sg, hwif->sg_nents,
140 hwif->sg_dma_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142EXPORT_SYMBOL_GPL(ide_build_sglist);
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/**
145 * ide_destroy_dmatable - clean up DMA mapping
146 * @drive: The drive to unmap
147 *
148 * Teardown mappings after DMA has completed. This must be called
149 * after the completion of each use of ide_build_dmatable and before
150 * the next use of ide_build_dmatable. Failure to do so will cause
151 * an oops as only one mapping can be live for each target at a given
152 * time.
153 */
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200154
155void ide_destroy_dmatable(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100157 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Bartlomiej Zolnierkiewicz5c05ff62008-02-01 23:09:32 +0100159 dma_unmap_sg(hwif->dev, hwif->sg_table, hwif->sg_nents,
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100160 hwif->sg_dma_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162EXPORT_SYMBOL_GPL(ide_destroy_dmatable);
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/**
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100165 * ide_dma_off_quietly - Generic DMA kill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * @drive: drive to control
167 *
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200168 * Turn off the current DMA on this IDE controller.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
170
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100171void ide_dma_off_quietly(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200173 drive->dev_flags &= ~IDE_DFLAG_USING_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 ide_toggle_bounce(drive, 0);
175
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200176 drive->hwif->dma_ops->dma_host_set(drive, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100178EXPORT_SYMBOL(ide_dma_off_quietly);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180/**
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100181 * ide_dma_off - disable DMA on a device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * @drive: drive to disable DMA on
183 *
184 * Disable IDE DMA for a device on this IDE controller.
185 * Inform the user that DMA has been disabled.
186 */
187
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100188void ide_dma_off(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 printk(KERN_INFO "%s: DMA disabled\n", drive->name);
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100191 ide_dma_off_quietly(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100193EXPORT_SYMBOL(ide_dma_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/**
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100196 * ide_dma_on - Enable DMA on a device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * @drive: drive to enable DMA on
198 *
199 * Enable IDE DMA for a device on this IDE controller.
200 */
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100201
202void ide_dma_on(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200204 drive->dev_flags |= IDE_DFLAG_USING_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 ide_toggle_bounce(drive, 1);
206
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200207 drive->hwif->dma_ops->dma_host_set(drive, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200210int __ide_dma_bad_drive(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200212 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Jordan Crouse65e5f2e2005-12-15 02:16:18 +0100214 int blacklist = ide_in_drive_list(id, drive_blacklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (blacklist) {
216 printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n",
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200217 drive->name, (char *)&id[ATA_ID_PROD]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return blacklist;
219 }
220 return 0;
221}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222EXPORT_SYMBOL(__ide_dma_bad_drive);
223
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200224static const u8 xfer_mode_bases[] = {
225 XFER_UDMA_0,
226 XFER_MW_DMA_0,
227 XFER_SW_DMA_0,
228};
229
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200230static unsigned int ide_get_mode_mask(ide_drive_t *drive, u8 base, u8 req_mode)
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200231{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200232 u16 *id = drive->id;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200233 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200234 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200235 unsigned int mask = 0;
236
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200237 switch (base) {
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200238 case XFER_UDMA_0:
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200239 if ((id[ATA_ID_FIELD_VALID] & 4) == 0)
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200240 break;
241
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200242 if (port_ops && port_ops->udma_filter)
243 mask = port_ops->udma_filter(drive);
Sergei Shtylyov851dd332007-10-11 23:53:59 +0200244 else
245 mask = hwif->ultra_mask;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200246 mask &= id[ATA_ID_UDMA_MODES];
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200247
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200248 /*
249 * avoid false cable warning from eighty_ninty_three()
250 */
251 if (req_mode > XFER_UDMA_2) {
252 if ((mask & 0x78) && (eighty_ninty_three(drive) == 0))
253 mask &= 0x07;
254 }
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200255 break;
256 case XFER_MW_DMA_0:
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200257 if ((id[ATA_ID_FIELD_VALID] & 2) == 0)
Sergei Shtylyovb4e44362007-10-11 23:53:58 +0200258 break;
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200259 if (port_ops && port_ops->mdma_filter)
260 mask = port_ops->mdma_filter(drive);
Sergei Shtylyovb4e44362007-10-11 23:53:58 +0200261 else
262 mask = hwif->mwdma_mask;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200263 mask &= id[ATA_ID_MWDMA_MODES];
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200264 break;
265 case XFER_SW_DMA_0:
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200266 if (id[ATA_ID_FIELD_VALID] & 2) {
267 mask = id[ATA_ID_SWDMA_MODES] & hwif->swdma_mask;
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200268 } else if (id[ATA_ID_OLD_DMA_MODES] >> 8) {
269 u8 mode = id[ATA_ID_OLD_DMA_MODES] >> 8;
Bartlomiej Zolnierkiewicz15a4f942007-07-09 23:17:57 +0200270
271 /*
272 * if the mode is valid convert it to the mask
273 * (the maximum allowed mode is XFER_SW_DMA_2)
274 */
275 if (mode <= 2)
276 mask = ((2 << mode) - 1) & hwif->swdma_mask;
277 }
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200278 break;
279 default:
280 BUG();
281 break;
282 }
283
284 return mask;
285}
286
287/**
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200288 * ide_find_dma_mode - compute DMA speed
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200289 * @drive: IDE device
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200290 * @req_mode: requested mode
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200291 *
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200292 * Checks the drive/host capabilities and finds the speed to use for
293 * the DMA transfer. The speed is then limited by the requested mode.
294 *
295 * Returns 0 if the drive/host combination is incapable of DMA transfers
296 * or if the requested mode is not a DMA mode.
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200297 */
298
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200299u8 ide_find_dma_mode(ide_drive_t *drive, u8 req_mode)
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200300{
301 ide_hwif_t *hwif = drive->hwif;
302 unsigned int mask;
303 int x, i;
304 u8 mode = 0;
305
Bartlomiej Zolnierkiewicz33c10022007-10-19 00:30:06 +0200306 if (drive->media != ide_disk) {
307 if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
308 return 0;
309 }
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200310
311 for (i = 0; i < ARRAY_SIZE(xfer_mode_bases); i++) {
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200312 if (req_mode < xfer_mode_bases[i])
313 continue;
314 mask = ide_get_mode_mask(drive, xfer_mode_bases[i], req_mode);
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200315 x = fls(mask) - 1;
316 if (x >= 0) {
317 mode = xfer_mode_bases[i] + x;
318 break;
319 }
320 }
321
Bartlomiej Zolnierkiewicz75d7d962007-10-13 17:47:50 +0200322 if (hwif->chipset == ide_acorn && mode == 0) {
323 /*
324 * is this correct?
325 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200326 if (ide_dma_good_drive(drive) &&
327 drive->id[ATA_ID_EIDE_DMA_TIME] < 150)
Bartlomiej Zolnierkiewicz75d7d962007-10-13 17:47:50 +0200328 mode = XFER_MW_DMA_1;
329 }
330
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100331 mode = min(mode, req_mode);
332
333 printk(KERN_INFO "%s: %s mode selected\n", drive->name,
Bartlomiej Zolnierkiewiczd34887d2007-11-05 21:42:27 +0100334 mode ? ide_xfer_verbose(mode) : "no DMA");
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200335
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100336 return mode;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200337}
Bartlomiej Zolnierkiewicz7670df72007-10-11 23:53:59 +0200338EXPORT_SYMBOL_GPL(ide_find_dma_mode);
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200339
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200340static int ide_tune_dma(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200341{
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +0100342 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200343 u8 speed;
344
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200345 if (ata_id_has_dma(drive->id) == 0 ||
346 (drive->dev_flags & IDE_DFLAG_NODMA))
Bartlomiej Zolnierkiewicz122ab082007-05-16 00:51:46 +0200347 return 0;
348
349 /* consult the list of known "bad" drives */
350 if (__ide_dma_bad_drive(drive))
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200351 return 0;
352
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100353 if (ide_id_dma_bug(drive))
354 return 0;
355
Bartlomiej Zolnierkiewicz8704de82008-01-26 20:13:00 +0100356 if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200357 return config_drive_for_dma(drive);
358
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200359 speed = ide_max_dma_mode(drive);
360
Bartlomiej Zolnierkiewicz951784b2008-04-26 17:36:38 +0200361 if (!speed)
362 return 0;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200363
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200364 if (ide_set_dma_mode(drive, speed))
Bartlomiej Zolnierkiewicz4728d542007-05-16 00:51:46 +0200365 return 0;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200366
Bartlomiej Zolnierkiewicz4728d542007-05-16 00:51:46 +0200367 return 1;
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200368}
369
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200370static int ide_dma_check(ide_drive_t *drive)
371{
372 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200373
Bartlomiej Zolnierkiewiczba4b2e62008-07-23 19:55:55 +0200374 if (ide_tune_dma(drive))
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200375 return 0;
376
377 /* TODO: always do PIO fallback */
378 if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
379 return -1;
380
381 ide_set_max_pio(drive);
382
Bartlomiej Zolnierkiewiczba4b2e62008-07-23 19:55:55 +0200383 return -1;
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200384}
385
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100386int ide_id_dma_bug(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200388 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200390 if (id[ATA_ID_FIELD_VALID] & 4) {
391 if ((id[ATA_ID_UDMA_MODES] >> 8) &&
392 (id[ATA_ID_MWDMA_MODES] >> 8))
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100393 goto err_out;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200394 } else if (id[ATA_ID_FIELD_VALID] & 2) {
395 if ((id[ATA_ID_MWDMA_MODES] >> 8) &&
396 (id[ATA_ID_SWDMA_MODES] >> 8))
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100397 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Bartlomiej Zolnierkiewicz3ab7efe2007-12-12 23:31:58 +0100399 return 0;
400err_out:
401 printk(KERN_ERR "%s: bad DMA info in identify block\n", drive->name);
402 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100405int ide_set_dma(ide_drive_t *drive)
406{
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100407 int rc;
408
Bartlomiej Zolnierkiewicz7b905992008-01-25 22:17:19 +0100409 /*
410 * Force DMAing for the beginning of the check.
411 * Some chipsets appear to do interesting
412 * things, if not checked and cleared.
413 * PARANOIA!!!
414 */
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100415 ide_dma_off_quietly(drive);
Bartlomiej Zolnierkiewicz7b905992008-01-25 22:17:19 +0100416
Bartlomiej Zolnierkiewicz0ae2e172007-10-16 22:29:55 +0200417 rc = ide_dma_check(drive);
Bartlomiej Zolnierkiewicz7b905992008-01-25 22:17:19 +0100418 if (rc)
419 return rc;
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100420
Bartlomiej Zolnierkiewicz4a546e02008-01-26 20:13:01 +0100421 ide_dma_on(drive);
422
423 return 0;
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100424}
425
Bartlomiej Zolnierkiewicz578cfa02008-02-02 19:56:47 +0100426void ide_check_dma_crc(ide_drive_t *drive)
427{
428 u8 mode;
429
430 ide_dma_off_quietly(drive);
431 drive->crc_count = 0;
432 mode = drive->current_speed;
433 /*
434 * Don't try non Ultra-DMA modes without iCRC's. Force the
435 * device to PIO and make the user enable SWDMA/MWDMA modes.
436 */
437 if (mode > XFER_UDMA_0 && mode <= XFER_UDMA_7)
438 mode--;
439 else
440 mode = XFER_PIO_4;
441 ide_set_xfer_rate(drive, mode);
442 if (drive->current_speed >= XFER_SW_DMA_0)
443 ide_dma_on(drive);
444}
445
Bartlomiej Zolnierkiewiczde23ec92008-10-13 21:39:46 +0200446void ide_dma_lost_irq(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Bartlomiej Zolnierkiewiczde23ec92008-10-13 21:39:46 +0200448 printk(KERN_ERR "%s: DMA interrupt recovery\n", drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
Bartlomiej Zolnierkiewiczde23ec92008-10-13 21:39:46 +0200450EXPORT_SYMBOL_GPL(ide_dma_lost_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Bartlomiej Zolnierkiewiczffa15a62008-10-13 21:39:46 +0200452void ide_dma_timeout(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Bartlomiej Zolnierkiewiczdb3f99e2008-10-13 21:39:47 +0200454 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200456 printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
457
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200458 if (hwif->dma_ops->dma_test_irq(drive))
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200459 return;
460
Bartlomiej Zolnierkiewiczffa15a62008-10-13 21:39:46 +0200461 ide_dump_status(drive, "DMA timeout", hwif->tp_ops->read_status(hwif));
462
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200463 hwif->dma_ops->dma_end(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
Bartlomiej Zolnierkiewiczffa15a62008-10-13 21:39:46 +0200465EXPORT_SYMBOL_GPL(ide_dma_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Bartlomiej Zolnierkiewicz0d1bad22008-04-26 22:25:19 +0200467void ide_release_dma_engine(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 if (hwif->dmatable_cpu) {
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200470 int prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100471
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200472 dma_free_coherent(hwif->dev, prd_size,
473 hwif->dmatable_cpu, hwif->dmatable_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 hwif->dmatable_cpu = NULL;
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200477EXPORT_SYMBOL_GPL(ide_release_dma_engine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Bartlomiej Zolnierkiewiczb8e73fb2008-04-26 22:25:21 +0200479int ide_allocate_dma_engine(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200481 int prd_size;
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100482
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200483 if (hwif->prd_max_nents == 0)
484 hwif->prd_max_nents = PRD_ENTRIES;
485 if (hwif->prd_ent_size == 0)
486 hwif->prd_ent_size = PRD_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200488 prd_size = hwif->prd_max_nents * hwif->prd_ent_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200490 hwif->dmatable_cpu = dma_alloc_coherent(hwif->dev, prd_size,
491 &hwif->dmatable_dma,
492 GFP_ATOMIC);
493 if (hwif->dmatable_cpu == NULL) {
494 printk(KERN_ERR "%s: unable to allocate PRD table\n",
Bartlomiej Zolnierkiewicz5e59c232008-04-26 22:25:20 +0200495 hwif->name);
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200496 return -ENOMEM;
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Bartlomiej Zolnierkiewicz2bbd57c2008-10-13 21:39:47 +0200499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
Bartlomiej Zolnierkiewiczb8e73fb2008-04-26 22:25:21 +0200501EXPORT_SYMBOL_GPL(ide_allocate_dma_engine);