2 * linux/drivers/ide/hd.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * This is the low-level hd interrupt support. It traverses the
9 * request-list, using interrupts to jump between functions. As
10 * all the functions are called within interrupts, we may not
11 * sleep. Special care is recommended.
13 * modified by Drew Eckhardt to check nr of hd's from the CMOS.
15 * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
16 * in the early extended-partition checks and added DM partitions
18 * IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
19 * and general streamlining by Mark Lord.
21 * Removed 99% of above. Use Mark's ide driver for those options.
22 * This is now a lightweight ST-506 driver. (Paul Gortmaker)
24 * Modified 1995 Russell King for ARM processor.
26 * Bugfix: max_sectors must be <= 255 or the wheels tend to come
27 * off in a hurry once you queue things up - Paul G. 02/2001
30 /* Uncomment the following if you want verbose error reports. */
31 /* #define VERBOSE_ERRORS */
33 #include <linux/errno.h>
34 #include <linux/signal.h>
35 #include <linux/sched.h>
36 #include <linux/timer.h>
38 #include <linux/devfs_fs_kernel.h>
39 #include <linux/kernel.h>
40 #include <linux/hdreg.h>
41 #include <linux/genhd.h>
42 #include <linux/slab.h>
43 #include <linux/string.h>
44 #include <linux/ioport.h>
45 #include <linux/mc146818rtc.h> /* CMOS defines */
46 #include <linux/init.h>
47 #include <linux/blkpg.h>
49 #define REALLY_SLOW_IO
50 #include <asm/system.h>
52 #include <asm/uaccess.h>
54 #define MAJOR_NR HD_MAJOR
55 #include <linux/blk.h>
62 #define HD_IRQ IRQ_HARDDISK
65 static int revalidate_hddisk(kdev_t, int);
69 #define MAX_ERRORS 16 /* Max read/write errors/sector */
70 #define RESET_FREQ 8 /* Reset controller every 8th retry */
71 #define RECAL_FREQ 4 /* Recalibrate every 4th retry */
74 #define STAT_OK (READY_STAT|SEEK_STAT)
75 #define OK_STATUS(s) (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
77 static void recal_intr(void);
78 static void bad_rw_intr(void);
80 static char recalibrate[MAX_HD];
81 static char special_op[MAX_HD];
82 static int access_count[MAX_HD];
83 static char busy[MAX_HD];
84 static DECLARE_WAIT_QUEUE_HEAD(busy_wait);
89 #define SUBSECTOR(block) (CURRENT->current_nr_sectors > 0)
92 * This struct defines the HD's and their types.
95 unsigned int head,sect,cyl,wpcom,lzone,ctl;
99 static struct hd_i_struct hd_info[] = { HD_TYPE };
100 static int NR_HD = ((sizeof (hd_info))/(sizeof (struct hd_i_struct)));
102 static struct hd_i_struct hd_info[MAX_HD];
106 static struct hd_struct hd[MAX_HD<<6];
107 static int hd_sizes[MAX_HD<<6];
108 static int hd_blocksizes[MAX_HD<<6];
109 static int hd_hardsectsizes[MAX_HD<<6];
110 static int hd_maxsect[MAX_HD<<6];
112 static struct timer_list device_timer;
116 mod_timer(&device_timer, jiffies + TIMEOUT_VALUE); \
119 #define CLEAR_TIMER del_timer(&device_timer);
123 #define SET_INTR(x) \
124 if ((DEVICE_INTR = (x)) != NULL) \
131 unsigned long last_req;
133 unsigned long read_timer(void)
135 unsigned long t, flags;
138 spin_lock_irqsave(&io_request_lock, flags);
143 spin_unlock_irqrestore(&io_request_lock, flags);
148 void __init hd_setup(char *str, int *ints)
154 if (hd_info[0].head != 0)
156 hd_info[hdind].head = ints[2];
157 hd_info[hdind].sect = ints[3];
158 hd_info[hdind].cyl = ints[1];
159 hd_info[hdind].wpcom = 0;
160 hd_info[hdind].lzone = ints[1];
161 hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);
165 static void dump_status (const char *msg, unsigned int stat)
170 devc = !QUEUE_EMPTY ? 'a' + DEVICE_NR(CURRENT->rq_dev) : '?';
173 #ifdef VERBOSE_ERRORS
174 printk("hd%c: %s: status=0x%02x { ", devc, msg, stat & 0xff);
175 if (stat & BUSY_STAT) printk("Busy ");
176 if (stat & READY_STAT) printk("DriveReady ");
177 if (stat & WRERR_STAT) printk("WriteFault ");
178 if (stat & SEEK_STAT) printk("SeekComplete ");
179 if (stat & DRQ_STAT) printk("DataRequest ");
180 if (stat & ECC_STAT) printk("CorrectedError ");
181 if (stat & INDEX_STAT) printk("Index ");
182 if (stat & ERR_STAT) printk("Error ");
184 if ((stat & ERR_STAT) == 0) {
187 hd_error = inb(HD_ERROR);
188 printk("hd%c: %s: error=0x%02x { ", devc, msg, hd_error & 0xff);
189 if (hd_error & BBD_ERR) printk("BadSector ");
190 if (hd_error & ECC_ERR) printk("UncorrectableError ");
191 if (hd_error & ID_ERR) printk("SectorIdNotFound ");
192 if (hd_error & ABRT_ERR) printk("DriveStatusError ");
193 if (hd_error & TRK0_ERR) printk("TrackZeroNotFound ");
194 if (hd_error & MARK_ERR) printk("AddrMarkNotFound ");
196 if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
197 printk(", CHS=%d/%d/%d",
198 (inb(HD_HCYL)<<8) + inb(HD_LCYL),
199 inb(HD_CURRENT) & 0xf, inb(HD_SECTOR));
201 printk(", sector=%ld", CURRENT->sector);
206 printk("hd%c: %s: status=0x%02x.\n", devc, msg, stat & 0xff);
207 if ((stat & ERR_STAT) == 0) {
210 hd_error = inb(HD_ERROR);
211 printk("hd%c: %s: error=0x%02x.\n", devc, msg, hd_error & 0xff);
213 #endif /* verbose errors */
214 restore_flags (flags);
217 void check_status(void)
219 int i = inb_p(HD_STATUS);
222 dump_status("check_status", i);
227 static int controller_busy(void)
229 int retries = 100000;
230 unsigned char status;
233 status = inb_p(HD_STATUS);
234 } while ((status & BUSY_STAT) && --retries);
238 static int status_ok(void)
240 unsigned char status = inb_p(HD_STATUS);
242 if (status & BUSY_STAT)
243 return 1; /* Ancient, but does it make sense??? */
244 if (status & WRERR_STAT)
246 if (!(status & READY_STAT))
248 if (!(status & SEEK_STAT))
253 static int controller_ready(unsigned int drive, unsigned int head)
258 if (controller_busy() & BUSY_STAT)
260 outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
267 static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect,
268 unsigned int head,unsigned int cyl,unsigned int cmd,
269 void (*intr_addr)(void))
274 while (read_timer() - last_req < HD_DELAY)
279 if (!controller_ready(drive, head)) {
284 outb_p(hd_info[drive].ctl,HD_CMD);
286 outb_p(hd_info[drive].wpcom>>2,++port);
287 outb_p(nsect,++port);
290 outb_p(cyl>>8,++port);
291 outb_p(0xA0|(drive<<4)|head,++port);
295 static void hd_request (void);
297 static int drive_busy(void)
302 for (i = 0; i < 500000 ; i++) {
303 c = inb_p(HD_STATUS);
304 if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
307 dump_status("reset timed out", c);
311 static void reset_controller(void)
316 for(i = 0; i < 1000; i++) barrier();
317 outb_p(hd_info[0].ctl & 0x0f,HD_CMD);
318 for(i = 0; i < 1000; i++) barrier();
320 printk("hd: controller still busy\n");
321 else if ((hd_error = inb(HD_ERROR)) != 1)
322 printk("hd: controller reset failed: %02x\n",hd_error);
325 static void reset_hd(void)
340 special_op[i] = recalibrate[i] = 1;
341 hd_out(i,hd_info[i].sect,hd_info[i].sect,hd_info[i].head-1,
342 hd_info[i].cyl,WIN_SPECIFY,&reset_hd);
349 void do_reset_hd(void)
357 * Ok, don't know what to do with the unexpected interrupts: on some machines
358 * doing a reset and a retry seems to result in an eternal loop. Right now I
359 * ignore it, and just set the timeout.
361 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
362 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
363 * "good", we just ignore the interrupt completely.
365 void unexpected_hd_interrupt(void)
367 unsigned int stat = inb_p(HD_STATUS);
369 if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
370 dump_status ("unexpected interrupt", stat);
376 * bad_rw_intr() now tries to be a bit smarter and does things
377 * according to the error returned by the controller.
378 * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
380 static void bad_rw_intr(void)
386 dev = DEVICE_NR(CURRENT->rq_dev);
387 if (++CURRENT->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
389 special_op[dev] = recalibrate[dev] = 1;
390 } else if (CURRENT->errors % RESET_FREQ == 0)
392 else if ((hd_error & TRK0_ERR) || CURRENT->errors % RECAL_FREQ == 0)
393 special_op[dev] = recalibrate[dev] = 1;
394 /* Otherwise just retry */
397 static inline int wait_DRQ(void)
399 int retries = 100000, stat;
401 while (--retries > 0)
402 if ((stat = inb_p(HD_STATUS)) & DRQ_STAT)
404 dump_status("wait_DRQ", stat);
408 static void read_intr(void)
410 int i, retries = 100000;
413 i = (unsigned) inb_p(HD_STATUS);
420 } while (--retries > 0);
421 dump_status("read_intr", i);
426 insw(HD_DATA,CURRENT->buffer,256);
428 CURRENT->buffer += 512;
430 i = --CURRENT->nr_sectors;
431 --CURRENT->current_nr_sectors;
433 printk("hd%c: read: sector %ld, remaining = %ld, buffer=0x%08lx\n",
434 dev+'a', CURRENT->sector, CURRENT->nr_sectors,
435 (unsigned long) CURRENT->buffer+512));
437 if (CURRENT->current_nr_sectors <= 0)
440 SET_INTR(&read_intr);
443 (void) inb_p(HD_STATUS);
445 last_req = read_timer();
452 static void write_intr(void)
455 int retries = 100000;
458 i = (unsigned) inb_p(HD_STATUS);
463 if ((CURRENT->nr_sectors <= 1) || (i & DRQ_STAT))
465 } while (--retries > 0);
466 dump_status("write_intr", i);
472 i = --CURRENT->nr_sectors;
473 --CURRENT->current_nr_sectors;
474 CURRENT->buffer += 512;
475 if (!i || (CURRENT->bh && !SUBSECTOR(i)))
478 SET_INTR(&write_intr);
479 outsw(HD_DATA,CURRENT->buffer,256);
483 last_req = read_timer();
490 static void recal_intr(void)
494 last_req = read_timer();
500 * This is another of the error-routines I don't know what to do with. The
501 * best idea seems to just set reset, and start all over again.
503 static void hd_times_out(unsigned long dummy)
513 dev = DEVICE_NR(CURRENT->rq_dev);
514 printk("hd%c: timeout\n", dev+'a');
515 if (++CURRENT->errors >= MAX_ERRORS) {
517 printk("hd%c: too many errors\n", dev+'a');
526 int do_special_op (unsigned int dev)
528 if (recalibrate[dev]) {
529 recalibrate[dev] = 0;
530 hd_out(dev,hd_info[dev].sect,0,0,0,WIN_RESTORE,&recal_intr);
533 if (hd_info[dev].head > 16) {
534 printk ("hd%c: cannot handle device with more than 16 heads - giving up\n", dev+'a');
542 * The driver enables interrupts as much as possible. In order to do this,
543 * (a) the device-interrupt is disabled before entering hd_request(),
544 * and (b) the timeout-interrupt is disabled before the sti().
546 * Interrupts are still masked (by default) whenever we are exchanging
547 * data/cmds with a drive, because some drives seem to have very poor
548 * tolerance for latency during I/O. The IDE driver has support to unmask
549 * interrupts for non-broken hardware, so use that driver if required.
551 static void hd_request(void)
553 unsigned int dev, block, nsect, sec, track, head, cyl;
555 if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE) return;
559 del_timer(&device_timer);
567 dev = MINOR(CURRENT->rq_dev);
568 block = CURRENT->sector;
569 nsect = CURRENT->nr_sectors;
570 if (dev >= (NR_HD<<6) || block >= hd[dev].nr_sects || ((block+nsect) > hd[dev].nr_sects)) {
572 if (dev >= (NR_HD<<6))
573 printk("hd: bad minor number: device=%s\n",
574 kdevname(CURRENT->rq_dev));
576 printk("hd%c: bad access: block=%d, count=%d\n",
577 (MINOR(CURRENT->rq_dev)>>6)+'a', block, nsect);
582 block += hd[dev].start_sect;
584 if (special_op[dev]) {
585 if (do_special_op(dev))
589 sec = block % hd_info[dev].sect + 1;
590 track = block / hd_info[dev].sect;
591 head = track % hd_info[dev].head;
592 cyl = track / hd_info[dev].head;
594 printk("hd%c: %sing: CHS=%d/%d/%d, sectors=%d, buffer=0x%08lx\n",
595 dev+'a', (CURRENT->cmd == READ)?"read":"writ",
596 cyl, head, sec, nsect, (unsigned long) CURRENT->buffer);
598 if (CURRENT->cmd == READ) {
599 hd_out(dev,nsect,sec,head,cyl,WIN_READ,&read_intr);
604 if (CURRENT->cmd == WRITE) {
605 hd_out(dev,nsect,sec,head,cyl,WIN_WRITE,&write_intr);
612 outsw(HD_DATA,CURRENT->buffer,256);
615 panic("unknown hd-command");
618 static void do_hd_request (request_queue_t * q)
625 static int hd_ioctl(struct inode * inode, struct file * file,
626 unsigned int cmd, unsigned long arg)
628 struct hd_geometry *loc = (struct hd_geometry *) arg;
631 if ((!inode) || !(inode->i_rdev))
633 dev = DEVICE_NR(inode->i_rdev);
639 struct hd_geometry g;
640 if (!loc) return -EINVAL;
641 g.heads = hd_info[dev].head;
642 g.sectors = hd_info[dev].sect;
643 g.cylinders = hd_info[dev].cyl;
644 g.start = hd[MINOR(inode->i_rdev)].start_sect;
645 return copy_to_user(loc, &g, sizeof g) ? -EFAULT : 0;
648 case BLKGETSIZE: /* Return device size */
649 return put_user(hd[MINOR(inode->i_rdev)].nr_sects,
650 (unsigned long *) arg);
652 return put_user((u64)hd[MINOR(inode->i_rdev)].nr_sects << 9,
655 case BLKRRPART: /* Re-read partition tables */
656 if (!capable(CAP_SYS_ADMIN))
658 return revalidate_hddisk(inode->i_rdev, 1);
666 return blk_ioctl(inode->i_rdev, cmd, arg);
673 static int hd_open(struct inode * inode, struct file * filp)
676 target = DEVICE_NR(inode->i_rdev);
681 sleep_on(&busy_wait);
682 access_count[target]++;
687 * Releasing a block device means we sync() it, so that it can safely
688 * be forgotten about...
690 static int hd_release(struct inode * inode, struct file * file)
692 int target = DEVICE_NR(inode->i_rdev);
693 access_count[target]--;
697 extern struct block_device_operations hd_fops;
699 static struct gendisk hd_gendisk = {
709 static void hd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
711 void (*handler)(void) = DEVICE_INTR;
714 del_timer(&device_timer);
716 handler = unexpected_hd_interrupt;
721 static struct block_device_operations hd_fops = {
728 * This is the hard disk IRQ description. The SA_INTERRUPT in sa_flags
729 * means we run the IRQ-handler with interrupts disabled: this is bad for
730 * interrupt latency, but anything else has led to problems on some
733 * We enable interrupts in some of the routines after making sure it's
736 static void __init hd_geninit(void)
740 for(drive=0; drive < (MAX_HD << 6); drive++) {
741 hd_blocksizes[drive] = 1024;
742 hd_hardsectsizes[drive] = 512;
743 hd_maxsect[drive]=255;
745 blksize_size[MAJOR_NR] = hd_blocksizes;
746 hardsect_size[MAJOR_NR] = hd_hardsectsizes;
747 max_sectors[MAJOR_NR] = hd_maxsect;
751 extern struct drive_info drive_info;
752 unsigned char *BIOS = (unsigned char *) &drive_info;
756 for (drive=0 ; drive<2 ; drive++) {
757 hd_info[drive].cyl = *(unsigned short *) BIOS;
758 hd_info[drive].head = *(2+BIOS);
759 hd_info[drive].wpcom = *(unsigned short *) (5+BIOS);
760 hd_info[drive].ctl = *(8+BIOS);
761 hd_info[drive].lzone = *(unsigned short *) (12+BIOS);
762 hd_info[drive].sect = *(14+BIOS);
763 #ifdef does_not_work_for_everybody_with_scsi_but_helps_ibm_vp
764 if (hd_info[drive].cyl && NR_HD == drive)
771 We query CMOS about hard disks : it could be that
772 we have a SCSI/ESDI/etc controller that is BIOS
773 compatible with ST-506, and thus showing up in our
774 BIOS table, but not register compatible, and therefore
777 Furthermore, we will assume that our ST-506 drives
778 <if any> are the primary drives in the system, and
779 the ones reflected as drive 1 or 2.
781 The first drive is stored in the high nibble of CMOS
782 byte 0x12, the second in the low nibble. This will be
783 either a 4 bit drive type or 0xf indicating use byte 0x19
784 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS.
786 Needless to say, a non-zero value means we have
787 an AT controller hard disk for that drive.
789 Currently the rtc_lock is a bit academic since this
790 driver is non-modular, but someday... ? Paul G.
793 spin_lock_irqsave(&rtc_lock, flags);
794 cmos_disks = CMOS_READ(0x12);
795 spin_unlock_irqrestore(&rtc_lock, flags);
797 if (cmos_disks & 0xf0) {
798 if (cmos_disks & 0x0f)
804 #endif /* __i386__ */
807 /* We don't know anything about the drive. This means
808 * that you *MUST* specify the drive parameters to the
811 printk("hd: no drives specified - use hd=cyl,head,sectors"
812 " on kernel command line\n");
816 for (drive=0 ; drive < NR_HD ; drive++) {
817 hd[drive<<6].nr_sects = hd_info[drive].head *
818 hd_info[drive].sect * hd_info[drive].cyl;
819 printk ("hd%c: %ldMB, CHS=%d/%d/%d\n", drive+'a',
820 hd[drive<<6].nr_sects / 2048, hd_info[drive].cyl,
821 hd_info[drive].head, hd_info[drive].sect);
826 if (request_irq(HD_IRQ, hd_interrupt, SA_INTERRUPT, "hd", NULL)) {
827 printk("hd: unable to get IRQ%d for the hard disk driver\n",
832 request_region(HD_DATA, 8, "hd");
833 request_region(HD_CMD, 1, "hd(cmd)");
835 hd_gendisk.nr_real = NR_HD;
837 for(drive=0; drive < NR_HD; drive++)
838 register_disk(&hd_gendisk, MKDEV(MAJOR_NR,drive<<6), 1<<6,
839 &hd_fops, hd_info[drive].head * hd_info[drive].sect *
843 int __init hd_init(void)
845 if (devfs_register_blkdev(MAJOR_NR,"hd",&hd_fops)) {
846 printk("hd: unable to get major %d for hard disk\n",MAJOR_NR);
849 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
850 read_ahead[MAJOR_NR] = 8; /* 8 sector (4kB) read-ahead */
851 add_gendisk(&hd_gendisk);
852 init_timer(&device_timer);
853 device_timer.function = hd_times_out;
858 #define DEVICE_BUSY busy[target]
859 #define USAGE access_count[target]
860 #define CAPACITY (hd_info[target].head*hd_info[target].sect*hd_info[target].cyl)
861 /* We assume that the BIOS parameters do not change, so the disk capacity
864 #define GENDISK_STRUCT hd_gendisk
867 * This routine is called to flush all partitions and partition tables
868 * for a changed disk, and then re-read the new partition table.
869 * If we are revalidating a disk because of a media change, then we
870 * enter with usage == 0. If we are using an ioctl, we automatically have
871 * usage == 1 (we need an open channel to use an ioctl :-), so this
874 static int revalidate_hddisk(kdev_t dev, int maxusage)
877 struct gendisk * gdev;
883 target = DEVICE_NR(dev);
884 gdev = &GENDISK_STRUCT;
886 spin_lock_irqsave(&io_request_lock, flags);
887 if (DEVICE_BUSY || USAGE > maxusage) {
888 spin_unlock_irqrestore(&io_request_lock, flags);
892 spin_unlock_irqrestore(&io_request_lock, flags);
895 start = target << gdev->minor_shift;
897 for (i=max_p - 1; i >=0 ; i--) {
898 int minor = start + i;
899 invalidate_device(MKDEV(MAJOR_NR, minor), 1);
900 gdev->part[minor].start_sect = 0;
901 gdev->part[minor].nr_sects = 0;
908 grok_partitions(gdev, target, 1<<6, CAPACITY);
915 static int parse_hd_setup (char *line) {
918 (void) get_options(line, ARRAY_SIZE(ints), ints);
919 hd_setup(NULL, ints);
923 __setup("hd=", parse_hd_setup);
925 module_init(hd_init);