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/mc146818rtc.h> /* CMOS defines */
41 #include <linux/init.h>
42 #include <linux/blkpg.h>
43 #include <linux/hdreg.h>
45 #define REALLY_SLOW_IO
46 #include <asm/system.h>
48 #include <asm/uaccess.h>
55 #define HD_IRQ IRQ_HARDDISK
58 /* Hd controller regster ports */
60 #define HD_DATA 0x1f0 /* _CTL when writing */
61 #define HD_ERROR 0x1f1 /* see err-bits */
62 #define HD_NSECTOR 0x1f2 /* nr of sectors to read/write */
63 #define HD_SECTOR 0x1f3 /* starting sector */
64 #define HD_LCYL 0x1f4 /* starting cylinder */
65 #define HD_HCYL 0x1f5 /* high byte of starting cyl */
66 #define HD_CURRENT 0x1f6 /* 101dhhhh , d=drive, hhhh=head */
67 #define HD_STATUS 0x1f7 /* see status-bits */
68 #define HD_FEATURE HD_ERROR /* same io address, read=error, write=feature */
69 #define HD_PRECOMP HD_FEATURE /* obsolete use of this port - predates IDE */
70 #define HD_COMMAND HD_STATUS /* same io address, read=status, write=cmd */
72 #define HD_CMD 0x3f6 /* used for resets */
73 #define HD_ALTSTATUS 0x3f6 /* same as HD_STATUS but doesn't clear irq */
75 /* Bits of HD_STATUS */
77 #define INDEX_STAT 0x02
78 #define ECC_STAT 0x04 /* Corrected error */
80 #define SEEK_STAT 0x10
81 #define SERVICE_STAT SEEK_STAT
82 #define WRERR_STAT 0x20
83 #define READY_STAT 0x40
84 #define BUSY_STAT 0x80
86 /* Bits for HD_ERROR */
87 #define MARK_ERR 0x01 /* Bad address mark */
88 #define TRK0_ERR 0x02 /* couldn't find track 0 */
89 #define ABRT_ERR 0x04 /* Command aborted */
90 #define MCR_ERR 0x08 /* media change request */
91 #define ID_ERR 0x10 /* ID field not found */
92 #define MC_ERR 0x20 /* media changed */
93 #define ECC_ERR 0x40 /* Uncorrectable ECC error */
94 #define BBD_ERR 0x80 /* pre-EIDE meaning: block marked bad */
95 #define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */
97 static DEFINE_SPINLOCK(hd_lock
);
98 static struct request_queue
*hd_queue
;
100 #define MAJOR_NR HD_MAJOR
101 #define QUEUE (hd_queue)
102 #define CURRENT elv_next_request(hd_queue)
104 #define TIMEOUT_VALUE (6*HZ)
107 #define MAX_ERRORS 16 /* Max read/write errors/sector */
108 #define RESET_FREQ 8 /* Reset controller every 8th retry */
109 #define RECAL_FREQ 4 /* Recalibrate every 4th retry */
112 #define STAT_OK (READY_STAT|SEEK_STAT)
113 #define OK_STATUS(s) (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
115 static void recal_intr(void);
116 static void bad_rw_intr(void);
122 * This struct defines the HD's and their types.
125 unsigned int head
,sect
,cyl
,wpcom
,lzone
,ctl
;
132 static struct hd_i_struct hd_info
[] = { HD_TYPE
};
133 static int NR_HD
= ((sizeof (hd_info
))/(sizeof (struct hd_i_struct
)));
135 static struct hd_i_struct hd_info
[MAX_HD
];
139 static struct gendisk
*hd_gendisk
[MAX_HD
];
141 static struct timer_list device_timer
;
143 #define TIMEOUT_VALUE (6*HZ)
147 mod_timer(&device_timer, jiffies + TIMEOUT_VALUE); \
150 static void (*do_hd
)(void) = NULL
;
151 #define SET_HANDLER(x) \
152 if ((do_hd = (x)) != NULL) \
155 del_timer(&device_timer);
159 unsigned long last_req
;
161 unsigned long read_timer(void)
163 extern spinlock_t i8253_lock
;
164 unsigned long t
, flags
;
167 spin_lock_irqsave(&i8253_lock
, flags
);
172 spin_unlock_irqrestore(&i8253_lock
, flags
);
177 static void __init
hd_setup(char *str
, int *ints
)
183 if (hd_info
[0].head
!= 0)
185 hd_info
[hdind
].head
= ints
[2];
186 hd_info
[hdind
].sect
= ints
[3];
187 hd_info
[hdind
].cyl
= ints
[1];
188 hd_info
[hdind
].wpcom
= 0;
189 hd_info
[hdind
].lzone
= ints
[1];
190 hd_info
[hdind
].ctl
= (ints
[2] > 8 ? 8 : 0);
194 static void dump_status (const char *msg
, unsigned int stat
)
198 name
= CURRENT
->rq_disk
->disk_name
;
200 #ifdef VERBOSE_ERRORS
201 printk("%s: %s: status=0x%02x { ", name
, msg
, stat
& 0xff);
202 if (stat
& BUSY_STAT
) printk("Busy ");
203 if (stat
& READY_STAT
) printk("DriveReady ");
204 if (stat
& WRERR_STAT
) printk("WriteFault ");
205 if (stat
& SEEK_STAT
) printk("SeekComplete ");
206 if (stat
& DRQ_STAT
) printk("DataRequest ");
207 if (stat
& ECC_STAT
) printk("CorrectedError ");
208 if (stat
& INDEX_STAT
) printk("Index ");
209 if (stat
& ERR_STAT
) printk("Error ");
211 if ((stat
& ERR_STAT
) == 0) {
214 hd_error
= inb(HD_ERROR
);
215 printk("%s: %s: error=0x%02x { ", name
, msg
, hd_error
& 0xff);
216 if (hd_error
& BBD_ERR
) printk("BadSector ");
217 if (hd_error
& ECC_ERR
) printk("UncorrectableError ");
218 if (hd_error
& ID_ERR
) printk("SectorIdNotFound ");
219 if (hd_error
& ABRT_ERR
) printk("DriveStatusError ");
220 if (hd_error
& TRK0_ERR
) printk("TrackZeroNotFound ");
221 if (hd_error
& MARK_ERR
) printk("AddrMarkNotFound ");
223 if (hd_error
& (BBD_ERR
|ECC_ERR
|ID_ERR
|MARK_ERR
)) {
224 printk(", CHS=%d/%d/%d", (inb(HD_HCYL
)<<8) + inb(HD_LCYL
),
225 inb(HD_CURRENT
) & 0xf, inb(HD_SECTOR
));
227 printk(", sector=%ld", CURRENT
->sector
);
232 printk("%s: %s: status=0x%02x.\n", name
, msg
, stat
& 0xff);
233 if ((stat
& ERR_STAT
) == 0) {
236 hd_error
= inb(HD_ERROR
);
237 printk("%s: %s: error=0x%02x.\n", name
, msg
, hd_error
& 0xff);
242 static void check_status(void)
244 int i
= inb_p(HD_STATUS
);
247 dump_status("check_status", i
);
252 static int controller_busy(void)
254 int retries
= 100000;
255 unsigned char status
;
258 status
= inb_p(HD_STATUS
);
259 } while ((status
& BUSY_STAT
) && --retries
);
263 static int status_ok(void)
265 unsigned char status
= inb_p(HD_STATUS
);
267 if (status
& BUSY_STAT
)
268 return 1; /* Ancient, but does it make sense??? */
269 if (status
& WRERR_STAT
)
271 if (!(status
& READY_STAT
))
273 if (!(status
& SEEK_STAT
))
278 static int controller_ready(unsigned int drive
, unsigned int head
)
283 if (controller_busy() & BUSY_STAT
)
285 outb_p(0xA0 | (drive
<<4) | head
, HD_CURRENT
);
293 static void hd_out(struct hd_i_struct
*disk
,
299 void (*intr_addr
)(void))
304 while (read_timer() - last_req
< HD_DELAY
)
309 if (!controller_ready(disk
->unit
, head
)) {
313 SET_HANDLER(intr_addr
);
314 outb_p(disk
->ctl
,HD_CMD
);
316 outb_p(disk
->wpcom
>>2,++port
);
317 outb_p(nsect
,++port
);
320 outb_p(cyl
>>8,++port
);
321 outb_p(0xA0|(disk
->unit
<<4)|head
,++port
);
325 static void hd_request (void);
327 static int drive_busy(void)
332 for (i
= 0; i
< 500000 ; i
++) {
333 c
= inb_p(HD_STATUS
);
334 if ((c
& (BUSY_STAT
| READY_STAT
| SEEK_STAT
)) == STAT_OK
)
337 dump_status("reset timed out", c
);
341 static void reset_controller(void)
346 for(i
= 0; i
< 1000; i
++) barrier();
347 outb_p(hd_info
[0].ctl
& 0x0f,HD_CMD
);
348 for(i
= 0; i
< 1000; i
++) barrier();
350 printk("hd: controller still busy\n");
351 else if ((hd_error
= inb(HD_ERROR
)) != 1)
352 printk("hd: controller reset failed: %02x\n",hd_error
);
355 static void reset_hd(void)
370 struct hd_i_struct
*disk
= &hd_info
[i
];
371 disk
->special_op
= disk
->recalibrate
= 1;
372 hd_out(disk
,disk
->sect
,disk
->sect
,disk
->head
-1,
373 disk
->cyl
,WIN_SPECIFY
,&reset_hd
);
381 * Ok, don't know what to do with the unexpected interrupts: on some machines
382 * doing a reset and a retry seems to result in an eternal loop. Right now I
383 * ignore it, and just set the timeout.
385 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
386 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
387 * "good", we just ignore the interrupt completely.
389 static void unexpected_hd_interrupt(void)
391 unsigned int stat
= inb_p(HD_STATUS
);
393 if (stat
& (BUSY_STAT
|DRQ_STAT
|ECC_STAT
|ERR_STAT
)) {
394 dump_status ("unexpected interrupt", stat
);
400 * bad_rw_intr() now tries to be a bit smarter and does things
401 * according to the error returned by the controller.
402 * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
404 static void bad_rw_intr(void)
406 struct request
*req
= CURRENT
;
408 struct hd_i_struct
*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 */
420 static inline int wait_DRQ(void)
422 int retries
= 100000, stat
;
424 while (--retries
> 0)
425 if ((stat
= inb_p(HD_STATUS
)) & DRQ_STAT
)
427 dump_status("wait_DRQ", stat
);
431 static void read_intr(void)
434 int i
, retries
= 100000;
437 i
= (unsigned) inb_p(HD_STATUS
);
444 } while (--retries
> 0);
445 dump_status("read_intr", i
);
451 insw(HD_DATA
,req
->buffer
,256);
455 i
= --req
->nr_sectors
;
456 --req
->current_nr_sectors
;
458 printk("%s: read: sector %ld, remaining = %ld, buffer=%p\n",
459 req
->rq_disk
->disk_name
, req
->sector
, req
->nr_sectors
,
462 if (req
->current_nr_sectors
<= 0)
465 SET_HANDLER(&read_intr
);
468 (void) inb_p(HD_STATUS
);
470 last_req
= read_timer();
472 if (elv_next_request(QUEUE
))
477 static void write_intr(void)
479 struct request
*req
= CURRENT
;
481 int retries
= 100000;
484 i
= (unsigned) inb_p(HD_STATUS
);
489 if ((req
->nr_sectors
<= 1) || (i
& DRQ_STAT
))
491 } while (--retries
> 0);
492 dump_status("write_intr", i
);
498 i
= --req
->nr_sectors
;
499 --req
->current_nr_sectors
;
501 if (!i
|| (req
->bio
&& req
->current_nr_sectors
<= 0))
504 SET_HANDLER(&write_intr
);
505 outsw(HD_DATA
,req
->buffer
,256);
509 last_req
= read_timer();
516 static void recal_intr(void)
520 last_req
= read_timer();
526 * This is another of the error-routines I don't know what to do with. The
527 * best idea seems to just set reset, and start all over again.
529 static void hd_times_out(unsigned long dummy
)
541 name
= CURRENT
->rq_disk
->disk_name
;
542 printk("%s: timeout\n", name
);
543 if (++CURRENT
->errors
>= MAX_ERRORS
) {
545 printk("%s: too many errors\n", name
);
547 end_request(CURRENT
, 0);
554 static int do_special_op(struct hd_i_struct
*disk
, struct request
*req
)
556 if (disk
->recalibrate
) {
557 disk
->recalibrate
= 0;
558 hd_out(disk
,disk
->sect
,0,0,0,WIN_RESTORE
,&recal_intr
);
561 if (disk
->head
> 16) {
562 printk ("%s: cannot handle device with more than 16 heads - giving up\n", req
->rq_disk
->disk_name
);
565 disk
->special_op
= 0;
570 * The driver enables interrupts as much as possible. In order to do this,
571 * (a) the device-interrupt is disabled before entering hd_request(),
572 * and (b) the timeout-interrupt is disabled before the sti().
574 * Interrupts are still masked (by default) whenever we are exchanging
575 * data/cmds with a drive, because some drives seem to have very poor
576 * tolerance for latency during I/O. The IDE driver has support to unmask
577 * interrupts for non-broken hardware, so use that driver if required.
579 static void hd_request(void)
581 unsigned int block
, nsect
, sec
, track
, head
, cyl
;
582 struct hd_i_struct
*disk
;
588 del_timer(&device_timer
);
602 disk
= req
->rq_disk
->private_data
;
604 nsect
= req
->nr_sectors
;
605 if (block
>= get_capacity(req
->rq_disk
) ||
606 ((block
+nsect
) > get_capacity(req
->rq_disk
))) {
607 printk("%s: bad access: block=%d, count=%d\n",
608 req
->rq_disk
->disk_name
, block
, nsect
);
613 if (disk
->special_op
) {
614 if (do_special_op(disk
, req
))
618 sec
= block
% disk
->sect
+ 1;
619 track
= block
/ disk
->sect
;
620 head
= track
% disk
->head
;
621 cyl
= track
/ disk
->head
;
623 printk("%s: %sing: CHS=%d/%d/%d, sectors=%d, buffer=%p\n",
624 req
->rq_disk
->disk_name
, (req
->cmd
== READ
)?"read":"writ",
625 cyl
, head
, sec
, nsect
, req
->buffer
);
627 if (req
->flags
& REQ_CMD
) {
628 switch (rq_data_dir(req
)) {
630 hd_out(disk
,nsect
,sec
,head
,cyl
,WIN_READ
,&read_intr
);
635 hd_out(disk
,nsect
,sec
,head
,cyl
,WIN_WRITE
,&write_intr
);
642 outsw(HD_DATA
,req
->buffer
,256);
645 printk("unknown hd-command\n");
652 static void do_hd_request (request_queue_t
* q
)
659 static int hd_ioctl(struct inode
* inode
, struct file
* file
,
660 unsigned int cmd
, unsigned long arg
)
662 struct hd_i_struct
*disk
= inode
->i_bdev
->bd_disk
->private_data
;
663 struct hd_geometry __user
*loc
= (struct hd_geometry __user
*) arg
;
664 struct hd_geometry g
;
666 if (cmd
!= HDIO_GETGEO
)
670 g
.heads
= disk
->head
;
671 g
.sectors
= disk
->sect
;
672 g
.cylinders
= disk
->cyl
;
673 g
.start
= get_start_sect(inode
->i_bdev
);
674 return copy_to_user(loc
, &g
, sizeof g
) ? -EFAULT
: 0;
678 * Releasing a block device means we sync() it, so that it can safely
679 * be forgotten about...
682 static irqreturn_t
hd_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
684 void (*handler
)(void) = do_hd
;
687 del_timer(&device_timer
);
689 handler
= unexpected_hd_interrupt
;
695 static struct block_device_operations hd_fops
= {
700 * This is the hard disk IRQ description. The SA_INTERRUPT in sa_flags
701 * means we run the IRQ-handler with interrupts disabled: this is bad for
702 * interrupt latency, but anything else has led to problems on some
705 * We enable interrupts in some of the routines after making sure it's
709 static int __init
hd_init(void)
713 if (register_blkdev(MAJOR_NR
,"hd"))
716 hd_queue
= blk_init_queue(do_hd_request
, &hd_lock
);
718 unregister_blkdev(MAJOR_NR
,"hd");
722 blk_queue_max_sectors(hd_queue
, 255);
723 init_timer(&device_timer
);
724 device_timer
.function
= hd_times_out
;
725 blk_queue_hardsect_size(hd_queue
, 512);
729 extern struct drive_info drive_info
;
730 unsigned char *BIOS
= (unsigned char *) &drive_info
;
734 for (drive
=0 ; drive
<2 ; drive
++) {
735 hd_info
[drive
].cyl
= *(unsigned short *) BIOS
;
736 hd_info
[drive
].head
= *(2+BIOS
);
737 hd_info
[drive
].wpcom
= *(unsigned short *) (5+BIOS
);
738 hd_info
[drive
].ctl
= *(8+BIOS
);
739 hd_info
[drive
].lzone
= *(unsigned short *) (12+BIOS
);
740 hd_info
[drive
].sect
= *(14+BIOS
);
741 #ifdef does_not_work_for_everybody_with_scsi_but_helps_ibm_vp
742 if (hd_info
[drive
].cyl
&& NR_HD
== drive
)
749 We query CMOS about hard disks : it could be that
750 we have a SCSI/ESDI/etc controller that is BIOS
751 compatible with ST-506, and thus showing up in our
752 BIOS table, but not register compatible, and therefore
755 Furthermore, we will assume that our ST-506 drives
756 <if any> are the primary drives in the system, and
757 the ones reflected as drive 1 or 2.
759 The first drive is stored in the high nibble of CMOS
760 byte 0x12, the second in the low nibble. This will be
761 either a 4 bit drive type or 0xf indicating use byte 0x19
762 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS.
764 Needless to say, a non-zero value means we have
765 an AT controller hard disk for that drive.
767 Currently the rtc_lock is a bit academic since this
768 driver is non-modular, but someday... ? Paul G.
771 spin_lock_irqsave(&rtc_lock
, flags
);
772 cmos_disks
= CMOS_READ(0x12);
773 spin_unlock_irqrestore(&rtc_lock
, flags
);
775 if (cmos_disks
& 0xf0) {
776 if (cmos_disks
& 0x0f)
782 #endif /* __i386__ */
785 /* We don't know anything about the drive. This means
786 * that you *MUST* specify the drive parameters to the
789 printk("hd: no drives specified - use hd=cyl,head,sectors"
790 " on kernel command line\n");
796 for (drive
=0 ; drive
< NR_HD
; drive
++) {
797 struct gendisk
*disk
= alloc_disk(64);
798 struct hd_i_struct
*p
= &hd_info
[drive
];
801 disk
->major
= MAJOR_NR
;
802 disk
->first_minor
= drive
<< 6;
803 disk
->fops
= &hd_fops
;
804 sprintf(disk
->disk_name
, "hd%c", 'a'+drive
);
805 disk
->private_data
= p
;
806 set_capacity(disk
, p
->head
* p
->sect
* p
->cyl
);
807 disk
->queue
= hd_queue
;
809 hd_gendisk
[drive
] = disk
;
810 printk ("%s: %luMB, CHS=%d/%d/%d\n",
811 disk
->disk_name
, (unsigned long)get_capacity(disk
)/2048,
812 p
->cyl
, p
->head
, p
->sect
);
815 if (request_irq(HD_IRQ
, hd_interrupt
, SA_INTERRUPT
, "hd", NULL
)) {
816 printk("hd: unable to get IRQ%d for the hard disk driver\n",
820 if (!request_region(HD_DATA
, 8, "hd")) {
821 printk(KERN_WARNING
"hd: port 0x%x busy\n", HD_DATA
);
824 if (!request_region(HD_CMD
, 1, "hd(cmd)")) {
825 printk(KERN_WARNING
"hd: port 0x%x busy\n", HD_CMD
);
830 for(drive
=0; drive
< NR_HD
; drive
++)
831 add_disk(hd_gendisk
[drive
]);
836 release_region(HD_DATA
, 8);
838 free_irq(HD_IRQ
, NULL
);
840 for (drive
= 0; drive
< NR_HD
; drive
++)
841 put_disk(hd_gendisk
[drive
]);
844 del_timer(&device_timer
);
845 unregister_blkdev(MAJOR_NR
,"hd");
846 blk_cleanup_queue(hd_queue
);
850 put_disk(hd_gendisk
[drive
]);
854 static int __init
parse_hd_setup (char *line
) {
857 (void) get_options(line
, ARRAY_SIZE(ints
), ints
);
858 hd_setup(NULL
, ints
);
862 __setup("hd=", parse_hd_setup
);
864 module_init(hd_init
);