📄 hd.c
字号:
/* * linux/arch/arm/drivers/block/hd.c * [ origional file: linux/drivers/block/hd.c ] * * Copyright (C) 1991, 1992 Linus Torvalds * Modified 1995 Russell King for ARM processor. *//* * 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. * * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug * in the early extended-partition checks and added DM partitions * * IRQ-unmask, drive-id, multiple-mode, support for ">16 heads", * and general streamlining by Mark Lord. */#define DEFAULT_MULT_COUNT 0 /* set to 0 to disable multiple mode at boot */#define DEFAULT_UNMASK_INTR 0 /* set to 0 to *NOT* unmask irq's more often */#include <asm/irq.h>#include <linux/errno.h>#include <linux/signal.h>#include <linux/sched.h>#include <linux/timer.h>#include <linux/fs.h>#include <linux/kernel.h>#include <linux/hdreg.h>#include <linux/genhd.h>#include <linux/malloc.h>#include <linux/string.h>#include <linux/ioport.h>#define REALLY_SLOW_IO#include <asm/system.h>#include <asm/io.h>#include <asm/segment.h>#define MAJOR_NR HD_MAJOR#include <linux/blk.h>#undef HD_IRQ#define HD_IRQ 11static int revalidate_hddisk(kdev_t, int);#define HD_DELAY 0#define MAX_ERRORS 16 /* Max read/write errors/sector */#define RESET_FREQ 8 /* Reset controller every 8th retry */#define RECAL_FREQ 4 /* Recalibrate every 4th retry */#define MAX_HD 2#define STAT_OK (READY_STAT|SEEK_STAT)#define OK_STATUS(s) (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)static void recal_intr(void);static void bad_rw_intr(void);static char recalibrate[MAX_HD];static char special_op[MAX_HD];static int access_count[MAX_HD];static char busy[MAX_HD];static struct wait_queue * busy_wait;static int reset;static int hd_error;/* * This struct defines the HD's and their types. */struct hd_i_struct { unsigned int head,sect,cyl,wpcom,lzone,ctl; };static struct hd_driveid *hd_ident_info[MAX_HD]; #ifdef HD_TYPEstatic struct hd_i_struct hd_info[] = { HD_TYPE };struct hd_i_struct bios_info[] = { HD_TYPE };static int NR_HD = ((sizeof (hd_info))/(sizeof (struct hd_i_struct)));#elsestatic struct hd_i_struct hd_info[MAX_HD];struct hd_i_struct bios_info[MAX_HD];static int NR_HD;#endifstatic struct hd_struct hd[MAX_HD<<6];static int hd_sizes[MAX_HD<<6];static int hd_blocksizes[MAX_HD<<6];static int hd_hardsectsizes[MAX_HD<<6];#if (HD_DELAY > 0)unsigned long last_req;unsigned long read_timer(void){ unsigned long t, flags; int i; save_flags_cli (flags); t = jiffies * 11932; outb_p(0, 0x43); i = inb_p(0x40); i |= inb(0x40) << 8; restore_flags(flags); return(t - i);}#endifvoid hd_setup(char *str, int *ints){ int hdind = 0; if (ints[0] != 3) return; if (bios_info[0].head != 0) hdind=1; bios_info[hdind].head = hd_info[hdind].head = ints[2]; bios_info[hdind].sect = hd_info[hdind].sect = ints[3]; bios_info[hdind].cyl = hd_info[hdind].cyl = ints[1]; bios_info[hdind].wpcom = hd_info[hdind].wpcom = 0; bios_info[hdind].lzone = hd_info[hdind].lzone = ints[1]; bios_info[hdind].ctl = hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0); NR_HD = hdind+1;}static void dump_status (const char *msg, unsigned int stat){ unsigned long flags; char devc; devc = CURRENT ? 'a' + DEVICE_NR(CURRENT->rq_dev) : '?'; save_flags (flags); sti(); printk("hd%c: %s: status=0x%02x { ", devc, msg, stat & 0xff); if (stat & BUSY_STAT) printk("Busy "); if (stat & READY_STAT) printk("DriveReady "); if (stat & WRERR_STAT) printk("WriteFault "); if (stat & SEEK_STAT) printk("SeekComplete "); if (stat & DRQ_STAT) printk("DataRequest "); if (stat & ECC_STAT) printk("CorrectedError "); if (stat & INDEX_STAT) printk("Index "); if (stat & ERR_STAT) printk("Error "); printk("}\n"); if ((stat & ERR_STAT) == 0) { hd_error = 0; } else { hd_error = inb(HD_ERROR); printk("hd%c: %s: error=0x%02x { ", devc, msg, hd_error & 0xff); if (hd_error & BBD_ERR) printk("BadSector "); if (hd_error & ECC_ERR) printk("UncorrectableError "); if (hd_error & ID_ERR) printk("SectorIdNotFound "); if (hd_error & ABRT_ERR) printk("DriveStatusError "); if (hd_error & TRK0_ERR) printk("TrackZeroNotFound "); if (hd_error & MARK_ERR) printk("AddrMarkNotFound "); printk("}"); if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) { printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL), inb(HD_CURRENT) & 0xf, inb(HD_SECTOR)); if (CURRENT) printk(", sector=%ld", CURRENT->sector); } printk("\n"); } restore_flags (flags);}void check_status(void){ int i = inb_p(HD_STATUS); if (!OK_STATUS(i)) { dump_status("check_status", i); bad_rw_intr(); }}static int controller_busy(void){ int retries = 100000; unsigned char status; do { status = inb_p(HD_STATUS); } while ((status & BUSY_STAT) && --retries); return status;}static int status_ok(void){ unsigned char status = inb_p(HD_STATUS); if (status & BUSY_STAT) return 1; /* Ancient, but does it make sense??? */ if (status & WRERR_STAT) return 0; if (!(status & READY_STAT)) return 0; if (!(status & SEEK_STAT)) return 0; return 1;}static int controller_ready(unsigned int drive, unsigned int head){ int retry = 100; do { if (controller_busy() & BUSY_STAT) return 0; outb_p(0xA0 | (drive<<4) | head, HD_CURRENT); if (status_ok()) return 1; } while (--retry); return 0;}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)){ unsigned short port;#if (HD_DELAY > 0) while (read_timer() - last_req < HD_DELAY) /* nothing */;#endif if (reset) return; if (!controller_ready(drive, head)) { reset = 1; return; } SET_INTR(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_p(cmd,++port);}static void hd_request (void);static unsigned int identified [MAX_HD]; /* 1 = drive ID already displayed */static unsigned int unmask_intr [MAX_HD]; /* 1 = unmask IRQs during I/O */static unsigned int max_mult [MAX_HD]; /* max sectors for MultMode */static unsigned int mult_req [MAX_HD]; /* requested MultMode count */static unsigned int mult_count [MAX_HD]; /* currently enabled MultMode count */static struct request WCURRENT;static void fixstring (unsigned char *s, int bytecount){ unsigned char *p, *end = &s[bytecount &= ~1]; /* bytecount must be even */ /* convert from big-endian to little-endian */ for (p = end ; p != s;) { unsigned short *pp = (unsigned short *) (p -= 2); *pp = (*pp >> 8) | (*pp << 8); } /* strip leading blanks */ while (s != end && *s == ' ') ++s; /* compress internal blanks and strip trailing blanks */ while (s != end && *s) { if (*s++ != ' ' || (s != end && *s && *s != ' ')) *p++ = *(s-1); } /* wipe out trailing garbage */ while (p != end) *p++ = '\0';}static void identify_intr(void){ unsigned int dev = DEVICE_NR(CURRENT->rq_dev); unsigned short stat = inb_p(HD_STATUS); struct hd_driveid *id = hd_ident_info[dev]; if (unmask_intr[dev]) sti(); if (stat & (BUSY_STAT|ERR_STAT)) { printk (" hd%c: non-IDE device, %dMB, CHS=%d/%d/%d\n", dev+'a', hd_info[dev].cyl*hd_info[dev].head*hd_info[dev].sect / 2048, hd_info[dev].cyl, hd_info[dev].head, hd_info[dev].sect); if (id != NULL) { hd_ident_info[dev] = NULL; kfree_s (id, 512); } } else { insw(HD_DATA, id, 256); /* get ID info */ max_mult[dev] = id->max_multsect; if ((id->field_valid&1) && id->cur_cyls && id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) { /* * Extract the physical drive geometry for our use. * Note that we purposely do *not* update the bios_info. * This way, programs that use it (like fdisk) will * still have the same logical view as the BIOS does, * which keeps the partition table from being screwed. */ hd_info[dev].cyl = id->cur_cyls; hd_info[dev].head = id->cur_heads; hd_info[dev].sect = id->cur_sectors; } fixstring (id->serial_no, sizeof(id->serial_no)); fixstring (id->fw_rev, sizeof(id->fw_rev)); fixstring (id->model, sizeof(id->model)); printk (" hd%c: %.40s, %dMB w/%dKB Cache, CHS=%d/%d/%d, MaxMult=%d\n", dev+'a', id->model, id->cyls*id->heads*id->sectors/2048, id->buf_size/2, bios_info[dev].cyl, bios_info[dev].head, bios_info[dev].sect, id->max_multsect); /* * Early model Quantum drives go weird at this point, * but doing a recalibrate seems to "fix" them. * (Doing a full reset confuses some other model Quantums) */ if (!strncmp(id->model, "QUANTUM", 7)) special_op[dev] = recalibrate[dev] = 1; }#if (HD_DELAY > 0) last_req = read_timer();#endif hd_request(); return;}static void set_multmode_intr(void){ unsigned int dev = DEVICE_NR(CURRENT->rq_dev), stat = inb_p(HD_STATUS); if (unmask_intr[dev]) sti(); if (stat & (BUSY_STAT|ERR_STAT)) { mult_req[dev] = mult_count[dev] = 0; dump_status("set multmode failed", stat); } else { if ((mult_count[dev] = mult_req[dev])) printk (" hd%c: enabled %d-sector multiple mode\n", dev+'a', mult_count[dev]); else printk (" hd%c: disabled multiple mode\n", dev+'a'); }#if (HD_DELAY > 0) last_req = read_timer();#endif hd_request(); return;}static int drive_busy(void){ unsigned int i; unsigned char c; for (i = 0; i < 500000 ; i++) { c = inb_p(HD_STATUS); if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK) return 0; } dump_status("reset timed out", c); return 1;}static void reset_controller(void){ int i; outb_p(4,HD_CMD); for(i = 0; i < 1000; i++) barrier(); outb_p(hd_info[0].ctl & 0x0f,HD_CMD); for(i = 0; i < 1000; i++) barrier(); if (drive_busy()) printk("hd: controller still busy\n"); else if ((hd_error = inb(HD_ERROR)) != 1) printk("hd: controller reset failed: %02x\n",hd_error);}static void reset_hd(void){ static int i;repeat: if (reset) { reset = 0; i = -1; reset_controller(); } else { check_status(); if (reset) goto repeat; } if (++i < NR_HD) { special_op[i] = recalibrate[i] = 1; if (unmask_intr[i]) { unmask_intr[i] = DEFAULT_UNMASK_INTR; printk("hd%c: reset irq-unmasking to %d\n",i+'a', DEFAULT_UNMASK_INTR); } if (mult_req[i] || mult_count[i]) { mult_count[i] = 0; mult_req[i] = DEFAULT_MULT_COUNT; printk("hd%c: reset multiple mode to %d\n",i+'a', DEFAULT_MULT_COUNT); } hd_out(i,hd_info[i].sect,hd_info[i].sect,hd_info[i].head-1, hd_info[i].cyl,WIN_SPECIFY,&reset_hd); if (reset) goto repeat; } else hd_request();}/* * Ok, don't know what to do with the unexpected interrupts: on some machines * doing a reset and a retry seems to result in an eternal loop. Right now I * ignore it, and just set the timeout. * * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the * drive enters "idle", "standby", or "sleep" mode, so if the status looks * "good", we just ignore the interrupt completely. */void unexpected_hd_interrupt(void){ unsigned int stat = inb_p(HD_STATUS); if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) { dump_status ("unexpected interrupt", stat); SET_TIMER; }}/* * bad_rw_intr() now tries to be a bit smarter and does things * according to the error returned by the controller. * -Mika Liljeberg (liljeber@cs.Helsinki.FI) */static void bad_rw_intr(void){ int dev; if (!CURRENT) return; dev = DEVICE_NR(CURRENT->rq_dev); if (++CURRENT->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) { end_request(0); special_op[dev] = recalibrate[dev] = 1; } else if (CURRENT->errors % RESET_FREQ == 0) reset = 1; else if ((hd_error & TRK0_ERR) || CURRENT->errors % RECAL_FREQ == 0) special_op[dev] = recalibrate[dev] = 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){ unsigned int dev = DEVICE_NR(CURRENT->rq_dev); int i, retries = 100000, msect = mult_count[dev], nsect; if (unmask_intr[dev]) sti(); /* permit other IRQs during xfer */ 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: if (msect) { if ((nsect = CURRENT->current_nr_sectors) > msect) nsect = msect; msect -= nsect; } else nsect = 1; insw(HD_DATA,CURRENT->buffer,nsect<<8); CURRENT->sector += nsect; CURRENT->buffer += nsect<<9; CURRENT->errors = 0; i = (CURRENT->nr_sectors -= nsect);#ifdef DEBUG printk("hd%c: read: sectors(%ld-%ld), remaining=%ld, buffer=0x%08lx\n", dev+'a', CURRENT->sector, CURRENT->sector+nsect, CURRENT->nr_sectors, (unsigned long) CURRENT->buffer+(nsect<<9));#endif if ((CURRENT->current_nr_sectors -= nsect) <= 0) end_request(1); if (i > 0) { if (msect) goto ok_to_read; SET_INTR(&read_intr); return; } (void) inb_p(HD_STATUS);#if (HD_DELAY > 0) last_req = read_timer();#endif if (CURRENT) hd_request(); return;}static inline void multwrite (unsigned int dev){ unsigned int mcount = mult_count[dev]; while (mcount--) { outsw(HD_DATA,WCURRENT.buffer,256); if (!--WCURRENT.nr_sectors) return; WCURRENT.buffer += 512; if (!--WCURRENT.current_nr_sectors) { WCURRENT.bh = WCURRENT.bh->b_reqnext; if (WCURRENT.bh == NULL) panic("buffer list corrupted\n"); WCURRENT.current_nr_sectors = WCURRENT.bh->b_size>>9; WCURRENT.buffer = WCURRENT.bh->b_data; } }}static void multwrite_intr(void){ int i; unsigned int dev = DEVICE_NR(WCURRENT.rq_dev); if (unmask_intr[dev]) sti(); if (OK_STATUS(i=inb_p(HD_STATUS))) { if (i & DRQ_STAT) { if (WCURRENT.nr_sectors) { multwrite(dev); SET_INTR(&multwrite_intr); return; } } else { if (!WCURRENT.nr_sectors) { /* all done? */ for (i = CURRENT->nr_sectors; i > 0;){ i -= CURRENT->current_nr_sectors; end_request(1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -