30 #include <linux/errno.h>
31 #include <linux/signal.h>
35 #include <linux/kernel.h>
37 #include <linux/string.h>
46 #define REALLY_SLOW_IO
48 #include <asm/uaccess.h>
55 #define HD_IRQ IRQ_HARDDISK
61 #define HD_ERROR 0x1f1
62 #define HD_NSECTOR 0x1f2
63 #define HD_SECTOR 0x1f3
66 #define HD_CURRENT 0x1f6
67 #define HD_STATUS 0x1f7
68 #define HD_FEATURE HD_ERROR
69 #define HD_PRECOMP HD_FEATURE
70 #define HD_COMMAND HD_STATUS
73 #define HD_ALTSTATUS 0x3f6
77 #define INDEX_STAT 0x02
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
101 #define TIMEOUT_VALUE (6*HZ)
104 #define MAX_ERRORS 16
109 #define STAT_OK (READY_STAT|SEEK_STAT)
110 #define OK_STATUS(s) (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
112 static void recal_intr(
void);
113 static void bad_rw_intr(
void);
136 static struct gendisk *hd_gendisk[
MAX_HD];
140 #define TIMEOUT_VALUE (6*HZ)
144 mod_timer(&device_timer, jiffies + TIMEOUT_VALUE); \
148 #define SET_HANDLER(x) \
149 if ((do_hd = (x)) != NULL) \
152 del_timer(&device_timer);
159 unsigned long last_req;
161 unsigned long read_timer(
void)
176 static 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 bool hd_end_request(
int err,
unsigned int bytes)
201 static bool hd_end_request_cur(
int err)
203 return hd_end_request(err, blk_rq_cur_bytes(hd_req));
206 static void dump_status(
const char *
msg,
unsigned int stat)
210 name = hd_req->rq_disk->disk_name;
212 #ifdef VERBOSE_ERRORS
213 printk(
"%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
223 if ((stat & ERR_STAT) == 0) {
227 printk(
"%s: %s: error=0x%02x { ", name, msg, hd_error & 0xff);
235 if (hd_error & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
239 printk(
", sector=%ld", blk_rq_pos(hd_req));
244 printk(
"%s: %s: status=0x%02x.\n", name, msg, stat & 0xff);
245 if ((stat & ERR_STAT) == 0) {
249 printk(
"%s: %s: error=0x%02x.\n", name, msg, hd_error & 0xff);
254 static void check_status(
void)
259 dump_status(
"check_status", i);
264 static int controller_busy(
void)
271 }
while ((status & BUSY_STAT) && --retries);
275 static int status_ok(
void)
279 if (status & BUSY_STAT)
281 if (status & WRERR_STAT)
283 if (!(status & READY_STAT))
285 if (!(status & SEEK_STAT))
290 static int controller_ready(
unsigned int drive,
unsigned int head)
295 if (controller_busy() & BUSY_STAT)
310 void (*intr_addr)(
void))
315 while (read_timer() - last_req <
HD_DELAY)
320 if (!controller_ready(disk->
unit, head)) {
332 outb_p(0xA0 | (disk->
unit << 4) | head, ++port);
336 static void hd_request (
void);
338 static int drive_busy(
void)
343 for (i = 0; i < 500000 ; i++) {
345 if ((c & (BUSY_STAT | READY_STAT | SEEK_STAT)) ==
STAT_OK)
348 dump_status(
"reset timed out", c);
352 static void reset_controller(
void)
357 for (i = 0; i < 1000; i++)
barrier();
359 for (i = 0; i < 1000; i++)
barrier();
361 printk(
"hd: controller still busy\n");
363 printk(
"hd: controller reset failed: %02x\n", hd_error);
366 static void reset_hd(
void)
400 static void unexpected_hd_interrupt(
void)
404 if (stat & (BUSY_STAT|DRQ_STAT|ECC_STAT|ERR_STAT)) {
405 dump_status(
"unexpected interrupt", stat);
415 static void bad_rw_intr(
void)
420 struct hd_i_struct *disk = req->rq_disk->private_data;
421 if (++req->errors >=
MAX_ERRORS || (hd_error & BBD_ERR)) {
422 hd_end_request_cur(-
EIO);
426 else if ((hd_error & TRK0_ERR) || req->errors %
RECAL_FREQ == 0)
432 static inline int wait_DRQ(
void)
437 for (retries = 0; retries < 100000; retries++) {
442 dump_status(
"wait_DRQ", stat);
446 static void read_intr(
void)
449 int i, retries = 100000;
459 }
while (--retries > 0);
460 dump_status(
"read_intr", i);
469 printk(
"%s: read: sector %ld, remaining = %u, buffer=%p\n",
470 req->rq_disk->disk_name, blk_rq_pos(req) + 1,
471 blk_rq_sectors(req) - 1, req->buffer+512);
473 if (hd_end_request(0, 512)) {
480 last_req = read_timer();
485 static void write_intr(
void)
489 int retries = 100000;
497 if ((blk_rq_sectors(req) <= 1) || (i &
DRQ_STAT))
499 }
while (--retries > 0);
500 dump_status(
"write_intr", i);
506 if (hd_end_request(0, 512)) {
513 last_req = read_timer();
518 static void recal_intr(
void)
522 last_req = read_timer();
531 static void hd_times_out(
unsigned long dummy)
540 spin_lock_irq(hd_queue->queue_lock);
542 name = hd_req->rq_disk->disk_name;
543 printk(
"%s: timeout\n", name);
546 printk(
"%s: too many errors\n", name);
548 hd_end_request_cur(-
EIO);
551 spin_unlock_irq(hd_queue->queue_lock);
561 if (disk->
head > 16) {
562 printk(
"%s: cannot handle device with more than 16 heads - giving up\n", req->rq_disk->disk_name);
563 hd_end_request_cur(-
EIO);
579 static void hd_request(
void)
603 disk = req->rq_disk->private_data;
604 block = blk_rq_pos(req);
605 nsect = blk_rq_sectors(req);
606 if (block >= get_capacity(req->rq_disk) ||
607 ((block+nsect) > get_capacity(req->rq_disk))) {
608 printk(
"%s: bad access: block=%d, count=%d\n",
609 req->rq_disk->disk_name, block, nsect);
610 hd_end_request_cur(-
EIO);
615 if (do_special_op(disk, req))
619 sec = block % disk->
sect + 1;
620 track = block / disk->
sect;
621 head = track % disk->
head;
622 cyl = track / disk->
head;
624 printk(
"%s: %sing: CHS=%d/%d/%d, sectors=%d, buffer=%p\n",
625 req->rq_disk->disk_name,
626 req_data_dir(req) ==
READ ?
"read" :
"writ",
627 cyl, head, sec, nsect, req->buffer);
629 if (req->cmd_type == REQ_TYPE_FS) {
630 switch (rq_data_dir(req)) {
649 printk(
"unknown hd-command\n");
650 hd_end_request_cur(-
EIO);
680 spin_lock(hd_queue->queue_lock);
685 handler = unexpected_hd_interrupt;
688 spin_unlock(hd_queue->queue_lock);
693 static const struct block_device_operations hd_fops = {
707 static int __init hd_init(
void)
722 device_timer.function = hd_times_out;
740 printk(
"hd: no drives specified - use hd=cyl,head,sectors"
741 " on kernel command line\n");
745 for (drive = 0 ; drive < NR_HD ; drive++) {
751 disk->first_minor = drive << 6;
752 disk->fops = &hd_fops;
753 sprintf(disk->disk_name,
"hd%c",
'a'+drive);
754 disk->private_data =
p;
756 disk->queue = hd_queue;
758 hd_gendisk[drive] = disk;
759 printk(
"%s: %luMB, CHS=%d/%d/%d\n",
760 disk->disk_name, (
unsigned long)get_capacity(disk)/2048,
765 printk(
"hd: unable to get IRQ%d for the hard disk driver\n",
779 for (drive = 0; drive < NR_HD; drive++)
789 for (drive = 0; drive < NR_HD; drive++)
808 hd_setup(
NULL, ints);
812 __setup(
"hd=", parse_hd_setup);