--- linux/kernel/blk_drv/hd.c 2018/04/24 18:06:41 1.1.1.6 +++ linux/kernel/blk_drv/hd.c 2018/04/24 18:15:22 1.1.1.10 @@ -1,7 +1,7 @@ /* * linux/kernel/hd.c * - * (C) 1991 Linus Torvalds + * Copyright (C) 1991, 1992 Linus Torvalds */ /* @@ -16,14 +16,21 @@ * in the early extended-partition checks and added DM partitions */ -#include - #include +#ifdef CONFIG_BLK_DEV_HD + +#define HD_IRQ 14 + +#include +#include #include #include #include #include #include +#include + +#define REALLY_SLOW_IO #include #include #include @@ -37,6 +44,8 @@ static inline unsigned char CMOS_READ(un return inb_p(0x71); } +#define HD_DELAY 0 + /* Max read/write errors/sector */ #define MAX_ERRORS 7 #define MAX_HD 2 @@ -44,9 +53,14 @@ static inline unsigned char CMOS_READ(un static void recal_intr(void); static void bad_rw_intr(void); -static int recalibrate = 0; +static char recalibrate[ MAX_HD ] = { 0, }; + static int reset = 0; +#if (HD_DELAY > 0) +unsigned long last_req, read_timer(); +#endif + /* * This struct defines the HD's and their types. */ @@ -61,11 +75,7 @@ struct hd_i_struct hd_info[] = { {0,0,0, static int NR_HD = 0; #endif -static struct hd_struct { - long start_sect; - long nr_sects; -} hd[MAX_HD<<6]={{0,0},}; - +static struct hd_struct hd[MAX_HD<<6]={{0,0},}; static int hd_sizes[MAX_HD<<6] = {0, }; #define port_read(port,buf,nr) \ @@ -74,207 +84,21 @@ __asm__("cld;rep;insw"::"d" (port),"D" ( #define port_write(port,buf,nr) \ __asm__("cld;rep;outsw"::"d" (port),"S" (buf),"c" (nr):"cx","si") -extern void hd_interrupt(void); -extern void rd_load(void); - -static unsigned int current_minor; - -/* - * Create devices for each logical partition in an extended partition. - * The logical partitions form a linked list, with each entry being - * a partition table with two entries. The first entry - * is the real data partition (with a start relative to the partition - * table start). The second is a pointer to the next logical partition - * (with a start relative to the entire extended partition). - * We do not create a Linux partition for the partition tables, but - * only for the actual data partitions. - */ -static void extended_partition(unsigned int dev) +#if (HD_DELAY > 0) +unsigned long read_timer(void) { - struct buffer_head *bh; - struct partition *p; - unsigned long first_sector, this_sector; - - first_sector = hd[MINOR(dev)].start_sect; - this_sector = first_sector; - - while (1) { - if ((current_minor & 0x3f) >= 60) - return; - if (!(bh = bread(dev,0))) { - printk("Unable to read partition table of device %04x\n",dev); - return; - } - /* - * This block is from a device that we're about to stomp on. - * So make sure nobody thinks this block is usable. - */ - bh->b_dirt=0; - bh->b_uptodate=0; - if (*(unsigned short *) (bh->b_data+510) == 0xAA55) { - p = 0x1BE + (void *)bh->b_data; - /* - * Process the first entry, which should be the real - * data partition. - */ - if (p->sys_ind == EXTENDED_PARTITION || - !(hd[current_minor].nr_sects = p->nr_sects)) - goto done; /* shouldn't happen */ - hd[current_minor].start_sect = this_sector + p->start_sect; - printk(" Logical part %d start %d size %d end %d\n\r", - current_minor, hd[current_minor].start_sect, - hd[current_minor].nr_sects, - hd[current_minor].start_sect + - hd[current_minor].nr_sects); - current_minor++; - p++; - /* - * Process the second entry, which should be a link - * to the next logical partition. Create a minor - * for this just long enough to get the next partition - * table. The minor will be reused for the real - * data partition. - */ - if (p->sys_ind != EXTENDED_PARTITION || - !(hd[current_minor].nr_sects = p->nr_sects)) - goto done; /* no more logicals in this partition */ - hd[current_minor].start_sect = first_sector + p->start_sect; - this_sector = first_sector + p->start_sect; - dev = 0x0300 | current_minor; - brelse(bh); - } else - goto done; - } -done: - brelse(bh); -} - -static void check_partition(unsigned int dev) -{ - int i, minor = current_minor; - struct buffer_head *bh; - struct partition *p; - unsigned long first_sector; - - first_sector = hd[MINOR(dev)].start_sect; - if (!(bh = bread(dev,0))) { - printk("Unable to read partition table of device %04x\n",dev); - return; - } - printk("Drive %d:\n\r",minor >> 6); - current_minor += 4; /* first "extra" minor */ - if (*(unsigned short *) (bh->b_data+510) == 0xAA55) { - p = 0x1BE + (void *)bh->b_data; - for (i=1 ; i<=4 ; minor++,i++,p++) { - if (!(hd[minor].nr_sects = p->nr_sects)) - continue; - hd[minor].start_sect = first_sector + p->start_sect; - printk(" part %d start %d size %d end %d \n\r", i, - hd[minor].start_sect, hd[minor].nr_sects, - hd[minor].start_sect + hd[minor].nr_sects); - if ((current_minor & 0x3f) >= 60) - continue; - if (p->sys_ind == EXTENDED_PARTITION) { - extended_partition(0x0300 | minor); - } - } - /* - * check for Disk Manager partition table - */ - if (*(unsigned short *) (bh->b_data+0xfc) == 0x55AA) { - p = 0x1BE + (void *)bh->b_data; - for (i = 4 ; i < 16 ; i++, current_minor++) { - p--; - if ((current_minor & 0x3f) >= 60) - break; - if (!(p->start_sect && p->nr_sects)) - continue; - hd[current_minor].start_sect = p->start_sect; - hd[current_minor].nr_sects = p->nr_sects; - printk(" DM part %d start %d size %d end %d\n\r", - current_minor, - hd[current_minor].start_sect, - hd[current_minor].nr_sects, - hd[current_minor].start_sect + - hd[current_minor].nr_sects); - } - } - } else - printk("Bad partition table on dev %04x\n",dev); - brelse(bh); -} - -/* This may be used only once, enforced by 'static int callable' */ -int sys_setup(void * BIOS) -{ - static int callable = 1; - int i,drive; - unsigned char cmos_disks; - - if (!callable) - return -1; - callable = 0; -#ifndef HD_TYPE - for (drive=0 ; drive<2 ; drive++) { - hd_info[drive].cyl = *(unsigned short *) BIOS; - hd_info[drive].head = *(unsigned char *) (2+BIOS); - hd_info[drive].wpcom = *(unsigned short *) (5+BIOS); - hd_info[drive].ctl = *(unsigned char *) (8+BIOS); - hd_info[drive].lzone = *(unsigned short *) (12+BIOS); - hd_info[drive].sect = *(unsigned char *) (14+BIOS); - BIOS += 16; - } + unsigned long t; + int i; - /* - We querry CMOS about hard disks : it could be that - we have a SCSI/ESDI/etc controller that is BIOS - compatable with ST-506, and thus showing up in our - BIOS table, but not register compatable, and therefore - not present in CMOS. - - Furthurmore, we will assume that our ST-506 drives - are the primary drives in the system, and - the ones reflected as drive 1 or 2. - - The first drive is stored in the high nibble of CMOS - byte 0x12, the second in the low nibble. This will be - either a 4 bit drive type or 0xf indicating use byte 0x19 - for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS. - - Needless to say, a non-zero value means we have - an AT controller hard disk for that drive. - - - */ - - if ((cmos_disks = CMOS_READ(0x12)) & 0xf0) - if (cmos_disks & 0x0f) - NR_HD = 2; - else - NR_HD = 1; - else - NR_HD = 0; -#endif - for (i = 0 ; i < (MAX_HD<<6) ; i++) { - hd[i].start_sect = 0; - hd[i].nr_sects = 0; - } - for (i = 0 ; i < NR_HD ; i++) - hd[i<<6].nr_sects = hd_info[i].head* - hd_info[i].sect*hd_info[i].cyl; - for (drive=0 ; drive>1 ; - blk_size[MAJOR_NR] = hd_sizes; - if (NR_HD) - printk("Partition table%s ok.\n\r",(NR_HD>1)?"s":""); - rd_load(); - mount_root(); - return (0); + cli(); + outb_p(0xc2, 0x43); + t = jiffies * 11931 + (inb_p(0x40) & 0x80 ? 5966 : 11932); + i = inb_p(0x40); + i |= inb(0x40) << 8; + sti(); + return(t - i / 2); } +#endif static int controller_ready(void) { @@ -294,10 +118,13 @@ static int win_result(void) if ((i & (BUSY_STAT | READY_STAT | WRERR_STAT | SEEK_STAT | ERR_STAT)) == (READY_STAT | SEEK_STAT)) - return(0); /* ok */ - if (i&1) + return 0; /* ok */ + printk("HD: win_result: status = 0x%02x\n",i); + if (i&1) { i=inb(HD_ERROR); - return (1); + printk("HD: win_result: error = 0x%02x\n",i); + } + return 1; } static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect, @@ -308,6 +135,10 @@ static void hd_out(unsigned int drive,un if (drive>1 || head>15) panic("Trying to write bad sector"); +#if (HD_DELAY > 0) + while (read_timer() - last_req < HD_DELAY) + /* nothing */; +#endif if (reset || !controller_ready()) { reset = 1; return; @@ -321,7 +152,7 @@ static void hd_out(unsigned int drive,un outb_p(cyl,++port); outb_p(cyl>>8,++port); outb_p(0xA0|(drive<<4)|head,++port); - outb(cmd,++port); + outb_p(cmd,++port); } static int drive_busy(void) @@ -335,7 +166,7 @@ static int drive_busy(void) if (c == (READY_STAT | SEEK_STAT)) return 0; } - printk("HD controller times out, c=%02x\n\r",c); + printk("HD controller times out, status = 0x%02x\n\r",c); return(1); } @@ -343,6 +174,7 @@ static void reset_controller(void) { int i; + printk("HD-controller reset\r\n"); outb(4,HD_CMD); for(i = 0; i < 1000; i++) nop(); outb(hd_info[0].ctl & 0x0f ,HD_CMD); @@ -383,62 +215,127 @@ repeat: */ void unexpected_hd_interrupt(void) { + sti(); printk("Unexpected HD interrupt\n\r"); SET_TIMER; -#if 0 - reset = 1; - do_hd_request(); -#endif } static void bad_rw_intr(void) { + int i; + if (!CURRENT) return; if (++CURRENT->errors >= MAX_ERRORS) end_request(0); - if (CURRENT->errors > MAX_ERRORS/2) + else if (CURRENT->errors > MAX_ERRORS/2) reset = 1; else - recalibrate = 1; + for (i=0; i < NR_HD; i++) + recalibrate[i] = 1; +} + +static inline int wait_DRQ(void) +{ + int retries = 100000; + + while (--retries > 0) + if (inb_p(HD_STATUS) & DRQ_STAT) + return 0; + return -1; } +#define STAT_MASK (BUSY_STAT | READY_STAT | WRERR_STAT | SEEK_STAT | ERR_STAT) +#define STAT_OK (READY_STAT | SEEK_STAT) + static void read_intr(void) { - SET_INTR(&read_intr); - if (win_result()) { - SET_INTR(NULL); - bad_rw_intr(); - do_hd_request(); - return; + int i; + int retries = 100000; + + do { + i = (unsigned) inb_p(HD_STATUS); + if ((i & STAT_MASK) != STAT_OK) + break; + if (i & DRQ_STAT) + goto ok_to_read; + } while (--retries > 0); + sti(); + printk("HD: read_intr: status = 0x%02x\n",i); + if (i & ERR_STAT) { + i = (unsigned) inb(HD_ERROR); + printk("HD: read_intr: error = 0x%02x\n",i); } + bad_rw_intr(); + cli(); + do_hd_request(); + return; +ok_to_read: port_read(HD_DATA,CURRENT->buffer,256); CURRENT->errors = 0; CURRENT->buffer += 512; CURRENT->sector++; - if (--CURRENT->nr_sectors) + i = --CURRENT->nr_sectors; + --CURRENT->current_nr_sectors; +#ifdef DEBUG + printk("hd%d : sector = %d, %d remaining to buffer = %08x\n", + MINOR(CURRENT->dev), CURRENT->sector, i, CURRENT-> + buffer); +#endif + if (!i || (CURRENT->bh && !SUBSECTOR(i))) + end_request(1); + if (i > 0) { + SET_INTR(&read_intr); + sti(); return; - SET_INTR(NULL); - end_request(1); + } +#if (HD_DELAY > 0) + last_req = read_timer(); +#endif do_hd_request(); + return; } static void write_intr(void) { - if (win_result()) { - bad_rw_intr(); - do_hd_request(); - return; + int i; + int retries = 100000; + + do { + i = (unsigned) inb_p(HD_STATUS); + if ((i & STAT_MASK) != STAT_OK) + break; + if ((CURRENT->nr_sectors <= 1) || (i & DRQ_STAT)) + goto ok_to_write; + } while (--retries > 0); + sti(); + printk("HD: write_intr: status = 0x%02x\n",i); + if (i & ERR_STAT) { + i = (unsigned) inb(HD_ERROR); + printk("HD: write_intr: error = 0x%02x\n",i); } - if (--CURRENT->nr_sectors) { - CURRENT->sector++; - CURRENT->buffer += 512; + bad_rw_intr(); + cli(); + do_hd_request(); + return; +ok_to_write: + CURRENT->sector++; + i = --CURRENT->nr_sectors; + --CURRENT->current_nr_sectors; + CURRENT->buffer += 512; + if (!i || (CURRENT->bh && !SUBSECTOR(i))) + end_request(1); + if (i > 0) { SET_INTR(&write_intr); port_write(HD_DATA,CURRENT->buffer,256); - return; + sti(); + } else { +#if (HD_DELAY > 0) + last_req = read_timer(); +#endif + do_hd_request(); } - end_request(1); - do_hd_request(); + return; } static void recal_intr(void) @@ -453,101 +350,264 @@ static void recal_intr(void) * best idea seems to just set reset, and start all over again. */ static void hd_times_out(void) -{ - do_hd = NULL; +{ + DEVICE_INTR = NULL; + sti(); reset = 1; if (!CURRENT) return; printk("HD timeout\n\r"); cli(); - if (++CURRENT->errors >= MAX_ERRORS) + if (++CURRENT->errors >= MAX_ERRORS) { +#ifdef DEBUG + printk("hd : too many errors.\n"); +#endif end_request(0); + } + do_hd_request(); } +/* + * The driver has been modified to enable interrupts a bit more: in order to + * do this we first (a) disable the timeout-interrupt and (b) clear the + * device-interrupt. This way the interrupts won't mess with out code (the + * worst that can happen is that an unexpected HD-interrupt comes in and + * sets the "reset" variable and starts the timer) + */ static void do_hd_request(void) { - int i,r; unsigned int block,dev; - unsigned int sec,head,cyl; + unsigned int sec,head,cyl,track; unsigned int nsect; + if (DEVICE_INTR) + return; +repeat: + timer_active &= ~(1<dev); block = CURRENT->sector; nsect = CURRENT->nr_sectors; - if (dev >= (NR_HD<<6) || block+nsect > hd[dev].nr_sects) { + if (dev >= (NR_HD<<6) || block >= hd[dev].nr_sects) { +#ifdef DEBUG + printk("hd%d : attempted read for sector %d past end of device at sector %d.\n", + block, hd[dev].nr_sects); +#endif end_request(0); goto repeat; } block += hd[dev].start_sect; dev >>= 6; - sec = block % hd_info[dev].sect; - block /= hd_info[dev].sect; - head = block % hd_info[dev].head; - cyl = block / hd_info[dev].head; - sec++; + sec = block % hd_info[dev].sect + 1; + track = block / hd_info[dev].sect; + head = track % hd_info[dev].head; + cyl = track / hd_info[dev].head; +#ifdef DEBUG + printk("hd%d : cyl = %d, head = %d, sector = %d, buffer = %08x\n", + dev, cyl, head, sec, CURRENT->buffer); +#endif + cli(); if (reset) { - recalibrate = 1; + int i; + + for (i=0; i < NR_HD; i++) + recalibrate[i] = 1; reset_hd(); + sti(); return; } - if (recalibrate) { - recalibrate = 0; - hd_out(dev,hd_info[dev].sect,0,0,0, - WIN_RESTORE,&recal_intr); + if (recalibrate[dev]) { + recalibrate[dev] = 0; + hd_out(dev,hd_info[dev].sect,0,0,0,WIN_RESTORE,&recal_intr); if (reset) goto repeat; + sti(); return; } if (CURRENT->cmd == WRITE) { hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr); if (reset) goto repeat; - for(i=0 ; i<10000 && !(r=inb_p(HD_STATUS)&DRQ_STAT) ; i++) - /* nothing */ ; - if (!r) { + if (wait_DRQ()) { + printk("HD: do_hd_request: no DRQ\n"); bad_rw_intr(); goto repeat; } port_write(HD_DATA,CURRENT->buffer,256); - } else if (CURRENT->cmd == READ) { + sti(); + return; + } + if (CURRENT->cmd == READ) { hd_out(dev,nsect,sec,head,cyl,WIN_READ,&read_intr); if (reset) goto repeat; - } else - panic("unknown hd-command"); -} - -void hd_init(void) -{ - blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST; - set_intr_gate(0x2E,&hd_interrupt); - outb_p(inb_p(0x21)&0xfb,0x21); - outb(inb_p(0xA1)&0xbf,0xA1); - timer_table[HD_TIMER].fn = hd_times_out; + sti(); + return; + } + panic("unknown hd-command"); } -int hd_ioctl(int dev, int cmd, int arg) +static int hd_ioctl(struct inode * inode, struct file * file, + unsigned int cmd, unsigned int arg) { struct hd_geometry *loc = (void *) arg; + int dev; - if (!loc) + if (!loc || !inode) return -EINVAL; - dev = MINOR(dev) >> 6; + dev = MINOR(inode->i_rdev) >> 6; if (dev >= NR_HD) return -EINVAL; - switch (cmd) { case HDIO_REQ: + verify_area(loc, sizeof(*loc)); put_fs_byte(hd_info[dev].head, (char *) &loc->heads); put_fs_byte(hd_info[dev].sect, (char *) &loc->sectors); put_fs_word(hd_info[dev].cyl, (short *) &loc->cylinders); + put_fs_long(hd[MINOR(inode->i_rdev)].start_sect, + (long *) &loc->start); return 0; + RO_IOCTLS(inode->i_rdev,arg); default: return -EINVAL; } } + +/* + * Releasing a block device means we sync() it, so that it can safely + * be forgotten about... + */ +static void hd_release(struct inode * inode, struct file * file) +{ + sync_dev(inode->i_rdev); +} + +static void hd_geninit(); + +static struct gendisk hd_gendisk = { + MAJOR_NR, /* Major number */ + "hd", /* Major name */ + 6, /* Bits to shift to get real from partition */ + 1 << 6, /* Number of partitions per real */ + MAX_HD, /* maximum number of real */ + hd_geninit, /* init function */ + hd, /* hd struct */ + hd_sizes, /* block sizes */ + 0, /* number */ + (void *) hd_info, /* internal */ + NULL /* next */ +}; + +static void hd_geninit(void) +{ + int drive, i; +#ifndef HD_TYPE + extern struct drive_info drive_info; + void *BIOS = (void *) &drive_info; + int cmos_disks; + + for (drive=0 ; drive<2 ; drive++) { + hd_info[drive].cyl = *(unsigned short *) BIOS; + hd_info[drive].head = *(unsigned char *) (2+BIOS); + hd_info[drive].wpcom = *(unsigned short *) (5+BIOS); + hd_info[drive].ctl = *(unsigned char *) (8+BIOS); + hd_info[drive].lzone = *(unsigned short *) (12+BIOS); + hd_info[drive].sect = *(unsigned char *) (14+BIOS); + BIOS += 16; + } + + /* + We querry CMOS about hard disks : it could be that + we have a SCSI/ESDI/etc controller that is BIOS + compatable with ST-506, and thus showing up in our + BIOS table, but not register compatable, and therefore + not present in CMOS. + + Furthurmore, we will assume that our ST-506 drives + are the primary drives in the system, and + the ones reflected as drive 1 or 2. + + The first drive is stored in the high nibble of CMOS + byte 0x12, the second in the low nibble. This will be + either a 4 bit drive type or 0xf indicating use byte 0x19 + for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS. + + Needless to say, a non-zero value means we have + an AT controller hard disk for that drive. + + + */ + + if ((cmos_disks = CMOS_READ(0x12)) & 0xf0) + if (cmos_disks & 0x0f) + NR_HD = 2; + else + NR_HD = 1; + else + NR_HD = 0; +#endif + + for (i = 0 ; i < NR_HD ; i++) + hd[i<<6].nr_sects = hd_info[i].head* + hd_info[i].sect*hd_info[i].cyl; + + hd_gendisk.nr_real = NR_HD; +} + +static struct file_operations hd_fops = { + NULL, /* lseek - default */ + block_read, /* read - general block-dev read */ + block_write, /* write - general block-dev write */ + NULL, /* readdir - bad */ + NULL, /* select */ + hd_ioctl, /* ioctl */ + NULL, /* no special open code */ + hd_release /* release */ +}; + +static void hd_interrupt(int unused) +{ + void (*handler)(void) = DEVICE_INTR; + + DEVICE_INTR = NULL; + timer_active &= ~(1<