root/drivers/block/triton.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dma_intr
  2. build_dmatable
  3. config_drive_for_dma
  4. triton_dmaproc
  5. print_triton_drive_flags
  6. init_triton_dma
  7. calc_mode
  8. ide_init_triton

   1 /*
   2  *  linux/drivers/block/triton.c        Version 1.06  Feb 6, 1996
   3  *
   4  *  Copyright (c) 1995-1996  Mark Lord
   5  *  May be copied or modified under the terms of the GNU General Public License
   6  */
   7 
   8 /*
   9  * This module provides support for the Bus Master IDE DMA function
  10  * of the Intel PCI Triton chipset (82371FB).
  11  *
  12  * DMA is currently supported only for hard disk drives (not cdroms).
  13  *
  14  * Support for cdroms will likely be added at a later date,
  15  * after broader experience has been obtained with hard disks.
  16  *
  17  * Up to four drives may be enabled for DMA, and the Triton chipset will
  18  * (hopefully) arbitrate the PCI bus among them.  Note that the 82371FB chip
  19  * provides a single "line buffer" for the BM IDE function, so performance of
  20  * multiple (two) drives doing DMA simultaneously will suffer somewhat,
  21  * as they contest for that resource bottleneck.  This is handled transparently
  22  * inside the 82371FB chip.
  23  *
  24  * By default, DMA support is prepared for use, but is currently enabled only
  25  * for drives which support multi-word DMA mode2 (mword2), or which are
  26  * recognized as "good" (see table below).  Drives with only mode0 or mode1
  27  * (single or multi) DMA should also work with this chipset/driver (eg. MC2112A)
  28  * but are not enabled by default.  Use "hdparm -i" to view modes supported
  29  * by a given drive.
  30  *
  31  * The hdparm-2.4 (or later) utility can be used for manually enabling/disabling
  32  * DMA support, but must be (re-)compiled against this kernel version or later.
  33  *
  34  * To enable DMA, use "hdparm -d1 /dev/hd?" on a per-drive basis after booting.
  35  * If problems arise, ide.c will disable DMA operation after a few retries.
  36  * This error recovery mechanism works and has been extremely well exercised.
  37  *
  38  * IDE drives, depending on their vintage, may support several different modes
  39  * of DMA operation.  The boot-time modes are indicated with a "*" in
  40  * the "hdparm -i" listing, and can be changed with *knowledgeable* use of
  41  * the "hdparm -X" feature.  There is seldom a need to do this, as drives
  42  * normally power-up with their "best" PIO/DMA modes enabled.
  43  *
  44  * Testing was done with an ASUS P55TP4XE/100 system and the following drives:
  45  *
  46  *   Quantum Fireball 1080A (1Gig w/83kB buffer), DMA mode2, PIO mode4.
  47  *      - DMA mode2 works well (7.4MB/sec), despite the tiny on-drive buffer.
  48  *      - This drive also does PIO mode4, at about the same speed as DMA mode2.
  49  *        An awesome drive for the price!
  50  *
  51  *   Fujitsu M1606TA (1Gig w/256kB buffer), DMA mode2, PIO mode4.
  52  *      - DMA mode2 gives horrible performance (1.6MB/sec), despite the good
  53  *        size of the on-drive buffer and a boasted 10ms average access time.
  54  *      - PIO mode4 was better, but peaked at a mere 4.5MB/sec.
  55  *
  56  *   Micropolis MC2112A (1Gig w/508kB buffer), drive pre-dates EIDE and ATA2.
  57  *      - DMA works fine (2.2MB/sec), probably due to the large on-drive buffer.
  58  *      - This older drive can also be tweaked for fastPIO (3.7MB/sec) by using
  59  *        maximum clock settings (5,4) and setting all flags except prefetch.
  60  *
  61  *   Western Digital AC31000H (1Gig w/128kB buffer), DMA mode1, PIO mode3.
  62  *      - DMA does not work reliably.  The drive appears to be somewhat tardy
  63  *        in deasserting DMARQ at the end of a sector.  This is evident in
  64  *        the observation that WRITEs work most of the time, depending on
  65  *        cache-buffer occupancy, but multi-sector reads seldom work.
  66  *
  67  * Testing was done with a Gigabyte GA-586 ATE system and the following drive:
  68  * (Uwe Bonnes - bon@elektron.ikp.physik.th-darmstadt.de)
  69  *
  70  *   Western Digital AC31600H (1.6Gig w/128kB buffer), DMA mode2, PIO mode4.
  71  *      - much better than its 1Gig cousin, this drive is reported to work
  72  *        very well with DMA (7.3MB/sec).
  73  *
  74  * Other drives:
  75  *
  76  *   Maxtor 7540AV (515Meg w/32kB buffer), DMA modes mword0/sword2, PIO mode3.
  77  *      - a budget drive, with budget performance, around 3MB/sec.
  78  *
  79  *   Western Digital AC2850F (814Meg w/64kB buffer), DMA mode1, PIO mode3.
  80  *      - another "caviar" drive, similar to the AC31000, except that this one
  81  *        worked with DMA in at least one system.  Throughput is about 3.8MB/sec
  82  *        for both DMA and PIO.
  83  *
  84  *   Conner CFS850A (812Meg w/64kB buffer), DMA mode2, PIO mode4.
  85  *      - like most Conner models, this drive proves that even a fast interface
  86  *        cannot improve slow media.  Both DMA and PIO peak around 3.5MB/sec.
  87  *
  88  *   Maxtor 71260AT (1204Meg w/256kB buffer), DMA mword0/sword2, PIO mode3.
  89  *      - works with DMA, giving 3-4MB/sec performance, about the same as mode3.
  90  *
  91  * If you have any drive models to add, email your results to:  mlord@pobox.com
  92  * Keep an eye on /var/adm/messages for "DMA disabled" messages.
  93  *
  94  * Some people have reported trouble with Intel Zappa motherboards.
  95  * This can be fixed by upgrading the AMI BIOS to version 1.00.04.BS0,
  96  * available from ftp://ftp.intel.com/pub/bios/10004bs0.exe
  97  * (thanks to Glen Morrell <glen@spin.Stanford.edu> for researching this).
  98  *
  99  * And, yes, Intel Zappa boards really *do* use the Triton IDE ports.
 100  */
 101 #include <linux/config.h>
 102 #include <linux/types.h>
 103 #include <linux/kernel.h>
 104 #include <linux/timer.h>
 105 #include <linux/mm.h>
 106 #include <linux/ioport.h>
 107 #include <linux/interrupt.h>
 108 #include <linux/blkdev.h>
 109 #include <linux/hdreg.h>
 110 #include <linux/pci.h>
 111 #include <linux/bios32.h>
 112 
 113 #include <asm/io.h>
 114 #include <asm/dma.h>
 115 
 116 #include "ide.h"
 117 
 118 /*
 119  * good_dma_drives() lists the model names (from "hdparm -i")
 120  * of drives which do not support mword2 DMA but which are
 121  * known to work fine with this interface under Linux.
 122  */
 123 const char *good_dma_drives[] = {"Micropolis 2112A",
 124                                  "Maxtor 71260 AT",
 125                                  "CONNER CTMA 4000"};
 126 
 127 /*
 128  * Our Physical Region Descriptor (PRD) table should be large enough
 129  * to handle the biggest I/O request we are likely to see.  Since requests
 130  * can have no more than 256 sectors, and since the typical blocksize is
 131  * two sectors, we could get by with a limit of 128 entries here for the
 132  * usual worst case.  Most requests seem to include some contiguous blocks,
 133  * further reducing the number of table entries required.
 134  *
 135  * The driver reverts to PIO mode for individual requests that exceed
 136  * this limit (possible with 512 byte blocksizes, eg. MSDOS f/s), so handling
 137  * 100% of all crazy scenarios here is not necessary.
 138  *
 139  * As it turns out though, we must allocate a full 4KB page for this,
 140  * so the two PRD tables (ide0 & ide1) will each get half of that,
 141  * allowing each to have about 256 entries (8 bytes each) from this.
 142  */
 143 #define PRD_BYTES       8
 144 #define PRD_ENTRIES     (PAGE_SIZE / (2 * PRD_BYTES))
 145 
 146 /*
 147  * dma_intr() is the handler for disk read/write DMA interrupts
 148  */
 149 static void dma_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151         byte stat, dma_stat;
 152         int i;
 153         struct request *rq = HWGROUP(drive)->rq;
 154         unsigned short dma_base = HWIF(drive)->dma_base;
 155 
 156         dma_stat = inb(dma_base+2);             /* get DMA status */
 157         outb(inb(dma_base)&~1, dma_base);       /* stop DMA operation */
 158         stat = GET_STAT();                      /* get drive status */
 159         if (OK_STAT(stat,DRIVE_READY,drive->bad_wstat|DRQ_STAT)) {
 160                 if ((dma_stat & 7) == 4) {      /* verify good DMA status */
 161                         rq = HWGROUP(drive)->rq;
 162                         for (i = rq->nr_sectors; i > 0;) {
 163                                 i -= rq->current_nr_sectors;
 164                                 ide_end_request(1, HWGROUP(drive));
 165                         }
 166                         return;
 167                 }
 168                 printk("%s: bad DMA status: 0x%02x\n", drive->name, dma_stat);
 169         }
 170         sti();
 171         ide_error(drive, "dma_intr", stat);
 172 }
 173 
 174 /*
 175  * build_dmatable() prepares a dma request.
 176  * Returns 0 if all went okay, returns 1 otherwise.
 177  */
 178 static int build_dmatable (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 179 {
 180         struct request *rq = HWGROUP(drive)->rq;
 181         struct buffer_head *bh = rq->bh;
 182         unsigned long size, addr, *table = HWIF(drive)->dmatable;
 183         unsigned int count = 0;
 184 
 185         do {
 186                 /*
 187                  * Determine addr and size of next buffer area.  We assume that
 188                  * individual virtual buffers are always composed linearly in
 189                  * physical memory.  For example, we assume that any 8kB buffer
 190                  * is always composed of two adjacent physical 4kB pages rather
 191                  * than two possibly non-adjacent physical 4kB pages.
 192                  */
 193                 if (bh == NULL) {  /* paging and tape requests have (rq->bh == NULL) */
 194                         addr = virt_to_bus (rq->buffer);
 195 #ifdef CONFIG_BLK_DEV_IDETAPE
 196                         if (drive->media == ide_tape)
 197                                 size = drive->tape.pc->request_transfer;
 198                         else
 199 #endif /* CONFIG_BLK_DEV_IDETAPE */     
 200                         size = rq->nr_sectors << 9;
 201                 } else {
 202                         /* group sequential buffers into one large buffer */
 203                         addr = virt_to_bus (bh->b_data);
 204                         size = bh->b_size;
 205                         while ((bh = bh->b_reqnext) != NULL) {
 206                                 if ((addr + size) != virt_to_bus (bh->b_data))
 207                                         break;
 208                                 size += bh->b_size;
 209                         }
 210                 }
 211 
 212                 /*
 213                  * Fill in the dma table, without crossing any 64kB boundaries.
 214                  * We assume 16-bit alignment of all blocks.
 215                  */
 216                 while (size) {
 217                         if (++count >= PRD_ENTRIES) {
 218                                 printk("%s: DMA table too small\n", drive->name);
 219                                 return 1; /* revert to PIO for this request */
 220                         } else {
 221                                 unsigned long bcount = 0x10000 - (addr & 0xffff);
 222                                 if (bcount > size)
 223                                         bcount = size;
 224                                 *table++ = addr;
 225                                 *table++ = bcount;
 226                                 addr += bcount;
 227                                 size -= bcount;
 228                         }
 229                 }
 230         } while (bh != NULL);
 231         if (count) {
 232                 *--table |= 0x80000000; /* set End-Of-Table (EOT) bit */
 233                 return 0;
 234         }
 235         printk("%s: empty DMA table?\n", drive->name);
 236         return 1;       /* let the PIO routines handle this weirdness */
 237 }
 238 
 239 static int config_drive_for_dma (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 240 {
 241         const char **list;
 242 
 243         struct hd_driveid *id = drive->id;
 244         if (id && (id->capability & 1)) {
 245                 /* Enable DMA on any drive that supports mword2 DMA */
 246                 if ((id->field_valid & 2) && (id->dma_mword & 0x404) == 0x404) {
 247                         drive->using_dma = 1;
 248                         return 0;               /* DMA enabled */
 249                 }
 250                 /* Consult the list of known "good" drives */
 251                 list = good_dma_drives;
 252                 while (*list) {
 253                         if (!strcmp(*list++,id->model)) {
 254                                 drive->using_dma = 1;
 255                                 return 0;       /* DMA enabled */
 256                         }
 257                 }
 258         }
 259         return 1;       /* DMA not enabled */
 260 }
 261 
 262 /*
 263  * triton_dmaproc() initiates/aborts DMA read/write operations on a drive.
 264  *
 265  * The caller is assumed to have selected the drive and programmed the drive's
 266  * sector address using CHS or LBA.  All that remains is to prepare for DMA
 267  * and then issue the actual read/write DMA/PIO command to the drive.
 268  *
 269  * For ATAPI devices, we just prepare for DMA and return. The caller should
 270  * then issue the packet command to the drive and call us again with
 271  * ide_dma_begin afterwards.
 272  *
 273  * Returns 0 if all went well.
 274  * Returns 1 if DMA read/write could not be started, in which case
 275  * the caller should revert to PIO for the current request.
 276  */
 277 static int triton_dmaproc (ide_dma_action_t func, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 278 {
 279         unsigned long dma_base = HWIF(drive)->dma_base;
 280         unsigned int reading = (1 << 3);
 281 
 282         switch (func) {
 283                 case ide_dma_abort:
 284                         outb(inb(dma_base)&~1, dma_base);       /* stop DMA */
 285                         return 0;
 286                 case ide_dma_check:
 287                         return config_drive_for_dma (drive);
 288                 case ide_dma_write:
 289                         reading = 0;
 290                 case ide_dma_read:
 291                         break;
 292                 case ide_dma_status_bad:
 293                         return ((inb(dma_base+2) & 7) != 4);    /* verify good DMA status */
 294                 case ide_dma_transferred:
 295 #if 0
 296                         return (number of bytes actually transferred);
 297 #else
 298                         return (0);
 299 #endif
 300                 case ide_dma_begin:
 301                         outb(inb(dma_base)|1, dma_base);        /* begin DMA */
 302                         return 0;
 303                 default:
 304                         printk("triton_dmaproc: unsupported func: %d\n", func);
 305                         return 1;
 306         }
 307         if (build_dmatable (drive))
 308                 return 1;
 309         outl(virt_to_bus (HWIF(drive)->dmatable), dma_base + 4); /* PRD table */
 310         outb(reading, dma_base);                        /* specify r/w */
 311         outb(0x26, dma_base+2);                         /* clear status bits */
 312 #ifdef CONFIG_BLK_DEV_IDEATAPI
 313         if (drive->media != ide_disk)
 314                 return 0;
 315 #endif /* CONFIG_BLK_DEV_IDEATAPI */    
 316         ide_set_handler(drive, &dma_intr, WAIT_CMD);    /* issue cmd to drive */
 317         OUT_BYTE(reading ? WIN_READDMA : WIN_WRITEDMA, IDE_COMMAND_REG);
 318         outb(inb(dma_base)|1, dma_base);                /* begin DMA */
 319         return 0;
 320 }
 321 
 322 /*
 323  * print_triton_drive_flags() displays the currently programmed options
 324  * in the Triton chipset for a given drive.
 325  *
 326  *      If fastDMA  is "no", then slow ISA timings are used for DMA data xfers.
 327  *      If fastPIO  is "no", then slow ISA timings are used for PIO data xfers.
 328  *      If IORDY    is "no", then IORDY is assumed to always be asserted.
 329  *      If PreFetch is "no", then data pre-fetch/post are not used.
 330  *
 331  * When "fastPIO" and/or "fastDMA" are "yes", then faster PCI timings and
 332  * back-to-back 16-bit data transfers are enabled, using the sample_CLKs
 333  * and recovery_CLKs (PCI clock cycles) timing parameters for that interface.
 334  */
 335 static void print_triton_drive_flags (unsigned int unit, byte flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 336 {
 337         printk("         %s ", unit ? "slave :" : "master:");
 338         printk( "fastDMA=%s",   (flags&9)       ? "on " : "off");
 339         printk(" PreFetch=%s",  (flags&4)       ? "on " : "off");
 340         printk(" IORDY=%s",     (flags&2)       ? "on " : "off");
 341         printk(" fastPIO=%s\n", ((flags&9)==1)  ? "on " : "off");
 342 }
 343 
 344 static void init_triton_dma (ide_hwif_t *hwif, unsigned short base)
     /* [previous][next][first][last][top][bottom][index][help] */
 345 {
 346         static unsigned long dmatable = 0;
 347 
 348         printk("    %s: BusMaster DMA at 0x%04x-0x%04x", hwif->name, base, base+7);
 349         if (check_region(base, 8)) {
 350                 printk(" -- ERROR, PORTS ALREADY IN USE");
 351         } else {
 352                 request_region(base, 8, "triton DMA");
 353                 hwif->dma_base = base;
 354                 if (!dmatable) {
 355                         /*
 356                          * Since we know we are on a PCI bus, we could
 357                          * actually use __get_free_pages() here instead
 358                          * of __get_dma_pages() -- no ISA limitations.
 359                          */
 360                         dmatable = __get_dma_pages(GFP_KERNEL, 0);
 361                 }
 362                 if (dmatable) {
 363                         hwif->dmatable = (unsigned long *) dmatable;
 364                         dmatable += (PRD_ENTRIES * PRD_BYTES);
 365                         outl(virt_to_bus(hwif->dmatable), base + 4);
 366                         hwif->dmaproc  = &triton_dmaproc;
 367                 }
 368         }
 369         printk("\n");
 370 }
 371 
 372 /*
 373  * calc_mode() returns the ATA PIO mode number, based on the number
 374  * of cycle clks passed in.  Assumes 33Mhz bus operation (30ns per clk).
 375  */
 376 byte calc_mode (byte clks)
     /* [previous][next][first][last][top][bottom][index][help] */
 377 {
 378         if (clks == 3)  return 5;
 379         if (clks == 4)  return 4;
 380         if (clks <  6)  return 3;
 381         if (clks <  8)  return 2;
 382         if (clks < 13)  return 1;
 383         return 0;
 384 }
 385 
 386 /*
 387  * ide_init_triton() prepares the IDE driver for DMA operation.
 388  * This routine is called once, from ide.c during driver initialization,
 389  * for each triton chipset which is found (unlikely to be more than one).
 390  */
 391 void ide_init_triton (byte bus, byte fn)
     /* [previous][next][first][last][top][bottom][index][help] */
 392 {
 393         int rc = 0, h;
 394         int dma_enabled = 0;
 395         unsigned short bmiba, pcicmd;
 396         unsigned int timings;
 397 
 398         printk("ide: Triton BM-IDE on PCI bus %d function %d\n", bus, fn);
 399         /*
 400          * See if IDE and BM-DMA features are enabled:
 401          */
 402         if ((rc = pcibios_read_config_word(bus, fn, 0x04, &pcicmd)))
 403                 goto quit;
 404         if ((pcicmd & 1) == 0)  {
 405                 printk("ide: Triton IDE ports are not enabled\n");
 406                 goto quit;
 407         }
 408         if ((pcicmd & 4) == 0) {
 409                 printk("ide: Triton BM-DMA feature is not enabled -- upgrade your BIOS\n");
 410         } else {
 411                 /*
 412                  * Get the bmiba base address
 413                  */
 414                 if ((rc = pcibios_read_config_word(bus, fn, 0x20, &bmiba)))
 415                         goto quit;
 416                 bmiba &= 0xfff0;        /* extract port base address */
 417                 dma_enabled = 1;
 418         }
 419 
 420         /*
 421          * See if ide port(s) are enabled
 422          */
 423         if ((rc = pcibios_read_config_dword(bus, fn, 0x40, &timings)))
 424                 goto quit;
 425         if (!(timings & 0x80008000)) {
 426                 printk("ide: neither Triton IDE port is enabled\n");
 427                 goto quit;
 428         }
 429 
 430         /*
 431          * Save the dma_base port addr for each interface
 432          */
 433         for (h = 0; h < MAX_HWIFS; ++h) {
 434                 byte s_clks, r_clks;
 435                 ide_hwif_t *hwif = &ide_hwifs[h];
 436                 unsigned short time;
 437                 if (hwif->io_base == 0x1f0) {
 438                         time = timings & 0xffff;
 439                         if ((timings & 0x8000) == 0)    /* interface enabled? */
 440                                 continue;
 441                         hwif->chipset = ide_triton;
 442                         if (dma_enabled)
 443                                 init_triton_dma(hwif, bmiba);
 444                 } else if (hwif->io_base == 0x170) {
 445                         time = timings >> 16;
 446                         if ((timings & 0x8000) == 0)    /* interface enabled? */
 447                                 continue;
 448                         hwif->chipset = ide_triton;
 449                         if (dma_enabled)
 450                                 init_triton_dma(hwif, bmiba + 8);
 451                 } else
 452                         continue;
 453                 s_clks = ((~time >> 12) & 3) + 2;
 454                 r_clks = ((~time >>  8) & 3) + 1;
 455                 printk("    %s timing: (0x%04x) sample_CLKs=%d, recovery_CLKs=%d (PIO mode%d)\n",
 456                  hwif->name, time, s_clks, r_clks, calc_mode(s_clks+r_clks));
 457                 print_triton_drive_flags (0, time & 0xf);
 458                 print_triton_drive_flags (1, (time >> 4) & 0xf);
 459         }
 460 
 461 quit: if (rc) printk("ide: pcibios access failed - %s\n", pcibios_strerror(rc));
 462 }
 463 

/* [previous][next][first][last][top][bottom][index][help] */