/*
* 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//硬盘主设备号,由于blk.h文件中要用到这个宏,所以要在#include "blk.h" 前定义这个主设备号
#include "blk.h"
#define CMOS_READ(addr) ({ \
outb_p(0x80|addr,0x70); \//0x70写端口号,0x80|addr 为要读写 的内存地址
inb_p(0x71); \//ox71读端口号
})//读CMOS参数宏函数,main.c函数81行有调用的
/* Max read/write errors/sector */
#define MAX_ERRORS 7 //读写一个扇区时允许的最多出错数
#define MAX_HD 2 //系统支持的最大硬盘数
static void recal_intr(void);//重亲校正处理函数,将磁头移到0柱面
static int recalibrate = 1;//重新校正标志,
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_TYPE//如include/linux/config.h,配置了HD_TYPE,则就取HD_TYPE来填充磁盘停息
struct hd_i_struct hd_info[] = { HD_TYPE };
#define NR_HD ((sizeof (hd_info))/(sizeof (struct hd_i_struct)))
#else//如果没有,则全部置为0,支持最多两个磁盘
struct hd_i_struct hd_info[] = { {0,0,0,0,0,0},{0,0,0,0,0,0} };
static int NR_HD = 0;
#endif
static struct hd_struct {
long start_sect;//分区在磁盘中的起始物理绝对扇区号
long nr_sects;//分区中的扇区总数
} hd[5*MAX_HD]={{0,0},};//磁盘分区结构
#define port_read(port,buf,nr) \
__asm__("cld;rep;insw"::"d" (port),"D" (buf),"c" (nr):"cx","di")//读端口port,读nr个字,保存在buf中
#define port_write(port,buf,nr) \
__asm__("cld;rep;outsw"::"d" (port),"S" (buf),"c" (nr):"cx","si")//写端口port,写nr个字,从buf中取
extern void hd_interrupt(void);//磁盘中断过程 ,/kernel/system_call.s line 221调用
extern void rd_load(void);//虚拟盘创建加载函数
/* This may be used only once, enforced by 'static int callable' */
int sys_setup(void * BIOS)//读取CMOS信息,根据/boot/setup.s提供 的信息对硬盘进行参数设置,由mian.c line 172 setup((void *) &drive_info)调用
{
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//如果nclude/linux/config.h没有设置HD_TYPE,则读取内存0x90080 开始的硬盘参数表。main.c文件的调用路线setup((void *) &drive_info);--->init()--->main();drive_info = DRIVE_INFO;--->#define DRIVE_INFO (*(struct drive_info *)0x90080)
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个磁盘,否则为1个磁盘
NR_HD=2;
else
NR_HD=1;
#endif
for (i=0 ; i<NR_HD ; i++) {//设置磁盘分区数据结构相关信息,0和5分别表示两个磁盘的整体参数,1--4、6--9分别表示两个磁盘的4个分区的参数
hd[i*5].start_sect = 0;//硬盘起始扇区号
hd[i*5].nr_sects = hd_info[i].head*
hd_info[i].sect*hd_info[i].cyl;//硬盘总扇区数
}
//磁头数:磁盘上用几面盘片;柱面数:每个盘片上有几个磁道;扇区数:每个磁道上有几个扇区,1扇区=512B(字节)
/*
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.
*/
//我们对CMOS有关磁盘的信息产生怀疑,将进一步进行确认,检测磁盘到底是不是AT控制器兼容的
if ((cmos_disks = CMOS_READ(0x12)) & 0xf0)//从CMOS偏移地址0x12处读出磁盘类型字节,每一个磁盘参数存入在CMOS 偏移地址字节0x12高半字节中,第二个磁盘参数存于低半字节处,高半字节不为空时
if (cmos_disks & 0x0f)//低半字节不为空时
NR_HD = 2;
else
NR_HD = 1;
else
NR_HD = 0;
for (i = NR_HD ; i < 2 ; i++) {//将磁盘为空的那个清0
hd[i*5].start_sect = 0;
hd[i*5].nr_sects = 0;
}//到此为值,我们真正确定的磁盘的个数
for (drive=0 ; drive<NR_HD ; drive++) {//读取每个磁盘上第一个扇区中的分区表信息,也就是说,磁盘的分区信息在磁盘的第一扇区中存放
if (!(bh = bread(0x300 + drive*5,0))) {//读取0x300 0x305 设备号第0号扇区中的内容存入bh->b_data中,每一个扇区的大小为512B
printk("Unable to read partition table of drive %d\n\r",
drive);
panic("");
}
if (bh->b_data[510] != 0x55 || (unsigned char)
bh->b_data[511] != 0xAA) {//如果磁盘中第1个扇区最后两个字节为0xAA55(不是0X55AA),则说明这个扇区上的数据有效
printk("Bad partition table on drive %d\n\r",drive);
panic("");
}
p = 0x1BE + (void *)bh->b_data;//磁盘的分区表信息位于第1扇区的0x1BE处
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();//尝试创建并加载虚拟盘
mount_root();//安装根文件系统
return (0);
}
static int controller_ready(void)//判断并循环等待硬盘控制器就绪 空闲状态
{
int retries=10000;//等待的时间
while (--retries && (inb_p(HD_STATUS)&0xc0)!=0x40);//读硬盘控制器状态寄存器HD_STATUS(0x1f7),验证第7位和第6位,第7位为1则控制器忙,第6位为1则控制器就绪,/include/linux/hdreg.h,当第7位为0,第6位为1时则退出循环
return (retries);//返回不为0时,则说明在时间期限内控制器回到就绪空闲状态,OK
}
static int win_result(void)//检查磁盘执行命令后的状态,win温切斯特硬盘
{
int i=inb_p(HD_STATUS);//硬盘控制器状态寄存器HD_STATUS(0x1f7)
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);//读硬盘控制器错误寄存器HD_ERROR(0x1f1)
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))//向磁盘控制器发送命令块,设置磁盘相关寄存器,drive磁盘号(0--1),nsect读写扇区数,sect起始扇区,head磁头号,cyl柱面号,cmd命令码,intr_addr硬盘中断处理程序
{
register int port asm("dx");//定义局部寄存器变量将放在指定寄存器dx中去
if (drive>1 || head>15)//drive(0--1),head(0--15)
panic("Trying to write bad sector");
if (!controller_ready())//等待硬盘控制器就绪
panic("HD controller not ready");
do_hd = intr_addr;//#define DEVICE_INTR do_hd中断处理函数,blk.h line 81,在中断程序中被调用
outb_p(hd_info[drive].ctl,HD_CMD);//向磁盘控制器HD_CMD控制寄存器写入控制字节
port=HD_DATA;//port 指向HD_DATA数据寄存器
outb_p(hd_info[drive].wpcom>>2,++port);//HD_ERROR错误寄存器,写预补偿柱面号(除于4)
outb_p(nsect,++port);//HD_NSECTOR总扇区寄存器,读写扇区总数
outb_p(sect,++port);//HD_SECTOR起始扇区寄存器,起始扇区号
outb_p(cyl,++port);//HD_LCY柱面低8位寄存器,柱面号低8位
outb_p(cyl>>8,++port);//HD_HCYL柱面高8位寄存器,柱面号高8位
outb_p(0xA0|(drive<<4)|head,++port);//HD_CURRENT磁盘号磁头号寄存器,磁盘号+ 磁头号
outb(cmd,++port);//HD_STATUS状态寄存器,硬盘控制命令
}
static int drive_busy(void)//等待磁盘就绪,主要检查磁盘寄存器的标示 位
{
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)//如果就绪标志为1或寻道结束标志为1时,
return(0);//返回0,则说明已等待硬盘就绪
printk("HD controller times out\n\r");
return(1);//否则返回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)//读取磁盘错误寄存器,为1时表示无错误,其他表示有错误
printk("HD-controller reset failed: %02x\n\r",i);
}
static void reset_hd(int nr)//磁盘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)//当前请求项的出错次数大于MAX_ERRORS(7),
end_request(0);//结束此次的请求项处理
if (CURRENT->errors > MAX_ERRORS/2)//当前请求项的出错次数大于MAX_ERRORS(7)/2,
reset = 1;//置复位标志,要求复位磁盘控制器
}
static void read_intr(void)//读操作中断处理函数,当用户应用程序读命令执行后,将产生磁盘中断信号,并执行磁盘中断处理程序 C函数指针do_hd,此时指向read_intr
{
if (win_result()) {//检查磁盘执行命令后的状态,出错时
bad_rw_intr();//读写磁盘失败中断调用函数
do_hd_request();//执行硬盘读写请求操作
return;
}
port_read(HD_DATA,CURRENT->buffer,256);//从硬盘控制器HD_DATA数据 寄存器读取256个字(512个字节,1个扇区)的数据 存入CURRENT->buffer
CURRENT->errors = 0;//错误为0
CURRENT->buffer += 512;//缓冲区头指针下移
CURRENT->sector++;//要读取 起始扇区号加1
if (--CURRENT->nr_sectors) {//要读取的总扇区数减1
do_hd = &read_intr;//重设中断处理函数
return;//port_read读取1个扇区的数据后,再一次硬盘控制器将数据从磁盘中读入HD_DATA数据寄存器,后引发中断,调用中断处理函数read_intr
}
end_request(1);//结束此请示项的请求处理
do_hd_request();//执行硬盘读写请求操作
}
static void write_intr(void)//写操作中断处理函数,当用户应用程序写命令执行后,将产生磁盘中断信号,并执行磁盘中断处理程序 C函数指针do_hd,此时指向read_intr
{
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);//从CURRENT->buffer中取得数据,向HD_DATA端口写256个字,也就是512个字节,也就是一个扇区,(硬盘控制器把HD_DATA数据寄存器中的数据写回磁盘,后引发中断,调用中断处理函数write_intr)
return;
}
end_request(1);//同前
do_hd_request();//同前
}
static void recal_intr(void)//磁盘重新校正(复位)中断调用函数
{
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) {//子设备号对应磁盘各分区,如果子设备号不存在或当前项的起始扇区号+2大于当前项的扇区数则退出
end_request(0);//结束此请示项的请求处理
goto repeat;
}
block += hd[dev].start_sect;//加上子设备号(某个扇区)的起始扇区号,block就变为整个磁盘的绝对扇区号
dev /= 5;//取行主设备号,也就是磁盘0还是磅盘1
__asm__("divl %4":"=a" (block),"=d" (sec):"0" (block),"1" (0),
"r" (hd_info[dev].sect));//由前面的dev block 来计算出所在的磁头号、这个磁头号中的柱面号、这个柱面中的扇区号(磁头数:磁盘上用几面盘片;柱面数:每个盘片上有几个磁道;扇区数:每个磁道上有几个扇区,1扇区=512B(字节))
__asm__("divl %4":"=a" (cyl),"=d" (head):"0" (block),"1" (0),
"r" (hd_info[dev].head));
sec++;
nsect = CURRENT->nr_sectors;//要读写的总的扇区数,读写是按扇区进行的,1扇区=512字节
if (reset) {//如果磁盘复位标志置位时
reset = 0;//清标志
recalibrate = 1;//需重新校正,校正标志闰置1
reset_hd(CURRENT_DEV);//磁盘nr 复位
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++)//DRQ_STAT硬盘状态寄存器的请求服务位,如果置位则说明驱动器已经准备好在主机和数据端口之间传输一个字或一个字节的数据
/* nothing */ ;
if (!r) {
bad_rw_intr();//读写磁盘失败中断调用函数
goto repeat;
}
port_write(HD_DATA,CURRENT->buffer,256);//从CURRENT->buffer中取得数据,向HD_DATA端口写256个字,也就是512个字节,也就是一个扇区,(do_hd_request返回,硬盘控制器把HD_DATA数据寄存器中的数据写回磁盘,后引发中断,调用中断处理函数write_intr)
} else if (CURRENT->cmd == READ) {
hd_out(dev,nsect,sec,head,cyl,WIN_READ,&read_intr);//向硬盘控制器发送读命令,(do_hd_request返回,硬盘控制器将数据从磁盘中读入HD_DATA数据寄存器,后引发中断,调用中断处理函数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);//设置硬盘中断门描述述,硬盘中断号为0x2E,对应于8259A芯片的中断信号IRQ13
outb_p(inb_p(0x21)&0xfb,0x21);//复位连接的主8259A int2的屏蔽位。
outb(inb_p(0xA1)&0xbf,0xA1);//复位硬盘中断请求屏蔽位,(在从片上)
}
|