OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [block/] [hd.c] - Diff between revs 1622 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 1622 Rev 1765
/*
/*
 *  linux/arch/arm/drivers/block/hd.c
 *  linux/arch/arm/drivers/block/hd.c
 *   [ origional file: linux/drivers/block/hd.c ]
 *   [ origional file: linux/drivers/block/hd.c ]
 *
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *  Modified 1995  Russell King for ARM processor.
 *  Modified 1995  Russell King for ARM processor.
 */
 */
 
 
/*
/*
 * This is the low-level hd interrupt support. It traverses the
 * This is the low-level hd interrupt support. It traverses the
 * request-list, using interrupts to jump between functions. As
 * request-list, using interrupts to jump between functions. As
 * all the functions are called within interrupts, we may not
 * all the functions are called within interrupts, we may not
 * sleep. Special care is recommended.
 * sleep. Special care is recommended.
 *
 *
 *  modified by Drew Eckhardt to check nr of hd's from the CMOS.
 *  modified by Drew Eckhardt to check nr of hd's from the CMOS.
 *
 *
 *  Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
 *  Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
 *  in the early extended-partition checks and added DM partitions
 *  in the early extended-partition checks and added DM partitions
 *
 *
 *  IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
 *  IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
 *  and general streamlining by Mark Lord.
 *  and general streamlining by Mark Lord.
 */
 */
 
 
#define DEFAULT_MULT_COUNT  0   /* set to 0 to disable multiple mode at boot */
#define DEFAULT_MULT_COUNT  0   /* set to 0 to disable multiple mode at boot */
#define DEFAULT_UNMASK_INTR 0   /* set to 0 to *NOT* unmask irq's more often */
#define DEFAULT_UNMASK_INTR 0   /* set to 0 to *NOT* unmask irq's more often */
 
 
#include <asm/irq.h>
#include <asm/irq.h>
#include <linux/errno.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/timer.h>
#include <linux/fs.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/kernel.h>
#include <linux/hdreg.h>
#include <linux/hdreg.h>
#include <linux/genhd.h>
#include <linux/genhd.h>
#include <linux/malloc.h>
#include <linux/malloc.h>
#include <linux/string.h>
#include <linux/string.h>
#include <linux/ioport.h>
#include <linux/ioport.h>
 
 
#define REALLY_SLOW_IO
#define REALLY_SLOW_IO
#include <asm/system.h>
#include <asm/system.h>
#include <asm/io.h>
#include <asm/io.h>
#include <asm/segment.h>
#include <asm/segment.h>
 
 
#define MAJOR_NR HD_MAJOR
#define MAJOR_NR HD_MAJOR
#include <linux/blk.h>
#include <linux/blk.h>
 
 
#undef  HD_IRQ
#undef  HD_IRQ
#define HD_IRQ 11
#define HD_IRQ 11
 
 
static int revalidate_hddisk(kdev_t, int);
static int revalidate_hddisk(kdev_t, int);
 
 
#define HD_DELAY        0
#define HD_DELAY        0
 
 
#define MAX_ERRORS     16       /* Max read/write errors/sector */
#define MAX_ERRORS     16       /* Max read/write errors/sector */
#define RESET_FREQ      8       /* Reset controller every 8th retry */
#define RESET_FREQ      8       /* Reset controller every 8th retry */
#define RECAL_FREQ      4       /* Recalibrate every 4th retry */
#define RECAL_FREQ      4       /* Recalibrate every 4th retry */
#define MAX_HD          2
#define MAX_HD          2
 
 
#define STAT_OK         (READY_STAT|SEEK_STAT)
#define STAT_OK         (READY_STAT|SEEK_STAT)
#define OK_STATUS(s)    (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
#define OK_STATUS(s)    (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
 
 
static void recal_intr(void);
static void recal_intr(void);
static void bad_rw_intr(void);
static void bad_rw_intr(void);
 
 
static char recalibrate[MAX_HD];
static char recalibrate[MAX_HD];
static char special_op[MAX_HD];
static char special_op[MAX_HD];
static int access_count[MAX_HD];
static int access_count[MAX_HD];
static char busy[MAX_HD];
static char busy[MAX_HD];
static struct wait_queue * busy_wait;
static struct wait_queue * busy_wait;
 
 
static int reset;
static int reset;
static int hd_error;
static int hd_error;
 
 
/*
/*
 *  This struct defines the HD's and their types.
 *  This struct defines the HD's and their types.
 */
 */
struct hd_i_struct {
struct hd_i_struct {
        unsigned int head,sect,cyl,wpcom,lzone,ctl;
        unsigned int head,sect,cyl,wpcom,lzone,ctl;
        };
        };
static struct hd_driveid *hd_ident_info[MAX_HD];
static struct hd_driveid *hd_ident_info[MAX_HD];
 
 
#ifdef HD_TYPE
#ifdef HD_TYPE
static struct hd_i_struct hd_info[] = { HD_TYPE };
static struct hd_i_struct hd_info[] = { HD_TYPE };
struct hd_i_struct bios_info[] = { HD_TYPE };
struct hd_i_struct bios_info[] = { HD_TYPE };
static int NR_HD = ((sizeof (hd_info))/(sizeof (struct hd_i_struct)));
static int NR_HD = ((sizeof (hd_info))/(sizeof (struct hd_i_struct)));
#else
#else
static struct hd_i_struct hd_info[MAX_HD];
static struct hd_i_struct hd_info[MAX_HD];
struct hd_i_struct bios_info[MAX_HD];
struct hd_i_struct bios_info[MAX_HD];
static int NR_HD;
static int NR_HD;
#endif
#endif
 
 
static struct hd_struct hd[MAX_HD<<6];
static struct hd_struct hd[MAX_HD<<6];
static int hd_sizes[MAX_HD<<6];
static int hd_sizes[MAX_HD<<6];
static int hd_blocksizes[MAX_HD<<6];
static int hd_blocksizes[MAX_HD<<6];
static int hd_hardsectsizes[MAX_HD<<6];
static int hd_hardsectsizes[MAX_HD<<6];
 
 
#if (HD_DELAY > 0)
#if (HD_DELAY > 0)
unsigned long last_req;
unsigned long last_req;
 
 
unsigned long read_timer(void)
unsigned long read_timer(void)
{
{
        unsigned long t, flags;
        unsigned long t, flags;
        int i;
        int i;
 
 
        save_flags_cli (flags);
        save_flags_cli (flags);
        t = jiffies * 11932;
        t = jiffies * 11932;
        outb_p(0, 0x43);
        outb_p(0, 0x43);
        i = inb_p(0x40);
        i = inb_p(0x40);
        i |= inb(0x40) << 8;
        i |= inb(0x40) << 8;
        restore_flags(flags);
        restore_flags(flags);
        return(t - i);
        return(t - i);
}
}
#endif
#endif
 
 
void hd_setup(char *str, int *ints)
void hd_setup(char *str, int *ints)
{
{
        int hdind = 0;
        int hdind = 0;
 
 
        if (ints[0] != 3)
        if (ints[0] != 3)
                return;
                return;
        if (bios_info[0].head != 0)
        if (bios_info[0].head != 0)
                hdind=1;
                hdind=1;
        bios_info[hdind].head  = hd_info[hdind].head = ints[2];
        bios_info[hdind].head  = hd_info[hdind].head = ints[2];
        bios_info[hdind].sect  = hd_info[hdind].sect = ints[3];
        bios_info[hdind].sect  = hd_info[hdind].sect = ints[3];
        bios_info[hdind].cyl   = hd_info[hdind].cyl = ints[1];
        bios_info[hdind].cyl   = hd_info[hdind].cyl = ints[1];
        bios_info[hdind].wpcom = hd_info[hdind].wpcom = 0;
        bios_info[hdind].wpcom = hd_info[hdind].wpcom = 0;
        bios_info[hdind].lzone = hd_info[hdind].lzone = ints[1];
        bios_info[hdind].lzone = hd_info[hdind].lzone = ints[1];
        bios_info[hdind].ctl   = hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);
        bios_info[hdind].ctl   = hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);
        NR_HD = hdind+1;
        NR_HD = hdind+1;
}
}
 
 
static void dump_status (const char *msg, unsigned int stat)
static void dump_status (const char *msg, unsigned int stat)
{
{
        unsigned long flags;
        unsigned long flags;
        char devc;
        char devc;
 
 
        devc = CURRENT ? 'a' + DEVICE_NR(CURRENT->rq_dev) : '?';
        devc = CURRENT ? 'a' + DEVICE_NR(CURRENT->rq_dev) : '?';
        save_flags (flags);
        save_flags (flags);
        sti();
        sti();
        printk("hd%c: %s: status=0x%02x { ", devc, msg, stat & 0xff);
        printk("hd%c: %s: status=0x%02x { ", devc, msg, stat & 0xff);
        if (stat & BUSY_STAT)   printk("Busy ");
        if (stat & BUSY_STAT)   printk("Busy ");
        if (stat & READY_STAT)  printk("DriveReady ");
        if (stat & READY_STAT)  printk("DriveReady ");
        if (stat & WRERR_STAT)  printk("WriteFault ");
        if (stat & WRERR_STAT)  printk("WriteFault ");
        if (stat & SEEK_STAT)   printk("SeekComplete ");
        if (stat & SEEK_STAT)   printk("SeekComplete ");
        if (stat & DRQ_STAT)    printk("DataRequest ");
        if (stat & DRQ_STAT)    printk("DataRequest ");
        if (stat & ECC_STAT)    printk("CorrectedError ");
        if (stat & ECC_STAT)    printk("CorrectedError ");
        if (stat & INDEX_STAT)  printk("Index ");
        if (stat & INDEX_STAT)  printk("Index ");
        if (stat & ERR_STAT)    printk("Error ");
        if (stat & ERR_STAT)    printk("Error ");
        printk("}\n");
        printk("}\n");
        if ((stat & ERR_STAT) == 0) {
        if ((stat & ERR_STAT) == 0) {
                hd_error = 0;
                hd_error = 0;
        } else {
        } else {
                hd_error = inb(HD_ERROR);
                hd_error = inb(HD_ERROR);
                printk("hd%c: %s: error=0x%02x { ", devc, msg, hd_error & 0xff);
                printk("hd%c: %s: error=0x%02x { ", devc, msg, hd_error & 0xff);
                if (hd_error & BBD_ERR)         printk("BadSector ");
                if (hd_error & BBD_ERR)         printk("BadSector ");
                if (hd_error & ECC_ERR)         printk("UncorrectableError ");
                if (hd_error & ECC_ERR)         printk("UncorrectableError ");
                if (hd_error & ID_ERR)          printk("SectorIdNotFound ");
                if (hd_error & ID_ERR)          printk("SectorIdNotFound ");
                if (hd_error & ABRT_ERR)        printk("DriveStatusError ");
                if (hd_error & ABRT_ERR)        printk("DriveStatusError ");
                if (hd_error & TRK0_ERR)        printk("TrackZeroNotFound ");
                if (hd_error & TRK0_ERR)        printk("TrackZeroNotFound ");
                if (hd_error & MARK_ERR)        printk("AddrMarkNotFound ");
                if (hd_error & MARK_ERR)        printk("AddrMarkNotFound ");
                printk("}");
                printk("}");
                if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
                if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
                        printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL),
                        printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL),
                                inb(HD_CURRENT) & 0xf, inb(HD_SECTOR));
                                inb(HD_CURRENT) & 0xf, inb(HD_SECTOR));
                        if (CURRENT)
                        if (CURRENT)
                                printk(", sector=%ld", CURRENT->sector);
                                printk(", sector=%ld", CURRENT->sector);
                }
                }
                printk("\n");
                printk("\n");
        }
        }
        restore_flags (flags);
        restore_flags (flags);
}
}
 
 
void check_status(void)
void check_status(void)
{
{
        int i = inb_p(HD_STATUS);
        int i = inb_p(HD_STATUS);
 
 
        if (!OK_STATUS(i)) {
        if (!OK_STATUS(i)) {
                dump_status("check_status", i);
                dump_status("check_status", i);
                bad_rw_intr();
                bad_rw_intr();
        }
        }
}
}
 
 
static int controller_busy(void)
static int controller_busy(void)
{
{
        int retries = 100000;
        int retries = 100000;
        unsigned char status;
        unsigned char status;
 
 
        do {
        do {
                status = inb_p(HD_STATUS);
                status = inb_p(HD_STATUS);
        } while ((status & BUSY_STAT) && --retries);
        } while ((status & BUSY_STAT) && --retries);
        return status;
        return status;
}
}
 
 
static int status_ok(void)
static int status_ok(void)
{
{
        unsigned char status = inb_p(HD_STATUS);
        unsigned char status = inb_p(HD_STATUS);
 
 
        if (status & BUSY_STAT)
        if (status & BUSY_STAT)
                return 1;       /* Ancient, but does it make sense??? */
                return 1;       /* Ancient, but does it make sense??? */
        if (status & WRERR_STAT)
        if (status & WRERR_STAT)
                return 0;
                return 0;
        if (!(status & READY_STAT))
        if (!(status & READY_STAT))
                return 0;
                return 0;
        if (!(status & SEEK_STAT))
        if (!(status & SEEK_STAT))
                return 0;
                return 0;
        return 1;
        return 1;
}
}
 
 
static int controller_ready(unsigned int drive, unsigned int head)
static int controller_ready(unsigned int drive, unsigned int head)
{
{
        int retry = 100;
        int retry = 100;
 
 
        do {
        do {
                if (controller_busy() & BUSY_STAT)
                if (controller_busy() & BUSY_STAT)
                        return 0;
                        return 0;
                outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
                outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
                if (status_ok())
                if (status_ok())
                        return 1;
                        return 1;
        } while (--retry);
        } while (--retry);
        return 0;
        return 0;
}
}
 
 
static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect,
static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect,
                unsigned int head,unsigned int cyl,unsigned int cmd,
                unsigned int head,unsigned int cyl,unsigned int cmd,
                void (*intr_addr)(void))
                void (*intr_addr)(void))
{
{
        unsigned short port;
        unsigned short port;
 
 
#if (HD_DELAY > 0)
#if (HD_DELAY > 0)
        while (read_timer() - last_req < HD_DELAY)
        while (read_timer() - last_req < HD_DELAY)
                /* nothing */;
                /* nothing */;
#endif
#endif
        if (reset)
        if (reset)
                return;
                return;
        if (!controller_ready(drive, head)) {
        if (!controller_ready(drive, head)) {
                reset = 1;
                reset = 1;
                return;
                return;
        }
        }
        SET_INTR(intr_addr);
        SET_INTR(intr_addr);
        outb_p(hd_info[drive].ctl,HD_CMD);
        outb_p(hd_info[drive].ctl,HD_CMD);
        port=HD_DATA;
        port=HD_DATA;
        outb_p(hd_info[drive].wpcom>>2,++port);
        outb_p(hd_info[drive].wpcom>>2,++port);
        outb_p(nsect,++port);
        outb_p(nsect,++port);
        outb_p(sect,++port);
        outb_p(sect,++port);
        outb_p(cyl,++port);
        outb_p(cyl,++port);
        outb_p(cyl>>8,++port);
        outb_p(cyl>>8,++port);
        outb_p(0xA0|(drive<<4)|head,++port);
        outb_p(0xA0|(drive<<4)|head,++port);
        outb_p(cmd,++port);
        outb_p(cmd,++port);
}
}
 
 
static void hd_request (void);
static void hd_request (void);
static unsigned int identified  [MAX_HD]; /* 1 = drive ID already displayed   */
static unsigned int identified  [MAX_HD]; /* 1 = drive ID already displayed   */
static unsigned int unmask_intr [MAX_HD]; /* 1 = unmask IRQs during I/O       */
static unsigned int unmask_intr [MAX_HD]; /* 1 = unmask IRQs during I/O       */
static unsigned int max_mult    [MAX_HD]; /* max sectors for MultMode         */
static unsigned int max_mult    [MAX_HD]; /* max sectors for MultMode         */
static unsigned int mult_req    [MAX_HD]; /* requested MultMode count         */
static unsigned int mult_req    [MAX_HD]; /* requested MultMode count         */
static unsigned int mult_count  [MAX_HD]; /* currently enabled MultMode count */
static unsigned int mult_count  [MAX_HD]; /* currently enabled MultMode count */
static struct request WCURRENT;
static struct request WCURRENT;
 
 
static void fixstring (unsigned char *s, int bytecount)
static void fixstring (unsigned char *s, int bytecount)
{
{
        unsigned char *p, *end = &s[bytecount &= ~1];   /* bytecount must be even */
        unsigned char *p, *end = &s[bytecount &= ~1];   /* bytecount must be even */
 
 
        /* convert from big-endian to little-endian */
        /* convert from big-endian to little-endian */
        for (p = end ; p != s;) {
        for (p = end ; p != s;) {
                unsigned short *pp = (unsigned short *) (p -= 2);
                unsigned short *pp = (unsigned short *) (p -= 2);
                *pp = (*pp >> 8) | (*pp << 8);
                *pp = (*pp >> 8) | (*pp << 8);
        }
        }
 
 
        /* strip leading blanks */
        /* strip leading blanks */
        while (s != end && *s == ' ')
        while (s != end && *s == ' ')
                ++s;
                ++s;
 
 
        /* compress internal blanks and strip trailing blanks */
        /* compress internal blanks and strip trailing blanks */
        while (s != end && *s) {
        while (s != end && *s) {
                if (*s++ != ' ' || (s != end && *s && *s != ' '))
                if (*s++ != ' ' || (s != end && *s && *s != ' '))
                        *p++ = *(s-1);
                        *p++ = *(s-1);
        }
        }
 
 
        /* wipe out trailing garbage */
        /* wipe out trailing garbage */
        while (p != end)
        while (p != end)
                *p++ = '\0';
                *p++ = '\0';
}
}
 
 
static void identify_intr(void)
static void identify_intr(void)
{
{
        unsigned int dev = DEVICE_NR(CURRENT->rq_dev);
        unsigned int dev = DEVICE_NR(CURRENT->rq_dev);
        unsigned short stat = inb_p(HD_STATUS);
        unsigned short stat = inb_p(HD_STATUS);
        struct hd_driveid *id = hd_ident_info[dev];
        struct hd_driveid *id = hd_ident_info[dev];
 
 
        if (unmask_intr[dev])
        if (unmask_intr[dev])
                sti();
                sti();
        if (stat & (BUSY_STAT|ERR_STAT)) {
        if (stat & (BUSY_STAT|ERR_STAT)) {
                printk ("  hd%c: non-IDE device, %dMB, CHS=%d/%d/%d\n", dev+'a',
                printk ("  hd%c: non-IDE device, %dMB, CHS=%d/%d/%d\n", dev+'a',
                        hd_info[dev].cyl*hd_info[dev].head*hd_info[dev].sect / 2048,
                        hd_info[dev].cyl*hd_info[dev].head*hd_info[dev].sect / 2048,
                        hd_info[dev].cyl, hd_info[dev].head, hd_info[dev].sect);
                        hd_info[dev].cyl, hd_info[dev].head, hd_info[dev].sect);
                if (id != NULL) {
                if (id != NULL) {
                        hd_ident_info[dev] = NULL;
                        hd_ident_info[dev] = NULL;
                        kfree_s (id, 512);
                        kfree_s (id, 512);
                }
                }
        } else {
        } else {
                insw(HD_DATA, id, 256); /* get ID info */
                insw(HD_DATA, id, 256); /* get ID info */
                max_mult[dev] = id->max_multsect;
                max_mult[dev] = id->max_multsect;
                if ((id->field_valid&1) && id->cur_cyls && id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
                if ((id->field_valid&1) && id->cur_cyls && id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
                        /*
                        /*
                         * Extract the physical drive geometry for our use.
                         * Extract the physical drive geometry for our use.
                         * Note that we purposely do *not* update the bios_info.
                         * Note that we purposely do *not* update the bios_info.
                         * This way, programs that use it (like fdisk) will
                         * This way, programs that use it (like fdisk) will
                         * still have the same logical view as the BIOS does,
                         * still have the same logical view as the BIOS does,
                         * which keeps the partition table from being screwed.
                         * which keeps the partition table from being screwed.
                         */
                         */
                        hd_info[dev].cyl  = id->cur_cyls;
                        hd_info[dev].cyl  = id->cur_cyls;
                        hd_info[dev].head = id->cur_heads;
                        hd_info[dev].head = id->cur_heads;
                        hd_info[dev].sect = id->cur_sectors;
                        hd_info[dev].sect = id->cur_sectors;
                }
                }
                fixstring (id->serial_no, sizeof(id->serial_no));
                fixstring (id->serial_no, sizeof(id->serial_no));
                fixstring (id->fw_rev, sizeof(id->fw_rev));
                fixstring (id->fw_rev, sizeof(id->fw_rev));
                fixstring (id->model, sizeof(id->model));
                fixstring (id->model, sizeof(id->model));
                printk ("  hd%c: %.40s, %dMB w/%dKB Cache, CHS=%d/%d/%d, MaxMult=%d\n",
                printk ("  hd%c: %.40s, %dMB w/%dKB Cache, CHS=%d/%d/%d, MaxMult=%d\n",
                        dev+'a', id->model, id->cyls*id->heads*id->sectors/2048,
                        dev+'a', id->model, id->cyls*id->heads*id->sectors/2048,
                        id->buf_size/2, bios_info[dev].cyl, bios_info[dev].head,
                        id->buf_size/2, bios_info[dev].cyl, bios_info[dev].head,
                        bios_info[dev].sect, id->max_multsect);
                        bios_info[dev].sect, id->max_multsect);
                /*
                /*
                 * Early model Quantum drives go weird at this point,
                 * Early model Quantum drives go weird at this point,
                 *   but doing a recalibrate seems to "fix" them.
                 *   but doing a recalibrate seems to "fix" them.
                 * (Doing a full reset confuses some other model Quantums)
                 * (Doing a full reset confuses some other model Quantums)
                 */
                 */
                if (!strncmp(id->model, "QUANTUM", 7))
                if (!strncmp(id->model, "QUANTUM", 7))
                        special_op[dev] = recalibrate[dev] = 1;
                        special_op[dev] = recalibrate[dev] = 1;
        }
        }
#if (HD_DELAY > 0)
#if (HD_DELAY > 0)
        last_req = read_timer();
        last_req = read_timer();
#endif
#endif
        hd_request();
        hd_request();
        return;
        return;
}
}
 
 
static void set_multmode_intr(void)
static void set_multmode_intr(void)
{
{
        unsigned int dev = DEVICE_NR(CURRENT->rq_dev), stat = inb_p(HD_STATUS);
        unsigned int dev = DEVICE_NR(CURRENT->rq_dev), stat = inb_p(HD_STATUS);
 
 
        if (unmask_intr[dev])
        if (unmask_intr[dev])
                sti();
                sti();
        if (stat & (BUSY_STAT|ERR_STAT)) {
        if (stat & (BUSY_STAT|ERR_STAT)) {
                mult_req[dev] = mult_count[dev] = 0;
                mult_req[dev] = mult_count[dev] = 0;
                dump_status("set multmode failed", stat);
                dump_status("set multmode failed", stat);
        } else {
        } else {
                if ((mult_count[dev] = mult_req[dev]))
                if ((mult_count[dev] = mult_req[dev]))
                        printk ("  hd%c: enabled %d-sector multiple mode\n",
                        printk ("  hd%c: enabled %d-sector multiple mode\n",
                                dev+'a', mult_count[dev]);
                                dev+'a', mult_count[dev]);
                else
                else
                        printk ("  hd%c: disabled multiple mode\n", dev+'a');
                        printk ("  hd%c: disabled multiple mode\n", dev+'a');
        }
        }
#if (HD_DELAY > 0)
#if (HD_DELAY > 0)
        last_req = read_timer();
        last_req = read_timer();
#endif
#endif
        hd_request();
        hd_request();
        return;
        return;
}
}
 
 
static int drive_busy(void)
static int drive_busy(void)
{
{
        unsigned int i;
        unsigned int i;
        unsigned char c;
        unsigned char c;
 
 
        for (i = 0; i < 500000 ; i++) {
        for (i = 0; i < 500000 ; i++) {
                c = inb_p(HD_STATUS);
                c = inb_p(HD_STATUS);
                if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
                if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
                        return 0;
                        return 0;
        }
        }
        dump_status("reset timed out", c);
        dump_status("reset timed out", c);
        return 1;
        return 1;
}
}
 
 
static void reset_controller(void)
static void reset_controller(void)
{
{
        int     i;
        int     i;
 
 
        outb_p(4,HD_CMD);
        outb_p(4,HD_CMD);
        for(i = 0; i < 1000; i++) barrier();
        for(i = 0; i < 1000; i++) barrier();
        outb_p(hd_info[0].ctl & 0x0f,HD_CMD);
        outb_p(hd_info[0].ctl & 0x0f,HD_CMD);
        for(i = 0; i < 1000; i++) barrier();
        for(i = 0; i < 1000; i++) barrier();
        if (drive_busy())
        if (drive_busy())
                printk("hd: controller still busy\n");
                printk("hd: controller still busy\n");
        else if ((hd_error = inb(HD_ERROR)) != 1)
        else if ((hd_error = inb(HD_ERROR)) != 1)
                printk("hd: controller reset failed: %02x\n",hd_error);
                printk("hd: controller reset failed: %02x\n",hd_error);
}
}
 
 
static void reset_hd(void)
static void reset_hd(void)
{
{
        static int i;
        static int i;
 
 
repeat:
repeat:
        if (reset) {
        if (reset) {
                reset = 0;
                reset = 0;
                i = -1;
                i = -1;
                reset_controller();
                reset_controller();
        } else {
        } else {
                check_status();
                check_status();
                if (reset)
                if (reset)
                        goto repeat;
                        goto repeat;
        }
        }
        if (++i < NR_HD) {
        if (++i < NR_HD) {
                special_op[i] = recalibrate[i] = 1;
                special_op[i] = recalibrate[i] = 1;
                if (unmask_intr[i]) {
                if (unmask_intr[i]) {
                        unmask_intr[i] = DEFAULT_UNMASK_INTR;
                        unmask_intr[i] = DEFAULT_UNMASK_INTR;
                        printk("hd%c: reset irq-unmasking to %d\n",i+'a',
                        printk("hd%c: reset irq-unmasking to %d\n",i+'a',
                                DEFAULT_UNMASK_INTR);
                                DEFAULT_UNMASK_INTR);
                }
                }
                if (mult_req[i] || mult_count[i]) {
                if (mult_req[i] || mult_count[i]) {
                        mult_count[i] = 0;
                        mult_count[i] = 0;
                        mult_req[i] = DEFAULT_MULT_COUNT;
                        mult_req[i] = DEFAULT_MULT_COUNT;
                        printk("hd%c: reset multiple mode to %d\n",i+'a',
                        printk("hd%c: reset multiple mode to %d\n",i+'a',
                                DEFAULT_MULT_COUNT);
                                DEFAULT_MULT_COUNT);
                }
                }
                hd_out(i,hd_info[i].sect,hd_info[i].sect,hd_info[i].head-1,
                hd_out(i,hd_info[i].sect,hd_info[i].sect,hd_info[i].head-1,
                        hd_info[i].cyl,WIN_SPECIFY,&reset_hd);
                        hd_info[i].cyl,WIN_SPECIFY,&reset_hd);
                if (reset)
                if (reset)
                        goto repeat;
                        goto repeat;
        } else
        } else
                hd_request();
                hd_request();
}
}
 
 
/*
/*
 * Ok, don't know what to do with the unexpected interrupts: on some machines
 * Ok, don't know what to do with the unexpected interrupts: on some machines
 * doing a reset and a retry seems to result in an eternal loop. Right now I
 * doing a reset and a retry seems to result in an eternal loop. Right now I
 * ignore it, and just set the timeout.
 * ignore it, and just set the timeout.
 *
 *
 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
 * "good", we just ignore the interrupt completely.
 * "good", we just ignore the interrupt completely.
 */
 */
void unexpected_hd_interrupt(void)
void unexpected_hd_interrupt(void)
{
{
        unsigned int stat = inb_p(HD_STATUS);
        unsigned int stat = inb_p(HD_STATUS);
 
 
        if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
        if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
                dump_status ("unexpected interrupt", stat);
                dump_status ("unexpected interrupt", stat);
                SET_TIMER;
                SET_TIMER;
        }
        }
}
}
 
 
/*
/*
 * bad_rw_intr() now tries to be a bit smarter and does things
 * bad_rw_intr() now tries to be a bit smarter and does things
 * according to the error returned by the controller.
 * according to the error returned by the controller.
 * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
 * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
 */
 */
static void bad_rw_intr(void)
static void bad_rw_intr(void)
{
{
        int dev;
        int dev;
 
 
        if (!CURRENT)
        if (!CURRENT)
                return;
                return;
        dev = DEVICE_NR(CURRENT->rq_dev);
        dev = DEVICE_NR(CURRENT->rq_dev);
        if (++CURRENT->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
        if (++CURRENT->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
                end_request(0);
                end_request(0);
                special_op[dev] = recalibrate[dev] = 1;
                special_op[dev] = recalibrate[dev] = 1;
        } else if (CURRENT->errors % RESET_FREQ == 0)
        } else if (CURRENT->errors % RESET_FREQ == 0)
                reset = 1;
                reset = 1;
        else if ((hd_error & TRK0_ERR) || CURRENT->errors % RECAL_FREQ == 0)
        else if ((hd_error & TRK0_ERR) || CURRENT->errors % RECAL_FREQ == 0)
                special_op[dev] = recalibrate[dev] = 1;
                special_op[dev] = recalibrate[dev] = 1;
        /* Otherwise just retry */
        /* Otherwise just retry */
}
}
 
 
static inline int wait_DRQ(void)
static inline int wait_DRQ(void)
{
{
        int retries = 100000, stat;
        int retries = 100000, stat;
 
 
        while (--retries > 0)
        while (--retries > 0)
                if ((stat = inb_p(HD_STATUS)) & DRQ_STAT)
                if ((stat = inb_p(HD_STATUS)) & DRQ_STAT)
                        return 0;
                        return 0;
        dump_status("wait_DRQ", stat);
        dump_status("wait_DRQ", stat);
        return -1;
        return -1;
}
}
 
 
static void read_intr(void)
static void read_intr(void)
{
{
        unsigned int dev = DEVICE_NR(CURRENT->rq_dev);
        unsigned int dev = DEVICE_NR(CURRENT->rq_dev);
        int i, retries = 100000, msect = mult_count[dev], nsect;
        int i, retries = 100000, msect = mult_count[dev], nsect;
 
 
        if (unmask_intr[dev])
        if (unmask_intr[dev])
                sti();                  /* permit other IRQs during xfer */
                sti();                  /* permit other IRQs during xfer */
        do {
        do {
                i = (unsigned) inb_p(HD_STATUS);
                i = (unsigned) inb_p(HD_STATUS);
                if (i & BUSY_STAT)
                if (i & BUSY_STAT)
                        continue;
                        continue;
                if (!OK_STATUS(i))
                if (!OK_STATUS(i))
                        break;
                        break;
                if (i & DRQ_STAT)
                if (i & DRQ_STAT)
                        goto ok_to_read;
                        goto ok_to_read;
        } while (--retries > 0);
        } while (--retries > 0);
        dump_status("read_intr", i);
        dump_status("read_intr", i);
        bad_rw_intr();
        bad_rw_intr();
        hd_request();
        hd_request();
        return;
        return;
ok_to_read:
ok_to_read:
        if (msect) {
        if (msect) {
                if ((nsect = CURRENT->current_nr_sectors) > msect)
                if ((nsect = CURRENT->current_nr_sectors) > msect)
                        nsect = msect;
                        nsect = msect;
                msect -= nsect;
                msect -= nsect;
        } else
        } else
                nsect = 1;
                nsect = 1;
        insw(HD_DATA,CURRENT->buffer,nsect<<8);
        insw(HD_DATA,CURRENT->buffer,nsect<<8);
        CURRENT->sector += nsect;
        CURRENT->sector += nsect;
        CURRENT->buffer += nsect<<9;
        CURRENT->buffer += nsect<<9;
        CURRENT->errors = 0;
        CURRENT->errors = 0;
        i = (CURRENT->nr_sectors -= nsect);
        i = (CURRENT->nr_sectors -= nsect);
 
 
#ifdef DEBUG
#ifdef DEBUG
        printk("hd%c: read: sectors(%ld-%ld), remaining=%ld, buffer=0x%08lx\n",
        printk("hd%c: read: sectors(%ld-%ld), remaining=%ld, buffer=0x%08lx\n",
                dev+'a', CURRENT->sector, CURRENT->sector+nsect,
                dev+'a', CURRENT->sector, CURRENT->sector+nsect,
                CURRENT->nr_sectors, (unsigned long) CURRENT->buffer+(nsect<<9));
                CURRENT->nr_sectors, (unsigned long) CURRENT->buffer+(nsect<<9));
#endif
#endif
        if ((CURRENT->current_nr_sectors -= nsect) <= 0)
        if ((CURRENT->current_nr_sectors -= nsect) <= 0)
                end_request(1);
                end_request(1);
        if (i > 0) {
        if (i > 0) {
                if (msect)
                if (msect)
                        goto ok_to_read;
                        goto ok_to_read;
                SET_INTR(&read_intr);
                SET_INTR(&read_intr);
                return;
                return;
        }
        }
        (void) inb_p(HD_STATUS);
        (void) inb_p(HD_STATUS);
#if (HD_DELAY > 0)
#if (HD_DELAY > 0)
        last_req = read_timer();
        last_req = read_timer();
#endif
#endif
        if (CURRENT)
        if (CURRENT)
                hd_request();
                hd_request();
        return;
        return;
}
}
 
 
static inline void multwrite (unsigned int dev)
static inline void multwrite (unsigned int dev)
{
{
        unsigned int mcount = mult_count[dev];
        unsigned int mcount = mult_count[dev];
 
 
        while (mcount--) {
        while (mcount--) {
                outsw(HD_DATA,WCURRENT.buffer,256);
                outsw(HD_DATA,WCURRENT.buffer,256);
                if (!--WCURRENT.nr_sectors)
                if (!--WCURRENT.nr_sectors)
                        return;
                        return;
                WCURRENT.buffer += 512;
                WCURRENT.buffer += 512;
                if (!--WCURRENT.current_nr_sectors) {
                if (!--WCURRENT.current_nr_sectors) {
                        WCURRENT.bh = WCURRENT.bh->b_reqnext;
                        WCURRENT.bh = WCURRENT.bh->b_reqnext;
                        if (WCURRENT.bh == NULL)
                        if (WCURRENT.bh == NULL)
                                panic("buffer list corrupted\n");
                                panic("buffer list corrupted\n");
                        WCURRENT.current_nr_sectors = WCURRENT.bh->b_size>>9;
                        WCURRENT.current_nr_sectors = WCURRENT.bh->b_size>>9;
                        WCURRENT.buffer             = WCURRENT.bh->b_data;
                        WCURRENT.buffer             = WCURRENT.bh->b_data;
                }
                }
        }
        }
}
}
 
 
static void multwrite_intr(void)
static void multwrite_intr(void)
{
{
        int i;
        int i;
        unsigned int dev = DEVICE_NR(WCURRENT.rq_dev);
        unsigned int dev = DEVICE_NR(WCURRENT.rq_dev);
 
 
        if (unmask_intr[dev])
        if (unmask_intr[dev])
                sti();
                sti();
        if (OK_STATUS(i=inb_p(HD_STATUS))) {
        if (OK_STATUS(i=inb_p(HD_STATUS))) {
                if (i & DRQ_STAT) {
                if (i & DRQ_STAT) {
                        if (WCURRENT.nr_sectors) {
                        if (WCURRENT.nr_sectors) {
                                multwrite(dev);
                                multwrite(dev);
                                SET_INTR(&multwrite_intr);
                                SET_INTR(&multwrite_intr);
                                return;
                                return;
                        }
                        }
                } else {
                } else {
                        if (!WCURRENT.nr_sectors) {     /* all done? */
                        if (!WCURRENT.nr_sectors) {     /* all done? */
                                for (i = CURRENT->nr_sectors; i > 0;){
                                for (i = CURRENT->nr_sectors; i > 0;){
                                        i -= CURRENT->current_nr_sectors;
                                        i -= CURRENT->current_nr_sectors;
                                        end_request(1);
                                        end_request(1);
                                }
                                }
#if (HD_DELAY > 0)
#if (HD_DELAY > 0)
                                last_req = read_timer();
                                last_req = read_timer();
#endif
#endif
                                if (CURRENT)
                                if (CURRENT)
                                        hd_request();
                                        hd_request();
                                return;
                                return;
                        }
                        }
                }
                }
        }
        }
        dump_status("multwrite_intr", i);
        dump_status("multwrite_intr", i);
        bad_rw_intr();
        bad_rw_intr();
        hd_request();
        hd_request();
}
}
 
 
static void write_intr(void)
static void write_intr(void)
{
{
        int i;
        int i;
        int retries = 100000;
        int retries = 100000;
 
 
        if (unmask_intr[DEVICE_NR(WCURRENT.rq_dev)])
        if (unmask_intr[DEVICE_NR(WCURRENT.rq_dev)])
                sti();
                sti();
        do {
        do {
                i = (unsigned) inb_p(HD_STATUS);
                i = (unsigned) inb_p(HD_STATUS);
                if (i & BUSY_STAT)
                if (i & BUSY_STAT)
                        continue;
                        continue;
                if (!OK_STATUS(i))
                if (!OK_STATUS(i))
                        break;
                        break;
                if ((CURRENT->nr_sectors <= 1) || (i & DRQ_STAT))
                if ((CURRENT->nr_sectors <= 1) || (i & DRQ_STAT))
                        goto ok_to_write;
                        goto ok_to_write;
        } while (--retries > 0);
        } while (--retries > 0);
        dump_status("write_intr", i);
        dump_status("write_intr", i);
        bad_rw_intr();
        bad_rw_intr();
        hd_request();
        hd_request();
        return;
        return;
ok_to_write:
ok_to_write:
        CURRENT->sector++;
        CURRENT->sector++;
        i = --CURRENT->nr_sectors;
        i = --CURRENT->nr_sectors;
        --CURRENT->current_nr_sectors;
        --CURRENT->current_nr_sectors;
        CURRENT->buffer += 512;
        CURRENT->buffer += 512;
        if (!i || (CURRENT->bh && !SUBSECTOR(i)))
        if (!i || (CURRENT->bh && !SUBSECTOR(i)))
                end_request(1);
                end_request(1);
        if (i > 0) {
        if (i > 0) {
                SET_INTR(&write_intr);
                SET_INTR(&write_intr);
                outsw(HD_DATA,CURRENT->buffer,256);
                outsw(HD_DATA,CURRENT->buffer,256);
                sti();
                sti();
        } else {
        } else {
#if (HD_DELAY > 0)
#if (HD_DELAY > 0)
                last_req = read_timer();
                last_req = read_timer();
#endif
#endif
                hd_request();
                hd_request();
        }
        }
        return;
        return;
}
}
 
 
static void recal_intr(void)
static void recal_intr(void)
{
{
        check_status();
        check_status();
#if (HD_DELAY > 0)
#if (HD_DELAY > 0)
        last_req = read_timer();
        last_req = read_timer();
#endif
#endif
        hd_request();
        hd_request();
}
}
 
 
/*
/*
 * This is another of the error-routines I don't know what to do with. The
 * This is another of the error-routines I don't know what to do with. The
 * best idea seems to just set reset, and start all over again.
 * best idea seems to just set reset, and start all over again.
 */
 */
static void hd_times_out(void)
static void hd_times_out(void)
{
{
        unsigned int dev;
        unsigned int dev;
 
 
        DEVICE_INTR = NULL;
        DEVICE_INTR = NULL;
        if (!CURRENT)
        if (!CURRENT)
                return;
                return;
        disable_irq(HD_IRQ);
        disable_irq(HD_IRQ);
        sti();
        sti();
        reset = 1;
        reset = 1;
        dev = DEVICE_NR(CURRENT->rq_dev);
        dev = DEVICE_NR(CURRENT->rq_dev);
        printk("hd%c: timeout\n", dev+'a');
        printk("hd%c: timeout\n", dev+'a');
        if (++CURRENT->errors >= MAX_ERRORS) {
        if (++CURRENT->errors >= MAX_ERRORS) {
#ifdef DEBUG
#ifdef DEBUG
                printk("hd%c: too many errors\n", dev+'a');
                printk("hd%c: too many errors\n", dev+'a');
#endif
#endif
                end_request(0);
                end_request(0);
        }
        }
        cli();
        cli();
        hd_request();
        hd_request();
        enable_irq(HD_IRQ);
        enable_irq(HD_IRQ);
}
}
 
 
int do_special_op (unsigned int dev)
int do_special_op (unsigned int dev)
{
{
        if (recalibrate[dev]) {
        if (recalibrate[dev]) {
                recalibrate[dev] = 0;
                recalibrate[dev] = 0;
                hd_out(dev,hd_info[dev].sect,0,0,0,WIN_RESTORE,&recal_intr);
                hd_out(dev,hd_info[dev].sect,0,0,0,WIN_RESTORE,&recal_intr);
                return reset;
                return reset;
        }
        }
        if (!identified[dev]) {
        if (!identified[dev]) {
                identified[dev]  = 1;
                identified[dev]  = 1;
                unmask_intr[dev] = DEFAULT_UNMASK_INTR;
                unmask_intr[dev] = DEFAULT_UNMASK_INTR;
                mult_req[dev]    = DEFAULT_MULT_COUNT;
                mult_req[dev]    = DEFAULT_MULT_COUNT;
                hd_out(dev,0,0,0,0,WIN_IDENTIFY,&identify_intr);
                hd_out(dev,0,0,0,0,WIN_IDENTIFY,&identify_intr);
                return reset;
                return reset;
        }
        }
        if (mult_req[dev] != mult_count[dev]) {
        if (mult_req[dev] != mult_count[dev]) {
                hd_out(dev,mult_req[dev],0,0,0,WIN_SETMULT,&set_multmode_intr);
                hd_out(dev,mult_req[dev],0,0,0,WIN_SETMULT,&set_multmode_intr);
                return reset;
                return reset;
        }
        }
        if (hd_info[dev].head > 16) {
        if (hd_info[dev].head > 16) {
                printk ("hd%c: cannot handle device with more than 16 heads - giving up\n", dev+'a');
                printk ("hd%c: cannot handle device with more than 16 heads - giving up\n", dev+'a');
                end_request(0);
                end_request(0);
        }
        }
        special_op[dev] = 0;
        special_op[dev] = 0;
        return 1;
        return 1;
}
}
 
 
/*
/*
 * The driver enables interrupts as much as possible.  In order to do this,
 * The driver enables interrupts as much as possible.  In order to do this,
 * (a) the device-interrupt is disabled before entering hd_request(),
 * (a) the device-interrupt is disabled before entering hd_request(),
 * and (b) the timeout-interrupt is disabled before the sti().
 * and (b) the timeout-interrupt is disabled before the sti().
 *
 *
 * Interrupts are still masked (by default) whenever we are exchanging
 * Interrupts are still masked (by default) whenever we are exchanging
 * data/cmds with a drive, because some drives seem to have very poor
 * data/cmds with a drive, because some drives seem to have very poor
 * tolerance for latency during I/O.  For devices which don't suffer from
 * tolerance for latency during I/O.  For devices which don't suffer from
 * that problem (most don't), the unmask_intr[] flag can be set to unmask
 * that problem (most don't), the unmask_intr[] flag can be set to unmask
 * other interrupts during data/cmd transfers (by defining DEFAULT_UNMASK_INTR
 * other interrupts during data/cmd transfers (by defining DEFAULT_UNMASK_INTR
 * to 1, or by using "hdparm -u1 /dev/hd?" from the shell).
 * to 1, or by using "hdparm -u1 /dev/hd?" from the shell).
 */
 */
static void hd_request(void)
static void hd_request(void)
{
{
        unsigned int dev, block, nsect, sec, track, head, cyl;
        unsigned int dev, block, nsect, sec, track, head, cyl;
 
 
        if (CURRENT && CURRENT->rq_status == RQ_INACTIVE) return;
        if (CURRENT && CURRENT->rq_status == RQ_INACTIVE) return;
        if (DEVICE_INTR)
        if (DEVICE_INTR)
                return;
                return;
repeat:
repeat:
        timer_active &= ~(1<<HD_TIMER);
        timer_active &= ~(1<<HD_TIMER);
        sti();
        sti();
        INIT_REQUEST;
        INIT_REQUEST;
        if (reset) {
        if (reset) {
                cli();
                cli();
                reset_hd();
                reset_hd();
                return;
                return;
        }
        }
        dev = MINOR(CURRENT->rq_dev);
        dev = MINOR(CURRENT->rq_dev);
        block = CURRENT->sector;
        block = CURRENT->sector;
        nsect = CURRENT->nr_sectors;
        nsect = CURRENT->nr_sectors;
        if (dev >= (NR_HD<<6) || block >= hd[dev].nr_sects || ((block+nsect) > hd[dev].nr_sects)) {
        if (dev >= (NR_HD<<6) || block >= hd[dev].nr_sects || ((block+nsect) > hd[dev].nr_sects)) {
#ifdef DEBUG
#ifdef DEBUG
                if (dev >= (NR_HD<<6))
                if (dev >= (NR_HD<<6))
                        printk("hd: bad minor number: device=%s\n",
                        printk("hd: bad minor number: device=%s\n",
                               kdevname(CURRENT->rq_dev));
                               kdevname(CURRENT->rq_dev));
                else
                else
                        printk("hd%c: bad access: block=%d, count=%d\n",
                        printk("hd%c: bad access: block=%d, count=%d\n",
                                (MINOR(CURRENT->rq_dev)>>6)+'a', block, nsect);
                                (MINOR(CURRENT->rq_dev)>>6)+'a', block, nsect);
#endif
#endif
                end_request(0);
                end_request(0);
                goto repeat;
                goto repeat;
        }
        }
        block += hd[dev].start_sect;
        block += hd[dev].start_sect;
        dev >>= 6;
        dev >>= 6;
        if (special_op[dev]) {
        if (special_op[dev]) {
                if (do_special_op(dev))
                if (do_special_op(dev))
                        goto repeat;
                        goto repeat;
                return;
                return;
        }
        }
        sec   = block % hd_info[dev].sect + 1;
        sec   = block % hd_info[dev].sect + 1;
        track = block / hd_info[dev].sect;
        track = block / hd_info[dev].sect;
        head  = track % hd_info[dev].head;
        head  = track % hd_info[dev].head;
        cyl   = track / hd_info[dev].head;
        cyl   = track / hd_info[dev].head;
#ifdef DEBUG
#ifdef DEBUG
        printk("hd%c: %sing: CHS=%d/%d/%d, sectors=%d, buffer=0x%08lx\n",
        printk("hd%c: %sing: CHS=%d/%d/%d, sectors=%d, buffer=0x%08lx\n",
                dev+'a', (CURRENT->cmd == READ)?"read":"writ",
                dev+'a', (CURRENT->cmd == READ)?"read":"writ",
                cyl, head, sec, nsect, (unsigned long) CURRENT->buffer);
                cyl, head, sec, nsect, (unsigned long) CURRENT->buffer);
#endif
#endif
        if (!unmask_intr[dev])
        if (!unmask_intr[dev])
                cli();
                cli();
        if (CURRENT->cmd == READ) {
        if (CURRENT->cmd == READ) {
                unsigned int cmd = mult_count[dev] > 1 ? WIN_MULTREAD : WIN_READ;
                unsigned int cmd = mult_count[dev] > 1 ? WIN_MULTREAD : WIN_READ;
                hd_out(dev,nsect,sec,head,cyl,cmd,&read_intr);
                hd_out(dev,nsect,sec,head,cyl,cmd,&read_intr);
                if (reset)
                if (reset)
                        goto repeat;
                        goto repeat;
                return;
                return;
        }
        }
        if (CURRENT->cmd == WRITE) {
        if (CURRENT->cmd == WRITE) {
                if (mult_count[dev])
                if (mult_count[dev])
                        hd_out(dev,nsect,sec,head,cyl,WIN_MULTWRITE,&multwrite_intr);
                        hd_out(dev,nsect,sec,head,cyl,WIN_MULTWRITE,&multwrite_intr);
                else
                else
                        hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr);
                        hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr);
                if (reset)
                if (reset)
                        goto repeat;
                        goto repeat;
                if (wait_DRQ()) {
                if (wait_DRQ()) {
                        bad_rw_intr();
                        bad_rw_intr();
                        goto repeat;
                        goto repeat;
                }
                }
                if (mult_count[dev]) {
                if (mult_count[dev]) {
                        WCURRENT = *CURRENT;
                        WCURRENT = *CURRENT;
                        multwrite(dev);
                        multwrite(dev);
                } else
                } else
                        outsw(HD_DATA,CURRENT->buffer,256);
                        outsw(HD_DATA,CURRENT->buffer,256);
                return;
                return;
        }
        }
        panic("unknown hd-command");
        panic("unknown hd-command");
}
}
 
 
static void do_hd_request (void)
static void do_hd_request (void)
{
{
        disable_irq(HD_IRQ);
        disable_irq(HD_IRQ);
        hd_request();
        hd_request();
        enable_irq(HD_IRQ);
        enable_irq(HD_IRQ);
}
}
 
 
static int hd_ioctl(struct inode * inode, struct file * file,
static int hd_ioctl(struct inode * inode, struct file * file,
        unsigned int cmd, unsigned long arg)
        unsigned int cmd, unsigned long arg)
{
{
        struct hd_geometry *loc = (struct hd_geometry *) arg;
        struct hd_geometry *loc = (struct hd_geometry *) arg;
        int dev, err;
        int dev, err;
        unsigned long flags;
        unsigned long flags;
 
 
        if ((!inode) || !(inode->i_rdev))
        if ((!inode) || !(inode->i_rdev))
                return -EINVAL;
                return -EINVAL;
        dev = DEVICE_NR(inode->i_rdev);
        dev = DEVICE_NR(inode->i_rdev);
        if (dev >= NR_HD)
        if (dev >= NR_HD)
                return -EINVAL;
                return -EINVAL;
        switch (cmd) {
        switch (cmd) {
                case HDIO_GETGEO:
                case HDIO_GETGEO:
                        if (!loc)  return -EINVAL;
                        if (!loc)  return -EINVAL;
                        err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
                        err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
                        if (err)
                        if (err)
                                return err;
                                return err;
                        put_user(bios_info[dev].head,
                        put_user(bios_info[dev].head,
                                (char *) &loc->heads);
                                (char *) &loc->heads);
                        put_user(bios_info[dev].sect,
                        put_user(bios_info[dev].sect,
                                (char *) &loc->sectors);
                                (char *) &loc->sectors);
                        put_user(bios_info[dev].cyl,
                        put_user(bios_info[dev].cyl,
                                (short *) &loc->cylinders);
                                (short *) &loc->cylinders);
                        put_user(hd[MINOR(inode->i_rdev)].start_sect,
                        put_user(hd[MINOR(inode->i_rdev)].start_sect,
                                (long *) &loc->start);
                                (long *) &loc->start);
                        return 0;
                        return 0;
                case BLKRASET:
                case BLKRASET:
                        if(!suser())  return -EACCES;
                        if(!suser())  return -EACCES;
                        if(arg > 0xff) return -EINVAL;
                        if(arg > 0xff) return -EINVAL;
                        read_ahead[MAJOR(inode->i_rdev)] = arg;
                        read_ahead[MAJOR(inode->i_rdev)] = arg;
                        return 0;
                        return 0;
                case BLKRAGET:
                case BLKRAGET:
                        if (!arg)  return -EINVAL;
                        if (!arg)  return -EINVAL;
                        err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
                        err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
                        if (err)
                        if (err)
                                return err;
                                return err;
                        put_user(read_ahead[MAJOR(inode->i_rdev)],(long *) arg);
                        put_user(read_ahead[MAJOR(inode->i_rdev)],(long *) arg);
                        return 0;
                        return 0;
                case BLKGETSIZE:   /* Return device size */
                case BLKGETSIZE:   /* Return device size */
                        if (!arg)  return -EINVAL;
                        if (!arg)  return -EINVAL;
                        err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
                        err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
                        if (err)
                        if (err)
                                return err;
                                return err;
                        put_user(hd[MINOR(inode->i_rdev)].nr_sects, (long *) arg);
                        put_user(hd[MINOR(inode->i_rdev)].nr_sects, (long *) arg);
                        return 0;
                        return 0;
                case BLKFLSBUF:
                case BLKFLSBUF:
                        if(!suser())  return -EACCES;
                        if(!suser())  return -EACCES;
                        fsync_dev(inode->i_rdev);
                        fsync_dev(inode->i_rdev);
                        invalidate_buffers(inode->i_rdev);
                        invalidate_buffers(inode->i_rdev);
                        return 0;
                        return 0;
 
 
                case BLKRRPART: /* Re-read partition tables */
                case BLKRRPART: /* Re-read partition tables */
                        return revalidate_hddisk(inode->i_rdev, 1);
                        return revalidate_hddisk(inode->i_rdev, 1);
 
 
                case HDIO_SET_UNMASKINTR:
                case HDIO_SET_UNMASKINTR:
                        if (!suser()) return -EACCES;
                        if (!suser()) return -EACCES;
                        if ((arg > 1) || (MINOR(inode->i_rdev) & 0x3F))
                        if ((arg > 1) || (MINOR(inode->i_rdev) & 0x3F))
                                return -EINVAL;
                                return -EINVAL;
                        unmask_intr[dev] = arg;
                        unmask_intr[dev] = arg;
                        return 0;
                        return 0;
 
 
                case HDIO_GET_UNMASKINTR:
                case HDIO_GET_UNMASKINTR:
                        if (!arg)  return -EINVAL;
                        if (!arg)  return -EINVAL;
                        err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
                        err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
                        if (err)
                        if (err)
                                return err;
                                return err;
                        put_user(unmask_intr[dev], (long *) arg);
                        put_user(unmask_intr[dev], (long *) arg);
                        return 0;
                        return 0;
 
 
                case HDIO_GET_MULTCOUNT:
                case HDIO_GET_MULTCOUNT:
                        if (!arg)  return -EINVAL;
                        if (!arg)  return -EINVAL;
                        err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
                        err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
                        if (err)
                        if (err)
                                return err;
                                return err;
                        put_user(mult_count[dev], (long *) arg);
                        put_user(mult_count[dev], (long *) arg);
                        return 0;
                        return 0;
 
 
                case HDIO_SET_MULTCOUNT:
                case HDIO_SET_MULTCOUNT:
                        if (!suser()) return -EACCES;
                        if (!suser()) return -EACCES;
                        if (MINOR(inode->i_rdev) & 0x3F) return -EINVAL;
                        if (MINOR(inode->i_rdev) & 0x3F) return -EINVAL;
                        save_flags(flags);
                        save_flags(flags);
                        cli();  /* a prior request might still be in progress */
                        cli();  /* a prior request might still be in progress */
                        if (arg > max_mult[dev])
                        if (arg > max_mult[dev])
                                err = -EINVAL;  /* out of range for device */
                                err = -EINVAL;  /* out of range for device */
                        else if (mult_req[dev] != mult_count[dev]) {
                        else if (mult_req[dev] != mult_count[dev]) {
                                special_op[dev] = 1;
                                special_op[dev] = 1;
                                err = -EBUSY;   /* busy, try again */
                                err = -EBUSY;   /* busy, try again */
                        } else {
                        } else {
                                mult_req[dev] = arg;
                                mult_req[dev] = arg;
                                special_op[dev] = 1;
                                special_op[dev] = 1;
                                err = 0;
                                err = 0;
                        }
                        }
                        restore_flags(flags);
                        restore_flags(flags);
                        return err;
                        return err;
 
 
                case HDIO_GET_IDENTITY:
                case HDIO_GET_IDENTITY:
                        if (!arg)  return -EINVAL;
                        if (!arg)  return -EINVAL;
                        if (MINOR(inode->i_rdev) & 0x3F) return -EINVAL;
                        if (MINOR(inode->i_rdev) & 0x3F) return -EINVAL;
                        if (hd_ident_info[dev] == NULL)  return -ENOMSG;
                        if (hd_ident_info[dev] == NULL)  return -ENOMSG;
                        err = verify_area(VERIFY_WRITE, (char *) arg, sizeof(struct hd_driveid));
                        err = verify_area(VERIFY_WRITE, (char *) arg, sizeof(struct hd_driveid));
                        if (err)
                        if (err)
                                return err;
                                return err;
                        memcpy_tofs((char *)arg, (char *) hd_ident_info[dev], sizeof(struct hd_driveid));
                        memcpy_tofs((char *)arg, (char *) hd_ident_info[dev], sizeof(struct hd_driveid));
                        return 0;
                        return 0;
 
 
                RO_IOCTLS(inode->i_rdev,arg);
                RO_IOCTLS(inode->i_rdev,arg);
                default:
                default:
                        return -EINVAL;
                        return -EINVAL;
        }
        }
}
}
 
 
static int hd_open(struct inode * inode, struct file * filp)
static int hd_open(struct inode * inode, struct file * filp)
{
{
        int target;
        int target;
        target =  DEVICE_NR(inode->i_rdev);
        target =  DEVICE_NR(inode->i_rdev);
 
 
        if (target >= NR_HD)
        if (target >= NR_HD)
                return -ENODEV;
                return -ENODEV;
        while (busy[target])
        while (busy[target])
                sleep_on(&busy_wait);
                sleep_on(&busy_wait);
        access_count[target]++;
        access_count[target]++;
        return 0;
        return 0;
}
}
 
 
/*
/*
 * Releasing a block device means we sync() it, so that it can safely
 * Releasing a block device means we sync() it, so that it can safely
 * be forgotten about...
 * be forgotten about...
 */
 */
static void hd_release(struct inode * inode, struct file * file)
static void hd_release(struct inode * inode, struct file * file)
{
{
        int target;
        int target;
        sync_dev(inode->i_rdev);
        sync_dev(inode->i_rdev);
 
 
        target =  DEVICE_NR(inode->i_rdev);
        target =  DEVICE_NR(inode->i_rdev);
        access_count[target]--;
        access_count[target]--;
 
 
}
}
 
 
static void hd_geninit(struct gendisk *);
static void hd_geninit(struct gendisk *);
 
 
static struct gendisk hd_gendisk = {
static struct gendisk hd_gendisk = {
        MAJOR_NR,       /* Major number */
        MAJOR_NR,       /* Major number */
        "hd",           /* Major name */
        "hd",           /* Major name */
        6,              /* Bits to shift to get real from partition */
        6,              /* Bits to shift to get real from partition */
        1 << 6,         /* Number of partitions per real */
        1 << 6,         /* Number of partitions per real */
        MAX_HD,         /* maximum number of real */
        MAX_HD,         /* maximum number of real */
        hd_geninit,     /* init function */
        hd_geninit,     /* init function */
        hd,             /* hd struct */
        hd,             /* hd struct */
        hd_sizes,       /* block sizes */
        hd_sizes,       /* block sizes */
        0,               /* number */
        0,               /* number */
        (void *) bios_info,     /* internal */
        (void *) bios_info,     /* internal */
        NULL            /* next */
        NULL            /* next */
};
};
 
 
static void hd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
static void hd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
{
        void (*handler)(void) = DEVICE_INTR;
        void (*handler)(void) = DEVICE_INTR;
 
 
        DEVICE_INTR = NULL;
        DEVICE_INTR = NULL;
        timer_active &= ~(1<<HD_TIMER);
        timer_active &= ~(1<<HD_TIMER);
        if (!handler)
        if (!handler)
                handler = unexpected_hd_interrupt;
                handler = unexpected_hd_interrupt;
        handler();
        handler();
        sti();
        sti();
}
}
 
 
/*
/*
 * Since we find out the physical drive geometry, we don't touch that.
 * Since we find out the physical drive geometry, we don't touch that.
 * We only alter the logical disk geometry that is passed to user programs.
 * We only alter the logical disk geometry that is passed to user programs.
 * [as per PC Linux].
 * [as per PC Linux].
 */
 */
void hd_set_geometry (kdev_t dev, unsigned char secspertrack, unsigned char heads,
void hd_set_geometry (kdev_t dev, unsigned char secspertrack, unsigned char heads,
                unsigned long discsize, unsigned int secsize)
                unsigned long discsize, unsigned int secsize)
{
{
    int minor = MINOR(dev);
    int minor = MINOR(dev);
    int drv = minor >> 6;
    int drv = minor >> 6;
 
 
    if (bios_info[drv].cyl == 1) {
    if (bios_info[drv].cyl == 1) {
        bios_info[drv].cyl = discsize / (secspertrack * heads * secsize);
        bios_info[drv].cyl = discsize / (secspertrack * heads * secsize);
        bios_info[drv].head = heads;
        bios_info[drv].head = heads;
        bios_info[drv].wpcom = -1;
        bios_info[drv].wpcom = -1;
        bios_info[drv].ctl = 8;
        bios_info[drv].ctl = 8;
        bios_info[drv].lzone = bios_info[drv].cyl - 1;
        bios_info[drv].lzone = bios_info[drv].cyl - 1;
        bios_info[drv].sect = secspertrack;
        bios_info[drv].sect = secspertrack;
    }
    }
    hd[minor].start_sect = 0;
    hd[minor].start_sect = 0;
    hd[minor].nr_sects = discsize / secsize;
    hd[minor].nr_sects = discsize / secsize;
}
}
 
 
/*
/*
 * This is the harddisk IRQ description. The SA_INTERRUPT in sa_flags
 * This is the harddisk IRQ description. The SA_INTERRUPT in sa_flags
 * means we run the IRQ-handler with interrupts disabled: this is bad for
 * means we run the IRQ-handler with interrupts disabled: this is bad for
 * interrupt latency, but anything else has led to problems on some
 * interrupt latency, but anything else has led to problems on some
 * machines...
 * machines...
 *
 *
 * We enable interrupts in some of the routines after making sure it's
 * We enable interrupts in some of the routines after making sure it's
 * safe.
 * safe.
 */
 */
static void hd_geninit(struct gendisk *ignored)
static void hd_geninit(struct gendisk *ignored)
{
{
        int i;
        int i;
 
 
        if (!NR_HD) {
        if (!NR_HD) {
                int drive;
                int drive;
                extern int number_ide_drives;
                extern int number_ide_drives;
                /*
                /*
                 * Default settings
                 * Default settings
                 *
                 *
                 * If we don't know anything about the drive, then set it
                 * If we don't know anything about the drive, then set it
                 * so that we have enough to read the boot sector of the
                 * so that we have enough to read the boot sector of the
                 * ADFS drive.  This means that you *MUST* specify the
                 * ADFS drive.  This means that you *MUST* specify the
                 * drive parameters of *all* drives if you have one IDE
                 * drive parameters of *all* drives if you have one IDE
                 * drive that is not ADFS formatted.
                 * drive that is not ADFS formatted.
                 */
                 */
                for (drive=0 ; drive<2 ; drive++) {
                for (drive=0 ; drive<2 ; drive++) {
                        bios_info[drive].cyl   = hd_info[drive].cyl = 1;
                        bios_info[drive].cyl   = hd_info[drive].cyl = 1;
                        bios_info[drive].head  = hd_info[drive].head = 1;
                        bios_info[drive].head  = hd_info[drive].head = 1;
                        bios_info[drive].wpcom = hd_info[drive].wpcom = -1;
                        bios_info[drive].wpcom = hd_info[drive].wpcom = -1;
                        bios_info[drive].ctl   = hd_info[drive].ctl = 8;
                        bios_info[drive].ctl   = hd_info[drive].ctl = 8;
                        bios_info[drive].lzone = hd_info[drive].lzone = 1;
                        bios_info[drive].lzone = hd_info[drive].lzone = 1;
                        bios_info[drive].sect  = hd_info[drive].sect = 17;
                        bios_info[drive].sect  = hd_info[drive].sect = 17;
                }
                }
 
 
                /*
                /*
                 * We only set this to the one that the host OS gave us
                 * We only set this to the one that the host OS gave us
                 * if the user has not defined any types.
                 * if the user has not defined any types.
                 */
                 */
                NR_HD = number_ide_drives;
                NR_HD = number_ide_drives;
        }
        }
 
 
        i = NR_HD;
        i = NR_HD;
        while (i-- > 0) {
        while (i-- > 0) {
                /*
                /*
                 * The newer E-IDE BIOSs handle drives larger than 1024
                 * The newer E-IDE BIOSs handle drives larger than 1024
                 * cylinders by increasing the number of logical heads
                 * cylinders by increasing the number of logical heads
                 * to keep the number of logical cylinders below the
                 * to keep the number of logical cylinders below the
                 * sacred INT13 limit of 1024 (10 bits).  If that is
                 * sacred INT13 limit of 1024 (10 bits).  If that is
                 * what's happening here, we'll find out and correct
                 * what's happening here, we'll find out and correct
                 * it later when "identifying" the drive.
                 * it later when "identifying" the drive.
                 */
                 */
                hd[i<<6].nr_sects = bios_info[i].head *
                hd[i<<6].nr_sects = bios_info[i].head *
                                bios_info[i].sect * bios_info[i].cyl;
                                bios_info[i].sect * bios_info[i].cyl;
                hd_ident_info[i] = (struct hd_driveid *) kmalloc(512,GFP_KERNEL);
                hd_ident_info[i] = (struct hd_driveid *) kmalloc(512,GFP_KERNEL);
                special_op[i] = 1;
                special_op[i] = 1;
        }
        }
        if (NR_HD) {
        if (NR_HD) {
                if (request_irq(HD_IRQ, hd_interrupt, SA_INTERRUPT, "hd", NULL)) {
                if (request_irq(HD_IRQ, hd_interrupt, SA_INTERRUPT, "hd", NULL)) {
                        printk("hd: unable to get IRQ%d for the harddisk driver\n",HD_IRQ);
                        printk("hd: unable to get IRQ%d for the harddisk driver\n",HD_IRQ);
                        NR_HD = 0;
                        NR_HD = 0;
                } else {
                } else {
                        request_region(HD_DATA, 8, "hd");
                        request_region(HD_DATA, 8, "hd");
                        request_region(HD_CMD, 1, "hd(cmd)");
                        request_region(HD_CMD, 1, "hd(cmd)");
                }
                }
        }
        }
        hd_gendisk.nr_real = NR_HD;
        hd_gendisk.nr_real = NR_HD;
 
 
        for (i = 0; i < (MAX_HD << 6); i++) {
        for (i = 0; i < (MAX_HD << 6); i++) {
                hd_blocksizes[i] = 1024;
                hd_blocksizes[i] = 1024;
                hd_hardsectsizes[i] = 512;
                hd_hardsectsizes[i] = 512;
        }
        }
        blksize_size[MAJOR_NR] = hd_blocksizes;
        blksize_size[MAJOR_NR] = hd_blocksizes;
        hardsect_size[MAJOR_NR] = hd_hardsectsizes;
        hardsect_size[MAJOR_NR] = hd_hardsectsizes;
}
}
 
 
static struct file_operations hd_fops = {
static struct file_operations hd_fops = {
        NULL,                   /* lseek - default */
        NULL,                   /* lseek - default */
        block_read,             /* read - general block-dev read */
        block_read,             /* read - general block-dev read */
        block_write,            /* write - general block-dev write */
        block_write,            /* write - general block-dev write */
        NULL,                   /* readdir - bad */
        NULL,                   /* readdir - bad */
        NULL,                   /* select */
        NULL,                   /* select */
        hd_ioctl,               /* ioctl */
        hd_ioctl,               /* ioctl */
        NULL,                   /* mmap */
        NULL,                   /* mmap */
        hd_open,                /* open */
        hd_open,                /* open */
        hd_release,             /* release */
        hd_release,             /* release */
        block_fsync             /* fsync */
        block_fsync             /* fsync */
};
};
 
 
int hd_init(void)
int hd_init(void)
{
{
        if (register_blkdev(MAJOR_NR,"hd",&hd_fops)) {
        if (register_blkdev(MAJOR_NR,"hd",&hd_fops)) {
                printk("hd: unable to get major %d for harddisk\n",MAJOR_NR);
                printk("hd: unable to get major %d for harddisk\n",MAJOR_NR);
                return -1;
                return -1;
        }
        }
        blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
        blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
        read_ahead[MAJOR_NR] = 8;               /* 8 sector (4kB) read-ahead */
        read_ahead[MAJOR_NR] = 8;               /* 8 sector (4kB) read-ahead */
        hd_gendisk.next = gendisk_head;
        hd_gendisk.next = gendisk_head;
        gendisk_head = &hd_gendisk;
        gendisk_head = &hd_gendisk;
        timer_table[HD_TIMER].fn = hd_times_out;
        timer_table[HD_TIMER].fn = hd_times_out;
        return 0;
        return 0;
}
}
 
 
#define DEVICE_BUSY busy[target]
#define DEVICE_BUSY busy[target]
#define USAGE access_count[target]
#define USAGE access_count[target]
#define CAPACITY (bios_info[target].head*bios_info[target].sect*bios_info[target].cyl)
#define CAPACITY (bios_info[target].head*bios_info[target].sect*bios_info[target].cyl)
/* We assume that the the bios parameters do not change, so the disk capacity
/* We assume that the the bios parameters do not change, so the disk capacity
   will not change */
   will not change */
#undef MAYBE_REINIT
#undef MAYBE_REINIT
#define GENDISK_STRUCT hd_gendisk
#define GENDISK_STRUCT hd_gendisk
 
 
/*
/*
 * This routine is called to flush all partitions and partition tables
 * This routine is called to flush all partitions and partition tables
 * for a changed scsi disk, and then re-read the new partition table.
 * for a changed scsi disk, and then re-read the new partition table.
 * If we are revalidating a disk because of a media change, then we
 * If we are revalidating a disk because of a media change, then we
 * enter with usage == 0.  If we are using an ioctl, we automatically have
 * enter with usage == 0.  If we are using an ioctl, we automatically have
 * usage == 1 (we need an open channel to use an ioctl :-), so this
 * usage == 1 (we need an open channel to use an ioctl :-), so this
 * is our limit.
 * is our limit.
 */
 */
static int revalidate_hddisk(kdev_t dev, int maxusage)
static int revalidate_hddisk(kdev_t dev, int maxusage)
{
{
        int target;
        int target;
        struct gendisk * gdev;
        struct gendisk * gdev;
        int max_p;
        int max_p;
        int start;
        int start;
        int i;
        int i;
        long flags;
        long flags;
 
 
        target = DEVICE_NR(dev);
        target = DEVICE_NR(dev);
        gdev = &GENDISK_STRUCT;
        gdev = &GENDISK_STRUCT;
 
 
        save_flags_cli (flags);
        save_flags_cli (flags);
        if (DEVICE_BUSY || USAGE > maxusage) {
        if (DEVICE_BUSY || USAGE > maxusage) {
                restore_flags(flags);
                restore_flags(flags);
                return -EBUSY;
                return -EBUSY;
        };
        };
        DEVICE_BUSY = 1;
        DEVICE_BUSY = 1;
        restore_flags(flags);
        restore_flags(flags);
 
 
        max_p = gdev->max_p;
        max_p = gdev->max_p;
        start = target << gdev->minor_shift;
        start = target << gdev->minor_shift;
 
 
        for (i=max_p - 1; i >=0 ; i--) {
        for (i=max_p - 1; i >=0 ; i--) {
                int minor = start + i;
                int minor = start + i;
                kdev_t devi = MKDEV(MAJOR_NR, minor);
                kdev_t devi = MKDEV(MAJOR_NR, minor);
                sync_dev(devi);
                sync_dev(devi);
                invalidate_inodes(devi);
                invalidate_inodes(devi);
                invalidate_buffers(devi);
                invalidate_buffers(devi);
                gdev->part[minor].start_sect = 0;
                gdev->part[minor].start_sect = 0;
                gdev->part[minor].nr_sects = 0;
                gdev->part[minor].nr_sects = 0;
        };
        };
 
 
#ifdef MAYBE_REINIT
#ifdef MAYBE_REINIT
        MAYBE_REINIT;
        MAYBE_REINIT;
#endif
#endif
 
 
        gdev->part[start].nr_sects = CAPACITY;
        gdev->part[start].nr_sects = CAPACITY;
        resetup_one_dev(gdev, target);
        resetup_one_dev(gdev, target);
 
 
        DEVICE_BUSY = 0;
        DEVICE_BUSY = 0;
        wake_up(&busy_wait);
        wake_up(&busy_wait);
        return 0;
        return 0;
}
}
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.