Import 2.3.13pre7
[davej-history.git] / drivers / block / ide-dma.c
blob008756dc7224f37e81e302c6041ebe927668f0ba
1 /*
2 * linux/drivers/block/ide-dma.c Version 4.09 April 23, 1999
4 * Copyright (c) 1999 Andre Hedrick
5 * May be copied or modified under the terms of the GNU General Public License
6 */
8 /*
9 * Special Thanks to Mark for his Six years of work.
11 * Copyright (c) 1995-1998 Mark Lord
12 * May be copied or modified under the terms of the GNU General Public License
16 * This module provides support for the bus-master IDE DMA functions
17 * of various PCI chipsets, including the Intel PIIX (i82371FB for
18 * the 430 FX chipset), the PIIX3 (i82371SB for the 430 HX/VX and
19 * 440 chipsets), and the PIIX4 (i82371AB for the 430 TX chipset)
20 * ("PIIX" stands for "PCI ISA IDE Xcellerator").
22 * Pretty much the same code works for other IDE PCI bus-mastering chipsets.
24 * DMA is supported for all IDE devices (disk drives, cdroms, tapes, floppies).
26 * By default, DMA support is prepared for use, but is currently enabled only
27 * for drives which already have DMA enabled (UltraDMA or mode 2 multi/single),
28 * or which are recognized as "good" (see table below). Drives with only mode0
29 * or mode1 (multi/single) DMA should also work with this chipset/driver
30 * (eg. MC2112A) but are not enabled by default.
32 * Use "hdparm -i" to view modes supported by a given drive.
34 * The hdparm-3.5 (or later) utility can be used for manually enabling/disabling
35 * DMA support, but must be (re-)compiled against this kernel version or later.
37 * To enable DMA, use "hdparm -d1 /dev/hd?" on a per-drive basis after booting.
38 * If problems arise, ide.c will disable DMA operation after a few retries.
39 * This error recovery mechanism works and has been extremely well exercised.
41 * IDE drives, depending on their vintage, may support several different modes
42 * of DMA operation. The boot-time modes are indicated with a "*" in
43 * the "hdparm -i" listing, and can be changed with *knowledgeable* use of
44 * the "hdparm -X" feature. There is seldom a need to do this, as drives
45 * normally power-up with their "best" PIO/DMA modes enabled.
47 * Testing has been done with a rather extensive number of drives,
48 * with Quantum & Western Digital models generally outperforming the pack,
49 * and Fujitsu & Conner (and some Seagate which are really Conner) drives
50 * showing more lackluster throughput.
52 * Keep an eye on /var/adm/messages for "DMA disabled" messages.
54 * Some people have reported trouble with Intel Zappa motherboards.
55 * This can be fixed by upgrading the AMI BIOS to version 1.00.04.BS0,
56 * available from ftp://ftp.intel.com/pub/bios/10004bs0.exe
57 * (thanks to Glen Morrell <glen@spin.Stanford.edu> for researching this).
59 * Thanks to "Christopher J. Reimer" <reimer@doe.carleton.ca> for
60 * fixing the problem with the BIOS on some Acer motherboards.
62 * Thanks to "Benoit Poulot-Cazajous" <poulot@chorus.fr> for testing
63 * "TX" chipset compatibility and for providing patches for the "TX" chipset.
65 * Thanks to Christian Brunner <chb@muc.de> for taking a good first crack
66 * at generic DMA -- his patches were referred to when preparing this code.
68 * Most importantly, thanks to Robert Bringman <rob@mars.trion.com>
69 * for supplying a Promise UDMA board & WD UDMA drive for this work!
71 * And, yes, Intel Zappa boards really *do* use both PIIX IDE ports.
73 * ACARD ATP850UF Chipset "Modified SCSI Class" with other names
74 * AEC6210 U/UF
75 * SIIG's UltraIDE Pro CN-2449
76 * TTI HPT343 Chipset "Modified SCSI Class" but reports as an
77 * unknown storage device.
78 * NEW check_drive_lists(ide_drive_t *drive, int good_bad)
81 #include <linux/config.h>
82 #include <linux/types.h>
83 #include <linux/kernel.h>
84 #include <linux/timer.h>
85 #include <linux/mm.h>
86 #include <linux/interrupt.h>
87 #include <linux/pci.h>
88 #include <linux/init.h>
89 #include <linux/ide.h>
91 #include <asm/io.h>
92 #include <asm/irq.h>
94 #ifdef IDEDMA_NEW_DRIVE_LISTINGS
96 struct drive_list_entry {
97 char * id_model;
98 char * id_firmware;
101 struct drive_list_entry drive_whitelist [] = {
103 { "Micropolis 2112A" , "ALL" },
104 { "CONNER CTMA 4000" , "ALL" },
105 { "CONNER CTT8000-A" , "ALL" },
106 { "ST34342A" , "ALL" },
107 { 0 , 0 }
110 struct drive_list_entry drive_blacklist [] = {
112 { "WDC AC11000H" , "ALL" },
113 { "WDC AC22100H" , "ALL" },
114 { "WDC AC32500H" , "ALL" },
115 { "WDC AC33100H" , "ALL" },
116 { "WDC AC31600H" , "ALL" },
117 { "WDC AC32100H" , "24.09P07" },
118 { "WDC AC23200L" , "21.10N21" },
119 { 0 , 0 }
123 int in_drive_list(struct hd_driveid *id, struct drive_list_entry * drive_table)
125 for ( ; drive_table->id_model ; drive_table++)
126 if ((!strcmp(drive_table->id_model, id->model)) &&
127 ((!strstr(drive_table->id_firmware, id->fw_rev)) ||
128 (!strcmp(drive_table->id_firmware, "ALL"))))
129 return 1;
130 return 0;
133 #else /* !IDEDMA_NEW_DRIVE_LISTINGS */
136 * good_dma_drives() lists the model names (from "hdparm -i")
137 * of drives which do not support mode2 DMA but which are
138 * known to work fine with this interface under Linux.
140 const char *good_dma_drives[] = {"Micropolis 2112A",
141 "CONNER CTMA 4000",
142 "CONNER CTT8000-A",
143 "ST34342A", /* for Sun Ultra */
144 NULL};
147 * bad_dma_drives() lists the model names (from "hdparm -i")
148 * of drives which supposedly support (U)DMA but which are
149 * known to corrupt data with this interface under Linux.
151 * This is an empirical list. Its generated from bug reports. That means
152 * while it reflects actual problem distributions it doesn't answer whether
153 * the drive or the controller, or cabling, or software, or some combination
154 * thereof is the fault. If you don't happen to agree with the kernel's
155 * opinion of your drive - use hdparm to turn DMA on.
157 const char *bad_dma_drives[] = {"WDC AC11000H",
158 "WDC AC22100H",
159 "WDC AC32100H",
160 "WDC AC32500H",
161 "WDC AC33100H",
162 "WDC AC31600H",
163 NULL};
165 #endif /* IDEDMA_NEW_DRIVE_LISTINGS */
168 * Our Physical Region Descriptor (PRD) table should be large enough
169 * to handle the biggest I/O request we are likely to see. Since requests
170 * can have no more than 256 sectors, and since the typical blocksize is
171 * two or more sectors, we could get by with a limit of 128 entries here for
172 * the usual worst case. Most requests seem to include some contiguous blocks,
173 * further reducing the number of table entries required.
175 * The driver reverts to PIO mode for individual requests that exceed
176 * this limit (possible with 512 byte blocksizes, eg. MSDOS f/s), so handling
177 * 100% of all crazy scenarios here is not necessary.
179 * As it turns out though, we must allocate a full 4KB page for this,
180 * so the two PRD tables (ide0 & ide1) will each get half of that,
181 * allowing each to have about 256 entries (8 bytes each) from this.
183 #define PRD_BYTES 8
184 #define PRD_ENTRIES (PAGE_SIZE / (2 * PRD_BYTES))
187 * dma_intr() is the handler for disk read/write DMA interrupts
189 void ide_dma_intr (ide_drive_t *drive)
191 int i;
192 byte stat, dma_stat;
194 dma_stat = HWIF(drive)->dmaproc(ide_dma_end, drive);
195 stat = GET_STAT(); /* get drive status */
196 if (OK_STAT(stat,DRIVE_READY,drive->bad_wstat|DRQ_STAT)) {
197 if (!dma_stat) {
198 struct request *rq = HWGROUP(drive)->rq;
199 rq = HWGROUP(drive)->rq;
200 for (i = rq->nr_sectors; i > 0;) {
201 i -= rq->current_nr_sectors;
202 ide_end_request(1, HWGROUP(drive));
204 return;
206 printk("%s: dma_intr: bad DMA status\n", drive->name);
208 ide__sti(); /* local CPU only */
209 ide_error(drive, "dma_intr", stat);
213 * ide_build_dmatable() prepares a dma request.
214 * Returns 0 if all went okay, returns 1 otherwise.
215 * May also be invoked from trm290.c
217 int ide_build_dmatable (ide_drive_t *drive, ide_dma_action_t func)
219 struct request *rq = HWGROUP(drive)->rq;
220 struct buffer_head *bh = rq->bh;
221 unsigned int size, addr, *table = (unsigned int *)HWIF(drive)->dmatable;
222 unsigned char *virt_addr;
223 #ifdef CONFIG_BLK_DEV_TRM290
224 unsigned int is_trm290_chipset = (HWIF(drive)->chipset == ide_trm290);
225 #else
226 const int is_trm290_chipset = 0;
227 #endif
228 unsigned int count = 0;
230 do {
232 * Determine addr and size of next buffer area. We assume that
233 * individual virtual buffers are always composed linearly in
234 * physical memory. For example, we assume that any 8kB buffer
235 * is always composed of two adjacent physical 4kB pages rather
236 * than two possibly non-adjacent physical 4kB pages.
238 if (bh == NULL) { /* paging requests have (rq->bh == NULL) */
239 virt_addr = rq->buffer;
240 addr = virt_to_bus (virt_addr);
241 size = rq->nr_sectors << 9;
242 } else {
243 /* group sequential buffers into one large buffer */
244 virt_addr = bh->b_data;
245 addr = virt_to_bus (virt_addr);
246 size = bh->b_size;
247 while ((bh = bh->b_reqnext) != NULL) {
248 if ((addr + size) != virt_to_bus (bh->b_data))
249 break;
250 size += bh->b_size;
254 * Fill in the dma table, without crossing any 64kB boundaries.
255 * Most hardware requires 16-bit alignment of all blocks,
256 * but the trm290 requires 32-bit alignment.
258 if ((addr & 3)) {
259 printk("%s: misaligned DMA buffer\n", drive->name);
260 return 0;
264 * Some CPUs without cache snooping need to invalidate/write
265 * back their caches before DMA transfers to guarantee correct
266 * data. -- rmk
268 if (size) {
269 if (func == ide_dma_read) {
270 dma_cache_inv((unsigned int)virt_addr, size);
271 } else {
272 dma_cache_wback((unsigned int)virt_addr, size);
276 while (size) {
277 if (++count >= PRD_ENTRIES) {
278 printk("%s: DMA table too small\n", drive->name);
279 return 0; /* revert to PIO for this request */
280 } else {
281 unsigned int xcount, bcount = 0x10000 - (addr & 0xffff);
282 if (bcount > size)
283 bcount = size;
284 *table++ = cpu_to_le32(addr);
285 xcount = bcount & 0xffff;
286 if (is_trm290_chipset)
287 xcount = ((xcount >> 2) - 1) << 16;
288 *table++ = cpu_to_le32(xcount);
289 addr += bcount;
290 size -= bcount;
293 } while (bh != NULL);
294 if (!count) {
295 printk("%s: empty DMA table?\n", drive->name);
296 } else {
297 if (!is_trm290_chipset)
298 *--table |= cpu_to_le32(0x80000000); /* set End-Of-Table (EOT) bit */
300 * Some CPUs need to flush the DMA table to physical RAM
301 * before DMA can start. -- rmk
303 dma_cache_wback((unsigned long)HWIF(drive)->dmatable, count * sizeof(unsigned int) * 2);
305 return count;
309 * For both Blacklisted and Whitelisted drives.
310 * This is setup to be called as an extern for future support
311 * to other special driver code.
313 int check_drive_lists (ide_drive_t *drive, int good_bad)
315 struct hd_driveid *id = drive->id;
317 #ifdef IDEDMA_NEW_DRIVE_LISTINGS
318 if (good_bad) {
319 return in_drive_list(id, drive_whitelist);
320 } else {
321 int blacklist = in_drive_list(id, drive_blacklist);
322 if (blacklist)
323 printk("%s: Disabling (U)DMA for %s\n", drive->name, id->model);
324 return(blacklist);
326 #else /* !IDEDMA_NEW_DRIVE_LISTINGS */
327 const char **list;
329 if (good_bad) {
330 /* Consult the list of known "good" drives */
331 list = good_dma_drives;
332 while (*list) {
333 if (!strcmp(*list++,id->model))
334 return 1;
336 } else {
337 /* Consult the list of known "bad" drives */
338 list = bad_dma_drives;
339 while (*list) {
340 if (!strcmp(*list++,id->model)) {
341 printk("%s: Disabling (U)DMA for %s\n",
342 drive->name, id->model);
343 return 1;
347 #endif /* IDEDMA_NEW_DRIVE_LISTINGS */
348 return 0;
351 static int config_drive_for_dma (ide_drive_t *drive)
353 struct hd_driveid *id = drive->id;
354 ide_hwif_t *hwif = HWIF(drive);
356 if (id && (id->capability & 1) && hwif->autodma) {
357 /* Consult the list of known "bad" drives */
358 if (ide_dmaproc(ide_dma_bad_drive, drive))
359 return hwif->dmaproc(ide_dma_off, drive);
361 /* Enable DMA on any drive that has UltraDMA (mode 3/4) enabled */
362 if ((id->field_valid & 4) && (hwif->udma_four) && (id->word93 & 0x2000))
363 if ((id->dma_ultra & (id->dma_ultra >> 11) & 3))
364 return hwif->dmaproc(ide_dma_on, drive);
365 /* Enable DMA on any drive that has UltraDMA (mode 0/1/2) enabled */
366 if (id->field_valid & 4) /* UltraDMA */
367 if ((id->dma_ultra & (id->dma_ultra >> 8) & 7))
368 return hwif->dmaproc(ide_dma_on, drive);
369 /* Enable DMA on any drive that has mode2 DMA (multi or single) enabled */
370 if (id->field_valid & 2) /* regular DMA */
371 if ((id->dma_mword & 0x404) == 0x404 || (id->dma_1word & 0x404) == 0x404)
372 return hwif->dmaproc(ide_dma_on, drive);
373 /* Consult the list of known "good" drives */
374 if (ide_dmaproc(ide_dma_good_drive, drive))
375 return hwif->dmaproc(ide_dma_on, drive);
377 return hwif->dmaproc(ide_dma_off_quietly, drive);
381 * ide_dmaproc() initiates/aborts DMA read/write operations on a drive.
383 * The caller is assumed to have selected the drive and programmed the drive's
384 * sector address using CHS or LBA. All that remains is to prepare for DMA
385 * and then issue the actual read/write DMA/PIO command to the drive.
387 * For ATAPI devices, we just prepare for DMA and return. The caller should
388 * then issue the packet command to the drive and call us again with
389 * ide_dma_begin afterwards.
391 * Returns 0 if all went well.
392 * Returns 1 if DMA read/write could not be started, in which case
393 * the caller should revert to PIO for the current request.
394 * May also be invoked from trm290.c
396 int ide_dmaproc (ide_dma_action_t func, ide_drive_t *drive)
398 ide_hwif_t *hwif = HWIF(drive);
399 unsigned long dma_base = hwif->dma_base;
400 unsigned int count, reading = 0;
401 byte dma_stat;
403 switch (func) {
404 case ide_dma_off:
405 printk("%s: DMA disabled\n", drive->name);
406 case ide_dma_off_quietly:
407 case ide_dma_on:
408 drive->using_dma = (func == ide_dma_on);
409 return 0;
410 case ide_dma_check:
411 return config_drive_for_dma (drive);
412 case ide_dma_read:
413 reading = 1 << 3;
414 case ide_dma_write:
415 if (!(count = ide_build_dmatable(drive, func)))
416 return 1; /* try PIO instead of DMA */
417 outl(virt_to_bus(hwif->dmatable), dma_base + 4); /* PRD table */
418 outb(reading, dma_base); /* specify r/w */
419 outb(inb(dma_base+2)|6, dma_base+2); /* clear INTR & ERROR flags */
420 drive->waiting_for_dma = 1;
421 if (drive->media != ide_disk)
422 return 0;
423 ide_set_handler(drive, &ide_dma_intr, WAIT_CMD);/* issue cmd to drive */
424 OUT_BYTE(reading ? WIN_READDMA : WIN_WRITEDMA, IDE_COMMAND_REG);
425 case ide_dma_begin:
426 /* Note that this is done *after* the cmd has
427 * been issued to the drive, as per the BM-IDE spec.
428 * The Promise Ultra33 doesn't work correctly when
429 * we do this part before issuing the drive cmd.
431 outb(inb(dma_base)|1, dma_base); /* start DMA */
432 return 0;
433 case ide_dma_end: /* returns 1 on error, 0 otherwise */
434 drive->waiting_for_dma = 0;
435 outb(inb(dma_base)&~1, dma_base); /* stop DMA */
436 dma_stat = inb(dma_base+2); /* get DMA status */
437 outb(dma_stat|6, dma_base+2); /* clear the INTR & ERROR bits */
438 return (dma_stat & 7) != 4; /* verify good DMA status */
439 case ide_dma_test_irq: /* returns 1 if dma irq issued, 0 otherwise */
440 dma_stat = inb(dma_base+2);
441 return (dma_stat & 4) == 4; /* return 1 if INTR asserted */
442 case ide_dma_bad_drive:
443 case ide_dma_good_drive:
444 return check_drive_lists(drive, (func == ide_dma_good_drive));
445 case ide_dma_lostirq:
446 case ide_dma_timeout:
448 * printk("ide_dmaproc: chipset supported func only: %d\n", func);
450 return 1;
451 default:
452 printk("ide_dmaproc: unsupported func: %d\n", func);
453 return 1;
458 * Needed for allowing full modular support of ide-driver
460 int ide_release_dma (ide_hwif_t *hwif)
462 if (hwif->dmatable) {
463 clear_page((unsigned long)hwif->dmatable); /* clear PRD 1st */
464 free_page((unsigned long)hwif->dmatable); /* free PRD 2nd */
466 if ((hwif->dma_extra) && (hwif->channel == 0))
467 release_region((hwif->dma_base + 16), hwif->dma_extra);
468 release_region(hwif->dma_base, 8);
469 return 1;
473 * This can be called for a dynamically installed interface. Don't initfunc it
476 void ide_setup_dma (ide_hwif_t *hwif, unsigned long dma_base, unsigned int num_ports)
478 static unsigned long dmatable = 0;
479 static unsigned leftover = 0;
481 printk(" %s: BM-DMA at 0x%04lx-0x%04lx", hwif->name, dma_base, dma_base + num_ports - 1);
482 if (check_region(dma_base, num_ports)) {
483 printk(" -- ERROR, PORT ADDRESSES ALREADY IN USE\n");
484 return;
486 request_region(dma_base, num_ports, hwif->name);
487 hwif->dma_base = dma_base;
488 if (leftover < (PRD_ENTRIES * PRD_BYTES)) {
490 * The BM-DMA uses full 32bit addr, so we can
491 * safely use __get_free_page() here instead
492 * of __get_dma_pages() -- no ISA limitations.
494 dmatable = __get_free_pages(GFP_KERNEL,1);
495 leftover = dmatable ? PAGE_SIZE : 0;
497 if (!dmatable) {
498 printk(" -- ERROR, UNABLE TO ALLOCATE PRD TABLE\n");
499 } else {
500 hwif->dmatable = (unsigned long *) dmatable;
501 dmatable += (PRD_ENTRIES * PRD_BYTES);
502 leftover -= (PRD_ENTRIES * PRD_BYTES);
503 hwif->dmaproc = &ide_dmaproc;
505 if (hwif->chipset != ide_trm290) {
506 byte dma_stat = inb(dma_base+2);
507 printk(", BIOS settings: %s:%s, %s:%s",
508 hwif->drives[0].name, (dma_stat & 0x20) ? "DMA" : "pio",
509 hwif->drives[1].name, (dma_stat & 0x40) ? "DMA" : "pio");
511 printk("\n");
516 * Fetch the DMA Bus-Master-I/O-Base-Address (BMIBA) from PCI space:
518 unsigned long __init ide_get_or_set_dma_base (ide_hwif_t *hwif, int extra, const char *name)
520 unsigned long dma_base = 0;
521 struct pci_dev *dev = hwif->pci_dev;
523 if (hwif->mate && hwif->mate->dma_base) {
524 dma_base = hwif->mate->dma_base - (hwif->channel ? 0 : 8);
525 } else {
526 dma_base = dev->resource[4].start;
527 if (!dma_base || dma_base == PCI_BASE_ADDRESS_IO_MASK) {
528 printk("%s: dma_base is invalid (0x%04lx)\n", name, dma_base);
529 dma_base = 0;
532 if (dma_base) {
533 if (extra) /* PDC20246, PDC20262, & HPT343 */
534 request_region(dma_base+16, extra, name);
535 dma_base += hwif->channel ? 8 : 0;
536 hwif->dma_extra = extra;
538 switch(dev->device) {
539 case PCI_DEVICE_ID_CMD_643:
540 #ifdef CONFIG_BLK_DEV_ALI15X3
541 case PCI_DEVICE_ID_AL_M5219:
542 case PCI_DEVICE_ID_AL_M5229:
544 * Ali 15x3 chipsets know as ALI IV and V report
545 * this as simplex, skip this test for them.
547 #endif /* CONFIG_BLK_DEV_ALI15X3 */
548 outb(inb(dma_base+2) & 0x60, dma_base+2);
549 if (inb(dma_base+2) & 0x80) {
550 printk("%s: simplex device: DMA forced\n", name);
552 break;
553 default:
555 * If the device claims "simplex" DMA,
556 * this means only one of the two interfaces
557 * can be trusted with DMA at any point in time.
558 * So we should enable DMA only on one of the
559 * two interfaces.
561 if ((inb(dma_base+2) & 0x80)) { /* simplex device? */
562 if ((!hwif->drives[0].present && !hwif->drives[1].present) ||
563 (hwif->mate && hwif->mate->dma_base)) {
564 printk("%s: simplex device: DMA disabled\n", name);
565 dma_base = 0;
570 return dma_base;