Import 2.3.52pre1
[davej-history.git] / drivers / ide / ide-dma.c
blob75142483181ff20b1e6dc3e15cf9ae2557d3c537
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 extern char *ide_dmafunc_verbose(ide_dma_action_t dmafunc);
96 #ifdef CONFIG_IDEDMA_NEW_DRIVE_LISTINGS
98 struct drive_list_entry {
99 char * id_model;
100 char * id_firmware;
103 struct drive_list_entry drive_whitelist [] = {
105 { "Micropolis 2112A" , "ALL" },
106 { "CONNER CTMA 4000" , "ALL" },
107 { "CONNER CTT8000-A" , "ALL" },
108 { "ST34342A" , "ALL" },
109 { 0 , 0 }
112 struct drive_list_entry drive_blacklist [] = {
114 { "WDC AC11000H" , "ALL" },
115 { "WDC AC22100H" , "ALL" },
116 { "WDC AC32500H" , "ALL" },
117 { "WDC AC33100H" , "ALL" },
118 { "WDC AC31600H" , "ALL" },
119 { "WDC AC32100H" , "24.09P07" },
120 { "WDC AC23200L" , "21.10N21" },
121 { 0 , 0 }
125 int in_drive_list(struct hd_driveid *id, struct drive_list_entry * drive_table)
127 for ( ; drive_table->id_model ; drive_table++)
128 if ((!strcmp(drive_table->id_model, id->model)) &&
129 ((!strstr(drive_table->id_firmware, id->fw_rev)) ||
130 (!strcmp(drive_table->id_firmware, "ALL"))))
131 return 1;
132 return 0;
135 #else /* !CONFIG_IDEDMA_NEW_DRIVE_LISTINGS */
138 * good_dma_drives() lists the model names (from "hdparm -i")
139 * of drives which do not support mode2 DMA but which are
140 * known to work fine with this interface under Linux.
142 const char *good_dma_drives[] = {"Micropolis 2112A",
143 "CONNER CTMA 4000",
144 "CONNER CTT8000-A",
145 "ST34342A", /* for Sun Ultra */
146 NULL};
149 * bad_dma_drives() lists the model names (from "hdparm -i")
150 * of drives which supposedly support (U)DMA but which are
151 * known to corrupt data with this interface under Linux.
153 * This is an empirical list. Its generated from bug reports. That means
154 * while it reflects actual problem distributions it doesn't answer whether
155 * the drive or the controller, or cabling, or software, or some combination
156 * thereof is the fault. If you don't happen to agree with the kernel's
157 * opinion of your drive - use hdparm to turn DMA on.
159 const char *bad_dma_drives[] = {"WDC AC11000H",
160 "WDC AC22100H",
161 "WDC AC32100H",
162 "WDC AC32500H",
163 "WDC AC33100H",
164 "WDC AC31600H",
165 NULL};
167 #endif /* CONFIG_IDEDMA_NEW_DRIVE_LISTINGS */
170 * Our Physical Region Descriptor (PRD) table should be large enough
171 * to handle the biggest I/O request we are likely to see. Since requests
172 * can have no more than 256 sectors, and since the typical blocksize is
173 * two or more sectors, we could get by with a limit of 128 entries here for
174 * the usual worst case. Most requests seem to include some contiguous blocks,
175 * further reducing the number of table entries required.
177 * The driver reverts to PIO mode for individual requests that exceed
178 * this limit (possible with 512 byte blocksizes, eg. MSDOS f/s), so handling
179 * 100% of all crazy scenarios here is not necessary.
181 * As it turns out though, we must allocate a full 4KB page for this,
182 * so the two PRD tables (ide0 & ide1) will each get half of that,
183 * allowing each to have about 256 entries (8 bytes each) from this.
185 #define PRD_BYTES 8
186 #define PRD_ENTRIES (PAGE_SIZE / (2 * PRD_BYTES))
189 * dma_intr() is the handler for disk read/write DMA interrupts
191 ide_startstop_t ide_dma_intr (ide_drive_t *drive)
193 int i;
194 byte stat, dma_stat;
196 dma_stat = HWIF(drive)->dmaproc(ide_dma_end, drive);
197 stat = GET_STAT(); /* get drive status */
198 if (OK_STAT(stat,DRIVE_READY,drive->bad_wstat|DRQ_STAT)) {
199 if (!dma_stat) {
200 struct request *rq = HWGROUP(drive)->rq;
201 rq = HWGROUP(drive)->rq;
202 for (i = rq->nr_sectors; i > 0;) {
203 i -= rq->current_nr_sectors;
204 ide_end_request(1, HWGROUP(drive));
206 return ide_stopped;
208 printk("%s: dma_intr: bad DMA status\n", drive->name);
210 return ide_error(drive, "dma_intr", stat);
213 static int ide_build_sglist (ide_hwif_t *hwif, struct request *rq)
215 struct buffer_head *bh;
216 struct scatterlist *sg = hwif->sg_table;
217 int nents = 0;
219 if (rq->cmd == READ)
220 hwif->sg_dma_direction = PCI_DMA_FROMDEVICE;
221 else
222 hwif->sg_dma_direction = PCI_DMA_TODEVICE;
223 bh = rq->bh;
224 do {
225 unsigned char *virt_addr = bh->b_data;
226 unsigned int size = bh->b_size;
228 while ((bh = bh->b_reqnext) != NULL) {
229 if ((virt_addr + size) != (unsigned char *) bh->b_data)
230 break;
231 size += bh->b_size;
233 memset(&sg[nents], 0, sizeof(*sg));
234 sg[nents].address = virt_addr;
235 sg[nents].length = size;
236 nents++;
237 } while (bh != NULL);
239 return pci_map_sg(hwif->pci_dev, sg, nents, hwif->sg_dma_direction);
243 * ide_build_dmatable() prepares a dma request.
244 * Returns 0 if all went okay, returns 1 otherwise.
245 * May also be invoked from trm290.c
247 int ide_build_dmatable (ide_drive_t *drive, ide_dma_action_t func)
249 unsigned int *table = HWIF(drive)->dmatable_cpu;
250 #ifdef CONFIG_BLK_DEV_TRM290
251 unsigned int is_trm290_chipset = (HWIF(drive)->chipset == ide_trm290);
252 #else
253 const int is_trm290_chipset = 0;
254 #endif
255 unsigned int count = 0;
256 int i;
257 struct scatterlist *sg;
259 HWIF(drive)->sg_nents = i = ide_build_sglist(HWIF(drive), HWGROUP(drive)->rq);
261 sg = HWIF(drive)->sg_table;
262 while (i && sg_dma_len(sg)) {
263 u32 cur_addr;
264 u32 cur_len;
266 cur_addr = sg_dma_address(sg);
267 cur_len = sg_dma_len(sg);
269 while (cur_len) {
270 if (++count >= PRD_ENTRIES) {
271 printk("%s: DMA table too small\n", drive->name);
272 pci_unmap_sg(HWIF(drive)->pci_dev,
273 HWIF(drive)->sg_table,
274 HWIF(drive)->sg_nents,
275 HWIF(drive)->sg_dma_direction);
276 return 0; /* revert to PIO for this request */
277 } else {
278 u32 xcount, bcount = 0x10000 - (cur_addr & 0xffff);
280 if (bcount > cur_len)
281 bcount = cur_len;
282 *table++ = cpu_to_le32(cur_addr);
283 xcount = bcount & 0xffff;
284 if (is_trm290_chipset)
285 xcount = ((xcount >> 2) - 1) << 16;
286 *table++ = cpu_to_le32(xcount);
287 cur_addr += bcount;
288 cur_len -= bcount;
292 sg++;
293 i--;
296 if (!count)
297 printk("%s: empty DMA table?\n", drive->name);
298 else if (!is_trm290_chipset)
299 *--table |= cpu_to_le32(0x80000000);
301 return count;
304 /* Teardown mappings after DMA has completed. */
305 void ide_destroy_dmatable (ide_drive_t *drive)
307 struct pci_dev *dev = HWIF(drive)->pci_dev;
308 struct scatterlist *sg = HWIF(drive)->sg_table;
309 int nents = HWIF(drive)->sg_nents;
311 pci_unmap_sg(dev, sg, nents, HWIF(drive)->sg_dma_direction);
315 * For both Blacklisted and Whitelisted drives.
316 * This is setup to be called as an extern for future support
317 * to other special driver code.
319 int check_drive_lists (ide_drive_t *drive, int good_bad)
321 struct hd_driveid *id = drive->id;
323 #ifdef CONFIG_IDEDMA_NEW_DRIVE_LISTINGS
324 if (good_bad) {
325 return in_drive_list(id, drive_whitelist);
326 } else {
327 int blacklist = in_drive_list(id, drive_blacklist);
328 if (blacklist)
329 printk("%s: Disabling (U)DMA for %s\n", drive->name, id->model);
330 return(blacklist);
332 #else /* !CONFIG_IDEDMA_NEW_DRIVE_LISTINGS */
333 const char **list;
335 if (good_bad) {
336 /* Consult the list of known "good" drives */
337 list = good_dma_drives;
338 while (*list) {
339 if (!strcmp(*list++,id->model))
340 return 1;
342 } else {
343 /* Consult the list of known "bad" drives */
344 list = bad_dma_drives;
345 while (*list) {
346 if (!strcmp(*list++,id->model)) {
347 printk("%s: Disabling (U)DMA for %s\n",
348 drive->name, id->model);
349 return 1;
353 #endif /* CONFIG_IDEDMA_NEW_DRIVE_LISTINGS */
354 return 0;
357 static int config_drive_for_dma (ide_drive_t *drive)
359 struct hd_driveid *id = drive->id;
360 ide_hwif_t *hwif = HWIF(drive);
362 if (id && (id->capability & 1) && hwif->autodma) {
363 /* Consult the list of known "bad" drives */
364 if (ide_dmaproc(ide_dma_bad_drive, drive))
365 return hwif->dmaproc(ide_dma_off, drive);
367 /* Enable DMA on any drive that has UltraDMA (mode 3/4) enabled */
368 if ((id->field_valid & 4) && (hwif->udma_four) && (id->hw_config & 0x2000))
369 if ((id->dma_ultra & (id->dma_ultra >> 11) & 3))
370 return hwif->dmaproc(ide_dma_on, drive);
371 /* Enable DMA on any drive that has UltraDMA (mode 0/1/2) enabled */
372 if (id->field_valid & 4) /* UltraDMA */
373 if ((id->dma_ultra & (id->dma_ultra >> 8) & 7))
374 return hwif->dmaproc(ide_dma_on, drive);
375 /* Enable DMA on any drive that has mode2 DMA (multi or single) enabled */
376 if (id->field_valid & 2) /* regular DMA */
377 if ((id->dma_mword & 0x404) == 0x404 || (id->dma_1word & 0x404) == 0x404)
378 return hwif->dmaproc(ide_dma_on, drive);
379 /* Consult the list of known "good" drives */
380 if (ide_dmaproc(ide_dma_good_drive, drive))
381 return hwif->dmaproc(ide_dma_on, drive);
383 return hwif->dmaproc(ide_dma_off_quietly, drive);
387 * ide_dmaproc() initiates/aborts DMA read/write operations on a drive.
389 * The caller is assumed to have selected the drive and programmed the drive's
390 * sector address using CHS or LBA. All that remains is to prepare for DMA
391 * and then issue the actual read/write DMA/PIO command to the drive.
393 * For ATAPI devices, we just prepare for DMA and return. The caller should
394 * then issue the packet command to the drive and call us again with
395 * ide_dma_begin afterwards.
397 * Returns 0 if all went well.
398 * Returns 1 if DMA read/write could not be started, in which case
399 * the caller should revert to PIO for the current request.
400 * May also be invoked from trm290.c
402 int ide_dmaproc (ide_dma_action_t func, ide_drive_t *drive)
404 ide_hwif_t *hwif = HWIF(drive);
405 unsigned long dma_base = hwif->dma_base;
406 byte unit = (drive->select.b.unit & 0x01);
407 unsigned int count, reading = 0;
408 byte dma_stat;
410 switch (func) {
411 case ide_dma_off:
412 printk("%s: DMA disabled\n", drive->name);
413 case ide_dma_off_quietly:
414 outb(inb(dma_base+2) & ~(1<<(5+unit)), dma_base+2);
415 case ide_dma_on:
416 drive->using_dma = (func == ide_dma_on);
417 if (drive->using_dma)
418 outb(inb(dma_base+2)|(1<<(5+unit)), dma_base+2);
419 return 0;
420 case ide_dma_check:
421 return config_drive_for_dma (drive);
422 case ide_dma_read:
423 reading = 1 << 3;
424 case ide_dma_write:
425 if (!(count = ide_build_dmatable(drive, func)))
426 return 1; /* try PIO instead of DMA */
427 outl(hwif->dmatable_dma, dma_base + 4); /* PRD table */
428 outb(reading, dma_base); /* specify r/w */
429 outb(inb(dma_base+2)|6, dma_base+2); /* clear INTR & ERROR flags */
430 drive->waiting_for_dma = 1;
431 if (drive->media != ide_disk)
432 return 0;
433 ide_set_handler(drive, &ide_dma_intr, WAIT_CMD, NULL); /* issue cmd to drive */
434 OUT_BYTE(reading ? WIN_READDMA : WIN_WRITEDMA, IDE_COMMAND_REG);
435 case ide_dma_begin:
436 /* Note that this is done *after* the cmd has
437 * been issued to the drive, as per the BM-IDE spec.
438 * The Promise Ultra33 doesn't work correctly when
439 * we do this part before issuing the drive cmd.
441 outb(inb(dma_base)|1, dma_base); /* start DMA */
442 return 0;
443 case ide_dma_end: /* returns 1 on error, 0 otherwise */
444 drive->waiting_for_dma = 0;
445 outb(inb(dma_base)&~1, dma_base); /* stop DMA */
446 dma_stat = inb(dma_base+2); /* get DMA status */
447 outb(dma_stat|6, dma_base+2); /* clear the INTR & ERROR bits */
448 ide_destroy_dmatable(drive); /* purge DMA mappings */
449 return (dma_stat & 7) != 4; /* verify good DMA status */
450 case ide_dma_test_irq: /* returns 1 if dma irq issued, 0 otherwise */
451 dma_stat = inb(dma_base+2);
452 return (dma_stat & 4) == 4; /* return 1 if INTR asserted */
453 case ide_dma_bad_drive:
454 case ide_dma_good_drive:
455 return check_drive_lists(drive, (func == ide_dma_good_drive));
456 case ide_dma_lostirq:
457 case ide_dma_timeout:
458 printk("ide_dmaproc: chipset supported %s func only: %d\n", ide_dmafunc_verbose(func), func);
459 return 1;
460 default:
461 printk("ide_dmaproc: unsupported %s func: %d\n", ide_dmafunc_verbose(func), func);
462 return 1;
467 * Needed for allowing full modular support of ide-driver
469 int ide_release_dma (ide_hwif_t *hwif)
471 if (hwif->dmatable_cpu) {
472 pci_free_consistent(hwif->pci_dev,
473 PRD_ENTRIES * PRD_BYTES,
474 hwif->dmatable_cpu,
475 hwif->dmatable_dma);
476 hwif->dmatable_cpu = NULL;
478 if (hwif->sg_table) {
479 kfree(hwif->sg_table);
480 hwif->sg_table = NULL;
482 if ((hwif->dma_extra) && (hwif->channel == 0))
483 release_region((hwif->dma_base + 16), hwif->dma_extra);
484 release_region(hwif->dma_base, 8);
485 return 1;
489 * This can be called for a dynamically installed interface. Don't __init it
492 void ide_setup_dma (ide_hwif_t *hwif, unsigned long dma_base, unsigned int num_ports)
494 printk(" %s: BM-DMA at 0x%04lx-0x%04lx", hwif->name, dma_base, dma_base + num_ports - 1);
495 if (check_region(dma_base, num_ports)) {
496 printk(" -- ERROR, PORT ADDRESSES ALREADY IN USE\n");
497 return;
499 request_region(dma_base, num_ports, hwif->name);
500 hwif->dma_base = dma_base;
501 hwif->dmatable_cpu = pci_alloc_consistent(hwif->pci_dev,
502 PRD_ENTRIES * PRD_BYTES,
503 &hwif->dmatable_dma);
504 if (hwif->dmatable_cpu == NULL)
505 goto dma_alloc_failure;
507 hwif->sg_table = kmalloc(sizeof(struct scatterlist) * PRD_ENTRIES,
508 GFP_KERNEL);
509 if (hwif->sg_table == NULL) {
510 pci_free_consistent(hwif->pci_dev, PRD_ENTRIES * PRD_BYTES,
511 hwif->dmatable_cpu, hwif->dmatable_dma);
512 goto dma_alloc_failure;
515 hwif->dmaproc = &ide_dmaproc;
517 if (hwif->chipset != ide_trm290) {
518 byte dma_stat = inb(dma_base+2);
519 printk(", BIOS settings: %s:%s, %s:%s",
520 hwif->drives[0].name, (dma_stat & 0x20) ? "DMA" : "pio",
521 hwif->drives[1].name, (dma_stat & 0x40) ? "DMA" : "pio");
523 printk("\n");
524 return;
526 dma_alloc_failure:
527 printk(" -- ERROR, UNABLE TO ALLOCATE DMA TABLES\n");
531 * Fetch the DMA Bus-Master-I/O-Base-Address (BMIBA) from PCI space:
533 unsigned long __init ide_get_or_set_dma_base (ide_hwif_t *hwif, int extra, const char *name)
535 unsigned long dma_base = 0;
536 struct pci_dev *dev = hwif->pci_dev;
538 if (hwif->mate && hwif->mate->dma_base) {
539 dma_base = hwif->mate->dma_base - (hwif->channel ? 0 : 8);
540 } else {
541 dma_base = dev->resource[4].start;
542 if (!dma_base || dma_base == PCI_BASE_ADDRESS_IO_MASK) {
543 printk("%s: dma_base is invalid (0x%04lx)\n", name, dma_base);
544 dma_base = 0;
547 if (dma_base) {
548 if (extra) /* PDC20246, PDC20262, HPT343, & HPT366 */
549 request_region(dma_base+16, extra, name);
550 dma_base += hwif->channel ? 8 : 0;
551 hwif->dma_extra = extra;
553 switch(dev->device) {
554 case PCI_DEVICE_ID_AL_M5219:
555 case PCI_DEVICE_ID_AMD_VIPER_7409:
556 case PCI_DEVICE_ID_CMD_643:
557 outb(inb(dma_base+2) & 0x60, dma_base+2);
558 if (inb(dma_base+2) & 0x80) {
559 printk("%s: simplex device: DMA forced\n", name);
561 break;
562 default:
564 * If the device claims "simplex" DMA,
565 * this means only one of the two interfaces
566 * can be trusted with DMA at any point in time.
567 * So we should enable DMA only on one of the
568 * two interfaces.
570 if ((inb(dma_base+2) & 0x80)) { /* simplex device? */
571 if ((!hwif->drives[0].present && !hwif->drives[1].present) ||
572 (hwif->mate && hwif->mate->dma_base)) {
573 printk("%s: simplex device: DMA disabled\n", name);
574 dma_base = 0;
579 return dma_base;