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

📄 hd.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
{	struct request *req = CURRENT;	if (req != NULL) {		struct hd_i_struct *disk = req->rq_disk->private_data;		if (++req->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {			end_request(req, 0);			disk->special_op = disk->recalibrate = 1;		} else if (req->errors % RESET_FREQ == 0)			reset = 1;		else if ((hd_error & TRK0_ERR) || req->errors % RECAL_FREQ == 0)			disk->special_op = disk->recalibrate = 1;		/* Otherwise just retry */	}}static inline int wait_DRQ(void){	int retries = 100000, stat;	while (--retries > 0)		if ((stat = inb_p(HD_STATUS)) & DRQ_STAT)			return 0;	dump_status("wait_DRQ", stat);	return -1;}static void read_intr(void){	struct request *req;	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_data_dir(req) == READ ? "read" : "writ",		cyl, head, sec, nsect, req->buffer);#endif	if (blk_fs_request(req)) {		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 (struct request_queue * q){	disable_irq(HD_IRQ);	hd_request();	enable_irq(HD_IRQ);}static int hd_getgeo(struct block_device *bdev, struct hd_geometry *geo){	struct hd_i_struct *disk = bdev->bd_disk->private_data;	geo->heads = disk->head;	geo->sectors = disk->sect;	geo->cylinders = disk->cyl;	return 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){	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 = {	.getgeo =	hd_getgeo,};/* * This is the hard disk IRQ description. The IRQF_DISABLED 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);	if (!NR_HD) {		/*		 * We don't know anything about the drive.  This means		 * that you *MUST* specify the drive parameters to the		 * kernel yourself.		 *		 * If we were on an i386, we used to read this info from		 * the BIOS or CMOS.  This doesn't work all that well,		 * since this assumes that this is a primary or secondary		 * drive, and if we're using this legacy driver, it's		 * probably an auxilliary controller added to recover		 * legacy data off an ST-506 drive.  Either way, it's		 * definitely safest to have the user explicitly specify		 * the information.		 */		printk("hd: no drives specified - use hd=cyl,head,sectors"			" on kernel command line\n");		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, IRQF_DISABLED, "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 __init 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 + -