ide: add device flags
[deliverable/linux.git] / drivers / ide / ide-dma.c
CommitLineData
1da177e4 1/*
204f47c5
BZ
2 * IDE DMA support (including IDE PCI BM-DMA).
3 *
59bca8cc
BZ
4 * Copyright (C) 1995-1998 Mark Lord
5 * Copyright (C) 1999-2000 Andre Hedrick <andre@linux-ide.org>
6 * Copyright (C) 2004, 2007 Bartlomiej Zolnierkiewicz
58f189fc 7 *
1da177e4 8 * May be copied or modified under the terms of the GNU General Public License
204f47c5
BZ
9 *
10 * DMA is supported for all IDE devices (disk drives, cdroms, tapes, floppies).
1da177e4
LT
11 */
12
13/*
14 * Special Thanks to Mark for his Six years of work.
1da177e4
LT
15 */
16
17/*
1da177e4
LT
18 * 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!
1da177e4
LT
29 */
30
1da177e4
LT
31#include <linux/module.h>
32#include <linux/types.h>
33#include <linux/kernel.h>
34#include <linux/timer.h>
35#include <linux/mm.h>
36#include <linux/interrupt.h>
37#include <linux/pci.h>
38#include <linux/init.h>
39#include <linux/ide.h>
40#include <linux/delay.h>
41#include <linux/scatterlist.h>
5c05ff68 42#include <linux/dma-mapping.h>
1da177e4
LT
43
44#include <asm/io.h>
45#include <asm/irq.h>
46
1da177e4
LT
47static const struct drive_list_entry drive_whitelist [] = {
48
c2d3ce8c
JH
49 { "Micropolis 2112A" , NULL },
50 { "CONNER CTMA 4000" , NULL },
51 { "CONNER CTT8000-A" , NULL },
52 { "ST34342A" , NULL },
1da177e4
LT
53 { NULL , NULL }
54};
55
56static const struct drive_list_entry drive_blacklist [] = {
57
c2d3ce8c
JH
58 { "WDC AC11000H" , NULL },
59 { "WDC AC22100H" , NULL },
60 { "WDC AC32500H" , NULL },
61 { "WDC AC33100H" , NULL },
62 { "WDC AC31600H" , NULL },
1da177e4
LT
63 { "WDC AC32100H" , "24.09P07" },
64 { "WDC AC23200L" , "21.10N21" },
c2d3ce8c
JH
65 { "Compaq CRD-8241B" , NULL },
66 { "CRD-8400B" , NULL },
67 { "CRD-8480B", NULL },
68 { "CRD-8482B", NULL },
69 { "CRD-84" , NULL },
70 { "SanDisk SDP3B" , NULL },
71 { "SanDisk SDP3B-64" , NULL },
72 { "SANYO CD-ROM CRD" , NULL },
73 { "HITACHI CDR-8" , NULL },
74 { "HITACHI CDR-8335" , NULL },
75 { "HITACHI CDR-8435" , NULL },
76 { "Toshiba CD-ROM XM-6202B" , NULL },
77 { "TOSHIBA CD-ROM XM-1702BC", NULL },
78 { "CD-532E-A" , NULL },
79 { "E-IDE CD-ROM CR-840", NULL },
80 { "CD-ROM Drive/F5A", NULL },
81 { "WPI CDD-820", NULL },
82 { "SAMSUNG CD-ROM SC-148C", NULL },
83 { "SAMSUNG CD-ROM SC", NULL },
84 { "ATAPI CD-ROM DRIVE 40X MAXIMUM", NULL },
85 { "_NEC DV5800A", NULL },
5a6248ca 86 { "SAMSUNG CD-ROM SN-124", "N001" },
c2d3ce8c 87 { "Seagate STT20000A", NULL },
b0bc65b9 88 { "CD-ROM CDR_U200", "1.09" },
1da177e4
LT
89 { NULL , NULL }
90
91};
92
1da177e4
LT
93/**
94 * ide_dma_intr - IDE DMA interrupt handler
95 * @drive: the drive the interrupt is for
96 *
97 * Handle an interrupt completing a read/write DMA transfer on an
98 * IDE device
99 */
100
101ide_startstop_t ide_dma_intr (ide_drive_t *drive)
102{
b73c7ee2 103 ide_hwif_t *hwif = drive->hwif;
1da177e4
LT
104 u8 stat = 0, dma_stat = 0;
105
b73c7ee2 106 dma_stat = hwif->dma_ops->dma_end(drive);
374e042c 107 stat = hwif->tp_ops->read_status(hwif);
c47137a9 108
3a7d2484 109 if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat | ATA_DRQ)) {
1da177e4
LT
110 if (!dma_stat) {
111 struct request *rq = HWGROUP(drive)->rq;
112
4d7a984b 113 task_end_request(drive, rq, stat);
1da177e4
LT
114 return ide_stopped;
115 }
116 printk(KERN_ERR "%s: dma_intr: bad DMA status (dma_stat=%x)\n",
117 drive->name, dma_stat);
118 }
119 return ide_error(drive, "dma_intr", stat);
120}
121
122EXPORT_SYMBOL_GPL(ide_dma_intr);
123
75d7d963
BZ
124static int ide_dma_good_drive(ide_drive_t *drive)
125{
126 return ide_in_drive_list(drive->id, drive_whitelist);
127}
128
1da177e4
LT
129/**
130 * ide_build_sglist - map IDE scatter gather for DMA I/O
131 * @drive: the drive to build the DMA table for
132 * @rq: the request holding the sg list
133 *
5c05ff68
BZ
134 * Perform the DMA mapping magic necessary to access the source or
135 * target buffers of a request via DMA. The lower layers of the
1da177e4 136 * kernel provide the necessary cache management so that we can
5c05ff68 137 * operate in a portable fashion.
1da177e4
LT
138 */
139
140int ide_build_sglist(ide_drive_t *drive, struct request *rq)
141{
142 ide_hwif_t *hwif = HWIF(drive);
143 struct scatterlist *sg = hwif->sg_table;
144
1da177e4
LT
145 ide_map_sg(drive, rq);
146
147 if (rq_data_dir(rq) == READ)
5c05ff68 148 hwif->sg_dma_direction = DMA_FROM_DEVICE;
1da177e4 149 else
5c05ff68 150 hwif->sg_dma_direction = DMA_TO_DEVICE;
1da177e4 151
5c05ff68
BZ
152 return dma_map_sg(hwif->dev, sg, hwif->sg_nents,
153 hwif->sg_dma_direction);
1da177e4
LT
154}
155
156EXPORT_SYMBOL_GPL(ide_build_sglist);
157
8e882ba1 158#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
1da177e4
LT
159/**
160 * ide_build_dmatable - build IDE DMA table
161 *
162 * ide_build_dmatable() prepares a dma request. We map the command
163 * to get the pci bus addresses of the buffers and then build up
164 * the PRD table that the IDE layer wants to be fed. The code
165 * knows about the 64K wrap bug in the CS5530.
166 *
167 * Returns the number of built PRD entries if all went okay,
168 * returns 0 otherwise.
169 *
170 * May also be invoked from trm290.c
171 */
172
173int ide_build_dmatable (ide_drive_t *drive, struct request *rq)
174{
175 ide_hwif_t *hwif = HWIF(drive);
7fa897b9 176 __le32 *table = (__le32 *)hwif->dmatable_cpu;
1da177e4
LT
177 unsigned int is_trm290 = (hwif->chipset == ide_trm290) ? 1 : 0;
178 unsigned int count = 0;
179 int i;
180 struct scatterlist *sg;
181
182 hwif->sg_nents = i = ide_build_sglist(drive, rq);
183
184 if (!i)
185 return 0;
186
187 sg = hwif->sg_table;
188 while (i) {
189 u32 cur_addr;
190 u32 cur_len;
191
192 cur_addr = sg_dma_address(sg);
193 cur_len = sg_dma_len(sg);
194
195 /*
196 * Fill in the dma table, without crossing any 64kB boundaries.
197 * Most hardware requires 16-bit alignment of all blocks,
198 * but the trm290 requires 32-bit alignment.
199 */
200
201 while (cur_len) {
202 if (count++ >= PRD_ENTRIES) {
203 printk(KERN_ERR "%s: DMA table too small\n", drive->name);
204 goto use_pio_instead;
205 } else {
206 u32 xcount, bcount = 0x10000 - (cur_addr & 0xffff);
207
208 if (bcount > cur_len)
209 bcount = cur_len;
210 *table++ = cpu_to_le32(cur_addr);
211 xcount = bcount & 0xffff;
212 if (is_trm290)
213 xcount = ((xcount >> 2) - 1) << 16;
22e05b45 214 else if (xcount == 0x0000) {
1da177e4
LT
215 /*
216 * Most chipsets correctly interpret a length of 0x0000 as 64KB,
217 * but at least one (e.g. CS5530) misinterprets it as zero (!).
218 * So here we break the 64KB entry into two 32KB entries instead.
219 */
220 if (count++ >= PRD_ENTRIES) {
221 printk(KERN_ERR "%s: DMA table too small\n", drive->name);
222 goto use_pio_instead;
223 }
224 *table++ = cpu_to_le32(0x8000);
225 *table++ = cpu_to_le32(cur_addr + 0x8000);
226 xcount = 0x8000;
227 }
228 *table++ = cpu_to_le32(xcount);
229 cur_addr += bcount;
230 cur_len -= bcount;
231 }
232 }
233
55c16a70 234 sg = sg_next(sg);
1da177e4
LT
235 i--;
236 }
237
238 if (count) {
239 if (!is_trm290)
240 *--table |= cpu_to_le32(0x80000000);
241 return count;
242 }
f6fb786d 243
1da177e4 244 printk(KERN_ERR "%s: empty DMA table?\n", drive->name);
f6fb786d 245
1da177e4 246use_pio_instead:
f6fb786d
BZ
247 ide_destroy_dmatable(drive);
248
1da177e4
LT
249 return 0; /* revert to PIO for this request */
250}
251
252EXPORT_SYMBOL_GPL(ide_build_dmatable);
062f9f02 253#endif
1da177e4
LT
254
255/**
256 * ide_destroy_dmatable - clean up DMA mapping
257 * @drive: The drive to unmap
258 *
259 * Teardown mappings after DMA has completed. This must be called
260 * after the completion of each use of ide_build_dmatable and before
261 * the next use of ide_build_dmatable. Failure to do so will cause
262 * an oops as only one mapping can be live for each target at a given
263 * time.
264 */
265
266void ide_destroy_dmatable (ide_drive_t *drive)
267{
36501650 268 ide_hwif_t *hwif = drive->hwif;
1da177e4 269
5c05ff68 270 dma_unmap_sg(hwif->dev, hwif->sg_table, hwif->sg_nents,
36501650 271 hwif->sg_dma_direction);
1da177e4
LT
272}
273
274EXPORT_SYMBOL_GPL(ide_destroy_dmatable);
275
8e882ba1 276#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
1da177e4
LT
277/**
278 * config_drive_for_dma - attempt to activate IDE DMA
279 * @drive: the drive to place in DMA mode
280 *
281 * If the drive supports at least mode 2 DMA or UDMA of any kind
282 * then attempt to place it into DMA mode. Drives that are known to
283 * support DMA but predate the DMA properties or that are known
284 * to have DMA handling bugs are also set up appropriately based
285 * on the good/bad drive lists.
286 */
287
288static int config_drive_for_dma (ide_drive_t *drive)
289{
1116fae5 290 ide_hwif_t *hwif = drive->hwif;
4dde4492 291 u16 *id = drive->id;
1da177e4 292
33c1002e
BZ
293 if (drive->media != ide_disk) {
294 if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
bcbf6ee3 295 return 0;
33c1002e 296 }
1116fae5 297
0ae2e178
BZ
298 /*
299 * Enable DMA on any drive that has
300 * UltraDMA (mode 0/1/2/3/4/5/6) enabled
301 */
4dde4492
BZ
302 if ((id[ATA_ID_FIELD_VALID] & 4) &&
303 ((id[ATA_ID_UDMA_MODES] >> 8) & 0x7f))
0ae2e178
BZ
304 return 1;
305
306 /*
307 * Enable DMA on any drive that has mode2 DMA
308 * (multi or single) enabled
309 */
4dde4492
BZ
310 if (id[ATA_ID_FIELD_VALID] & 2) /* regular DMA */
311 if ((id[ATA_ID_MWDMA_MODES] & 0x404) == 0x404 ||
312 (id[ATA_ID_SWDMA_MODES] & 0x404) == 0x404)
0ae2e178 313 return 1;
3608b5d7 314
0ae2e178
BZ
315 /* Consult the list of known "good" drives */
316 if (ide_dma_good_drive(drive))
317 return 1;
318
319 return 0;
1da177e4
LT
320}
321
322/**
323 * dma_timer_expiry - handle a DMA timeout
324 * @drive: Drive that timed out
325 *
326 * An IDE DMA transfer timed out. In the event of an error we ask
327 * the driver to resolve the problem, if a DMA transfer is still
328 * in progress we continue to wait (arguably we need to add a
329 * secondary 'I don't care what the drive thinks' timeout here)
330 * Finally if we have an interrupt we let it complete the I/O.
331 * But only one time - we clear expiry and if it's still not
332 * completed after WAIT_CMD, we error and retry in PIO.
333 * This can occur if an interrupt is lost or due to hang or bugs.
334 */
335
336static int dma_timer_expiry (ide_drive_t *drive)
337{
338 ide_hwif_t *hwif = HWIF(drive);
374e042c 339 u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
1da177e4
LT
340
341 printk(KERN_WARNING "%s: dma_timer_expiry: dma status == 0x%02x\n",
342 drive->name, dma_stat);
343
344 if ((dma_stat & 0x18) == 0x18) /* BUSY Stupid Early Timer !! */
345 return WAIT_CMD;
346
347 HWGROUP(drive)->expiry = NULL; /* one free ride for now */
348
349 /* 1 dmaing, 2 error, 4 intr */
350 if (dma_stat & 2) /* ERROR */
351 return -1;
352
353 if (dma_stat & 1) /* DMAing */
354 return WAIT_CMD;
355
356 if (dma_stat & 4) /* Got an Interrupt */
357 return WAIT_CMD;
358
359 return 0; /* Status is unknown -- reset the bus */
360}
361
362/**
15ce926a 363 * ide_dma_host_set - Enable/disable DMA on a host
1da177e4
LT
364 * @drive: drive to control
365 *
15ce926a
BZ
366 * Enable/disable DMA on an IDE controller following generic
367 * bus-mastering IDE controller behaviour.
1da177e4
LT
368 */
369
15ce926a 370void ide_dma_host_set(ide_drive_t *drive, int on)
1da177e4
LT
371{
372 ide_hwif_t *hwif = HWIF(drive);
373 u8 unit = (drive->select.b.unit & 0x01);
374e042c 374 u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
1da177e4 375
15ce926a
BZ
376 if (on)
377 dma_stat |= (1 << (5 + unit));
378 else
379 dma_stat &= ~(1 << (5 + unit));
380
ab86f91e 381 if (hwif->host_flags & IDE_HFLAG_MMIO)
cab7f8ed
BZ
382 writeb(dma_stat,
383 (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
ab86f91e 384 else
cab7f8ed 385 outb(dma_stat, hwif->dma_base + ATA_DMA_STATUS);
1da177e4
LT
386}
387
15ce926a 388EXPORT_SYMBOL_GPL(ide_dma_host_set);
8e882ba1 389#endif /* CONFIG_BLK_DEV_IDEDMA_SFF */
1da177e4
LT
390
391/**
7469aaf6 392 * ide_dma_off_quietly - Generic DMA kill
1da177e4
LT
393 * @drive: drive to control
394 *
395 * Turn off the current DMA on this IDE controller.
396 */
397
7469aaf6 398void ide_dma_off_quietly(ide_drive_t *drive)
1da177e4 399{
97100fc8 400 drive->dev_flags &= ~IDE_DFLAG_USING_DMA;
1da177e4
LT
401 ide_toggle_bounce(drive, 0);
402
5e37bdc0 403 drive->hwif->dma_ops->dma_host_set(drive, 0);
1da177e4
LT
404}
405
7469aaf6 406EXPORT_SYMBOL(ide_dma_off_quietly);
1da177e4
LT
407
408/**
7469aaf6 409 * ide_dma_off - disable DMA on a device
1da177e4
LT
410 * @drive: drive to disable DMA on
411 *
412 * Disable IDE DMA for a device on this IDE controller.
413 * Inform the user that DMA has been disabled.
414 */
415
7469aaf6 416void ide_dma_off(ide_drive_t *drive)
1da177e4
LT
417{
418 printk(KERN_INFO "%s: DMA disabled\n", drive->name);
4a546e04 419 ide_dma_off_quietly(drive);
1da177e4
LT
420}
421
7469aaf6 422EXPORT_SYMBOL(ide_dma_off);
1da177e4 423
1da177e4 424/**
4a546e04 425 * ide_dma_on - Enable DMA on a device
1da177e4
LT
426 * @drive: drive to enable DMA on
427 *
428 * Enable IDE DMA for a device on this IDE controller.
429 */
4a546e04
BZ
430
431void ide_dma_on(ide_drive_t *drive)
1da177e4 432{
97100fc8 433 drive->dev_flags |= IDE_DFLAG_USING_DMA;
1da177e4
LT
434 ide_toggle_bounce(drive, 1);
435
5e37bdc0 436 drive->hwif->dma_ops->dma_host_set(drive, 1);
1da177e4
LT
437}
438
8e882ba1 439#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
1da177e4
LT
440/**
441 * ide_dma_setup - begin a DMA phase
442 * @drive: target device
443 *
444 * Build an IDE DMA PRD (IDE speak for scatter gather table)
445 * and then set up the DMA transfer registers for a device
446 * that follows generic IDE PCI DMA behaviour. Controllers can
447 * override this function if they need to
448 *
449 * Returns 0 on success. If a PIO fallback is required then 1
450 * is returned.
451 */
452
453int ide_dma_setup(ide_drive_t *drive)
454{
455 ide_hwif_t *hwif = drive->hwif;
456 struct request *rq = HWGROUP(drive)->rq;
457 unsigned int reading;
ab86f91e 458 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
1da177e4
LT
459 u8 dma_stat;
460
461 if (rq_data_dir(rq))
462 reading = 0;
463 else
464 reading = 1 << 3;
465
466 /* fall back to pio! */
467 if (!ide_build_dmatable(drive, rq)) {
468 ide_map_sg(drive, rq);
469 return 1;
470 }
471
472 /* PRD table */
13572144 473 if (hwif->host_flags & IDE_HFLAG_MMIO)
55224bc8
BZ
474 writel(hwif->dmatable_dma,
475 (void __iomem *)(hwif->dma_base + ATA_DMA_TABLE_OFS));
0ecdca26 476 else
55224bc8 477 outl(hwif->dmatable_dma, hwif->dma_base + ATA_DMA_TABLE_OFS);
1da177e4
LT
478
479 /* specify r/w */
ab86f91e 480 if (mmio)
cab7f8ed 481 writeb(reading, (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
ab86f91e 482 else
cab7f8ed 483 outb(reading, hwif->dma_base + ATA_DMA_CMD);
1da177e4 484
b2f951aa 485 /* read DMA status for INTR & ERROR flags */
374e042c 486 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
1da177e4
LT
487
488 /* clear INTR & ERROR flags */
ab86f91e 489 if (mmio)
cab7f8ed
BZ
490 writeb(dma_stat | 6,
491 (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
ab86f91e 492 else
cab7f8ed 493 outb(dma_stat | 6, hwif->dma_base + ATA_DMA_STATUS);
ab86f91e 494
1da177e4
LT
495 drive->waiting_for_dma = 1;
496 return 0;
497}
498
499EXPORT_SYMBOL_GPL(ide_dma_setup);
500
f37afdac 501void ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
1da177e4
LT
502{
503 /* issue cmd to drive */
504 ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, dma_timer_expiry);
505}
f37afdac 506EXPORT_SYMBOL_GPL(ide_dma_exec_cmd);
1da177e4
LT
507
508void ide_dma_start(ide_drive_t *drive)
509{
ab86f91e
BZ
510 ide_hwif_t *hwif = drive->hwif;
511 u8 dma_cmd;
1da177e4
LT
512
513 /* Note that this is done *after* the cmd has
514 * been issued to the drive, as per the BM-IDE spec.
515 * The Promise Ultra33 doesn't work correctly when
516 * we do this part before issuing the drive cmd.
517 */
ab86f91e 518 if (hwif->host_flags & IDE_HFLAG_MMIO) {
cab7f8ed 519 dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
ab86f91e 520 /* start DMA */
cab7f8ed
BZ
521 writeb(dma_cmd | 1,
522 (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
ab86f91e 523 } else {
cab7f8ed
BZ
524 dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
525 outb(dma_cmd | 1, hwif->dma_base + ATA_DMA_CMD);
ab86f91e
BZ
526 }
527
1da177e4
LT
528 hwif->dma = 1;
529 wmb();
530}
531
532EXPORT_SYMBOL_GPL(ide_dma_start);
533
534/* returns 1 on error, 0 otherwise */
535int __ide_dma_end (ide_drive_t *drive)
536{
ab86f91e
BZ
537 ide_hwif_t *hwif = drive->hwif;
538 u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
1da177e4
LT
539 u8 dma_stat = 0, dma_cmd = 0;
540
541 drive->waiting_for_dma = 0;
ab86f91e
BZ
542
543 if (mmio) {
544 /* get DMA command mode */
cab7f8ed 545 dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
ab86f91e 546 /* stop DMA */
cab7f8ed
BZ
547 writeb(dma_cmd & ~1,
548 (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
ab86f91e 549 } else {
cab7f8ed
BZ
550 dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
551 outb(dma_cmd & ~1, hwif->dma_base + ATA_DMA_CMD);
ab86f91e
BZ
552 }
553
1da177e4 554 /* get DMA status */
374e042c 555 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
ab86f91e
BZ
556
557 if (mmio)
558 /* clear the INTR & ERROR bits */
cab7f8ed
BZ
559 writeb(dma_stat | 6,
560 (void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
ab86f91e 561 else
cab7f8ed 562 outb(dma_stat | 6, hwif->dma_base + ATA_DMA_STATUS);
ab86f91e 563
1da177e4
LT
564 /* purge DMA mappings */
565 ide_destroy_dmatable(drive);
566 /* verify good DMA status */
567 hwif->dma = 0;
568 wmb();
569 return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0;
570}
571
572EXPORT_SYMBOL(__ide_dma_end);
573
574/* returns 1 if dma irq issued, 0 otherwise */
f37afdac 575int ide_dma_test_irq(ide_drive_t *drive)
1da177e4
LT
576{
577 ide_hwif_t *hwif = HWIF(drive);
374e042c 578 u8 dma_stat = hwif->tp_ops->read_sff_dma_status(hwif);
1da177e4 579
1da177e4
LT
580 /* return 1 if INTR asserted */
581 if ((dma_stat & 4) == 4)
582 return 1;
583 if (!drive->waiting_for_dma)
584 printk(KERN_WARNING "%s: (%s) called while not waiting\n",
eb63963a 585 drive->name, __func__);
1da177e4
LT
586 return 0;
587}
f37afdac 588EXPORT_SYMBOL_GPL(ide_dma_test_irq);
0ae2e178
BZ
589#else
590static inline int config_drive_for_dma(ide_drive_t *drive) { return 0; }
8e882ba1 591#endif /* CONFIG_BLK_DEV_IDEDMA_SFF */
1da177e4
LT
592
593int __ide_dma_bad_drive (ide_drive_t *drive)
594{
4dde4492 595 u16 *id = drive->id;
1da177e4 596
65e5f2e3 597 int blacklist = ide_in_drive_list(id, drive_blacklist);
1da177e4
LT
598 if (blacklist) {
599 printk(KERN_WARNING "%s: Disabling (U)DMA for %s (blacklisted)\n",
4dde4492 600 drive->name, (char *)&id[ATA_ID_PROD]);
1da177e4
LT
601 return blacklist;
602 }
603 return 0;
604}
605
606EXPORT_SYMBOL(__ide_dma_bad_drive);
607
2d5eaa6d
BZ
608static const u8 xfer_mode_bases[] = {
609 XFER_UDMA_0,
610 XFER_MW_DMA_0,
611 XFER_SW_DMA_0,
612};
613
7670df73 614static unsigned int ide_get_mode_mask(ide_drive_t *drive, u8 base, u8 req_mode)
2d5eaa6d 615{
4dde4492 616 u16 *id = drive->id;
2d5eaa6d 617 ide_hwif_t *hwif = drive->hwif;
ac95beed 618 const struct ide_port_ops *port_ops = hwif->port_ops;
2d5eaa6d
BZ
619 unsigned int mask = 0;
620
621 switch(base) {
622 case XFER_UDMA_0:
4dde4492 623 if ((id[ATA_ID_FIELD_VALID] & 4) == 0)
2d5eaa6d
BZ
624 break;
625
ac95beed
BZ
626 if (port_ops && port_ops->udma_filter)
627 mask = port_ops->udma_filter(drive);
851dd33b
SS
628 else
629 mask = hwif->ultra_mask;
4dde4492 630 mask &= id[ATA_ID_UDMA_MODES];
2d5eaa6d 631
7670df73
BZ
632 /*
633 * avoid false cable warning from eighty_ninty_three()
634 */
635 if (req_mode > XFER_UDMA_2) {
636 if ((mask & 0x78) && (eighty_ninty_three(drive) == 0))
637 mask &= 0x07;
638 }
2d5eaa6d
BZ
639 break;
640 case XFER_MW_DMA_0:
4dde4492 641 if ((id[ATA_ID_FIELD_VALID] & 2) == 0)
b4e44369 642 break;
ac95beed
BZ
643 if (port_ops && port_ops->mdma_filter)
644 mask = port_ops->mdma_filter(drive);
b4e44369
SS
645 else
646 mask = hwif->mwdma_mask;
4dde4492 647 mask &= id[ATA_ID_MWDMA_MODES];
2d5eaa6d
BZ
648 break;
649 case XFER_SW_DMA_0:
4dde4492
BZ
650 if (id[ATA_ID_FIELD_VALID] & 2) {
651 mask = id[ATA_ID_SWDMA_MODES] & hwif->swdma_mask;
48fb2688
BZ
652 } else if (id[ATA_ID_OLD_DMA_MODES] >> 8) {
653 u8 mode = id[ATA_ID_OLD_DMA_MODES] >> 8;
15a4f943
BZ
654
655 /*
656 * if the mode is valid convert it to the mask
657 * (the maximum allowed mode is XFER_SW_DMA_2)
658 */
659 if (mode <= 2)
660 mask = ((2 << mode) - 1) & hwif->swdma_mask;
661 }
2d5eaa6d
BZ
662 break;
663 default:
664 BUG();
665 break;
666 }
667
668 return mask;
669}
670
671/**
7670df73 672 * ide_find_dma_mode - compute DMA speed
2d5eaa6d 673 * @drive: IDE device
7670df73
BZ
674 * @req_mode: requested mode
675 *
676 * Checks the drive/host capabilities and finds the speed to use for
677 * the DMA transfer. The speed is then limited by the requested mode.
2d5eaa6d 678 *
7670df73
BZ
679 * Returns 0 if the drive/host combination is incapable of DMA transfers
680 * or if the requested mode is not a DMA mode.
2d5eaa6d
BZ
681 */
682
7670df73 683u8 ide_find_dma_mode(ide_drive_t *drive, u8 req_mode)
2d5eaa6d
BZ
684{
685 ide_hwif_t *hwif = drive->hwif;
686 unsigned int mask;
687 int x, i;
688 u8 mode = 0;
689
33c1002e
BZ
690 if (drive->media != ide_disk) {
691 if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
692 return 0;
693 }
2d5eaa6d
BZ
694
695 for (i = 0; i < ARRAY_SIZE(xfer_mode_bases); i++) {
7670df73
BZ
696 if (req_mode < xfer_mode_bases[i])
697 continue;
698 mask = ide_get_mode_mask(drive, xfer_mode_bases[i], req_mode);
2d5eaa6d
BZ
699 x = fls(mask) - 1;
700 if (x >= 0) {
701 mode = xfer_mode_bases[i] + x;
702 break;
703 }
704 }
705
75d7d963
BZ
706 if (hwif->chipset == ide_acorn && mode == 0) {
707 /*
708 * is this correct?
709 */
4dde4492
BZ
710 if (ide_dma_good_drive(drive) &&
711 drive->id[ATA_ID_EIDE_DMA_TIME] < 150)
75d7d963
BZ
712 mode = XFER_MW_DMA_1;
713 }
714
3ab7efe8
BZ
715 mode = min(mode, req_mode);
716
717 printk(KERN_INFO "%s: %s mode selected\n", drive->name,
d34887da 718 mode ? ide_xfer_verbose(mode) : "no DMA");
2d5eaa6d 719
3ab7efe8 720 return mode;
2d5eaa6d
BZ
721}
722
7670df73 723EXPORT_SYMBOL_GPL(ide_find_dma_mode);
2d5eaa6d 724
0ae2e178 725static int ide_tune_dma(ide_drive_t *drive)
29e744d0 726{
8704de8f 727 ide_hwif_t *hwif = drive->hwif;
29e744d0
BZ
728 u8 speed;
729
97100fc8
BZ
730 if (ata_id_has_dma(drive->id) == 0 ||
731 (drive->dev_flags & IDE_DFLAG_NODMA))
122ab088
BZ
732 return 0;
733
734 /* consult the list of known "bad" drives */
735 if (__ide_dma_bad_drive(drive))
29e744d0
BZ
736 return 0;
737
3ab7efe8
BZ
738 if (ide_id_dma_bug(drive))
739 return 0;
740
8704de8f 741 if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
0ae2e178
BZ
742 return config_drive_for_dma(drive);
743
29e744d0
BZ
744 speed = ide_max_dma_mode(drive);
745
951784b6
BZ
746 if (!speed)
747 return 0;
29e744d0 748
88b2b32b 749 if (ide_set_dma_mode(drive, speed))
4728d546 750 return 0;
29e744d0 751
4728d546 752 return 1;
29e744d0
BZ
753}
754
0ae2e178
BZ
755static int ide_dma_check(ide_drive_t *drive)
756{
757 ide_hwif_t *hwif = drive->hwif;
0ae2e178 758
ba4b2e60 759 if (ide_tune_dma(drive))
0ae2e178
BZ
760 return 0;
761
762 /* TODO: always do PIO fallback */
763 if (hwif->host_flags & IDE_HFLAG_TRUST_BIOS_FOR_DMA)
764 return -1;
765
766 ide_set_max_pio(drive);
767
ba4b2e60 768 return -1;
0ae2e178
BZ
769}
770
3ab7efe8 771int ide_id_dma_bug(ide_drive_t *drive)
1da177e4 772{
4dde4492 773 u16 *id = drive->id;
1da177e4 774
4dde4492
BZ
775 if (id[ATA_ID_FIELD_VALID] & 4) {
776 if ((id[ATA_ID_UDMA_MODES] >> 8) &&
777 (id[ATA_ID_MWDMA_MODES] >> 8))
3ab7efe8 778 goto err_out;
4dde4492
BZ
779 } else if (id[ATA_ID_FIELD_VALID] & 2) {
780 if ((id[ATA_ID_MWDMA_MODES] >> 8) &&
781 (id[ATA_ID_SWDMA_MODES] >> 8))
3ab7efe8 782 goto err_out;
1da177e4 783 }
3ab7efe8
BZ
784 return 0;
785err_out:
786 printk(KERN_ERR "%s: bad DMA info in identify block\n", drive->name);
787 return 1;
1da177e4
LT
788}
789
3608b5d7
BZ
790int ide_set_dma(ide_drive_t *drive)
791{
3608b5d7
BZ
792 int rc;
793
7b905994
BZ
794 /*
795 * Force DMAing for the beginning of the check.
796 * Some chipsets appear to do interesting
797 * things, if not checked and cleared.
798 * PARANOIA!!!
799 */
4a546e04 800 ide_dma_off_quietly(drive);
3608b5d7 801
7b905994
BZ
802 rc = ide_dma_check(drive);
803 if (rc)
804 return rc;
3608b5d7 805
4a546e04
BZ
806 ide_dma_on(drive);
807
808 return 0;
3608b5d7
BZ
809}
810
578cfa0d
BZ
811void ide_check_dma_crc(ide_drive_t *drive)
812{
813 u8 mode;
814
815 ide_dma_off_quietly(drive);
816 drive->crc_count = 0;
817 mode = drive->current_speed;
818 /*
819 * Don't try non Ultra-DMA modes without iCRC's. Force the
820 * device to PIO and make the user enable SWDMA/MWDMA modes.
821 */
822 if (mode > XFER_UDMA_0 && mode <= XFER_UDMA_7)
823 mode--;
824 else
825 mode = XFER_PIO_4;
826 ide_set_xfer_rate(drive, mode);
827 if (drive->current_speed >= XFER_SW_DMA_0)
828 ide_dma_on(drive);
829}
830
8e882ba1 831#ifdef CONFIG_BLK_DEV_IDEDMA_SFF
841d2a9b 832void ide_dma_lost_irq (ide_drive_t *drive)
1da177e4
LT
833{
834 printk("%s: DMA interrupt recovery\n", drive->name);
1da177e4
LT
835}
836
841d2a9b 837EXPORT_SYMBOL(ide_dma_lost_irq);
1da177e4 838
c283f5db 839void ide_dma_timeout (ide_drive_t *drive)
1da177e4 840{
c283f5db
SS
841 ide_hwif_t *hwif = HWIF(drive);
842
1da177e4 843 printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
1da177e4 844
5e37bdc0 845 if (hwif->dma_ops->dma_test_irq(drive))
c283f5db
SS
846 return;
847
5e37bdc0 848 hwif->dma_ops->dma_end(drive);
1da177e4
LT
849}
850
c283f5db 851EXPORT_SYMBOL(ide_dma_timeout);
1da177e4 852
0d1bad21 853void ide_release_dma_engine(ide_hwif_t *hwif)
1da177e4
LT
854{
855 if (hwif->dmatable_cpu) {
36501650
BZ
856 struct pci_dev *pdev = to_pci_dev(hwif->dev);
857
858 pci_free_consistent(pdev, PRD_ENTRIES * PRD_BYTES,
859 hwif->dmatable_cpu, hwif->dmatable_dma);
1da177e4
LT
860 hwif->dmatable_cpu = NULL;
861 }
1da177e4
LT
862}
863
b8e73fba 864int ide_allocate_dma_engine(ide_hwif_t *hwif)
1da177e4 865{
36501650
BZ
866 struct pci_dev *pdev = to_pci_dev(hwif->dev);
867
868 hwif->dmatable_cpu = pci_alloc_consistent(pdev,
1da177e4
LT
869 PRD_ENTRIES * PRD_BYTES,
870 &hwif->dmatable_dma);
871
872 if (hwif->dmatable_cpu)
873 return 0;
874
dc844e05 875 printk(KERN_ERR "%s: -- Error, unable to allocate DMA table.\n",
5e59c236 876 hwif->name);
1da177e4 877
1da177e4
LT
878 return 1;
879}
b8e73fba 880EXPORT_SYMBOL_GPL(ide_allocate_dma_engine);
1da177e4 881
81e8d5a3 882const struct ide_dma_ops sff_dma_ops = {
5e37bdc0
BZ
883 .dma_host_set = ide_dma_host_set,
884 .dma_setup = ide_dma_setup,
885 .dma_exec_cmd = ide_dma_exec_cmd,
886 .dma_start = ide_dma_start,
887 .dma_end = __ide_dma_end,
f37afdac 888 .dma_test_irq = ide_dma_test_irq,
5e37bdc0
BZ
889 .dma_timeout = ide_dma_timeout,
890 .dma_lost_irq = ide_dma_lost_irq,
891};
81e8d5a3 892EXPORT_SYMBOL_GPL(sff_dma_ops);
8e882ba1 893#endif /* CONFIG_BLK_DEV_IDEDMA_SFF */
This page took 0.432563 seconds and 5 git commands to generate.