?? hd.c
字號:
--CURRENT->current_nr_sectors; CURRENT->buffer += 512; if (!i || (CURRENT->bh && !SUBSECTOR(i))) end_request(1); if (i > 0) { SET_INTR(&write_intr); outsw(HD_DATA,CURRENT->buffer,256); sti(); } 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){ unsigned int dev; DEVICE_INTR = NULL; if (QUEUE_EMPTY) return; disable_irq(HD_IRQ); sti(); reset = 1; dev = DEVICE_NR(CURRENT->rq_dev); printk("hd%c: timeout\n", dev+'a'); if (++CURRENT->errors >= MAX_ERRORS) {#ifdef DEBUG printk("hd%c: too many errors\n", dev+'a');#endif end_request(0); } cli(); hd_request(); enable_irq(HD_IRQ);}int do_special_op (unsigned int dev){ if (recalibrate[dev]) { recalibrate[dev] = 0; hd_out(dev,hd_info[dev].sect,0,0,0,WIN_RESTORE,&recal_intr); return reset; } if (hd_info[dev].head > 16) { printk ("hd%c: cannot handle device with more than 16 heads - giving up\n", dev+'a'); end_request(0); } special_op[dev] = 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 dev, block, nsect, sec, track, head, cyl; if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE) return; if (DEVICE_INTR) return;repeat: del_timer(&device_timer); sti(); INIT_REQUEST; if (reset) { cli(); reset_hd(); return; } dev = MINOR(CURRENT->rq_dev); block = CURRENT->sector; nsect = CURRENT->nr_sectors; if (dev >= (NR_HD<<6) || block >= hd[dev].nr_sects || ((block+nsect) > hd[dev].nr_sects)) {#ifdef DEBUG if (dev >= (NR_HD<<6)) printk("hd: bad minor number: device=%s\n", kdevname(CURRENT->rq_dev)); else printk("hd%c: bad access: block=%d, count=%d\n", (MINOR(CURRENT->rq_dev)>>6)+'a', block, nsect);#endif end_request(0); goto repeat; } block += hd[dev].start_sect; dev >>= 6; if (special_op[dev]) { if (do_special_op(dev)) goto repeat; return; } sec = block % hd_info[dev].sect + 1; track = block / hd_info[dev].sect; head = track % hd_info[dev].head; cyl = track / hd_info[dev].head;#ifdef DEBUG printk("hd%c: %sing: CHS=%d/%d/%d, sectors=%d, buffer=0x%08lx\n", dev+'a', (CURRENT->cmd == READ)?"read":"writ", cyl, head, sec, nsect, (unsigned long) CURRENT->buffer);#endif if (CURRENT->cmd == READ) { hd_out(dev,nsect,sec,head,cyl,WIN_READ,&read_intr); if (reset) goto repeat; return; } if (CURRENT->cmd == WRITE) { hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr); if (reset) goto repeat; if (wait_DRQ()) { bad_rw_intr(); goto repeat; } outsw(HD_DATA,CURRENT->buffer,256); return; } panic("unknown hd-command");}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_geometry *loc = (struct hd_geometry *) arg; int dev; if ((!inode) || !(inode->i_rdev)) return -EINVAL; dev = DEVICE_NR(inode->i_rdev); if (dev >= NR_HD) return -EINVAL; switch (cmd) { case HDIO_GETGEO: { struct hd_geometry g; if (!loc) return -EINVAL; g.heads = hd_info[dev].head; g.sectors = hd_info[dev].sect; g.cylinders = hd_info[dev].cyl; g.start = hd[MINOR(inode->i_rdev)].start_sect; return copy_to_user(loc, &g, sizeof g) ? -EFAULT : 0; } case BLKGETSIZE: /* Return device size */ if (!arg) return -EINVAL; return put_user(hd[MINOR(inode->i_rdev)].nr_sects, (long *) arg); case BLKRRPART: /* Re-read partition tables */ if (!capable(CAP_SYS_ADMIN)) return -EACCES; return revalidate_hddisk(inode->i_rdev, 1); case BLKROSET: case BLKROGET: case BLKRASET: case BLKRAGET: case BLKFLSBUF: case BLKPG: return blk_ioctl(inode->i_rdev, cmd, arg); default: return -EINVAL; }}static int hd_open(struct inode * inode, struct file * filp){ int target; target = DEVICE_NR(inode->i_rdev); if (target >= NR_HD) return -ENODEV; while (busy[target]) sleep_on(&busy_wait); access_count[target]++; return 0;}/* * Releasing a block device means we sync() it, so that it can safely * be forgotten about... */static int hd_release(struct inode * inode, struct file * file){ int target = DEVICE_NR(inode->i_rdev); access_count[target]--; return 0;}extern struct block_device_operations hd_fops;static struct gendisk hd_gendisk = { MAJOR_NR, /* Major number */ "hd", /* Major name */ 6, /* Bits to shift to get real from partition */ 1 << 6, /* Number of partitions per real */ hd, /* hd struct */ hd_sizes, /* block sizes */ 0, /* number */ NULL, /* internal use, not presently used */ NULL, /* next */ &hd_fops, /* file operations */}; static void hd_interrupt(int irq, void *dev_id, struct pt_regs *regs){ void (*handler)(void) = DEVICE_INTR; DEVICE_INTR = NULL; del_timer(&device_timer); if (!handler) handler = unexpected_hd_interrupt; handler(); sti();}static struct block_device_operations hd_fops = { open: hd_open, release: hd_release, 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 void __init hd_geninit(void){ int drive; for(drive=0; drive < (MAX_HD << 6); drive++) { hd_blocksizes[drive] = 1024; hd_hardsectsizes[drive] = 512; } blksize_size[MAJOR_NR] = hd_blocksizes; hardsect_size[MAJOR_NR] = hd_hardsectsizes;#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 for (drive=0 ; drive < NR_HD ; drive++) { hd[drive<<6].nr_sects = hd_info[drive].head * hd_info[drive].sect * hd_info[drive].cyl; printk ("hd%c: %ldMB, CHS=%d/%d/%d\n", drive+'a', hd[drive<<6].nr_sects / 2048, hd_info[drive].cyl, hd_info[drive].head, hd_info[drive].sect); } if (!NR_HD) return; 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); NR_HD = 0; return; } request_region(HD_DATA, 8, "hd"); request_region(HD_CMD, 1, "hd(cmd)"); hd_gendisk.nr_real = NR_HD; for(drive=0; drive < NR_HD; drive++) register_disk(&hd_gendisk, MKDEV(MAJOR_NR,drive<<6), 1<<6, &hd_fops, hd_info[drive].head * hd_info[drive].sect * hd_info[drive].cyl);}int __init hd_init(void){ if (devfs_register_blkdev(MAJOR_NR,"hd",&hd_fops)) { printk("hd: unable to get major %d for hard disk\n",MAJOR_NR); return -1; } blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST); read_ahead[MAJOR_NR] = 8; /* 8 sector (4kB) read-ahead */ hd_gendisk.next = gendisk_head; gendisk_head = &hd_gendisk; init_timer(&device_timer); device_timer.function = hd_times_out; hd_geninit(); return 0;}#define DEVICE_BUSY busy[target]#define USAGE access_count[target]#define CAPACITY (hd_info[target].head*hd_info[target].sect*hd_info[target].cyl)/* We assume that the BIOS parameters do not change, so the disk capacity will not change */#undef MAYBE_REINIT#define GENDISK_STRUCT hd_gendisk/* * This routine is called to flush all partitions and partition tables * for a changed disk, and then re-read the new partition table. * If we are revalidating a disk because of a media change, then we * enter with usage == 0. If we are using an ioctl, we automatically have * usage == 1 (we need an open channel to use an ioctl :-), so this * is our limit. */static int revalidate_hddisk(kdev_t dev, int maxusage){ int target; struct gendisk * gdev; int max_p; int start; int i; long flags; target = DEVICE_NR(dev); gdev = &GENDISK_STRUCT; save_flags(flags); cli(); if (DEVICE_BUSY || USAGE > maxusage) { restore_flags(flags); return -EBUSY; } DEVICE_BUSY = 1; restore_flags(flags); max_p = gdev->max_p; start = target << gdev->minor_shift; for (i=max_p - 1; i >=0 ; i--) { int minor = start + i; kdev_t devi = MKDEV(MAJOR_NR, minor); struct super_block *sb = get_super(devi); sync_dev(devi); if (sb) invalidate_inodes(sb); invalidate_buffers(devi); gdev->part[minor].start_sect = 0; gdev->part[minor].nr_sects = 0; }#ifdef MAYBE_REINIT MAYBE_REINIT;#endif grok_partitions(gdev, target, 1<<6, CAPACITY); DEVICE_BUSY = 0; wake_up(&busy_wait); return 0;}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);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -