2 * Copyright (C) 1991, 1992 Linus Torvalds
4 * This is the low-level hd interrupt support. It traverses the
5 * request-list, using interrupts to jump between functions. As
6 * all the functions are called within interrupts, we may not
7 * sleep. Special care is recommended.
9 * modified by Drew Eckhardt to check nr of hd's from the CMOS.
11 * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
12 * in the early extended-partition checks and added DM partitions
14 * IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
15 * and general streamlining by Mark Lord.
17 * Removed 99% of above. Use Mark's ide driver for those options.
18 * This is now a lightweight ST-506 driver. (Paul Gortmaker)
20 * Modified 1995 Russell King for ARM processor.
22 * Bugfix: max_sectors must be <= 255 or the wheels tend to come
23 * off in a hurry once you queue things up - Paul G. 02/2001
26 /* Uncomment the following if you want verbose error reports. */
27 /* #define VERBOSE_ERRORS */
29 #include <linux/errno.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/timer.h>
34 #include <linux/kernel.h>
35 #include <linux/genhd.h>
36 #include <linux/slab.h>
37 #include <linux/string.h>
38 #include <linux/ioport.h>
39 #include <linux/mc146818rtc.h> /* CMOS defines */
40 #include <linux/init.h>
41 #include <linux/blkpg.h>
42 #include <linux/hdreg.h>
44 #define REALLY_SLOW_IO
45 #include <asm/system.h>
47 #include <asm/uaccess.h>
56 #define HD_IRQ IRQ_HARDDISK
59 /* Hd controller regster ports */
61 #define HD_DATA 0x640 /* _CTL when writing */
62 #define HD_ERROR 0x642 /* see err-bits */
63 #define HD_NSECTOR 0x644 /* nr of sectors to read/write */
64 #define HD_SECTOR 0x646 /* starting sector */
65 #define HD_LCYL 0x648 /* starting cylinder */
66 #define HD_HCYL 0x64a /* high byte of starting cyl */
67 #define HD_CURRENT 0x64c /* 101dhhhh , d=drive, hhhh=head */
68 #define HD_STATUS 0x64e /* see status-bits */
69 #define HD_FEATURE HD_ERROR /* same io address, read=error, write=feature */
70 #define HD_PRECOMP HD_FEATURE /* obsolete use of this port - predates IDE */
71 #define HD_COMMAND HD_STATUS /* same io address, read=status, write=cmd */
73 #define HD_CMD 0x74c /* used for resets */
74 #define HD_ALTSTATUS 0x74c /* same as HD_STATUS but doesn't clear irq */
76 /* Bits of HD_STATUS */
78 #define INDEX_STAT 0x02
79 #define ECC_STAT 0x04 /* Corrected error */
81 #define SEEK_STAT 0x10
82 #define SERVICE_STAT SEEK_STAT
83 #define WRERR_STAT 0x20
84 #define READY_STAT 0x40
85 #define BUSY_STAT 0x80
87 /* Bits for HD_ERROR */
88 #define MARK_ERR 0x01 /* Bad address mark */
89 #define TRK0_ERR 0x02 /* couldn't find track 0 */
90 #define ABRT_ERR 0x04 /* Command aborted */
91 #define MCR_ERR 0x08 /* media change request */
92 #define ID_ERR 0x10 /* ID field not found */
93 #define MC_ERR 0x20 /* media changed */
94 #define ECC_ERR 0x40 /* Uncorrectable ECC error */
95 #define BBD_ERR 0x80 /* pre-EIDE meaning: block marked bad */
96 #define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */
98 static spinlock_t hd_lock = SPIN_LOCK_UNLOCKED;
99 static struct request_queue *hd_queue;
101 #define CURRENT elv_next_request(hd_queue)
103 #define TIMEOUT_VALUE (6*HZ)
106 #define MAX_ERRORS 16 /* Max read/write errors/sector */
107 #define RESET_FREQ 8 /* Reset controller every 8th retry */
108 #define RECAL_FREQ 4 /* Recalibrate every 4th retry */
111 #define STAT_OK (READY_STAT|SEEK_STAT)
112 #define OK_STATUS(s) (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
114 static void recal_intr(void);
115 static void bad_rw_intr(void);
121 * This struct defines the HD's and their types.
124 unsigned int head,sect,cyl,wpcom,lzone,ctl;
131 static struct hd_i_struct hd_info[] = { HD_TYPE };
132 static int NR_HD = ((sizeof (hd_info))/(sizeof (struct hd_i_struct)));
134 static struct hd_i_struct hd_info[MAX_HD];
138 static struct gendisk *hd_gendisk[MAX_HD];
140 static struct timer_list device_timer;
142 #define TIMEOUT_VALUE (6*HZ)
146 mod_timer(&device_timer, jiffies + TIMEOUT_VALUE); \
149 static void (*do_hd)(void) = NULL;
150 #define SET_HANDLER(x) \
151 if ((do_hd = (x)) != NULL) \
154 del_timer(&device_timer);
158 unsigned long last_req;
160 unsigned long read_timer(void)
162 extern spinlock_t i8253_lock;
163 unsigned long t, flags;
166 spin_lock_irqsave(&i8253_lock, flags);
170 i |= inb(PIT_CH0) << 8;
171 spin_unlock_irqrestore(&i8253_lock, flags);
176 void __init hd_setup(char *str, int *ints)
182 if (hd_info[0].head != 0)
184 hd_info[hdind].head = ints[2];
185 hd_info[hdind].sect = ints[3];
186 hd_info[hdind].cyl = ints[1];
187 hd_info[hdind].wpcom = 0;
188 hd_info[hdind].lzone = ints[1];
189 hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);
193 static void dump_status (const char *msg, unsigned int stat)
195 char *name = CURRENT ?
196 CURRENT->rq_dev->bd_disk->disk_name :
198 #ifdef VERBOSE_ERRORS
199 printk("%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
200 if (stat & BUSY_STAT) printk("Busy ");
201 if (stat & READY_STAT) printk("DriveReady ");
202 if (stat & WRERR_STAT) printk("WriteFault ");
203 if (stat & SEEK_STAT) printk("SeekComplete ");
204 if (stat & DRQ_STAT) printk("DataRequest ");
205 if (stat & ECC_STAT) printk("CorrectedError ");
206 if (stat & INDEX_STAT) printk("Index ");
207 if (stat & ERR_STAT) printk("Error ");
209 if ((stat & ERR_STAT) == 0) {
212 hd_error = inb(HD_ERROR);
213 printk("%s: %s: error=0x%02x { ", name, msg, hd_error & 0xff);
214 if (hd_error & BBD_ERR) printk("BadSector ");
215 if (hd_error & ECC_ERR) printk("UncorrectableError ");
216 if (hd_error & ID_ERR) printk("SectorIdNotFound ");
217 if (hd_error & ABRT_ERR) printk("DriveStatusError ");
218 if (hd_error & TRK0_ERR) printk("TrackZeroNotFound ");
219 if (hd_error & MARK_ERR) printk("AddrMarkNotFound ");
221 if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
222 printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL),
223 inb(HD_CURRENT) & 0xf, inb(HD_SECTOR));
225 printk(", sector=%ld", CURRENT->sector);
230 printk("%s: %s: status=0x%02x.\n", name, msg, stat & 0xff);
231 if ((stat & ERR_STAT) == 0) {
234 hd_error = inb(HD_ERROR);
235 printk("%s: %s: error=0x%02x.\n", name, msg, hd_error & 0xff);
240 void check_status(void)
242 int i = inb(HD_STATUS);
245 dump_status("check_status", i);
250 static int controller_busy(void)
252 int retries = 100000;
253 unsigned char status;
256 status = inb(HD_STATUS);
257 } while ((status & BUSY_STAT) && --retries);
261 static int status_ok(void)
263 unsigned char status = inb(HD_STATUS);
265 if (status & BUSY_STAT)
266 return 1; /* Ancient, but does it make sense??? */
267 if (status & WRERR_STAT)
269 if (!(status & READY_STAT))
271 if (!(status & SEEK_STAT))
276 static int controller_ready(unsigned int drive, unsigned int head)
281 if (controller_busy() & BUSY_STAT)
283 outb(0xA0 | (drive<<4) | head, HD_CURRENT);
290 static void hd_out(struct hd_i_struct *disk,
296 void (*intr_addr)(void))
301 while (read_timer() - last_req < HD_DELAY)
306 if (!controller_ready(disk->unit, head)) {
310 SET_HANDLER(intr_addr);
311 outb(disk->ctl,HD_CMD);
313 outb(disk->wpcom>>2, port); port += 2;
314 outb(nsect, port); port += 2;
315 outb(sect, port); port += 2;
316 outb(cyl, port); port += 2;
317 outb(cyl>>8, port); port += 2;
318 outb(0xA0|(disk->unit<<4)|head, port); port += 2;
322 static void hd_request (void);
324 static int drive_busy(void)
329 for (i = 0; i < 500000 ; i++) {
331 if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
334 dump_status("reset timed out", c);
338 static void reset_controller(void)
343 for(i = 0; i < 1000; i++) barrier();
344 outb(hd_info[0].ctl & 0x0f,HD_CMD);
345 for(i = 0; i < 1000; i++) barrier();
347 printk("hd: controller still busy\n");
348 else if ((hd_error = inb(HD_ERROR)) != 1)
349 printk("hd: controller reset failed: %02x\n",hd_error);
352 static void reset_hd(void)
367 struct hd_i_struct *disk = &hd_info[i];
368 disk->special_op = disk->recalibrate = 1;
369 hd_out(disk, disk->sect, disk->sect, disk->head-1,
370 disk->cyl, WIN_SPECIFY, &reset_hd);
378 * Ok, don't know what to do with the unexpected interrupts: on some machines
379 * doing a reset and a retry seems to result in an eternal loop. Right now I
380 * ignore it, and just set the timeout.
382 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
383 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
384 * "good", we just ignore the interrupt completely.
386 void unexpected_hd_interrupt(void)
388 unsigned int stat = inb(HD_STATUS);
390 if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
391 dump_status ("unexpected interrupt", stat);
397 * bad_rw_intr() now tries to be a bit smarter and does things
398 * according to the error returned by the controller.
399 * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
401 static void bad_rw_intr(void)
403 struct request *req = CURRENT;
404 struct hd_i_struct *disk;
408 disk = req->rq_disk->private_data;
409 if (++req->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
411 disk->special_op = disk->recalibrate = 1;
412 } else if (req->errors % RESET_FREQ == 0)
414 else if ((hd_error & TRK0_ERR) || req->errors % RECAL_FREQ == 0)
415 disk->special_op = disk->recalibrate = 1;
416 /* Otherwise just retry */
419 static inline int wait_DRQ(void)
421 int retries = 100000, stat;
423 while (--retries > 0)
424 if ((stat = inb(HD_STATUS)) & DRQ_STAT)
426 dump_status("wait_DRQ", stat);
430 static void read_intr(void)
432 int i, retries = 100000;
436 i = (unsigned) inb(HD_STATUS);
443 } while (--retries > 0);
444 dump_status("read_intr", i);
450 insw(HD_DATA,req->buffer,256);
454 i = --req->nr_sectors;
455 --req->current_nr_sectors;
457 printk("%s: read: sector %ld, remaining = %ld, buffer=%p\n",
458 req->rq_disk->disk_name, req->sector, req->nr_sectors,
461 if (req->current_nr_sectors <= 0)
464 SET_HANDLER(&read_intr);
467 (void) inb(HD_STATUS);
469 last_req = read_timer();
476 static void write_intr(void)
479 int retries = 100000;
480 struct request *req = CURRENT;
483 i = (unsigned) inb(HD_STATUS);
488 if ((req->nr_sectors <= 1) || (i & DRQ_STAT))
490 } while (--retries > 0);
491 dump_status("write_intr", i);
497 i = --req->nr_sectors;
498 --req->current_nr_sectors;
500 if (!i || (req->bio && req->current_nr_sectors < 1))
503 SET_HANDLER(&write_intr);
504 outsw(HD_DATA,req->buffer,256);
508 last_req = read_timer();
515 static void recal_intr(void)
519 last_req = read_timer();
525 * This is another of the error-routines I don't know what to do with. The
526 * best idea seems to just set reset, and start all over again.
528 static void hd_times_out(unsigned long dummy)
538 printk("%s: timeout\n", CURRENT->rq_disk->disk_name);
539 if (++CURRENT->errors >= MAX_ERRORS) {
541 printk("%s: too many errors\n", CURRENT->rq_disk->disk_name);
543 end_request(CURRENT, 0);
550 int do_special_op(struct hd_i_struct *disk, struct request *req)
552 if (disk->recalibrate) {
553 disk->recalibrate = 0;
554 hd_out(disk, disk->sect,0,0,0,WIN_RESTORE,&recal_intr);
557 if (disk->head > 16) {
558 printk ("%s: cannot handle device with more than 16 heads - giving up\n", req->rq_disk->disk_name);
561 disk->special_op = 0;
566 * The driver enables interrupts as much as possible. In order to do this,
567 * (a) the device-interrupt is disabled before entering hd_request(),
568 * and (b) the timeout-interrupt is disabled before the sti().
570 * Interrupts are still masked (by default) whenever we are exchanging
571 * data/cmds with a drive, because some drives seem to have very poor
572 * tolerance for latency during I/O. The IDE driver has support to unmask
573 * interrupts for non-broken hardware, so use that driver if required.
575 static void hd_request(void)
577 unsigned int block, nsect, sec, track, head, cyl;
578 struct hd_i_struct *disk;
584 del_timer(&device_timer);
598 disk = req->rq_disk->private_data;
600 nsect = req->nr_sectors;
601 if (block >= get_capacity(req->rq_disk) ||
602 ((block+nsect) > get_capacity(req->rq_disk))) {
603 printk("%s: bad access: block=%d, count=%d\n",
604 req->rq_disk->disk_name, block, nsect);
609 if (disk->special_op) {
610 if (do_special_op(disk, req))
614 sec = block % disk->sect + 1;
615 track = block / disk->sect;
616 head = track % disk->head;
617 cyl = track / disk->head;
619 printk("%s: %sing: CHS=%d/%d/%d, sectors=%d, buffer=%p\n",
620 req->rq_disk->disk_name, (req->cmd == READ)?"read":"writ",
621 cyl, head, sec, nsect, req->buffer);
623 if (req->flags & REQ_CMD) {
624 switch (rq_data_dir(req)) {
626 hd_out(disk,nsect,sec,head,cyl,WIN_READ,&read_intr);
631 hd_out(disk,nsect,sec,head,cyl,WIN_WRITE,&write_intr);
638 outsw(HD_DATA,req->buffer,256);
641 printk("unknown hd-command\n");
648 static void do_hd_request (request_queue_t * q)
655 static int hd_ioctl(struct inode * inode, struct file * file,
656 unsigned int cmd, unsigned long arg)
658 struct hd_i_struct *disk = inode->i_bdev->bd_disk->private_data;
659 struct hd_geometry *loc = (struct hd_geometry *) arg;
660 struct hd_geometry g;
662 if (cmd != HDIO_GETGEO)
666 g.heads = disk->head;
667 g.sectors = disk->sect;
668 g.cylinders = disk->cyl;
669 g.start = get_start_sect(inode->i_bdev);
670 return copy_to_user(loc, &g, sizeof g) ? -EFAULT : 0;
674 * Releasing a block device means we sync() it, so that it can safely
675 * be forgotten about...
678 static void hd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
680 void (*handler)(void) = do_hd;
683 del_timer(&device_timer);
685 handler = unexpected_hd_interrupt;
690 static struct block_device_operations hd_fops = {
695 * This is the hard disk IRQ description. The SA_INTERRUPT in sa_flags
696 * means we run the IRQ-handler with interrupts disabled: this is bad for
697 * interrupt latency, but anything else has led to problems on some
700 * We enable interrupts in some of the routines after making sure it's
704 static int __init hd_init(void)
707 if (register_blkdev(HD_MAJOR,"hd")) {
708 printk("hd: unable to get major %d for hard disk\n",HD_MAJOR);
711 hd_queue = blk_init_queue(do_hd_request, &hd_lock);
713 unregister_blkdev(HD_MAJOR,"hd");
716 blk_queue_max_sectors(hd_queue, 255);
717 init_timer(&device_timer);
718 device_timer.function = hd_times_out;
719 blk_queue_hardsect_size(hd_queue, 512);
723 extern struct drive_info drive_info;
724 unsigned char *BIOS = (unsigned char *) &drive_info;
726 #ifndef CONFIG_X86_PC9800
730 for (drive=0 ; drive<2 ; drive++) {
731 hd_info[drive].cyl = *(unsigned short *) BIOS;
732 hd_info[drive].head = *(3+BIOS);
733 hd_info[drive].sect = *(2+BIOS);
734 hd_info[drive].wpcom = 0;
735 hd_info[drive].ctl = *(3+BIOS) > 8 ? 8 : 0;
736 hd_info[drive].lzone = *(unsigned short *) BIOS;
737 if (hd_info[drive].cyl && NR_HD == drive)
743 #endif /* __i386__ */
746 /* We don't know anything about the drive. This means
747 * that you *MUST* specify the drive parameters to the
750 printk("hd: no drives specified - use hd=cyl,head,sectors"
751 " on kernel command line\n");
757 for (drive=0 ; drive < NR_HD ; drive++) {
758 struct gendisk *disk = alloc_disk(64);
759 struct hd_i_struct *p = &hd_info[drive];
762 disk->major = HD_MAJOR;
763 disk->first_minor = drive << 6;
764 disk->fops = &hd_fops;
765 sprintf(disk->disk_name, "hd%c", 'a'+drive);
766 disk->private_data = p;
767 set_capacity(disk, p->head * p->sect * p->cyl);
768 disk->queue = hd_queue;
770 hd_gendisk[drive] = disk;
771 printk ("%s: %luMB, CHS=%d/%d/%d\n",
772 disk->disk_name, (unsigned long)get_capacity(disk)/2048,
773 p->cyl, p->head, p->sect);
776 if (request_irq(HD_IRQ, hd_interrupt, SA_INTERRUPT, "hd", NULL)) {
777 printk("hd: unable to get IRQ%d for the hard disk driver\n",
782 if (!request_region(HD_DATA, 2, "hd(data)")) {
783 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);
785 free_irq(HD_IRQ, NULL);
789 if (!request_region(HD_DATA + 2, 1, "hd"))
791 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);
795 if (!request_region(HD_DATA + 4, 1, "hd"))
797 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);
801 if (!request_region(HD_DATA + 6, 1, "hd"))
803 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);
807 if (!request_region(HD_DATA + 8, 1, "hd"))
809 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);
813 if (!request_region(HD_DATA + 10, 1, "hd"))
815 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);
819 if (!request_region(HD_DATA + 12, 1, "hd"))
821 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);
825 if (!request_region(HD_CMD, 1, "hd(cmd)"))
827 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_CMD);
831 if (!request_region(HD_CMD + 2, 1, "hd(cmd)"))
833 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_CMD);
837 for(drive=0; drive < NR_HD; drive++)
838 add_disk(hd_gendisk[drive]);
842 release_region(HD_CMD, 1);
844 release_region(HD_DATA + 12, 1);
846 release_region(HD_DATA + 10, 1);
848 release_region(HD_DATA + 8, 1);
850 release_region(HD_DATA + 6, 1);
852 release_region(HD_DATA + 4, 1);
854 release_region(HD_DATA + 2, 1);
856 release_region(HD_DATA, 2);
857 free_irq(HD_IRQ, NULL);
859 for (drive = 0; drive < NR_HD; drive++)
860 put_disk(hd_gendisk[drive]);
863 del_timer(&device_timer);
864 unregister_blkdev(HD_MAJOR,"hd");
865 blk_cleanup_queue(hd_queue);
869 put_disk(hd_gendisk[drive]);
873 static int parse_hd_setup (char *line) {
876 (void) get_options(line, ARRAY_SIZE(ints), ints);
877 hd_setup(NULL, ints);
881 __setup("hd=", parse_hd_setup);
883 module_init(hd_init);