--- linux/kernel/blk_drv/hd.c 2018/04/24 18:02:28 1.1.1.2 +++ linux/kernel/blk_drv/hd.c 2018/04/24 18:06:41 1.1.1.6 @@ -11,10 +11,16 @@ * sleep. Special care is recommended. * * 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 + * in the early extended-partition checks and added DM partitions */ +#include + #include #include +#include #include #include #include @@ -25,25 +31,27 @@ #define MAJOR_NR 3 #include "blk.h" -#define CMOS_READ(addr) ({ \ -outb_p(0x80|addr,0x70); \ -inb_p(0x71); \ -}) +static inline unsigned char CMOS_READ(unsigned char addr) +{ + outb_p(0x80|addr,0x70); + return inb_p(0x71); +} /* Max read/write errors/sector */ #define MAX_ERRORS 7 #define MAX_HD 2 static void recal_intr(void); +static void bad_rw_intr(void); -static int recalibrate = 1; -static int reset = 1; +static int recalibrate = 0; +static int reset = 0; /* * This struct defines the HD's and their types. */ struct hd_i_struct { - int head,sect,cyl,wpcom,lzone,ctl; + unsigned int head,sect,cyl,wpcom,lzone,ctl; }; #ifdef HD_TYPE struct hd_i_struct hd_info[] = { HD_TYPE }; @@ -56,7 +64,9 @@ static int NR_HD = 0; static struct hd_struct { long start_sect; long nr_sects; -} hd[5*MAX_HD]={{0,0},}; +} hd[MAX_HD<<6]={{0,0},}; + +static int hd_sizes[MAX_HD<<6] = {0, }; #define port_read(port,buf,nr) \ __asm__("cld;rep;insw"::"d" (port),"D" (buf),"c" (nr):"cx","di") @@ -67,14 +77,139 @@ __asm__("cld;rep;outsw"::"d" (port),"S" 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) +{ + 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; - struct partition *p; - struct buffer_head * bh; if (!callable) return -1; @@ -89,16 +224,6 @@ int sys_setup(void * BIOS) hd_info[drive].sect = *(unsigned char *) (14+BIOS); BIOS += 16; } - if (hd_info[1].cyl) - NR_HD=2; - else - NR_HD=1; -#endif - for (i=0 ; ib_data[510] != 0x55 || (unsigned char) - bh->b_data[511] != 0xAA) { - printk("Bad partition table on drive %d\n\r",drive); - panic(""); - } - p = 0x1BE + (void *)bh->b_data; - for (i=1;i<5;i++,p++) { - hd[i+5*drive].start_sect = p->start_sect; - hd[i+5*drive].nr_sects = p->nr_sects; - } - brelse(bh); + current_minor = 1+(drive<<6); + check_partition(0x0300+(drive<<6)); } + for (i=0 ; i<(MAX_HD<<6) ; 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(); @@ -160,9 +278,13 @@ int sys_setup(void * BIOS) static int controller_ready(void) { - int retries=10000; + int retries = 100000; - while (--retries && (inb_p(HD_STATUS)&0xc0)!=0x40); + while (--retries && (inb_p(HD_STATUS)&0x80)) + /* nothing */; + if (!retries) + printk("controller_ready: status = %02x\n\r", + (unsigned char) inb_p(HD_STATUS)); return (retries); } @@ -173,7 +295,8 @@ 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) i=inb(HD_ERROR); + if (i&1) + i=inb(HD_ERROR); return (1); } @@ -181,13 +304,15 @@ static void hd_out(unsigned int drive,un unsigned int head,unsigned int cyl,unsigned int cmd, void (*intr_addr)(void)) { - register int port asm("dx"); + unsigned short port; if (drive>1 || head>15) panic("Trying to write bad sector"); - if (!controller_ready()) - panic("HD controller not ready"); - do_hd = intr_addr; + if (reset || !controller_ready()) { + reset = 1; + return; + } + SET_INTR(intr_addr); outb_p(hd_info[drive].ctl,HD_CMD); port=HD_DATA; outb_p(hd_info[drive].wpcom>>2,++port); @@ -202,15 +327,15 @@ static void hd_out(unsigned int drive,un static int drive_busy(void) { unsigned int i; + unsigned char c; - for (i = 0; i < 10000; i++) - if (READY_STAT == (inb_p(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); - printk("HD controller times out\n\r"); + for (i = 0; i < 500000 ; 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, c=%02x\n\r",c); return(1); } @@ -219,7 +344,7 @@ static void reset_controller(void) int i; outb(4,HD_CMD); - for(i = 0; i < 100; i++) nop(); + for(i = 0; i < 1000; i++) nop(); outb(hd_info[0].ctl & 0x0f ,HD_CMD); if (drive_busy()) printk("HD-controller still busy\n\r"); @@ -227,29 +352,62 @@ static void reset_controller(void) printk("HD-controller reset failed: %02x\n\r",i); } -static void reset_hd(int nr) +static void reset_hd(void) { - reset_controller(); - hd_out(nr,hd_info[nr].sect,hd_info[nr].sect,hd_info[nr].head-1, - hd_info[nr].cyl,WIN_SPECIFY,&recal_intr); + static int i; + +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); + if (reset) + goto repeat; + } else + do_hd_request(); } +/* + * 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 + * ignore it, and just set the timeout. + */ void unexpected_hd_interrupt(void) { printk("Unexpected HD interrupt\n\r"); + SET_TIMER; +#if 0 + reset = 1; + do_hd_request(); +#endif } static void bad_rw_intr(void) { + if (!CURRENT) + return; if (++CURRENT->errors >= MAX_ERRORS) end_request(0); if (CURRENT->errors > MAX_ERRORS/2) reset = 1; + else + recalibrate = 1; } static void read_intr(void) { + SET_INTR(&read_intr); if (win_result()) { + SET_INTR(NULL); bad_rw_intr(); do_hd_request(); return; @@ -258,10 +416,9 @@ static void read_intr(void) CURRENT->errors = 0; CURRENT->buffer += 512; CURRENT->sector++; - if (--CURRENT->nr_sectors) { - do_hd = &read_intr; + if (--CURRENT->nr_sectors) return; - } + SET_INTR(NULL); end_request(1); do_hd_request(); } @@ -276,7 +433,7 @@ static void write_intr(void) if (--CURRENT->nr_sectors) { CURRENT->sector++; CURRENT->buffer += 512; - do_hd = &write_intr; + SET_INTR(&write_intr); port_write(HD_DATA,CURRENT->buffer,256); return; } @@ -291,7 +448,24 @@ static void recal_intr(void) do_hd_request(); } -void do_hd_request(void) +/* + * 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. + */ +static void hd_times_out(void) +{ + do_hd = NULL; + reset = 1; + if (!CURRENT) + return; + printk("HD timeout\n\r"); + cli(); + if (++CURRENT->errors >= MAX_ERRORS) + end_request(0); + do_hd_request(); +} + +static void do_hd_request(void) { int i,r; unsigned int block,dev; @@ -301,33 +475,36 @@ void do_hd_request(void) INIT_REQUEST; dev = MINOR(CURRENT->dev); block = CURRENT->sector; - if (dev >= 5*NR_HD || block+2 > hd[dev].nr_sects) { + nsect = CURRENT->nr_sectors; + if (dev >= (NR_HD<<6) || block+nsect > hd[dev].nr_sects) { end_request(0); goto repeat; } block += hd[dev].start_sect; - dev /= 5; - __asm__("divl %4":"=a" (block),"=d" (sec):"0" (block),"1" (0), - "r" (hd_info[dev].sect)); - __asm__("divl %4":"=a" (cyl),"=d" (head):"0" (block),"1" (0), - "r" (hd_info[dev].head)); + 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++; - nsect = CURRENT->nr_sectors; if (reset) { - reset = 0; recalibrate = 1; - reset_hd(CURRENT_DEV); + reset_hd(); return; } if (recalibrate) { recalibrate = 0; - hd_out(dev,hd_info[CURRENT_DEV].sect,0,0,0, + hd_out(dev,hd_info[dev].sect,0,0,0, WIN_RESTORE,&recal_intr); + if (reset) + goto repeat; 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++) + if (reset) + goto repeat; + for(i=0 ; i<10000 && !(r=inb_p(HD_STATUS)&DRQ_STAT) ; i++) /* nothing */ ; if (!r) { bad_rw_intr(); @@ -336,6 +513,8 @@ void do_hd_request(void) port_write(HD_DATA,CURRENT->buffer,256); } else if (CURRENT->cmd == READ) { hd_out(dev,nsect,sec,head,cyl,WIN_READ,&read_intr); + if (reset) + goto repeat; } else panic("unknown hd-command"); } @@ -346,4 +525,29 @@ void hd_init(void) 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; +} + +int hd_ioctl(int dev, int cmd, int arg) +{ + struct hd_geometry *loc = (void *) arg; + + if (!loc) + return -EINVAL; + dev = MINOR(dev) >> 6; + if (dev >= NR_HD) + return -EINVAL; + + switch (cmd) { + case HDIO_REQ: + 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); + return 0; + default: + return -EINVAL; + } }