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/blkdev.h>
30 #include <linux/errno.h>
31 #include <linux/signal.h>
32 #include <linux/interrupt.h>
33 #include <linux/timer.h>
35 #include <linux/kernel.h>
36 #include <linux/genhd.h>
37 #include <linux/slab.h>
38 #include <linux/string.h>
39 #include <linux/ioport.h>
40 #include <linux/init.h>
41 #include <linux/blkpg.h>
42 #include <linux/ata.h>
43 #include <linux/hdreg.h>
47 #define REALLY_SLOW_IO
48 #include <asm/system.h>
50 #include <asm/uaccess.h>
57 #define HD_IRQ IRQ_HARDDISK
60 /* Hd controller regster ports */
62 #define HD_DATA 0x1f0 /* _CTL when writing */
63 #define HD_ERROR 0x1f1 /* see err-bits */
64 #define HD_NSECTOR 0x1f2 /* nr of sectors to read/write */
65 #define HD_SECTOR 0x1f3 /* starting sector */
66 #define HD_LCYL 0x1f4 /* starting cylinder */
67 #define HD_HCYL 0x1f5 /* high byte of starting cyl */
68 #define HD_CURRENT 0x1f6 /* 101dhhhh , d=drive, hhhh=head */
69 #define HD_STATUS 0x1f7 /* see status-bits */
70 #define HD_FEATURE HD_ERROR /* same io address, read=error, write=feature */
71 #define HD_PRECOMP HD_FEATURE /* obsolete use of this port - predates IDE */
72 #define HD_COMMAND HD_STATUS /* same io address, read=status, write=cmd */
74 #define HD_CMD 0x3f6 /* used for resets */
75 #define HD_ALTSTATUS 0x3f6 /* same as HD_STATUS but doesn't clear irq */
77 /* Bits of HD_STATUS */
79 #define INDEX_STAT 0x02
80 #define ECC_STAT 0x04 /* Corrected error */
82 #define SEEK_STAT 0x10
83 #define SERVICE_STAT SEEK_STAT
84 #define WRERR_STAT 0x20
85 #define READY_STAT 0x40
86 #define BUSY_STAT 0x80
88 /* Bits for HD_ERROR */
89 #define MARK_ERR 0x01 /* Bad address mark */
90 #define TRK0_ERR 0x02 /* couldn't find track 0 */
91 #define ABRT_ERR 0x04 /* Command aborted */
92 #define MCR_ERR 0x08 /* media change request */
93 #define ID_ERR 0x10 /* ID field not found */
94 #define MC_ERR 0x20 /* media changed */
95 #define ECC_ERR 0x40 /* Uncorrectable ECC error */
96 #define BBD_ERR 0x80 /* pre-EIDE meaning: block marked bad */
97 #define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */
99 static DEFINE_SPINLOCK(hd_lock);
100 static struct request_queue *hd_queue;
102 #define MAJOR_NR HD_MAJOR
103 #define QUEUE (hd_queue)
104 #define CURRENT elv_next_request(hd_queue)
106 #define TIMEOUT_VALUE (6*HZ)
109 #define MAX_ERRORS 16 /* Max read/write errors/sector */
110 #define RESET_FREQ 8 /* Reset controller every 8th retry */
111 #define RECAL_FREQ 4 /* Recalibrate every 4th retry */
114 #define STAT_OK (READY_STAT|SEEK_STAT)
115 #define OK_STATUS(s) (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
117 static void recal_intr(void);
118 static void bad_rw_intr(void);
124 * This struct defines the HD's and their types.
127 unsigned int head, sect, cyl, wpcom, lzone, ctl;
134 static struct hd_i_struct hd_info[] = { HD_TYPE };
135 static int NR_HD = ARRAY_SIZE(hd_info);
137 static struct hd_i_struct hd_info[MAX_HD];
141 static struct gendisk *hd_gendisk[MAX_HD];
143 static struct timer_list device_timer;
145 #define TIMEOUT_VALUE (6*HZ)
149 mod_timer(&device_timer, jiffies + TIMEOUT_VALUE); \
152 static void (*do_hd)(void) = NULL;
153 #define SET_HANDLER(x) \
154 if ((do_hd = (x)) != NULL) \
157 del_timer(&device_timer);
162 #include <asm/i8253.h>
164 unsigned long last_req;
166 unsigned long read_timer(void)
168 unsigned long t, flags;
171 spin_lock_irqsave(&i8253_lock, flags);
176 spin_unlock_irqrestore(&i8253_lock, flags);
181 static void __init hd_setup(char *str, int *ints)
187 if (hd_info[0].head != 0)
189 hd_info[hdind].head = ints[2];
190 hd_info[hdind].sect = ints[3];
191 hd_info[hdind].cyl = ints[1];
192 hd_info[hdind].wpcom = 0;
193 hd_info[hdind].lzone = ints[1];
194 hd_info[hdind].ctl = (ints[2] > 8 ? 8 : 0);
198 static void dump_status(const char *msg, unsigned int stat)
202 name = CURRENT->rq_disk->disk_name;
204 #ifdef VERBOSE_ERRORS
205 printk("%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
206 if (stat & BUSY_STAT) printk("Busy ");
207 if (stat & READY_STAT) printk("DriveReady ");
208 if (stat & WRERR_STAT) printk("WriteFault ");
209 if (stat & SEEK_STAT) printk("SeekComplete ");
210 if (stat & DRQ_STAT) printk("DataRequest ");
211 if (stat & ECC_STAT) printk("CorrectedError ");
212 if (stat & INDEX_STAT) printk("Index ");
213 if (stat & ERR_STAT) printk("Error ");
215 if ((stat & ERR_STAT) == 0) {
218 hd_error = inb(HD_ERROR);
219 printk("%s: %s: error=0x%02x { ", name, msg, hd_error & 0xff);
220 if (hd_error & BBD_ERR) printk("BadSector ");
221 if (hd_error & ECC_ERR) printk("UncorrectableError ");
222 if (hd_error & ID_ERR) printk("SectorIdNotFound ");
223 if (hd_error & ABRT_ERR) printk("DriveStatusError ");
224 if (hd_error & TRK0_ERR) printk("TrackZeroNotFound ");
225 if (hd_error & MARK_ERR) printk("AddrMarkNotFound ");
227 if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
228 printk(", CHS=%d/%d/%d", (inb(HD_HCYL)<<8) + inb(HD_LCYL),
229 inb(HD_CURRENT) & 0xf, inb(HD_SECTOR));
231 printk(", sector=%ld", CURRENT->sector);
236 printk("%s: %s: status=0x%02x.\n", name, msg, stat & 0xff);
237 if ((stat & ERR_STAT) == 0) {
240 hd_error = inb(HD_ERROR);
241 printk("%s: %s: error=0x%02x.\n", name, msg, hd_error & 0xff);
246 static void check_status(void)
248 int i = inb_p(HD_STATUS);
251 dump_status("check_status", i);
256 static int controller_busy(void)
258 int retries = 100000;
259 unsigned char status;
262 status = inb_p(HD_STATUS);
263 } while ((status & BUSY_STAT) && --retries);
267 static int status_ok(void)
269 unsigned char status = inb_p(HD_STATUS);
271 if (status & BUSY_STAT)
272 return 1; /* Ancient, but does it make sense??? */
273 if (status & WRERR_STAT)
275 if (!(status & READY_STAT))
277 if (!(status & SEEK_STAT))
282 static int controller_ready(unsigned int drive, unsigned int head)
287 if (controller_busy() & BUSY_STAT)
289 outb_p(0xA0 | (drive<<4) | head, HD_CURRENT);
296 static void hd_out(struct hd_i_struct *disk,
302 void (*intr_addr)(void))
307 while (read_timer() - last_req < HD_DELAY)
312 if (!controller_ready(disk->unit, head)) {
316 SET_HANDLER(intr_addr);
317 outb_p(disk->ctl, HD_CMD);
319 outb_p(disk->wpcom >> 2, ++port);
320 outb_p(nsect, ++port);
321 outb_p(sect, ++port);
323 outb_p(cyl >> 8, ++port);
324 outb_p(0xA0 | (disk->unit << 4) | head, ++port);
328 static void hd_request (void);
330 static int drive_busy(void)
335 for (i = 0; i < 500000 ; i++) {
336 c = inb_p(HD_STATUS);
337 if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) == STAT_OK)
340 dump_status("reset timed out", c);
344 static void reset_controller(void)
349 for (i = 0; i < 1000; i++) barrier();
350 outb_p(hd_info[0].ctl & 0x0f, HD_CMD);
351 for (i = 0; i < 1000; i++) barrier();
353 printk("hd: controller still busy\n");
354 else if ((hd_error = inb(HD_ERROR)) != 1)
355 printk("hd: controller reset failed: %02x\n", hd_error);
358 static void reset_hd(void)
373 struct hd_i_struct *disk = &hd_info[i];
374 disk->special_op = disk->recalibrate = 1;
375 hd_out(disk, disk->sect, disk->sect, disk->head-1,
376 disk->cyl, ATA_CMD_INIT_DEV_PARAMS, &reset_hd);
384 * Ok, don't know what to do with the unexpected interrupts: on some machines
385 * doing a reset and a retry seems to result in an eternal loop. Right now I
386 * ignore it, and just set the timeout.
388 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
389 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
390 * "good", we just ignore the interrupt completely.
392 static void unexpected_hd_interrupt(void)
394 unsigned int stat = inb_p(HD_STATUS);
396 if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
397 dump_status("unexpected interrupt", stat);
403 * bad_rw_intr() now tries to be a bit smarter and does things
404 * according to the error returned by the controller.
405 * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
407 static void bad_rw_intr(void)
409 struct request *req = CURRENT;
411 struct hd_i_struct *disk = req->rq_disk->private_data;
412 if (++req->errors >= MAX_ERRORS || (hd_error & BBD_ERR)) {
414 disk->special_op = disk->recalibrate = 1;
415 } else if (req->errors % RESET_FREQ == 0)
417 else if ((hd_error & TRK0_ERR) || req->errors % RECAL_FREQ == 0)
418 disk->special_op = disk->recalibrate = 1;
419 /* Otherwise just retry */
423 static inline int wait_DRQ(void)
428 for (retries = 0; retries < 100000; retries++) {
429 stat = inb_p(HD_STATUS);
433 dump_status("wait_DRQ", stat);
437 static void read_intr(void)
440 int i, retries = 100000;
443 i = (unsigned) inb_p(HD_STATUS);
450 } while (--retries > 0);
451 dump_status("read_intr", i);
457 insw(HD_DATA, req->buffer, 256);
461 i = --req->nr_sectors;
462 --req->current_nr_sectors;
464 printk("%s: read: sector %ld, remaining = %ld, buffer=%p\n",
465 req->rq_disk->disk_name, req->sector, req->nr_sectors,
468 if (req->current_nr_sectors <= 0)
471 SET_HANDLER(&read_intr);
474 (void) inb_p(HD_STATUS);
476 last_req = read_timer();
478 if (elv_next_request(QUEUE))
483 static void write_intr(void)
485 struct request *req = CURRENT;
487 int retries = 100000;
490 i = (unsigned) inb_p(HD_STATUS);
495 if ((req->nr_sectors <= 1) || (i & DRQ_STAT))
497 } while (--retries > 0);
498 dump_status("write_intr", i);
504 i = --req->nr_sectors;
505 --req->current_nr_sectors;
507 if (!i || (req->bio && req->current_nr_sectors <= 0))
510 SET_HANDLER(&write_intr);
511 outsw(HD_DATA, req->buffer, 256);
515 last_req = read_timer();
522 static void recal_intr(void)
526 last_req = read_timer();
532 * This is another of the error-routines I don't know what to do with. The
533 * best idea seems to just set reset, and start all over again.
535 static void hd_times_out(unsigned long dummy)
547 name = CURRENT->rq_disk->disk_name;
548 printk("%s: timeout\n", name);
549 if (++CURRENT->errors >= MAX_ERRORS) {
551 printk("%s: too many errors\n", name);
553 end_request(CURRENT, 0);
560 static int do_special_op(struct hd_i_struct *disk, struct request *req)
562 if (disk->recalibrate) {
563 disk->recalibrate = 0;
564 hd_out(disk, disk->sect, 0, 0, 0, ATA_CMD_RESTORE, &recal_intr);
567 if (disk->head > 16) {
568 printk("%s: cannot handle device with more than 16 heads - giving up\n", req->rq_disk->disk_name);
571 disk->special_op = 0;
576 * The driver enables interrupts as much as possible. In order to do this,
577 * (a) the device-interrupt is disabled before entering hd_request(),
578 * and (b) the timeout-interrupt is disabled before the sti().
580 * Interrupts are still masked (by default) whenever we are exchanging
581 * data/cmds with a drive, because some drives seem to have very poor
582 * tolerance for latency during I/O. The IDE driver has support to unmask
583 * interrupts for non-broken hardware, so use that driver if required.
585 static void hd_request(void)
587 unsigned int block, nsect, sec, track, head, cyl;
588 struct hd_i_struct *disk;
594 del_timer(&device_timer);
608 disk = req->rq_disk->private_data;
610 nsect = req->nr_sectors;
611 if (block >= get_capacity(req->rq_disk) ||
612 ((block+nsect) > get_capacity(req->rq_disk))) {
613 printk("%s: bad access: block=%d, count=%d\n",
614 req->rq_disk->disk_name, block, nsect);
619 if (disk->special_op) {
620 if (do_special_op(disk, req))
624 sec = block % disk->sect + 1;
625 track = block / disk->sect;
626 head = track % disk->head;
627 cyl = track / disk->head;
629 printk("%s: %sing: CHS=%d/%d/%d, sectors=%d, buffer=%p\n",
630 req->rq_disk->disk_name,
631 req_data_dir(req) == READ ? "read" : "writ",
632 cyl, head, sec, nsect, req->buffer);
634 if (blk_fs_request(req)) {
635 switch (rq_data_dir(req)) {
637 hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_READ,
643 hd_out(disk, nsect, sec, head, cyl, ATA_CMD_PIO_WRITE,
651 outsw(HD_DATA, req->buffer, 256);
654 printk("unknown hd-command\n");
661 static void do_hd_request(struct request_queue *q)
668 static int hd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
670 struct hd_i_struct *disk = bdev->bd_disk->private_data;
672 geo->heads = disk->head;
673 geo->sectors = disk->sect;
674 geo->cylinders = disk->cyl;
679 * Releasing a block device means we sync() it, so that it can safely
680 * be forgotten about...
683 static irqreturn_t hd_interrupt(int irq, void *dev_id)
685 void (*handler)(void) = do_hd;
688 del_timer(&device_timer);
690 handler = unexpected_hd_interrupt;
696 static struct block_device_operations hd_fops = {
701 * This is the hard disk IRQ description. The IRQF_DISABLED in sa_flags
702 * means we run the IRQ-handler with interrupts disabled: this is bad for
703 * interrupt latency, but anything else has led to problems on some
706 * We enable interrupts in some of the routines after making sure it's
710 static int __init hd_init(void)
714 if (register_blkdev(MAJOR_NR, "hd"))
717 hd_queue = blk_init_queue(do_hd_request, &hd_lock);
719 unregister_blkdev(MAJOR_NR, "hd");
723 blk_queue_max_sectors(hd_queue, 255);
724 init_timer(&device_timer);
725 device_timer.function = hd_times_out;
726 blk_queue_hardsect_size(hd_queue, 512);
730 * We don't know anything about the drive. This means
731 * that you *MUST* specify the drive parameters to the
734 * If we were on an i386, we used to read this info from
735 * the BIOS or CMOS. This doesn't work all that well,
736 * since this assumes that this is a primary or secondary
737 * drive, and if we're using this legacy driver, it's
738 * probably an auxilliary controller added to recover
739 * legacy data off an ST-506 drive. Either way, it's
740 * definitely safest to have the user explicitly specify
743 printk("hd: no drives specified - use hd=cyl,head,sectors"
744 " on kernel command line\n");
748 for (drive = 0 ; drive < NR_HD ; drive++) {
749 struct gendisk *disk = alloc_disk(64);
750 struct hd_i_struct *p = &hd_info[drive];
753 disk->major = MAJOR_NR;
754 disk->first_minor = drive << 6;
755 disk->fops = &hd_fops;
756 sprintf(disk->disk_name, "hd%c", 'a'+drive);
757 disk->private_data = p;
758 set_capacity(disk, p->head * p->sect * p->cyl);
759 disk->queue = hd_queue;
761 hd_gendisk[drive] = disk;
762 printk("%s: %luMB, CHS=%d/%d/%d\n",
763 disk->disk_name, (unsigned long)get_capacity(disk)/2048,
764 p->cyl, p->head, p->sect);
767 if (request_irq(HD_IRQ, hd_interrupt, IRQF_DISABLED, "hd", NULL)) {
768 printk("hd: unable to get IRQ%d for the hard disk driver\n",
772 if (!request_region(HD_DATA, 8, "hd")) {
773 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_DATA);
776 if (!request_region(HD_CMD, 1, "hd(cmd)")) {
777 printk(KERN_WARNING "hd: port 0x%x busy\n", HD_CMD);
782 for (drive = 0; drive < NR_HD; drive++)
783 add_disk(hd_gendisk[drive]);
788 release_region(HD_DATA, 8);
790 free_irq(HD_IRQ, NULL);
792 for (drive = 0; drive < NR_HD; drive++)
793 put_disk(hd_gendisk[drive]);
796 del_timer(&device_timer);
797 unregister_blkdev(MAJOR_NR, "hd");
798 blk_cleanup_queue(hd_queue);
802 put_disk(hd_gendisk[drive]);
806 static int __init parse_hd_setup(char *line)
810 (void) get_options(line, ARRAY_SIZE(ints), ints);
811 hd_setup(NULL, ints);
815 __setup("hd=", parse_hd_setup);
817 late_initcall(hd_init);