--- linux/kernel/blk_drv/hd.c 2018/04/24 18:01:40 1.1.1.1 +++ linux/kernel/blk_drv/hd.c 2018/04/24 18:03:22 1.1.1.3 @@ -9,6 +9,8 @@ * request-list, using interrupts to jump between functions. As * all the functions are called within interrupts, we may not * sleep. Special care is recommended. + * + * modified by Drew Eckhardt to check nr of hd's from the CMOS. */ #include @@ -23,10 +25,21 @@ #define MAJOR_NR 3 #include "blk.h" +#define CMOS_READ(addr) ({ \ +outb_p(0x80|addr,0x70); \ +inb_p(0x71); \ +}) + /* Max read/write errors/sector */ -#define MAX_ERRORS 5 +#define MAX_ERRORS 7 #define MAX_HD 2 +static void recal_intr(void); +static void bad_rw_intr(void); + +static int recalibrate = 0; +static int reset = 0; + /* * This struct defines the HD's and their types. */ @@ -46,6 +59,8 @@ static struct hd_struct { long nr_sects; } hd[5*MAX_HD]={{0,0},}; +static int hd_sizes[5*MAX_HD] = {0, }; + #define port_read(port,buf,nr) \ __asm__("cld;rep;insw"::"d" (port),"D" (buf),"c" (nr):"cx","di") @@ -53,12 +68,14 @@ __asm__("cld;rep;insw"::"d" (port),"D" ( __asm__("cld;rep;outsw"::"d" (port),"S" (buf),"c" (nr):"cx","si") extern void hd_interrupt(void); +extern void rd_load(void); /* 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; struct partition *p; struct buffer_head * bh; @@ -85,6 +102,40 @@ int sys_setup(void * BIOS) hd[i*5].nr_sects = hd_info[i].head* hd_info[i].sect*hd_info[i].cyl; } + + /* + 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; + for (i = NR_HD ; i < 2 ; i++) { + hd[i*5].start_sect = 0; + hd[i*5].nr_sects = 0; + } for (drive=0 ; drive1)?"s":""); + for (i=0 ; i<5*MAX_HD ; i++) + hd_sizes[i] = hd[i].nr_sects>>1 ; + blk_size[MAJOR_NR] = hd_sizes; + if (NR_HD) + printk("Partition table%s ok.\n\r",(NR_HD>1)?"s":""); + rd_load(); + init_swapping(); mount_root(); return (0); } static int controller_ready(void) { - int retries=1000; + int retries = 100000; - while (--retries && (inb(HD_STATUS)&0xc0)!=0x40); + while (--retries && (inb_p(HD_STATUS)&0xc0)!=0x40); return (retries); } static int win_result(void) { - int i=inb(HD_STATUS); + int i=inb_p(HD_STATUS); if ((i & (BUSY_STAT | READY_STAT | WRERR_STAT | SEEK_STAT | ERR_STAT)) == (READY_STAT | SEEK_STAT)) @@ -137,8 +194,8 @@ static void hd_out(unsigned int drive,un panic("Trying to write bad sector"); if (!controller_ready()) panic("HD controller not ready"); - do_hd = intr_addr; - outb(hd_info[drive].ctl,HD_CMD); + SET_INTR(intr_addr); + outb_p(hd_info[drive].ctl,HD_CMD); port=HD_DATA; outb_p(hd_info[drive].wpcom>>2,++port); outb_p(nsect,++port); @@ -152,14 +209,14 @@ static void hd_out(unsigned int drive,un static int drive_busy(void) { unsigned int i; + unsigned char c; - for (i = 0; i < 100000; i++) - if (READY_STAT == (inb(HD_STATUS) & (BUSY_STAT | READY_STAT))) - break; - i = inb(HD_STATUS); - i &= BUSY_STAT | READY_STAT | SEEK_STAT; - if (i == READY_STAT | SEEK_STAT) - return(0); + for (i = 0; i < 50000; i++) { + c = inb_p(HD_STATUS); + c &= (BUSY_STAT | READY_STAT | SEEK_STAT); + if (c == (READY_STAT | SEEK_STAT)) + return 0; + } printk("HD controller times out\n\r"); return(1); } @@ -170,53 +227,65 @@ static void reset_controller(void) outb(4,HD_CMD); for(i = 0; i < 1000; i++) nop(); - outb(0,HD_CMD); - for(i = 0; i < 10000 && drive_busy(); i++) /* nothing */; + outb(hd_info[0].ctl & 0x0f ,HD_CMD); if (drive_busy()) printk("HD-controller still busy\n\r"); - if((i = inb(ERR_STAT)) != 1) + if ((i = inb(HD_ERROR)) != 1) printk("HD-controller reset failed: %02x\n\r",i); } -static void redo_hd_request(void) +static void reset_hd(void) { - do_hd = NULL; - do_hd_request(); -} + static int i; -static void reset_hd(int nr) -{ - reset_controller(); - hd_out(nr,hd_info[nr].sect,hd_info[nr].sect,hd_info[nr].head-1, - hd_info[nr].cyl,WIN_SPECIFY,&redo_hd_request); +repeat: + if (reset) { + reset = 0; + i = -1; + reset_controller(); + } else if (win_result()) { + bad_rw_intr(); + if (reset) + goto repeat; + } + i++; + if (i < NR_HD) { + hd_out(i,hd_info[i].sect,hd_info[i].sect,hd_info[i].head-1, + hd_info[i].cyl,WIN_SPECIFY,&reset_hd); + } else + do_hd_request(); } void unexpected_hd_interrupt(void) { printk("Unexpected HD interrupt\n\r"); + reset = 1; + do_hd_request(); } static void bad_rw_intr(void) { - int i = CURRENT_DEV; - - if (CURRENT->errors++ >= MAX_ERRORS) + if (++CURRENT->errors >= MAX_ERRORS) end_request(0); - reset_hd(i); + if (CURRENT->errors > MAX_ERRORS/2) + reset = 1; } static void read_intr(void) { if (win_result()) { bad_rw_intr(); + do_hd_request(); return; } port_read(HD_DATA,CURRENT->buffer,256); CURRENT->errors = 0; CURRENT->buffer += 512; CURRENT->sector++; - if (--CURRENT->nr_sectors) + if (--CURRENT->nr_sectors) { + SET_INTR(&read_intr); return; + } end_request(1); do_hd_request(); } @@ -225,11 +294,13 @@ static void write_intr(void) { if (win_result()) { bad_rw_intr(); + do_hd_request(); return; } if (--CURRENT->nr_sectors) { CURRENT->sector++; CURRENT->buffer += 512; + SET_INTR(&write_intr); port_write(HD_DATA,CURRENT->buffer,256); return; } @@ -237,6 +308,25 @@ static void write_intr(void) do_hd_request(); } +static void recal_intr(void) +{ + if (win_result()) + bad_rw_intr(); + do_hd_request(); +} + +void hd_times_out(void) +{ + if (!CURRENT) + return; + printk("HD timeout"); + if (++CURRENT->errors >= MAX_ERRORS) + end_request(0); + SET_INTR(NULL); + reset = 1; + do_hd_request(); +} + void do_hd_request(void) { int i,r; @@ -259,13 +349,24 @@ void do_hd_request(void) "r" (hd_info[dev].head)); sec++; nsect = CURRENT->nr_sectors; + if (reset) { + recalibrate = 1; + reset_hd(); + return; + } + if (recalibrate) { + recalibrate = 0; + hd_out(dev,hd_info[CURRENT_DEV].sect,0,0,0, + WIN_RESTORE,&recal_intr); + return; + } if (CURRENT->cmd == WRITE) { hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr); - for(i=0 ; i<3000 && !(r=inb_p(HD_STATUS)&DRQ_STAT) ; i++) + for(i=0 ; i<10000 && !(r=inb_p(HD_STATUS)&DRQ_STAT) ; i++) /* nothing */ ; if (!r) { - reset_hd(CURRENT_DEV); - return; + bad_rw_intr(); + goto repeat; } port_write(HD_DATA,CURRENT->buffer,256); } else if (CURRENT->cmd == READ) { @@ -277,7 +378,7 @@ void do_hd_request(void) void hd_init(void) { blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST; - set_trap_gate(0x2E,&hd_interrupt); + set_intr_gate(0x2E,&hd_interrupt); outb_p(inb_p(0x21)&0xfb,0x21); outb(inb_p(0xA1)&0xbf,0xA1); }