2 * linux/drivers/block/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)
26 /* Uncomment the following if you want verbose error reports. */
27 /* #define VERBOSE_ERRORS */
30 #include <linux/errno.h>
31 #include <linux/signal.h>
32 #include <linux/sched.h>
33 #include <linux/timer.h>
35 #include <linux/kernel.h>
36 #include <linux/hdreg.h>
37 #include <linux/genhd.h>
38 #include <linux/malloc.h>
39 #include <linux/string.h>
40 #include <linux/ioport.h>
41 #include <linux/mc146818rtc.h> /* CMOS defines */
42 #include <linux/init.h>
44 #define REALLY_SLOW_IO
45 #include <asm/system.h>
47 #include <asm/uaccess.h>
49 #define MAJOR_NR HD_MAJOR
50 #include <linux/blk.h>
52 static int revalidate_hddisk(kdev_t
, int);
56 #define MAX_ERRORS 16 /* Max read/write errors/sector */
57 #define RESET_FREQ 8 /* Reset controller every 8th retry */
58 #define RECAL_FREQ 4 /* Recalibrate every 4th retry */
61 #define STAT_OK (READY_STAT|SEEK_STAT)
62 #define OK_STATUS(s) (((s)&(STAT_OK|(BUSY_STAT|WRERR_STAT|ERR_STAT)))==STAT_OK)
64 static void recal_intr(void);
65 static void bad_rw_intr(void);
67 static char recalibrate
[MAX_HD
] = { 0, };
68 static char special_op
[MAX_HD
] = { 0, };
69 static int access_count
[MAX_HD
] = {0, };
70 static char busy
[MAX_HD
] = {0, };
71 static DECLARE_WAIT_QUEUE_HEAD(busy_wait
);
74 static int hd_error
= 0;
76 #define SUBSECTOR(block) (CURRENT->current_nr_sectors > 0)
79 * This struct defines the HD's and their types.
82 unsigned int head
,sect
,cyl
,wpcom
,lzone
,ctl
;
86 static struct hd_i_struct hd_info
[] = { HD_TYPE
};
87 static int NR_HD
= ((sizeof (hd_info
))/(sizeof (struct hd_i_struct
)));
89 static struct hd_i_struct hd_info
[] = { {0,0,0,0,0,0},{0,0,0,0,0,0} };
93 static struct hd_struct hd
[MAX_HD
<<6]={{0,0},};
94 static int hd_sizes
[MAX_HD
<<6] = {0, };
95 static int hd_blocksizes
[MAX_HD
<<6] = {0, };
98 unsigned long last_req
;
100 unsigned long read_timer(void)
102 unsigned long t
, flags
;
111 restore_flags(flags
);
116 __initfunc(void hd_setup(char *str
, int *ints
))
122 if (hd_info
[0].head
!= 0)
124 hd_info
[hdind
].head
= ints
[2];
125 hd_info
[hdind
].sect
= ints
[3];
126 hd_info
[hdind
].cyl
= ints
[1];
127 hd_info
[hdind
].wpcom
= 0;
128 hd_info
[hdind
].lzone
= ints
[1];
129 hd_info
[hdind
].ctl
= (ints
[2] > 8 ? 8 : 0);
133 static void dump_status (const char *msg
, unsigned int stat
)
138 devc
= CURRENT
? 'a' + DEVICE_NR(CURRENT
->rq_dev
) : '?';
141 #ifdef VERBOSE_ERRORS
142 printk("hd%c: %s: status=0x%02x { ", devc
, msg
, stat
& 0xff);
143 if (stat
& BUSY_STAT
) printk("Busy ");
144 if (stat
& READY_STAT
) printk("DriveReady ");
145 if (stat
& WRERR_STAT
) printk("WriteFault ");
146 if (stat
& SEEK_STAT
) printk("SeekComplete ");
147 if (stat
& DRQ_STAT
) printk("DataRequest ");
148 if (stat
& ECC_STAT
) printk("CorrectedError ");
149 if (stat
& INDEX_STAT
) printk("Index ");
150 if (stat
& ERR_STAT
) printk("Error ");
152 if ((stat
& ERR_STAT
) == 0) {
155 hd_error
= inb(HD_ERROR
);
156 printk("hd%c: %s: error=0x%02x { ", devc
, msg
, hd_error
& 0xff);
157 if (hd_error
& BBD_ERR
) printk("BadSector ");
158 if (hd_error
& ECC_ERR
) printk("UncorrectableError ");
159 if (hd_error
& ID_ERR
) printk("SectorIdNotFound ");
160 if (hd_error
& ABRT_ERR
) printk("DriveStatusError ");
161 if (hd_error
& TRK0_ERR
) printk("TrackZeroNotFound ");
162 if (hd_error
& MARK_ERR
) printk("AddrMarkNotFound ");
164 if (hd_error
& (BBD_ERR
|ECC_ERR
|ID_ERR
|MARK_ERR
)) {
165 printk(", CHS=%d/%d/%d", (inb(HD_HCYL
)<<8) + inb(HD_LCYL
),
166 inb(HD_CURRENT
) & 0xf, inb(HD_SECTOR
));
168 printk(", sector=%ld", CURRENT
->sector
);
173 printk("hd%c: %s: status=0x%02x.\n", devc
, msg
, stat
& 0xff);
174 if ((stat
& ERR_STAT
) == 0) {
177 hd_error
= inb(HD_ERROR
);
178 printk("hd%c: %s: error=0x%02x.\n", devc
, msg
, hd_error
& 0xff);
180 #endif /* verbose errors */
181 restore_flags (flags
);
184 void check_status(void)
186 int i
= inb_p(HD_STATUS
);
189 dump_status("check_status", i
);
194 static int controller_busy(void)
196 int retries
= 100000;
197 unsigned char status
;
200 status
= inb_p(HD_STATUS
);
201 } while ((status
& BUSY_STAT
) && --retries
);
205 static int status_ok(void)
207 unsigned char status
= inb_p(HD_STATUS
);
209 if (status
& BUSY_STAT
)
210 return 1; /* Ancient, but does it make sense??? */
211 if (status
& WRERR_STAT
)
213 if (!(status
& READY_STAT
))
215 if (!(status
& SEEK_STAT
))
220 static int controller_ready(unsigned int drive
, unsigned int head
)
225 if (controller_busy() & BUSY_STAT
)
227 outb_p(0xA0 | (drive
<<4) | head
, HD_CURRENT
);
234 static void hd_out(unsigned int drive
,unsigned int nsect
,unsigned int sect
,
235 unsigned int head
,unsigned int cyl
,unsigned int cmd
,
236 void (*intr_addr
)(void))
241 while (read_timer() - last_req
< HD_DELAY
)
246 if (!controller_ready(drive
, head
)) {
251 outb_p(hd_info
[drive
].ctl
,HD_CMD
);
253 outb_p(hd_info
[drive
].wpcom
>>2,++port
);
254 outb_p(nsect
,++port
);
257 outb_p(cyl
>>8,++port
);
258 outb_p(0xA0|(drive
<<4)|head
,++port
);
262 static void hd_request (void);
264 static int drive_busy(void)
269 for (i
= 0; i
< 500000 ; i
++) {
270 c
= inb_p(HD_STATUS
);
271 if ((c
& (BUSY_STAT
| READY_STAT
| SEEK_STAT
)) == STAT_OK
)
274 dump_status("reset timed out", c
);
278 static void reset_controller(void)
283 for(i
= 0; i
< 1000; i
++) barrier();
284 outb_p(hd_info
[0].ctl
& 0x0f,HD_CMD
);
285 for(i
= 0; i
< 1000; i
++) barrier();
287 printk("hd: controller still busy\n");
288 else if ((hd_error
= inb(HD_ERROR
)) != 1)
289 printk("hd: controller reset failed: %02x\n",hd_error
);
292 static void reset_hd(void)
307 special_op
[i
] = recalibrate
[i
] = 1;
308 hd_out(i
,hd_info
[i
].sect
,hd_info
[i
].sect
,hd_info
[i
].head
-1,
309 hd_info
[i
].cyl
,WIN_SPECIFY
,&reset_hd
);
317 * Ok, don't know what to do with the unexpected interrupts: on some machines
318 * doing a reset and a retry seems to result in an eternal loop. Right now I
319 * ignore it, and just set the timeout.
321 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
322 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
323 * "good", we just ignore the interrupt completely.
325 void unexpected_hd_interrupt(void)
327 unsigned int stat
= inb_p(HD_STATUS
);
329 if (stat
& (BUSY_STAT
|DRQ_STAT
|ECC_STAT
|ERR_STAT
)) {
330 dump_status ("unexpected interrupt", stat
);
336 * bad_rw_intr() now tries to be a bit smarter and does things
337 * according to the error returned by the controller.
338 * -Mika Liljeberg (liljeber@cs.Helsinki.FI)
340 static void bad_rw_intr(void)
346 dev
= DEVICE_NR(CURRENT
->rq_dev
);
347 if (++CURRENT
->errors
>= MAX_ERRORS
|| (hd_error
& BBD_ERR
)) {
349 special_op
[dev
] = recalibrate
[dev
] = 1;
350 } else if (CURRENT
->errors
% RESET_FREQ
== 0)
352 else if ((hd_error
& TRK0_ERR
) || CURRENT
->errors
% RECAL_FREQ
== 0)
353 special_op
[dev
] = recalibrate
[dev
] = 1;
354 /* Otherwise just retry */
357 static inline int wait_DRQ(void)
359 int retries
= 100000, stat
;
361 while (--retries
> 0)
362 if ((stat
= inb_p(HD_STATUS
)) & DRQ_STAT
)
364 dump_status("wait_DRQ", stat
);
368 static void read_intr(void)
370 int i
, retries
= 100000;
373 i
= (unsigned) inb_p(HD_STATUS
);
380 } while (--retries
> 0);
381 dump_status("read_intr", i
);
386 insw(HD_DATA
,CURRENT
->buffer
,256);
388 CURRENT
->buffer
+= 512;
390 i
= --CURRENT
->nr_sectors
;
391 --CURRENT
->current_nr_sectors
;
393 printk("hd%c: read: sector %ld, remaining = %ld, buffer=0x%08lx\n",
394 dev
+'a', CURRENT
->sector
, CURRENT
->nr_sectors
,
395 (unsigned long) CURRENT
->buffer
+512));
397 if (CURRENT
->current_nr_sectors
<= 0)
400 SET_INTR(&read_intr
);
403 (void) inb_p(HD_STATUS
);
405 last_req
= read_timer();
412 static void write_intr(void)
415 int retries
= 100000;
418 i
= (unsigned) inb_p(HD_STATUS
);
423 if ((CURRENT
->nr_sectors
<= 1) || (i
& DRQ_STAT
))
425 } while (--retries
> 0);
426 dump_status("write_intr", i
);
432 i
= --CURRENT
->nr_sectors
;
433 --CURRENT
->current_nr_sectors
;
434 CURRENT
->buffer
+= 512;
435 if (!i
|| (CURRENT
->bh
&& !SUBSECTOR(i
)))
438 SET_INTR(&write_intr
);
439 outsw(HD_DATA
,CURRENT
->buffer
,256);
443 last_req
= read_timer();
450 static void recal_intr(void)
454 last_req
= read_timer();
460 * This is another of the error-routines I don't know what to do with. The
461 * best idea seems to just set reset, and start all over again.
463 static void hd_times_out(void)
473 dev
= DEVICE_NR(CURRENT
->rq_dev
);
474 printk("hd%c: timeout\n", dev
+'a');
475 if (++CURRENT
->errors
>= MAX_ERRORS
) {
477 printk("hd%c: too many errors\n", dev
+'a');
486 int do_special_op (unsigned int dev
)
488 if (recalibrate
[dev
]) {
489 recalibrate
[dev
] = 0;
490 hd_out(dev
,hd_info
[dev
].sect
,0,0,0,WIN_RESTORE
,&recal_intr
);
493 if (hd_info
[dev
].head
> 16) {
494 printk ("hd%c: cannot handle device with more than 16 heads - giving up\n", dev
+'a');
502 * The driver enables interrupts as much as possible. In order to do this,
503 * (a) the device-interrupt is disabled before entering hd_request(),
504 * and (b) the timeout-interrupt is disabled before the sti().
506 * Interrupts are still masked (by default) whenever we are exchanging
507 * data/cmds with a drive, because some drives seem to have very poor
508 * tolerance for latency during I/O. The IDE driver has support to unmask
509 * interrupts for non-broken hardware, so use that driver if required.
511 static void hd_request(void)
513 unsigned int dev
, block
, nsect
, sec
, track
, head
, cyl
;
515 if (CURRENT
&& CURRENT
->rq_status
== RQ_INACTIVE
) return;
519 timer_active
&= ~(1<<HD_TIMER
);
527 dev
= MINOR(CURRENT
->rq_dev
);
528 block
= CURRENT
->sector
;
529 nsect
= CURRENT
->nr_sectors
;
530 if (dev
>= (NR_HD
<<6) || block
>= hd
[dev
].nr_sects
|| ((block
+nsect
) > hd
[dev
].nr_sects
)) {
532 if (dev
>= (NR_HD
<<6))
533 printk("hd: bad minor number: device=%s\n",
534 kdevname(CURRENT
->rq_dev
));
536 printk("hd%c: bad access: block=%d, count=%d\n",
537 (MINOR(CURRENT
->rq_dev
)>>6)+'a', block
, nsect
);
542 block
+= hd
[dev
].start_sect
;
544 if (special_op
[dev
]) {
545 if (do_special_op(dev
))
549 sec
= block
% hd_info
[dev
].sect
+ 1;
550 track
= block
/ hd_info
[dev
].sect
;
551 head
= track
% hd_info
[dev
].head
;
552 cyl
= track
/ hd_info
[dev
].head
;
554 printk("hd%c: %sing: CHS=%d/%d/%d, sectors=%d, buffer=0x%08lx\n",
555 dev
+'a', (CURRENT
->cmd
== READ
)?"read":"writ",
556 cyl
, head
, sec
, nsect
, (unsigned long) CURRENT
->buffer
);
558 if (CURRENT
->cmd
== READ
) {
559 hd_out(dev
,nsect
,sec
,head
,cyl
,WIN_READ
,&read_intr
);
564 if (CURRENT
->cmd
== WRITE
) {
565 hd_out(dev
,nsect
,sec
,head
,cyl
,WIN_WRITE
,&write_intr
);
572 outsw(HD_DATA
,CURRENT
->buffer
,256);
575 panic("unknown hd-command");
578 static void do_hd_request (void)
585 static int hd_ioctl(struct inode
* inode
, struct file
* file
,
586 unsigned int cmd
, unsigned long arg
)
588 struct hd_geometry
*loc
= (struct hd_geometry
*) arg
;
591 if ((!inode
) || !(inode
->i_rdev
))
593 dev
= DEVICE_NR(inode
->i_rdev
);
599 struct hd_geometry g
;
600 if (!loc
) return -EINVAL
;
601 g
.heads
= hd_info
[dev
].head
;
602 g
.sectors
= hd_info
[dev
].sect
;
603 g
.cylinders
= hd_info
[dev
].cyl
;
604 g
.start
= hd
[MINOR(inode
->i_rdev
)].start_sect
;
605 return copy_to_user(loc
, &g
, sizeof g
) ? -EFAULT
: 0;
608 case BLKGETSIZE
: /* Return device size */
609 if (!arg
) return -EINVAL
;
610 return put_user(hd
[MINOR(inode
->i_rdev
)].nr_sects
,
613 case BLKRRPART
: /* Re-read partition tables */
614 if (!capable(CAP_SYS_ADMIN
))
616 return revalidate_hddisk(inode
->i_rdev
, 1);
624 return blk_ioctl(inode
->i_rdev
, cmd
, arg
);
631 static int hd_open(struct inode
* inode
, struct file
* filp
)
634 target
= DEVICE_NR(inode
->i_rdev
);
639 sleep_on(&busy_wait
);
640 access_count
[target
]++;
645 * Releasing a block device means we sync() it, so that it can safely
646 * be forgotten about...
648 static int hd_release(struct inode
* inode
, struct file
* file
)
651 sync_dev(inode
->i_rdev
);
653 target
= DEVICE_NR(inode
->i_rdev
);
654 access_count
[target
]--;
658 static void hd_geninit(struct gendisk
*);
660 static struct gendisk hd_gendisk
= {
661 MAJOR_NR
, /* Major number */
662 "hd", /* Major name */
663 6, /* Bits to shift to get real from partition */
664 1 << 6, /* Number of partitions per real */
665 MAX_HD
, /* maximum number of real */
666 hd_geninit
, /* init function */
668 hd_sizes
, /* block sizes */
670 NULL
, /* internal use, not presently used */
674 static void hd_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
676 void (*handler
)(void) = DEVICE_INTR
;
679 timer_active
&= ~(1<<HD_TIMER
);
681 handler
= unexpected_hd_interrupt
;
687 * This is the hard disk IRQ description. The SA_INTERRUPT in sa_flags
688 * means we run the IRQ-handler with interrupts disabled: this is bad for
689 * interrupt latency, but anything else has led to problems on some
692 * We enable interrupts in some of the routines after making sure it's
695 static void hd_geninit(struct gendisk
*ignored
)
701 extern struct drive_info drive_info
;
702 unsigned char *BIOS
= (unsigned char *) &drive_info
;
705 for (drive
=0 ; drive
<2 ; drive
++) {
706 hd_info
[drive
].cyl
= *(unsigned short *) BIOS
;
707 hd_info
[drive
].head
= *(2+BIOS
);
708 hd_info
[drive
].wpcom
= *(unsigned short *) (5+BIOS
);
709 hd_info
[drive
].ctl
= *(8+BIOS
);
710 hd_info
[drive
].lzone
= *(unsigned short *) (12+BIOS
);
711 hd_info
[drive
].sect
= *(14+BIOS
);
712 #ifdef does_not_work_for_everybody_with_scsi_but_helps_ibm_vp
713 if (hd_info
[drive
].cyl
&& NR_HD
== drive
)
720 We query CMOS about hard disks : it could be that
721 we have a SCSI/ESDI/etc controller that is BIOS
722 compatible with ST-506, and thus showing up in our
723 BIOS table, but not register compatible, and therefore
726 Furthermore, we will assume that our ST-506 drives
727 <if any> are the primary drives in the system, and
728 the ones reflected as drive 1 or 2.
730 The first drive is stored in the high nibble of CMOS
731 byte 0x12, the second in the low nibble. This will be
732 either a 4 bit drive type or 0xf indicating use byte 0x19
733 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS.
735 Needless to say, a non-zero value means we have
736 an AT controller hard disk for that drive.
741 if ((cmos_disks
= CMOS_READ(0x12)) & 0xf0) {
742 if (cmos_disks
& 0x0f)
748 #endif /* __i386__ */
749 for (drive
=0 ; drive
< NR_HD
; drive
++) {
750 hd
[drive
<<6].nr_sects
= hd_info
[drive
].head
*
751 hd_info
[drive
].sect
* hd_info
[drive
].cyl
;
752 printk ("hd%c: %ldMB, CHS=%d/%d/%d\n", drive
+'a',
753 hd
[drive
<<6].nr_sects
/ 2048, hd_info
[drive
].cyl
,
754 hd_info
[drive
].head
, hd_info
[drive
].sect
);
757 if (request_irq(HD_IRQ
, hd_interrupt
, SA_INTERRUPT
, "hd", NULL
)) {
758 printk("hd: unable to get IRQ%d for the hard disk driver\n",HD_IRQ
);
761 request_region(HD_DATA
, 8, "hd");
762 request_region(HD_CMD
, 1, "hd(cmd)");
765 hd_gendisk
.nr_real
= NR_HD
;
767 for(drive
=0; drive
< (MAX_HD
<< 6); drive
++)
768 hd_blocksizes
[drive
] = 1024;
769 blksize_size
[MAJOR_NR
] = hd_blocksizes
;
772 static struct file_operations hd_fops
= {
773 NULL
, /* lseek - default */
774 block_read
, /* read - general block-dev read */
775 block_write
, /* write - general block-dev write */
776 NULL
, /* readdir - bad */
778 hd_ioctl
, /* ioctl */
782 hd_release
, /* release */
783 block_fsync
/* fsync */
786 __initfunc(int hd_init(void))
788 if (register_blkdev(MAJOR_NR
,"hd",&hd_fops
)) {
789 printk("hd: unable to get major %d for hard disk\n",MAJOR_NR
);
792 blk_dev
[MAJOR_NR
].request_fn
= DEVICE_REQUEST
;
793 read_ahead
[MAJOR_NR
] = 8; /* 8 sector (4kB) read-ahead */
794 hd_gendisk
.next
= gendisk_head
;
795 gendisk_head
= &hd_gendisk
;
796 timer_table
[HD_TIMER
].fn
= hd_times_out
;
800 #define DEVICE_BUSY busy[target]
801 #define USAGE access_count[target]
802 #define CAPACITY (hd_info[target].head*hd_info[target].sect*hd_info[target].cyl)
803 /* We assume that the BIOS parameters do not change, so the disk capacity
806 #define GENDISK_STRUCT hd_gendisk
809 * This routine is called to flush all partitions and partition tables
810 * for a changed disk, and then re-read the new partition table.
811 * If we are revalidating a disk because of a media change, then we
812 * enter with usage == 0. If we are using an ioctl, we automatically have
813 * usage == 1 (we need an open channel to use an ioctl :-), so this
816 static int revalidate_hddisk(kdev_t dev
, int maxusage
)
819 struct gendisk
* gdev
;
825 target
= DEVICE_NR(dev
);
826 gdev
= &GENDISK_STRUCT
;
830 if (DEVICE_BUSY
|| USAGE
> maxusage
) {
831 restore_flags(flags
);
835 restore_flags(flags
);
838 start
= target
<< gdev
->minor_shift
;
840 for (i
=max_p
- 1; i
>=0 ; i
--) {
841 int minor
= start
+ i
;
842 kdev_t devi
= MKDEV(MAJOR_NR
, minor
);
843 struct super_block
*sb
= get_super(devi
);
847 invalidate_inodes(sb
);
848 invalidate_buffers(devi
);
849 gdev
->part
[minor
].start_sect
= 0;
850 gdev
->part
[minor
].nr_sects
= 0;
857 gdev
->part
[start
].nr_sects
= CAPACITY
;
858 resetup_one_dev(gdev
, target
);