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. ide_init_triton

   1 /*
   2  *  linux/drivers/block/triton.c        Version 1.01  Aug 28, 1995
   3  *
   4  *  Copyright (c) 1995  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 fine (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  *
  50  *   Micropolis MC2112A (1Gig w/508kB buffer), drive pre-dates EIDE and ATA2.
  51  *      - DMA works fine (2.2MB/sec), probably due to the large on-drive buffer.
  52  *      - This older drive can also be tweaked for fastPIO (3.7MB/sec) by using
  53  *        maximum clock settings (5,4) and setting all flags except prefetch.
  54  *
  55  *   Western Digital AC31000H (1Gig w/128kB buffer), DMA mode1, PIO mode3.
  56  *      - DMA does not work reliably.  The drive appears to be somewhat tardy
  57  *        in deasserting DMARQ at the end of a sector.  This is evident in
  58  *        the observation that WRITEs work most of the time, depending on
  59  *        cache-buffer occupancy, but multi-sector reads seldom work.
  60  *
  61  * Drives like the AC31000H could likely be made to work if all DMA were done
  62  * one sector at a time, but that would likely negate any advantage over PIO.
  63  *
  64  * If you have any drive models to add, email your results to:  mlord@bnr.ca
  65  * Keep an eye on /var/adm/messages for "DMA disabled" messages.
  66  */
  67 #define _TRITON_C
  68 #include <linux/config.h>
  69 #ifndef CONFIG_BLK_DEV_TRITON
  70 #define CONFIG_BLK_DEV_TRITON y
  71 #endif
  72 #include <linux/types.h>
  73 #include <linux/kernel.h>
  74 #include <linux/timer.h>
  75 #include <linux/mm.h>
  76 #include <linux/ioport.h>
  77 #include <linux/interrupt.h>
  78 #include <linux/blkdev.h>
  79 #include <linux/hdreg.h>
  80 #include <linux/pci.h>
  81 #include <linux/bios32.h>
  82 #include <asm/io.h>
  83 
  84 #include "ide.h"
  85 
  86 /*
  87  * good_dma_drives() lists the model names (from "hdparm -i")
  88  * of drives which do not support mword2 DMA but which are
  89  * known to work fine with this interface under Linux.
  90  */
  91 const char *good_dma_drives[] = {"Micropolis 2112A"};
  92 
  93 /*
  94  * Our Physical Region Descriptor (PRD) table should be large enough
  95  * to handle the biggest I/O request we are likely to see.  Since requests
  96  * can have no more than 256 sectors, and since the typical blocksize is
  97  * two sectors, we can get by with a limit of 128 entries here for the
  98  * usual worst case.  Most requests seem to include some contiguous blocks,
  99  * further reducing the number of table entries required.
 100  *
 101  * Note that the driver reverts to PIO mode for individual requests that exceed
 102  * this limit (possible with 512 byte blocksizes, eg. MSDOS f/s), so handling
 103  * 100% of all crazy scenarios here is not necessary.
 104  */
 105 #define PRD_ENTRIES     128     /* max memory area count per DMA */
 106 
 107 /*
 108  * dma_intr() is the handler for disk read/write DMA interrupts
 109  */
 110 static void dma_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 111 {
 112         byte stat, dma_stat;
 113         int i;
 114         struct request *rq = HWGROUP(drive)->rq;
 115         unsigned short dma_base = HWIF(drive)->dma_base;
 116 
 117         dma_stat = inb(dma_base+2);             /* get DMA status */
 118         outb(inb(dma_base)&~1, dma_base);       /* stop DMA operation */
 119         stat = GET_STAT();                      /* get drive status */
 120         if (OK_STAT(stat,DRIVE_READY,drive->bad_wstat|DRQ_STAT)) {
 121                 if ((dma_stat & 7) == 4) {      /* verify good DMA status */
 122                         rq = HWGROUP(drive)->rq;
 123                         for (i = rq->nr_sectors; i > 0;) {
 124                                 i -= rq->current_nr_sectors;
 125                                 ide_end_request(1, HWGROUP(drive));
 126                         }
 127                         IDE_DO_REQUEST;
 128                         return;
 129                 }
 130                 printk("%s: bad DMA status: 0x%02x\n", drive->name, dma_stat);
 131         }
 132         sti();
 133         if (!ide_error(drive, "dma_intr", stat))
 134                 IDE_DO_REQUEST;
 135 }
 136 
 137 /*
 138  * build_dmatable() prepares a dma request.
 139  * Returns 0 if all went okay, returns 1 otherwise.
 140  */
 141 static int build_dmatable (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 142 {
 143         struct request *rq = HWGROUP(drive)->rq;
 144         struct buffer_head *bh = rq->bh;
 145         unsigned long size, addr, *table = HWIF(drive)->dmatable;
 146         unsigned int count = 0;
 147 
 148         do {
 149                 /*
 150                  * Determine addr and size of next buffer area.  We assume that
 151                  * individual virtual buffers are always composed linearly in
 152                  * physical memory.  For example, we assume that any 8kB buffer
 153                  * is always composed of two adjacent physical 4kB pages rather
 154                  * than two possibly non-adjacent physical 4kB pages.
 155                  */
 156                 if (bh == NULL) {  /* paging requests have (rq->bh == NULL) */
 157                         addr = virt_to_bus (rq->buffer);
 158                         size = rq->nr_sectors << 9;
 159                 } else {
 160                         /* group sequential buffers into one large buffer */
 161                         addr = virt_to_bus (bh->b_data);
 162                         size = bh->b_size;
 163                         while ((bh = bh->b_reqnext) != NULL) {
 164                                 if ((addr + size) != virt_to_bus (bh->b_data))
 165                                         break;
 166                                 size += bh->b_size;
 167                         }
 168                 }
 169 
 170                 /*
 171                  * Fill in the dma table, without crossing any 64kB boundaries.
 172                  * We assume 16-bit alignment of all blocks.
 173                  */
 174                 while (size) {
 175                         if (++count >= PRD_ENTRIES) {
 176                                 printk("%s: DMA table too small\n", drive->name);
 177                                 return 1; /* revert to PIO for this request */
 178                         } else {
 179                                 unsigned long bcount = 0x10000 - (addr & 0xffff);
 180                                 if (bcount > size)
 181                                         bcount = size;
 182                                 *table++ = addr;
 183                                 *table++ = bcount;
 184                                 addr += bcount;
 185                                 size -= bcount;
 186                         }
 187                 }
 188         } while (bh != NULL);
 189         if (count) {
 190                 *--table |= 0x80000000; /* set End-Of-Table (EOT) bit */
 191                 return 0;
 192         }
 193         printk("%s: empty DMA table?\n", drive->name);
 194         return 1;       /* let the PIO routines handle this weirdness */
 195 }
 196 
 197 static int config_drive_for_dma (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 198 {
 199         const char **list;
 200 
 201         struct hd_driveid *id = drive->id;
 202         if (id && (id->capability & 1)) {
 203                 /* Enable DMA on any drive that supports mword2 DMA */
 204                 if ((id->field_valid & 2) && (id->dma_mword & 0x404) == 0x404) {
 205                         drive->using_dma = 1;
 206                         return 0;               /* DMA enabled */
 207                 }
 208                 /* Consult the list of known "good" drives */
 209                 list = good_dma_drives;
 210                 while (*list) {
 211                         if (!strcmp(*list++,id->model)) {
 212                                 drive->using_dma = 1;
 213                                 return 0;       /* DMA enabled */
 214                         }
 215                 }
 216         }
 217         return 1;       /* DMA not enabled */
 218 }
 219 
 220 /*
 221  * triton_dmaproc() initiates/aborts DMA read/write operations on a drive.
 222  *
 223  * The caller is assumed to have selected the drive and programmed the drive's
 224  * sector address using CHS or LBA.  All that remains is to prepare for DMA
 225  * and then issue the actual read/write DMA/PIO command to the drive.
 226  *
 227  * Returns 0 if all went well.
 228  * Returns 1 if DMA read/write could not be started, in which case
 229  * the caller should revert to PIO for the current request.
 230  */
 231 static int triton_dmaproc (ide_dma_action_t func, ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 232 {
 233         unsigned long dma_base = HWIF(drive)->dma_base;
 234         unsigned int reading = (1 << 3);
 235 
 236         switch (func) {
 237                 case ide_dma_abort:
 238                         outb(inb(dma_base)&~1, dma_base);       /* stop DMA */
 239                         return 0;
 240                 case ide_dma_check:
 241                         return config_drive_for_dma (drive);
 242                 case ide_dma_write:
 243                         reading = 0;
 244                 case ide_dma_read:
 245                         break;
 246                 default:
 247                         printk("triton_dmaproc: unsupported func: %d\n", func);
 248                         return 1;
 249         }
 250         if (build_dmatable (drive))
 251                 return 1;
 252         outl(virt_to_bus (HWIF(drive)->dmatable), dma_base + 4); /* PRD table */
 253         outb(reading, dma_base);                        /* specify r/w */
 254         outb(0x26, dma_base+2);                         /* clear status bits */
 255         ide_set_handler (drive, &dma_intr);             /* issue cmd to drive */
 256         OUT_BYTE(reading ? WIN_READDMA : WIN_WRITEDMA, IDE_COMMAND_REG);
 257         outb(inb(dma_base)|1, dma_base);                /* begin DMA */
 258         return 0;
 259 }
 260 
 261 /*
 262  * print_triton_drive_flags() displays the currently programmed options
 263  * in the Triton chipset for a given drive.
 264  *
 265  *      If fastDMA  is "no", then slow ISA timings are used for DMA data xfers.
 266  *      If fastPIO  is "no", then slow ISA timings are used for PIO data xfers.
 267  *      If IORDY    is "no", then IORDY is assumed to always be asserted.
 268  *      If PreFetch is "no", then data pre-fetch/post are not used.
 269  *
 270  * When "fastPIO" and/or "fastDMA" are "yes", then faster PCI timings and
 271  * back-to-back 16-bit data transfers are enabled, using the sample_CLKs
 272  * and recovery_CLKs (PCI clock cycles) timing parameters for that interface.
 273  */
 274 static void print_triton_drive_flags (unsigned int unit, byte flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 275 {
 276         printk("         %s ", unit ? "slave :" : "master:");
 277         printk( "fastDMA=%s",   (flags&9)       ? "on " : "off");
 278         printk(" PreFetch=%s",  (flags&4)       ? "on " : "off");
 279         printk(" IORDY=%s",     (flags&2)       ? "on " : "off");
 280         printk(" fastPIO=%s\n", ((flags&9)==1)  ? "on " : "off");
 281 }
 282 
 283 /*
 284  * ide_init_triton() prepares the IDE driver for DMA operation.
 285  * This routine is called once, from ide.c during driver initialization,
 286  * for each triton chipset which is found (unlikely to be more than one).
 287  */
 288 void ide_init_triton (byte bus, byte fn)
     /* [previous][next][first][last][top][bottom][index][help] */
 289 {
 290         int rc = 0, h;
 291         unsigned short bmiba, pcicmd;
 292         unsigned int timings;
 293         extern ide_hwif_t ide_hwifs[];
 294 
 295         /*
 296          * See if IDE and BM-DMA features are enabled:
 297          */
 298         if ((rc = pcibios_read_config_word(bus, fn, 0x04, &pcicmd)))
 299                 goto quit;
 300         if ((pcicmd & 5) != 5) {
 301                 if ((pcicmd & 1) == 0)
 302                         printk("ide: Triton IDE ports are not enabled\n");
 303                 else
 304                         printk("ide: Triton BM-DMA feature is not enabled\n");
 305                 goto quit;
 306         }
 307 #if 0
 308         (void) pcibios_write_config_word(bus, fn, 0x42, 0x8037); /* for my MC2112A */
 309 #endif
 310         /*
 311          * See if ide port(s) are enabled
 312          */
 313         if ((rc = pcibios_read_config_dword(bus, fn, 0x40, &timings)))
 314                 goto quit;
 315         if (!(timings & 0x80008000)) {
 316                 printk("ide: Triton IDE ports are not enabled\n");
 317                 goto quit;
 318         }
 319         printk("ide: Triton BM-IDE on PCI bus %d function %d\n", bus, fn);
 320 
 321         /*
 322          * Get the bmiba base address
 323          */
 324         if ((rc = pcibios_read_config_word(bus, fn, 0x20, &bmiba)))
 325                 goto quit;
 326         bmiba &= 0xfff0;        /* extract port base address */
 327 
 328         /*
 329          * Save the dma_base port addr for each interface
 330          */
 331         for (h = 0; h < MAX_HWIFS; ++h) {
 332                 ide_hwif_t *hwif = &ide_hwifs[h];
 333                 unsigned short base, time;
 334                 if (hwif->io_base == 0x1f0 && (timings & 0x8000)) {
 335                         time = timings & 0xffff;
 336                         base = bmiba;
 337                 } else if (hwif->io_base == 0x170 && (timings & 0x80000000)) {
 338                         time = timings >> 16;
 339                         base = bmiba + 8;
 340                 } else
 341                         continue;
 342                 printk("    %s: BusMaster DMA at 0x%04x-0x%04x", hwif->name, base, base+7);
 343                 if (check_region(base, 8)) {
 344                         printk(" -- ERROR, PORTS ALREADY IN USE");
 345                 } else {
 346                         unsigned long *table;
 347                         request_region(base, 8, hwif->name);
 348                         hwif->dma_base  = base;
 349                         table = ide_alloc(2 * PRD_ENTRIES * sizeof(long), 4096);
 350                         hwif->dmatable = table;
 351                         outl((unsigned long) table, base + 4);
 352                         hwif->dmaproc  = &triton_dmaproc;
 353                 }
 354                 printk("\n    %s timing: (0x%04x) sample_CLKs=%d, recovery_CLKs=%d\n",
 355                  hwif->name, time, ((~time>>12)&3)+2, ((~time>>8)&3)+1);
 356                 print_triton_drive_flags (0, time & 0xf);
 357                 print_triton_drive_flags (1, (time >> 4) & 0xf);
 358         }
 359 
 360 quit: if (rc) printk("ide: pcibios access failed - %s\n", pcibios_strerror(rc));
 361 }
 362 

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