📄 hd.c
字号:
/* * linux/kernel/hd.c * * (C) 1991 Linus Torvalds *//* * This is the low-level hd interrupt support. It traverses the * 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 <linux/config.h>#include <linux/sched.h>#include <linux/fs.h>#include <linux/kernel.h>#include <linux/hdreg.h>#include <asm/system.h>#include <asm/io.h>#include <asm/segment.h>#define MAJOR_NR 3#include "blk.h"#define CMOS_READ(addr) ({ \ /*读CMOS参数*/outb_p(0x80|addr,0x70); \inb_p(0x71); \})/* Max read/write errors/sector */#define MAX_ERRORS 7 /*读写一个扇区时允许的最多出错数*/#define MAX_HD 2 /*系统支持的最多硬盘数*/static void recal_intr(void);/*重新校正处理函数*/static int recalibrate = 1;/*重新校正标志,当设置了该标志,程序中会调用recal_intr以将磁头移动到0柱面*/static int reset = 1;/*复位标志,当发生读写错误时会设置该标志以复位硬盘和控制器*//* * This struct defines the HD's and their types. */struct hd_i_struct { int head,sect,cyl,wpcom,lzone,ctl; };#ifdef HD_TYPEstruct hd_i_struct hd_info[] = { HD_TYPE };#define NR_HD ((sizeof (hd_info))/(sizeof (struct hd_i_struct)))/*计算硬盘个数*/#elsestruct hd_i_struct hd_info[] = { {0,0,0,0,0,0},{0,0,0,0,0,0} };static int NR_HD = 0;#endifstatic struct hd_struct { /*硬盘分区结构*/ long start_sect; /*某分区的起始扇区*/ long nr_sects; /*某分区的长度*/} hd[5*MAX_HD]={{0,0},};#define port_read(port,buf,nr) \ /*读端口port,共读nr字,保存在buf中*/__asm__("cld;rep;insw"::"d" (port),"D" (buf),"c" (nr):"cx","di") /*rep;insw; 重复读*/#define port_write(port,buf,nr) \ /*写端口port,共写nr字,从buf中取*/__asm__("cld;rep;outsw"::"d" (port),"S" (buf),"c" (nr):"cx","si")extern void hd_interrupt(void);/*硬盘中断过程system_call.s,221*/extern void rd_load(void);/*虚拟盘创建加载,ramdisk.c,71*//* This may be used only once, enforced by 'static int callable' */int sys_setup(void * BIOS)/*读取CMOS和硬盘参数表信息,BIOS(指向硬盘参数表的指针)*/{ static int callable = 1;/*限制本函数只被调用一次*/ int i,drive; unsigned char cmos_disks; struct partition *p; struct buffer_head * bh; 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;/*每个硬盘参数表长度16字节,这里BIOS指向下一表*/ } if (hd_info[1].cyl)/*判断是否有第2个硬盘*/ NR_HD=2;/*硬盘数*/ else NR_HD=1;#endif for (i=0 ; i<NR_HD ; i++) {/*hd[]为硬盘分区结构数组*/ hd[i*5].start_sect = 0;/*硬盘起始扇区号*//*此处hd[0]和hd[5]为硬盘整体参数,其余的为扇区*/ 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 <if any> 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)/*判断硬盘是否为AT兼容硬盘*/ 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 ; drive<NR_HD ; drive++) {/*读每个硬盘上第1个扇区中的分区表信息,用来设置hd[]*/ if (!(bh = bread(0x300 + drive*5,0))) {/*bread(设备号,所需读取的块号)用来读块数据,存放于缓冲块bh中*/ printk("Unable to read partition table of drive %d\n\r", drive); panic(""); } if (bh->b_data[510] != 0x55 || (unsigned char)/*若硬盘第一个扇区中最后两个字节为0xAA55则说明扇区中的数据有效,若有效则将硬盘分区表信息存于hd[]中,最后释放bh缓冲区*/ bh->b_data[511] != 0xAA) { printk("Bad partition table on drive %d\n\r",drive); panic(""); } p = 0x1BE + (void *)bh->b_data;/*硬盘第一个扇区中0x1BE(446字节)开始处为硬盘分区表*/ 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);/*释放缓冲*/ } if (NR_HD) printk("Partition table%s ok.\n\r",(NR_HD>1)?"s":""); rd_load();/*尝试创建并加载虚拟盘(ramdisk.c,71)*/ mount_root();/*安装根文件系统(super.c,242)*/ return (0);}static int controller_ready(void)/*循环等待硬盘控制器就绪,返回0说明失败*/{ int retries=10000;/*等待时间上限*/ while (--retries && (inb_p(HD_STATUS)&0xc0)!=0x40); return (retries);}static int win_result(void)/*检查硬盘执行命令后的状态,返回0表示正常*/{ int i=inb_p(HD_STATUS); 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); return (1);}static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect,/*设置硬盘中断处理程序,并向硬盘控制器发送命令块*/ unsigned int head,unsigned int cyl,unsigned int cmd, void (*intr_addr)(void)){ register int port asm("dx"); if (drive>1 || head>15) panic("Trying to write bad sector"); if (!controller_ready()) panic("HD controller not ready"); do_hd = 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); outb_p(sect,++port); outb_p(cyl,++port); outb_p(cyl>>8,++port); outb_p(0xA0|(drive<<4)|head,++port); outb(cmd,++port);/*命令*/}static int drive_busy(void)/*等待硬盘就绪,返回0成功*/{ unsigned int i; 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"); return(1);}static void reset_controller(void)/*复位硬盘控制器*/{ int i; outb(4,HD_CMD); for(i = 0; i < 100; i++) nop(); outb(hd_info[0].ctl & 0x0f ,HD_CMD); if (drive_busy()) printk("HD-controller still busy\n\r"); if ((i = inb(HD_ERROR)) != 1) printk("HD-controller reset failed: %02x\n\r",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,&recal_intr);}void unexpected_hd_interrupt(void)/*意外硬盘中断*/{ printk("Unexpected HD interrupt\n\r");}static void bad_rw_intr(void)/*硬盘读写失败*/{ if (++CURRENT->errors >= MAX_ERRORS) end_request(0);/*结束当前请求项并唤醒等待该请求的进程,且对应缓冲区更新标志复位,表示数据未更新*/ if (CURRENT->errors > MAX_ERRORS/2) reset = 1;/*设置复位标志*/}static void read_intr(void)/*读操作中断调用函数,即读操作时do_hd将指向该函数*/{ if (win_result()) { /*是否 控制器忙、读写错或命令执行错*/ bad_rw_intr(); do_hd_request();/*复位处理*/ return; } port_read(HD_DATA,CURRENT->buffer,256);/*从数据寄存器端口把1个扇区的数据读到请求项的缓冲区中*/ CURRENT->errors = 0; CURRENT->buffer += 512;/*缓冲区指针指向新的空区*/ CURRENT->sector++; if (--CURRENT->nr_sectors) {/*未读完则再置read_intr为中断处理程序*/ do_hd = &read_intr; return; } end_request(1);/*数据已更新标志位*/ do_hd_request();}static void write_intr(void)/*写操作中断调用函数,即写操作时do_hd将指向该函数*/{ if (win_result()) { bad_rw_intr(); do_hd_request(); return; } if (--CURRENT->nr_sectors) { CURRENT->sector++; CURRENT->buffer += 512; do_hd = &write_intr; port_write(HD_DATA,CURRENT->buffer,256); return; } end_request(1); do_hd_request();}static void recal_intr(void)/*硬盘复位中断程序,即复位作时do_hd将指向该函数*/{ if (win_result()) bad_rw_intr(); do_hd_request();}void do_hd_request(void)/*硬盘操作*/{ int i,r; unsigned int block,dev; unsigned int sec,head,cyl; unsigned int nsect; INIT_REQUEST;/*初始化,判断是否还有需处理的请求项*/ dev = MINOR(CURRENT->dev); block = CURRENT->sector; if (dev >= 5*NR_HD || block+2 > 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)); sec++; nsect = CURRENT->nr_sectors;/*预读写的扇区数*/ if (reset) { /*若reset置位,则需进行硬盘和控制器的复位操作*/ reset = 0; recalibrate = 1; reset_hd(CURRENT_DEV); return; } if (recalibrate) {/*重新校正硬盘控制器*/ recalibrate = 0; hd_out(dev,hd_info[CURRENT_DEV].sect,0,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++) /* nothing */ ; if (!r) { bad_rw_intr(); goto repeat; } port_write(HD_DATA,CURRENT->buffer,256);/*向硬盘控制器数据寄存器端口HD_DATA写入1扇区的数据*/ } else if (CURRENT->cmd == READ) { hd_out(dev,nsect,sec,head,cyl,WIN_READ,&read_intr); } 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);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -