⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hd.c

📁 h内核
💻 C
📖 第 1 页 / 共 2 页
字号:
	int i, retries = 100000;	do {		i = (unsigned) inb_p(HD_STATUS);		if (i & BUSY_STAT)			continue;		if (!OK_STATUS(i))			break;		if (i & DRQ_STAT)			goto ok_to_read;	} while (--retries > 0);	dump_status("read_intr", i);	bad_rw_intr();	hd_request();	return;ok_to_read:	req = CURRENT;	insw(HD_DATA,req->buffer,256);	req->sector++;	req->buffer += 512;	req->errors = 0;	i = --req->nr_sectors;	--req->current_nr_sectors;#ifdef DEBUG	printk("%s: read: sector %ld, remaining = %ld, buffer=%p\n",		req->rq_disk->disk_name, req->sector, req->nr_sectors,		req->buffer+512));#endif	if (req->current_nr_sectors <= 0)		end_request(req, 1);	if (i > 0) {		SET_HANDLER(&read_intr);		return;	}	(void) inb_p(HD_STATUS);#if (HD_DELAY > 0)	last_req = read_timer();#endif	if (elv_next_request(QUEUE))		hd_request();	return;}static void write_intr(void){	struct request *req = CURRENT;	int i;	int retries = 100000;	do {		i = (unsigned) inb_p(HD_STATUS);		if (i & BUSY_STAT)			continue;		if (!OK_STATUS(i))			break;		if ((req->nr_sectors <= 1) || (i & DRQ_STAT))			goto ok_to_write;	} while (--retries > 0);	dump_status("write_intr", i);	bad_rw_intr();	hd_request();	return;ok_to_write:	req->sector++;	i = --req->nr_sectors;	--req->current_nr_sectors;	req->buffer += 512;	if (!i || (req->bio && req->current_nr_sectors <= 0))		end_request(req, 1);	if (i > 0) {		SET_HANDLER(&write_intr);		outsw(HD_DATA,req->buffer,256);		local_irq_enable();	} else {#if (HD_DELAY > 0)		last_req = read_timer();#endif		hd_request();	}	return;}static void recal_intr(void){	check_status();#if (HD_DELAY > 0)	last_req = read_timer();#endif	hd_request();}/* * 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(unsigned long dummy){	char *name;	do_hd = NULL;	if (!CURRENT)		return;	disable_irq(HD_IRQ);	local_irq_enable();	reset = 1;	name = CURRENT->rq_disk->disk_name;	printk("%s: timeout\n", name);	if (++CURRENT->errors >= MAX_ERRORS) {#ifdef DEBUG		printk("%s: too many errors\n", name);#endif		end_request(CURRENT, 0);	}	local_irq_disable();	hd_request();	enable_irq(HD_IRQ);}static int do_special_op(struct hd_i_struct *disk, struct request *req){	if (disk->recalibrate) {		disk->recalibrate = 0;		hd_out(disk,disk->sect,0,0,0,WIN_RESTORE,&recal_intr);		return reset;	}	if (disk->head > 16) {		printk ("%s: cannot handle device with more than 16 heads - giving up\n", req->rq_disk->disk_name);		end_request(req, 0);	}	disk->special_op = 0;	return 1;}/* * The driver enables interrupts as much as possible.  In order to do this, * (a) the device-interrupt is disabled before entering hd_request(), * and (b) the timeout-interrupt is disabled before the sti(). * * Interrupts are still masked (by default) whenever we are exchanging * data/cmds with a drive, because some drives seem to have very poor * tolerance for latency during I/O. The IDE driver has support to unmask * interrupts for non-broken hardware, so use that driver if required. */static void hd_request(void){	unsigned int block, nsect, sec, track, head, cyl;	struct hd_i_struct *disk;	struct request *req;	if (do_hd)		return;repeat:	del_timer(&device_timer);	local_irq_enable();	req = CURRENT;	if (!req) {		do_hd = NULL;		return;	}	if (reset) {		local_irq_disable();		reset_hd();		return;	}	disk = req->rq_disk->private_data;	block = req->sector;	nsect = req->nr_sectors;	if (block >= get_capacity(req->rq_disk) ||	    ((block+nsect) > get_capacity(req->rq_disk))) {		printk("%s: bad access: block=%d, count=%d\n",			req->rq_disk->disk_name, block, nsect);		end_request(req, 0);		goto repeat;	}	if (disk->special_op) {		if (do_special_op(disk, req))			goto repeat;		return;	}	sec   = block % disk->sect + 1;	track = block / disk->sect;	head  = track % disk->head;	cyl   = track / disk->head;#ifdef DEBUG	printk("%s: %sing: CHS=%d/%d/%d, sectors=%d, buffer=%p\n",		req->rq_disk->disk_name, (req->cmd == READ)?"read":"writ",		cyl, head, sec, nsect, req->buffer);#endif	if (req->flags & REQ_CMD) {		switch (rq_data_dir(req)) {		case READ:			hd_out(disk,nsect,sec,head,cyl,WIN_READ,&read_intr);			if (reset)				goto repeat;			break;		case WRITE:			hd_out(disk,nsect,sec,head,cyl,WIN_WRITE,&write_intr);			if (reset)				goto repeat;			if (wait_DRQ()) {				bad_rw_intr();				goto repeat;			}			outsw(HD_DATA,req->buffer,256);			break;		default:			printk("unknown hd-command\n");			end_request(req, 0);			break;		}	}}static void do_hd_request (request_queue_t * q){	disable_irq(HD_IRQ);	hd_request();	enable_irq(HD_IRQ);}static int hd_ioctl(struct inode * inode, struct file * file,	unsigned int cmd, unsigned long arg){	struct hd_i_struct *disk = inode->i_bdev->bd_disk->private_data;	struct hd_geometry __user *loc = (struct hd_geometry __user *) arg;	struct hd_geometry g; 	if (cmd != HDIO_GETGEO)		return -EINVAL;	if (!loc)		return -EINVAL;	g.heads = disk->head;	g.sectors = disk->sect;	g.cylinders = disk->cyl;	g.start = get_start_sect(inode->i_bdev);	return copy_to_user(loc, &g, sizeof g) ? -EFAULT : 0; }/* * Releasing a block device means we sync() it, so that it can safely * be forgotten about... */static irqreturn_t hd_interrupt(int irq, void *dev_id, struct pt_regs *regs){	void (*handler)(void) = do_hd;	do_hd = NULL;	del_timer(&device_timer);	if (!handler)		handler = unexpected_hd_interrupt;	handler();	local_irq_enable();	return IRQ_HANDLED;}static struct block_device_operations hd_fops = {	.ioctl =	hd_ioctl,};/* * This is the hard disk IRQ description. The SA_INTERRUPT in sa_flags * means we run the IRQ-handler with interrupts disabled:  this is bad for * interrupt latency, but anything else has led to problems on some * machines. * * We enable interrupts in some of the routines after making sure it's * safe. */static int __init hd_init(void){	int drive;	if (register_blkdev(MAJOR_NR,"hd"))		return -1;	hd_queue = blk_init_queue(do_hd_request, &hd_lock);	if (!hd_queue) {		unregister_blkdev(MAJOR_NR,"hd");		return -ENOMEM;	}	blk_queue_max_sectors(hd_queue, 255);	init_timer(&device_timer);	device_timer.function = hd_times_out;	blk_queue_hardsect_size(hd_queue, 512);#ifdef __i386__	if (!NR_HD) {		extern struct drive_info drive_info;		unsigned char *BIOS = (unsigned char *) &drive_info;		unsigned long flags;		int cmos_disks;		for (drive=0 ; drive<2 ; drive++) {			hd_info[drive].cyl = *(unsigned short *) BIOS;			hd_info[drive].head = *(2+BIOS);			hd_info[drive].wpcom = *(unsigned short *) (5+BIOS);			hd_info[drive].ctl = *(8+BIOS);			hd_info[drive].lzone = *(unsigned short *) (12+BIOS);			hd_info[drive].sect = *(14+BIOS);#ifdef does_not_work_for_everybody_with_scsi_but_helps_ibm_vp			if (hd_info[drive].cyl && NR_HD == drive)				NR_HD++;#endif			BIOS += 16;		}	/*		We query CMOS about hard disks : it could be that 		we have a SCSI/ESDI/etc controller that is BIOS		compatible with ST-506, and thus showing up in our		BIOS table, but not register compatible, and therefore		not present in CMOS.		Furthermore, 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.		Currently the rtc_lock is a bit academic since this		driver is non-modular, but someday... ?         Paul G.	*/		spin_lock_irqsave(&rtc_lock, flags);		cmos_disks = CMOS_READ(0x12);		spin_unlock_irqrestore(&rtc_lock, flags);		if (cmos_disks & 0xf0) {			if (cmos_disks & 0x0f)				NR_HD = 2;			else				NR_HD = 1;		}	}#endif /* __i386__ */#ifdef __arm__	if (!NR_HD) {		/* We don't know anything about the drive.  This means		 * that you *MUST* specify the drive parameters to the		 * kernel yourself.		 */		printk("hd: no drives specified - use hd=cyl,head,sectors"			" on kernel command line\n");	}#endif	if (!NR_HD)		goto out;	for (drive=0 ; drive < NR_HD ; drive++) {		struct gendisk *disk = alloc_disk(64);		struct hd_i_struct *p = &hd_info[drive];		if (!disk)			goto Enomem;		disk->major = MAJOR_NR;		disk->first_minor = drive << 6;		disk->fops = &hd_fops;		sprintf(disk->disk_name, "hd%c", 'a'+drive);		disk->private_data = p;		set_capacity(disk, p->head * p->sect * p->cyl);		disk->queue = hd_queue;		p->unit = drive;		hd_gendisk[drive] = disk;		printk ("%s: %luMB, CHS=%d/%d/%d\n",			disk->disk_name, (unsigned long)get_capacity(disk)/2048,			p->cyl, p->head, p->sect);	}	if (request_irq(HD_IRQ, hd_interrupt, SA_INTERRUPT, "hd", NULL)) {		printk("hd: unable to get IRQ%d for the hard disk driver\n",			HD_IRQ);		goto out1;	}	if (!request_region(HD_DATA, 8, "hd")) {		printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);		goto out2;	}	if (!request_region(HD_CMD, 1, "hd(cmd)")) {		printk(KERN_WARNING "hd: port 0x%x busy\n", HD_CMD);		goto out3;	}	/* Let them fly */	for(drive=0; drive < NR_HD; drive++)		add_disk(hd_gendisk[drive]);	return 0;out3:	release_region(HD_DATA, 8);out2:	free_irq(HD_IRQ, NULL);out1:	for (drive = 0; drive < NR_HD; drive++)		put_disk(hd_gendisk[drive]);	NR_HD = 0;out:	del_timer(&device_timer);	unregister_blkdev(MAJOR_NR,"hd");	blk_cleanup_queue(hd_queue);	return -1;Enomem:	while (drive--)		put_disk(hd_gendisk[drive]);	goto out;}static int parse_hd_setup (char *line) {	int ints[6];	(void) get_options(line, ARRAY_SIZE(ints), ints);	hd_setup(NULL, ints);	return 1;}__setup("hd=", parse_hd_setup);module_init(hd_init);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -